ADDED SRM/663-U/1A.cpp Index: SRM/663-U/1A.cpp ================================================================== --- SRM/663-U/1A.cpp +++ SRM/663-U/1A.cpp @@ -0,0 +1,99 @@ +#include <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +#include <tuple> +using namespace std; +typedef long long LL; +typedef complex<double> CMP; + +class ABBADiv1 { public: + string canObtain(string initial, string target) + { + return can(target, initial) ? "Possible" : "Impossible"; + } + + map<string, bool> memo; + bool can(const string& X, const string& S) + { + if(X == S) + return true; + if(X.size() < S.size()) + return false; + if(memo.count(X)) + return memo[X]; + if(X[X.size()-1] == 'A' && can(X.substr(0, X.size()-1), S)) return memo[X] = true; + if(X[0] == 'B' && can(rev(X.substr(1, X.size())), S)) return memo[X] = true; + return memo[X] = false; + } + + string rev(const string& x) { return string(x.rbegin(), x.rend()); } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::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(_, ABBADiv1().canObtain(initial, target));} +int main(){ + +CASE(0) + string initial = "A"; + string target = "BABA"; + string _ = "Possible"; +END +CASE(1) + string initial = "BAAAAABAA"; + string target = "BAABAAAAAB"; + string _ = "Possible"; +END +CASE(2) + string initial = "A"; + string target = "ABBA"; + string _ = "Impossible"; +END +CASE(3) + string initial = "AAABBAABB"; + string target = "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB"; + string _ = "Possible"; +END +CASE(4) + string initial = "AAABAAABB"; + string target = "BAABAAABAABAABBBAAAAAABBAABBBBBBBABB"; + string _ = "Impossible"; +END +/* +CASE(5) + string initial = ; + string target = ; + string _ = ; +END +CASE(6) + string initial = ; + string target = ; + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/663-U/1B-U.cpp Index: SRM/663-U/1B-U.cpp ================================================================== --- SRM/663-U/1B-U.cpp +++ SRM/663-U/1B-U.cpp @@ -0,0 +1,213 @@ +#include <iostream> +#include <sstream> +#include <iomanip> +#include <vector> +#include <string> +#include <map> +#include <set> +#include <algorithm> +#include <numeric> +#include <iterator> +#include <functional> +#include <complex> +#include <queue> +#include <stack> +#include <cmath> +#include <cassert> +#include <tuple> +using namespace std; +typedef long long LL; +typedef complex<double> 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<mint> FAC_(1,1); +mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*LL(FAC_.size()) ); return FAC_[n]; } + +// nCk :: O(log MODVAL) time, O(n) space. +mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } + +// nCk :: O(1) time, O(n^2) space. +vector< vector<mint> > CP_; +mint C(int n, int k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector<mint>(nn+1,1)); + for(int kk=1; kk<nn; ++kk) + CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; + } + return k<0 || n<k ? 0 : CP_[n][k]; +} + +class ChangingChange { public: + vector <int> countWays(vector <int> ways, vector <int> valueRemoved, vector <int> numRemoved) + { + const int D = ways.size()-1; + + vector<mint> coins(1, 0); + for(int v=1; v<=D; ++v) + coins.push_back(ways[v] - eval(coins,v)); + + vector<int> ans; + for(int q=0; q<valueRemoved.size(); ++q) { + const int v=valueRemoved[q], n=numRemoved[q]; + coins[v] -= n; // or += MODVAL-n, meaning mult (1+x^v)^(MODVAL-n) + // then if cur is known and (1+x^v)^(MODVAL-n) is known, + // O(D) to calc eval(coins,D) + ans.push_back(eval(coins,D).val); + coins[v] += n; + } + return ans; + } + + // Calc \Pi_v (1 + x^v)^c[v] 's coeefficient at x^D + // c[v] may be very large + // D <= 2000 + // v <= 2000 + mint eval(const vector<mint>& c, int D) + { + vector<mint> cur(D+1, 0); + cur[0] = 1; + for(int v=1; v<c.size(); ++v) { + cur = polymul(cur, polypow(v, c[v].val, D), D); + } + return D<cur.size() ? cur[D] : 0; + } + + vector<mint> polymul(const vector<mint>& a, const vector<mint>& b, int D) + { + int maxD = max<int>(D, (a.size()-1)*(b.size()-1)); + vector<mint> c(maxD+1); + for(int i=0; i<a.size(); ++i) + for(int k=0; k<b.size(); ++k) + if(i+k<=maxD) + c[i+k] += a[i]*b[k]; + return c; + } + vector<mint> polypow(int V, int K, int D) + { + vector<mint> poly; + // (1+x^V)^K upto x^D + mint Ckd = 1; + for(int d=0; d<=K; ++d) { + LL dd = LL(d)*V; + if(dd > D) + break; + poly.resize(int(dd)+1); + poly[int(dd)] = Ckd; + + Ckd *= (K-d); + Ckd /= (d+1); + } + return poly; + } +}; + +// BEGIN CUT HERE +#include <ctime> +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) + { os << "{ "; + for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector <int>& Expected, const vector <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(_, ChangingChange().countWays(ways, valueRemoved, numRemoved));} +int main(){ + +CASE(0) + int ways_[] = {1, 4, 6}; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = {1, 1, 1}; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = {1, 2, 3}; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = {3, 1, 0 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int ways_[] = {1, 2, 1, 0, 0, 0, 0, 0, 7}; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = {8, 8, 1, 1}; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = {1, 7, 1, 2}; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = {6, 0, 7, 7 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int ways_[] = {1, 2, 3, 6, 9, 14}; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = {1, 2, 3, 4, 5, 1, 2, 3, 4, 5}; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2}; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = {9, 10, 11, 12, 13, 6, 8, 8, 10, 12 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int ways_[] = {1, 0}; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = {1, 1}; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = {1, 1000000}; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = {1000000006, 999000007 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int ways_[] = {1, 2, 3, 6, 9, 14}; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = {1, 3, 5}; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = {1000000, 4, 2}; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = {34955525, 2, 12 }; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + int ways_[] = ; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = ; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = ; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = ; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(6) + int ways_[] = ; + vector <int> ways(ways_, ways_+sizeof(ways_)/sizeof(*ways_)); + int valueRemoved_[] = ; + vector <int> valueRemoved(valueRemoved_, valueRemoved_+sizeof(valueRemoved_)/sizeof(*valueRemoved_)); + int numRemoved_[] = ; + vector <int> numRemoved(numRemoved_, numRemoved_+sizeof(numRemoved_)/sizeof(*numRemoved_)); + int __[] = ; + vector <int> _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE Index: lib/numeric/modArith.cpp ================================================================== --- lib/numeric/modArith.cpp +++ lib/numeric/modArith.cpp @@ -27,11 +27,11 @@ 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<mint> FAC_(1,1); -mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } +mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*LL(FAC_.size()) ); return FAC_[n]; } // nCk :: O(log MODVAL) time, O(n) space. mint C(LL n, LL k) { return k<0 || n<k ? 0 : FAC(n) / (FAC(k) * FAC(n-k)); } // nCk :: O(1) time, O(n^2) space.