Artifact Content
Not logged in

Artifact 6a42e8ac2f9b115bb934973ba3d5058a79d19b4b


#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;

class RabbitNumber { public:
	LL dsum(LL n) {
		LL x = 0;
		while(n) {
			x+=n%10;
			n/=10;
		}
		return x;
	}
	bool isRabbit(LL n) {
		LL a = dsum(n);
		LL b = dsum(n*n);
		return b==a*a;
	}
	void enumerate(int s, LL c, vector<int>& v)
	{
		if( c > 1000000000 )
			return;
		if( s == 0 )
			if( isRabbit(c) )
				v.push_back(int(c));
		for(int i=(c==0?1:0); i<=s && i<=9; ++i)
			enumerate(s-i, c*10+i, v);
	}

	int theCount(int low, int high) 
	{
		vector<int> v;
		for(int s=1; s<=15; ++s)
			enumerate(s, 0, v);
		sort(v.begin(), v.end());
		return upper_bound(v.begin(), v.end(), high)
		     - lower_bound(v.begin(), v.end(), low);
	}
};

// 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(_, RabbitNumber().theCount(low, high));}
int main(){

CASE(0)
	int low = 22; 
	int high = 22; 
	int _ = 1; 
END
CASE(1)
	int low = 484; 
	int high = 484; 
	int _ = 0; 
END
CASE(2)
	int low = 1; 
	int high = 58; 
	int _ = 12; 
END
CASE(3)
	int low = 58; 
	int high = 484; 
	int _ = 24; 
END
CASE(4)
	int low = 1000000000; 
	int high = 1000000000; 
	int _ = 1; 
END
CASE(5)
	int low = 1; 
	int high = 1; 
	int _ = 1; 
END
CASE(6)
	int low = 1; 
	int high = 1000000; 
	int _ = 615; 
END
CASE(7)
	int low = 1; 
	int high = 1000000000; 
	int _ = -1; 
END
}
// END CUT HERE