Artifact Content
Not logged in

Artifact 7c1da83b0a1da4da5c64d4db70fedf4e1df63cfb


#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 <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const int MODVAL = 1000000007; // must be prime for op/
struct mint
{
	int val;
	mint():val(0){}
	mint(int    x):val(x%MODVAL) {} // x>=0
	mint(size_t x):val(x%MODVAL) {} // x>=0
	mint(LL     x):val(x%MODVAL) {} // x>=0
};
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 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; }
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; }
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)); }
mint Perm(LL n, LL k) { return FAC(n) / FAC(n-k); }

class NoRepeatPlaylist { public:
	int numPlaylists(int N, int M, int P)
	{
		if( N == P )
			return FAC(N).val;

		if( M == N )
			return 0;

		int r = M+1;
		vector<mint> heard(N+1, 0);
		heard[r] = Perm(N, r);
		for(int i=0; i<P-r; ++i) {
			vector<mint> h2(N+1, 0);
			for(int x=0; x<=N; ++x) {
				if(x>=M)
				h2[x]   += heard[x] * (x-M);
				if(x<N)
				h2[x+1] += heard[x] * (N - x); 
			}
			heard = h2;
		}
		return heard[N].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(_, NoRepeatPlaylist().numPlaylists(N, M, P));}
int main(){

CASE(0)
	int N = 1; 
	int M = 0; 
	int P = 3; 
	int _ = 1; 
END
CASE(1)
	int N = 1; 
	int M = 1; 
	int P = 3; 
	int _ = 0; 
END
CASE(2)
	int N = 2; 
	int M = 0; 
	int P = 3; 
	int _ = 6; 
END
CASE(3)
	int N = 4; 
	int M = 0; 
	int P = 4; 
	int _ = 24; 
END
CASE(4)
	int N = 2; 
	int M = 1; 
	int P = 4; 
	int _ = 2; 
END
CASE(5)
	int N = 50; 
	int M = 5; 
	int P = 100; 
	int _ = 222288991; 
END
/*
CASE(6)
	int N = ; 
	int M = ; 
	int P = ; 
	int _ = ; 
END
CASE(7)
	int N = ; 
	int M = ; 
	int P = ; 
	int _ = ; 
END
*/
}
// END CUT HERE