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: LL dig(LL x) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if(x<=9) return x; 4fd800b3a8 2011-02-23 kinaba: LL s = 0; 4fd800b3a8 2011-02-23 kinaba: while(x) 4fd800b3a8 2011-02-23 kinaba: s += x%10, x/=10; 4fd800b3a8 2011-02-23 kinaba: return dig(s); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: class LocateTreasure 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: string location(int K, int m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<LL> ds; 4fd800b3a8 2011-02-23 kinaba: ds.push_back(1); 4fd800b3a8 2011-02-23 kinaba: LL found[10] = {-1, 0, -1, -1, -1, -1, -1, -1, -1, -1}; 4fd800b3a8 2011-02-23 kinaba: LL loop_beg, loop_end; 4fd800b3a8 2011-02-23 kinaba: for(LL i=1;; ++i) { 4fd800b3a8 2011-02-23 kinaba: LL d = dig( ds.back()*m ); 4fd800b3a8 2011-02-23 kinaba: if( found[d] < 0 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: ds.push_back(d); 4fd800b3a8 2011-02-23 kinaba: found[d] = i; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: loop_beg = found[d]; 4fd800b3a8 2011-02-23 kinaba: loop_end = i; 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> ds2 = ds; 4fd800b3a8 2011-02-23 kinaba: ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); 4fd800b3a8 2011-02-23 kinaba: ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); 4fd800b3a8 2011-02-23 kinaba: ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); 4fd800b3a8 2011-02-23 kinaba: ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); 4fd800b3a8 2011-02-23 kinaba: ds = ds2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL x=0, y=0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( K <= loop_beg ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(LL i=0; i<K; ++i) { 4fd800b3a8 2011-02-23 kinaba: if( i%4==0 ) y += ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==1 ) x += ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==2 ) y -= ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==3 ) x -= ds[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(LL i=0; i<loop_beg; ++i) { 4fd800b3a8 2011-02-23 kinaba: if( i%4==0 ) y += ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==1 ) x += ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==2 ) y -= ds[i]; 4fd800b3a8 2011-02-23 kinaba: if( i%4==3 ) x -= ds[i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL loop_len = 4 * (loop_end - loop_beg); 4fd800b3a8 2011-02-23 kinaba: K -= loop_beg; 4fd800b3a8 2011-02-23 kinaba: LL dx=0, dy=0; 4fd800b3a8 2011-02-23 kinaba: for(LL i=0; i<loop_len; ++i) { 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==0 ) dy += ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==1 ) dx += ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==2 ) dy -= ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==3 ) dx -= ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: x += dx * (K / loop_len); 4fd800b3a8 2011-02-23 kinaba: y += dy * (K / loop_len); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: K %= loop_len; 4fd800b3a8 2011-02-23 kinaba: for(LL i=0; i<K; ++i) { 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==0 ) y += ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==1 ) x += ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==2 ) y -= ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: if( (loop_beg+i)%4==3 ) x -= ds[loop_beg+i]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: stringstream sout; 4fd800b3a8 2011-02-23 kinaba: sout << x << " " << y; 4fd800b3a8 2011-02-23 kinaba: return sout.str(); 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(); } 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 string &Expected, const string &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() { int Arg0 = 5; int Arg1 = 2; string Arg2 = "-6 4"; verify_case(0, Arg2, location(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_1() { int Arg0 = 99; int Arg1 = 1; string Arg2 = "1 0"; verify_case(1, Arg2, location(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: void test_case_2() { int Arg0 = 6; int Arg1 = 9; string Arg2 = "9 1"; verify_case(2, Arg2, location(Arg0, Arg1)); } 4fd800b3a8 2011-02-23 kinaba: 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() { LocateTreasure().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE