ADDED SRM/661-U/1A.cpp Index: SRM/661-U/1A.cpp ================================================================== --- SRM/661-U/1A.cpp +++ SRM/661-U/1A.cpp @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class MissingLCM { public: + int getMin(int N) + { + int M = N+1; + vector isp(N+1, true); + for(int p=2; p<=N; ++p) + if( isp[p] ) { + for(LL q=p; q<=N; q*=p) + M = max(M, (N/q+1)*q); + for(int q=p+p; q<=N; q+=p) + isp[q] = false; + } + return M; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, MissingLCM().getMin(N));} +int main(){ + +CASE(0) + int N = 1; + int _ = 2; +END +CASE(1) + int N = 2; + int _ = 4; +END +CASE(2) + int N = 3; + int _ = 6; +END +CASE(3) + int N = 4; + int _ = 8; +END +CASE(4) + int N = 5; + int _ = 10; +END +CASE(5) + int N = 42; + int _ = 82; +END +CASE(6) + int N = 999999; + int _ = 1999966; +END +/* +CASE(7) + int N = ; + int _ = ; +END +CASE(8) + int N = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/661-U/1B.cpp Index: SRM/661-U/1B.cpp ================================================================== --- SRM/661-U/1B.cpp +++ SRM/661-U/1B.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static unsigned MODVAL; +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 = x.val+y.val; } +mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } +mint operator+(mint x, mint y) { return x+=y; } +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 ColorfulLineGraphs { public: + int countWays(long long N, long long K, int M) + { + MODVAL = M; + --K; + + // The problem reduces to + // (K+1)(2K+1)(3K+1) ... (NK+1) + // and note that + // (MK+1) == 1 mod M + mint v_rem = 1; + for(LL n=N/M*M; n<=N; ++n) + v_rem *= mint(n)*K + 1; + + mint v = 1; + for(int n=1; n +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, ColorfulLineGraphs().countWays(N, K, M));} +int main(){ + +CASE(0) + long long N = 3LL; + long long K = 2LL; + int M = 100000; + int _ = 24; +END +CASE(1) + long long N = 15LL; + long long K = 3LL; + int M = 1000000; + int _ = 510625; +END +CASE(2) + long long N = 100000LL; + long long K = 100000LL; + int M = 999999; + int _ = 185185; +END +CASE(3) + long long N = 1000000000000LL; + long long K = 6LL; + int M = 1000000; + int _ = 109376; +END +CASE(4) + long long N = 5000LL; + long long K = 1000000000000LL; + int M = 314159; + int _ = 202996; +END +/* +CASE(5) + long long N = LL; + long long K = LL; + int M = ; + int _ = ; +END +CASE(6) + long long N = LL; + long long K = LL; + int M = ; + int _ = ; +END +*/ +} +// END CUT HERE