Artifact Content
Not logged in

Artifact 80f635d2b3ec8a206188bd80b8bce3d5a9b0d05d


#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 const unsigned MODVAL = 1000000007;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) {
		assert(data.size() * sizeof(T)<(1 << 28));
	}
	T& operator()(int i1, int i2)
	{
		return data[(i1*N2) + i2];
	}
	void swap(DP2& rhs)
	{
		data.swap(rhs.data);
	}
};

class SumPyramid { public:
	int countPyramids(int levels, int top)
	{
		vector<int> C(1, 1);
		for (int _ = 1; _ < levels; ++_) {
			vector<int> CC(C.size() + 1);
			for (size_t i = 0; i < CC.size(); ++i)
				CC[i] = min(top + 1, (i ? C[i - 1] : 0) + (i<C.size() ? C[i] : 0));
			C = CC;
		}

		DP2<int> memo(levels, top+1, -1);
		return rec(levels - 1, 0, top, C, memo);
	}

	int rec(int n, int k, int sum, const vector<int>& C, DP2<int>& memo) 
	{
		// #{x,y,..,z} where nCk x + nC{k+1} y + ... + nCn z == sum
		if (memo(k, sum) != -1)
			return memo(k, sum);

		if (k == n)
			return memo(k, sum) = 1;
		int ans = 0;
		for (int v = 0; v*C[k] <= sum; ++v)
			ans = (ans + rec(n, k+1, sum - v*C[k], C, memo)) % MODVAL;
		return memo(k, sum) = ans;
	}
};

// 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(_, SumPyramid().countPyramids(levels, top));}
int main(){

CASE(0)
	int levels = 1; 
	int top = 47; 
	int _ = 1; 
END
CASE(1)
	int levels = 2; 
	int top = 10; 
	int _ = 11; 
END
CASE(2)
	int levels = 3; 
	int top = 2; 
	int _ = 4; 
END
CASE(3)
	int levels = 5; 
	int top = 7; 
	int _ = 18; 
END
CASE(4)
	int levels = 1000; 
	int top = 1000; 
	int _ = -1; 
END
/*
CASE(5)
	int levels = ; 
	int top = ; 
	int _ = ; 
END
*/
}
// END CUT HERE