Artifact Content
Not logged in

Artifact 2687f89822088323ce76b1226e4cddb09ff156a7


#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;

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<M; ++n)
			v *= mint(n)*K + 1;
		return (v_rem * POW(v,N/M)).val;
	}
};

// 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(_, 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