ADDED SRM/669-U/1A-U.cpp Index: SRM/669-U/1A-U.cpp ================================================================== --- SRM/669-U/1A-U.cpp +++ SRM/669-U/1A-U.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class SubdividedSlimes { public: + int needCut(int S, int M) + { + int L=0, R=S; + while(R-L>1) { + int C = (L+R)/2; + (maxScore(S, C) >= M ? R : L) = C; + } + return R==S ? -1 : R; + } + + LL maxScore(int S, int Turn) { + LL score = 0; + + multiset> X; + X.insert(S); + for(int t=0; t1; ++t) { + int V = *X.begin(), U = V/2; + X.erase(X.begin()); + score += U*(V-U); + X.insert(U); + X.insert(V-U); + } + return score; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, SubdividedSlimes().needCut(S, M));} +int main(){ + +CASE(0) + int S = 3; + int M = 2; + int _ = 1; +END +CASE(1) + int S = 3; + int M = 3; + int _ = 2; +END +CASE(2) + int S = 3; + int M = 4; + int _ = -1; +END +CASE(3) + int S = 765; + int M = 271828; + int _ = 14; +END +/* +CASE(4) + int S = ; + int M = ; + int _ = ; +END +CASE(5) + int S = ; + int M = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/669-U/1B.cpp Index: SRM/669-U/1B.cpp ================================================================== --- SRM/669-U/1B.cpp +++ SRM/669-U/1B.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +static const 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; } + +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } +mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } +mint operator/(mint x, mint y) { return x/=y; } + +vector FAC_(1,1); +mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*LL(FAC_.size()) ); return FAC_[size_t(n)]; } + +template +struct DP2 +{ + const int N1, N2; + vector data; + DP2(){} + 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 LineMST { public: + + int count(int N, int L) + { + DP2 dp(N, L+1, -1); + DP2 dp_sub(N, L+1, -1); + return (solve(N-1, L, L, dp, dp_sub)*FAC(N)/2).val; + } + + // maximum <= L + mint solve(int N, int L, const int OL, DP2& dp, DP2& dp_sub) + { + if(N==0) return 1; + if(L==0) return 0; + if(dp(N,L) != -1) + return dp(N,L); + + mint total = 0; + for(int XL=1; XL<=L; ++XL) + total += solve_sub(N, XL, OL, dp, dp_sub); + return dp(N,L) = total.val; + } + + // L = exact maximum. + int solve_sub(int N, int L, const int OL, DP2& dp, DP2& dp_sub) { + if(N==0) return 1; + if(L==0) return 0; + if(dp_sub(N,L) != -1) + return dp_sub(N,L); + + mint total = 0; + for(int maxP=1; maxP<=N; ++maxP) { + total += + mint(solve(maxP-1, L-1, OL, dp, dp_sub)) + * solve(N-maxP, L, OL, dp, dp_sub) + * POW(OL-L+1, maxP*(N-maxP+1)-1); + } + return dp_sub(N,L)=total.val; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, LineMST().count(N, L));} +int main(){ + +CASE(0) + int N = 3; + int L = 2; + int _ = 15; +END +CASE(1) + int N = 2; + int L = 10; + int _ = 10; +END +CASE(2) + int N = 3; + int L = 1; + int _ = 3; +END +CASE(3) + int N = 8; + int L = 41; + int _ = 655468587; +END +CASE(4) + int N = 50; + int L = 50; + int _ = 699887037; +END +CASE(4) + int N = 200; + int L = 200; + int _ = 152699064; +END +/* +CASE(5) + int N = ; + int L = ; + int _ = ; +END +CASE(6) + int N = ; + int L = ; + int _ = ; +END +*/ +} +// END CUT HERE