Artifact Content
Not logged in

Artifact b637a94f34ab84c4af9b577d232ada00d50866fb


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

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

vector< vector<mint> > CP_;
mint C(int n, int 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];
}

class BearPermutations { public:
	int countPermutations(int N, int S, int MOD)
	{
		MODVAL = MOD;
		return pat_below(N, S).val;
	}

	mint pat_below(int N, int S) {
		mint total = 0;
		for(int s=0; s<=S; ++s)
			total += pat_exact(N, s);
		return total;
	}

	vector<int> memo3;
	int pat_exact(int N, int S) {
		memo3.resize(101*101, -1);
		auto key = N*101+S;
		if(memo3[key]!=-1)
			return memo3[key];

		mint total = 0;
		for(int c=0; c<N; ++c)
			total += pat_center_exact(N, c, S);
		return memo3[key] = total.val;
	}

	vector<int> memo2;
	int pat_exact_shifted(int N, int S, int score_offset) {
		memo2.resize(101*101*101, -1);
		auto key = (N*101+score_offset)*101+S;
		if(memo2[key]!=-1)
			return memo2[key];

		mint total = 0;
		for(int c=0; c<N; ++c)
			if(score_offset+c <= S)
				total += pat_center_exact(N, c, S-(score_offset+c));
		return memo2[key]=total.val;
	}

	vector<int> memo;
	int pat_center_exact(int N, int c, int S)
	{
		c = min(c, N-1-c);

		if(N==1)
			return (S==0 ? 1 : 0);
		if(c==0 || c==N-1)
			return pat_exact(N-1, S);

		memo.resize(101*101*101, -1);
		auto key = (N*101+c)*101+S;
		if(memo[key]!=-1)
			return memo[key];

		LL total = 0;
		for(int cl=0; cl<c; ++cl)
		for(int SS=0; SS<=S; ++SS)
			total += ((LL)pat_center_exact(c, cl, SS) * pat_exact_shifted(N-c-1, S-SS, (c-cl)+1)) % MODVAL;
		return memo[key] = (total * C(N-1, c)).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(_, BearPermutations().countPermutations(N, S, MOD));}
int main(){

CASE(0)
	int N = 3; 
	int S = 1; 
	int MOD = 71876209; 
	int _ = 4; 
END
CASE(1)
	int N = 4; 
	int S = 0; 
	int MOD = 1000003; 
	int _ = 8; 
END
CASE(2)
	int N = 4; 
	int S = 1; 
	int MOD = 483128897; 
	int _ = 8; 
END
CASE(3)
	int N = 5; 
	int S = 3; 
	int MOD = 907283243; 
	int _ = 82; 
END
CASE(4)
	int N = 5; 
	int S = 100; 
	int MOD = 101; 
	int _ = 19; 
END
CASE(5)
	int N = 20; 
	int S = 30; 
	int MOD = 3; 
	int _ = 2; 
END
CASE(6)
	int N = 100; 
	int S = 100; 
	int MOD = 1000000007; 
	int _ = -1; 
END
/*
CASE(7)
	int N = ; 
	int S = ; 
	int MOD = ; 
	int _ = ; 
END
*/
}
// END CUT HERE