Artifact Content
Not logged in

Artifact cd5512e0c909abdd423297d7c0e9c22b5be08dd4


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
typedef long long LL;

LL dig(LL x)
{
	if(x<=9) return x;
	LL s = 0;
	while(x)
		s += x%10, x/=10;
	return dig(s);
}
class LocateTreasure
{
public:
	string location(int K, int m) 
	{
		vector<LL> ds;
		ds.push_back(1);
		LL found[10] = {-1, 0, -1, -1, -1, -1, -1, -1, -1, -1};
		LL loop_beg, loop_end;
		for(LL i=1;; ++i) {
			LL d = dig( ds.back()*m );
			if( found[d] < 0 )
			{
				ds.push_back(d);
				found[d] = i;
			}
			else
			{
				loop_beg = found[d];
				loop_end = i;
				break;
			}
		}

		vector<LL> ds2 = ds;
		ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end);
		ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end);
		ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end);
		ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end);
		ds = ds2;

		LL x=0, y=0;

		if( K <= loop_beg )
		{
			for(LL i=0; i<K; ++i) {
				if( i%4==0 ) y += ds[i];
				if( i%4==1 ) x += ds[i];
				if( i%4==2 ) y -= ds[i];
				if( i%4==3 ) x -= ds[i];
			}
		}
		else
		{
			for(LL i=0; i<loop_beg; ++i) {
				if( i%4==0 ) y += ds[i];
				if( i%4==1 ) x += ds[i];
				if( i%4==2 ) y -= ds[i];
				if( i%4==3 ) x -= ds[i];
			}

			LL loop_len = 4 * (loop_end - loop_beg);
			K -= loop_beg;
			LL dx=0, dy=0;
			for(LL i=0; i<loop_len; ++i) {
				if( (loop_beg+i)%4==0 ) dy += ds[loop_beg+i];
				if( (loop_beg+i)%4==1 ) dx += ds[loop_beg+i];
				if( (loop_beg+i)%4==2 ) dy -= ds[loop_beg+i];
				if( (loop_beg+i)%4==3 ) dx -= ds[loop_beg+i];
			}
			x += dx * (K / loop_len);
			y += dy * (K / loop_len);

			K %= loop_len;
			for(LL i=0; i<K; ++i) {
				if( (loop_beg+i)%4==0 ) y += ds[loop_beg+i];
				if( (loop_beg+i)%4==1 ) x += ds[loop_beg+i];
				if( (loop_beg+i)%4==2 ) y -= ds[loop_beg+i];
				if( (loop_beg+i)%4==3 ) x -= ds[loop_beg+i];
			}
		}

		stringstream sout;
		sout << x << " " << y;
		return sout.str();
	}

// BEGIN CUT HERE
	public:
	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(); }
	private:
	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(); }
	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; } }
	void test_case_0() { int Arg0 = 5; int Arg1 = 2; string Arg2 = "-6 4"; verify_case(0, Arg2, location(Arg0, Arg1)); }
	void test_case_1() { int Arg0 = 99; int Arg1 = 1; string Arg2 = "1 0"; verify_case(1, Arg2, location(Arg0, Arg1)); }
	void test_case_2() { int Arg0 = 6; int Arg1 = 9; string Arg2 = "9 1"; verify_case(2, Arg2, location(Arg0, Arg1)); }

// END CUT HERE
};
// BEGIN CUT HERE 
int main() { LocateTreasure().run_test(-1); }
// END CUT HERE