f94ac98860 2016-12-29 kinaba: #include <iostream> f94ac98860 2016-12-29 kinaba: #include <sstream> f94ac98860 2016-12-29 kinaba: #include <iomanip> f94ac98860 2016-12-29 kinaba: #include <vector> f94ac98860 2016-12-29 kinaba: #include <string> f94ac98860 2016-12-29 kinaba: #include <map> f94ac98860 2016-12-29 kinaba: #include <set> f94ac98860 2016-12-29 kinaba: #include <algorithm> f94ac98860 2016-12-29 kinaba: #include <numeric> f94ac98860 2016-12-29 kinaba: #include <iterator> f94ac98860 2016-12-29 kinaba: #include <functional> f94ac98860 2016-12-29 kinaba: #include <complex> f94ac98860 2016-12-29 kinaba: #include <queue> f94ac98860 2016-12-29 kinaba: #include <stack> f94ac98860 2016-12-29 kinaba: #include <cmath> f94ac98860 2016-12-29 kinaba: #include <cassert> f94ac98860 2016-12-29 kinaba: #include <tuple> f94ac98860 2016-12-29 kinaba: using namespace std; f94ac98860 2016-12-29 kinaba: typedef long long LL; f94ac98860 2016-12-29 kinaba: typedef complex<double> CMP; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: static const unsigned MODVAL = 1000000007; f94ac98860 2016-12-29 kinaba: struct mint f94ac98860 2016-12-29 kinaba: { f94ac98860 2016-12-29 kinaba: unsigned val; f94ac98860 2016-12-29 kinaba: mint() :val(0) {} f94ac98860 2016-12-29 kinaba: mint(int x) :val(x%MODVAL) {} f94ac98860 2016-12-29 kinaba: mint(unsigned x) :val(x%MODVAL) {} f94ac98860 2016-12-29 kinaba: mint(LL x) :val(x%MODVAL) {} f94ac98860 2016-12-29 kinaba: }; f94ac98860 2016-12-29 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val + y.val; } f94ac98860 2016-12-29 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val - y.val + MODVAL; } f94ac98860 2016-12-29 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } f94ac98860 2016-12-29 kinaba: mint operator+(mint x, mint y) { return x += y; } f94ac98860 2016-12-29 kinaba: mint operator-(mint x, mint y) { return x -= y; } f94ac98860 2016-12-29 kinaba: mint operator*(mint x, mint y) { return x *= y; } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; } f94ac98860 2016-12-29 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); } f94ac98860 2016-12-29 kinaba: mint operator/(mint x, mint y) { return x /= y; } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: LL eulerPhi(LL n) { f94ac98860 2016-12-29 kinaba: LL ans = n; f94ac98860 2016-12-29 kinaba: for (LL x = 2; x*x <= n; ++x) { f94ac98860 2016-12-29 kinaba: if (n % x == 0) { f94ac98860 2016-12-29 kinaba: ans -= ans / x; f94ac98860 2016-12-29 kinaba: while (n % x == 0) n /= x; f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: if (n > 1) ans -= ans / n; f94ac98860 2016-12-29 kinaba: return ans; f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: class ModEquation { public: f94ac98860 2016-12-29 kinaba: vector <int> count(int n, int K, vector <int> query) f94ac98860 2016-12-29 kinaba: { f94ac98860 2016-12-29 kinaba: int epk = (int)eulerPhi(K); f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: vector<pair<int, int>> dK; f94ac98860 2016-12-29 kinaba: int v = K; f94ac98860 2016-12-29 kinaba: for (int p = 2; p*p <= v; ++p) f94ac98860 2016-12-29 kinaba: if (v%p == 0) { f94ac98860 2016-12-29 kinaba: int cnt = 0; f94ac98860 2016-12-29 kinaba: while (v%p == 0) cnt++, v /= p; f94ac98860 2016-12-29 kinaba: dK.emplace_back(p, cnt); f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: if (v != 1) f94ac98860 2016-12-29 kinaba: dK.emplace_back(v, 1); f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: vector<int> ans; f94ac98860 2016-12-29 kinaba: for (int q : query) f94ac98860 2016-12-29 kinaba: ans.push_back(solve(n, K, epk, dK, q).val); f94ac98860 2016-12-29 kinaba: return ans; f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: // # of solutions in mod K world s.t. x[0] x[1] ... x[n-1] = q f94ac98860 2016-12-29 kinaba: // f94ac98860 2016-12-29 kinaba: // * n <= 50 f94ac98860 2016-12-29 kinaba: // * K = BIG f94ac98860 2016-12-29 kinaba: // * must be solved 1000 times in time limits. f94ac98860 2016-12-29 kinaba: mint solve(int n, int K, int epk, const vector<pair<int,int>>& dK, int q) f94ac98860 2016-12-29 kinaba: { f94ac98860 2016-12-29 kinaba: if (n == 1) f94ac98860 2016-12-29 kinaba: return 1; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: mint ans = POW(epk, n-1); f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: for (auto pe : dK) { f94ac98860 2016-12-29 kinaba: int p = pe.first; f94ac98860 2016-12-29 kinaba: int e = pe.second; f94ac98860 2016-12-29 kinaba: int qq = q; f94ac98860 2016-12-29 kinaba: int cnt = 0; f94ac98860 2016-12-29 kinaba: while (qq%p == 0 && cnt<e) f94ac98860 2016-12-29 kinaba: cnt++, qq /= p; f94ac98860 2016-12-29 kinaba: // p^cnt is in gcd(K,q) f94ac98860 2016-12-29 kinaba: if (cnt < e) { f94ac98860 2016-12-29 kinaba: ans *= POW(n, cnt); // div dups... f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: else { f94ac98860 2016-12-29 kinaba: ans *= POW(n, cnt); // or more... f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: return ans; f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: }; f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: // BEGIN CUT HERE f94ac98860 2016-12-29 kinaba: #include <ctime> f94ac98860 2016-12-29 kinaba: double start_time; string timer() f94ac98860 2016-12-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f94ac98860 2016-12-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f94ac98860 2016-12-29 kinaba: { os << "{ "; f94ac98860 2016-12-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f94ac98860 2016-12-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f94ac98860 2016-12-29 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { f94ac98860 2016-12-29 kinaba: bool ok = (Expected == Received); f94ac98860 2016-12-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f94ac98860 2016-12-29 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } f94ac98860 2016-12-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f94ac98860 2016-12-29 kinaba: #define END verify_case(_, ModEquation().count(n, K, query));} f94ac98860 2016-12-29 kinaba: int main(){ f94ac98860 2016-12-29 kinaba: f94ac98860 2016-12-29 kinaba: CASE(0) f94ac98860 2016-12-29 kinaba: int n = 2; f94ac98860 2016-12-29 kinaba: int K = 2; f94ac98860 2016-12-29 kinaba: int query_[] = {0,1}; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = {3, 1 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(1) f94ac98860 2016-12-29 kinaba: int n = 2; f94ac98860 2016-12-29 kinaba: int K = 4; f94ac98860 2016-12-29 kinaba: int query_[] = {0,1,2,3}; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = {8, 2, 4, 2 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(2) f94ac98860 2016-12-29 kinaba: int n = 6; f94ac98860 2016-12-29 kinaba: int K = 6; f94ac98860 2016-12-29 kinaba: int query_[] = {4}; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = {2016 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(3) f94ac98860 2016-12-29 kinaba: int n = 1; f94ac98860 2016-12-29 kinaba: int K = 2; f94ac98860 2016-12-29 kinaba: int query_[] = {0,0,0,1,1,1}; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = {1, 1, 1, 1, 1, 1 }; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: /* f94ac98860 2016-12-29 kinaba: CASE(4) f94ac98860 2016-12-29 kinaba: int n = ; f94ac98860 2016-12-29 kinaba: int K = ; f94ac98860 2016-12-29 kinaba: int query_[] = ; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = ; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: CASE(5) f94ac98860 2016-12-29 kinaba: int n = ; f94ac98860 2016-12-29 kinaba: int K = ; f94ac98860 2016-12-29 kinaba: int query_[] = ; f94ac98860 2016-12-29 kinaba: vector <int> query(query_, query_+sizeof(query_)/sizeof(*query_)); f94ac98860 2016-12-29 kinaba: int __[] = ; f94ac98860 2016-12-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); f94ac98860 2016-12-29 kinaba: END f94ac98860 2016-12-29 kinaba: */ f94ac98860 2016-12-29 kinaba: } f94ac98860 2016-12-29 kinaba: // END CUT HERE