47a150c34b 2015-10-14 kinaba: #include <iostream> 47a150c34b 2015-10-14 kinaba: #include <sstream> 47a150c34b 2015-10-14 kinaba: #include <iomanip> 47a150c34b 2015-10-14 kinaba: #include <vector> 47a150c34b 2015-10-14 kinaba: #include <string> 47a150c34b 2015-10-14 kinaba: #include <map> 47a150c34b 2015-10-14 kinaba: #include <set> 47a150c34b 2015-10-14 kinaba: #include <algorithm> 47a150c34b 2015-10-14 kinaba: #include <numeric> 47a150c34b 2015-10-14 kinaba: #include <iterator> 47a150c34b 2015-10-14 kinaba: #include <functional> 47a150c34b 2015-10-14 kinaba: #include <complex> 47a150c34b 2015-10-14 kinaba: #include <queue> 47a150c34b 2015-10-14 kinaba: #include <stack> 47a150c34b 2015-10-14 kinaba: #include <cmath> 47a150c34b 2015-10-14 kinaba: #include <cassert> 47a150c34b 2015-10-14 kinaba: #include <tuple> 47a150c34b 2015-10-14 kinaba: using namespace std; 47a150c34b 2015-10-14 kinaba: typedef long long LL; 47a150c34b 2015-10-14 kinaba: typedef complex<double> CMP; 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: static const unsigned MODVAL = 1000000007; 47a150c34b 2015-10-14 kinaba: struct mint 47a150c34b 2015-10-14 kinaba: { 47a150c34b 2015-10-14 kinaba: unsigned val; 47a150c34b 2015-10-14 kinaba: mint():val(0){} 47a150c34b 2015-10-14 kinaba: mint(int x):val(x%MODVAL) {} 47a150c34b 2015-10-14 kinaba: mint(unsigned x):val(x%MODVAL) {} 47a150c34b 2015-10-14 kinaba: mint(LL x):val(x%MODVAL) {} 47a150c34b 2015-10-14 kinaba: }; 47a150c34b 2015-10-14 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 47a150c34b 2015-10-14 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 47a150c34b 2015-10-14 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 47a150c34b 2015-10-14 kinaba: mint operator+(mint x, mint y) { return x+=y; } 47a150c34b 2015-10-14 kinaba: mint operator-(mint x, mint y) { return x-=y; } 47a150c34b 2015-10-14 kinaba: mint operator*(mint x, mint y) { return x*=y; } 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 47a150c34b 2015-10-14 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 47a150c34b 2015-10-14 kinaba: mint operator/(mint x, mint y) { return x/=y; } 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: vector<mint> FAC_(1,1); 47a150c34b 2015-10-14 kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*LL(FAC_.size()) ); return FAC_[size_t(n)]; } 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: template<typename T> 47a150c34b 2015-10-14 kinaba: struct DP2 47a150c34b 2015-10-14 kinaba: { 47a150c34b 2015-10-14 kinaba: const int N1, N2; 47a150c34b 2015-10-14 kinaba: vector<T> data; 47a150c34b 2015-10-14 kinaba: DP2(){} 47a150c34b 2015-10-14 kinaba: DP2(int N1, int N2, const T& t = T()) 47a150c34b 2015-10-14 kinaba: : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<28)); } 47a150c34b 2015-10-14 kinaba: T& operator()(int i1, int i2) 47a150c34b 2015-10-14 kinaba: { return data[ (i1*N2)+i2 ]; } 47a150c34b 2015-10-14 kinaba: void swap(DP2& rhs) 47a150c34b 2015-10-14 kinaba: { data.swap(rhs.data); } 47a150c34b 2015-10-14 kinaba: }; 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: class LineMST { public: 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: int count(int N, int L) 47a150c34b 2015-10-14 kinaba: { 47a150c34b 2015-10-14 kinaba: DP2<int> dp(N, L+1, -1); 47a150c34b 2015-10-14 kinaba: DP2<int> dp_sub(N, L+1, -1); 47a150c34b 2015-10-14 kinaba: return (solve(N-1, L, L, dp, dp_sub)*FAC(N)/2).val; 47a150c34b 2015-10-14 kinaba: } 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: // maximum <= L 47a150c34b 2015-10-14 kinaba: mint solve(int N, int L, const int OL, DP2<int>& dp, DP2<int>& dp_sub) 47a150c34b 2015-10-14 kinaba: { 47a150c34b 2015-10-14 kinaba: if(N==0) return 1; 47a150c34b 2015-10-14 kinaba: if(L==0) return 0; 47a150c34b 2015-10-14 kinaba: if(dp(N,L) != -1) 47a150c34b 2015-10-14 kinaba: return dp(N,L); 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: mint total = 0; 47a150c34b 2015-10-14 kinaba: for(int XL=1; XL<=L; ++XL) 47a150c34b 2015-10-14 kinaba: total += solve_sub(N, XL, OL, dp, dp_sub); 47a150c34b 2015-10-14 kinaba: return dp(N,L) = total.val; 47a150c34b 2015-10-14 kinaba: } 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: // L = exact maximum. 47a150c34b 2015-10-14 kinaba: int solve_sub(int N, int L, const int OL, DP2<int>& dp, DP2<int>& dp_sub) { 47a150c34b 2015-10-14 kinaba: if(N==0) return 1; 47a150c34b 2015-10-14 kinaba: if(L==0) return 0; 47a150c34b 2015-10-14 kinaba: if(dp_sub(N,L) != -1) 47a150c34b 2015-10-14 kinaba: return dp_sub(N,L); 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: mint total = 0; 47a150c34b 2015-10-14 kinaba: for(int maxP=1; maxP<=N; ++maxP) { 47a150c34b 2015-10-14 kinaba: total += 47a150c34b 2015-10-14 kinaba: mint(solve(maxP-1, L-1, OL, dp, dp_sub)) 47a150c34b 2015-10-14 kinaba: * solve(N-maxP, L, OL, dp, dp_sub) 47a150c34b 2015-10-14 kinaba: * POW(OL-L+1, maxP*(N-maxP+1)-1); 47a150c34b 2015-10-14 kinaba: } 47a150c34b 2015-10-14 kinaba: return dp_sub(N,L)=total.val; 47a150c34b 2015-10-14 kinaba: } 47a150c34b 2015-10-14 kinaba: }; 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: // BEGIN CUT HERE 47a150c34b 2015-10-14 kinaba: #include <ctime> 47a150c34b 2015-10-14 kinaba: double start_time; string timer() 47a150c34b 2015-10-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 47a150c34b 2015-10-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 47a150c34b 2015-10-14 kinaba: { os << "{ "; 47a150c34b 2015-10-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 47a150c34b 2015-10-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 47a150c34b 2015-10-14 kinaba: void verify_case(const int& Expected, const int& Received) { 47a150c34b 2015-10-14 kinaba: bool ok = (Expected == Received); 47a150c34b 2015-10-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 47a150c34b 2015-10-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 47a150c34b 2015-10-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 47a150c34b 2015-10-14 kinaba: #define END verify_case(_, LineMST().count(N, L));} 47a150c34b 2015-10-14 kinaba: int main(){ 47a150c34b 2015-10-14 kinaba: 47a150c34b 2015-10-14 kinaba: CASE(0) 47a150c34b 2015-10-14 kinaba: int N = 3; 47a150c34b 2015-10-14 kinaba: int L = 2; 47a150c34b 2015-10-14 kinaba: int _ = 15; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(1) 47a150c34b 2015-10-14 kinaba: int N = 2; 47a150c34b 2015-10-14 kinaba: int L = 10; 47a150c34b 2015-10-14 kinaba: int _ = 10; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(2) 47a150c34b 2015-10-14 kinaba: int N = 3; 47a150c34b 2015-10-14 kinaba: int L = 1; 47a150c34b 2015-10-14 kinaba: int _ = 3; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(3) 47a150c34b 2015-10-14 kinaba: int N = 8; 47a150c34b 2015-10-14 kinaba: int L = 41; 47a150c34b 2015-10-14 kinaba: int _ = 655468587; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(4) 47a150c34b 2015-10-14 kinaba: int N = 50; 47a150c34b 2015-10-14 kinaba: int L = 50; 47a150c34b 2015-10-14 kinaba: int _ = 699887037; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(4) 47a150c34b 2015-10-14 kinaba: int N = 200; 47a150c34b 2015-10-14 kinaba: int L = 200; 47a150c34b 2015-10-14 kinaba: int _ = 152699064; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: /* 47a150c34b 2015-10-14 kinaba: CASE(5) 47a150c34b 2015-10-14 kinaba: int N = ; 47a150c34b 2015-10-14 kinaba: int L = ; 47a150c34b 2015-10-14 kinaba: int _ = ; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: CASE(6) 47a150c34b 2015-10-14 kinaba: int N = ; 47a150c34b 2015-10-14 kinaba: int L = ; 47a150c34b 2015-10-14 kinaba: int _ = ; 47a150c34b 2015-10-14 kinaba: END 47a150c34b 2015-10-14 kinaba: */ 47a150c34b 2015-10-14 kinaba: } 47a150c34b 2015-10-14 kinaba: // END CUT HERE