c9a076d509 2014-01-11 kinaba: #include <iostream> c9a076d509 2014-01-11 kinaba: #include <sstream> c9a076d509 2014-01-11 kinaba: #include <iomanip> c9a076d509 2014-01-11 kinaba: #include <vector> c9a076d509 2014-01-11 kinaba: #include <string> c9a076d509 2014-01-11 kinaba: #include <map> c9a076d509 2014-01-11 kinaba: #include <set> c9a076d509 2014-01-11 kinaba: #include <algorithm> c9a076d509 2014-01-11 kinaba: #include <numeric> c9a076d509 2014-01-11 kinaba: #include <iterator> c9a076d509 2014-01-11 kinaba: #include <functional> c9a076d509 2014-01-11 kinaba: #include <complex> c9a076d509 2014-01-11 kinaba: #include <queue> c9a076d509 2014-01-11 kinaba: #include <stack> c9a076d509 2014-01-11 kinaba: #include <cmath> c9a076d509 2014-01-11 kinaba: #include <cassert> c9a076d509 2014-01-11 kinaba: #include <tuple> c9a076d509 2014-01-11 kinaba: using namespace std; c9a076d509 2014-01-11 kinaba: typedef long long LL; c9a076d509 2014-01-11 kinaba: typedef complex<double> CMP; c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: static const unsigned MODVAL = 1000000007; c9a076d509 2014-01-11 kinaba: struct mint c9a076d509 2014-01-11 kinaba: { c9a076d509 2014-01-11 kinaba: unsigned val; c9a076d509 2014-01-11 kinaba: mint():val(0){} c9a076d509 2014-01-11 kinaba: mint(int x):val(x%MODVAL) {} c9a076d509 2014-01-11 kinaba: mint(unsigned x):val(x%MODVAL) {} c9a076d509 2014-01-11 kinaba: mint(LL x):val(x%MODVAL) {} c9a076d509 2014-01-11 kinaba: }; c9a076d509 2014-01-11 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } c9a076d509 2014-01-11 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } c9a076d509 2014-01-11 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } c9a076d509 2014-01-11 kinaba: mint operator+(mint x, mint y) { return x+=y; } c9a076d509 2014-01-11 kinaba: mint operator-(mint x, mint y) { return x-=y; } c9a076d509 2014-01-11 kinaba: mint operator*(mint x, mint y) { return x*=y; } c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: class PairsOfStrings { public: c9a076d509 2014-01-11 kinaba: int getNumber(int n, int k) c9a076d509 2014-01-11 kinaba: { c9a076d509 2014-01-11 kinaba: set<int> ds; c9a076d509 2014-01-11 kinaba: for(int d=1; d*d<=n; ++d) c9a076d509 2014-01-11 kinaba: if(n%d == 0) { c9a076d509 2014-01-11 kinaba: ds.insert(d); c9a076d509 2014-01-11 kinaba: ds.insert(n/d); c9a076d509 2014-01-11 kinaba: } c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: memo.clear(); c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: mint total = 0; c9a076d509 2014-01-11 kinaba: for(int d: ds) c9a076d509 2014-01-11 kinaba: total += count_unit(ds, d, k) * d; c9a076d509 2014-01-11 kinaba: return total.val; c9a076d509 2014-01-11 kinaba: } c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: map<int, mint> memo; c9a076d509 2014-01-11 kinaba: mint count_unit(const set<int>& ds, int d, int k) c9a076d509 2014-01-11 kinaba: { c9a076d509 2014-01-11 kinaba: if(memo.count(d)) c9a076d509 2014-01-11 kinaba: return memo[d]; c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: mint v = POW(k, d); c9a076d509 2014-01-11 kinaba: for(int x: ds) { c9a076d509 2014-01-11 kinaba: if(x>=d) c9a076d509 2014-01-11 kinaba: break; c9a076d509 2014-01-11 kinaba: if(d%x==0) c9a076d509 2014-01-11 kinaba: v -= count_unit(ds, x, k); c9a076d509 2014-01-11 kinaba: } c9a076d509 2014-01-11 kinaba: return memo[d] = v; c9a076d509 2014-01-11 kinaba: } c9a076d509 2014-01-11 kinaba: }; c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: // BEGIN CUT HERE c9a076d509 2014-01-11 kinaba: #include <ctime> c9a076d509 2014-01-11 kinaba: double start_time; string timer() c9a076d509 2014-01-11 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c9a076d509 2014-01-11 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c9a076d509 2014-01-11 kinaba: { os << "{ "; c9a076d509 2014-01-11 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c9a076d509 2014-01-11 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c9a076d509 2014-01-11 kinaba: void verify_case(const int& Expected, const int& Received) { c9a076d509 2014-01-11 kinaba: bool ok = (Expected == Received); c9a076d509 2014-01-11 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c9a076d509 2014-01-11 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c9a076d509 2014-01-11 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c9a076d509 2014-01-11 kinaba: #define END verify_case(_, PairsOfStrings().getNumber(n, k));} c9a076d509 2014-01-11 kinaba: int main(){ c9a076d509 2014-01-11 kinaba: c9a076d509 2014-01-11 kinaba: CASE(0) c9a076d509 2014-01-11 kinaba: int n = 2; c9a076d509 2014-01-11 kinaba: int k = 2; c9a076d509 2014-01-11 kinaba: int _ = 6; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: CASE(1) c9a076d509 2014-01-11 kinaba: int n = 3; c9a076d509 2014-01-11 kinaba: int k = 2; c9a076d509 2014-01-11 kinaba: int _ = 20; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: CASE(2) c9a076d509 2014-01-11 kinaba: int n = 3; c9a076d509 2014-01-11 kinaba: int k = 4; c9a076d509 2014-01-11 kinaba: int _ = 184; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: CASE(3) c9a076d509 2014-01-11 kinaba: int n = 6; c9a076d509 2014-01-11 kinaba: int k = 2; c9a076d509 2014-01-11 kinaba: int _ = 348; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: CASE(4) c9a076d509 2014-01-11 kinaba: int n = 100; c9a076d509 2014-01-11 kinaba: int k = 26; c9a076d509 2014-01-11 kinaba: int _ = 46519912; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: CASE(5) c9a076d509 2014-01-11 kinaba: int n = 100000000; c9a076d509 2014-01-11 kinaba: int k = 26; c9a076d509 2014-01-11 kinaba: int _ = -1; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: /* c9a076d509 2014-01-11 kinaba: CASE(6) c9a076d509 2014-01-11 kinaba: int n = ; c9a076d509 2014-01-11 kinaba: int k = ; c9a076d509 2014-01-11 kinaba: int _ = ; c9a076d509 2014-01-11 kinaba: END c9a076d509 2014-01-11 kinaba: */ c9a076d509 2014-01-11 kinaba: } c9a076d509 2014-01-11 kinaba: // END CUT HERE