Artifact Content
Not logged in

Artifact 7df1c0211e8d6875c3a5b3a8e2f40fb9217eb12b


#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 TheLuckyBasesDivOne { public:
	set<LL> all;
	void computeAll()
	{
		for(int len=1; len<=15; ++len)
		{
			for(int pat=0; pat<(1<<len); ++pat)
			{
				LL val = 0;
				for(int i=0; i<len; ++i)
					val = val*10 + ((pat&(1<<i)) ? 7 : 4);
				all.insert(val);
			}
		}
	}

	long long find(long long n) 
	{
		computeAll();
		if( all.count(n) ) // if itself is lucky, then inf.
			return -1;

		LL cnt = 0;
		// 3 or more B-its
		for(LL B=2; B*B*B<=n; ++B)
			if( isLuckyBase(n, B) )
				++cnt;
		// 2 B-its
		for(set<LL>::iterator it=all.begin(); it!=all.end(); ++it)
		{
			LL r = n-*it;
			for(LL B=*it+1; B<=n/B && B*B<n; ++B) {
				if( r%B==0 && n/B<B && all.count(n/B) )
					++cnt;
			}
		}
		return cnt;
	}

	bool isLuckyBase(LL n, LL B)
	{
		for(;n; n/=B)
			if( !all.count(n%B) )
				return false;
		return true;
	}
};

// 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 long long& Expected, const long long& 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(_, TheLuckyBasesDivOne().find(n));}
int main(){

CASE(0)
	long long n = 255LL; 
	long long _ = 2LL; 
END
CASE(1)
	long long n = 474LL; 
	long long _ = -1LL; 
END
CASE(2)
	long long n = 13LL; 
	long long _ = 0LL; 
END
CASE(3)
	long long n = 4748LL; 
	long long _ = 5LL; 
END
CASE(4)
	long long n = 1LL; 
	long long _ = 0LL; 
END
CASE(5)
	long long n = 10000000000000000LL; 
	long long _ = -1LL; 
END

}
// END CUT HERE