Artifact Content
Not logged in

Artifact b6522f93bcade2955df5e2c79c3b76a3d5365402



//-------------------------------------------------------------
// Modulo Arithmetics
//
// Verified by
//   - TCO10 R3 LV3
//   - SRM 545 LV2
//-------------------------------------------------------------

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 = x.val-y.val+MODVAL; }
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 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; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); }
mint operator/(mint x, mint y) { return x/=y; }

// nCk :: O(log MODVAL) time, O(n) space.
vector<mint> FAC_(1,1);
mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }
mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); }

// nCk :: O(1) time, O(n^2) space.
vector< vector<mint> > CP_;
mint C(LL n, LL k) {
	while( CP_.size() <= n ) {
		int nn = CP_.size();
		CP_.push_back(vector<mint>(nn+1,1));
		for(int kk=1; kk<nn; ++kk)
			CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk];
	}
	return k<0 || n<k ? 0 : CP_[n][k];
}

template<typename T>
vector<T> MATMUL(const vector< 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[i][j]*v[j];
	return u;
}

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

template<typename T>
vector< vector<T> > MATPOW(vector< vector<T> > a, LL e)
{
	int N = a.size();
	vector< vector<T> > c(N, vector<T>(N));
	for(int i=0; i<N; ++i) c[i][i] = 1;
	for(; e; e>>=1) {
		if(e&1)
			c = MATMUL(c, a);
		a = MATMUL(a, a);
	}
	return c;
}

/*
// MODVAL must be a prime!!
LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e
{
	if( b >  e ) return 0;
	if( k <= 1 ) return k*(e-b+1);
	return DIV(SUB(POW(k, e+1), POW(k,b)), k-1);
}

// works for non-prime MODVAL
LL GEO(LL x_, LL e) // x^0 + x^1 + ... + x^e-1
{
   vector< vector<LL> > v(2, vector<LL>(2));
   vector< vector<LL> > x(2, vector<LL>(2));
   v[0][0] = v[1][1] = 1;
   x[0][0] = x_; x[0][1] = 0;
   x[1][0] = 1 ; x[1][1] = 1;
   for(;e;x=MATMUL(x,x),e>>=1)
      if(e&1)
         v = MATMUL(v, x);
   return v[1][0];
}

// works for non-prime MODVAL
LL HYP(LL x_, LL e) // e x^0 + (e-1) x^1 + ... + 1 x^e-1 = GEO(x,1)+GEO(x,2)+...+GEO(x,e)
{
   vector< vector<LL> > v(3, vector<LL>(3));
   vector< vector<LL> > x(3, vector<LL>(3));
   v[0][0] = v[1][1] = v[2][2] = 1;
   x[0][0] = x_; x[0][1] = 0; x[0][2] = 0;
   x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0;
   x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1;
   e++;
   for(;e;x=MATMUL(x,x),e>>=1)
      if(e&1)
         v = MATMUL(v, x);
   return v[2][0];
}
*/