23dfcca431 2011-02-23 kinaba: 23dfcca431 2011-02-23 kinaba: //------------------------------------------------------------- 23dfcca431 2011-02-23 kinaba: // Modulo Arithmetics 23dfcca431 2011-02-23 kinaba: // 23dfcca431 2011-02-23 kinaba: // Verified by 23dfcca431 2011-02-23 kinaba: // - TCO10 R3 LV3 7d2fe88cb2 2012-09-08 kinaba: // - SRM 545 Div1 LV2 7d2fe88cb2 2012-09-08 kinaba: // - SRM 554 Div1 LV3 23dfcca431 2011-02-23 kinaba: //------------------------------------------------------------- 23dfcca431 2011-02-23 kinaba: 0ea7f42c5c 2012-09-02 kinaba: static const unsigned MODVAL = 1000000007; 23dfcca431 2011-02-23 kinaba: struct mint 23dfcca431 2011-02-23 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: unsigned val; 23dfcca431 2011-02-23 kinaba: mint():val(0){} 0ea7f42c5c 2012-09-02 kinaba: mint(int x):val(x%MODVAL) {} 0ea7f42c5c 2012-09-02 kinaba: mint(unsigned x):val(x%MODVAL) {} 0ea7f42c5c 2012-09-02 kinaba: mint(LL x):val(x%MODVAL) {} 23dfcca431 2011-02-23 kinaba: }; 068d2337c1 2011-09-18 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 068d2337c1 2011-09-18 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 068d2337c1 2011-09-18 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 068d2337c1 2011-09-18 kinaba: mint operator+(mint x, mint y) { return x+=y; } 068d2337c1 2011-09-18 kinaba: mint operator-(mint x, mint y) { return x-=y; } 068d2337c1 2011-09-18 kinaba: mint operator*(mint x, mint y) { return x*=y; } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 0ea7f42c5c 2012-09-02 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 068d2337c1 2011-09-18 kinaba: mint operator/(mint x, mint y) { return x/=y; } 0ea7f42c5c 2012-09-02 kinaba: 23dfcca431 2011-02-23 kinaba: vector<mint> FAC_(1,1); 068d2337c1 2011-09-18 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 7d2fe88cb2 2012-09-08 kinaba: 7d2fe88cb2 2012-09-08 kinaba: // nCk :: O(log MODVAL) time, O(n) space. 068d2337c1 2011-09-18 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: // nCk :: O(1) time, O(n^2) space. 0ea7f42c5c 2012-09-02 kinaba: vector< vector<mint> > CP_; 16685c1758 2012-06-07 kinaba: mint C(LL n, LL k) { 4318dd2827 2012-06-07 kinaba: while( CP_.size() <= n ) { 4318dd2827 2012-06-07 kinaba: int nn = CP_.size(); 4318dd2827 2012-06-07 kinaba: CP_.push_back(vector<mint>(nn+1,1)); 4318dd2827 2012-06-07 kinaba: for(int kk=1; kk<nn; ++kk) 4318dd2827 2012-06-07 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; 16685c1758 2012-06-07 kinaba: } 16685c1758 2012-06-07 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; 23dfcca431 2011-02-23 kinaba: } 23dfcca431 2011-02-23 kinaba: 7d2fe88cb2 2012-09-08 kinaba: // O(log MODVAL), MODVAL must be prime: k^b + k^b+1 + ... + k^e 7d2fe88cb2 2012-09-08 kinaba: mint GSS(mint k, LL b, LL e) 23dfcca431 2011-02-23 kinaba: { 7d2fe88cb2 2012-09-08 kinaba: if( b > e ) return 0; 7d2fe88cb2 2012-09-08 kinaba: if( k.val <= 1 ) return k*(e-b+1); 7d2fe88cb2 2012-09-08 kinaba: return (POW(k, e+1) - POW(k,b)) / (k-1); 23dfcca431 2011-02-23 kinaba: }