f6f3e6962a 2011-04-09 kinaba: #include <iostream> f6f3e6962a 2011-04-09 kinaba: #include <sstream> f6f3e6962a 2011-04-09 kinaba: #include <iomanip> f6f3e6962a 2011-04-09 kinaba: #include <vector> f6f3e6962a 2011-04-09 kinaba: #include <string> f6f3e6962a 2011-04-09 kinaba: #include <map> f6f3e6962a 2011-04-09 kinaba: #include <set> f6f3e6962a 2011-04-09 kinaba: #include <algorithm> f6f3e6962a 2011-04-09 kinaba: #include <numeric> f6f3e6962a 2011-04-09 kinaba: #include <iterator> f6f3e6962a 2011-04-09 kinaba: #include <functional> f6f3e6962a 2011-04-09 kinaba: #include <complex> f6f3e6962a 2011-04-09 kinaba: #include <queue> f6f3e6962a 2011-04-09 kinaba: #include <stack> f6f3e6962a 2011-04-09 kinaba: #include <cmath> f6f3e6962a 2011-04-09 kinaba: #include <cassert> f6f3e6962a 2011-04-09 kinaba: #include <cstring> f6f3e6962a 2011-04-09 kinaba: using namespace std; f6f3e6962a 2011-04-09 kinaba: typedef long long LL; f6f3e6962a 2011-04-09 kinaba: typedef complex<double> CMP; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: template<typename T> f6f3e6962a 2011-04-09 kinaba: struct DP4 f6f3e6962a 2011-04-09 kinaba: { f6f3e6962a 2011-04-09 kinaba: int N1, N2, N3, N4; f6f3e6962a 2011-04-09 kinaba: vector<T> data; f6f3e6962a 2011-04-09 kinaba: DP4(int N1, int N2, int N3, int N4, const T& t = T()) f6f3e6962a 2011-04-09 kinaba: : N1(N1), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert(data.size()*sizeof(T)<(1<<26)); } f6f3e6962a 2011-04-09 kinaba: T& operator()(int i1, int i2, int i3, int i4) f6f3e6962a 2011-04-09 kinaba: { return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } f6f3e6962a 2011-04-09 kinaba: }; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: static const int MODVAL = 1000000007; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: class FoxAverageSequence { public: f6f3e6962a 2011-04-09 kinaba: int theCount(vector <int> seq) f6f3e6962a 2011-04-09 kinaba: { f6f3e6962a 2011-04-09 kinaba: const int N = seq.size(); f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: DP4<LL> dp(N,2,41,40*N+1); f6f3e6962a 2011-04-09 kinaba: for(int b=0; b<=40; ++b) f6f3e6962a 2011-04-09 kinaba: if( seq[0]==-1 || seq[0]==b ) f6f3e6962a 2011-04-09 kinaba: dp(0,0,b,b) += 1; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: for(int i=1; i<N; ++i) f6f3e6962a 2011-04-09 kinaba: for(int dn=0; dn<=1; ++dn) f6f3e6962a 2011-04-09 kinaba: for(int b=0; b<=40; ++b) f6f3e6962a 2011-04-09 kinaba: for(int s=0; s<=40*i; ++s) f6f3e6962a 2011-04-09 kinaba: if( LL v = dp(i-1,dn,b,s) ) f6f3e6962a 2011-04-09 kinaba: for(int c=0; c<=min(40,s/i); ++c) f6f3e6962a 2011-04-09 kinaba: if( seq[i]==-1 || seq[i]==c ) f6f3e6962a 2011-04-09 kinaba: if( !dn || b<=c ) f6f3e6962a 2011-04-09 kinaba: (dp(i,b>c,c,s+c) += v) %= MODVAL; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: LL ans = 0; f6f3e6962a 2011-04-09 kinaba: for(int dn=0; dn<=1; ++dn) f6f3e6962a 2011-04-09 kinaba: for(int b=0; b<=40; ++b) f6f3e6962a 2011-04-09 kinaba: for(int s=0; s<=40*N; ++s) f6f3e6962a 2011-04-09 kinaba: ans += dp(N-1,dn,b,s); f6f3e6962a 2011-04-09 kinaba: return ans % MODVAL; f6f3e6962a 2011-04-09 kinaba: } f6f3e6962a 2011-04-09 kinaba: }; f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: // BEGIN CUT HERE f6f3e6962a 2011-04-09 kinaba: #include <ctime> f6f3e6962a 2011-04-09 kinaba: double start_time; string timer() f6f3e6962a 2011-04-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f6f3e6962a 2011-04-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f6f3e6962a 2011-04-09 kinaba: { os << "{ "; f6f3e6962a 2011-04-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f6f3e6962a 2011-04-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f6f3e6962a 2011-04-09 kinaba: void verify_case(const int& Expected, const int& Received) { f6f3e6962a 2011-04-09 kinaba: bool ok = (Expected == Received); f6f3e6962a 2011-04-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f6f3e6962a 2011-04-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f6f3e6962a 2011-04-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f6f3e6962a 2011-04-09 kinaba: #define END verify_case(_, FoxAverageSequence().theCount(seq));} f6f3e6962a 2011-04-09 kinaba: int main(){ f6f3e6962a 2011-04-09 kinaba: f6f3e6962a 2011-04-09 kinaba: CASE(0) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {3, -1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 4; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(1) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {5, 3, -1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 2; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(2) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1, 0, 40}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 0; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(3) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1, 40, -1, -1, -1, 10, -1, -1, -1, 21, -1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 579347890; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(4) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1, 12, 25, 0, 18, -1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 58; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(5) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = -1; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(6) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 41; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(7) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1,-1}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = (41+1)*41/2; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: CASE(8) f6f3e6962a 2011-04-09 kinaba: int seq_[] = {-1,-1,0}; f6f3e6962a 2011-04-09 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f6f3e6962a 2011-04-09 kinaba: int _ = 81; f6f3e6962a 2011-04-09 kinaba: END f6f3e6962a 2011-04-09 kinaba: } f6f3e6962a 2011-04-09 kinaba: // END CUT HERE