ADDED SRM/646-U/1A.cpp Index: SRM/646-U/1A.cpp ================================================================== --- SRM/646-U/1A.cpp +++ SRM/646-U/1A.cpp @@ -0,0 +1,106 @@ +#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 char* BAD = "XXX"; + +class AB { public: + map, string> memo; + string create(int N, int KPair, int NumB) + { + if(N==0) + return (KPair==0 && NumB==0 ? "" : BAD); + + tuple key(N, KPair, NumB); + if(memo.count(key)) + return memo[key]; + + auto a = create(N-1, KPair-NumB, NumB); + if(a != BAD) + return memo[key] = "A" + a; + if(NumB>=1) { + auto b = create(N-1, KPair, NumB-1); + if(b != BAD) + return memo[key] = "B" + b; + } + return memo[key] = BAD; + } + + string createString(int N, int K) + { + for(int NumB=0; NumB<=N; ++NumB) { + auto s = create(N, K, NumB); + if(s != BAD) + return s; + } + return ""; + } +}; + +// 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 string& Expected, const string& 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(_, AB().createString(N, K));} +int main(){ + +CASE(0) + int N = 3; + int K = 2; + string _ = "ABB"; +END +CASE(1) + int N = 2; + int K = 0; + string _ = "BA"; +END +CASE(2) + int N = 5; + int K = 8; + string _ = ""; +END +CASE(3) + int N = 10; + int K = 12; + string _ = "BAABBABAAB"; +END +/* +CASE(4) + int N = ; + int K = ; + string _ = ; +END +CASE(5) + int N = ; + int K = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/646-U/1B-U.cpp Index: SRM/646-U/1B-U.cpp ================================================================== --- SRM/646-U/1B-U.cpp +++ SRM/646-U/1B-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; } +mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } + +// cnt, ith +typedef tuple item; +struct cmp { + bool operator()(const item& lhs, const item& rhs) { + LL e1,b1,e2,b2; + tie(e1,b1) = lhs; + tie(e2,b2) = rhs; + // b1 2^e1 > b2 2^e2 ? + + LL e = abs(e1-e2); + if(e1==e2) + return b1 > b2; + if(e1 > e2) { + // b1 2^e > b2 ? + LL bb=b1; + for(int i=0; ib2) return true; + } + return false; + } else { + // b1 < b2 2^e ? + LL bb=b2; + for(int i=0; i1) { + int C=(L+R)/2; + (N<=max_buy(K, C) ? R : L) = C; + } + // L+1==R + + // Invariant: Can buy N apples with max in (LL*2^(R-1), RR*2^(R-1)] + int Ll=K/2, RR=K; + while(RR-Ll>1) { + int CC=(Ll+RR)/2; + (N<=max_buy(K, R, CC) ? RR : Ll) = CC; + } + //Ll+1==RR + + // Should be close enough. Do naively. + LL total = K*L + Ll; + + priority_queue, cmp> Q; + Q.emplace(R, RR); + int c=R+1; + for(LL div=2;; div*=2,++c) { + total += Ll/div; + Q.emplace(c, Ll/div+1); + if(Ll/div==0) + break; + } + + int ans = 0; + for(; total<=N; ++total) { + LL cnt,ith; + tie(cnt,ith) = Q.top(); + Q.pop(); + + ans = (mint(ith)*POW(2,cnt-1)).val; + + if(ith==1) + Q.emplace(cnt+1,ith); + if(ith=1; div*=2) + total += Km/div; + return total; + } +}; + +// 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(_, KitayutaMart().lastPrice(N, K));} +int main(){ + +CASE(0) + int N = 3; + int K = 1; + int _ = 4; +END +CASE(1) + int N = 3; + int K = 2; + int _ = 2; +END +CASE(2) + int N = 5; + int K = 3; + int _ = 4; +END +CASE(3) + int N = 1000000000; + int K = 1; + int _ = 570312504; +END +CASE(4) + int N = 987654321; + int K = 876543210; + int _ = 493827168; +END +/* +CASE(5) + int N = ; + int K = ; + int _ = ; +END +CASE(6) + int N = ; + int K = ; + int _ = ; +END +*/ +} +// END CUT HERE