ADDED SRM/666-U/1A-U.cpp Index: SRM/666-U/1A-U.cpp ================================================================== --- SRM/666-U/1A-U.cpp +++ SRM/666-U/1A-U.cpp @@ -0,0 +1,150 @@ +#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 WalkOverATree { public: + int maxNodesVisited(vector parent, int L) + { + const int N = parent.size() + 1; + vector> G(N); + for(int i=0; i numBelow(N); { + function rec; + rec = [&](int v) { + int cnt = 1; + for(auto u: G[v]) + cnt += rec(u); + return numBelow[v] = cnt; + }; + rec(0); + } + + function rec; + map, int> memo; + rec = [&](int v, int L) { + const pair key(v, L); + if(memo.count(key)) return memo[key]; + + const vector& child = G[v]; + if(L<=0 || child.empty()) + return 1; + + int visMax = 0; + for(int i=0; i previsit; + previsit.insert(0); + for(int k=0; k pv2 = previsit; + for(int z: previsit) + pv2.insert(z + numBelow[child[k]]); + previsit = pv2; + } + + for(int p: previsit) if(p*2 <= L) + visMax = max(visMax, 1 + p + rec(child[i], L-p*2-1)); + } + return memo[key] = visMax; + }; + return rec(0, L); + } +}; + +// 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(_, WalkOverATree().maxNodesVisited(parent, L));} +int main(){ +/* +CASE(0) + int parent_[] = {0, 0}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 2; + int _ = 2; +END +*/ +CASE(1) + int parent_[] = {0, 0}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 3; + int _ = 3; +END +CASE(2) + int parent_[] = {0, 1, 2, 3}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 2; + int _ = 3; +END +CASE(3) + int parent_[] = {0,0,0,0,2,4,2,3,1}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 1; + int _ = 2; +END +CASE(4) + int parent_[] = {0,0,1,2,3,2,3,1,3,0,1,8,6,8,0,5,15,0,9}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 4; + int _ = 5; +END +CASE(5) + int parent_[] = {0,0,0,1,1,3,5,1,4,5,2,2,10,5,10,10,11,13,8,3,18,15,20,20,23,8,11,26,4}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 26; + int _ = 17; +END +CASE(6) + int parent_[] = {0, 0, 2, 0} +; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 100; + int _ = 5; +END +CASE(7) + int parent_[] = {0, 0, 2}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 4; + int _ = 4; +END +CASE(8) + int parent_[] = {0,1,2,0,4,4,4,4}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 3; + int _ = 4; +END +CASE(9) + int parent_[] = {0,0,2,0,4,5}; + vector parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)); + int L = 7; + int _ = 6; +END +} +// END CUT HERE ADDED SRM/666-U/1B.cpp Index: SRM/666-U/1B.cpp ================================================================== --- SRM/666-U/1B.cpp +++ SRM/666-U/1B.cpp @@ -0,0 +1,145 @@ +#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; } + +vector< vector > CP_; +mint C(int n, int k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector(nn+1,1)); + for(int kk=1; kk memoOO, memoCO, memoCC; + + int findSum(int n) + { + N = n; + memoOO.assign(N+1, -1); + memoCO.assign(N+1, -1); + memoCC.assign(N+1, -1); + return recOO(n); + } + + int recOO(int k) { + if(k == 0) return 1; + if(k == 1) return N; + if(memoOO[k] != -1) return memoOO[k]; + + mint total = 0; + total += mint(N) * recCO(k-1); // left + total += mint(N) * recCO(k-1); // right + for(int i=1; i+1 +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(_, SumOverPermutations().findSum(n));} +int main(){ + +CASE(0) + int n = 2; + int _ = 4; +END +CASE(1) + int n = 3; + int _ = 66; +END +CASE(2) + int n = 10; + int _ = 58310114; +END +CASE(3) + int n = 3900; + int _ = 940560814; +END +/* +CASE(4) + int n = ; + int _ = ; +END +CASE(5) + int n = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/666-U/1C-U.cpp Index: SRM/666-U/1C-U.cpp ================================================================== --- SRM/666-U/1C-U.cpp +++ SRM/666-U/1C-U.cpp @@ -0,0 +1,186 @@ +#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; } + +class CountBinarySequences { public: + int countSequences(int n, int k, vector L, vector R) + { + const int N = L.size(); + vector> RNG; + for(int i=0; i a, pair b){ + if(a.second-a.first != b.second-b.first) + return a.second-a.first < b.second-b.first; + return a> child(N+1); + for(int i=0; i memo; + int solve( + const vector>& RNG, + const vector>& rng_list, + const int K, + int idx, int L1, int R1) { + const bool must_be_even = (idx+1 == RNG.size()); + const int key = (idx*(K+1)+L1)*(K+1)+R1; + if(memo[key] != -1) return memo[key]; + + // RNG[idx] is the current range you need to consider. + // rng_list[idx] is left-to-right sorted sub-ranges. + // the region must be sum-even if must_be_even. + // more than K consecutive 1's are not allowed. + // left-end must have exactly L1 consecutive 1's. + // right-end must have exactly R1 consecutive 1's. + return memo[key] = 0; + } +}; + +// 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(_, CountBinarySequences().countSequences(n, k, L, R));} +int main(){ + +CASE(0) + int n = 4; + int k = 2; + int L_[] = {2}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {3}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 5; +END +CASE(1) + int n = 4; + int k = 1; + int L_[] = {2}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {2}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 6; +END +CASE(2) + int n = 6; + int k = 3; + int L_[] = {1, 2, 3}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {6, 5, 4}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 6; +END +CASE(3) + int n = 1000; + int k = 4; + int L_[] = {10, 101, 201, 110, 121}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {100, 200, 300, 120, 130}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 444743885; +END +CASE(4) + int n = 1; + int k = 5; + int L_[] = {1}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 1; +END +CASE(5) + int n = 998; + int k = 2; + int L_[] = {313,365,189,913,334,360,160,931,313,402,389,328,376,47,906,383,381,927,338,178,934,933,162,332,191,188,380,912,970,360,161,179,966,405,971,381}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {313,365,189,913,334,362,160,931,340,405,389,329,398,425,907,383,382,927,338,178,934,933,162,332,191,188,384,912,970,365,161,179,966,405,971,381}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 420889003; +END +/* +CASE(6) + int n = ; + int k = ; + int L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +CASE(7) + int n = ; + int k = ; + int L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +*/ +} +// END CUT HERE