4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > mMul( const vector< vector<LL> >& A, const vector< vector<LL> >& B ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int n = A.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > C(n, vector<LL>(n)); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<n; ++j) { 4fd800b3a8 2011-02-23 kinaba: LL Cij = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<n; ++k) 4fd800b3a8 2011-02-23 kinaba: Cij += A[i][k] * B[k][j]; 4fd800b3a8 2011-02-23 kinaba: C[i][j] = Cij % 1000003; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return C; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > mPow( vector< vector<LL> > M, int t ) // t>0 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > R; 4fd800b3a8 2011-02-23 kinaba: for(; t; t>>=1, M=mMul(M,M)) 4fd800b3a8 2011-02-23 kinaba: if( t&1 ) 4fd800b3a8 2011-02-23 kinaba: R = (R.empty() ? M : mMul(R,M)); 4fd800b3a8 2011-02-23 kinaba: return R; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class DrivingAround 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int numberOfWays(vector <string> adj, int start, int finish, int time) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int n = adj.size()*5; 4fd800b3a8 2011-02-23 kinaba: vector< vector<LL> > M(n, vector<LL>(n)); 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<adj.size(); ++u) 4fd800b3a8 2011-02-23 kinaba: for(int d=0; d<4; ++d) 4fd800b3a8 2011-02-23 kinaba: M[u*5+d+1][u*5+d] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<adj.size(); ++u) 4fd800b3a8 2011-02-23 kinaba: for(int v=0; v<adj.size(); ++v) 4fd800b3a8 2011-02-23 kinaba: if( adj[u][v] != '.' ) 4fd800b3a8 2011-02-23 kinaba: M[u*5][v*5+adj[u][v]-'0'-1] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return int( mPow(M, time)[start*5][finish*5] ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: void test_case_0() { string Arr0[] = {".12", 4fd800b3a8 2011-02-23 kinaba: "2.1", 4fd800b3a8 2011-02-23 kinaba: "12."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 2; int Arg3 = 5; int Arg4 = 8; verify_case(0, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { string Arr0[] = {"....52....", 4fd800b3a8 2011-02-23 kinaba: "..5.......", 4fd800b3a8 2011-02-23 kinaba: "..........", 4fd800b3a8 2011-02-23 kinaba: ".......1..", 4fd800b3a8 2011-02-23 kinaba: "......42.2", 4fd800b3a8 2011-02-23 kinaba: "5...4.....", 4fd800b3a8 2011-02-23 kinaba: ".5...4...1", 4fd800b3a8 2011-02-23 kinaba: "......5...", 4fd800b3a8 2011-02-23 kinaba: ".3244.....", 4fd800b3a8 2011-02-23 kinaba: ".........."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; int Arg2 = 2; int Arg3 = 10; int Arg4 = 0; verify_case(1, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { string Arr0[] = {"...14....1", 4fd800b3a8 2011-02-23 kinaba: "......13..", 4fd800b3a8 2011-02-23 kinaba: ".2...4....", 4fd800b3a8 2011-02-23 kinaba: "....52.5..", 4fd800b3a8 2011-02-23 kinaba: "1.3..4....", 4fd800b3a8 2011-02-23 kinaba: ".3....35.5", 4fd800b3a8 2011-02-23 kinaba: "4......1.1", 4fd800b3a8 2011-02-23 kinaba: "..4.4.1.54", 4fd800b3a8 2011-02-23 kinaba: "....4.11.5", 4fd800b3a8 2011-02-23 kinaba: "31144.2.4."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; int Arg2 = 2; int Arg3 = 100; int Arg4 = 316984; verify_case(2, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_3() { string Arr0[] = {".555555555", "5.55555555", "55.5554555", "555.555555", "5555.55555", "55555.5555", "555555.555", "5555555.55", "55553555.5", "555555555."}; 4fd800b3a8 2011-02-23 kinaba: vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; int Arg2 = 7; int Arg3 = 1000000000; int Arg4 = 255218; verify_case(3, Arg4, numberOfWays(Arg0, Arg1, Arg2, Arg3)); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { DrivingAround().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE