Artifact Content
Not logged in

Artifact c9cf49d6b744849fa1e123d5b03dedb138d0d3b7


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

int powi(int a, int b)
{
	int v=1;
	for(int i=0; i<b; ++i)
		v*=a;
	return v;
}

class HanoiGoodAndBad { public:
	int moves(int N, int Dave) 
	{
		if( N == 0 )
			return 0;
		if( Dave <= (1<<N-1)-1 )
			return moves2(N-1, Dave);
		return (powi(3,N-1))*2+moves3(N-1, Dave - (1<<N-1));
	}
	int moves2(int N, int Dave)
	{
		if( N == 0 )
			return 0;
		if( Dave <= (1<<N-1)-1 )
			return moves(N-1, Dave);
		return powi(3,N-1)+moves2(N-1, Dave - (1<<N-1));
	}
	int moves3(int N, int Dave)
	{
		if( N == 0 )
			return 0;
		if( Dave <= (1<<N-1)-1 )
			return powi(3,N-1)+moves3(N-1, Dave);
		return (powi(3,N-1))*2+moves(N-1, Dave - (1<<N-1));
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, HanoiGoodAndBad().moves(N, Dave));}
int main(){

CASE(0)
	int N = 1; 
	int Dave = 1; 
	int _ = 2; 
END
CASE(0)
	int N = 2; 
	int Dave = 1; 
	int _ = 2; 
END
CASE(0)
	int N = 3; 
	int Dave = 1; 
	int _ = 2; 
END
CASE(1)
	int N = 4; 
	int Dave = 15; 
	int _ = 80; 
END
CASE(2)
	int N = 9; 
	int Dave = 265; 
	int _ = 16418; 
END
CASE(3)
	int N = 1; 
	int Dave = 0; 
	int _ = 0; 
END
CASE(3)
	int N = 1;
	int Dave = 1; 
	int _ = 2; 
END
CASE(4)
	int N = 19; 
	int Dave = (1<<19)-1; 
	int _ = -1; 
END

}
// END CUT HERE