Artifact Content
Not logged in

Artifact 770f87bd162c875dcc47988870af5f3bb7c2bf8a


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

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 = 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; }

template<typename T>
vector<T> MATMULxx(const vector<T>& a, const vector<T>& v)
{
	int N = a.size();
	vector<T> u(N);
	for(int i=0; i<N; ++i)
	for(int j=0; j<N; ++j)
		u[i] += a[(j-i+N)%N]*v[j];
	return u;
}

template<typename T>
vector<T> MATMULxx(const vector<T>& a)
{
	int N = a.size();
	vector<T> c(N);
	for(int j=0; j<N; ++j)
	for(int k=0; k<N; ++k)
		c[j] += a[k]*a[(j-k+N)%N];
	return c;
}

template<typename T>
vector<T> MATPOWMULxx(vector<T> a, LL e, vector<T> v)
{
	for(; e; e>>=1, a=MATMULxx(a))
		if(e&1)
			v = MATMULxx(a, v);
	return v;
}

class PenguinEmperor { public:
	int countJourneys(int N, long long daysPassed)
	{
		vector<mint> v(N), u(N);
		u[0] = 1;
		for(int i=0; i<N; ++i) {
			if(i == daysPassed % N)
				v = u;
			vector<mint> uu(N);
			for(int x=0; x<N; ++x) {
				int f = (x+i+1)%N;
				int b = (x-i-1+N)%N;
				uu[x] += u[f];
				if(f!=b)
					uu[x] += u[b];
			}
			u = uu;
		}

		v = MATPOWMULxx(u, daysPassed/N, v);
		return v[0].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(_, PenguinEmperor().countJourneys(numCities, daysPassed));}
int main(){

CASE(0)
	int numCities = 3; 
	long long daysPassed = 2LL; 
	int _ = 2; 
END
CASE(1)
	int numCities = 4; 
	long long daysPassed = 3LL; 
	int _ = 2; 
END
CASE(2)
	int numCities = 5; 
	long long daysPassed = 36LL; 
	int _ = 107374182; 
END
CASE(3)
	int numCities = 300; 
	long long daysPassed = 751LL; 
	int _ = 413521250; 
END
CASE(4)
	int numCities = 300; 
	long long daysPassed = 750LL; 
	int _ = 0; 
END
CASE(5)
	int numCities = 350; 
	long long daysPassed = 1000000000000000000LL; 
	int _ = 667009739; 
END
CASE(6)
	int numCities = 5; 
	long long daysPassed = 7LL; 
	int _ = 12; 
END
/*
CASE(7)
	int numCities = ; 
	long long daysPassed = LL; 
	int _ = ; 
END
CASE(8)
	int numCities = ; 
	long long daysPassed = LL; 
	int _ = ; 
END
*/
}
// END CUT HERE