Artifact Content
Not logged in

Artifact bdee7ea796a0a8ca2491c09fe5cc8a678954b448


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

bool is_prime(int p)
{
	for(int q=2; q*q<=p; ++q)
		if(p%q==0)
			return false;
	return true;
}

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
	mint(LL       x):val(x%MODVAL) {}
};
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator*(mint x, mint y) { return x*=y; }
mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }

class PolynomialGCD { public:
	int gcd(string s)
	{
		mint ans = 1;
		for(int p=2; p<=s.size()+1; ++p)
			if(is_prime(p))
				ans *= POW(p, howManyTimesAlwaysDivisibleByP(s, p));
		return ans.val;
	}

	int howManyTimesAlwaysDivisibleByP(const string& s, int p)
	{
		static const int INF = 0x7fffffff;
		int at_least = INF;
		for(int k=-p; k<=+p; ++k) {
			int cur = 0;
			for(int b=((k+p+p-1)/p-1)*p,i=s.size()-1-(b-k); i>=0; b+=p,i-=p) {
				int e = s[i]-'0';
				if(b==0 && e)
					goto next;
				if(e)
					for(int bb=b; bb%p==0; bb/=p)
						cur+=e;
			}
			at_least = min(at_least, cur);
			next:;
		}
		return at_least;
	}
};

// 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(_, PolynomialGCD().gcd(s));}
int main(){

CASE(0)
	string s = "111"; 
	int _ = 6; 
END
CASE(1)
	string s = "00000"; 
	int _ = 1; 
END
CASE(2)
	string s = "2014"; 
	int _ = 16; 
END
CASE(3)
	string s = "31415926535"; 
	int _ = 659897170; 
END
/*
CASE(4)
	string s = ; 
	int _ = ; 
END
CASE(5)
	string s = ; 
	int _ = ; 
END
*/
}
// END CUT HERE