1fbde9e2e2 2012-02-09 kinaba: #include <iostream> 1fbde9e2e2 2012-02-09 kinaba: #include <sstream> 1fbde9e2e2 2012-02-09 kinaba: #include <iomanip> 1fbde9e2e2 2012-02-09 kinaba: #include <vector> 1fbde9e2e2 2012-02-09 kinaba: #include <string> 1fbde9e2e2 2012-02-09 kinaba: #include <map> 1fbde9e2e2 2012-02-09 kinaba: #include <set> 1fbde9e2e2 2012-02-09 kinaba: #include <algorithm> 1fbde9e2e2 2012-02-09 kinaba: #include <numeric> 1fbde9e2e2 2012-02-09 kinaba: #include <iterator> 1fbde9e2e2 2012-02-09 kinaba: #include <functional> 1fbde9e2e2 2012-02-09 kinaba: #include <complex> 1fbde9e2e2 2012-02-09 kinaba: #include <queue> 1fbde9e2e2 2012-02-09 kinaba: #include <stack> 1fbde9e2e2 2012-02-09 kinaba: #include <cmath> 1fbde9e2e2 2012-02-09 kinaba: #include <cassert> 1fbde9e2e2 2012-02-09 kinaba: #include <cstring> 1fbde9e2e2 2012-02-09 kinaba: #ifdef __GNUC__ 1fbde9e2e2 2012-02-09 kinaba: #include <ext/hash_map> 1fbde9e2e2 2012-02-09 kinaba: #define unordered_map __gnu_cxx::hash_map 1fbde9e2e2 2012-02-09 kinaba: #else 1fbde9e2e2 2012-02-09 kinaba: #include <unordered_map> 1fbde9e2e2 2012-02-09 kinaba: #endif 1fbde9e2e2 2012-02-09 kinaba: using namespace std; 1fbde9e2e2 2012-02-09 kinaba: typedef long long LL; 1fbde9e2e2 2012-02-09 kinaba: typedef complex<double> CMP; 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: static const int MODVAL = 1000000007; // must be prime for op/ 1fbde9e2e2 2012-02-09 kinaba: struct mint 1fbde9e2e2 2012-02-09 kinaba: { 1fbde9e2e2 2012-02-09 kinaba: int val; 1fbde9e2e2 2012-02-09 kinaba: mint():val(0){} 1fbde9e2e2 2012-02-09 kinaba: mint(int x):val(x%MODVAL) {} // x>=0 1fbde9e2e2 2012-02-09 kinaba: mint(size_t x):val(x%MODVAL) {} // x>=0 1fbde9e2e2 2012-02-09 kinaba: mint(LL x):val(x%MODVAL) {} // x>=0 1fbde9e2e2 2012-02-09 kinaba: }; 1fbde9e2e2 2012-02-09 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 1fbde9e2e2 2012-02-09 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 1fbde9e2e2 2012-02-09 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 1fbde9e2e2 2012-02-09 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 1fbde9e2e2 2012-02-09 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 1fbde9e2e2 2012-02-09 kinaba: mint operator+(mint x, mint y) { return x+=y; } 1fbde9e2e2 2012-02-09 kinaba: mint operator-(mint x, mint y) { return x-=y; } 1fbde9e2e2 2012-02-09 kinaba: mint operator*(mint x, mint y) { return x*=y; } 1fbde9e2e2 2012-02-09 kinaba: mint operator/(mint x, mint y) { return x/=y; } 1fbde9e2e2 2012-02-09 kinaba: vector<mint> FAC_(1,1); 1fbde9e2e2 2012-02-09 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } 1fbde9e2e2 2012-02-09 kinaba: mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } 1fbde9e2e2 2012-02-09 kinaba: mint Perm(LL n, LL k) { return FAC(n) / FAC(n-k); } 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: class NoRepeatPlaylist { public: 1fbde9e2e2 2012-02-09 kinaba: int numPlaylists(int N, int M, int P) 1fbde9e2e2 2012-02-09 kinaba: { 1fbde9e2e2 2012-02-09 kinaba: if( N == P ) 1fbde9e2e2 2012-02-09 kinaba: return FAC(N).val; 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: if( M == N ) 1fbde9e2e2 2012-02-09 kinaba: return 0; 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: int r = M+1; 1fbde9e2e2 2012-02-09 kinaba: vector<mint> heard(N+1, 0); 1fbde9e2e2 2012-02-09 kinaba: heard[r] = Perm(N, r); 1fbde9e2e2 2012-02-09 kinaba: for(int i=0; i<P-r; ++i) { 1fbde9e2e2 2012-02-09 kinaba: vector<mint> h2(N+1, 0); 1fbde9e2e2 2012-02-09 kinaba: for(int x=0; x<=N; ++x) { 1fbde9e2e2 2012-02-09 kinaba: if(x>=M) 1fbde9e2e2 2012-02-09 kinaba: h2[x] += heard[x] * (x-M); 1fbde9e2e2 2012-02-09 kinaba: if(x<N) 1fbde9e2e2 2012-02-09 kinaba: h2[x+1] += heard[x] * (N - x); 1fbde9e2e2 2012-02-09 kinaba: } 1fbde9e2e2 2012-02-09 kinaba: heard = h2; 1fbde9e2e2 2012-02-09 kinaba: } 1fbde9e2e2 2012-02-09 kinaba: return heard[N].val; 1fbde9e2e2 2012-02-09 kinaba: } 1fbde9e2e2 2012-02-09 kinaba: }; 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: // BEGIN CUT HERE 1fbde9e2e2 2012-02-09 kinaba: #include <ctime> 1fbde9e2e2 2012-02-09 kinaba: double start_time; string timer() 1fbde9e2e2 2012-02-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 1fbde9e2e2 2012-02-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 1fbde9e2e2 2012-02-09 kinaba: { os << "{ "; 1fbde9e2e2 2012-02-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 1fbde9e2e2 2012-02-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 1fbde9e2e2 2012-02-09 kinaba: void verify_case(const int& Expected, const int& Received) { 1fbde9e2e2 2012-02-09 kinaba: bool ok = (Expected == Received); 1fbde9e2e2 2012-02-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 1fbde9e2e2 2012-02-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 1fbde9e2e2 2012-02-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 1fbde9e2e2 2012-02-09 kinaba: #define END verify_case(_, NoRepeatPlaylist().numPlaylists(N, M, P));} 1fbde9e2e2 2012-02-09 kinaba: int main(){ 1fbde9e2e2 2012-02-09 kinaba: 1fbde9e2e2 2012-02-09 kinaba: CASE(0) 1fbde9e2e2 2012-02-09 kinaba: int N = 1; 1fbde9e2e2 2012-02-09 kinaba: int M = 0; 1fbde9e2e2 2012-02-09 kinaba: int P = 3; 1fbde9e2e2 2012-02-09 kinaba: int _ = 1; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(1) 1fbde9e2e2 2012-02-09 kinaba: int N = 1; 1fbde9e2e2 2012-02-09 kinaba: int M = 1; 1fbde9e2e2 2012-02-09 kinaba: int P = 3; 1fbde9e2e2 2012-02-09 kinaba: int _ = 0; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(2) 1fbde9e2e2 2012-02-09 kinaba: int N = 2; 1fbde9e2e2 2012-02-09 kinaba: int M = 0; 1fbde9e2e2 2012-02-09 kinaba: int P = 3; 1fbde9e2e2 2012-02-09 kinaba: int _ = 6; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(3) 1fbde9e2e2 2012-02-09 kinaba: int N = 4; 1fbde9e2e2 2012-02-09 kinaba: int M = 0; 1fbde9e2e2 2012-02-09 kinaba: int P = 4; 1fbde9e2e2 2012-02-09 kinaba: int _ = 24; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(4) 1fbde9e2e2 2012-02-09 kinaba: int N = 2; 1fbde9e2e2 2012-02-09 kinaba: int M = 1; 1fbde9e2e2 2012-02-09 kinaba: int P = 4; 1fbde9e2e2 2012-02-09 kinaba: int _ = 2; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(5) 1fbde9e2e2 2012-02-09 kinaba: int N = 50; 1fbde9e2e2 2012-02-09 kinaba: int M = 5; 1fbde9e2e2 2012-02-09 kinaba: int P = 100; 1fbde9e2e2 2012-02-09 kinaba: int _ = 222288991; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: /* 1fbde9e2e2 2012-02-09 kinaba: CASE(6) 1fbde9e2e2 2012-02-09 kinaba: int N = ; 1fbde9e2e2 2012-02-09 kinaba: int M = ; 1fbde9e2e2 2012-02-09 kinaba: int P = ; 1fbde9e2e2 2012-02-09 kinaba: int _ = ; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: CASE(7) 1fbde9e2e2 2012-02-09 kinaba: int N = ; 1fbde9e2e2 2012-02-09 kinaba: int M = ; 1fbde9e2e2 2012-02-09 kinaba: int P = ; 1fbde9e2e2 2012-02-09 kinaba: int _ = ; 1fbde9e2e2 2012-02-09 kinaba: END 1fbde9e2e2 2012-02-09 kinaba: */ 1fbde9e2e2 2012-02-09 kinaba: } 1fbde9e2e2 2012-02-09 kinaba: // END CUT HERE