5f2714f57a 2012-02-18 kinaba: #include <iostream> 5f2714f57a 2012-02-18 kinaba: #include <sstream> 5f2714f57a 2012-02-18 kinaba: #include <iomanip> 5f2714f57a 2012-02-18 kinaba: #include <vector> 5f2714f57a 2012-02-18 kinaba: #include <string> 5f2714f57a 2012-02-18 kinaba: #include <map> 5f2714f57a 2012-02-18 kinaba: #include <set> 5f2714f57a 2012-02-18 kinaba: #include <algorithm> 5f2714f57a 2012-02-18 kinaba: #include <numeric> 5f2714f57a 2012-02-18 kinaba: #include <iterator> 5f2714f57a 2012-02-18 kinaba: #include <functional> 5f2714f57a 2012-02-18 kinaba: #include <complex> 5f2714f57a 2012-02-18 kinaba: #include <queue> 5f2714f57a 2012-02-18 kinaba: #include <stack> 5f2714f57a 2012-02-18 kinaba: #include <cmath> 5f2714f57a 2012-02-18 kinaba: #include <cassert> 5f2714f57a 2012-02-18 kinaba: #include <cstring> 5f2714f57a 2012-02-18 kinaba: #ifdef __GNUC__ 5f2714f57a 2012-02-18 kinaba: #include <ext/hash_map> 5f2714f57a 2012-02-18 kinaba: #define unordered_map __gnu_cxx::hash_map 5f2714f57a 2012-02-18 kinaba: #else 5f2714f57a 2012-02-18 kinaba: #include <unordered_map> 5f2714f57a 2012-02-18 kinaba: #endif 5f2714f57a 2012-02-18 kinaba: using namespace std; 5f2714f57a 2012-02-18 kinaba: typedef long long LL; 5f2714f57a 2012-02-18 kinaba: typedef complex<double> CMP; 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: static const int MODVAL = 1000000007; 5f2714f57a 2012-02-18 kinaba: struct mint 5f2714f57a 2012-02-18 kinaba: { 5f2714f57a 2012-02-18 kinaba: int val; 5f2714f57a 2012-02-18 kinaba: mint():val(0){} 5f2714f57a 2012-02-18 kinaba: mint(int x):val(x%MODVAL) {} // x>=0 5f2714f57a 2012-02-18 kinaba: mint(size_t x):val(x%MODVAL) {} // x>=0 5f2714f57a 2012-02-18 kinaba: mint(LL x):val(x%MODVAL) {} // x>=0 5f2714f57a 2012-02-18 kinaba: }; 5f2714f57a 2012-02-18 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 5f2714f57a 2012-02-18 kinaba: mint operator+(mint x, mint y) { return x+=y; } 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: template<typename T> 5f2714f57a 2012-02-18 kinaba: struct DP4 5f2714f57a 2012-02-18 kinaba: { 5f2714f57a 2012-02-18 kinaba: int N1, N2, N3, N4; 5f2714f57a 2012-02-18 kinaba: vector<T> data; 5f2714f57a 2012-02-18 kinaba: DP4(int N1, int N2, int N3, int N4, const T& t = T()) 5f2714f57a 2012-02-18 kinaba: : N1(N1), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert(data.size()*sizeof(T)<(1<<26)); } 5f2714f57a 2012-02-18 kinaba: T& operator()(int i1, int i2, int i3, int i4) 5f2714f57a 2012-02-18 kinaba: { return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } 5f2714f57a 2012-02-18 kinaba: void swap(DP4& rhs) 5f2714f57a 2012-02-18 kinaba: { data.swap(rhs.data); } 5f2714f57a 2012-02-18 kinaba: }; 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: class DengklekBuildingRoads { public: 5f2714f57a 2012-02-18 kinaba: int numWays(int N, int M, int K) 5f2714f57a 2012-02-18 kinaba: { 5f2714f57a 2012-02-18 kinaba: DP4<mint> dp(N, M+1, 1<<K+1, K); 5f2714f57a 2012-02-18 kinaba: for(int a=N-1; a>=0; --a) 5f2714f57a 2012-02-18 kinaba: for(int e=0; e<=M; ++e) 5f2714f57a 2012-02-18 kinaba: for(int mask=0; mask<(1<<K+1); ++mask) 5f2714f57a 2012-02-18 kinaba: for(int b=K-1; b>=0; --b) 5f2714f57a 2012-02-18 kinaba: { 5f2714f57a 2012-02-18 kinaba: mint v = 0; 5f2714f57a 2012-02-18 kinaba: // draw edge 5f2714f57a 2012-02-18 kinaba: if(e && a+b+1<N) 5f2714f57a 2012-02-18 kinaba: v += dp(a, e-1, mask^1^(1<<b+1), b); 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: // no edge 5f2714f57a 2012-02-18 kinaba: if(b+1<K) 5f2714f57a 2012-02-18 kinaba: v += dp(a, e, mask, b+1); 5f2714f57a 2012-02-18 kinaba: else { 5f2714f57a 2012-02-18 kinaba: if(a+1<N) { 5f2714f57a 2012-02-18 kinaba: if( (mask&1)==0 ) 5f2714f57a 2012-02-18 kinaba: v += dp(a+1,e,mask>>1,0); 5f2714f57a 2012-02-18 kinaba: } else { 5f2714f57a 2012-02-18 kinaba: if( mask==0 && e==0 ) 5f2714f57a 2012-02-18 kinaba: v += 1; 5f2714f57a 2012-02-18 kinaba: } 5f2714f57a 2012-02-18 kinaba: } 5f2714f57a 2012-02-18 kinaba: dp(a,e,mask,b) = v; 5f2714f57a 2012-02-18 kinaba: } 5f2714f57a 2012-02-18 kinaba: return dp(0,M,0,0).val; 5f2714f57a 2012-02-18 kinaba: } 5f2714f57a 2012-02-18 kinaba: }; 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: // BEGIN CUT HERE 5f2714f57a 2012-02-18 kinaba: #include <ctime> 5f2714f57a 2012-02-18 kinaba: double start_time; string timer() 5f2714f57a 2012-02-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 5f2714f57a 2012-02-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 5f2714f57a 2012-02-18 kinaba: { os << "{ "; 5f2714f57a 2012-02-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 5f2714f57a 2012-02-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 5f2714f57a 2012-02-18 kinaba: void verify_case(const int& Expected, const int& Received) { 5f2714f57a 2012-02-18 kinaba: bool ok = (Expected == Received); 5f2714f57a 2012-02-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 5f2714f57a 2012-02-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 5f2714f57a 2012-02-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 5f2714f57a 2012-02-18 kinaba: #define END verify_case(_, DengklekBuildingRoads().numWays(N, M, K));} 5f2714f57a 2012-02-18 kinaba: int main(){ 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: CASE(0) 5f2714f57a 2012-02-18 kinaba: int N = 3; 5f2714f57a 2012-02-18 kinaba: int M = 4; 5f2714f57a 2012-02-18 kinaba: int K = 1; 5f2714f57a 2012-02-18 kinaba: int _ = 3; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(1) 5f2714f57a 2012-02-18 kinaba: int N = 4; 5f2714f57a 2012-02-18 kinaba: int M = 3; 5f2714f57a 2012-02-18 kinaba: int K = 3; 5f2714f57a 2012-02-18 kinaba: int _ = 4; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(2) 5f2714f57a 2012-02-18 kinaba: int N = 2; 5f2714f57a 2012-02-18 kinaba: int M = 1; 5f2714f57a 2012-02-18 kinaba: int K = 1; 5f2714f57a 2012-02-18 kinaba: int _ = 0; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(3) 5f2714f57a 2012-02-18 kinaba: int N = 5; 5f2714f57a 2012-02-18 kinaba: int M = 0; 5f2714f57a 2012-02-18 kinaba: int K = 3; 5f2714f57a 2012-02-18 kinaba: int _ = 1; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(4) 5f2714f57a 2012-02-18 kinaba: int N = 10; 5f2714f57a 2012-02-18 kinaba: int M = 20; 5f2714f57a 2012-02-18 kinaba: int K = 5; 5f2714f57a 2012-02-18 kinaba: int _ = 26964424; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(5) 5f2714f57a 2012-02-18 kinaba: int N = 1; 5f2714f57a 2012-02-18 kinaba: int M = 0; 5f2714f57a 2012-02-18 kinaba: int K = 1; 5f2714f57a 2012-02-18 kinaba: int _ = 1; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: CASE(6) 5f2714f57a 2012-02-18 kinaba: int N = 30; 5f2714f57a 2012-02-18 kinaba: int M = 30; 5f2714f57a 2012-02-18 kinaba: int K = 8; 5f2714f57a 2012-02-18 kinaba: int _ = -1; 5f2714f57a 2012-02-18 kinaba: END 5f2714f57a 2012-02-18 kinaba: 5f2714f57a 2012-02-18 kinaba: } 5f2714f57a 2012-02-18 kinaba: // END CUT HERE