SHA1 Hash: | 4fd800b3a8ad1d42cc1a75a0cc716ee53f9fad04 |
---|---|
Date: | 2011-02-23 09:21:16 |
User: | kinaba |
Comment: | Copied from private svn repository. |
Timelines: | family | ancestors | descendants | both | trunk |
Downloads: | Tarball | ZIP archive |
Other Links: | files | file ages | manifest |
- branch=trunk inherited from [9165bd3629]
- sym-trunk inherited from [9165bd3629]
Added CheckList.pptx version [41156cd7a947e2a7]
cannot compute difference between binary files
Added SRM/144/1A.cpp version [053f425528226932]
> 1 #include <vector> > 2 #include <string> > 3 using namespace std; > 4 > 5 struct BinaryCode > 6 { > 7 vector<string> decode( string message ) > 8 { > 9 vector<string> ans; > 10 ans.push_back( decode(0, message) ); > 11 ans.push_back( decode(1, message) ); > 12 return ans; > 13 } > 14 > 15 string decode(int c, string Q) > 16 { > 17 string P; P += char('0'+c); > 18 for(int i=0; i<Q.size()-1; ++i) > 19 if( '0' <= Q[i]-c && Q[i]-c<='1' ) > 20 P += char(Q[i]-c), > 21 c = P[P.size()-2]-'0' + P[P.size()-1]-'0'; > 22 else > 23 return "NONE"; > 24 return c==Q[Q.size()-1]-'0' ? P : "NONE"; > 25 } > 26 };
Added SRM/144/1B.cpp version [014f566256f8eca5]
> 1 #include <vector> > 2 #include <string> > 3 #include <sstream> > 4 #include <utility> > 5 #include <algorithm> > 6 using namespace std; > 7 > 8 struct Lottery > 9 { > 10 vector<string> sortByOdds(vector<string> rules) > 11 { > 12 vector< pair<long long, string> > lots; > 13 for(int i=0; i<rules.size(); ++i) > 14 { > 15 stringstream sin(rules[i]); > 16 string name; > 17 int ch, bl; > 18 char so, un; > 19 getline(sin, name, ':') >> ch >> bl >> so >> un; > 20 > 21 lots.push_back( make_pair(numChoice(ch,bl,so=='T',un=='T > 22 } > 23 sort( lots.begin(), lots.end() ); > 24 > 25 vector<string> ans; > 26 for(int i=0; i<lots.size(); ++i) > 27 ans.push_back( lots[i].second ); > 28 return ans; > 29 } > 30 > 31 long long numChoice(long long ch, long long bl, bool so, bool un) > 32 { > 33 // Note: so && !un > 34 // 3 out of 5 === > 35 // +|++++[5][5][5], +|+++[4]+[5][5], +|+++[4][4]+[5], ... > 36 // === 3+5-1 C 3 > 37 long long a = 1; > 38 if(so) > 39 for(long long i=1; i<=bl; ++i) a = a*(ch-i+(un?1:bl))/i; > 40 else > 41 while(bl--) a *= (un ? ch-- : ch); > 42 return a; > 43 } > 44 };
Added SRM/144/1C.cpp version [709c778ec7e505b9]
> 1 #include <vector> > 2 #include <string> > 3 #include <sstream> > 4 #include <set> > 5 #include <map> > 6 using namespace std; > 7 > 8 struct PenLift > 9 { > 10 typedef pair<int, int> point; > 11 typedef map< point, set<point> > graph; > 12 > 13 int numTimes( vector<string> segments, int n ) > 14 { > 15 // Determine All Significant Coordinates > 16 set<int> xs, ys; > 17 for(int i=0; i<segments.size(); ++i) > 18 { > 19 stringstream sin(segments[i]); > 20 int x1, y1, x2, y2; sin >> x1 >> y1 >> x2 >> y2; > 21 xs.insert(x1); > 22 xs.insert(x2); > 23 ys.insert(y1); > 24 ys.insert(y2); > 25 } > 26 > 27 // Construct the graph, separated by all significant coordinates > 28 graph G; > 29 for(int i=0; i<segments.size(); ++i) > 30 { > 31 stringstream sin(segments[i]); > 32 int x1, y1, x2, y2; sin >> x1 >> y1 >> x2 >> y2; > 33 if( x1 == x2 ) > 34 { > 35 set<int>::iterator it = ys.lower_bound(min(y1,y2 > 36 set<int>::iterator ed = ys.upper_bound(max(y1,y2 > 37 for(set<int>::iterator pv=it++; it!=ed; ++it,++p > 38 G[point(x1,*pv)].insert(point(x1,*it)), > 39 G[point(x1,*it)].insert(point(x1,*pv)); > 40 } > 41 else > 42 { > 43 set<int>::iterator it = xs.lower_bound(min(x1,x2 > 44 set<int>::iterator ed = xs.upper_bound(max(x1,x2 > 45 for(set<int>::iterator pv=it++; it!=ed; ++it,++p > 46 G[point(*pv,y1)].insert(point(*it,y1)), > 47 G[point(*it,y1)].insert(point(*pv,y1)); > 48 } > 49 } > 50 > 51 // count the number of odd vertices (/2) per connected component > 52 set<point> vis; > 53 > 54 int cnt = 0; > 55 for(graph::iterator s=G.begin(); s!=G.end(); ++s) > 56 if( !vis.count(s->first) ) > 57 cnt += max(1, numOdd(s->first, G, vis, n)/2); > 58 return cnt - 1; // first touch do not require a pen-lift > 59 } > 60 > 61 // simple dfs > 62 int numOdd( point p, graph& G, set<point>& vis, int n ) > 63 { > 64 if( vis.count(p) ) > 65 return 0; > 66 vis.insert(p); > 67 > 68 int cnt = G[p].size()*n % 2; // if odd degree then 1 else 0 > 69 for(set<point>::iterator it=G[p].begin(); it!=G[p].end(); ++it) > 70 cnt += numOdd(*it, G, vis, n); > 71 return cnt; > 72 } > 73 };
Added SRM/145/1A.cpp version [070f78c25ba825a7]
> 1 #include <vector> > 2 #include <numeric> > 3 #include <algorithm> > 4 #include <utility> > 5 using namespace std; > 6 > 7 struct Bonuses > 8 { > 9 vector<int> getDivision(vector<int> points) > 10 { > 11 int sum = accumulate(points.begin(), points.end(), 0); > 12 > 13 int left = 100; > 14 vector<int> ans(points.size()); > 15 for(int i=0; i<points.size(); ++i) > 16 left -= (ans[i] = points[i]*100/sum); > 17 > 18 vector< pair<int,int> > poid(points.size()); > 19 for(int i=0; i<points.size(); ++i) > 20 poid[i] = make_pair( -points[i], i ); > 21 sort(poid.begin(), poid.end()); > 22 > 23 for(int i=0; left--; ++i) > 24 ans[poid[i].second]++; > 25 return ans; > 26 } > 27 };
Added SRM/145/1B.cpp version [aea0e2196df4be28]
> 1 #include <vector> > 2 #include <string> > 3 #include <sstream> > 4 #include <cstdlib> > 5 using namespace std; > 6 > 7 struct VendingMachine > 8 { > 9 int motorUse(vector<string> prices, vector<string> purchases) > 10 { > 11 vector<int> sum; > 12 vector< vector<int> > pri(prices.size()); > 13 for(int i=0; i<prices.size(); ++i) > 14 { > 15 stringstream sin(prices[i]); > 16 for(int j=0,n; sin>>n; ++j) > 17 {if(j>=sum.size())sum.push_back(0); sum[j]+=n; p > 18 } > 19 int M = pri[0].size(); > 20 > 21 int mot = 0; > 22 int pc=0, pt=-5; > 23 for(int i=0; i<purchases.size(); ++i) > 24 { > 25 stringstream sin(purchases[i]); > 26 string a; getline(sin,a,','); > 27 string b; getline(sin,b,':'); > 28 string c; getline(sin,c); > 29 int sh = atoi(a.c_str()); > 30 int cl = atoi(b.c_str()); > 31 int t = atoi(c.c_str()); > 32 > 33 if( pri[sh][cl] == 0 ) > 34 return -1; > 35 > 36 if( t-pt >= 5 ) > 37 { > 38 // rotate > 39 int hc = max_element(sum.begin(), sum.end()) - s > 40 mot += min( abs(hc-pc), M-abs(hc-pc) ); > 41 pc = hc; > 42 } > 43 > 44 // buy > 45 sum[cl] -= pri[sh][cl]; > 46 pri[sh][cl] = 0; > 47 mot += min( abs(cl-pc), M-abs(cl-pc) ); > 48 pc = cl; > 49 pt = t; > 50 } > 51 > 52 // rotate > 53 int hc = max_element(sum.begin(), sum.end()) - sum.begin(); > 54 mot += min( abs(hc-pc), M-abs(hc-pc) ); > 55 pc = hc; > 56 > 57 return mot; > 58 } > 59 };
Added SRM/145/1C.cpp version [55b5f1c30cb956ae]
> 1 #include <vector> > 2 using namespace std; > 3 > 4 struct HillHike > 5 { > 6 long long numPaths(int distance, int maxHeight, vector<int> landmarks) > 7 { > 8 // reached-maxHeight?, #reached-landmarks, current-height > 9 long long buf1[2][51][51]={0}, buf2[2][51][51], (*dp)[51][51]=bu > 10 dp[0][0][0] = 1; > 11 > 12 for(int d=1; d<=distance; ++d) > 13 { > 14 long long (*dp2)[51][51] = (dp==buf1 ? buf2 : buf1); > 15 memset(dp2, 0, sizeof(long long)*2*51*51); > 16 > 17 for(int rm=0; rm<=1; ++rm) > 18 for(int rl=0; rl<=landmarks.size(); ++rl) > 19 for(int ch=0; ch<=maxHeight; ++ch) > 20 { > 21 if(ch>1 || d==distance && ch>0) > 22 { > 23 dp2[rm] > 24 [rl<landmarks.size() > 25 [ch-1] += dp[rm][rl][ > 26 } > 27 if(ch>0 || d==distance) > 28 { > 29 dp2[rm] > 30 [rl<landmarks.size() > 31 [ch] += dp[rm][rl][ch > 32 } > 33 if( ch<maxHeight ) > 34 { > 35 dp2[ch+1==maxHeight ? 1 > 36 [rl<landmarks.size() > 37 [ch+1] += dp[rm][rl][ > 38 } > 39 } > 40 > 41 dp = dp2; > 42 } > 43 > 44 return dp[1][landmarks.size()][0]; > 45 } > 46 };
Added SRM/146/1A.cpp version [d12dbd8cea2ce431]
> 1 using namespace std; > 2 > 3 struct RectangularGrid > 4 { > 5 long long countRectangles(int width, int height) > 6 { > 7 long long w = width+1; > 8 long long h = height+1; > 9 long long s = w*(w-1)/2*h*(h-1)/2; > 10 for(long long x=1; x<w && x<h; ++x) > 11 s -= (w-x)*(h-x); > 12 return s; > 13 } > 14 };
Added SRM/146/1B.cpp version [3e66f13d80d37e14]
> 1 #include <vector> > 2 #include <string> > 3 #include <set> > 4 #include <algorithm> > 5 #include <iterator> > 6 using namespace std; > 7 > 8 struct Masterbrain > 9 { > 10 int possibleSecrets(vector<string> guesses, vector<string> results) > 11 { > 12 int N = guesses.size(); > 13 int cnt = 0; > 14 for(int n=0; n<7*7*7*7; ++n) > 15 { > 16 int x[] = {n/7/7/7%7+1,n/7/7%7+1,n/7%7+1,n%7+1}; > 17 > 18 bool possible = false; > 19 for(int lie=0; lie<N && !possible; ++lie) > 20 { > 21 bool possible_by_the_lie = true; > 22 for(int i=0; i<N && possible_by_the_lie; ++i) > 23 { > 24 int g[] = {guesses[i][0]-'0',guesses[i][ > 25 int b = results[i][0]-'0'; > 26 int w = results[i][3]-'0'; > 27 > 28 if( (i==lie) == match(x,g,b,w) ) > 29 possible_by_the_lie = false; > 30 } > 31 possible |= possible_by_the_lie; > 32 } > 33 if( possible ) > 34 ++cnt; > 35 } > 36 return cnt; > 37 } > 38 > 39 bool match(int x[], int g[], int b, int w) > 40 { > 41 int true_b = 0; > 42 multiset<int> xs, gs; > 43 for(int i=0; i<4; ++i) > 44 if( g[i] == x[i] ) > 45 true_b++; > 46 else > 47 xs.insert(x[i]), gs.insert(g[i]); > 48 vector<int> p; > 49 set_intersection(xs.begin(), xs.end(), gs.begin(), gs.end(), bac > 50 return true_b==b && w==p.size(); > 51 } > 52 };
Added SRM/146/1C.cpp version [561b82c7ab9e8ca6]
> 1 #include <string> > 2 #include <queue> > 3 #include <algorithm> > 4 using namespace std; > 5 > 6 struct Roundabout > 7 { > 8 int clearUpTime(string north, string east, string south, string west) > 9 { > 10 char M[4] = {'N', 'E', 'S', 'W'}; > 11 string C[4] = {north, east, south, west}; > 12 char R[4] = {'-', '-', '-', '-'}; > 13 queue<char> Q[4]; > 14 > 15 int N = 0; > 16 for(int i=0; i<4; ++i) > 17 for(int j=0; j<C[i].size(); ++j) > 18 if( C[i][j] != '-' ) > 19 ++N; > 20 > 21 int t = 0; > 22 for( ;; ++t ) > 23 { > 24 // leave > 25 for(int i=0; i<4; ++i) > 26 if( R[i] == M[i] ) > 27 R[i] = M[i]-'A'+'a', --N; // put ghosts > 28 if( !N ) > 29 return t; > 30 > 31 // rotate > 32 rotate( R+0, R+1, R+4 ); > 33 > 34 // enter > 35 if( !Q[0].empty() && !Q[1].empty() && !Q[2].empty() && ! > 36 { > 37 if( R[0]=='-' ) > 38 R[0] = Q[0].front(), Q[0].pop(); > 39 } > 40 else > 41 { > 42 bool Go[] = { > 43 R[0]=='-' && Q[1].empty(), > 44 R[1]=='-' && Q[2].empty(), > 45 R[2]=='-' && Q[3].empty(), > 46 R[3]=='-' && Q[0].empty(), > 47 }; > 48 for(int i=0; i<4; ++i) > 49 if( Go[i] && !Q[i].empty() ) > 50 R[i] = Q[i].front(), Q[i].pop(); > 51 } > 52 > 53 // clear ghost > 54 for(int i=0; i<4; ++i) > 55 if( 'a'<=R[i] && R[i]<='z' ) > 56 R[i] = '-'; > 57 > 58 // queue > 59 for(int i=0; i<4; ++i) > 60 if( t < C[i].size() && C[i][t]!='-' ) > 61 Q[i].push( C[i][t] ); > 62 } > 63 } > 64 };
Added SRM/245/1A.cpp version [29731d8057f68993]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 double f(int a, int n) > 20 { > 21 double x = 1; > 22 while( n --> 0 ) > 23 x *= a--; > 24 return x; > 25 } > 26 double f(int a) > 27 { > 28 return f(a, a); > 29 } > 30 double C(int n, int k) > 31 { > 32 return f(n,k) / f(k,k); > 33 } > 34 > 35 class Flush > 36 { > 37 public: > 38 double size(vector <int> suits, int number) > 39 { > 40 double all = f(accumulate(suits.begin(), suits.end(), 0), number > 41 > 42 double ans = 0; > 43 for(int i1=0; i1<=number && i1<=suits[0]; ++i1) > 44 for(int i2=0; i1+i2<=number && i2<=suits[1]; ++i2) > 45 for(int i3=0; i1+i2+i3<=number && i3<=suits[2]; ++i3) > 46 { > 47 int i4 = number - i1 - i2 - i3; > 48 if( i4 <= suits[3] ) { > 49 double nf = max(max(i1,i2),max(i3,i4)); > 50 double n = C(number, i1) * C(number-i1, i2) * C( > 51 * f(suits[0],i1) * f(suits[1],i2) * f(su > 52 ans += nf * n/all; > 53 } > 54 } > 55 return ans; > 56 } > 57 }; > 58 > 59 // BEGIN CUT HERE > 60 #include <ctime> > 61 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 62 > 63 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 64 int verify_case(const double &Expected, const double &Received) { double diff = > 65 > 66 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 67 char Test_(...); > 68 int Test_(Case_<0>) { > 69 int suits_[] = {2,2,2,2}; > 70 vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); > 71 int number = 2; > 72 double RetVal = 1.1428571428571428; > 73 return verify_case(RetVal, Flush().size(suits, number)); } > 74 int Test_(Case_<1>) { > 75 int suits_[] = {1,4,7,10}; > 76 vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); > 77 int number = 22; > 78 double RetVal = 10.0; > 79 return verify_case(RetVal, Flush().size(suits, number)); } > 80 int Test_(Case_<2>) { > 81 int suits_[] = {13, 13, 13, 13}; > 82 vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); > 83 int number = 49; > 84 double RetVal = 13.0; > 85 return verify_case(RetVal, Flush().size(suits, number)); } > 86 int Test_(Case_<3>) { > 87 int suits_[] = {13, 13, 13, 13}; > 88 vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); > 89 int number = 26; > 90 double RetVal = 8.351195960938014; > 91 return verify_case(RetVal, Flush().size(suits, number)); } > 92 int Test_(Case_<4>) { > 93 int suits_[] = {13,13,13,13}; > 94 vector <int> suits(suits_, suits_+sizeof(suits_)/sizeof(*suits_)); > 95 int number = 0; > 96 double RetVal = 0.0; > 97 return verify_case(RetVal, Flush().size(suits, number)); } > 98 > 99 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 100 template<> void Run_<-1>() {} > 101 int main() { Run_<0>(); } > 102 // END CUT HERE > 103
Added SRM/245/1B.cpp version [990eb2ca96ab96cb]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class BikeLock > 20 { > 21 public: > 22 map<pair<int,string>,int> memo; > 23 int minTurns(string current, string desired) > 24 { > 25 current += "__"; > 26 return rec(0, current.size()-2, current, desired); > 27 } > 28 > 29 int rec(int i, int N, string& s, const string& d) > 30 { > 31 if( i == N ) > 32 return 0; > 33 if( s[i] == d[i] ) > 34 return rec(i+1, N, s, d); > 35 pair<int,string> key( i, s.substr(i,3) ); > 36 if( memo.count(key) ) > 37 return memo[key]; > 38 memo[key] = 9999; > 39 > 40 int ans = 9999; > 41 for(int w=1; w<=3; ++w) > 42 for(int r=-3; r<=3; ++r) if(r) > 43 { > 44 for(int j=0; j<w; ++j) > 45 s[i+j] = (s[i+j]-'0'+10+r)%10+'0'; > 46 ans = min(ans, 1 + rec(i, N, s, d)); > 47 for(int j=0; j<w; ++j) > 48 s[i+j] = (s[i+j]-'0'+10-r)%10+'0'; > 49 } > 50 return memo[key] = ans; > 51 } > 52 }; > 53 > 54 // BEGIN CUT HERE > 55 #include <ctime> > 56 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 57 > 58 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 59 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 60 > 61 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 62 char Test_(...); > 63 int Test_(Case_<0>) { > 64 string current = "555"; > 65 string desired = "464"; > 66 int RetVal = 2; > 67 return verify_case(RetVal, BikeLock().minTurns(current, desired)); } > 68 int Test_(Case_<1>) { > 69 string current = "1234"; > 70 string desired = "3456"; > 71 int RetVal = 2; > 72 return verify_case(RetVal, BikeLock().minTurns(current, desired)); } > 73 int Test_(Case_<2>) { > 74 string current = "06012005"; > 75 string desired = "06012005"; > 76 int RetVal = 0; > 77 return verify_case(RetVal, BikeLock().minTurns(current, desired)); } > 78 int Test_(Case_<3>) { > 79 string current = "123456789"; > 80 string desired = "567412490"; > 81 int RetVal = 5; > 82 return verify_case(RetVal, BikeLock().minTurns(current, desired)); } > 83 int Test_(Case_<4>) { > 84 string current = "23887547676234543215443276347856987698648735634265"; > 85 string desired = "14327805497625497814327632146531429785698765309822"; > 86 int RetVal = 34; > 87 return verify_case(RetVal, BikeLock().minTurns(current, desired)); } > 88 > 89 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 90 template<> void Run_<-1>() {} > 91 int main() { Run_<0>(); } > 92 // END CUT HERE > 93
Added SRM/245/1C.cpp version [3313e77040505eed]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 int N; > 20 vector<int> A; > 21 > 22 typedef int vert; > 23 typedef int cost; > 24 typedef pair<cost,vert> edge; > 25 typedef vector<edge> edges; > 26 > 27 vector<int> dijkstra_like( vert S, int T, int I ) > 28 { > 29 set<int> ans; > 30 for(int i=1; i<=I; ++i) ans.insert(i); > 31 > 32 priority_queue< edge, edges, greater<edge> > Q; > 33 Q.push( edge(0, S) ); > 34 > 35 // set<vert> vis; > 36 vector<bool> vis(1<<24); > 37 > 38 while( !Q.empty() ) > 39 { > 40 edge ce=Q.top(); Q.pop(); > 41 > 42 int t = ce.first; > 43 vert v = ce.second; > 44 if( vis[v] ) > 45 continue; > 46 vis[v]=true; > 47 > 48 int sw = v&511; > 49 int a[] = {(v>>9)&31, (v>>14)&31, (v>>19)&31}; > 50 > 51 ans.erase(sw); > 52 for(int i=0; i<(1<<N+1); ++i) > 53 { > 54 int sw2 = sw; > 55 int a2[] = {a[0], a[1], a[2]}; > 56 > 57 int nzMin = 9999; > 58 for(int j=0; j<N; ++j) { > 59 if( (i&(1<<j)) ) > 60 a2[j] = A[j]-a2[j]; > 61 if( a2[j] ) > 62 nzMin = min(nzMin, a2[j]); > 63 } > 64 if( (i&(1<<N)) ) sw2 = 0; > 65 if( nzMin==9999 ) continue; > 66 if( t+nzMin > T ) > 67 continue; > 68 for(int j=0; j<N; ++j) > 69 if( a2[j] ) > 70 a2[j] -= nzMin; > 71 sw2 += nzMin; > 72 vert u = sw2 | (a2[0]<<9) | (a2[1]<<14) | (a2[2]<<19); > 73 if( !vis[u] ) { > 74 Q.push( edge(t+nzMin,u) ); > 75 } > 76 } > 77 } > 78 return vector<int>(ans.begin(), ans.end()); > 79 } > 80 > 81 class SandTimers > 82 { > 83 public: > 84 vector <int> badIntervals(vector <int> timers, int maxInterval, int maxT > 85 { > 86 N = timers.size(); > 87 A = timers; > 88 return dijkstra_like( vert(), maxTime, maxInterval ); > 89 } > 90 }; > 91 > 92 // BEGIN CUT HERE > 93 #include <ctime> > 94 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 95 > 96 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 97 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 98 > 99 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 100 char Test_(...); > 101 int Test_(Case_<0>) { > 102 int timers_[] = { 5, 7 }; > 103 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 104 int maxInterval = 10; > 105 int maxTime = 10; > 106 int RetVal_[] = {1, 6, 8 }; > 107 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 108 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 109 int Test_(Case_<1>) { > 110 int timers_[] = { 2, 10, 20 }; > 111 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 112 int maxInterval = 30; > 113 int maxTime = 40; > 114 int RetVal_[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 } > 115 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 116 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 117 int Test_(Case_<2>) { > 118 int timers_[] = { 2, 5, 9 }; > 119 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 120 int maxInterval = 360; > 121 int maxTime = 360; > 122 vector <int> RetVal; > 123 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 124 int Test_(Case_<3>) { > 125 int timers_[] = { 4 }; > 126 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 127 int maxInterval = 23; > 128 int maxTime = 47; > 129 int RetVal_[] = {1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 18, 19, 21 > 130 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 131 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 132 int Test_(Case_<4>) { > 133 int timers_[] = { 20, 13 }; > 134 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 135 int maxInterval = 30; > 136 int maxTime = 30; > 137 int RetVal_[] = {1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 21 > 138 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 139 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 140 int Test_(Case_<5>) { > 141 int timers_[] = { 20, 17, 13 }; > 142 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 143 int maxInterval = 25; > 144 int maxTime = 30; > 145 int RetVal_[] = {18, 19 }; > 146 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 147 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 148 int Test_(Case_<6>) { > 149 int timers_[] = { 20, 20, 19 }; > 150 vector <int> timers(timers_, timers_+sizeof(timers_)/sizeof(*timers_)) > 151 int maxInterval = 360; > 152 int maxTime = 360; > 153 vector <int> RetVal; > 154 return verify_case(RetVal, SandTimers().badIntervals(timers, maxInterval > 155 > 156 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 157 template<> void Run_<-1>() {} > 158 int main() { Run_<0>(); } > 159 // END CUT HERE > 160
Added SRM/264/1A.cpp version [7fcb01fe631559ac]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class DivisibilityRules > 19 { > 20 public: > 21 int similar(int numerationBase, int divisor) > 22 { > 23 vector< vector<int> > mb; > 24 for(int k=2; k<numerationBase; ++k) > 25 mb.push_back( mbase(numerationBase, k) ); > 26 return count( mb.begin(), mb.end(), mb[divisor-2] ); > 27 } > 28 > 29 vector<int> mbase(int n, int k) > 30 { > 31 vector<int> r; > 32 int b = 1; > 33 for(int i=0; i<n; ++i) { > 34 r.push_back(b); > 35 b = (b*n)%k; > 36 } > 37 return r; > 38 } > 39 > 40 // BEGIN CUT HERE > 41 public: > 42 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 43 private: > 44 template <typename T> string print_array(const vector<T> &V) { ostringst > 45 void verify_case(int Case, const int &Expected, const int &Received) { c > 46 void test_case_0() { int Arg0 = 10; int Arg1 = 3; int Arg2 = 2; verify_c > 47 void test_case_1() { int Arg0 = 10; int Arg1 = 5; int Arg2 = 2; verify_c > 48 void test_case_2() { int Arg0 = 511; int Arg1 = 32; int Arg2 = 10; verif > 49 void test_case_3() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 1; verify_ca > 50 void test_case_4() { int Arg0 = 1000; int Arg1 = 999; int Arg2 = 7; veri > 51 void test_case_5() { int Arg0 = 655; int Arg1 = 532; int Arg2 = 1; verif > 52 > 53 // END CUT HERE > 54 }; > 55 // BEGIN CUT HERE > 56 int main() { DivisibilityRules().run_test(-1); } > 57 // END CUT HERE
Added SRM/264/1B.cpp version [936ab331eda74764]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 int gcd(int x, int y) > 19 { > 20 while(y) { > 21 int t = x; > 22 x = y; > 23 y = t%y; > 24 } > 25 return x; > 26 } > 27 > 28 class GradingGridIns > 29 { > 30 public: > 31 vector <string> score(vector <string> answer, vector <int> lower, vector > 32 { > 33 int lg = gcd(lower[0], lower[1]); > 34 double la=lower[0]/lg, lb=lower[1]/lg; > 35 int ug = gcd(upper[0], upper[1]); > 36 double ua=upper[0]/ug, ub=upper[1]/ug; > 37 > 38 vector<double> world; > 39 > 40 char c[] = " ./0123456789"; > 41 for(int i=0; i<sizeof(c)-1; ++i) > 42 for(int j=0; j<sizeof(c)-1; ++j) > 43 for(int k=0; k<sizeof(c)-1; ++k) > 44 for(int l=0; l<sizeof(c)-1; ++l) { > 45 try { > 46 double d = eval( string(1,c[i])+c[j]+c[k]+c[l] ) > 47 world.push_back(d); > 48 } catch( const char* ) { > 49 } > 50 } > 51 > 52 sort(world.begin(), world.end()); > 53 world.erase(unique(world.begin(), world.end()), world.end()); > 54 vector<double>::iterator lt = lower_bound( world.begin(), world. > 55 vector<double>::iterator ut = upper_bound( world.begin(), world. > 56 if( lt!=world.begin() ) --lt; > 57 > 58 vector<string> result(answer.size()); > 59 for(int i=0; i<answer.size(); ++i) > 60 { > 61 try { > 62 double d = eval(answer[i]); > 63 result[i] = (*lt<=d && (ut==world.end() || d<=*u > 64 } catch( const char* ) { > 65 result[i] = "MALFORMED"; > 66 } > 67 } > 68 return result; > 69 } > 70 > 71 static double eval( string ans ) > 72 { > 73 int ii=0; while(ii<ans.size()&&ans[ii]==' ') ++ii; > 74 ans = ans.substr(ii); > 75 ii = ans.size(); while(ii>0 && ans[ii-1]==' ') --ii; > 76 ans = ans.substr(0, ii); > 77 > 78 > 79 int dot=-1, sla=-1, numdig=0; > 80 int value = 0, value_pre = 0; > 81 for(int i=0; i<ans.size(); ++i) > 82 if( ans[i]=='.' ) > 83 { > 84 if(dot>=0 || sla>=0) > 85 throw "MALFORMED"; > 86 dot = i; > 87 value_pre = value; > 88 value = 0; > 89 } > 90 else if( ans[i]=='/' ) > 91 { > 92 if(dot>=0 || sla>=0 || numdig==0 ) > 93 throw "MALFORMED"; > 94 sla = i; > 95 value_pre = value; > 96 value = 0; > 97 } > 98 else if( '0'<=ans[i] && ans[i]<='9' ) > 99 { > 100 ++numdig; > 101 value = value*10 + ans[i]-'0'; > 102 } > 103 else > 104 throw "MALFORMED"; > 105 if(numdig==0) > 106 throw "MALFORMED"; > 107 > 108 int L = value_pre, R = value; > 109 if( sla >= 0 ) { > 110 if( R == 0 ) > 111 throw "MALFORMED"; > 112 int g = gcd(L,R); L/=g, R/=g; > 113 return L/double(R); > 114 } > 115 else if( dot >= 0 ) { > 116 return L + R / pow(10.0, double(ans.size()-dot-1)); > 117 } > 118 else { > 119 return R; > 120 } > 121 } > 122 > 123 > 124 // BEGIN CUT HERE > 125 public: > 126 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 127 private: > 128 template <typename T> string print_array(const vector<T> &V) { ostringst > 129 void verify_case(int Case, const vector <string> &Expected, const vector > 130 void test_case_0() { string Arr0[] = {"4/7 "," 4/7","4/07","8/14",".571" > 131 void test_case_1() { string Arr0[] = {" 4/7","1.01","1.02"," 000"}; vect > 132 void test_case_2() { string Arr0[] = {"1.15","1 14","1.14"," 8/7"}; vect > 133 void test_case_3() { string Arr0[] = {" ","...."," . ","1 23","8//5" > 134 void test_case_4() { string Arr0[] = {"1/0 "}; vector <string> Arg0(Arr0 > 135 > 136 // END CUT HERE > 137 }; > 138 // BEGIN CUT HERE > 139 int main() { GradingGridIns().run_test(-1); } > 140 // END CUT HERE
Added SRM/264/1C.cpp version [b836622385c9a165]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class PolygonDecomposition > 19 { > 20 public: > 21 int howMany(int n, int k) > 22 { > 23 if( n-2 < k ) > 24 return -1; > 25 > 26 LL dp[101][101] = {}; > 27 dp[2][0] = 1; > 28 for(int N=3; N<=n; ++N) > 29 dp[N][1] = 1; > 30 > 31 for(int N=4; N<=n; ++N) > 32 for(int K=2; K<=k; ++K) > 33 { > 34 LL sum = 0; > 35 for(LL v=3; v<=N-1; ++v) // use 1--v and don't use 1--3, > 36 for(int x=1; x<=K-1; ++x) // left: x pieces, rig > 37 sum += (dp[v-1][x-1]+dp[v-1][x]) * dp[N- > 38 > 39 // don't use 1--*; if use 2--N, we'll have dp[N-1][K-1] > 40 // otherwise, will have dp[N-1][K] becau > 41 // can be regarded as a singl > 42 sum += dp[N-1][K-1] + dp[N-1][K]; > 43 dp[N][K] = sum % 1000000000; > 44 } > 45 return (int) dp[n][k]; > 46 } > 47 > 48 > 49 // BEGIN CUT HERE > 50 public: > 51 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 52 private: > 53 template <typename T> string print_array(const vector<T> &V) { ostringst > 54 void verify_case(int Case, const int &Expected, const int &Received) { c > 55 void test_case_0() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 2; verify_ca > 56 void test_case_1() { int Arg0 = 100; int Arg1 = 1; int Arg2 = 1; verify_ > 57 void test_case_2() { int Arg0 = 6; int Arg1 = 4; int Arg2 = 14; verify_c > 58 void test_case_3() { int Arg0 = 31; int Arg1 = 20; int Arg2 = 956146480; > 59 void test_case_4() { int Arg0 = 3; int Arg1 = 4; int Arg2 = -1; verify_c > 60 > 61 // END CUT HERE > 62 }; > 63 // BEGIN CUT HERE > 64 int main() { PolygonDecomposition().run_test(-1); } > 65 // END CUT HERE
Added SRM/282/1A.cpp version [284412eb45068628]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TileCutting { > 23 public: > 24 int cuts(vector <string> layout) > 25 { > 26 int san=0, ni=0, ic=0; > 27 for(int y=0; y<layout.size(); y+=2) > 28 for(int x=0; x<layout[0].size(); x+=2) > 29 { > 30 string s = layout[y].substr(x,2) + layout[y+1].s > 31 if( s==".xxx" || s=="x.xx" || s=="xx.x" || s=="x > 32 ic++; > 33 else if( s=="..xx" || s=="x.x." || s=="xx.." || > 34 ni++; > 35 else if( s=="x..x" || s==".xx." ) > 36 ic+=2; > 37 else if( s=="x..." || s==".x.." || s=="..x." || > 38 san++; > 39 } > 40 int mincut=0x7fffffff; > 41 for(int nk=0; nk<=san; ++nk) > 42 if(nk+nk/3>=san) > 43 mincut = min(mincut, calc(san,ni,ic,nk)); > 44 return mincut; > 45 } > 46 > 47 int calc(int san, int ni, int ic, int nk) { > 48 // create nk pieces of L-shaped block > 49 // reuse nk pieces of single-block to form the rest of neede L-b > 50 int cut = nk*2; > 51 int piece = nk-(san-nk)*3; // single-block piecese left > 52 > 53 // form single-blocks > 54 int pi = min(ic, piece); > 55 ic -= pi; > 56 piece -= pi; > 57 > 58 // if still left, use single-blocks to form I-shaped blocks > 59 if( piece ) > 60 { > 61 ni -= piece/2; > 62 piece -= piece/2*2; > 63 } > 64 > 65 // create I-shaped blocks > 66 int xpiece = 0; > 67 if(ni) { > 68 cut += (ni+1)/2*2; > 69 xpiece = ni%2 ? 1 : 0; > 70 } > 71 > 72 // create single-blocks > 73 pi = min(ic, piece); > 74 ic -= pi; > 75 piece -= pi; > 76 if( ic ) { > 77 if( xpiece ) ic-=2, cut+=1; > 78 if(ic>0) > 79 cut += ic/4*4 + ic%4 + (ic%4==0 ? 0 : 1); > 80 } > 81 return cut; > 82 } > 83 }; > 84 > 85 // BEGIN CUT HERE > 86 #include <ctime> > 87 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 88 > 89 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 90 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 91 > 92 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 93 char Test_(...); > 94 int Test_(Case_<0>) { > 95 string layout_[] = { "..", > 96 ".." }; > 97 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 98 int RetVal = 0; > 99 return verify_case(RetVal, TileCutting().cuts(layout)); } > 100 int Test_(Case_<1>) { > 101 string layout_[] = { "x.", > 102 ".." }; > 103 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 104 int RetVal = 2; > 105 return verify_case(RetVal, TileCutting().cuts(layout)); } > 106 int Test_(Case_<2>) { > 107 string layout_[] = { ".x", > 108 "xx" }; > 109 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 110 int RetVal = 2; > 111 return verify_case(RetVal, TileCutting().cuts(layout)); } > 112 int Test_(Case_<3>) { > 113 string layout_[] = { "xxxx..xxxx", > 114 "x..x..xx..", > 115 "x..xxxxx..", > 116 "xxxxxxxxxx" }; > 117 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 118 int RetVal = 6; > 119 return verify_case(RetVal, TileCutting().cuts(layout)); } > 120 int Test_(Case_<4>) { > 121 string layout_[] = { "xxxxxx", > 122 "x....x" }; > 123 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 124 int RetVal = 3; > 125 return verify_case(RetVal, TileCutting().cuts(layout)); } > 126 int Test_(Case_<5>) { > 127 string layout_[] = { "x..x....", > 128 "x..xx...", > 129 "..xx....", > 130 "...x....", > 131 "......xx", > 132 "......xx" }; > 133 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 134 int RetVal = 4; > 135 return verify_case(RetVal, TileCutting().cuts(layout)); } > 136 int Test_(Case_<6>) { > 137 string layout_[] = { "x..xx.x..x.xx..x.xx.", > 138 "..x..x..x.x..xx...x.", > 139 ".xx...x...x...x..x..", > 140 ".xx...x.x.x...x..xx.", > 141 ".x..x...x.....x...x.", > 142 ".x.x.x..x..x..x..x.x", > 143 "...x.x.x.x.x.x.x...x", > 144 ".x..x..x...x..x...x.", > 145 "...x.x.x..x.x.x.....", > 146 "...x.x.x..x.x.xxx.x.", > 147 "xx.xx.xx.x.x.x.x..x.", > 148 ".x..xxx...x.xx...x.x", > 149 "xx..x.x...x.x.x.x..x", > 150 ".xx..x.xx.xxxxx...xx", > 151 "x....x.x...x...x.x..", > 152 ".x.xx.x..x.x.xxx.x..", > 153 "...xx.xxx.....xx.xxx", > 154 ".xx..x..xx.x...x.xx.", > 155 "x.x...x.x.xx.x..x.xx", > 156 ".....xx.x.......xx.x", > 157 "x...x.xx.x..x....x..", > 158 ".x..xxx.....x.x.x.xx" } > 159 ; > 160 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 161 int RetVal = 121; > 162 return verify_case(RetVal, TileCutting().cuts(layout)); } > 163 int Test_(Case_<7>) { > 164 string layout_[] = { ".................................................. > 165 "..................................................", > 166 "..................................................", > 167 "..................................................", > 168 "..................................................", > 169 "..................................................", > 170 "..................................................", > 171 "..................................................", > 172 "..................................................", > 173 "..................................................", > 174 "..................................................", > 175 "..................................................", > 176 "..................................................", > 177 "..................................................", > 178 "..................................................", > 179 "..................................................", > 180 "..................................................", > 181 "..................................................", > 182 "..................................................", > 183 "..................................................", > 184 "..................................................", > 185 "..................................................", > 186 "..................................................", > 187 "..................................................", > 188 "..................................................", > 189 "..................................................", > 190 "..................................................", > 191 "..................................................", > 192 "..................................................", > 193 "..................................................", > 194 "..................................................", > 195 "..................................................", > 196 "..................................................", > 197 "..................................................", > 198 "..................................................", > 199 "..................................................", > 200 "..................................................", > 201 "..................................................", > 202 "..................................................", > 203 "..................................................", > 204 "..................................................", > 205 "..................................................", > 206 "..................................................", > 207 "..................................................", > 208 "..................................................", > 209 "..................................................", > 210 "..................................................", > 211 "..................................................", > 212 "..................................................", > 213 ".................................................." } > 214 ; > 215 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 216 int RetVal = 0; > 217 return verify_case(RetVal, TileCutting().cuts(layout)); } > 218 int Test_(Case_<8>) { > 219 string layout_[] = > 220 {"x..x...x...x..x....x", > 221 ".x..x.xxx....xx..x..", > 222 "..x...x..x...xxx....", > 223 "x....x.....xx..xx...", > 224 "..x.x...x..x........", > 225 ".x...xx.xx...xxx...x", > 226 "x..x.x......xx.x....", > 227 "...xxx.....x.x..xx.."} > 228 ; > 229 vector <string> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout > 230 int RetVal = 44; > 231 return verify_case(RetVal, TileCutting().cuts(layout)); } > 232 > 233 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 234 template<> void Run_<-1>() {} > 235 int main() { Run_<0>(); } > 236 // END CUT HERE > 237
Added SRM/282/1B.cpp version [0008d0ab74cb7d3e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int gcd(int a, int b) { > 23 while(a) swap(a,b%=a); return b; > 24 } > 25 > 26 class Fractionalization { > 27 public: > 28 vector <string> partial(vector <string> definitions, int m, int n, strin > 29 { > 30 // input > 31 map< string, vector<string> > D; > 32 for(int i=0; i<definitions.size(); ++i) { > 33 stringstream sin(definitions[i]); > 34 string name; sin>>name; name=name.substr(0, name.size()- > 35 vector<string> ds; > 36 for(string d; sin>>d;) > 37 ds.push_back(d); > 38 D[name] = ds; > 39 } > 40 > 41 // solve > 42 vector<string> result; > 43 while(m) { > 44 if( !D.count(danceCall) ) > 45 return vector<string>(1, "ILLEGAL"); > 46 vector<string>& d = D[danceCall]; > 47 int q = d.size(); > 48 int z = m*q/n; > 49 for(int i=0; i<z; ++i) > 50 result.push_back(d[i]); > 51 m = m*q - z*n; > 52 danceCall = d[z]; > 53 } > 54 return result; > 55 } > 56 }; > 57 > 58 // BEGIN CUT HERE > 59 #include <ctime> > 60 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 61 > 62 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 63 int verify_case(const vector <string> &Expected, const vector <string> &Received > 64 > 65 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 66 char Test_(...); > 67 int Test_(Case_<0>) { > 68 string definitions_[] = { "FiveParts: One Two Three Four Five" }; > 69 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 70 int m = 2; > 71 int n = 5; > 72 string danceCall = "FiveParts"; > 73 string RetVal_[] = {"One", "Two" }; > 74 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 75 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 76 int Test_(Case_<1>) { > 77 string definitions_[] = { "Trade: Hinge Hinge", > 78 "TradeThreeTimes: Trade Trade Trade" }; > 79 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 80 int m = 5; > 81 int n = 6; > 82 string danceCall = "TradeThreeTimes"; > 83 string RetVal_[] = {"Trade", "Trade", "Hinge" }; > 84 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 85 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 86 int Test_(Case_<2>) { > 87 string definitions_[] = { "Trade: Hinge Hinge", > 88 "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", > 89 "SpinTheTop: Trade FanTheTop", > 90 "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; > 91 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 92 int m = 1; > 93 int n = 2; > 94 string danceCall = "HotFootSpin"; > 95 string RetVal_[] = {"FanTheTop", "CenterTwoHinge" }; > 96 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 97 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 98 int Test_(Case_<3>) { > 99 string definitions_[] = { "Trade: Hinge Hinge", > 100 "CenterTwoTrade: CenterTwoHinge CenterTwoHinge", > 101 "SpinTheTop: Trade FanTheTop", > 102 "HotFootSpin: FanTheTop CenterTwoTrade SpinTheTop" }; > 103 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 104 int m = 3; > 105 int n = 4; > 106 string danceCall = "HotFootSpin"; > 107 string RetVal_[] = {"FanTheTop", "CenterTwoTrade", "Hinge" }; > 108 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 109 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 110 int Test_(Case_<4>) { > 111 string definitions_[] = { "A: One Two", > 112 "B: One Two Three", > 113 "C: One Two Three Four" }; > 114 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 115 int m = 3; > 116 int n = 4; > 117 string danceCall = "C"; > 118 string RetVal_[] = {"One", "Two", "Three" }; > 119 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 120 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 121 int Test_(Case_<5>) { > 122 string definitions_[] = { "A: X Y Z", > 123 "B: A A A" }; > 124 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 125 int m = 7; > 126 int n = 23; > 127 string danceCall = "B"; > 128 string RetVal_[] = {"ILLEGAL" }; > 129 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 130 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 131 int Test_(Case_<6>) { > 132 string definitions_[] = { "A: B C", > 133 "D: E F" }; > 134 vector <string> definitions(definitions_, definitions_+sizeof(definiti > 135 int m = 1; > 136 int n = 2; > 137 string danceCall = "G"; > 138 string RetVal_[] = {"ILLEGAL" }; > 139 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 140 return verify_case(RetVal, Fractionalization().partial(definitions, m, n > 141 > 142 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 143 template<> void Run_<-1>() {} > 144 int main() { Run_<0>(); } > 145 // END CUT HERE > 146
Added SRM/282/1C.cpp version [c1dd4b267114bf1f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class GreatExpectations { > 23 public: > 24 double expectedTime(vector <string> doors) > 25 { > 26 const int Y = doors.size()/2 +1; > 27 const int X = doors[0].size() +1; > 28 > 29 vector< vector<double> > e(Y, vector<double>(X, -1.0)); > 30 e[Y-1][X-1] = 0.0; > 31 > 32 for(int k=0; k<X*Y; ++k) > 33 for(int y=0; y<Y; ++y) > 34 for(int x=0; x<X; ++x) > 35 { > 36 // neibours : (expected_time, door_prob) > 37 vector< pair<double,double> > ep; > 38 if( x+1<X && doors[y*2][x]!='0' && e[y][ > 39 ep.push_back( make_pair(e[y][x+1 > 40 if( 0<x && doors[y*2][x-1]!='0' && e[y][ > 41 ep.push_back( make_pair(e[y][x-1 > 42 if( y+1<Y && doors[y*2+1][x]!='0' && e[y > 43 ep.push_back( make_pair(e[y+1][x > 44 if( 0<y && doors[y*2-1][x]!='0' && e[y-1 > 45 ep.push_back( make_pair(e[y-1][x > 46 > 47 if( ep.empty() ) continue; > 48 sort( ep.begin(), ep.end() ); // better > 49 > 50 // choose k that minimizes the following > 51 // E = (1+e[0])*p[0] > 52 // + (1+e[1])*(1-p[0])*p[1] > 53 // + (1+e[2])*(1-p[0])*(1-p[1])*p[2] > 54 // + ... > 55 // + (1+E) *(1-p[0])*(1-p[1])*...* > 56 double r=1.0, s=0.0, E=1e+50; > 57 for(int i=0; i<ep.size(); ++i) { > 58 double s2 = s + r * ep[i].second > 59 double r2 = r * (1 - ep[i].secon > 60 double E2 = (s2 + r2) / (1-r2); > 61 if( E2 > E ) break; > 62 r=r2, s=s2, E=E2; > 63 } > 64 if( e[y][x]<0 || e[y][x]>E ) > 65 e[y][x]=E; > 66 } > 67 > 68 return e[0][0]; > 69 } > 70 }; > 71 > 72 // BEGIN CUT HERE > 73 #include <ctime> > 74 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 75 > 76 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 77 int verify_case(const double &Expected, const double &Received) { double diff = > 78 > 79 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 80 char Test_(...); > 81 int Test_(Case_<0>) { > 82 string doors_[] = { "4", > 83 "44", > 84 "4" }; > 85 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 86 double RetVal = 3.3333333333333335; > 87 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 88 int Test_(Case_<1>) { > 89 string doors_[] = { "48", > 90 "440", > 91 "42", > 92 "862", > 93 "06" }; > 94 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 95 double RetVal = 6.0; > 96 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 97 int Test_(Case_<2>) { > 98 string doors_[] = { "0808", > 99 "88888", > 100 "0000", > 101 "88888", > 102 "8080" } > 103 ; > 104 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 105 double RetVal = 14.0; > 106 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 107 int Test_(Case_<3>) { > 108 string doors_[] = { "2815", > 109 "67686", > 110 "1324", > 111 "75767", > 112 "6051" } > 113 ; > 114 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 115 double RetVal = 11.852782338798935; > 116 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 117 int Test_(Case_<4>) { > 118 string doors_[] = { "823", > 119 "2630", > 120 "130" } > 121 ; > 122 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 123 double RetVal = -1.0; > 124 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 125 int Test_(Case_<5>) { > 126 string doors_[] = { "0", > 127 "44", > 128 "4" } > 129 ; > 130 vector <string> doors(doors_, doors_+sizeof(doors_)/sizeof(*doors_)); > 131 double RetVal = 4.0; > 132 return verify_case(RetVal, GreatExpectations().expectedTime(doors)); } > 133 > 134 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 135 template<> void Run_<-1>() {} > 136 int main() { Run_<0>(); } > 137 // END CUT HERE > 138
Added SRM/330/1A.cpp version [2cfe42a5f5982f84]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Arrows { public: > 23 int longestArrow(string s) > 24 { > 25 string rs = s; > 26 reverse(rs.begin(), rs.end()); > 27 return max(max(la(s,'<','-'), la(s,'<','=')), max(la(rs,'>','-') > 28 } > 29 int la(string s, char h, char t) > 30 { > 31 int m = -1; > 32 for(int i=0; i<s.size(); ++i) > 33 if( s[i]==h ) { > 34 int j=i+1; > 35 while( j<s.size() && s[j]==t ) > 36 ++j; > 37 m = max(m, j-i); > 38 } > 39 return m; > 40 } > 41 }; > 42 > 43 // BEGIN CUT HERE > 44 #include <ctime> > 45 double start_time; string timer() > 46 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 47 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 48 { os << "{ "; > 49 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 50 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 51 void verify_case(const int& Expected, const int& Received) { > 52 bool ok = (Expected == Received); > 53 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 54 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 55 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 56 #define END verify_case(_, Arrows().longestArrow(s));} > 57 int main(){ > 58 > 59 CASE(0) > 60 string s = "<--->--==>"; > 61 int _ = 4; > 62 END > 63 CASE(1) > 64 string s = "<<<<<<<<<<"; > 65 int _ = 1; > 66 END > 67 CASE(2) > 68 string s = "----==-"; > 69 int _ = -1; > 70 END > 71 CASE(3) > 72 string s = "<----=====>"; > 73 int _ = 6; > 74 END > 75 > 76 } > 77 // END CUT HERE
Added SRM/330/1B.cpp version [0980b7195d8f96ca]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 struct byLength { > 23 bool operator()(const string& lhs, const string& rhs) const { > 24 if(lhs.size() != rhs.size()) return lhs.size() < rhs.size(); > 25 return lhs < rhs; > 26 } > 27 }; > 28 class PrefixFreeSubsets { public: > 29 long long cantPrefFreeSubsets(vector <string> words) > 30 { > 31 int N = words.size(); > 32 sort(words.begin(), words.end(), byLength()); > 33 > 34 vector< vector<int> > dc; > 35 for(int i=0; i<N; ++i) > 36 dc.push_back( direct_children(words[i], words, i+1) ); > 37 dc.push_back( direct_children("", words, 0) ); > 38 return rec(N, dc) - 1; > 39 } > 40 > 41 LL rec(int t, vector< vector<int> >& dc ) > 42 { > 43 LL p = 1; > 44 for(int i=0; i<dc[t].size(); ++i) > 45 p *= rec(dc[t][i], dc); > 46 return p+1; > 47 } > 48 > 49 bool is_prefix(const string& pre, const string& all) { > 50 return equal(pre.begin(), pre.end(), all.begin()); > 51 } > 52 > 53 vector<int> direct_children(const string& s, vector<string>& w, int i) { > 54 vector<int> ans; > 55 for(; i<w.size(); ++i) > 56 if( is_prefix(s,w[i]) ){ > 57 bool direct = true; > 58 for(int j=0; j<ans.size(); ++j) > 59 direct = direct && !is_prefix(w[ans[j]], > 60 if( direct ) > 61 ans.push_back(i); > 62 } > 63 return ans; > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const long long& Expected, const long long& Received) { > 76 bool ok = (Expected == Received); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, PrefixFreeSubsets().cantPrefFreeSubsets(words)); > 81 int main(){ > 82 > 83 CASE(0) > 84 string words_[] = {"hello","hell","hi"}; > 85 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 86 long long _ = 6LL; > 87 END > 88 CASE(1) > 89 string words_[] = {"a","b","c","d"}; > 90 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 91 long long _ = 16LL; > 92 END > 93 CASE(2) > 94 string words_[] = {"a","ab","abc","abcd","abcde","abcdef"}; > 95 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 96 long long _ = 7LL; > 97 END > 98 CASE(3) > 99 string words_[] = {"a","b","aa","ab","ba","bb"}; > 100 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 101 long long _ = 25LL; > 102 END > 103 > 104 } > 105 // END CUT HERE
Added SRM/330/1C.cpp version [9065451ab4e56b9f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LongLongNim { public: > 23 int numberOfWins(int maxN, vector <int> moves) > 24 { > 25 int FULL = (1 << *max_element(moves.begin(), moves.end())) - 1; > 26 int MASK = 0; > 27 for(int i=0; i<moves.size(); ++i) > 28 MASK |= 1 << (moves[i]-1); > 29 > 30 vector<int> ss; > 31 vector<int> visited(FULL+1, -1); > 32 for(int i=1,s=1; i<=maxN; ++i) > 33 { > 34 s = (s<<1 | ((~s&MASK)==MASK)) & FULL; > 35 if( visited[s] >= 0 ) > 36 return countBits(ss, visited[s], ss.size(), maxN > 37 else > 38 { > 39 visited[s] = ss.size(); > 40 ss.push_back(s&1); > 41 } > 42 } > 43 return countBits(ss, 0, maxN, maxN); > 44 } > 45 > 46 int countBits(const vector<int>& b, int lb, int le, int N) > 47 { > 48 // Given an infinite seq b[0] ... b[lb-1] (b[lb] ... b[le-1])*, > 49 // compute \Sigma b[0..N) > 50 int sum = 0; > 51 for(int i=0; i<lb; ++i) > 52 sum += b[i]; > 53 > 54 N -= lb; > 55 int ll = le - lb; > 56 int Q = N/ll; > 57 int R = N%ll; > 58 for(int i=lb; i<le; ++i) > 59 sum += b[i] * Q; > 60 for(int i=lb; i<lb+R; ++i) > 61 sum += b[i]; > 62 return sum; > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time; string timer() > 69 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 70 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 71 { os << "{ "; > 72 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 73 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 74 void verify_case(const int& Expected, const int& Received) { > 75 bool ok = (Expected == Received); > 76 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 77 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 78 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 79 #define END verify_case(_, LongLongNim().numberOfWins(maxN, moves));} > 80 int main(){ > 81 > 82 CASE(0) > 83 int maxN = 20; > 84 int moves_[] = {1,2,3}; > 85 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 86 int _ = 5; > 87 END > 88 CASE(1) > 89 int maxN = 999; > 90 int moves_[] = {1}; > 91 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 92 int _ = 499; > 93 END > 94 CASE(3) > 95 int maxN = 6543; > 96 int moves_[] = {2,4,7,11,20}; > 97 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 98 int _ = 1637; > 99 END > 100 CASE(2) > 101 int maxN = 1000000000; > 102 int moves_[] = {1,2}; > 103 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 104 int _ = 333333333; > 105 END > 106 /* > 107 CASE(4) > 108 int maxN = ; > 109 int moves_[] = ; > 110 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 111 int _ = ; > 112 END > 113 CASE(5) > 114 int maxN = ; > 115 int moves_[] = ; > 116 vector <int> moves(moves_, moves_+sizeof(moves_)/sizeof(*moves_)); > 117 int _ = ; > 118 END > 119 */ > 120 } > 121 // END CUT HERE
Added SRM/330/2C.cpp version [97604476ec6f9367]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NextPalindromicNumber { public: > 23 string getNext(string n) > 24 { > 25 string s = mirror(n); > 26 if( n < s ) > 27 return s; > 28 return mirror(plus1(s, (s.size()-1)/2)); > 29 } > 30 > 31 string mirror(const string& s) > 32 { > 33 return string(s.begin(), s.begin()+s.size()/2) + string(s.rbegin > 34 } > 35 > 36 string plus1(string s, int i) > 37 { > 38 while( s[i] == '9' ) > 39 { > 40 s[i--] = '0'; > 41 if( i < 0 ) > 42 return '1'+s; > 43 } > 44 s[i]++; > 45 return s; > 46 } > 47 }; > 48 > 49 // BEGIN CUT HERE > 50 #include <ctime> > 51 double start_time; string timer() > 52 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 53 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 54 { os << "{ "; > 55 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 56 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 57 void verify_case(const string& Expected, const string& Received) { > 58 bool ok = (Expected == Received); > 59 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 60 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 61 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 62 #define END verify_case(_, NextPalindromicNumber().getNext(n));} > 63 int main(){ > 64 > 65 CASE(0) > 66 string n = "12345"; > 67 string _ = "12421"; > 68 END > 69 CASE(1) > 70 string n = "858"; > 71 string _ = "868"; > 72 END > 73 CASE(2) > 74 string n = "1999"; > 75 string _ = "2002"; > 76 END > 77 CASE(3) > 78 string n = "1"; > 79 string _ = "2"; > 80 END > 81 CASE(4) > 82 string n = "9999"; > 83 string _ = "10001"; > 84 END > 85 /* > 86 CASE(5) > 87 string n = ; > 88 string _ = ; > 89 END > 90 CASE(6) > 91 string n = ; > 92 string _ = ; > 93 END > 94 */ > 95 } > 96 // END CUT HERE
Added SRM/331/1A.cpp version [f62c58c81f050ba2]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CarolsSinging { > 23 public: > 24 int choose(vector <string> lyrics) > 25 { > 26 int nP = lyrics.size(); > 27 int nS = lyrics[0].size(); > 28 vector<int> people_who_know_the_song(nS); > 29 for(int s=0; s<nS; ++s) > 30 for(int p=0; p<nP; ++p) > 31 people_who_know_the_song[s] |= (lyrics[p][s]=='Y > 32 > 33 int minPop = nS; > 34 for(int m=1; m<(1<<nS); ++m) > 35 { > 36 int canSing = 0; > 37 for(int s=0; (1<<s)<=m; ++s) > 38 if( (1<<s) & m ) > 39 canSing |= people_who_know_the_song[s]; > 40 if( canSing == (1<<nP)-1 ) > 41 minPop = min(minPop, __builtin_popcount(m)); > 42 } > 43 return minPop; > 44 } > 45 }; > 46 > 47 // BEGIN CUT HERE > 48 #include <ctime> > 49 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 50 > 51 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 52 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 53 > 54 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 55 char Test_(...); > 56 int Test_(Case_<0>) { > 57 string lyrics_[] = {"YN","NY"}; > 58 vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics > 59 int RetVal = 2; > 60 return verify_case(RetVal, CarolsSinging().choose(lyrics)); } > 61 int Test_(Case_<1>) { > 62 string lyrics_[] = {"YN","YY","YN"}; > 63 vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics > 64 int RetVal = 1; > 65 return verify_case(RetVal, CarolsSinging().choose(lyrics)); } > 66 int Test_(Case_<2>) { > 67 string lyrics_[] = {"YNN","YNY","YNY","NYY","NYY","NYN"}; > 68 vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics > 69 int RetVal = 2; > 70 return verify_case(RetVal, CarolsSinging().choose(lyrics)); } > 71 int Test_(Case_<3>) { > 72 string lyrics_[] = {"YNNYYY","YYNYYY","YNNYYN","NYYNNN","YYYNNN","YYYNNY > 73 "YYYYYY","YNNNNN","YYYYNY","YYNNNN","NNYYYN","NNNNYY","YYYNNN","NYNNYN","YNNYYN > 74 "YYNNNY","NYYNNY","NNYYYN","YNYYYN","NNNYNY","YYYYNN","YYNYNN","NYYNYY","YYNYYN > 75 vector <string> lyrics(lyrics_, lyrics_+sizeof(lyrics_)/sizeof(*lyrics > 76 int RetVal = 4; > 77 return verify_case(RetVal, CarolsSinging().choose(lyrics)); } > 78 > 79 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 80 template<> void Run_<-1>() {} > 81 int main() { Run_<0>(); } > 82 // END CUT HERE > 83
Added SRM/331/1B.cpp version [b050d3607f1a5bd8]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Shopping { > 23 public: > 24 int minNumber(int X, vector <int> values) > 25 { > 26 sort( values.begin(), values.end() ); > 27 > 28 int next=1, cnt=0; > 29 while( next <= X ) > 30 { > 31 // maximum <= next; > 32 vector<int>::iterator it = upper_bound(values.begin(), v > 33 if( it == values.begin() ) > 34 return -1; > 35 next += *(it-1); > 36 ++cnt; > 37 } > 38 return cnt; > 39 } > 40 }; > 41 > 42 // BEGIN CUT HERE > 43 #include <ctime> > 44 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 45 > 46 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 47 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 48 > 49 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 50 char Test_(...); > 51 int Test_(Case_<0>) { > 52 int X = 20; > 53 int values_[] = {1, 2, 5, 10}; > 54 vector <int> values(values_, values_+sizeof(values_)/sizeof(*values_)) > 55 int RetVal = 5; > 56 return verify_case(RetVal, Shopping().minNumber(X, values)); } > 57 int Test_(Case_<1>) { > 58 int X = 7; > 59 int values_[] = {2, 4, 1, 7}; > 60 vector <int> values(values_, values_+sizeof(values_)/sizeof(*values_)) > 61 int RetVal = 3; > 62 return verify_case(RetVal, Shopping().minNumber(X, values)); } > 63 int Test_(Case_<2>) { > 64 int X = 20; > 65 int values_[] = {2,4,6,8}; > 66 vector <int> values(values_, values_+sizeof(values_)/sizeof(*values_)) > 67 int RetVal = -1; > 68 return verify_case(RetVal, Shopping().minNumber(X, values)); } > 69 int Test_(Case_<3>) { > 70 int X = 600; > 71 int values_[] = {1,2,3,10,11,30}; > 72 vector <int> values(values_, values_+sizeof(values_)/sizeof(*values_)) > 73 int RetVal = 25; > 74 return verify_case(RetVal, Shopping().minNumber(X, values)); } > 75 > 76 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 77 template<> void Run_<-1>() {} > 78 int main() { Run_<0>(); } > 79 // END CUT HERE > 80
Added SRM/331/1C.cpp version [1922da4e015e264c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef pair<int,int> pt; > 23 typedef pair<int,int> range; > 24 > 25 class HiddenSquares { > 26 public: > 27 int count(vector <int> x1, vector <int> y1, vector <int> x2, vector <int > 28 { > 29 vector< pair<int, range> > xseg, yseg; > 30 for(int i=0; i<x1.size(); ++i) > 31 { > 32 xseg.push_back( make_pair(x1[i], range(y1[i], y2[i])) ); > 33 xseg.push_back( make_pair(x2[i], range(y1[i], y2[i])) ); > 34 yseg.push_back( make_pair(y1[i], range(x1[i], x2[i])) ); > 35 yseg.push_back( make_pair(y2[i], range(x1[i], x2[i])) ); > 36 } > 37 > 38 // copy and > 39 map<int, vector<range> > xray; > 40 for(int i=0; i<xseg.size(); ++i) > 41 { > 42 set<int> p; > 43 > 44 p.insert(xseg[i].second.first); > 45 p.insert(xseg[i].second.second); > 46 for(int j=0; j<yseg.size(); ++j) > 47 if( xseg[i].second.first<=yseg[j].first && yseg[ > 48 && yseg[j].second.first<=xseg[i].first && xseg[ > 49 p.insert( yseg[j].first ); > 50 > 51 int py = xseg[i].second.first; > 52 for(set<int>::iterator y=p.begin(); ++y!=p.end(); py=*y) > 53 xray[xseg[i].first].push_back( range(py,*y) ); > 54 } > 55 > 56 // paset! > 57 map<int, vector<range> > yray; > 58 for(int i=0; i<yseg.size(); ++i) > 59 { > 60 set<int> p; > 61 > 62 p.insert(yseg[i].second.first); > 63 p.insert(yseg[i].second.second); > 64 for(int j=0; j<xseg.size(); ++j) > 65 if( yseg[i].second.first<=xseg[j].first && xseg[ > 66 && xseg[j].second.first<=yseg[i].first && yseg[ > 67 p.insert( xseg[j].first ); > 68 > 69 int px = yseg[i].second.first; > 70 for(set<int>::iterator x=p.begin(); ++x!=p.end(); px=*x) > 71 yray[yseg[i].first].push_back( range(px,*x) ); > 72 } > 73 > 74 // copy and > 75 map< pt, set<pt> > xconn; > 76 for(map<int, vector< pt > >::iterator it=xray.begin(); it!=xray. > 77 { > 78 int x = it->first; > 79 vector<range>& r = it->second; > 80 > 81 vector< pair<int,bool> > es; > 82 for(int i=0; i!=r.size(); ++i) > 83 es.push_back( make_pair(r[i].first,false) ), > 84 es.push_back( make_pair(r[i].second,true) ); > 85 sort(es.begin(), es.end()); > 86 vector<int> cur; > 87 int nest = 0; > 88 for(int i=0; i!=es.size(); ++i) > 89 { > 90 cur.push_back( es[i].first ); > 91 es[i].second ? --nest : ++nest; > 92 if( nest == 0 ) > 93 { > 94 for(int j=0; j<cur.size(); ++j) > 95 for(int k=j+1; k<cur.size(); ++k > 96 if( cur[j] < cur[k] ) > 97 xconn[pt(x,cur[j > 98 cur.clear(); > 99 } > 100 } > 101 } > 102 > 103 // paste! > 104 map< pt, set<pt> > yconn; > 105 for(map<int, vector< pt > >::iterator it=yray.begin(); it!=yray. > 106 { > 107 int y = it->first; > 108 vector<range>& r = it->second; > 109 > 110 vector< pair<int,bool> > es; > 111 for(int i=0; i!=r.size(); ++i) > 112 es.push_back( make_pair(r[i].first,false) ), > 113 es.push_back( make_pair(r[i].second,true) ); > 114 sort(es.begin(), es.end()); > 115 > 116 vector<int> cur; > 117 int nest = 0; > 118 for(int i=0; i!=es.size(); ++i) > 119 { > 120 cur.push_back( es[i].first ); > 121 es[i].second ? --nest : ++nest; > 122 if( nest == 0 ) > 123 { > 124 for(int j=0; j<cur.size(); ++j) > 125 for(int k=j+1; k<cur.size(); ++k > 126 if( cur[j] < cur[k] ) > 127 yconn[pt(cur[j], > 128 cur.clear(); > 129 } > 130 } > 131 } > 132 > 133 // count squares > 134 int cnt = 0; > 135 for(map< pt, set<pt> >::iterator it=xconn.begin(); it!=xconn.end > 136 { > 137 pt p = it->first; > 138 set<pt>& c = it->second; > 139 for(set<pt>::iterator jt=c.begin(); jt!=c.end(); ++jt) > 140 { > 141 pt q = *jt; > 142 int w = q.second-p.second; > 143 pt p1(p.first+w,p.second); > 144 pt q1(q.first+w,q.second); > 145 if( yconn[p].count(p1) && yconn[q].count(q1) && > 146 ++cnt; > 147 } > 148 } > 149 return cnt; > 150 } > 151 }; > 152 > 153 // BEGIN CUT HERE > 154 #include <ctime> > 155 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 156 > 157 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 158 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 159 > 160 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 161 char Test_(...); > 162 int Test_(Case_<0>) { > 163 int x1_[] = {0,1,0}; > 164 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 165 int y1_[] = {0,0,1}; > 166 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 167 int x2_[] = {3,2,3}; > 168 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 169 int y2_[] = {3,3,2}; > 170 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 171 int RetVal = 14; > 172 return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } > 173 int Test_(Case_<1>) { > 174 int x1_[] = {0,2}; > 175 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 176 int y1_[] = {0,0}; > 177 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 178 int x2_[] = {2,4}; > 179 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 180 int y2_[] = {4,4}; > 181 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 182 int RetVal = 1; > 183 return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } > 184 int Test_(Case_<2>) { > 185 int x1_[] = {0,0,3}; > 186 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 187 int y1_[] = {0,3,0}; > 188 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 189 int x2_[] = {1,4,4}; > 190 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 191 int y2_[] = {3,4,3}; > 192 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 193 int RetVal = 0; > 194 return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } > 195 int Test_(Case_<3>) { > 196 int x1_[] = {453453463,453453500,453453495,453453512,453453478,453453489 > 197 453453466,453453500,453453498,453453510,453453472,453453512, > 198 453453519,453453514,453453521,453453518,453453523,453453517, > 199 453453466,453453525,453453496,453453495,453453463,453453461, > 200 453453460,453453522,453453471,453453468,453453479,453453517, > 201 453453485,453453518,453453499,453453464,453453494}; > 202 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 203 int y1_[] = {364646438,364646402,364646449,364646438,364646415,364646401 > 204 364646446,364646416,364646456,364646414,364646463,364646407, > 205 364646436,364646450,364646421,364646411,364646414,364646419, > 206 364646445,364646427,364646405,364646442,364646418,364646464, > 207 364646457,364646463,364646432,364646426,364646444,364646431, > 208 364646450,364646418,364646434,364646458,364646402}; > 209 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 210 int x2_[] = {453453488,453453510,453453525,453453523,453453493,453453500 > 211 453453470,453453511,453453499,453453521,453453518,453453524, > 212 453453523,453453523,453453524,453453523,453453525,453453519, > 213 453453473,453453526,453453511,453453517,453453470,453453464, > 214 453453511,453453524,453453516,453453516,453453492,453453524, > 215 453453513,453453522,453453520,453453505,453453512}; > 216 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 217 int y2_[] = {364646460,364646454,364646462,364646466,364646464,364646455 > 218 364646457,364646461,364646457,364646450,364646466,364646453, > 219 364646441,364646451,364646460,364646461,364646446,364646464, > 220 364646447,364646460,364646454,364646450,364646444,364646466, > 221 364646458,364646466,364646455,364646442,364646462,364646435, > 222 364646464,364646444,364646455,364646459,364646430}; > 223 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 224 int RetVal = 124; > 225 return verify_case(RetVal, HiddenSquares().count(x1, y1, x2, y2)); } > 226 > 227 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 228 template<> void Run_<-1>() {} > 229 int main() { Run_<0>(); } > 230 // END CUT HERE > 231
Added SRM/332/1A.cpp version [40a888ac9f7816df]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class CreatePairs { > 21 public: > 22 int maximalSum(vector <int> data) > 23 { > 24 sort(data.begin(), data.end()); > 25 vector<int>::iterator neg = upper_bound(data.begin(), data.end() > 26 > 27 LL sum = 0; > 28 for(vector<int>::iterator it=data.begin(); it!=neg; ++it) > 29 if( it+1 == neg ) > 30 sum += *it; > 31 else > 32 sum += *it * *(it+1), ++it; > 33 > 34 vector<int>::iterator pos = lower_bound(data.begin(), data.end() > 35 for(vector<int>::iterator it=data.end()-1; it>=pos; --it) > 36 if( it == pos ) > 37 sum += *it; > 38 else > 39 sum += *it * *(it-1), --it; > 40 for(vector<int>::iterator it=neg; it!=pos; ++it) > 41 sum += *it; > 42 return sum; > 43 } > 44 }; > 45 > 46 // BEGIN CUT HERE > 47 #include <ctime> > 48 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 49 > 50 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 51 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 52 > 53 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 54 char Test_(...); > 55 int Test_(Case_<0>) { > 56 int data_[] = {0, 1, 2, 4, 3, 5}; > 57 vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); > 58 int RetVal = 27; > 59 return verify_case(RetVal, CreatePairs().maximalSum(data)); } > 60 int Test_(Case_<1>) { > 61 int data_[] = {-1, 1, 2, 3}; > 62 vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); > 63 int RetVal = 6; > 64 return verify_case(RetVal, CreatePairs().maximalSum(data)); } > 65 int Test_(Case_<2>) { > 66 int data_[] = {-1}; > 67 vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); > 68 int RetVal = -1; > 69 return verify_case(RetVal, CreatePairs().maximalSum(data)); } > 70 int Test_(Case_<3>) { > 71 int data_[] = {-1, 0, 1}; > 72 vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); > 73 int RetVal = 1; > 74 return verify_case(RetVal, CreatePairs().maximalSum(data)); } > 75 int Test_(Case_<4>) { > 76 int data_[] = {1, 1}; > 77 vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); > 78 int RetVal = 2; > 79 return verify_case(RetVal, CreatePairs().maximalSum(data)); } > 80 > 81 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 82 template<> void Run_<-1>() {} > 83 int main() { Run_<0>(); } > 84 // END CUT HERE > 85
Added SRM/332/1B.cpp version [42786b6cea1fe8ed]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 struct edge { > 21 int y, x1, x2; > 22 edge(){} > 23 edge(int y, int x1, int x2) :y(y),x1(min(x1,x2)),x2(max(x1,x2)) {} > 24 bool operator<( const edge& rhs ) const { > 25 if( y != rhs.y ) return y<rhs.y; > 26 if( x1 != rhs.x1 ) return x1<rhs.x1; > 27 return x2 < rhs.x2; > 28 } > 29 }; > 30 > 31 class RestoringPolygon { > 32 public: > 33 int restore(vector <int> x1, vector <int> x2, vector <int> y) > 34 { > 35 int N = x1.size(); > 36 > 37 vector<edge> e; > 38 for(int i=0; i!=N; ++i) > 39 e.push_back( edge(y[i], x1[i], x2[i]) ); > 40 sort(e.begin(), e.end()); > 41 > 42 int ans = 0; > 43 for(int m=1; m<(1<<N); ++m) > 44 ans = max(ans, calc(e,m)); > 45 return ans; > 46 } > 47 > 48 int calc(vector<edge>& e_, int m) > 49 { > 50 vector<edge> e; > 51 for(int i=0; (1<<i)<=m; ++i) > 52 if( m & (1<<i) ) > 53 e.push_back( e_[i] ); > 54 > 55 if( e.size() <= 1 ) > 56 return 0; > 57 > 58 typedef pair<int,int> point; > 59 typedef map< point, vector<point> > graph; > 60 graph G; > 61 for(int i=0; i<e.size(); ++i) > 62 { > 63 int y = e[i].y; > 64 int xx[] = {e[i].x1, e[i].x2}; > 65 > 66 G[ point(e[i].x1,y) ].push_back( point(e[i].x2,y) ); > 67 G[ point(e[i].x2,y) ].push_back( point(e[i].x1,y) ); > 68 > 69 for(int k=0; k<2; ++k) { > 70 int x = xx[k]; > 71 for(int j=i-1; j>=0; --j) > 72 if( e[j].x1 == x ) { > 73 point p1(x, y); > 74 point p2(e[j].x1, e[j].y); > 75 if( G[p2].size() < 2 ) { > 76 G[p1].push_back(p2); > 77 G[p2].push_back(p1); > 78 } > 79 } else if( e[j].x2 == x ) { > 80 point p1(x, y); > 81 point p2(e[j].x2, e[j].y); > 82 if( G[p2].size() < 2 ) { > 83 G[p1].push_back(p2); > 84 G[p2].push_back(p1); > 85 } > 86 } else if( e[j].x1 < x && x < e[j].x2 ) > 87 break; > 88 } > 89 } > 90 } > 91 > 92 for(graph::iterator it=G.begin(); it!=G.end(); ++it) > 93 if( it->second.size() != 2 ) > 94 return 0; > 95 > 96 point s = G.begin()->first; > 97 point p = s, p2 = G[p][0]; > 98 for(int c=2 ;; ++c) { > 99 point q = (G[p2][0] == p ? G[p2][1] : G[p2][0]); > 100 p=p2, p2=q; > 101 if( q == s ) > 102 return c==e.size()*2 ? c : 0; > 103 } > 104 } > 105 }; > 106 > 107 // BEGIN CUT HERE > 108 #include <ctime> > 109 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 110 > 111 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 112 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 113 > 114 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 115 char Test_(...); > 116 int Test_(Case_<0>) { > 117 int x1_[] = {1,2,3,1}; > 118 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 119 int x2_[] = {2,3,5,5}; > 120 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 121 int y_[] = {1,4,2,0}; > 122 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 123 int RetVal = 8; > 124 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 125 int Test_(Case_<1>) { > 126 int x1_[] = {1,1,2,2}; > 127 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 128 int x2_[] = {3,3,4,4}; > 129 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 130 int y_[] = {0,2,1,3}; > 131 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 132 int RetVal = 4; > 133 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 134 int Test_(Case_<2>) { > 135 int x1_[] = {1}; > 136 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 137 int x2_[] = {2}; > 138 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 139 int y_[] = {1}; > 140 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 141 int RetVal = 0; > 142 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 143 int Test_(Case_<3>) { > 144 int x1_[] = {0,0,0}; > 145 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 146 int x2_[] = {1000,1000,1000}; > 147 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 148 int y_[] = {0,1,2}; > 149 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 150 int RetVal = 4; > 151 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 152 int Test_(Case_<4>) { > 153 int x1_[] = {0,1,1,2}; > 154 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 155 int x2_[] = {1,0,2,1}; > 156 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 157 int y_[] = {0,4,2,3}; > 158 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 159 int RetVal = 8; > 160 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 161 int Test_(Case_<5>) { > 162 int x1_[] = {696, -193, -193, 367}; > 163 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 164 int x2_[] = {367, -276, -276, 696}; > 165 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 166 int y_[] = {-14, 168, -14, 168}; > 167 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 168 int RetVal = 4; > 169 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 170 int Test_(Case_<6>) { > 171 int x1_[] = { 0, 1, 6, 5, 5, 1, 6, 1, 0, 0, 9}; > 172 vector <int> x1(x1_, x1_+sizeof(x1_)/sizeof(*x1_)); > 173 int x2_[] = { 5, 9, 9, 6, 6, 0, 9, 0, 1, 5, 6}; > 174 vector <int> x2(x2_, x2_+sizeof(x2_)/sizeof(*x2_)); > 175 int y_[] = { 1, 4, 5, 3, 2, 0, 0, 2, 3, 5, 1}; > 176 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 177 int RetVal = 16; > 178 return verify_case(RetVal, RestoringPolygon().restore(x1, x2, y)); } > 179 > 180 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 181 template<> void Run_<-1>() {} > 182 int main() { Run_<0>(); } > 183 // END CUT HERE > 184
Added SRM/332/1C.cpp version [8e94bd05bec60fef]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LadderPermutation { public: > 23 vector <int> createLadder(int n, int m, int k) > 24 { > 25 try { > 26 vector<int> v = gen(n-k, m-1, k); > 27 for(int i=0; i<k; ++i) > 28 v.push_back(n-i); > 29 return v; > 30 } catch(...) { > 31 return vector<int>(); > 32 } > 33 } > 34 > 35 vector<int> gen(int n, int m, int k) // make m blocks of length <=k > 36 { > 37 if( m==0 ) { > 38 if( n==0 ) > 39 return vector<int>(); > 40 throw "fail"; > 41 } > 42 int x = min(k, n-(m-1)); > 43 if( x <= 0 ) > 44 throw "fail"; > 45 vector<int> v = gen(n-x, m-1, k); > 46 for(int i=0; i<x; ++i) > 47 v.push_back(n-i); > 48 return v; > 49 } > 50 }; > 51 > 52 // BEGIN CUT HERE > 53 #include <ctime> > 54 double start_time; string timer() > 55 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 56 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 57 { os << "{ "; > 58 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 59 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 60 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 61 bool ok = (Expected == Received); > 62 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 63 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 64 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 65 #define END verify_case(_, LadderPermutation().createLadder(n, m, k));} > 66 int main(){ > 67 > 68 CASE(0) > 69 int n = 4; > 70 int m = 2; > 71 int k = 2; > 72 int __[] = {2, 1, 4, 3 }; > 73 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 74 END > 75 CASE(1) > 76 int n = 3; > 77 int m = 2; > 78 int k = 2; > 79 int __[] = {1, 3, 2 }; > 80 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 81 END > 82 CASE(2) > 83 int n = 2; > 84 int m = 1; > 85 int k = 1; > 86 vector <int> _; > 87 END > 88 CASE(3) > 89 int n = 6; > 90 int m = 3; > 91 int k = 2; > 92 int __[] = {2, 1, 4, 3, 6, 5 }; > 93 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 94 END > 95 CASE(4) > 96 int n = 6; > 97 int m = 2; > 98 int k = 3; > 99 int __[] = {3, 2, 1, 6, 5, 4 }; > 100 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 101 END > 102 CASE(5) > 103 int n = 7; > 104 int m = 4; > 105 int k = 4; > 106 int __[] = {1, 2, 3, 7, 6, 5, 4 }; > 107 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 108 END > 109 /* > 110 CASE(6) > 111 int n = ; > 112 int m = ; > 113 int k = ; > 114 int __[] = ; > 115 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 116 END > 117 CASE(7) > 118 int n = ; > 119 int m = ; > 120 int k = ; > 121 int __[] = ; > 122 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 123 END > 124 */ > 125 } > 126 // END CUT HERE
Added SRM/332/2C.cpp version [4fe2b90f4373a079]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Squares { public: > 23 int countSquares(vector <string> field) > 24 { > 25 const int H = field.size(); > 26 const int W = field[0].size(); > 27 > 28 int cnt = 0; > 29 for(int y=0; y<H; ++y) > 30 for(int x=0; x<W; ++x) > 31 for(int y2=0; y2<H; ++y2) > 32 for(int x2=0; x2<W; ++x2) if( y!=y2 || x!=x2 ) > 33 { > 34 char c = field[y][x]; > 35 int x3 = x+(y2-y); > 36 int y3 = y-(x2-x); > 37 int x4 = x3+(x2-x); > 38 int y4 = y3+(y2-y); > 39 if( 0<=x3 && x3<W && 0<=y3 && y3<H ) > 40 if( 0<=x4 && x4<W && 0<=y4 && y4<H ) > 41 if( field[y2][x2]==c && field[y3][x3]==c && fiel > 42 ++cnt; > 43 } > 44 return cnt/4; > 45 } > 46 }; > 47 > 48 // BEGIN CUT HERE > 49 #include <ctime> > 50 double start_time; string timer() > 51 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 52 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 53 { os << "{ "; > 54 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 55 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 56 void verify_case(const int& Expected, const int& Received) { > 57 bool ok = (Expected == Received); > 58 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 59 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 60 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 61 #define END verify_case(_, Squares().countSquares(field));} > 62 int main(){ > 63 > 64 CASE(0) > 65 string field_[] = {"ABA", "BAB", "ABA"}; > 66 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 67 int _ = 2; > 68 END > 69 CASE(1) > 70 string field_[] = {"AA", "AA"}; > 71 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 72 int _ = 1; > 73 END > 74 CASE(2) > 75 string field_[] = {"ABC", "DEF", "GHI"}; > 76 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 77 int _ = 0; > 78 END > 79 CASE(3) > 80 string field_[] = {"AABCA", "AAAAA", "BAAAB", "AAAEA", "ADBFA"}; > 81 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 82 int _ = 11; > 83 END > 84 /* > 85 CASE(4) > 86 string field_[] = ; > 87 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 88 int _ = ; > 89 END > 90 CASE(5) > 91 string field_[] = ; > 92 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 93 int _ = ; > 94 END > 95 */ > 96 } > 97 // END CUT HERE
Added SRM/333/1A.cpp version [a3074f7b005ac1af]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 int to_i(const string& s) { > 21 int n; > 22 stringstream(s)>>n; > 23 return n; > 24 } > 25 > 26 class BirthNumbersValidator { > 27 public: > 28 vector <string> validate(vector <string> test) > 29 { > 30 vector<string> ans; > 31 transform(test.begin(), test.end(), back_inserter(ans), &testeac > 32 return ans; > 33 } > 34 > 35 static string testeach( const string& x ) > 36 { > 37 int y = to_i(x.substr(0,2)); > 38 int m = to_i(x.substr(2,2)); > 39 int d = to_i(x.substr(4,2)); > 40 if( 1<=m && m<=12 ) {} > 41 else if( 51<=m && m<=62 ) { m-=50; } > 42 else return "NO"; > 43 > 44 bool leap = (y%4)==0; > 45 int dd[] = {31, leap?29:28, 31, 30, 31, 30, 31, 31, 30, 31, 30, > 46 if( 1<=d && d<=dd[m-1] ) {} > 47 else return "NO"; > 48 > 49 LL cd; stringstream(x)>>cd; > 50 return cd%11==0 ? "YES" : "NO"; > 51 } > 52 }; > 53 > 54 // BEGIN CUT HERE > 55 #include <ctime> > 56 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 57 > 58 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 59 int verify_case(const vector <string> &Expected, const vector <string> &Received > 60 > 61 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 62 char Test_(...); > 63 int Test_(Case_<0>) { > 64 string test_[] = {"8104121234"}; > 65 vector <string> test(test_, test_+sizeof(test_)/sizeof(*test_)); > 66 string RetVal_[] = {"YES" }; > 67 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 68 return verify_case(RetVal, BirthNumbersValidator().validate(test)); } > 69 int Test_(Case_<1>) { > 70 string test_[] = {"8154121239"}; > 71 vector <string> test(test_, test_+sizeof(test_)/sizeof(*test_)); > 72 string RetVal_[] = {"YES" }; > 73 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 74 return verify_case(RetVal, BirthNumbersValidator().validate(test)); } > 75 int Test_(Case_<2>) { > 76 string test_[] = {"8134120005"}; > 77 vector <string> test(test_, test_+sizeof(test_)/sizeof(*test_)); > 78 string RetVal_[] = {"NO" }; > 79 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 80 return verify_case(RetVal, BirthNumbersValidator().validate(test)); } > 81 int Test_(Case_<3>) { > 82 string test_[] = {"8102310007","8104121235"}; > 83 vector <string> test(test_, test_+sizeof(test_)/sizeof(*test_)); > 84 string RetVal_[] = {"NO", "NO" }; > 85 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 86 return verify_case(RetVal, BirthNumbersValidator().validate(test)); } > 87 int Test_(Case_<4>) { > 88 string test_[] = {"0411131237"}; > 89 vector <string> test(test_, test_+sizeof(test_)/sizeof(*test_)); > 90 string RetVal_[] = {"YES" }; > 91 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 92 return verify_case(RetVal, BirthNumbersValidator().validate(test)); } > 93 > 94 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 95 template<> void Run_<-1>() {} > 96 int main() { Run_<0>(); } > 97 // END CUT HERE > 98
Added SRM/333/1B.cpp version [42a89a8a845fafd6]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class RepeatedPatterns { > 21 public: > 22 long long findZeroSegment(string P1, string P2, string zeroCount) > 23 { > 24 LL zc; stringstream(zeroCount)>>zc; > 25 > 26 bool allz1 = (P1.find('1') == string::npos); > 27 bool allz2 = (P2.find('1') == string::npos); > 28 if( allz1 && allz2 ) > 29 return 0; > 30 > 31 if( allz1 ) > 32 { > 33 LL z1 = P1.size()*1000000; > 34 if( zc <= z1 ) > 35 return 0; > 36 > 37 LL a2 = 0; while( P2[a2]=='0' ) ++a2; > 38 LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; > 39 LL c2=0, i2; > 40 for(int i=0; i<P2.size(); ++i) > 41 if( P2[i]=='0' ) { > 42 size_t ii = P2.find('1',i); > 43 if(ii==string::npos) ii=P2.size(); > 44 if( ii-i > c2 ) c2=ii-i, i2=i; > 45 } > 46 > 47 if( zc <= z1+a2 ) > 48 return 0; > 49 if( zc <= c2 ) > 50 return P1.size()*1000000 + i2; > 51 if( zc <= b2+z1+a2 ) > 52 return P1.size()*1000000 + P2.size() - b2; > 53 return -1; > 54 } > 55 else if( allz2 ) > 56 { > 57 LL a1 = 0; while( P1[a1]=='0' ) ++a1; > 58 LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; > 59 LL c1=0, i1; > 60 for(int i=0; i<P1.size(); ++i) > 61 if( P1[i]=='0' ) { > 62 size_t ii = P1.find('1',i); > 63 if(ii==string::npos) ii=P1.size(); > 64 if( ii-i > c1 ) c1=ii-i, i1=i; > 65 } > 66 if( zc <= a1 ) > 67 return 0; > 68 if( zc <= c1 ) > 69 return i1; > 70 if( zc <= b1+a1 ) > 71 return P1.size() - b1; > 72 // zc <= b1 + k*P2.size() + a1; > 73 LL k = (zc-b1-a1) / P2.size() + ((zc-b1-a1)%P2.size() ? > 74 LL idx = P1.size()*1000000*k + (k-1)*k/2*P2.size() - b1; > 75 return 0<=idx && idx+zc<=10000000000000000LL ? idx : -1; > 76 } > 77 else > 78 { > 79 LL a1 = 0; while( P1[a1]=='0' ) ++a1; > 80 LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; > 81 LL c1=0, i1; > 82 for(int i=0; i<P1.size(); ++i) > 83 if( P1[i]=='0' ) { > 84 size_t ii = P1.find('1',i); > 85 if(ii==string::npos) ii=P1.size(); > 86 if( ii-i > c1 ) c1=ii-i, i1=i; > 87 } > 88 LL a2 = 0; while( P2[a2]=='0' ) ++a2; > 89 LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; > 90 LL c2=0, i2; > 91 for(int i=0; i<P2.size(); ++i) > 92 if( P2[i]=='0' ) { > 93 size_t ii = P2.find('1',i); > 94 if(ii==string::npos) ii=P2.size(); > 95 if( ii-i > c2 ) c2=ii-i, i2=i; > 96 } > 97 if( zc <= a1 ) > 98 return 0; > 99 if( zc <= c1 ) > 100 return i1; > 101 if( zc <= b1+a1 ) > 102 return P1.size() - b1; > 103 if( zc <= b1+a2 ) > 104 return P1.size()*1000000 - b1; > 105 if( zc <= c2 ) > 106 return P1.size()*1000000 + i2; > 107 if( zc <= b2+a1 ) > 108 return P1.size()*1000000 + P2.size() - b2; > 109 if( zc <= b2+a2 ) > 110 return P1.size()*1000000 + P2.size() + P1.size() > 111 return -1; > 112 } > 113 } > 114 }; > 115 > 116 // BEGIN CUT HERE > 117 #include <ctime> > 118 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 119 > 120 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 121 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 122 > 123 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 124 char Test_(...); > 125 int Test_(Case_<0>) { > 126 string P1 = "111010100001010"; > 127 string P2 = "010000001000"; > 128 string zeroCount = "3"; > 129 long long RetVal = 7LL; > 130 return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, ze > 131 int Test_(Case_<1>) { > 132 string P1 = "1101010010"; > 133 string P2 = "0001000"; > 134 string zeroCount = "3"; > 135 long long RetVal = 9999999LL; > 136 return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, ze > 137 int Test_(Case_<2>) { > 138 string P1 = "1101010010"; > 139 string P2 = "0001000"; > 140 string zeroCount = "5"; > 141 long long RetVal = 20000011LL; > 142 return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, ze > 143 int Test_(Case_<3>) { > 144 string P1 = "10101010"; > 145 string P2 = "101010101010"; > 146 string zeroCount = "9876543219876"; > 147 long long RetVal = -1LL; > 148 return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, ze > 149 int Test_(Case_<4>) { > 150 string P1 = "11111111111111111111111111"; > 151 string P2 = "0"; > 152 string zeroCount = "9876543219876"; > 153 long long RetVal = -1LL; > 154 return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, ze > 155 > 156 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 157 template<> void Run_<-1>() {} > 158 int main() { Run_<0>(); } > 159 // END CUT HERE > 160
Added SRM/333/1C.cpp version [b6921f196282e5d1]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class ThreeBuses { > 21 public: > 22 double getProbability(vector <int> wait, vector <int> travel, int timeLe > 23 { > 24 double t = timeLeft - accumulate(travel.begin(), travel.end(), 0 > 25 sort(wait.begin(), wait.end()); > 26 double w1 = wait[0]; > 27 double w2 = wait[1]; > 28 double w3 = wait[2]; > 29 // [0,w1] + [0,w2] + [0,w3] <= t ?? > 30 if( t >= w1+w2+w3 ) return 1; > 31 if( t <= 0 ) return 0; > 32 > 33 if( w2==0 ) return t / w3; > 34 if( w1==0 ) { > 35 double vol = t*t/2; > 36 if( t > w2 ) vol -= (t-w2)*(t-w2)/2; > 37 if( t > w3 ) vol -= (t-w3)*(t-w3)/2; > 38 return vol / (w2*w3); > 39 } > 40 > 41 double vol = t*t*t/6; > 42 if( t > w1 ) vol -= (t-w1)*(t-w1)*(t-w1)/6; > 43 if( t > w2 ) vol -= (t-w2)*(t-w2)*(t-w2)/6; > 44 if( t > w3 ) vol -= (t-w3)*(t-w3)*(t-w3)/6; > 45 if( t > w1+w2 ) vol += (t-w1-w2)*(t-w1-w2)*(t-w1-w2)/6; > 46 if( t > w2+w3 ) vol += (t-w2-w3)*(t-w2-w3)*(t-w2-w3)/6; > 47 if( t > w3+w1 ) vol += (t-w3-w1)*(t-w3-w1)*(t-w3-w1)/6; > 48 return vol / (w1*w2*w3); > 49 } > 50 }; > 51 > 52 // BEGIN CUT HERE > 53 #include <ctime> > 54 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 55 > 56 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 57 int verify_case(const double &Expected, const double &Received) { double diff = > 58 > 59 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 60 char Test_(...); > 61 int Test_(Case_<0>) { > 62 int wait_[] = {0, 0, 0}; > 63 vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); > 64 int travel_[] = {10, 15, 10}; > 65 vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)) > 66 int timeLeft = 47; > 67 double RetVal = 1.0; > 68 return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, tim > 69 int Test_(Case_<1>) { > 70 int wait_[] = {0, 0, 0}; > 71 vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); > 72 int travel_[] = {10, 15, 10}; > 73 vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)) > 74 int timeLeft = 35; > 75 double RetVal = 1.0; > 76 return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, tim > 77 int Test_(Case_<2>) { > 78 int wait_[] = {1, 100, 1}; > 79 vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); > 80 int travel_[] = {10, 10, 10}; > 81 vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)) > 82 int timeLeft = 52; > 83 double RetVal = 0.21; > 84 return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, tim > 85 int Test_(Case_<3>) { > 86 int wait_[] = {100, 100, 70}; > 87 vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); > 88 int travel_[] = {1, 1, 1}; > 89 vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)) > 90 int timeLeft = 47; > 91 double RetVal = 0.020281904761904737; > 92 return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, tim > 93 int Test_(Case_<4>) { > 94 int wait_[] = {43210, 31730, 0}; > 95 vector <int> wait(wait_, wait_+sizeof(wait_)/sizeof(*wait_)); > 96 int travel_[] = {1, 1, 1}; > 97 vector <int> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel_)) > 98 int timeLeft = 43212; > 99 double RetVal = 0.6328164776672066; > 100 return verify_case(RetVal, ThreeBuses().getProbability(wait, travel, tim > 101 > 102 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 103 template<> void Run_<-1>() {} > 104 int main() { Run_<0>(); } > 105 // END CUT HERE > 106
Added SRM/334/1A.cpp version [aed588e5efe51737]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class EncodedSum > 20 { > 21 public: > 22 long long maximumSum(vector <string> numbers) > 23 { > 24 set<int> nonzero; > 25 for(int i=0; i<numbers.size(); ++i) > 26 nonzero.insert( numbers[i][0]-'A' ); > 27 > 28 LL v[] = {0,0,0,0,0,0,0,0,0,0}; > 29 for(int i=0; i<numbers.size(); ++i) > 30 for(LL j=numbers[i].size()-1,d=1; j>=0; --j,d*=10) > 31 v[numbers[i][j]-'A'] += d; > 32 > 33 LL d[] = {0,1,2,3,4,5,6,7,8,9}; > 34 LL sumMax = 0; > 35 do { > 36 LL sum = 0; > 37 for(int c=0; c<10; ++c) > 38 if( d[c]==0 && nonzero.count(c) ) > 39 goto nextPerm; > 40 else > 41 sum += v[c] * d[c]; > 42 sumMax = max(sumMax, sum); > 43 nextPerm:; > 44 } while( next_permutation(d+0, d+10) ); > 45 return sumMax; > 46 } > 47 }; > 48 > 49 // BEGIN CUT HERE > 50 #include <ctime> > 51 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 52 > 53 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 54 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 55 > 56 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 57 char Test_(...); > 58 int Test_(Case_<0>) { > 59 string numbers_[] = {"ABC", > 60 "BCA"}; > 61 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 62 long long RetVal = 1875LL; > 63 return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } > 64 int Test_(Case_<1>) { > 65 string numbers_[] = {"ABCDEFGHIJ"}; > 66 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 67 long long RetVal = 9876543210LL; > 68 return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } > 69 int Test_(Case_<2>) { > 70 string numbers_[] = {"ABCDEFGHIJ", > 71 "J"}; > 72 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 73 long long RetVal = 9876543202LL; > 74 return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } > 75 int Test_(Case_<3>) { > 76 string numbers_[] = {"A", > 77 "BB", > 78 "CCC", > 79 "DDDD", > 80 "EEEEE", > 81 "FFFFFF", > 82 "GGGGGGG", > 83 "HHHHHHHH", > 84 "IIIIIIIII", > 85 "AJJJJJJJJJ"}; > 86 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 87 long long RetVal = 9973936905LL; > 88 return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } > 89 int Test_(Case_<4>) { > 90 string numbers_[] = {"GHJIDDD", > 91 "AHHCCCA", > 92 "IIJCEJ", > 93 "F", > 94 "HDBIGFJAAJ"}; > 95 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 96 long long RetVal = 9888114550LL; > 97 return verify_case(RetVal, EncodedSum().maximumSum(numbers)); } > 98 > 99 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 100 template<> void Run_<-1>() {} > 101 int main() { Run_<0>(); } > 102 // END CUT HERE > 103
Added SRM/334/1B.cpp version [3402769c07a731ca]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class ExtendedHappyNumbers > 20 { > 21 public: > 22 long long calcTheSum(int A, int B, int K) > 23 { > 24 vector<int> happy(9*9*9*9*9*9*7); > 25 // map<int,int> happy; > 26 LL sum = 0; > 27 for(int n=A; n<=B; ++n) > 28 sum += rec(n, K, happy); > 29 return sum; > 30 } > 31 > 32 int S(int K, int n) > 33 { > 34 int sum = 0; > 35 for(int d=1; d<=n; d*=10) > 36 { > 37 int v = n/d%10; > 38 int t = 1; > 39 for(int i=0; i<K; ++i) > 40 t *= v; > 41 sum += t; > 42 } > 43 return sum; > 44 } > 45 > 46 int rec(int n, int K, vector<int>& happy) > 47 { > 48 int LOOP = -1; > 49 stack<int> vs; > 50 int m = 0x7fffffff; > 51 for(;;) > 52 { > 53 if( happy[n] == LOOP ) > 54 {m = looped(n, K); break;} > 55 if( happy[n] ) > 56 {m = happy[n]; break;} > 57 happy[n] = LOOP; > 58 vs.push(n); > 59 n = S(K, n); > 60 } > 61 while(!vs.empty()) { > 62 int v = vs.top(); vs.pop(); > 63 happy[v] = m = min(m, v); > 64 } > 65 return m; > 66 } > 67 > 68 int looped(int n, int K) > 69 { > 70 int mn = n; > 71 vector<int> vs(1, n); > 72 for(int v=S(K,n); v!=n; v=S(K,v)) > 73 mn = min(mn, v); > 74 return mn; > 75 } > 76 }; > 77 > 78 // BEGIN CUT HERE > 79 #include <ctime> > 80 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 81 > 82 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 83 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 84 > 85 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 86 char Test_(...); > 87 int Test_(Case_<0>) { > 88 int A = 13; > 89 int B = 13; > 90 int K = 2; > 91 long long RetVal = 1LL; > 92 return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); > 93 int Test_(Case_<1>) { > 94 int A = 1; > 95 int B = 5; > 96 int K = 2; > 97 long long RetVal = 14LL; > 98 return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); > 99 int Test_(Case_<2>) { > 100 int A = 10; > 101 int B = 99; > 102 int K = 1; > 103 long long RetVal = 450LL; > 104 return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); > 105 int Test_(Case_<3>) { > 106 int A = 535; > 107 int B = 538; > 108 int K = 3; > 109 long long RetVal = 820LL; > 110 return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); > 111 int Test_(Case_<4>) { > 112 int A = 100000; > 113 int B = 400000; > 114 int K = 6; > 115 long long RetVal = 5169721292LL; > 116 return verify_case(RetVal, ExtendedHappyNumbers().calcTheSum(A, B, K)); > 117 > 118 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 119 template<> void Run_<-1>() {} > 120 int main() { Run_<0>(); } > 121 // END CUT HERE > 122
Added SRM/334/1C.cpp version [bbf018ad57eb14ba]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 typedef int cost; > 20 typedef int vert; > 21 typedef vector< vector<cost> > graph; > 22 > 23 cost bidi_minCut( graph G, int N) > 24 { > 25 vector<vert> V; > 26 for(int v=0; v<N; ++v) > 27 V.push_back(v); > 28 > 29 cost result = 0x7fffffff; > 30 for(int n=N; n>1; --n) > 31 { > 32 // invariant: > 33 // the current set of vertices = {V[0] .. V[n-1]} > 34 > 35 // order the vertices > 36 vector<int> vs; > 37 cost lastCut = 0; > 38 { > 39 vector<cost> ws(n, 0); > 40 ws[0] = 1; > 41 > 42 for(int i=0; i<n; ++i) { > 43 int j = max_element(ws.begin(), ws.end()) - ws.b > 44 lastCut = ws[j]; > 45 > 46 vs.push_back(j); > 47 ws[j] = -1; > 48 for(int k=0; k<n; ++k) > 49 if( ws[k] != -1 ) > 50 ws[k] += G[V[k]][V[j]]; > 51 } > 52 } > 53 > 54 // update mincut > 55 result = min(result, lastCut); > 56 > 57 // reduce the graph (unify vs[n-2] and vs[n-1]) > 58 vert v2=vs[n-2], v1=vs[n-1]; > 59 for(int i=0; i<n; ++i) { > 60 G[V[v2]][V[i]] += G[V[v1]][V[i]]; > 61 G[V[i]][V[v2]] += G[V[i]][V[v1]]; > 62 } > 63 V.erase( V.begin() + v1 ); > 64 } > 65 return result; > 66 } > 67 > 68 class Terrorists > 69 { > 70 public: > 71 int requiredCost(vector <string> roads) > 72 { > 73 int N = roads.size(); > 74 graph G(N, vector<cost>(N)); > 75 > 76 for(int i=0; i<N; ++i) > 77 for(int j=0; j<N; ++j) > 78 G[i][j] = roads[i][j]-'0'; > 79 > 80 return bidi_minCut(G, N); > 81 } > 82 }; > 83 > 84 // BEGIN CUT HERE > 85 #include <ctime> > 86 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 87 > 88 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 89 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 90 > 91 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 92 char Test_(...); > 93 int Test_(Case_<0>) { > 94 string roads_[] = {"0911", > 95 "9011", > 96 "1109", > 97 "1190"}; > 98 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 99 int RetVal = 4; > 100 return verify_case(RetVal, Terrorists().requiredCost(roads)); } > 101 int Test_(Case_<1>) { > 102 string roads_[] = {"0399", > 103 "3033", > 104 "9309", > 105 "9390"}; > 106 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 107 int RetVal = 9; > 108 return verify_case(RetVal, Terrorists().requiredCost(roads)); } > 109 int Test_(Case_<2>) { > 110 string roads_[] = {"030900", > 111 "304120", > 112 "040174", > 113 "911021", > 114 "027207", > 115 "004170"}; > 116 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 117 int RetVal = 8; > 118 return verify_case(RetVal, Terrorists().requiredCost(roads)); } > 119 int Test_(Case_<3>) { > 120 string roads_[] = {"044967263", > 121 "409134231", > 122 "490642938", > 123 "916036261", > 124 "634306024", > 125 "742660550", > 126 "229205069", > 127 "633625604", > 128 "318140940"}; > 129 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 130 int RetVal = 27; > 131 return verify_case(RetVal, Terrorists().requiredCost(roads)); } > 132 > 133 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 134 template<> void Run_<-1>() {} > 135 int main() { Run_<0>(); } > 136 // END CUT HERE > 137
Added SRM/335/1A.cpp version [9d8dcb43599a380c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class Multifactorial > 20 { > 21 public: > 22 string calcMultiFact(int n, int k) > 23 { > 24 LL ans = 1; > 25 for(LL v=n; v>0; v-=k) > 26 { > 27 if( ans*v<ans || ans*v/v!=ans || ans*v>10000000000000000 > 28 return "overflow"; > 29 ans *= v; > 30 } > 31 stringstream sout; > 32 sout << ans; > 33 return sout.str(); > 34 } > 35 }; > 36 > 37 // BEGIN CUT HERE > 38 #include <ctime> > 39 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 40 > 41 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 42 int verify_case(const string &Expected, const string &Received) { if (Expected = > 43 > 44 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 45 char Test_(...); > 46 int Test_(Case_<0>) { > 47 int n = 14; > 48 int k = 3; > 49 string RetVal = "12320"; > 50 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 51 int Test_(Case_<1>) { > 52 int n = 5; > 53 int k = 4; > 54 string RetVal = "5"; > 55 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 56 int Test_(Case_<2>) { > 57 int n = 1000; > 58 int k = 2; > 59 string RetVal = "overflow"; > 60 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 61 int Test_(Case_<3>) { > 62 int n = 2000000000; > 63 int k = 1900000000; > 64 string RetVal = "200000000000000000"; > 65 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 66 int Test_(Case_<4>) { > 67 int n = 1000; > 68 int k = 256; > 69 string RetVal = "84232704000"; > 70 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 71 int Test_(Case_<5>) { > 72 int n = 2000000000; > 73 int k = 1; > 74 string RetVal = "overflow"; > 75 return verify_case(RetVal, Multifactorial().calcMultiFact(n, k)); } > 76 > 77 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 78 template<> void Run_<-1>() {} > 79 int main() { Run_<0>(); } > 80 // END CUT HERE > 81
Added SRM/335/1B.cpp version [77edbded42c604a6]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 typedef pair<int,int> vert; > 20 typedef int cost; > 21 > 22 // dijkstra > 23 template<typename OutIt> > 24 void neighbors(cost e[50][50], int Y, int X, vert v, bool vis[50][50], OutIt out > 25 { > 26 typedef pair<cost,vert> cedge; > 27 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 28 Q.push( cedge(e[v.first][v.second],v) ); > 29 > 30 set<vert> visited_now; > 31 while( !Q.empty() ) > 32 { > 33 cedge ce = Q.top(); Q.pop(); > 34 cost cc = ce.first; > 35 vert cv = ce.second; > 36 if( visited_now.count(cv) ) > 37 continue; > 38 visited_now.insert(cv); > 39 > 40 if( !vis[cv.first][cv.second] ) { > 41 *out++ = cv; > 42 vis[cv.first][cv.second] = true; > 43 } > 44 > 45 // for 4 neibs > 46 int dy[]={0,0,-1,+1}; > 47 int dx[]={-1,+1,0,0}; > 48 for(int i=0; i<4; ++i) > 49 { > 50 int yyy = cv.first + dy[i]; > 51 int xxx = cv.second + dx[i]; > 52 if( 0<=yyy && yyy<Y && 0<=xxx && xxx<X ) > 53 { > 54 int ccc = cc + e[yyy][xxx]; > 55 vert vvv(yyy, xxx); > 56 if( ccc<=9*8*7*6*5*4*3*2*1 ) > 57 Q.push( cedge(ccc, vvv) ); > 58 } > 59 } > 60 } > 61 } > 62 > 63 int bfs( cost e[50][50], int Y, int X, vert s, vert d ) > 64 { > 65 vector<vert> Q(1, s); > 66 bool vis[50][50] = {}; vis[s.first][s.second] = true; > 67 > 68 for(int step=0; !Q.empty(); ++step) > 69 { > 70 vector<vert> Q2; > 71 for(int i=0; i!=Q.size(); ++i) > 72 { > 73 if( Q[i] == d ) > 74 return step; > 75 neighbors(e, Y, X, Q[i], vis, back_inserter(Q2)); > 76 } > 77 Q.swap(Q2); > 78 } > 79 return -1; > 80 } > 81 > 82 class ExpensiveTravel > 83 { > 84 public: > 85 int minTime(vector <string> m, int startRow, int startCol, int endRow, i > 86 { > 87 int Y = m.size(), X = m[0].size(); > 88 int Sy = startRow-1, Sx = startCol-1; > 89 int Ey = endRow-1, Ex = endCol-1; > 90 cost e[50][50]; > 91 for(int y=0; y<Y; ++y) > 92 for(int x=0; x<X; ++x) > 93 e[y][x] = 9*8*7*6*5*4*3*2*1 / (m[y][x]-'0'); > 94 > 95 return bfs( e, Y, X, vert(Sy,Sx), vert(Ey,Ex) ); > 96 } > 97 }; > 98 > 99 // BEGIN CUT HERE > 100 #include <ctime> > 101 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 102 > 103 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 104 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 105 > 106 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 107 char Test_(...); > 108 int Test_(Case_<0>) { > 109 string m_[] = {"22334"}; > 110 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 111 int startRow = 1; > 112 int startCol = 1; > 113 int endRow = 1; > 114 int endCol = 5; > 115 int RetVal = 3; > 116 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 117 int Test_(Case_<1>) { > 118 string m_[] = {"55", > 119 "52", > 120 "55"}; > 121 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 122 int startRow = 1; > 123 int startCol = 2; > 124 int endRow = 3; > 125 int endCol = 2; > 126 int RetVal = 1; > 127 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 128 int Test_(Case_<2>) { > 129 string m_[] = {"251", > 130 "212", > 131 "122"}; > 132 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 133 int startRow = 1; > 134 int startCol = 1; > 135 int endRow = 3; > 136 int endCol = 3; > 137 int RetVal = -1; > 138 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 139 int Test_(Case_<3>) { > 140 string m_[] = {"452232", > 141 "287979", > 142 "219872", > 143 "928234", > 144 "767676"}; > 145 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 146 int startRow = 1; > 147 int startCol = 6; > 148 int endRow = 3; > 149 int endCol = 1; > 150 int RetVal = 3; > 151 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 152 int Test_(Case_<4>) { > 153 string m_[] = {"11"}; > 154 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 155 int startRow = 1; > 156 int startCol = 1; > 157 int endRow = 1; > 158 int endCol = 2; > 159 int RetVal = -1; > 160 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 161 int Test_(Case_<5>) { > 162 string m_[] = {"123456789987654321"}; > 163 vector <string> m(m_, m_+sizeof(m_)/sizeof(*m_)); > 164 int startRow = 1; > 165 int startCol = 2; > 166 int endRow = 1; > 167 int endCol = 16; > 168 int RetVal = 5; > 169 return verify_case(RetVal, ExpensiveTravel().minTime(m, startRow, startC > 170 > 171 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 172 template<> void Run_<-1>() {} > 173 int main() { Run_<0>(); } > 174 // END CUT HERE > 175
Added SRM/335/1C.cpp version [3fd802a4d545ca8e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 void gen( vector<int>& X, vector<LL>& R, int n ) > 20 { > 21 int j = 0; > 22 int m = X.size(); > 23 for(int i=0; i<n; ++i) > 24 { > 25 R[i] = X[j]; > 26 int s = (j+1)%m; > 27 X[j] = ( ( X[j] ^ X[s] ) + 13 ) % 49999; > 28 j = s; > 29 } > 30 } > 31 > 32 class RandomFights > 33 { > 34 public: > 35 double expectedNrOfPoints(vector <int> Aseed, vector <int> Bseed, int n) > 36 { > 37 vector<LL> A(n), B(n); > 38 gen(Aseed, A, n); > 39 gen(Bseed, B, n); > 40 > 41 // expected gain by A[i] = sqSum{B[j] | B[j]<A[i]} - sqSum{B[j] > 42 > 43 sort(B.begin(), B.end()); > 44 vector<LL> B2(n); > 45 for(int i=0; i<n; ++i) B2[i] = B[i]*B[i]; > 46 > 47 vector<LL> L1(n), R1(n), L2(n), R2(n); > 48 partial_sum( B.begin(), B.end(), L1.begin() ); > 49 partial_sum( B.rbegin(), B.rend(), R1.rbegin() ); > 50 partial_sum( B2.begin(), B2.end(), L2.begin() ); > 51 partial_sum( B2.rbegin(), B2.rend(), R2.rbegin() ); > 52 > 53 LL e = 0; > 54 for(int i=0; i<n; ++i) > 55 { > 56 int j = lower_bound(B.begin(), B.end(), A[i]) - B.begin( > 57 > 58 LL ln = j; > 59 LL ls = (j==0 ? 0 : L1[j-1]); > 60 LL ls2 = (j==0 ? 0 : L2[j-1]); > 61 LL rn = n-j; > 62 LL rs = (j==n ? 0 : R1[j]); > 63 LL rs2 = (j==n ? 0 : R2[j]); > 64 > 65 e += (ls2-rs2) - 2*A[i]*(ls-rs) + (ln-rn)*A[i]*A[i]; > 66 } > 67 return double(e) / n; > 68 } > 69 }; > 70 > 71 // BEGIN CUT HERE > 72 #include <ctime> > 73 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 74 > 75 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 76 int verify_case(const double &Expected, const double &Received) { double diff = > 77 > 78 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 79 char Test_(...); > 80 int Test_(Case_<0>) { > 81 int A_[] = {6}; > 82 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 83 int B_[] = {4}; > 84 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 85 int n = 1; > 86 double RetVal = 4.0; > 87 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 88 int Test_(Case_<1>) { > 89 int A_[] = {1,7}; > 90 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 91 int B_[] = {3,5}; > 92 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 93 int n = 2; > 94 double RetVal = 0.0; > 95 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 96 int Test_(Case_<2>) { > 97 int A_[] = {3,7}; > 98 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 99 int B_[] = {1,5}; > 100 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 101 int n = 2; > 102 double RetVal = 20.0; > 103 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 104 int Test_(Case_<3>) { > 105 int A_[] = {45,35,236,2342,5543,21,32,2,6,23,24,23,41,1}; > 106 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 107 int B_[] = {2345,45,2345,345}; > 108 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 109 int n = 50; > 110 double RetVal = 1.2721986164E8; > 111 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 112 int Test_(Case_<4>) { > 113 int A_[] = {34,3245,2534,53,53,46,346,246,346,2,624,624,6,245,6324,6542, > 114 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 115 int B_[] = {345,234,523,4624,6,2456,345,634,634,7,3457,376,34,6234,62,52 > 116 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 117 int n = 7; > 118 double RetVal = -9713701.714285715; > 119 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 120 int Test_(Case_<5>) { > 121 int A_[] = {1,2,3,4}; > 122 vector <int> A(A_, A_+sizeof(A_)/sizeof(*A_)); > 123 int B_[] = {1,2,3,4}; > 124 vector <int> B(B_, B_+sizeof(B_)/sizeof(*B_)); > 125 int n = 50000; > 126 double RetVal = 0.0; > 127 return verify_case(RetVal, RandomFights().expectedNrOfPoints(A, B, n)); > 128 > 129 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 130 template<> void Run_<-1>() {} > 131 int main() { Run_<0>(); } > 132 // END CUT HERE > 133
Added SRM/336/1A.cpp version [4da7fa5c9a34c708]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class ServiceNames > 20 { > 21 public: > 22 vector <string> makeList(vector <string> services) > 23 { > 24 map< string, set<string> > lst; > 25 for(int i=0; i<services.size(); ++i) { > 26 stringstream sin(services[i]); > 27 string h; sin>>h; > 28 for(string s; sin>>s; ) > 29 lst[s].insert(h); > 30 } > 31 > 32 vector<string> out; > 33 transform(lst.begin(), lst.end(), back_inserter(out), &pr); > 34 return out; > 35 } > 36 > 37 static string pr( const pair< string, set<string> >& p ) > 38 { > 39 stringstream sout; > 40 sout << p.first; > 41 string sep = " ==> "; > 42 for(set<string>::const_iterator it=p.second.begin(); it!=p.secon > 43 sout << sep << *it, sep=", "; > 44 return sout.str(); > 45 } > 46 > 47 // BEGIN CUT HERE > 48 public: > 49 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 50 private: > 51 template <typename T> string print_array(const vector<T> &V) { ostringst > 52 void verify_case(int Case, const vector <string> &Expected, const vector > 53 void test_case_0() { string Arr0[] = {"BLAST Genome Annotation Sensitivi > 54 void test_case_1() { string Arr0[] = {"PING"}; vector <string> Arg0(Arr0 > 55 void test_case_2() { string Arr0[] = {"BLAST Genome annotation Sensitivi > 56 > 57 // END CUT HERE > 58 }; > 59 // BEGIN CUT HERE > 60 int main() { ServiceNames().run_test(-1); } > 61 // END CUT HERE
Added SRM/336/1B.cpp version [3c0ef4de970a84c9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class LikelyWord > 20 { > 21 public: > 22 int likely(vector <string> dictionary, int k) > 23 { > 24 vector<LL> dl, dr; > 25 dl.push_back(-1); > 26 dr.push_back(-1); > 27 for(int i=0; i<dictionary.size(); ++i) { > 28 dl.push_back( toLLl(dictionary[i],k) ); > 29 dr.push_back( toLLr(dictionary[i],k) ); > 30 } > 31 dl.push_back( ipow(26,k) ); > 32 dr.push_back( ipow(26,k) ); > 33 > 34 LL max=-1, maxi=-1; > 35 for(int i=0; i+1<dl.size(); ++i) { > 36 LL hm = dr[i+1] - dl[i] - 1; > 37 if( hm>max ) > 38 max=hm, maxi=i; > 39 else if(hm==max) > 40 maxi=-1; > 41 } > 42 return maxi; > 43 } > 44 > 45 LL toLLl( string s, LL k ) > 46 { > 47 if( s.size() > k ) > 48 s.resize(k); > 49 else if( s.size() < k ) > 50 s.resize(k, 'a'), s[s.size()-1]-=1; > 51 return toLL(s,k); > 52 } > 53 > 54 LL toLLr( string s, LL k ) > 55 { > 56 if( s.size() > k ) > 57 s.resize(k), s[s.size()-1]+=1; > 58 else if( s.size() < k ) > 59 s.resize(k, 'a'); > 60 return toLL(s,k); > 61 } > 62 > 63 LL toLL( string s, LL k ) > 64 { > 65 LL n = 0; > 66 for(int i=0; i<k; ++i) > 67 n = n*26 + s[i]-'a'; > 68 return n; > 69 } > 70 > 71 LL ipow(LL a, LL k) { > 72 LL n = 1; > 73 while(k--) > 74 n *= a; > 75 return n; > 76 } > 77 > 78 // BEGIN CUT HERE > 79 public: > 80 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 81 private: > 82 template <typename T> string print_array(const vector<T> &V) { ostringst > 83 void verify_case(int Case, const int &Expected, const int &Received) { c > 84 void test_case_0() { string Arr0[] = {"time","zoology"}; vector <string> > 85 void test_case_1() { string Arr0[] = {"az","ma","xz"}; vector <string> A > 86 void test_case_2() { string Arr0[] = {"az","ma","xz"}; vector <string> A > 87 void test_case_3() { string Arr0[] = {"a","m","y"}; vector <string> Arg0 > 88 void test_case_4() { string Arr0[] = {"a", "b", "c", "d", "e", "f", "g", > 89 void test_case_5() { string Arr0[] = {"aa", "ab", "b", "c", "d", "e", "f > 90 > 91 // END CUT HERE > 92 }; > 93 // BEGIN CUT HERE > 94 int main() { LikelyWord().run_test(-1); } > 95 // END CUT HERE
Added SRM/336/1C.cpp version [6ae24e1dde2eb7e0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 typedef complex<double> pt; > 20 > 21 bool byY( pt a, pt b ) > 22 { > 23 if( a.imag() == b.imag() ) > 24 return a.real() < b.real(); > 25 return a.imag() < b.imag(); > 26 } > 27 > 28 bool byArg( pt a, pt b ) > 29 { > 30 return arg(a) < arg(b); > 31 } > 32 > 33 bool isRight( pt a, pt b, pt c ) > 34 { > 35 return arg((c-b) / (b-a)) < 0; > 36 } > 37 > 38 vector<pt> convex_hull( vector<pt> p ) > 39 { > 40 vector<pt>::iterator it = min_element( p.begin(), p.end(), byY ); > 41 pt o = *it; > 42 p.erase(it); > 43 for(int i=0; i<p.size(); ++i) > 44 p[i] -= o; > 45 sort( p.begin(), p.end(), byArg ); > 46 > 47 vector<pt> q; > 48 q.push_back( pt(0,0) ); > 49 q.push_back( p[0] ); > 50 for(int i=1; i<p.size(); ++i) { > 51 while( isRight(q[q.size()-2], q[q.size()-1], p[i]) ) > 52 q.pop_back(); > 53 q.push_back( p[i] ); > 54 } > 55 for(int i=0; i<q.size(); ++i) > 56 q[i] += o; > 57 return q; > 58 } > 59 > 60 double outer_prod( pt a, pt b ) > 61 { > 62 return (a.real()*b.imag() - b.real()*a.imag())/2; > 63 } > 64 > 65 double area( const vector<pt>& q ) > 66 { > 67 double a = 0.0; > 68 > 69 pt o = q[0]; > 70 for(int i=1; i+1<q.size(); ++i) > 71 a += outer_prod(q[i]-o, q[i+1]-o); > 72 return abs(a); > 73 } > 74 > 75 > 76 class Shadow > 77 { > 78 public: > 79 double area(vector <int> tree, vector <int> light) > 80 { > 81 double x1 = min(tree[0], tree[3]); > 82 double x2 = max(tree[0], tree[3]); > 83 double y1 = min(tree[1], tree[4]); > 84 double y2 = max(tree[1], tree[4]); > 85 double z1 = min(tree[2], tree[5]); > 86 double z2 = max(tree[2], tree[5]); > 87 double x = light[0]; > 88 double y = light[1]; > 89 double z = light[2]; > 90 > 91 if(x1==x2 && y1==y2 || y1==y2 && z1==z2 || z1==z2 && x1==x2 ) > 92 return 0; // line > 93 if(x1==x2 && x==x1 || y1==y2 && y==y1 || z1==z2 && z==z1 ) > 94 return 0; // seen as a line > 95 if( y1 >= y ) > 96 return 0; // tree above light > 97 if( y2 >= y ) > 98 return -1; // infinite > 99 > 100 // otherwise project all vertices two y=0 plain > 101 vector<pt> p; > 102 for(int i=0; i<8; ++i) > 103 { > 104 double X=(i&1 ? x1 : x2); > 105 double Y=(i&2 ? y1 : y2); > 106 double Z=(i&4 ? z1 : z2); > 107 > 108 // (Xp-X) : (x-X) = (0-Y) : (y-Y) > 109 double Xp = (X-x)*Y/(y-Y) + X; > 110 double Zp = (Z-z)*Y/(y-Y) + Z; > 111 p.push_back( pt(Xp,Zp) ); > 112 } > 113 > 114 // calc convex hull and its area > 115 p = convex_hull(p); > 116 return ::area(p); > 117 } > 118 > 119 // BEGIN CUT HERE > 120 public: > 121 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 122 private: > 123 template <typename T> string print_array(const vector<T> &V) { ostringst > 124 void verify_case(int Case, const double &Expected, const double &Receive > 125 void test_case_0() { int Arr0[] = {1,1,1, 10,1,1}; vector <int> Arg0(Arr > 126 void test_case_1() { int Arr0[] = {1,3,1, 10,1,1}; vector <int> Arg0(Arr > 127 void test_case_2() { int Arr0[] = {1,1,1, 2,2,2}; vector <int> Arg0(Arr0 > 128 void test_case_3() { int Arr0[] = {1,1,1, 3,3,3} ; vector <int> Arg0(Ar > 129 > 130 // END CUT HERE > 131 }; > 132 // BEGIN CUT HERE > 133 int main() { Shadow().run_test(-1); } > 134 // END CUT HERE
Added SRM/337/1A.cpp version [73f5cf8f82efdd62]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class CardStraights > 19 { > 20 public: > 21 int longestStraight(vector <int> cards) > 22 { > 23 int jok = count(cards.begin(), cards.end(), 0); > 24 > 25 set<int> cs(cards.begin(), cards.end()); > 26 cs.erase(0); > 27 if( cs.empty() ) > 28 return jok; // Ouggggggggg > 29 > 30 int ans = 0; > 31 for(set<int>::iterator it=cs.begin(); it!=cs.end(); ++it) > 32 ans = max(ans, asLongAsPossible(it, cs.end(), jok)); > 33 return ans; > 34 } > 35 > 36 int asLongAsPossible(set<int>::iterator it, set<int>::iterator end, int > 37 { > 38 int b = *it; > 39 int e = *it; > 40 for(++it; it!=end; e=*it++) > 41 if( *it-e-1 <= jok ) > 42 jok -= *it-e-1; > 43 else > 44 break; > 45 int nAfter = min(1000000-e, jok); > 46 e += nAfter; > 47 b -= min(b-1, jok-nAfter); > 48 return e-b+1; > 49 } > 50 > 51 // BEGIN CUT HERE > 52 public: > 53 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 54 private: > 55 template <typename T> string print_array(const vector<T> &V) { ostringst > 56 void verify_case(int Case, const int &Expected, const int &Received) { c > 57 void test_case_0() { int Arr0[] = {0,6,5,10,3,0,11}; vector <int> Arg0(A > 58 void test_case_1() { int Arr0[] = {100,100,100,101,100,99,97,103}; vecto > 59 void test_case_2() { int Arr0[] = {0,0,0,1,2,6,8,1000}; vector <int> Arg > 60 void test_case_3() { int Arr0[] = {1,9,5,7,3,4,0,0,0,10}; vector <int> A > 61 void test_case_4() { int Arr0[] = {0,0,999999}; vector <int> Arg0(Arr0, > 62 > 63 // END CUT HERE > 64 }; > 65 // BEGIN CUT HERE > 66 int main() { CardStraights().run_test(-1); } > 67 // END CUT HERE
Added SRM/337/1B+.cpp version [b4ff9352141581da]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 template<typename T> > 20 struct RMQ > 21 { > 22 vector< vector<int> > rm; > 23 vector<T> d; > 24 > 25 RMQ( const vector<T>& d ) : d(d) { > 26 int n = d.size(); > 27 > 28 // rm[k][x] = z s.t. d[z] is the minimum in [x, x+2^k) > 29 rm.push_back( vector<int>(n) ); > 30 for(int x=0; x<n; ++x) > 31 rm[0][x] = x; > 32 for(int k=1; (1<<k)<=n; ++k) { > 33 rm.push_back( rm[k-1] ); > 34 for(int x=0; x+(1<<k-1)<n; ++x) > 35 if( d[rm[k][x]] > d[rm[k-1][x + (1<<k-1)]] ) > 36 rm[k][x] = rm[k-1][x + (1<<k-1)]; > 37 } > 38 } > 39 > 40 int operator()(int L, int R) const { // inclusive > 41 int k=0; > 42 for(; L+(1<<k) < R-(1<<k)+1; ++k) {} > 43 LL i = rm[k][L]; > 44 LL j = rm[k][R-(1<<k)+1]; > 45 return (d[i]<=d[j] ? i : j); > 46 } > 47 }; > 48 > 49 class BuildingAdvertise > 50 { > 51 public: > 52 long long getMaxArea(vector <int> hseed, int n) > 53 { > 54 // input > 55 LL j = 0; > 56 LL m = hseed.size(); > 57 vector<LL> h(n); > 58 for(int i=0; i<n; ++i) { > 59 h[i] = hseed[j]; > 60 LL s = (j+1)%m; > 61 hseed[j] = ( ( hseed[j] ^ hseed[s] ) + 13 ) % 835454957; > 62 j = s; > 63 } > 64 > 65 // solve > 66 return rec(RMQ<LL>(h), h, 0, n-1); > 67 } > 68 LL rec(const RMQ<LL>& rmq, vector<LL>& h, LL L, LL R) > 69 { > 70 LL result = 0; > 71 > 72 stack< pair<LL,LL> > S; > 73 S.push( make_pair(L,R) ); > 74 while( !S.empty() ) > 75 { > 76 L = S.top().first; > 77 R = S.top().second; > 78 S.pop(); > 79 > 80 LL C = rmq(L, R); > 81 if( L < C ) S.push( make_pair(L,C-1) ); > 82 if( C < R ) S.push( make_pair(C+1,R) ); > 83 result = max(result, (R-L+1) * h[C]); > 84 } > 85 > 86 return result; > 87 } > 88 > 89 // BEGIN CUT HERE > 90 public: > 91 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 92 private: > 93 template <typename T> string print_array(const vector<T> &V) { ostringst > 94 void verify_case(int Case, const long long &Expected, const long long &R > 95 void test_case_0() { int Arr0[] = {3,6,5,6,2,4}; vector <int> Arg0(Arr0, > 96 void test_case_1() { int Arr0[] = {5,0,7,0,2,6,2}; vector <int> Arg0(Arr > 97 void test_case_2() { int Arr0[] = {1048589,2097165}; vector <int> Arg0(A > 98 void test_case_3() { int Arr0[] = {1,7,2,5,3,1}; vector <int> Arg0(Arr0, > 99 > 100 // END CUT HERE > 101 }; > 102 // BEGIN CUT HERE > 103 int main() { BuildingAdvertise().run_test(-1); } > 104 // END CUT HERE
Added SRM/337/1B.cpp version [15a7013bcd917907]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class BuildingAdvertise > 20 { > 21 public: > 22 long long getMaxArea(vector <int> h, int n) > 23 { > 24 // input > 25 LL j = 0; > 26 LL m = h.size(); > 27 vector<LL> R(n); > 28 for(int i=0; i<n; ++i) { > 29 R[i] = h[j]; > 30 LL s = (j+1)%m; > 31 h[j] = ( ( h[j] ^ h[s] ) + 13 ) % 835454957; > 32 j = s; > 33 } > 34 > 35 // solve > 36 vector<int> left(n); > 37 { > 38 map<LL, int> h; h[-1] = -1; > 39 for(int i=0; i<n; ++i) { > 40 // position of the highest building < R[i] > 41 map<LL,int>::iterator it = h.lower_bound(R[i]); > 42 left[i] = (--it)->second+1; > 43 h.erase( ++it, h.end() ); > 44 h[ R[i] ] = i; > 45 } > 46 } > 47 vector<int> right(n); > 48 { > 49 map<LL, int> h; h[-1] = n; > 50 for(int i=n-1; i>=0; --i) { > 51 // position of the highest building < R[i] > 52 map<LL,int>::iterator it = h.lower_bound(R[i]); > 53 right[i] = (--it)->second-1; > 54 h.erase( ++it, h.end() ); > 55 h[ R[i] ] = i; > 56 } > 57 } > 58 LL ans = 0; > 59 for(int i=0; i<n; ++i) { > 60 LL area = R[i] * (right[i] - left[i] + 1); > 61 ans = max(ans, area); > 62 } > 63 return ans; > 64 } > 65 > 66 // BEGIN CUT HERE > 67 public: > 68 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 69 private: > 70 template <typename T> string print_array(const vector<T> &V) { ostringst > 71 void verify_case(int Case, const long long &Expected, const long long &R > 72 void test_case_0() { int Arr0[] = {3,6,5,6,2,4}; vector <int> Arg0(Arr0, > 73 void test_case_1() { int Arr0[] = {5,0,7,0,2,6,2}; vector <int> Arg0(Arr > 74 void test_case_2() { int Arr0[] = {1048589,2097165}; vector <int> Arg0(A > 75 void test_case_3() { int Arr0[] = {1,7,2,5,3,1}; vector <int> Arg0(Arr0, > 76 void test_case_4() { int Arr0[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, > 77 // END CUT HERE > 78 }; > 79 // BEGIN CUT HERE > 80 int main() { BuildingAdvertise().run_test(-1); } > 81 // END CUT HERE
Added SRM/337/1C.cpp version [c85a397b1091370e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 static const LL MODVAL = 835454957; > 19 int memo[50*15*101*2]; > 20 int memo_k[101]; > 21 > 22 class CountPalindromes > 23 { > 24 public: > 25 vector<string> w; > 26 int count(vector<string> words, int k) > 27 { > 28 w = words; > 29 memset(memo, 0xff, sizeof(memo)); > 30 > 31 LL ans = 0; > 32 memo_k[0] = 0; > 33 for(int kk=1; kk<=k; ++kk) { > 34 LL ak = 0; > 35 for(int i=0; i<w.size(); ++i) > 36 ak += rec_l(i, 0, kk); > 37 ans += (memo_k[kk] = ak%MODVAL); > 38 } > 39 return int(ans % MODVAL); > 40 > 41 } > 42 > 43 int rec_l(int i, int ix, int k) > 44 { > 45 int key = (((i*15)+ix)*101+k)*2+0; > 46 if( memo[key]>=0 ) > 47 return memo[key]; > 48 > 49 LL ans = 0; > 50 for(int j=0; j<w.size(); ++j) > 51 ans += rec(i, ix, j, w[j].size()-1, k); > 52 return memo[key] = int(ans % MODVAL); > 53 } > 54 > 55 int rec_r(int j, int jx, int k) > 56 { > 57 int key = (((j*15)+jx)*101+k)*2+1; > 58 if( memo[key]>=0 ) > 59 return memo[key]; > 60 > 61 LL ans = 0; > 62 for(int i=0; i<w.size(); ++i) > 63 ans += rec(i, 0, j, jx, k); > 64 return memo[key] = int(ans % MODVAL); > 65 } > 66 > 67 > 68 int rec(int i, int ix, int j, int jx, int k) > 69 { > 70 while( k>=2 && ix<w[i].size() && 0<=jx ) > 71 if( w[i][ix] != w[j][jx] ) > 72 return 0; > 73 else > 74 ++ix, --jx, k-=2; > 75 > 76 if( k == 0 ) > 77 return (i==j && ix==jx+1 ? 1 : 0); > 78 if( k == 1 ) > 79 return (i==j && ix==jx || ix==w[i].size() && jx==-1 ? 1 > 80 > 81 if( ix==w[i].size() && jx==-1 ) > 82 return memo_k[k-2]; > 83 else if( ix==w[i].size() ) > 84 return rec_r(j, jx, k-1); > 85 else if( jx==-1 ) > 86 return rec_l(i, ix, k-1); > 87 else > 88 return 0; > 89 } > 90 > 91 // BEGIN CUT HERE > 92 public: > 93 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 94 private: > 95 template <typename T> string print_array(const vector<T> &V) { ostringst > 96 void verify_case(int Case, const int &Expected, const int &Received) { c > 97 void test_case_0() { string Arr0[] = {"tragic","cigar"}; vector <string> > 98 void test_case_1() { string Arr0[] = {"z","zz"}; vector <string> Arg0(Ar > 99 void test_case_2() { string Arr0[] = {"aba","acaba","baca","cac","b","c" > 100 void test_case_3() { string Arr0[] = {"hello"}; vector <string> Arg0(Arr > 101 void test_case_4() { string Arr0[] = {"a", "aaaaaaaaab", "aaa", "aaaaaaa > 102 // END CUT HERE > 103 }; > 104 // BEGIN CUT HERE > 105 int main() { CountPalindromes().run_test(-1); } > 106 // END CUT HERE
Added SRM/338/1A.cpp version [35838fc5989717b0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class ImprovingStatistics > 19 { > 20 public: > 21 int howManyGames(int played, int won) > 22 { > 23 LL p = played, w = won; > 24 LL z = w*100 / p; > 25 > 26 // z+1 <= (w+X)*100 / (p+X) > 27 // (z+1)(p+X) <= (w+X)*100 > 28 // (z+1)*p-w*100 <= (99-z)X > 29 if( z >= 99 ) > 30 return -1; > 31 LL a = (z+1)*p-w*100, b = 99-z; > 32 // a <= b*X > 33 return int(a%b==0 ? a/b : a/b+1); > 34 } > 35 > 36 // BEGIN CUT HERE > 37 public: > 38 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 39 private: > 40 template <typename T> string print_array(const vector<T> &V) { ostringst > 41 void verify_case(int Case, const int &Expected, const int &Received) { c > 42 void test_case_0() { int Arg0 = 10; int Arg1 = 8; int Arg2 = 1; verify_c > 43 void test_case_1() { int Arg0 = 100; int Arg1 = 80; int Arg2 = 6; verify > 44 void test_case_2() { int Arg0 = 47; int Arg1 = 47; int Arg2 = -1; verify > 45 void test_case_3() { int Arg0 = 99000; int Arg1 = 0; int Arg2 = 1000; ve > 46 void test_case_4() { int Arg0 = 1000000000; int Arg1 = 470000000; int Ar > 47 > 48 // END CUT HERE > 49 }; > 50 // BEGIN CUT HERE > 51 int main() { ImprovingStatistics().run_test(-1); } > 52 // END CUT HERE
Added SRM/338/1B.cpp version [21cb8b2950411a4a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 vector< vector<double> > mMul( const vector< vector<double> >& A, const vector< > 19 { > 20 const int n = A.size(); > 21 > 22 vector< vector<double> > C(n, vector<double>(n)); > 23 for(int i=0; i<n; ++i) > 24 for(int j=0; j<n; ++j) { > 25 double Cij = 0; > 26 for(int k=0; k<n; ++k) > 27 Cij += A[i][k] * B[k][j]; > 28 C[i][j] = Cij; > 29 } > 30 return C; > 31 } > 32 > 33 vector< vector<double> > mPow( vector< vector<double> > M, int t ) // t>0 > 34 { > 35 vector< vector<double> > R; > 36 for(; t; t>>=1, M=mMul(M,M)) > 37 if( t&1 ) > 38 R = (R.empty() ? M : mMul(R,M)); > 39 return R; > 40 } > 41 > 42 class RandomSwaps > 43 { > 44 public: > 45 double getProbability(int arrayLength, int swapCount, int a, int b) > 46 { > 47 double P = 1 / double(arrayLength * (arrayLength-1)/2); > 48 double Q = 1 - (arrayLength-1)*P; > 49 if( a != b ) { > 50 vector< vector<double> > M(3, vector<double>(3)); > 51 M[0][0] = Q; > 52 M[0][1] = P; > 53 M[0][2] = 1 - P - Q; > 54 M[1][0] = P; > 55 M[1][1] = Q; > 56 M[1][2] = 1 - P - Q; > 57 M[2][0] = P; > 58 M[2][1] = P; > 59 M[2][2] = 1 - M[2][0] - M[2][1]; > 60 M = mPow( M, swapCount ); > 61 return M[0][1]; > 62 } else { > 63 vector< vector<double> > M(2, vector<double>(2)); > 64 M[0][0] = Q; > 65 M[0][1] = 1 - M[0][0]; > 66 M[1][0] = P; > 67 M[1][1] = 1 - P; > 68 M = mPow( M, swapCount ); > 69 return M[0][0]; > 70 } > 71 } > 72 > 73 // BEGIN CUT HERE > 74 public: > 75 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 76 private: > 77 template <typename T> string print_array(const vector<T> &V) { ostringst > 78 void verify_case(int Case, const double &Expected, const double &Receive > 79 void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 > 80 void test_case_1() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 0; int Arg3 > 81 void test_case_2() { int Arg0 = 5; int Arg1 = 2; int Arg2 = 0; int Arg3 > 82 void test_case_3() { int Arg0 = 100; int Arg1 = 500; int Arg2 = 3; int A > 83 > 84 // END CUT HERE > 85 }; > 86 // BEGIN CUT HERE > 87 int main() { RandomSwaps().run_test(-1); } > 88 // END CUT HERE
Added SRM/338/1C.cpp version [c308f15b218b41e1]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class CakeParty > 20 { > 21 public: > 22 string makeMove(vector <int> pieces) > 23 { > 24 // Win iff #{i | pieces[i]==max(pieces)} is odd > 25 // --> if #==1 move not to make the opponent winning pos > 26 // if #==odd>=3 any move will do. choose the lexic-first > 27 // if #==even any move leads to lose. choose the lex1st > 28 /* > 29 <<Sprague Grundy Theorem ‚É‚æ‚铱o>> > 30 > 31 ’藂̃Xƒe[ƒgƒƒ“ƒg > 32 > 33 ‘ÎÛ‚Æ‚È‚éƒQ[ƒ€ > 34 - “ñlƒQ[ƒ€ > 35 - “ñl‚Æ‚à“®‚©‚¹‚éŽè‚ÌW‡‚Í“¯‚¶ > 36 - “®‚©‚¹‚È‚‚È‚Á‚½•û‚ª•‰‚¯ > 37 - –³ŒÀƒ‹[ƒv‚µ‚È‚¢ > 38 > 39 nim > 40 - ƒTƒCƒY s1, s2, .., sk ‚ÌŽR‚ª‚ ‚é > 41 - ‚Ç‚ê‚©ˆê‚ÂŽR‚ð‘I‚ñ‚Å 1 ` ŽRƒTƒCƒY ‚Ì”CˆÓŒÂŽæ‚蜂¯‚é > 42 - ÅŒã‚ÌŽR‚ðŽæ‚Á‚½•û‚ªŸ‚¿iŽR‚ª‚È‚‚È‚Á‚đłŽ肪–³‚‚È‚Á‚½•û‚ª•‰‚¯j > 43 > 44 *n ‚ðƒTƒCƒY n ‚ÌŽR‚ª1ŒÂ‚¾‚¯‚Ì nim ‚Æ‚·‚é > 45 - *0 ‚Í•‰‚¯ > 46 - *n ‚ÍŸ‚¿ n>=1 > 47 > 48 ƒQ[ƒ€ > 49 - ó‘Ô G ‚©‚ç‘Å‚Ä‚éŽè‚Å‘JˆÚ‚·‚éæ‚ð G1, ..., Gk ‚Æ‚·‚é > 50 G = {G1, ..., Gk} ‚Æ‘‚ > 51 > 52 “™‰¿ > 53 - “ñ‚‚̃Q[ƒ€G, F‚ª“™‰¿‚È‚±‚Æ‚ð G ß F ‚Æ‘‚ > 54 “™‰¿‚Æ‚¢‚¤‚Ì‚ÍŸ‚¿•‰‚¯ˆê’v‚æ‚è‚à‚Á‚Æ‹‚¢B“ñ‚‚̃Q[ƒ€‚̘a‚ðŽæ‚é‚Æ•K”s‚ɂȂ邱‚Æ > 55 *n ‚Æ *n ‚Í“¯ƒTƒCƒY‚ÌŽR2‚‚Ìnim‚Í•K”s‚È‚Ì‚Å“™‰¿B“¯ƒTƒCƒY‚Å‚È‚¯‚ê‚ΕKŸ‚È‚Ì‚Å“™‰¿‚Å‚È‚¢ > 56 > 57 ’è— > 58 - G1ß*n1, ..., Gkß*nk ‚È‚ç‚ÎAG={G1, ..., Gk} ß *mex(n1,...,nk) > 59 where mex(S) = min(nat \setminus S) > 60 ‹A”[“I‚ÉA‘S‚ẴQ[ƒ€‚Í‚È‚ñ‚ç‚©‚Ì *n ‚Æ“™‰¿ > 61 > 62 “K—p > 63 > 64 "#max == 1 ‚È‚ç•KŸ" > 65 [X,...] = { [X-1,...], [X-2,...], ..., [1, ...], [...] } > 66 ‚Å > 67 [X-1,...] = { [X-2,...], ..., [1, ...], [...] } > 68 ‚È‚Ì‚Å > 69 [X,...] ‚Ì nim ’l‚Í mex(mex{[X-2,...], ..., [1, ...], [...]}, [X-2,...], ... > 70 ‚ɂȂ邪A‚±‚ê‚Ímex2‰ñ‚ª‚¯‚È‚Ì‚Å *0 ‚Å‚Í‚È‚¢B‚æ‚Á‚Ä•KŸ > 71 "#max == 2 ‚È‚ç•K”s" > 72 ‚Ç‚¤‚â‚Á‚Ä‚à‘ŠŽè‚É#max==1‚Å“n‚Á‚Ä‚µ‚Ü‚¤ > 73 "#max == 3 ‚È‚ç•KŸ" > 74 ‚Ç‚¤‚â‚Á‚Ä‚à‘ŠŽè‚É#max==2‚Å“n‚é > 75 ... > 76 > 77 */ > 78 LL maxI = max_element(pieces.begin(), pieces.end()) - pieces.beg > 79 LL maxP = pieces[maxI]; > 80 if( count(pieces.begin(), pieces.end(), maxP) == 1 ) > 81 { > 82 pieces[maxI] = 0; > 83 LL second = *max_element(pieces.begin(), pieces.end()); > 84 > 85 // if there's odd number of the second largests, make it > 86 if( count(pieces.begin(), pieces.end(), second)%2 == 1 ) > 87 { > 88 stringstream sout; > 89 sout << "CAKE " << maxI << " PIECES " << (maxP - > 90 return sout.str(); > 91 } > 92 // otherwise, leave it as is. choose the lex-first move > 93 else > 94 { > 95 LL eat = maxP - second + 1; > 96 > 97 // d : lexicographically first number more than > 98 LL d = 1; > 99 while( !(eat <= d) ) > 100 d *= 10; > 101 if( d <= maxP ) // if edible > 102 eat = d; > 103 stringstream sout; > 104 sout << "CAKE " << maxI << " PIECES " << eat; > 105 return sout.str(); > 106 } > 107 } > 108 else > 109 { > 110 string ans = "Z"; > 111 for(LL i=0; i<pieces.size(); ++i) > 112 if( pieces[i] == maxP ) > 113 { > 114 stringstream sout; > 115 sout << "CAKE " << i << " PIECES 1"; > 116 ans = min(ans, sout.str()); > 117 } > 118 return ans; > 119 } > 120 } > 121 }; > 122 > 123 // BEGIN CUT HERE > 124 #include <ctime> > 125 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 126 > 127 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 128 int verify_case(const string &Expected, const string &Received) { if (Expected = > 129 > 130 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 131 char Test_(...); > 132 int Test_(Case_<0>) { > 133 int pieces_[] = {47}; > 134 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 135 string RetVal = "CAKE 0 PIECES 47"; > 136 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 137 int Test_(Case_<1>) { > 138 int pieces_[] = {3,3}; > 139 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 140 string RetVal = "CAKE 0 PIECES 1"; > 141 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 142 int Test_(Case_<2>) { > 143 int pieces_[] = {3,5}; > 144 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 145 string RetVal = "CAKE 1 PIECES 2"; > 146 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 147 int Test_(Case_<3>) { > 148 int pieces_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1}; > 149 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 150 string RetVal = "CAKE 0 PIECES 1"; > 151 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 152 int Test_(Case_<4>) { > 153 int pieces_[] = {3,3,112}; > 154 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 155 string RetVal = "CAKE 2 PIECES 110"; > 156 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 157 int Test_(Case_<5>) { > 158 int pieces_[] = {3,3,1}; > 159 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 160 string RetVal = "CAKE 0 PIECES 1"; > 161 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 162 int Test_(Case_<6>) { > 163 int pieces_[] = {4,7,4,7,4,7,4,7,47,47,47,47}; > 164 vector <int> pieces(pieces_, pieces_+sizeof(pieces_)/sizeof(*pieces_)) > 165 string RetVal = "CAKE 10 PIECES 1"; > 166 return verify_case(RetVal, CakeParty().makeMove(pieces)); } > 167 > 168 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 169 template<> void Run_<-1>() {} > 170 int main() { Run_<0>(); } > 171 // END CUT HERE > 172
Added SRM/339/1A.cpp version [85245486553aa92b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class BusTrip > 19 { > 20 public: > 21 int returnTime(int, vector <string> buses) > 22 { > 23 int t=-1, v=0; > 24 do > 25 onestep( t, v, buses ); > 26 while( v ); > 27 return t; > 28 } > 29 > 30 void onestep(int& t, int& v, vector<string>& buses) > 31 { > 32 int mt=1001, mv=0; > 33 for(int j=0; j<buses.size(); ++j) > 34 { > 35 stringstream sin(buses[j]); > 36 vector<int> vs; > 37 for(int x; sin>>x;) > 38 vs.push_back(x); > 39 > 40 int loop_time = 0; > 41 for(int i=0; i<vs.size(); ++i) > 42 loop_time += abs(vs[(i+1)%vs.size()] - vs[i]); > 43 > 44 int rem = 0; > 45 for(int i=0; i<vs.size(); ++i) { > 46 if( v==vs[i] ) > 47 { > 48 int nt = rem; > 49 while( t >= nt ) > 50 nt += loop_time; > 51 if( nt < mt ) { > 52 mt = nt; > 53 mv = vs[(i+1)%vs.size()]; > 54 } > 55 } > 56 rem += abs(vs[(i+1)%vs.size()] - vs[i]); > 57 } > 58 } > 59 > 60 t = mt + abs(mv-v); > 61 v = mv; > 62 if( t > 1000 ) > 63 t=-1, v=0; > 64 } > 65 > 66 // BEGIN CUT HERE > 67 public: > 68 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 69 private: > 70 template <typename T> string print_array(const vector<T> &V) { ostringst > 71 void verify_case(int Case, const int &Expected, const int &Received) { c > 72 void test_case_0() { int Arg0 = 3; string Arr1[] = {"0 1 2"}; vector <st > 73 void test_case_1() { int Arg0 = 51; string Arr1[] = {"0 5 10 15 20 25 30 > 74 void test_case_2() { int Arg0 = 3; string Arr1[] = {"0 1 2", "2 1 0"}; v > 75 void test_case_3() { int Arg0 = 5; string Arr1[] = {"0 1 2 3 4", "3 1 2 > 76 void test_case_4() { int Arg0 = 25; string Arr1[] = {"24 14 9 7 2", "21 > 77 "14 5 17 9 23 7 16 22 10 4 6", "19 8 1 9 24 3 5 22 16 7 6 4 10 23 17 0 13 15", > 78 "2 16 10 13 14 1 11 20 0 24 22 23 19 4 18", "19 15 18 0", "15 9 22 5 20 8 23 14 > 79 "2 6 19 3 21 10 20 22 24 13 16 15 8 18 17 14 5", "19 10 1 7 5 11 21 8 14 0 17 2 > 80 void test_case_5() { int Arg0 = 100; string Arr1[] = {"0 10 30 45 60 46 > 81 > 82 // END CUT HERE > 83 }; > 84 // BEGIN CUT HERE > 85 int main() { BusTrip().run_test(-1); } > 86 // END CUT HERE
Added SRM/339/1B.cpp version [f3f43873cef11bb1]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class TestBettingStrategy > 19 { > 20 public: > 21 map<int, double> memo; > 22 double winProbability(int initSum, int goalSum, int rounds, int prob) > 23 { > 24 memo.clear(); > 25 return rec(initSum, 1, rounds, goalSum, prob/100.0); > 26 } > 27 > 28 double rec(int cur, int bet, int rest, int goal, double pWin) > 29 { > 30 if( cur >= goal ) return 1.0; > 31 if( cur < bet ) return 0.0; > 32 if( rest == 0 ) return 0.0; > 33 > 34 int key = rest*2000*2000 + cur*2000 + bet; > 35 if( memo.count(key) ) > 36 return memo[key]; > 37 return memo[key] > 38 = pWin *rec(cur+bet, 1, rest-1, goal, pWin) > 39 + (1-pWin)*rec(cur-bet, bet*2, rest-1, goal, pWin); > 40 } > 41 > 42 // BEGIN CUT HERE > 43 public: > 44 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 45 private: > 46 template <typename T> string print_array(const vector<T> &V) { ostringst > 47 void verify_case(int Case, const double &Expected, const double &Receive > 48 void test_case_0() { int Arg0 = 10; int Arg1 = 11; int Arg2 = 4; int Arg > 49 void test_case_1() { int Arg0 = 10; int Arg1 = 20; int Arg2 = 20; int Ar > 50 void test_case_2() { int Arg0 = 10; int Arg1 = 20; int Arg2 = 10; int Ar > 51 void test_case_3() { int Arg0 = 24; int Arg1 = 38; int Arg2 = 24; int Ar > 52 > 53 // END CUT HERE > 54 }; > 55 // BEGIN CUT HERE > 56 int main() { TestBettingStrategy().run_test(-1); } > 57 // END CUT HERE
Added SRM/339/1C.cpp version [3ecdbbbf9c0cd8ea]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class RPSTournament > 19 { > 20 public: > 21 int greatestSeed(int rounds, int C) > 22 { > 23 int L=1, R=(1<<rounds)+1; > 24 while( R-L > 1) { > 25 int M = (L+R)/2; > 26 (can_win(M, rounds, C) ? L : R) = M; > 27 } > 28 return L; > 29 } > 30 > 31 bool can_win(int X, int R, int C) > 32 { > 33 queue< pair<int,int> > Q; > 34 Q.push( make_pair(R, X) ); > 35 > 36 set<int> unused; > 37 for(int n=0; n<=(1<<R); ++n) > 38 if( n!=X ) > 39 unused.insert(n); > 40 > 41 while( !Q.empty() ) { > 42 int r=Q.front().first; > 43 int x=Q.front().second; > 44 Q.pop(); > 45 > 46 int xl = x-(int)sqrt((double)C*x); > 47 set<int>::iterator it = unused.lower_bound(xl); > 48 if( it==unused.end() ) > 49 return false; > 50 if(r > 1) { > 51 Q.push( make_pair(r-1,max(*it,x)) ); > 52 Q.push( make_pair(r-1,min(*it,x)) ); > 53 } > 54 unused.erase(it); > 55 } > 56 return true; > 57 } > 58 > 59 // BEGIN CUT HERE > 60 public: > 61 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 62 private: > 63 template <typename T> string print_array(const vector<T> &V) { ostringst > 64 void verify_case(int Case, const int &Expected, const int &Received) { c > 65 void test_case_0() { int Arg0 = 2; int Arg1 = 0; int Arg2 = 1; verify_ca > 66 void test_case_1() { int Arg0 = 3; int Arg1 = 1; int Arg2 = 6; verify_ca > 67 void test_case_2() { int Arg0 = 4; int Arg1 = 1; int Arg2 = 9; verify_ca > 68 void test_case_3() { int Arg0 = 7; int Arg1 = 3; int Arg2 = 50; verify_c > 69 void test_case_4() { int Arg0 = 15; int Arg1 = 180; int Arg2 = 9755; ver > 70 > 71 // END CUT HERE > 72 }; > 73 // BEGIN CUT HERE > 74 int main() { RPSTournament().run_test(-1); } > 75 // END CUT HERE
Added SRM/340-U/1A.cpp version [5df1a0f8d5879ebe]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class ProblemsToSolve > 19 { > 20 public: > 21 int minNumber(vector <int> pre, int variety) > 22 { > 23 int ans = pre.size(); > 24 for(int i=0; i<pre.size(); ++i) > 25 for(int j=i+1; j<pre.size(); ++j) > 26 if( abs(pre[i]-pre[j]) >= variety ) > 27 ans = min(ans, solve(min(i,j), max(i,j)) > 28 return ans; > 29 } > 30 > 31 int solve(int S, int D) > 32 { > 33 return (S+1)/2+1 + (D-S+1)/2; > 34 } > 35 > 36 // BEGIN CUT HERE > 37 public: > 38 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 39 private: > 40 template <typename T> string print_array(const vector<T> &V) { ostringst > 41 void verify_case(int Case, const int &Expected, const int &Received) { c > 42 void test_case_0() { int Arr0[] = {1, 2, 3}; vector <int> Arg0(Arr0, Arr > 43 void test_case_1() { int Arr0[] = {1, 2, 3, 4, 5}; vector <int> Arg0(Arr > 44 void test_case_2() { int Arr0[] = {10, 1, 12, 101}; vector <int> Arg0(Ar > 45 void test_case_3() { int Arr0[] = {10, 1}; vector <int> Arg0(Arr0, Arr0 > 46 void test_case_4() { int Arr0[] = {6, 2, 6, 2, 6, 3, 3, 3, 7}; vector <i > 47 > 48 // END CUT HERE > 49 }; > 50 // BEGIN CUT HERE > 51 int main() { ProblemsToSolve().run_test(-1); } > 52 // END CUT HERE
Added SRM/340-U/1B.cpp version [bcaad1def4f8a2ec]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class CsCourses > 19 { > 20 public: > 21 vector <int> getOrder(vector <int> theoreticalValue, vector <int> practi > 22 { > 23 typedef pair<int,int> State; > 24 > 25 vector<State> Q; > 26 Q.push_back( State(0,0) ); > 27 map<State, State> prev; > 28 map<State, int> lect; > 29 lect[State(0,0)] = -1; > 30 > 31 for(int Month=0; !Q.empty(); ++Month) > 32 { > 33 vector<State> Q2; > 34 for(int i=0; i<Q.size(); ++i) > 35 { > 36 State s = Q[i]; > 37 int th=s.first, pr=s.second; > 38 if( min(th,pr) >= skillBound ) > 39 { > 40 vector<int> ans; > 41 for(State p=s; lect[p]>=0; p=prev[p]) > 42 ans.push_back(lect[p]); > 43 reverse(ans.begin(), ans.end()); > 44 return ans; > 45 } > 46 > 47 for(int k=0; k<theoreticalValue.size(); ++k) > 48 if( th >= theoreticalValue[k]-1 > 49 && pr >= practicalValue[k]-1 > 50 && Month < expire[k] ) > 51 { > 52 State s2( max(th,theoreticalValu > 53 if( !lect.count(s2) ) > 54 Q2.push_back(s2), prev[s > 55 } > 56 } > 57 Q.swap(Q2); > 58 } > 59 > 60 return vector<int>(); > 61 } > 62 > 63 // BEGIN CUT HERE > 64 public: > 65 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 66 private: > 67 template <typename T> string print_array(const vector<T> &V) { ostringst > 68 void verify_case(int Case, const vector <int> &Expected, const vector <i > 69 void test_case_0() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (s > 70 void test_case_1() { int Arr0[] = {1, 2, 1}; vector <int> Arg0(Arr0, Arr > 71 void test_case_2() { int Arr0[] = {1, 2, 1}; vector <int> Arg0(Arr0, Arr > 72 void test_case_3() { int Arr0[] = {1, 2, 1}; vector <int> Arg0(Arr0, Arr > 73 void test_case_4() { int Arr0[] = {1, 2, 3, 4, 5, 6, 7}; vector <int> Ar > 74 void test_case_5() { int Arr0[] = {0, 1, 2, 2, 1}; vector <int> Arg0(Arr > 75 > 76 // END CUT HERE > 77 }; > 78 // BEGIN CUT HERE > 79 int main() { CsCourses().run_test(-1); } > 80 // END CUT HERE
Added SRM/341/1A.cpp version [1da7b34c9052097a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class KLastNonZeroDigits > 19 { > 20 public: > 21 string getKDigits(int N, int K) > 22 { > 23 // 20! = 20*..*11*10! < 20*..*11*2^22 > 24 // < 16^10 * 2^22 > 25 // = 2^62 > 26 > 27 // 10^K > 28 LL Z = 1; > 29 for(int i=0; i<K; ++i) > 30 Z *= 10; > 31 > 32 // N! > 33 LL x = 1; > 34 for(LL n=2; n<=N; ++n) > 35 x *= n; > 36 while(x%10==0) > 37 x /= 10; > 38 > 39 // Output > 40 stringstream sout; > 41 if( x < Z ) > 42 sout << x; > 43 else > 44 sout << setw(K) << setfill('0') << x%Z; > 45 return sout.str(); > 46 } > 47 > 48 // BEGIN CUT HERE > 49 public: > 50 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 51 private: > 52 template <typename T> string print_array(const vector<T> &V) { ostringst > 53 void verify_case(int Case, const string &Expected, const string &Receive > 54 void test_case_0() { int Arg0 = 10; int Arg1 = 3; string Arg2 = "288"; v > 55 void test_case_1() { int Arg0 = 6; int Arg1 = 1; string Arg2 = "2"; veri > 56 void test_case_2() { int Arg0 = 6; int Arg1 = 3; string Arg2 = "72"; ver > 57 void test_case_3() { int Arg0 = 7; int Arg1 = 2; string Arg2 = "04"; ver > 58 void test_case_4() { int Arg0 = 20; int Arg1 = 9; string Arg2 = "2008176 > 59 void test_case_5() { int Arg0 = 1; int Arg1 = 1; string Arg2 = "1"; veri > 60 > 61 // END CUT HERE > 62 }; > 63 // BEGIN CUT HERE > 64 int main() { KLastNonZeroDigits().run_test(-1); } > 65 // END CUT HERE
Added SRM/341/1B.cpp version [bcf3463f5c537591]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 int dy[] = {-1,1, 0,0,-1,-1, 1,1}; > 19 int dx[] = { 0,0,-1,1,-1, 1,-1,1}; > 20 > 21 class LandAndSea > 22 { > 23 public: > 24 vector<int> howManyIslands(vector<string> seaMap) > 25 { > 26 const int Y = seaMap.size(); > 27 const int X = seaMap[0].size(); > 28 > 29 // paint > 30 vector< vector<int> > idMap(Y, vector<int>(X, -1)); > 31 int nIsland = 0; > 32 for(int y=0; y<Y; ++y) > 33 for(int x=0; x<X; ++x) > 34 if( seaMap[y][x]=='x' && idMap[y][x]==-1 ) > 35 { > 36 int id = (idMap[y][x] = nIsland++); > 37 stack< pair<int,int> > S; > 38 S.push( make_pair(y,x) ); > 39 while( !S.empty() ) { > 40 pair<int,int> p = S.top(); S.pop(); > 41 for(int i=0; i<8; ++i) { > 42 int yy = p.first +dy[i]; > 43 int xx = p.second+dx[i]; > 44 if( 0<=yy && yy<Y && 0<=xx && xx > 45 if( seaMap[yy][xx]=='x' > 46 idMap[yy][xx] = > 47 } > 48 } > 49 } > 50 > 51 // I is contained by J > 52 vector<int> refered(nIsland); > 53 vector<int> ct(nIsland, -1); > 54 > 55 set<int> outer_world; > 56 while( outer_world.size() < nIsland ) > 57 { > 58 set<int> outer2; > 59 for(int I=0; I<nIsland; ++I) > 60 if( !outer_world.count(I) ) > 61 { > 62 stack< pair<int,int> > S; > 63 set< pair<int,int> > vis; > 64 for(int y=0; y<Y; ++y) > 65 for(int x=0; x<X; ++x) > 66 if( idMap[y][x] == I ) > 67 S.push( make_pai > 68 > 69 int sID = -1; > 70 while( !S.empty() ) { > 71 pair<int,int> p = S.top(); S.po > 72 for(int i=0; i<4; ++i) { > 73 int yy = p.first+dy[i]; > 74 int xx = p.second+dx[i]; > 75 if( !(0<=yy && yy<Y && 0 > 76 goto notContaine > 77 int ssID = idMap[yy][xx] > 78 if( outer_world.count(ss > 79 {sID=ssID; goto > 80 > 81 pair<int,int> pp(yy,xx); > 82 if( !vis.count(pp) && se > 83 S.push(pp), vis. > 84 } > 85 } > 86 continue; > 87 notContained: > 88 outer2.insert(I); > 89 if(sID != -1) { > 90 ct[I] = sID; > 91 refered[sID]++; > 92 } > 93 } > 94 outer_world.insert(outer2.begin(), outer2.end()); > 95 } > 96 > 97 // topological sort > 98 vector<int> level(nIsland, -1); > 99 for(int L=0;;++L) > 100 { > 101 vector<int> victim; > 102 > 103 for(int I=0; I<nIsland; ++I) > 104 if( level[I] == -1 && refered[I] == 0 ) > 105 level[I] = L, victim.push_back(I); > 106 > 107 if( victim.size() == 0 ) > 108 break; > 109 > 110 for(int i=0; i<victim.size(); ++i) > 111 { > 112 int J = ct[victim[i]]; > 113 if( J != -1 ) > 114 refered[J]--; > 115 } > 116 } > 117 > 118 vector<int> ans; > 119 for(int i=0; i<level.size(); ++i) { > 120 if( level[i]+1 > ans.size() ) > 121 ans.resize( level[i]+1 ); > 122 ans[level[i]]++; > 123 } > 124 return ans; > 125 } > 126 > 127 // BEGIN CUT HERE > 128 public: > 129 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 130 private: > 131 template <typename T> string print_array(const vector<T> &V) { ostringst > 132 void verify_case(int Case, const vector <int> &Expected, const vector <i > 133 void test_case_0() { string Arr0[] = {"x"}; vector <string> Arg0(Arr0, A > 134 void test_case_1() { string Arr0[] = { > 135 "xxxxx", > 136 "x...x", > 137 "x.x.x", > 138 "x...x", > 139 "xxxxx" > 140 }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1 > 141 void test_case_2() { string Arr0[] = { > 142 "xxxxx", > 143 "x...x", > 144 "x.x.x", > 145 "x...x", > 146 "xxxxx", > 147 "xxxxx", > 148 "x...x", > 149 "x.x.x", > 150 "x...x", > 151 "xxxxx" > 152 }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1 > 153 void test_case_3() { string Arr0[] = { > 154 "..", > 155 ".." > 156 }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); vector < > 157 void test_case_4() { string Arr0[] = { > 158 "............", > 159 ".......xxxx.", > 160 "..xxx.x...x.", > 161 "..x..x..x.x.", > 162 "..x.x.x...x.", > 163 "..xx...xxx.." > 164 }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1 > 165 > 166 // END CUT HERE > 167 }; > 168 // BEGIN CUT HERE > 169 int main() { LandAndSea().run_test(-1); } > 170 // END CUT HERE
Added SRM/341/1C.cpp version [655c27d7a69723ce]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 vector<LL> vMul( const vector< vector<LL> >& A, const vector<LL>& B ) > 20 { > 21 const int n = A.size(); > 22 > 23 vector<LL> C(n); > 24 for(int i=0; i<n; ++i) > 25 for(int j=0; j<n; ++j) > 26 C[i] += A[i][j] * B[j]; > 27 return C; > 28 } > 29 > 30 vector< vector<LL> > mMul( const vector< vector<LL> >& A, const vector< vector<L > 31 { > 32 const int n = A.size(); > 33 > 34 vector< vector<LL> > C(n, vector<LL>(n)); > 35 for(int i=0; i<n; ++i) > 36 for(int j=0; j<n; ++j) { > 37 LL Cij = 0; > 38 for(int k=0; k<n; ++k) > 39 Cij += A[i][k] * B[k][j]; > 40 C[i][j] = Cij; > 41 } > 42 return C; > 43 } > 44 > 45 vector< vector<LL> > mPow( vector< vector<LL> > M, int t ) > 46 { > 47 if( t == 0 ) { > 48 vector< vector<LL> > R(M.size(), vector<LL>(M.size())); > 49 for(int i=0; i<R.size(); ++i) R[i][i] = 1; > 50 return R; > 51 } > 52 vector< vector<LL> > R; > 53 for(; t; t>>=1, M=mMul(M,M)) > 54 if( t&1 ) > 55 R = (R.empty() ? M : mMul(R,M)); > 56 return R; > 57 } > 58 > 59 class ValidPlates > 60 { > 61 public: > 62 map<pair<int,int>, LL> memo; > 63 > 64 string getPlate(vector <string> profane, int seqno) > 65 { > 66 // parse > 67 bool proh[10][10] = {}; > 68 for(int i=0; i<profane.size(); ++i) { > 69 stringstream sin(profane[i]); > 70 for(int n; sin>>n; ) > 71 proh[n/10][n%10] = true; > 72 } > 73 > 74 // matrix > 75 vector<LL> N(11, 1); N[10] = 0; > 76 vector< vector<LL> > M(11, vector<LL>(11)); > 77 for(int i=0; i<10; ++i) > 78 for(int j=0; j<10; ++j) > 79 if( !proh[i][j] ) > 80 M[i][j] = 1; > 81 for(int j=1; j<11; ++j) > 82 M[10][j] = 1; > 83 > 84 // binary search... > 85 LL L=-1, R=0; > 86 for(LL atprev=-1;; R=R*2+1) > 87 { > 88 vector< vector<LL> > Mt = mPow(M, R); > 89 vector<LL> Nt = vMul(Mt, N); > 90 LL at = accumulate(Nt.begin()+1, Nt.en > 91 if( atprev == at ) > 92 return ""; > 93 atprev = at; > 94 if( seqno <= at ) > 95 break; > 96 } > 97 while(R-L>1) > 98 { > 99 LL C=(L+R)/2; > 100 vector< vector<LL> > Mt = mPow(M, C); > 101 vector<LL> Nt = vMul(Mt, N); > 102 (seqno <= accumulate(Nt.begin()+1, Nt.end(), 0LL) ? R : > 103 } > 104 LL d = R+1; // #digits > 105 seqno -= vMul(mPow(M,R), N)[10]; > 106 > 107 string buf; > 108 int prev = -1; > 109 for(int i=0; i<(d>50 ? 47 : d); ++i) > 110 if( prev == -1 ) > 111 { > 112 vector<LL> Nt = vMul(mPow(M, d-1-i), N); > 113 LL n = 0; > 114 for(int a=1; a<10; ++a) > 115 { > 116 n += Nt[a]; > 117 if( seqno <= n ) > 118 {buf += char('0'+a); prev=a; seq > 119 } > 120 } > 121 else > 122 { > 123 vector<LL> Nt = vMul(mPow(M, d-1-i), N); > 124 LL n = 0; > 125 for(int a=0; a<10; ++a) if( !proh[prev][a] ) > 126 { > 127 n += Nt[a]; > 128 if( seqno <= n ) > 129 {buf += char('0'+a); prev=a; seq > 130 } > 131 } > 132 return (d>50 ? buf+"..." : buf); > 133 } > 134 > 135 // BEGIN CUT HERE > 136 public: > 137 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 138 private: > 139 template <typename T> string print_array(const vector<T> &V) { ostringst > 140 void verify_case(int Case, const string &Expected, const string &Receive > 141 void test_case_0() { vector <string> Arg0; int Arg1 = 1000; string Arg2 > 142 void test_case_1() { string Arr0[] = {"10"}; vector <string> Arg0(Arr0, > 143 void test_case_2() { string Arr0[] = {"10"}; vector <string> Arg0(Arr0, > 144 void test_case_3() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09 11 > 145 "18 19 22 23 24 25 26 27 28 29 33 34 35 36 37 38 39", > 146 "44 45 46 47 48 49 55 56 57 58 59 66 67 68 69 77 78", > 147 "79 88 89 99 99 99 99 99"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / s > 148 void test_case_4() { string Arr0[] = {"00 01 02 03 04 05 07 08 09", > 149 "10 11 12 13 14 15 17 18 19", > 150 "20 21 22 24 25 26 27 28 29", > 151 "30 31 32 33 34 36 37 38 39", > 152 "41 43 45 46 48", > 153 "52 53 54 55 56 58 59", > 154 "60 61 63 64 66 67 68 69", > 155 "70 72 73 74 75 76 77 78", > 156 "80 81 82 83 84 86 87 88 89", > 157 "90 91 92 94 95 96 97 98"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / s > 158 void test_case_5() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09", > 159 "10 11 12 13 14 16 17 19", > 160 "20 21 22 23 24 25 26 27 28 29", > 161 "30 31 32 33 34 35 36 38 39", > 162 "41 42 43 44 45 46 49", > 163 "50 52 53 54 55 57 58 59", > 164 "60 61 62 63 64 65 66 67 68 69", > 165 "70 72 73 74 75 76 77 78 79", > 166 "80 81 82 83 84 85 86 87 88 89", > 167 "90 91 92 93 94 95 98 99"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / s > 168 void test_case_6() { string Arr0[] = {"00 01 02 03 04 05 06 07 08 09", " > 169 vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = > 170 > 171 // END CUT HERE > 172 }; > 173 // BEGIN CUT HERE > 174 int main() { ValidPlates().run_test(-1); } > 175 // END CUT HERE
Added SRM/342/1A.cpp version [d1f9e48a4c45888d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 int get_and_go( const string& a, int& i ) > 19 { > 20 int dict[] = > 21 {'a','b','k','d','e','g','h','i','l','m','n','n'/*g*/,'o','p','r > 22 for(int k=0;; ++k) > 23 if( dict[k] == a[i] ) > 24 if( a[i]=='n' && i+1<a.size() && a[i+1]=='g' ) > 25 {i+=2; return k+1;} > 26 else > 27 {i++; return k;} > 28 } > 29 > 30 bool tgcmp( const string& a, const string& b ) > 31 { > 32 int i=0, j=0; > 33 for(;;) > 34 { > 35 if( i==a.size() && j==b.size() ) > 36 return false; > 37 if( i==a.size() ) > 38 return true; > 39 if( j==b.size() ) > 40 return false; > 41 > 42 int ai = get_and_go(a, i); > 43 int bj = get_and_go(b, j); > 44 if( ai != bj ) > 45 return ai < bj; > 46 } > 47 } > 48 > 49 class TagalogDictionary > 50 { > 51 public: > 52 vector <string> sortWords(vector <string> words) > 53 { > 54 sort( words.begin(), words.end(), &tgcmp ); > 55 return words; > 56 } > 57 > 58 // BEGIN CUT HERE > 59 public: > 60 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 61 private: > 62 template <typename T> string print_array(const vector<T> &V) { ostringst > 63 void verify_case(int Case, const vector <string> &Expected, const vector > 64 void test_case_0() { string Arr0[] = {"abakada","alpabet","tagalog","ako > 65 void test_case_1() { string Arr0[] = {"ang","ano","anim","alak","alam"," > 66 void test_case_2() { string Arr0[] = {"siya","niya","kaniya","ikaw","ito > 67 void test_case_3() { string Arr0[] = {"kaba","baka","naba","ngipin","nip > 68 void test_case_4() { string Arr0[] = {"knilngiggnngginggn","ingkigningg" > 69 void test_case_5() { string Arr0[] = {"silangang", "baka", "bada", "sila > 70 > 71 // END CUT HERE > 72 }; > 73 // BEGIN CUT HERE > 74 int main() { > 75 TagalogDictionary().run_test(-1); > 76 } > 77 // END CUT HERE
Added SRM/342/1B.cpp version [7881a6a8a8d93b4d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 static const int MODNUM = 1000000007; > 19 > 20 // make [i,j) by k[z..$] > 21 LL dp[31][31][51][51]; > 22 > 23 class ReverseResources > 24 { > 25 public: > 26 int findDecompositions(string str, vector <string> resources) > 27 { > 28 memset(dp, 0xff, sizeof(dp)); > 29 > 30 LL sum = 0; > 31 for(int k=0; k<resources.size(); ++k) > 32 sum = (sum + rec(0, str.size(), k, 0, str, resources, dp > 33 return sum; > 34 } > 35 > 36 LL rec(int i, int j, int k, int z, string& s, vector<string>& rs, LL dp[ > 37 { > 38 if( dp[i][j][k][z] >= 0 ) > 39 return dp[i][j][k][z]; > 40 > 41 string& r = rs[k]; > 42 if( z==r.size() ) > 43 return dp[i][j][k][z] = (i==j ? 1 : 0); > 44 > 45 if( r[z] == '%' ) > 46 { > 47 LL sum = 0; > 48 if( z+2 == r.size() ) > 49 { > 50 for(int kk=0; kk<rs.size(); ++kk) > 51 sum = (sum + rec(i, j, kk, 0, s, rs, dp) > 52 } > 53 else > 54 { > 55 for(int jj=i+1; jj<j; ++jj) > 56 { > 57 LL right = rec(jj, j, k, z+2, s, rs, dp) > 58 if( right ) { > 59 LL left = 0; > 60 for(int kk=0; kk<rs.size(); ++kk > 61 left = (left + rec(i, jj > 62 sum = (sum + left*right) % MODNU > 63 } > 64 } > 65 } > 66 return dp[i][j][k][z] = sum; > 67 } > 68 else > 69 { > 70 if( r[z]==s[i] && i+1<=j ) > 71 return dp[i][j][k][z] = rec(i+1,j,k,z+1,s,rs,dp) > 72 else > 73 return dp[i][j][k][z] = 0; > 74 } > 75 } > 76 > 77 // BEGIN CUT HERE > 78 public: > 79 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 80 private: > 81 template <typename T> string print_array(const vector<T> &V) { ostringst > 82 void verify_case(int Case, const int &Expected, const int &Received) { c > 83 void test_case_0() { string Arg0 = "Error in module foo, code 123."; str > 84 void test_case_1() { string Arg0 = "The fox jumped over the dog."; strin > 85 "lazy", > 86 "brown", > 87 "jump%s", > 88 "s", > 89 "ed", > 90 "The %s", > 91 "fox %s", > 92 "%s over %s", > 93 "the dog."}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0])) > 94 void test_case_2() { string Arg0 = "abcde"; string Arr1[] = {"%sc%s", "b > 95 void test_case_3() { string Arg0 = "abcde"; string Arr1[] = {"%se", "a%s > 96 void test_case_4() { string Arg0 = "aaaaa"; string Arr1[] = {"a","%s%s"} > 97 void test_case_5() { string Arg0 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; str > 98 "a%s%s","%saa","%sa%s","%s%sa","%s%s%s","aaaa", > 99 "aaa%s","aa%sa","aa%s%s","a%saa","a%sa%s","a%s%sa", > 100 "a%s%s%s","%saaa","%saa%s","%sa%sa","%sa%s%s", > 101 "%s%saa","%s%sa%s","%s%s%sa","%s%s%s%s","aaaaa", > 102 "aaaa%s","aaa%sa","aaa%s%s","aa%saa","aa%sa%s", > 103 "aa%s%sa","aa%s%s%s","a%saaa","a%saa%s","a%sa%sa", > 104 "a%sa%s%s","a%s%saa","a%s%sa%s","a%s%s%sa", > 105 "a%s%s%s%s","%saaaa","%saaa%s","%saa%sa","%saa%s%s"}; vector <string> Arg1(Arr1, > 106 void test_case_6() { string Arg0 = "aa"; string Arr1[] = {"a", "a", "%s% > 107 > 108 // END CUT HERE > 109 }; > 110 // BEGIN CUT HERE > 111 int main() { ReverseResources().run_test(-1); } > 112 // END CUT HERE
Added SRM/342/1C.cpp version [067a5c147f5030d5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 vector< vector<LL> > mMul( const vector< vector<LL> >& A, const vector< vector<L > 19 { > 20 const int n = A.size(); > 21 > 22 vector< vector<LL> > C(n, vector<LL>(n)); > 23 for(int i=0; i<n; ++i) > 24 for(int j=0; j<n; ++j) { > 25 LL Cij = 0; > 26 for(int k=0; k<n; ++k) > 27 Cij += A[i][k] * B[k][j]; > 28 C[i][j] = Cij % 1000003; > 29 } > 30 return C; > 31 } > 32 > 33 vector< vector<LL> > mPow( vector< vector<LL> > M, int t ) // t>0 > 34 { > 35 vector< vector<LL> > R; > 36 for(; t; t>>=1, M=mMul(M,M)) > 37 if( t&1 ) > 38 R = (R.empty() ? M : mMul(R,M)); > 39 return R; > 40 } > 41 > 42 class DrivingAround > 43 { > 44 public: > 45 int numberOfWays(vector <string> adj, int start, int finish, int time) > 46 { > 47 int n = adj.size()*5; > 48 vector< vector<LL> > M(n, vector<LL>(n)); > 49 for(int u=0; u<adj.size(); ++u) > 50 for(int d=0; d<4; ++d) > 51 M[u*5+d+1][u*5+d] = 1; > 52 > 53 for(int u=0; u<adj.size(); ++u) > 54 for(int v=0; v<adj.size(); ++v) > 55 if( adj[u][v] != '.' ) > 56 M[u*5][v*5+adj[u][v]-'0'-1] = 1; > 57 > 58 return int( mPow(M, time)[start*5][finish*5] ); > 59 } > 60 > 61 // BEGIN CUT HERE > 62 public: > 63 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 64 private: > 65 template <typename T> string print_array(const vector<T> &V) { ostringst > 66 void verify_case(int Case, const int &Expected, const int &Received) { c > 67 void test_case_0() { string Arr0[] = {".12", > 68 "2.1", > 69 "12."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); in > 70 void test_case_1() { string Arr0[] = {"....52....", > 71 "..5.......", > 72 "..........", > 73 ".......1..", > 74 "......42.2", > 75 "5...4.....", > 76 ".5...4...1", > 77 "......5...", > 78 ".3244.....", > 79 ".........."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0] > 80 void test_case_2() { string Arr0[] = {"...14....1", > 81 "......13..", > 82 ".2...4....", > 83 "....52.5..", > 84 "1.3..4....", > 85 ".3....35.5", > 86 "4......1.1", > 87 "..4.4.1.54", > 88 "....4.11.5", > 89 "31144.2.4."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0] > 90 void test_case_3() { string Arr0[] = {".555555555", "5.55555555", "55.55 > 91 vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = > 92 // END CUT HERE > 93 }; > 94 // BEGIN CUT HERE > 95 int main() { DrivingAround().run_test(-1); } > 96 // END CUT HERE
Added SRM/343/1A.cpp version [caafadae3df4cf52]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class CDPlayer > 19 { > 20 public: > 21 int isRandom(vector <string> songlist, int n) > 22 { > 23 string s; > 24 for(int i=0; i<songlist.size(); ++i) > 25 s += songlist[i]; > 26 > 27 for(int k=0; k<n; ++k) > 28 if( check(s, k, n) ) > 29 return k; > 30 return -1; > 31 } > 32 > 33 bool check(const string& s, int k, int n) > 34 { > 35 for(int i=0,e=k; i<s.size(); i=e,e+=n) > 36 if( !distinct(s, i, min<int>(e,s.size())) ) > 37 return false; > 38 return true; > 39 } > 40 > 41 bool distinct( const string& s, int i, int e ) > 42 { > 43 set<char> x; > 44 for(; i<e; ++i) > 45 if( x.count(s[i]) ) > 46 return false; > 47 else > 48 x.insert( s[i] ); > 49 return true; > 50 } > 51 > 52 > 53 // BEGIN CUT HERE > 54 public: > 55 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 56 private: > 57 template <typename T> string print_array(const vector<T> &V) { ostringst > 58 void verify_case(int Case, const int &Expected, const int &Received) { c > 59 void test_case_0() { string Arr0[] = {"BBAC"}; vector <string> Arg0(Arr0 > 60 void test_case_1() { string Arr0[] = {"BACAB", > 61 "BCA"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); in > 62 void test_case_2() { string Arr0[] = {"AAACBACBACBACBACBACBACB"}; vector > 63 void test_case_3() { string Arr0[] = {"ABCDEABDECBAECDEDACB"}; vector <s > 64 void test_case_4() { string Arr0[] = {"ABCABCABCABCABCABCABC", > 65 "ABCABCABCABCABCABCABC"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / siz > 66 void test_case_5() { string Arr0[] = {"AAAAAAAAAAAAAAAA", > 67 "AAAAAAAAAAAAAAAA", > 68 "AAAAAAAAAAAAAAAA", > 69 "AAAAAAAAAAAAAAAA", > 70 "AAAAAAAAAAAAAAAA", > 71 "AAAAAAAAAAAAAAAA", > 72 "AAAAAAAAAAAAAAAA", > 73 "AAAAAAAAAAAAAAAA", > 74 "AAAAAAAAAAAAAAAA", > 75 "AAAAAAAAAAAAAAAA"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(A > 76 void test_case_6() { string Arr0[] = {"ADEF"}; vector <string> Arg0(Arr0 > 77 > 78 // END CUT HERE > 79 }; > 80 // BEGIN CUT HERE > 81 int main() { > 82 CDPlayer().run_test(-1); > 83 } > 84 // END CUT HERE
Added SRM/343/1B.cpp version [718000c8b2d4687f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class MoneyGame > 19 { > 20 public: > 21 int V1, V2, V3; > 22 map<int, int> memo; > 23 int play(vector <int> lennysCoins, vector <int> fredsCoins, vector <int> > 24 { > 25 V1 = values[0]; > 26 V2 = values[1]; > 27 V3 = values[2]; > 28 memo.clear(); > 29 return rec( lennysCoins[0], lennysCoins[1], lennysCoins[2], > 30 fredsCoins[0], fredsCoins[1], fredsCoins[2], 0, 0 > 31 } > 32 > 33 int rec(int L1, int L2, int L3, int F1, int F2, int F3, int B1, int B2, > 34 { > 35 if( !L1 && !L2 && !L3 ) > 36 return -(V1*F1+V2*F2+V3*F3); > 37 int key = (L1<<20)|(L2<<16)|(L3<<12)|(F1<<8)|(F2<<4)|(F3<<0); > 38 if( memo.count(key) ) > 39 return memo[key]; > 40 > 41 int score = -0x7fffffff; > 42 if(L1) { > 43 for(int b1=0; b1<=B1 && V1*b1<V1; ++b1) > 44 for(int b2=0; b2<=B2 && V1*b1+V2*b2<V1; ++b2) > 45 for(int b3=0; b3<=B3 && V1*b1+V2*b2+V3*b3<V1; ++b3) > 46 score = max(score, -rec(F1, F2, F3, L1+b1-1, L2+ > 47 } > 48 if(L2) { > 49 for(int b1=0; b1<=B1 && V1*b1<V2; ++b1) > 50 for(int b2=0; b2<=B2 && V1*b1+V2*b2<V2; ++b2) > 51 for(int b3=0; b3<=B3 && V1*b1+V2*b2+V3*b3<V2; ++b3) > 52 score = max(score, -rec(F1, F2, F3, L1+b1, L2+b2 > 53 } > 54 if(L3) { > 55 for(int b1=0; b1<=B1 && V1*b1<V3; ++b1) > 56 for(int b2=0; b2<=B2 && V1*b1+V2*b2<V3; ++b2) > 57 for(int b3=0; b3<=B3 && V1*b1+V2*b2+V3*b3<V3; ++b3) > 58 score = max(score, -rec(F1, F2, F3, L1+b1, L2+b2 > 59 } > 60 return memo[key] = score; > 61 } > 62 > 63 > 64 // BEGIN CUT HERE > 65 public: > 66 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 67 private: > 68 template <typename T> string print_array(const vector<T> &V) { ostringst > 69 void verify_case(int Case, const int &Expected, const int &Received) { c > 70 void test_case_0() { int Arr0[] = {0,1,1}; vector <int> Arg0(Arr0, Arr0 > 71 void test_case_1() { int Arr0[] = {0,1,2}; vector <int> Arg0(Arr0, Arr0 > 72 void test_case_2() { int Arr0[] = {1,1,0}; vector <int> Arg0(Arr0, Arr0 > 73 void test_case_3() { int Arr0[] = {4,4,3}; vector <int> Arg0(Arr0, Arr0 > 74 void test_case_4() { int Arr0[] = {0,0,0}; vector <int> Arg0(Arr0, Arr0 > 75 void test_case_5() { int Arr0[] = {3,2,1}; vector <int> Arg0(Arr0, Arr0 > 76 > 77 // END CUT HERE > 78 }; > 79 // BEGIN CUT HERE > 80 int main() { > 81 MoneyGame().run_test(-1); > 82 } > 83 // END CUT HERE
Added SRM/343/1C.cpp version [efa6784811dbad8e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class RefactorableNumber > 19 { > 20 public: > 21 vector<int> P; > 22 > 23 int count(int low, int high) > 24 { > 25 P.clear(); > 26 vector<bool> isP(2000, true); > 27 for(int p=2; p<2000; ++p) > 28 if( isP[p] ) { > 29 P.push_back(p); > 30 for(int q=p*p; q<2000; q+=p) > 31 isP[q] = false; > 32 } > 33 > 34 int cnt = 0; > 35 for(int n=low; n<=high; ++n) > 36 if( ref(n) ) > 37 ++cnt; > 38 return cnt; > 39 } > 40 > 41 bool ref(int N) > 42 { > 43 int n = N; > 44 int x = 1; > 45 for(int i=0; i<P.size() && n>1; ++i) > 46 { > 47 int p = P[i]; > 48 if( p*p > n ) > 49 break; > 50 int c = 0; > 51 while(n%p==0) > 52 n/=p, ++c; > 53 x *= c+1; > 54 } > 55 if(n>1) > 56 x *= 2; > 57 return N%x == 0; > 58 } > 59 > 60 // BEGIN CUT HERE > 61 public: > 62 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 63 private: > 64 template <typename T> string print_array(const vector<T> &V) { ostringst > 65 void verify_case(int Case, const int &Expected, const int &Received) { c > 66 void test_case_0() { int Arg0 = 1; int Arg1 = 10; int Arg2 = 4; verify_c > 67 void test_case_1() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 12; verif > 68 void test_case_2() { int Arg0 = 25; int Arg1 = 35; int Arg2 = 0; verify_ > 69 void test_case_3() { int Arg0 = 123; int Arg1 = 4567; int Arg2 = 315; ve > 70 > 71 // END CUT HERE > 72 }; > 73 // BEGIN CUT HERE > 74 int main() { > 75 RefactorableNumber().run_test(-1); > 76 } > 77 // END CUT HERE
Added SRM/344/1A.cpp version [581a4d8a9088ed23]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class VolleyballTournament > 19 { > 20 public: > 21 string reconstructResults(int wonMatches, int lostMatches, int wonSets, > 22 { > 23 //0-3, 1-3, 2-3, 3-0, 3-1, 3-2 > 24 //Z0 Z1 Z2 W0 W1 W2 > 25 > 26 //Z0+ Z1 + Z2 == lostMatches > 27 //Z0*0 + Z1*1 + Z2*2 + 3*wonMatches == wonSets > 28 > 29 //W0+W1+W2 == wonMatches > 30 //W0*0+W1*1+W2*2+3*lostMatches == lostSets > 31 > 32 int a[6] = {-1}; > 33 for(int Z1=0; Z1<=lostMatches; ++Z1) > 34 { > 35 int Z2 = (wonSets - 3*wonMatches - Z1) / 2; > 36 int Z0 = lostMatches - Z1 - Z2; > 37 if( Z0<0 || Z2<0 || lostMatches<Z0 || lostMatches<Z2 ) c > 38 if( Z1+Z2*2+3*wonMatches != wonSets ) continue; > 39 > 40 for(int W1=0; W1<=wonMatches; ++W1) > 41 { > 42 int W2 = (lostSets - 3*lostMatches - W1) / 2; > 43 int W0 = wonMatches - W1 - W2; > 44 if( W0<0 || W2<0 || wonMatches<W0 || wonMatches< > 45 if( W1+W2*2+3*lostMatches != lostSets ) continue > 46 > 47 if( a[0]>=0 ) > 48 return "AMBIGUITY"; > 49 else > 50 a[0]=Z0, a[1]=Z1, a[2]=Z2, a[3]=W0, a[4] > 51 } > 52 } > 53 const char* s[6] = {"0-3", "1-3", "2-3", "3-0", "3-1", "3-2"}; > 54 > 55 string ans; > 56 for(int i=0; i<6; ++i) > 57 for(int j=0; j<a[i]; ++j) > 58 ans += s[i], ans += ","; > 59 ans.resize(ans.size()-1); > 60 return ans; > 61 } > 62 > 63 // BEGIN CUT HERE > 64 public: > 65 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 66 private: > 67 template <typename T> string print_array(const vector<T> &V) { ostringst > 68 void verify_case(int Case, const string &Expected, const string &Receive > 69 void test_case_0() { int Arg0 = 3; int Arg1 = 3; int Arg2 = 9; int Arg3 > 70 void test_case_1() { int Arg0 = 0; int Arg1 = 3; int Arg2 = 6; int Arg3 > 71 void test_case_2() { int Arg0 = 3; int Arg1 = 0; int Arg2 = 9; int Arg3 > 72 void test_case_3() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 4; int Arg3 > 73 > 74 // END CUT HERE > 75 }; > 76 // BEGIN CUT HERE > 77 int main() { > 78 VolleyballTournament().run_test(-1); > 79 } > 80 // END CUT HERE
Added SRM/344/1B.cpp version [be660a3b96dfe736]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class QuantumAlchemy > 19 { > 20 public: > 21 int minSteps(string initial, vector <string> reactions) > 22 { > 23 map<char, string> r; > 24 for(int i=0; i<reactions.size(); ++i) > 25 { > 26 int j = reactions[i].find('-'); > 27 r[reactions[i][j+2]] = reactions[i].substr(0, j); > 28 } > 29 > 30 map<char, int> g; > 31 for(int i=0; i<initial.size(); ++i) > 32 g[initial[i]]++; > 33 > 34 if( g['X'] ) > 35 return 0; > 36 set<char> stk; stk.insert('X'); > 37 return make('X', r, g, stk); > 38 } > 39 > 40 int make(char X, map<char,string>& r, map<char, int>& g, set<char>& stk) > 41 { > 42 if( !r.count(X) ) > 43 return -1; > 44 string s = r[X]; > 45 > 46 int rea = 1; > 47 for(int i=0; i<s.size(); ++i) > 48 if( g[s[i]] ) > 49 g[s[i]]--; > 50 else { > 51 if( stk.count(s[i]) ) > 52 return -1; > 53 stk.insert(s[i]); > 54 int subrea = make(s[i], r, g, stk); > 55 stk.erase(s[i]); > 56 if( subrea == -1 ) > 57 return -1; > 58 rea += subrea; > 59 } > 60 return rea; > 61 } > 62 > 63 // BEGIN CUT HERE > 64 public: > 65 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 66 private: > 67 template <typename T> string print_array(const vector<T> &V) { ostringst > 68 void verify_case(int Case, const int &Expected, const int &Received) { c > 69 void test_case_0() { string Arg0 = "ABCDE"; string Arr1[] = {"ABC->F", " > 70 void test_case_1() { string Arg0 = "AABBB"; string Arr1[] = {"BZ->Y", "Z > 71 void test_case_2() { string Arg0 = "AAABB"; string Arr1[] = {"BZ->Y", "Z > 72 void test_case_3() { string Arg0 = "AAXB"; string Arr1[] = {"BZ->Y", "ZY > 73 void test_case_4() { string Arg0 = "ABCDEFGHIJKLMNOPQRSTUVWYZABCDEFGHIJK > 74 > 75 // END CUT HERE > 76 }; > 77 // BEGIN CUT HERE > 78 int main() { > 79 QuantumAlchemy().run_test(-1); > 80 } > 81 // END CUT HERE
Added SRM/344/1C.cpp version [8458c6f3c3bb106e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class FairTournament > 19 { > 20 public: > 21 vector<string> memo; > 22 > 23 string add(const string& a, const string& b) > 24 { > 25 int n = max(a.size(), b.size()), carry=0; > 26 string c(n, '0'); > 27 for(int i=0; i<n; ++i) { > 28 int x = (a.size()<=i ? 0 : a[a.size()-1-i]-'0') > 29 + (b.size()<=i ? 0 : b[b.size()-1-i]-'0') + carry; > 30 c[n-1-i] = char('0'+x%10); > 31 carry = x/10; > 32 } > 33 if( carry ) c = char('0'+carry)+c; > 34 return c; > 35 } > 36 > 37 string countPermutations(int n, int k) > 38 { > 39 memo.clear(); > 40 memo.resize(n<<k+1+k, "X"); > 41 > 42 int pending = (1<<k+1) - 1; // 0b00111 (case k=2) > 43 return rec(pending, 0, n, k); > 44 } > 45 > 46 string rec(int pending, int i, int n, int k) > 47 { > 48 if( i == n ) > 49 return "1"; > 50 > 51 int key = (i<<k+1+k) | pending; > 52 if( memo[key]!="X" ) > 53 return memo[key]; > 54 > 55 string sum = ""; > 56 for(int m=1,j=k; m<=pending; m<<=1,j--) > 57 if( (pending & m) && i+j<n ) > 58 { > 59 int p2 = ((pending^m)<<1) | 1; > 60 if( p2 >= (1<<k+1+k) ) // 0b100000 > 61 continue; > 62 sum = add(sum, rec(p2, i+1, n, k)); > 63 } > 64 return memo[key]=sum; > 65 } > 66 > 67 // BEGIN CUT HERE > 68 public: > 69 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 70 private: > 71 template <typename T> string print_array(const vector<T> &V) { ostringst > 72 void verify_case(int Case, const string &Expected, const string &Receive > 73 void test_case_0() { int Arg0 = 3; int Arg1 = 1; string Arg2 = "3"; veri > 74 void test_case_1() { int Arg0 = 3; int Arg1 = 2; string Arg2 = "6"; veri > 75 void test_case_2() { int Arg0 = 10; int Arg1 = 3; string Arg2 = "19708"; > 76 void test_case_3() { int Arg0 = 100; int Arg1 = 1; string Arg2 = "573147 > 77 > 78 // END CUT HERE > 79 }; > 80 // BEGIN CUT HERE > 81 int main() { > 82 FairTournament().run_test(-1); > 83 } > 84 // END CUT HERE
Added SRM/345/1A.cpp version [c2bedd2e817bc099]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class Pathfinding > 19 { > 20 public: > 21 int getDirections(int x, int y) > 22 { > 23 if( x==0 && y==0 ) > 24 return 0; > 25 > 26 int xincr = 0; > 27 int yincr = 0; > 28 if(x<0) yincr=1, x=-x; > 29 if(y<0) xincr=1, y=-y; > 30 if( x==0 ) > 31 swap(xincr, yincr), swap(x, y); > 32 > 33 if( y==0 ) > 34 { > 35 if( yincr == 0 ) > 36 return x; > 37 else > 38 if( x%2 == 1 ) > 39 return x+2; > 40 else > 41 return x+4; > 42 } > 43 > 44 if( 0==xincr && y%2==yincr || 0==yincr && x%2==xincr ) > 45 return x+y; > 46 > 47 if( 0==xincr && x%2==xincr || 0==yincr && y%2==yincr ) > 48 return x+y; > 49 > 50 if( 0==xincr || 0==yincr ) > 51 return x+y+2; > 52 > 53 if( x%2==xincr || y%2==yincr ) > 54 return x+y+2; > 55 > 56 return x+y+4; > 57 } > 58 > 59 // BEGIN CUT HERE > 60 public: > 61 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 62 private: > 63 template <typename T> string print_array(const vector<T> &V) { ostringst > 64 void verify_case(int Case, const int &Expected, const int &Received) { c > 65 void test_case_0() { int Arg0 = 0; int Arg1 = -4; int Arg2 = 8; verify_c > 66 void test_case_1() { int Arg0 = 5; int Arg1 = -4; int Arg2 = 9; verify_c > 67 void test_case_2() { int Arg0 = 5; int Arg1 = 4; int Arg2 = 9; verify_ca > 68 void test_case_3() { int Arg0 = -1; int Arg1 = -4; int Arg2 = 7; verify_ > 69 void test_case_4() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 0; verify_ca > 70 > 71 // END CUT HERE > 72 }; > 73 // BEGIN CUT HERE > 74 int main() { Pathfinding().run_test(-1); } > 75 // END CUT HERE
Added SRM/345/1B.cpp version [75343b8007c95b13]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class StoneGame > 19 { > 20 public: > 21 int getScore(vector <int> treasure, vector <int> stones) > 22 { > 23 // Note that we always lose 'even' piles... > 24 > 25 vector<int> ones; > 26 int numOdds = 0; > 27 int sumGt1 = 0; > 28 > 29 int n = treasure.size(); > 30 for(int i=0; i<n; ++i) > 31 if( stones[i] == 1 ) > 32 ones.push_back( treasure[i] ); > 33 else > 34 numOdds += stones[i]%2, > 35 sumGt1 += treasure[i]; > 36 > 37 sort(ones.rbegin(), ones.rend()); > 38 > 39 int score = 0; > 40 for(int i=0; i<ones.size(); i+=2) // take 1-stone piles altenati > 41 score += ones[i]; > 42 if( (ones.size()+numOdds)%2 == 1 ) // then make odd-stone piles > 43 score += sumGt1; // if all piles are made even, one-side > 44 return score; > 45 } > 46 > 47 // BEGIN CUT HERE > 48 public: > 49 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 50 private: > 51 template <typename T> string print_array(const vector<T> &V) { ostringst > 52 void verify_case(int Case, const int &Expected, const int &Received) { c > 53 void test_case_0() { int Arr0[] = {3,2}; vector <int> Arg0(Arr0, Arr0 + > 54 void test_case_1() { int Arr0[] = {5,4,3,2,1}; vector <int> Arg0(Arr0, A > 55 void test_case_2() { int Arr0[] = {5,5}; vector <int> Arg0(Arr0, Arr0 + > 56 void test_case_3() { int Arr0[] = {1}; vector <int> Arg0(Arr0, Arr0 + (s > 57 > 58 // END CUT HERE > 59 }; > 60 // BEGIN CUT HERE > 61 int main() { StoneGame().run_test(-1); } > 62 // END CUT HERE
Added SRM/345/1C.cpp version [84d7893a9bc28147]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 LL next_combination(LL p) > 19 { > 20 LL lsb = p & -p; > 21 LL rem = p + lsb; > 22 LL rit = rem & ~p; > 23 return rem|(((rit/lsb)>>1)-1); > 24 } > 25 > 26 class ByteLand > 27 { > 28 public: > 29 int buildCastles(vector<int> road, vector<int> distance, vector<int> cas > 30 { > 31 int n = road.size(); > 32 > 33 // Construct the Graph > 34 int d[50][50] = {}; > 35 for(int v=0; v<n; ++v) > 36 for(int u=v+1; u<n; ++u) > 37 d[v][u] = d[u][v] = 0x3fffffff; > 38 for(int v=0; v<n; ++v) > 39 d[v][road[v]] = d[road[v]][v] = min( d[v][road[v]], dist > 40 > 41 // Warshall-Floyd > 42 for(int m=0; m<n; ++m) > 43 for(int i=0; i<n; ++i) > 44 for(int j=0; j<n; ++j) > 45 d[i][j] = min(d[i][j], d[i][m]+d[m][j]); > 46 > 47 // Binary Search: the answer is in (L,R] > 48 int L=-1, R=accumulate(distance.begin(), distance.end(), 0); > 49 while( R-L > 1 ) > 50 (is_possible((L+R)/2, d, n, castle, k) ? R : L) = (L+R)/ > 51 return R; > 52 } > 53 > 54 bool is_possible( int D, int d[50][50], int n, const vector<int>& c, int > 55 { > 56 // the set of cities not covered yet > 57 LL goal = (1LL << n) - 1; > 58 for(int v=0; v<n; ++v) > 59 for(int i=0; i<c.size(); ++i) > 60 if( d[v][c[i]] <= D ) > 61 goal &= ~(1LL << v); > 62 > 63 // the set of cities each city can cover > 64 set<LL> mask; > 65 for(int v=0; v<n; ++v) { > 66 LL m = 0; > 67 for(int u=0; u<n; ++u) > 68 if( d[v][u] <= D ) > 69 m |= (1LL << u); > 70 if( m&goal ) > 71 mask.insert( m&goal ); > 72 } > 73 > 74 // is it possible to cover the goal by at most k elements from m > 75 return canCover(goal, mask, k); > 76 } > 77 > 78 bool canCover( LL goal, set<LL>& mask, int k ) > 79 { > 80 // optimizer > 81 for(bool update=true; update; ) { > 82 update = false; > 83 > 84 // if *it \subseteq *jt for some jt, then we NEVER use i > 85 // if *it is the only mask covering some city, we ALWAYS > 86 for(set<LL>::iterator it=mask.begin(); it!=mask.end(); ) > 87 bool isSubset = false; > 88 LL onlyByMe = *it & goal; > 89 for(set<LL>::iterator jt=mask.begin(); jt!=mask. > 90 if( it!=jt ) { > 91 onlyByMe &= ~*jt; > 92 isSubset |= (*it & *jt & goal) = > 93 } > 94 > 95 update |= isSubset | !!onlyByMe; > 96 > 97 if( isSubset ) > 98 mask.erase(it++); > 99 else if( onlyByMe ) { > 100 if( --k < 0 ) > 101 return false; > 102 goal &= ~*it; > 103 mask.erase(it++); > 104 } > 105 else > 106 ++it; > 107 } > 108 > 109 if( mask.size()<=k || goal==0 ) > 110 return true; > 111 } > 112 > 113 // exhaustive search > 114 vector<LL> ms(mask.begin(), mask.end()); > 115 for(LL i=(1LL<<k)-1; i<(1LL<<ms.size()); i=next_combination(i)) > 116 { > 117 LL gg = goal; > 118 for(LL j=0; (1LL<<j)<=i; ++j) > 119 if( i & (1<<j) ) > 120 gg &= ~ms[j]; > 121 if( gg == 0 ) > 122 return true; > 123 } > 124 return false; > 125 } > 126 > 127 // BEGIN CUT HERE > 128 public: > 129 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 130 private: > 131 template <typename T> string print_array(const vector<T> &V) { ostringst > 132 void verify_case(int Case, const int &Expected, const int &Received) { c > 133 void test_case_0() { int Arr0[] = {1,2,3,4,0}; vector <int> Arg0(Arr0, A > 134 void test_case_1() { int Arr0[] = {1,2,0}; vector <int> Arg0(Arr0, Arr0 > 135 void test_case_2() { int Arr0[] = {0,1}; vector <int> Arg0(Arr0, Arr0 + > 136 void test_case_3() { int Arr0[] = {0,2,0,0,2,2,8,3,8,7}; vector <int> Ar > 137 void test_case_4() { int Arr0[] = {1, 0}; vector <int> Arg0(Arr0, Arr0 + > 138 > 139 // END CUT HERE > 140 }; > 141 // BEGIN CUT HERE > 142 int main() { ByteLand().run_test(-1); } > 143 // END CUT HERE
Added SRM/346-U/1A.cpp version [fe758173c829bb2f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL gcd(LL a, LL b) > 23 { > 24 while(a) > 25 swap(a, b%=a); > 26 return b; > 27 } > 28 > 29 LL lcm(LL a, LL b) > 30 { > 31 return a/gcd(a,b)*b; > 32 } > 33 > 34 class CommonMultiples { public: > 35 int countCommMult(vector <int> a, int lower, int upper) > 36 { > 37 LL m = 1; > 38 for(int i=0; i<a.size(); ++i) > 39 { > 40 m = lcm(m, a[i]); > 41 if( m > upper ) > 42 return 0; > 43 } > 44 return upper/m - (lower-1)/m; > 45 } > 46 }; > 47 > 48 // BEGIN CUT HERE > 49 #include <ctime> > 50 double start_time; string timer() > 51 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 52 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 53 { os << "{ "; > 54 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 55 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 56 void verify_case(const int& Expected, const int& Received) { > 57 bool ok = (Expected == Received); > 58 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 59 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 60 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 61 #define END verify_case(_, CommonMultiples().countCommMult(a, lower, upper) > 62 int main(){ > 63 > 64 CASE(0) > 65 int a_[] = {1,2,3}; > 66 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 67 int lower = 5; > 68 int upper = 15; > 69 int _ = 2; > 70 END > 71 CASE(1) > 72 int a_[] = {1,2,4,8,16,32,64}; > 73 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 74 int lower = 128; > 75 int upper = 128; > 76 int _ = 1; > 77 END > 78 CASE(2) > 79 int a_[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,49}; > 80 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 81 int lower = 1; > 82 int upper = 2000000000; > 83 int _ = 0; > 84 END > 85 CASE(3) > 86 int a_[] = {1,1,1}; > 87 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 88 int lower = 1; > 89 int upper = 2000000000; > 90 int _ = 2000000000; > 91 END > 92 CASE(4) > 93 int a_[] = {1}; > 94 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 95 int lower = 1; > 96 int upper = 1; > 97 int _ = 1; > 98 END > 99 CASE(5) > 100 int a_[] = {2,4}; > 101 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 102 int lower = 4; > 103 int upper = 8; > 104 int _ = 2; > 105 END > 106 > 107 } > 108 // END CUT HERE
Added SRM/346-U/1B.cpp version [b041c671ce46ca29]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class HeightRound { public: > 23 typedef pair<pair<int,int>,int> key_t; > 24 map<key_t, int> memo; > 25 > 26 int score( int F, int B, int I, const vector<int>& h ) > 27 { > 28 if( I == h.size()-1 ) > 29 return max(h[I]-h[F], h[I]-h[B]); > 30 > 31 key_t key(make_pair(F,B), I); > 32 if( memo.count(key) ) > 33 return memo[key]; > 34 > 35 int s1 = max(h[I]-h[F], score(I, B, I+1, h)); > 36 int s2 = max(h[I]-h[B], score(F, I, I+1, h)); > 37 return memo[key] = min(s1, s2); > 38 } > 39 > 40 void best( int F, int B, int I, const vector<int>& h, int acc, deque<int > 41 { > 42 if( I == h.size()-1 ) { > 43 result = deque<int>(1, h[I]); > 44 result.push_front(h[F]); > 45 result.push_back(h[B]); > 46 return; > 47 } > 48 > 49 int s1 = max(acc, max(h[I]-h[F], score(I, B, I+1, h))); > 50 int s2 = max(acc, max(h[I]-h[B], score(F, I, I+1, h))); > 51 > 52 if( s1<=s2 ) { // if tie, choose lexicographically earlier one > 53 best( I, B, I+1, h, s1, result ); > 54 result.push_front( h[F] ); > 55 } > 56 else { > 57 best( F, I, I+1, h, s2, result ); > 58 result.push_back( h[B] ); > 59 } > 60 } > 61 > 62 vector <int> getBestRound(vector <int> h) > 63 { > 64 // The answer must be of form: > 65 // h[0], h[1], (...ascending...), h[$-1], (...descending...). > 66 // Thus, I begin from > 67 // F=1, B=0: [h[1], .........., h[0]] > 68 // and fill each h[I=2,3,...] either to the left or right of the > 69 // and choose the best one. To choose lex-first candidate, I use > 70 // technique, which first computes only scores and then choose t > 71 // lexicographically afterwards. > 72 > 73 sort(h.begin(), h.end()); > 74 > 75 deque<int> result; > 76 best( 1, 0, 2, h, 0, result ); > 77 > 78 rotate(result.begin(), result.end()-1, result.end()); > 79 return vector<int>(result.begin(), result.end()); > 80 } > 81 }; > 82 > 83 // BEGIN CUT HERE > 84 #include <ctime> > 85 double start_time; string timer() > 86 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 87 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 88 { os << "{ "; > 89 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 90 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 91 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 92 bool ok = (Expected == Received); > 93 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 94 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 95 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 96 #define END verify_case(_, HeightRound().getBestRound(heights));} > 97 int main(){ > 98 > 99 CASE(0) > 100 int heights_[] = {1,2,3,4}; > 101 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 102 int __[] = {1, 2, 4, 3 }; > 103 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 104 END > 105 CASE(1) > 106 int heights_[] = {1000,500,1}; > 107 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 108 int __[] = {1, 500, 1000 }; > 109 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 110 END > 111 CASE(2) > 112 int heights_[] = {1,3,4,5,7}; > 113 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 114 int __[] = {1, 3, 5, 7, 4 }; > 115 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 116 END > 117 CASE(3) > 118 int heights_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 119 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; > 120 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 121 int __[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, > 122 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 123 END > 124 CASE(4) > 125 int heights_[] = {1,2,1}; > 126 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 127 int __[] = {1,1,2}; > 128 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 129 END > 130 CASE(5) > 131 int heights_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21, > 132 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 133 int __[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 134 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 135 END > 136 CASE(6) > 137 int heights_[] = {7, 43, 3, 6, 32, 2, 42, 23, 1, 30}; > 138 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 139 int __[] = {1, 2, 3, 6, 7, 30, 32, 42, 43, 23}; > 140 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 141 END > 142 > 143 } > 144 // END CUT HERE
Added SRM/346-U/2C.cpp version [c4cdee79cf335842]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class FastPostman { public: > 23 struct vert > 24 { > 25 int Ldone, Rdone; > 26 bool nowAtL; > 27 > 28 bool operator<( const vert& rhs ) const { > 29 if( Ldone != rhs.Ldone ) return Ldone < rhs.Ldone; > 30 if( Rdone != rhs.Rdone ) return Rdone < rhs.Rdone; > 31 return nowAtL < rhs.nowAtL; > 32 } > 33 vert(int l, int r, bool b) : Ldone(l), Rdone(r), nowAtL(b) {} > 34 }; > 35 typedef vert edge; > 36 typedef pair<int,edge> cedge; > 37 > 38 int minTime(vector <int> address, vector <int> maxTime, int initialPos) > 39 { > 40 const int N = address.size(); > 41 > 42 vector< pair<int,int> > L, R; > 43 R.push_back( make_pair(initialPos,0) ); > 44 for(int i=0; i<N; ++i) > 45 if( initialPos <= address[i] ) > 46 R.push_back( make_pair(address[i], maxTime[i]) ) > 47 else > 48 L.push_back( make_pair(address[i], maxTime[i]) ) > 49 stable_sort( R.begin(), R.end() ); > 50 stable_sort( L.rbegin(), L.rend() ); > 51 > 52 vert S(0, 1, false); > 53 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 54 set<vert> V; > 55 Q.push( cedge(0,S) ); > 56 while( !Q.empty() ) > 57 { > 58 int c = Q.top().first; > 59 vert v = Q.top().second; > 60 Q.pop(); > 61 if( V.count(v) ) > 62 continue; > 63 if( v.Ldone == L.size() && v.Rdone == R.size() ) > 64 return c; > 65 V.insert(v); > 66 > 67 if( v.Ldone < L.size() ) { > 68 vert u(v.Ldone+1, v.Rdone, true); > 69 int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v > 70 if( !V.count(u) && cu <= L[v.Ldone].second ) > 71 Q.push( cedge(cu,u) ); > 72 } > 73 if( v.Rdone < R.size() ) { > 74 vert u(v.Ldone, v.Rdone+1, false); > 75 int cu = c + abs( (v.nowAtL ? L[v.Ldone-1] : R[v > 76 if( !V.count(u) && cu <= R[v.Rdone].second ) > 77 Q.push( cedge(cu,u) ); > 78 } > 79 } > 80 return -1; > 81 } > 82 }; > 83 > 84 // BEGIN CUT HERE > 85 #include <ctime> > 86 double start_time; string timer() > 87 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 88 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 89 { os << "{ "; > 90 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 91 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 92 void verify_case(const int& Expected, const int& Received) { > 93 bool ok = (Expected == Received); > 94 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 95 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 96 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 97 #define END verify_case(_, FastPostman().minTime(address, maxTime, initialP > 98 int main(){ > 99 > 100 CASE(0) > 101 int address_[] = {1,3,5,7}; > 102 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 103 int maxTime_[] = {9,2,5,100}; > 104 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 105 int initialPos = 4; > 106 int _ = 13; > 107 END > 108 CASE(1) > 109 int address_[] = {1,5}; > 110 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 111 int maxTime_[] = {6,2}; > 112 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 113 int initialPos = 3; > 114 int _ = 6; > 115 END > 116 CASE(2) > 117 int address_[] = {1000000}; > 118 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 119 int maxTime_[] = {45634}; > 120 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 121 int initialPos = 876; > 122 int _ = -1; > 123 END > 124 CASE(3) > 125 int address_[] = {1,7,10,4}; > 126 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 127 int maxTime_[] = {15,6,28,39}; > 128 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 129 int initialPos = 2; > 130 int _ = 20; > 131 END > 132 CASE(4) > 133 int address_[] = {1000000,1000000,1000000,1000000}; > 134 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 135 int maxTime_[] = {563,23452,32426,1}; > 136 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 137 int initialPos = 1000000; > 138 int _ = 0; > 139 END > 140 CASE(5) > 141 int address_[] = {1}; > 142 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 143 int maxTime_[] = {2}; > 144 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 145 int initialPos = {3}; > 146 int _ = 2; > 147 END > 148 CASE(6) > 149 int address_[] = {0, 1000000}; > 150 vector <int> address(address_, address_+sizeof(address_)/sizeof(*addre > 151 int maxTime_[] = {500000, 1500000}; > 152 vector <int> maxTime(maxTime_, maxTime_+sizeof(maxTime_)/sizeof(*maxTi > 153 int initialPos = 500000; > 154 int _ = 1500000; > 155 END > 156 > 157 } > 158 // END CUT HERE
Added SRM/347-U/1A.cpp version [dc68ca069cbe1c91]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Aircraft { public: > 23 double f(const double p[], const double v[], double t) > 24 { > 25 return sqrt( (p[0]+t*v[0])*(p[0]+t*v[0]) + (p[1]+t*v[1])*(p[1]+t > 26 } > 27 string nearMiss(vector <int> p1, vector <int> v1, vector <int> p2, vecto > 28 { > 29 const double p[] = {p1[0]-p2[0], p1[1]-p2[1], p1[2]-p2[2]}; > 30 const double v[] = {v1[0]-v2[0], v1[1]-v2[1], v1[2]-v2[2]}; > 31 > 32 double x = 0.0; > 33 double w = 1e+10; > 34 for(int k=0; k<1000000; ++k) > 35 { > 36 double y = 2*x/3 + w/3; > 37 double z = x/3 + 2*w/3; > 38 double fx = f(p,v,x); > 39 double fy = f(p,v,y); > 40 double fz = f(p,v,z); > 41 double fw = f(p,v,w); > 42 if( fx < fy ) > 43 w = y; > 44 else if( fz > fw ) > 45 x = z; > 46 else if( fy < fz ) > 47 w = z; > 48 else > 49 x = y; > 50 } > 51 > 52 return f(p,v,x)-0.000001<=R ? "YES" : "NO"; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const string& Expected, const string& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, Aircraft().nearMiss(p1, v1, p2, v2, R));} > 70 int main(){ > 71 > 72 CASE(0) > 73 int p1_[] = {15,50,5}; > 74 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 75 int v1_[] = {25,1,0}; > 76 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 77 int p2_[] = {161,102,9}; > 78 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 79 int v2_[] = {-10,-10,-1}; > 80 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 81 int R = 10; > 82 string _ = "YES"; > 83 END > 84 CASE(1) > 85 int p1_[] = {0,0,0}; > 86 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 87 int v1_[] = {2,2,0}; > 88 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 89 int p2_[] = {9,0,5}; > 90 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 91 int v2_[] = {-2,2,0}; > 92 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 93 int R = 5; > 94 string _ = "YES"; > 95 END > 96 CASE(2) > 97 int p1_[] = {0,0,0}; > 98 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 99 int v1_[] = {-2,2,0}; > 100 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 101 int p2_[] = {9,0,5}; > 102 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 103 int v2_[] = {2,2,0}; > 104 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 105 int R = 5; > 106 string _ = "NO"; > 107 END > 108 CASE(3) > 109 int p1_[] = {-2838,-7940,-2936}; > 110 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 111 int v1_[] = {1,1,-2}; > 112 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 113 int p2_[] = {532,3850,9590}; > 114 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 115 int v2_[] = {1,0,-3}; > 116 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 117 int R = 3410; > 118 string _ = "YES"; > 119 END > 120 CASE(4) > 121 int p1_[] = {-8509,9560,345}; > 122 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 123 int v1_[] = {-89,-33,62}; > 124 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 125 int p2_[] = {-5185,-1417,2846}; > 126 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 127 int v2_[] = {-58,24,26}; > 128 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 129 int R = 8344; > 130 string _ = "YES"; > 131 END > 132 CASE(5) > 133 int p1_[] = {-7163,-371,-2459}; > 134 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 135 int v1_[] = {-59,-41,-14}; > 136 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 137 int p2_[] = {-2398,-426,-5487}; > 138 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 139 int v2_[] = {-43,27,67}; > 140 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 141 int R = 5410; > 142 string _ = "NO"; > 143 END > 144 CASE(6) > 145 int p1_[] = {1774,-4491,7810}; > 146 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 147 int v1_[] = {-12,19,-24}; > 148 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 149 int p2_[] = {2322,3793,9897}; > 150 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 151 int v2_[] = {-12,19,-24}; > 152 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 153 int R = 10000; > 154 string _ = "YES"; > 155 END > 156 CASE(7) > 157 int p1_[] = {3731,8537,5661}; > 158 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 159 int v1_[] = {-70,71,32}; > 160 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 161 int p2_[] = {8701,-1886,-5115}; > 162 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 163 int v2_[] = {28,-13,7}; > 164 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 165 int R = 9766; > 166 string _ = "NO"; > 167 END > 168 CASE(8) > 169 int p1_[] = {0,0,0}; > 170 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 171 int v1_[] = {0,0,0}; > 172 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 173 int p2_[] = {1,0,0}; > 174 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 175 int v2_[] = {0,0,0}; > 176 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 177 int R = 1; > 178 string _ = "YES"; > 179 END > 180 CASE(9) > 181 int p1_[] = {0,0,0}; > 182 vector <int> p1(p1_, p1_+sizeof(p1_)/sizeof(*p1_)); > 183 int v1_[] = {0,0,0}; > 184 vector <int> v1(v1_, v1_+sizeof(v1_)/sizeof(*v1_)); > 185 int p2_[] = {2,0,0}; > 186 vector <int> p2(p2_, p2_+sizeof(p2_)/sizeof(*p2_)); > 187 int v2_[] = {0,0,0}; > 188 vector <int> v2(v2_, v2_+sizeof(v2_)/sizeof(*v2_)); > 189 int R = 1; > 190 string _ = "NO"; > 191 END > 192 > 193 } > 194 // END CUT HERE
Added SRM/347-U/1B.cpp version [915c95d134abe3d5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class FlightScheduler { public: > 23 double f(double n, int distance, int K, int emptyMass, int takeoffFuel) > 24 { > 25 return n * (exp(distance/n / K) * emptyMass - emptyMass + takeof > 26 } > 27 double minimizeFuel(int distance, int K, int emptyMass, int takeoffFuel) > 28 { > 29 LL x = 1; > 30 LL w = (1LL<<60); > 31 while( w-x > 10 ) > 32 { > 33 LL y = 2*x/3 + w/3; > 34 LL z = x/3 + 2*w/3; > 35 double fx = f(x, distance, K, emptyMass, takeoffFuel); > 36 double fy = f(y, distance, K, emptyMass, takeoffFuel); > 37 double fz = f(z, distance, K, emptyMass, takeoffFuel); > 38 double fw = f(w, distance, K, emptyMass, takeoffFuel); > 39 if( fx < fy ) w = y; > 40 else if( fz > fw ) x = z; > 41 else if( fy < fz ) w = z; > 42 else x = y; > 43 } > 44 > 45 double fuMin = 1e+300; > 46 for(LL n=x; n<=w; ++n) > 47 fuMin = min(fuMin, f(n, distance, K, emptyMass, takeoffF > 48 return fuMin; > 49 } > 50 }; > 51 > 52 // BEGIN CUT HERE > 53 #include <ctime> > 54 double start_time; string timer() > 55 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 56 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 57 { os << "{ "; > 58 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 59 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 60 void verify_case(const double& Expected, const double& Received) { > 61 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 62 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 63 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 64 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 65 #define END verify_case(_, FlightScheduler().minimizeFuel(distance, K, empt > 66 int main(){ > 67 > 68 CASE(0) > 69 int distance = 40000; > 70 int K = 100000; > 71 int emptyMass = 150000; > 72 int takeoffFuel = 5000; > 73 double _ = 76420.82744805096; > 74 END > 75 CASE(1) > 76 int distance = 40000; > 77 int K = 55000; > 78 int emptyMass = 150000; > 79 int takeoffFuel = 5000; > 80 double _ = 138450.61724934017; > 81 END > 82 CASE(2) > 83 int distance = 1000; > 84 int K = 500; > 85 int emptyMass = 1000; > 86 int takeoffFuel = 50; > 87 double _ = 2664.9853821314487; > 88 END > 89 CASE(3) > 90 int distance = 10000; > 91 int K = 100; > 92 int emptyMass = 200; > 93 int takeoffFuel = 5; > 94 double _ = 24635.869665316768; > 95 END > 96 CASE(4) > 97 int distance = 140000; > 98 int K = 4; > 99 int emptyMass = 10000; > 100 int takeoffFuel = 10; > 101 double _ = 3.6576871282155204E8; > 102 END > 103 > 104 } > 105 // END CUT HERE
Added SRM/347-U/2C.cpp version [e3b47001da49105a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int INF = 0x3fffffff; > 23 > 24 class TaxiManager { public: > 25 int schedule(vector <string> roads, vector <string> customers) > 26 { > 27 const int V = roads.size(); > 28 vector< vector<int> > d(V, vector<int>(V)); > 29 for(int i=0; i<V; ++i) > 30 for(int j=0; j<V; ++j) > 31 if( i != j ) > 32 d[i][j] = (roads[i][j]=='0' ? INF : road > 33 for(int k=0; k<V; ++k) > 34 for(int i=0; i<V; ++i) > 35 for(int j=0; j<V; ++j) > 36 d[i][j] = min(d[i][j], d[i][k]+d[k][j]); > 37 > 38 const int M = customers.size(); > 39 vector< pair<int,int> > c; > 40 for(int i=0; i<M; ++i) > 41 { > 42 int f, t; > 43 stringstream(customers[i]) >> f >> t; > 44 c.push_back( make_pair(f,t) ); > 45 } > 46 > 47 int best = INF; > 48 for(int mask=0; mask<(1<<M); ++mask) > 49 best = min(best, max(tsp(d,c,mask), tsp(d,c,((1<<M)-1)&~ > 50 return best; > 51 } > 52 > 53 map<pair<int,int>, int> memo; > 54 int tsp(const vector< vector<int> >& d, const vector< pair<int,int> >& c > 55 { > 56 if( mask == 0 ) > 57 return d[cur][0]; > 58 > 59 pair<int,int> key(mask, cur); > 60 if( memo.count(key) ) return memo[key]; > 61 > 62 int best = INF; > 63 for(int next=0; (1<<next)<=mask; ++next) > 64 if( (1<<next) & mask ) > 65 best = min(best, > 66 d[cur][c[next].first] + d[c[next].first] > 67 return memo[key] = best; > 68 } > 69 }; > 70 > 71 // BEGIN CUT HERE > 72 #include <ctime> > 73 double start_time; string timer() > 74 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 75 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 76 { os << "{ "; > 77 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 78 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 79 void verify_case(const int& Expected, const int& Received) { > 80 bool ok = (Expected == Received); > 81 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 82 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 83 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 84 #define END verify_case(_, TaxiManager().schedule(roads, customers));} > 85 int main(){ > 86 > 87 CASE(0) > 88 string roads_[] = {"020200" > 89 ,"202020" > 90 ,"020002" > 91 ,"200020" > 92 ,"020202" > 93 ,"002020"}; > 94 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 95 string customers_[] = {"5 3","2 4","1 5","3 2"}; > 96 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 97 int _ = 16; > 98 END > 99 CASE(1) > 100 string roads_[] = > 101 {"00020251090265906661" > 102 ,"00763002550100090081" > 103 ,"06003699000080062771" > 104 ,"00000710460400035310" > 105 ,"50000039119198350060" > 106 ,"66060004050810046028" > 107 ,"02333108565000200880" > 108 ,"40212560000209205231" > 109 ,"02601150098329905062" > 110 ,"00210383709951005203" > 111 ,"10111087340780827070" > 112 ,"05065800003095040140" > 113 ,"15604020082000100090" > 114 ,"83430030070580600750" > 115 ,"10588355007006001150" > 116 ,"14400080790005400536" > 117 ,"23400990400933060004" > 118 ,"11053016300602000090" > 119 ,"90040920084059282502" > 120 ,"61300007077904050900"}; > 121 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 122 string customers_[] = {"0 19","4 16","15 16","4 18","2 7","9 15","11 6", > 123 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 124 int _ = 33; > 125 END > 126 CASE(2) > 127 string roads_[] = > 128 {"095222800320504" > 129 ,"107600288090501" > 130 ,"760973530769345" > 131 ,"963093337510830" > 132 ,"338404069255826" > 133 ,"291700050155264" > 134 ,"002783031709004" > 135 ,"404730701707712" > 136 ,"068870030090995" > 137 ,"320025180036103" > 138 ,"468695042801904" > 139 ,"233626561000105" > 140 ,"070014432197086" > 141 ,"887301000143802" > 142 ,"230852749990330"}; > 143 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 144 string customers_[] = {"3 6","0 4","2 7","9 7","13 9","1 6","7 13","14 2 > 145 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 146 int _ = 28; > 147 END > 148 CASE(3) > 149 string roads_[] = {"00401","50990","00062","08008","03000"}; > 150 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 151 string customers_[] = {"2 4"}; > 152 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 153 int _ = 14; > 154 END > 155 /* > 156 CASE(4) > 157 string roads_[] = ; > 158 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 159 string customers_[] = ; > 160 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 161 int _ = ; > 162 END > 163 CASE(5) > 164 string roads_[] = ; > 165 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 166 string customers_[] = ; > 167 vector <string> customers(customers_, customers_+sizeof(customers_)/si > 168 int _ = ; > 169 END > 170 */ > 171 } > 172 // END CUT HERE
Added SRM/348-U/1A.cpp version [7f2279a9bdb2181b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LostParentheses { public: > 23 int minResult(string e) > 24 { > 25 int sum = 0; > 26 bool prevOpPlus = true; > 27 bool minusFound = false; > 28 for(int p=0,i=0; i<=e.size(); ++i) > 29 if( i==e.size() || e[i] == '-' ) > 30 { > 31 stringstream ss; > 32 ss << e.substr(p, i-p); > 33 int num; > 34 ss >> num; > 35 > 36 sum += (prevOpPlus ? +num : -num); > 37 p = i+1; > 38 minusFound = true; > 39 prevOpPlus = false; > 40 } > 41 else if( e[i] == '+' ) > 42 { > 43 stringstream ss; > 44 ss << e.substr(p, i-p); > 45 int num; > 46 ss >> num; > 47 > 48 sum += (prevOpPlus ? +num : -num); > 49 p = i+1; > 50 prevOpPlus = !minusFound; > 51 } > 52 return sum; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const int& Expected, const int& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, LostParentheses().minResult(e));} > 70 int main(){ > 71 > 72 CASE(0) > 73 string e = "55-50+40"; > 74 int _ = -35; > 75 END > 76 CASE(1) > 77 string e = "10+20+30+40"; > 78 int _ = 100; > 79 END > 80 CASE(2) > 81 string e = "00009-00009"; > 82 int _ = 0; > 83 END > 84 CASE(3) > 85 string e = "1"; > 86 int _ = 1; > 87 END > 88 CASE(4) > 89 string e = "1-2+3"; > 90 int _ = -4; > 91 END > 92 > 93 } > 94 // END CUT HERE
Added SRM/348-U/1B-U.cpp version [cb0ca96bc6d09c19]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename To, typename From> > 23 To lexical_cast( const From& f ) > 24 { > 25 stringstream ss; > 26 ss << f; > 27 To t; > 28 ss >> t; > 29 return t; > 30 } > 31 > 32 > 33 class RailwayTickets { public: > 34 int minRejects(vector <string> travel, int seats) > 35 { > 36 typedef pair<bool,int> event; // [true,A --- false,B) > 37 vector<event> evs; > 38 > 39 for(int i=0; i<travel.size(); ++i) > 40 { > 41 stringstream ss(travel[i]); > 42 for(string s; ss>>s; ) > 43 { > 44 int k = s.find('-'); > 45 int A = lexical_cast<int>( s.substr(0, k) ); > 46 int B = lexical_cast<int>( s.substr(k+1) ); > 47 evs.push_back( event(true,A) ); > 48 evs.push_back( event(false,B) ); > 49 } > 50 } > 51 > 52 sort( evs.begin(), evs.end() ); > 53 > 54 vector< vector<int> > minDeny(evs.size()+1, vector<int>(seats+1) > 55 for(int ti=evs.size()-1; ti>=0; --ti) > 56 for(int si=0; si<=seats; ++si) > 57 if( evs[ti].first ) > 58 { > 59 if( si==0 ) > 60 minDeny[ti][si] = minDeny[ti+1][ > 61 else > 62 minDeny[ti][si] = min( minDeny[t > 63 } > 64 else > 65 { > 66 minDeny[ti][si] = minDeny[ti+1][si+1]; > 67 } > 68 return minDeny[0][seats]; > 69 } > 70 }; > 71 > 72 // BEGIN CUT HERE > 73 #include <ctime> > 74 double start_time; string timer() > 75 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 76 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 77 { os << "{ "; > 78 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 79 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 80 void verify_case(const int& Expected, const int& Received) { > 81 bool ok = (Expected == Received); > 82 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 83 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 84 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 85 #define END verify_case(_, RailwayTickets().minRejects(travel, seats));} > 86 int main(){ > 87 > 88 CASE(0) > 89 string travel_[] = {"1-3 3-5", > 90 "2-4 4-6", > 91 "1-2 2-3 3-4 4-5"}; > 92 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 93 int seats = 2; > 94 int _ = 2; > 95 END > 96 CASE(1) > 97 string travel_[] = {"1-10000","2-10000","1-9999","2-9999","5000-5001"}; > 98 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 99 int seats = 3; > 100 int _ = 2; > 101 END > 102 CASE(2) > 103 string travel_[] = {"1-2 2-5 2-8 234-236 56-878 6-34", > 104 "234-776 3242-8000 999-1000 3-14", > 105 "121-122 435-3455 123-987 77-999"}; > 106 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 107 int seats = 1000; > 108 int _ = 0; > 109 END > 110 CASE(3) > 111 string travel_[] = {"1-2 2-3 3-4 4-5 5-6 6-7 1-3 2-7 1-2 1-4"}; > 112 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 113 int seats = 1; > 114 int _ = 4; > 115 END > 116 CASE(4) > 117 string travel_[] = {"1-2","1-2"}; > 118 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 119 int seats = 1; > 120 int _ = 1; > 121 END > 122 CASE(5) > 123 string travel_[] = {"1-2"}; > 124 vector <string> travel(travel_, travel_+sizeof(travel_)/sizeof(*travel > 125 int seats = 1; > 126 int _ = 0; > 127 END > 128 > 129 } > 130 // END CUT HERE
Added SRM/348-U/2C.cpp version [2ec49067c50d0e11]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class IncreasingSubsequences { public: > 23 long long count(vector <int> a) > 24 { > 25 a.insert(a.begin(), -1); > 26 > 27 int N = a.size(); > 28 > 29 vector< vector<int> > next(N); > 30 for(int i=0; i<N; ++i) { > 31 int L = a[i]+1; > 32 int R = INT_MAX; > 33 for(int j=i+1; j<N; ++j) > 34 if( L<=a[j] && a[j]<=R ) { > 35 next[i].push_back(j); > 36 R = a[j]; > 37 } > 38 } > 39 > 40 vector<LL> num(N); > 41 for(int i=N-1; i>=0; --i) { > 42 if( next[i].size() == 0 ) > 43 num[i] = 1; > 44 else > 45 for(int j=0; j<next[i].size(); ++j) > 46 num[i] += num[next[i][j]]; > 47 } > 48 return num[0]; > 49 } > 50 }; > 51 > 52 // BEGIN CUT HERE > 53 #include <ctime> > 54 double start_time; string timer() > 55 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 56 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 57 { os << "{ "; > 58 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 59 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 60 void verify_case(const long long& Expected, const long long& Received) { > 61 bool ok = (Expected == Received); > 62 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 63 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 64 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 65 #define END verify_case(_, IncreasingSubsequences().count(a));} > 66 int main(){ > 67 > 68 CASE(0) > 69 int a_[] = {1,3,2,6,4,5}; > 70 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 71 long long _ = 4LL; > 72 END > 73 CASE(1) > 74 int a_[] = {1000000000,100000000,10000000,1000000,100000,10000,1000,100, > 75 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 76 long long _ = 10LL; > 77 END > 78 CASE(2) > 79 int a_[] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,100000 > 80 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 81 long long _ = 1LL; > 82 END > 83 CASE(3) > 84 int a_[] = {564,234,34,4365,424,2234,306,21,934,592,195,2395,2396,29345, > 85 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 86 long long _ = 41LL; > 87 END > 88 CASE(4) > 89 int a_[] = {1}; > 90 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 91 long long _ = 1LL; > 92 END > 93 CASE(5) > 94 int a_[] = {5,4,3,2,1}; > 95 vector <int> a(a_, a_+sizeof(a_)/sizeof(*a_)); > 96 long long _ = 5LL; > 97 END > 98 > 99 } > 100 // END CUT HERE
Added SRM/349-U/1A.cpp version [b7df62dcd4535e42]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RadarFinder { public: > 23 int possibleLocations(int x1, int y1, int r1, int x2, int y2, int r2) > 24 { > 25 if( x1==x2 && y1==y2 ) > 26 return r1==r2 ? -1 : 0; > 27 LL rr = LL(x1-x2)*(x1-x2) + LL(y1-y2)*(y1-y2); > 28 LL p12 = LL(r1+r2)*(r1+r2); > 29 LL m12 = LL(r1-r2)*(r1-r2); > 30 if( rr > p12 ) > 31 return 0; > 32 else if( rr == p12 ) > 33 return 1; > 34 else > 35 if( rr < m12 ) > 36 return 0; > 37 else if( rr == m12 ) > 38 return 1; > 39 else > 40 return 2; > 41 } > 42 }; > 43 > 44 // BEGIN CUT HERE > 45 #include <ctime> > 46 double start_time; string timer() > 47 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 48 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 49 { os << "{ "; > 50 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 51 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 52 void verify_case(const int& Expected, const int& Received) { > 53 bool ok = (Expected == Received); > 54 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 55 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 56 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 57 #define END verify_case(_, RadarFinder().possibleLocations(x1, y1, r1, x2, > 58 int main(){ > 59 > 60 CASE(0) > 61 int x1 = 0; > 62 int y1 = 0; > 63 int r1 = 13; > 64 int x2 = 40; > 65 int y2 = 0; > 66 int r2 = 37; > 67 int _ = 2; > 68 END > 69 CASE(1) > 70 int x1 = 0; > 71 int y1 = 0; > 72 int r1 = 3; > 73 int x2 = 0; > 74 int y2 = 7; > 75 int r2 = 4; > 76 int _ = 1; > 77 END > 78 CASE(2) > 79 int x1 = 0; > 80 int y1 = 0; > 81 int r1 = 5; > 82 int x2 = 10; > 83 int y2 = 10; > 84 int r2 = 3; > 85 int _ = 0; > 86 END > 87 CASE(3) > 88 int x1 = 0; > 89 int y1 = 0; > 90 int r1 = 1; > 91 int x2 = 0; > 92 int y2 = 0; > 93 int r2 = 1; > 94 int _ = -1; > 95 END > 96 CASE(4) > 97 int x1 = 1; > 98 int y1 = 1; > 99 int r1 = 1; > 100 int x2 = 1; > 101 int y2 = 1; > 102 int r2 = 2; > 103 int _ = 0; > 104 END > 105 CASE(5) > 106 int x1 = -100000000; > 107 int y1 = -100000000; > 108 int r1 = 141421357; > 109 int x2 = 100000000; > 110 int y2 = 100000000; > 111 int r2 = 141421357; > 112 int _ = 2; > 113 END > 114 CASE(6) > 115 int x1 = 0; > 116 int y1 = 0; > 117 int r1 = 5; > 118 int x2 = 1; > 119 int y2 = 0; > 120 int r2 = 4; > 121 int _ = 1; > 122 END > 123 CASE(7) > 124 int x1 = -1000000000; > 125 int y1 = -1000000000; > 126 int r1 = 1000000000; > 127 int x2 = -999999999; > 128 int y2 = 1000000000; > 129 int r2 = 1000000000; > 130 int _ = 0; > 131 END > 132 > 133 } > 134 // END CUT HERE
Added SRM/349-U/1B.cpp version [177e44b83c8f6a87]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class DiceGames { public: > 23 long long countFormations(vector <int> sides) > 24 { > 25 sort(sides.begin(), sides.end()); > 26 > 27 vector<LL> dp(sides.back(), 0); > 28 dp[0] = 1; > 29 for(int i=0; i<sides.size(); ++i) > 30 partial_sum(dp.begin(), dp.begin()+sides[i], dp.begin()) > 31 return accumulate(dp.begin(), dp.end(), 0LL); > 32 } > 33 }; > 34 > 35 // BEGIN CUT HERE > 36 #include <ctime> > 37 double start_time; string timer() > 38 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 39 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 40 { os << "{ "; > 41 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 42 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 43 void verify_case(const long long& Expected, const long long& Received) { > 44 bool ok = (Expected == Received); > 45 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 46 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 47 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 48 #define END verify_case(_, DiceGames().countFormations(sides));} > 49 int main(){ > 50 > 51 CASE(0) > 52 int sides_[] = {4}; > 53 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 54 long long _ = 4LL; > 55 END > 56 CASE(1) > 57 int sides_[] = {2, 2}; > 58 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 59 long long _ = 3LL; > 60 END > 61 CASE(2) > 62 int sides_[] = {4, 4}; > 63 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 64 long long _ = 10LL; > 65 END > 66 CASE(3) > 67 int sides_[] = {3, 4}; > 68 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 69 long long _ = 9LL; > 70 END > 71 CASE(4) > 72 int sides_[] = {4, 5, 6}; > 73 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 74 long long _ = 48LL; > 75 END > 76 CASE(5) > 77 int sides_[] = {1}; > 78 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 79 long long _ = 1LL; > 80 END > 81 CASE(6) > 82 int sides_[] = {1,1,1,1,1,1,1,1,1,1}; > 83 vector <int> sides(sides_, sides_+sizeof(sides_)/sizeof(*sides_)); > 84 long long _ = 1LL; > 85 END > 86 > 87 } > 88 // END CUT HERE
Added SRM/349-U/1C-U.cpp version [627b63bf02167db1]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LastMarble { public: > 23 typedef pair<int,int> rb; > 24 typedef map<rb,double> state; > 25 > 26 double winningChance(int red, int blue, int removed) > 27 { > 28 state s; > 29 addNextStates( red, blue, 1.0, removed, s ); > 30 return winChance(s); > 31 } > 32 > 33 double winChance( state& s ) > 34 { > 35 if( s.empty() ) > 36 return 1.0; // any value is ok because it will be multip > 37 > 38 int r_plus_b = s.begin()->first.first + s.begin()->first.second; > 39 > 40 double lc = 1.0; > 41 for(int take=1; take<=min(3,r_plus_b); ++take) > 42 { > 43 state ns; > 44 for(state::iterator it=s.begin(); it!=s.end(); ++it) > 45 addNextStates(it->first.first, it->first.second, > 46 > 47 double nowLoseChance = 0.0; > 48 for(state::iterator it=ns.begin(); it!=ns.end(); ) > 49 if( it->first.first == 0 ) { > 50 nowLoseChance += it->second; > 51 ns.erase( it++ ); > 52 } else { > 53 ++it; > 54 } > 55 > 56 double continueChance = 1.0 - nowLoseChance; > 57 for(state::iterator it=ns.begin(); it!=ns.end(); ++it) > 58 it->second /= continueChance; > 59 double loseChance = nowLoseChance + winChance(ns)*contin > 60 lc = min(lc, loseChance); > 61 } > 62 return 1.0 - lc; > 63 } > 64 > 65 void addNextStates(int r, int b, double p, int take, state& m) > 66 { > 67 if( take == 0 ) { > 68 m[rb(r,b)] += p; > 69 return; > 70 } > 71 > 72 state tmp; > 73 tmp[rb(r,b)] = p; > 74 > 75 while( take --> 0 ) > 76 { > 77 state tmp2; > 78 state& tgt = (take ? tmp2 : m); > 79 for(state::iterator it=tmp.begin(); it!=tmp.end(); ++it) > 80 { > 81 int r = it->first.first; > 82 int b = it->first.second; > 83 double p = it->second; > 84 if( r>0 ) tgt[rb(r-1,b)] += p * r / (r+b); > 85 if( b>0 ) tgt[rb(r,b-1)] += p * b / (r+b); > 86 } > 87 tmp.swap(tmp2); > 88 } > 89 } > 90 }; > 91 > 92 // BEGIN CUT HERE > 93 #include <ctime> > 94 double start_time; string timer() > 95 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 96 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 97 { os << "{ "; > 98 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 99 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 100 void verify_case(const double& Expected, const double& Received) { > 101 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 102 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 103 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 104 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 105 #define END verify_case(_, LastMarble().winningChance(red, blue, removed)); > 106 int main(){ > 107 > 108 CASE(0) > 109 int red = 1; > 110 int blue = 1; > 111 int removed = 0; > 112 double _ = 0.5; > 113 END > 114 CASE(1) > 115 int red = 1; > 116 int blue = 2; > 117 int removed = 0; > 118 double _ = 0.3333333333333333; > 119 END > 120 CASE(2) > 121 int red = 2; > 122 int blue = 1; > 123 int removed = 0; > 124 double _ = 0.6666666666666666; > 125 END > 126 CASE(3) > 127 int red = 2; > 128 int blue = 2; > 129 int removed = 1; > 130 double _ = 0.5; > 131 END > 132 /* > 133 CASE(4) > 134 int red = ; > 135 int blue = ; > 136 int removed = ; > 137 double _ = ; > 138 END > 139 CASE(5) > 140 int red = ; > 141 int blue = ; > 142 int removed = ; > 143 double _ = ; > 144 END > 145 */ > 146 } > 147 // END CUT HERE
Added SRM/350-U/1A.cpp version [691135ca23ec3f16]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SumsOfPerfectPowers { public: > 23 int howMany(int lowerBound, int upperBound) > 24 { > 25 set<LL> sq; > 26 sq.insert(0); > 27 sq.insert(1); > 28 for(LL n=2; n*n<=upperBound; ++n) > 29 for(LL nk=n*n; nk<=upperBound; nk*=n) > 30 sq.insert(nk); > 31 vector<LL> sqv( sq.begin(), sq.end() ); > 32 > 33 vector<LL> pp; > 34 for(vector<LL>::iterator it=sqv.begin(); it!=sqv.end(); ++it) > 35 for(vector<LL>::iterator jt=it; jt!=sqv.end() && *it+*jt > 36 pp.push_back( *it + *jt ); > 37 sort( pp.begin(), pp.end() ); > 38 pp.erase( unique(pp.begin(),pp.end()), pp.end() ); > 39 > 40 vector<LL>::iterator s = lower_bound( pp.begin(), pp.end(), lowe > 41 vector<LL>::iterator e = upper_bound( pp.begin(), pp.end(), uppe > 42 return distance(s, e); > 43 } > 44 }; > 45 > 46 // BEGIN CUT HERE > 47 #include <ctime> > 48 double start_time; string timer() > 49 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 50 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 51 { os << "{ "; > 52 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 53 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 54 void verify_case(const int& Expected, const int& Received) { > 55 bool ok = (Expected == Received); > 56 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 57 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 58 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 59 #define END verify_case(_, SumsOfPerfectPowers().howMany(lowerBound, upperB > 60 int main(){ > 61 > 62 CASE(0) > 63 int lowerBound = 0; > 64 int upperBound = 1; > 65 int _ = 2; > 66 END > 67 CASE(1) > 68 int lowerBound = 5; > 69 int upperBound = 6; > 70 int _ = 1; > 71 END > 72 CASE(2) > 73 int lowerBound = 25; > 74 int upperBound = 30; > 75 int _ = 5; > 76 END > 77 CASE(3) > 78 int lowerBound = 103; > 79 int upperBound = 103; > 80 int _ = 0; > 81 END > 82 CASE(4) > 83 int lowerBound = 1; > 84 int upperBound = 100000; > 85 int _ = 33604; > 86 END > 87 CASE(5) > 88 int lowerBound = 0; > 89 int upperBound = 0; > 90 int _ = 1; > 91 END > 92 CASE(6) > 93 int lowerBound = 0; > 94 int upperBound = 5000000; > 95 int _ = 1272868; > 96 END > 97 > 98 } > 99 // END CUT HERE
Added SRM/350-U/1B.cpp version [cbfcaf5b708d4921]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL comb(LL n, LL k) > 23 { > 24 k = min(k, n-k); > 25 > 26 LL c = 1; > 27 for(LL i=0; i<k; ++i) > 28 c *= n-i, c /= i+1; > 29 return c; > 30 } > 31 > 32 class StarsInGraphs { public: > 33 int starnum(LL n, LL C) > 34 { > 35 if( n<=2 ) > 36 return -1; > 37 LL sum = 0; > 38 for(LL k=3; k<=n; ++k) > 39 { > 40 sum += comb(n,k); > 41 if( sum > C ) > 42 return -1; > 43 } > 44 return (int) sum; > 45 } > 46 > 47 int starryPaths(vector <string> adjacencyMatrix, int C) > 48 { > 49 const int N = adjacencyMatrix.size(); > 50 > 51 vector<int> sn; > 52 for(int i=0; i<N; ++i) > 53 sn.push_back( starnum(count(adjacencyMatrix[i].begin(), > 54 > 55 vector<int> v; > 56 for(int i=0; i<N; ++i) > 57 if( sn[i] > 0 ) > 58 v.push_back(i); > 59 > 60 for(int step=0 ;; ++step) > 61 { > 62 if( v.empty() ) > 63 return step; > 64 if( step >= 3*N ) > 65 break; // infinite > 66 > 67 vector<int> v2; > 68 for(int u=0; u<N; ++u) > 69 for(int i=0; i<v.size(); ++i) > 70 if( adjacencyMatrix[v[i]][u]=='1' && sn[ > 71 {v2.push_back(u); break;} > 72 v.swap(v2); > 73 } > 74 > 75 return -1; > 76 } > 77 }; > 78 > 79 // BEGIN CUT HERE > 80 #include <ctime> > 81 double start_time; string timer() > 82 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 83 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 84 { os << "{ "; > 85 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 86 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 87 void verify_case(const int& Expected, const int& Received) { > 88 bool ok = (Expected == Received); > 89 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 90 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 91 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 92 #define END verify_case(_, StarsInGraphs().starryPaths(adjacencyMatrix, C)) > 93 int main(){ > 94 > 95 CASE(0) > 96 string adjacencyMatrix_[] = {"01110", > 97 "10111", > 98 "00000", > 99 "00000", > 100 "00000"}; > 101 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 102 int C = 1000; > 103 int _ = 2; > 104 END > 105 CASE(1) > 106 string adjacencyMatrix_[] = {"01011", > 107 "00111", > 108 "10011", > 109 "00000", > 110 "00000"}; > 111 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 112 int C = 1; > 113 int _ = -1; > 114 END > 115 CASE(2) > 116 string adjacencyMatrix_[] = {"0111", > 117 "0000", > 118 "0000", > 119 "0000"}; > 120 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 121 int C = 1; > 122 int _ = 1; > 123 END > 124 CASE(3) > 125 string adjacencyMatrix_[] = {"01111", > 126 "00000", > 127 "00000", > 128 "00000", > 129 "00000"}; > 130 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 131 int C = 4; > 132 int _ = 0; > 133 END > 134 CASE(4) > 135 string adjacencyMatrix_[] = {"010001100", > 136 "001001100", > 137 "000101110", > 138 "000010111", > 139 "000001111", > 140 "010000000", > 141 "000110000", > 142 "000100001", > 143 "100001000"}; > 144 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 145 int C = 10; > 146 int _ = 5; > 147 END > 148 CASE(5) > 149 string adjacencyMatrix_[] = {"00","00"}; > 150 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 151 int C = 9999999; > 152 int _ = 0; > 153 END > 154 CASE(6) > 155 string adjacencyMatrix_[] = {"0111","1011","1101","1110"}; > 156 vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+siz > 157 int C = 9999; > 158 int _ = -1; > 159 END > 160 > 161 } > 162 // END CUT HERE
Added SRM/351-U/1A.cpp version [a746083be2e0008b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CoinsExchange { public: > 23 int countExchanges(int G1, int S1, int B1, int G2, int S2, int B2) > 24 { > 25 if( G1 < G2 ) { > 26 int n = G2-G1; > 27 return check2( S1, B1, S2+n*11, B2, n ); // 11*n silver > 28 } > 29 if( B1 < B2 ) { > 30 int n = (B2-B1+8)/9; > 31 return check2( G1, S1, G2, S2+n, n ); // n silver needed > 32 } > 33 > 34 // need silver > 35 int n = 0; > 36 while( S1<S2 && G1- 1>=G2 ) S1+=9, G1-= 1, n++; > 37 while( S1<S2 && B1-11>=B2 ) S1+=1, B1-=11, n++; > 38 return S1<S2 ? -1 : n; > 39 } > 40 > 41 int check2(int S1, int B1, int S2, int B2, int acc) > 42 { > 43 if( S1 < S2 ) { > 44 int n = S2-S1; > 45 return B2<=B1-n*11 ? n+acc : -1; > 46 } > 47 if( B1 < B2 ) { > 48 int n = (B2-B1+8)/9; > 49 return S2<=S1-n ? n+acc : -1; > 50 } > 51 return acc; > 52 } > 53 }; > 54 > 55 // BEGIN CUT HERE > 56 #include <ctime> > 57 double start_time; string timer() > 58 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 59 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 60 { os << "{ "; > 61 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 62 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 63 void verify_case(const int& Expected, const int& Received) { > 64 bool ok = (Expected == Received); > 65 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 66 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 67 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 68 #define END verify_case(_, CoinsExchange().countExchanges(G1, S1, B1, G2, S > 69 int main(){ > 70 > 71 CASE(0) > 72 int G1 = 1; > 73 int S1 = 0; > 74 int B1 = 0; > 75 int G2 = 0; > 76 int S2 = 0; > 77 int B2 = 81; > 78 int _ = 10; > 79 END > 80 CASE(1) > 81 int G1 = 1; > 82 int S1 = 100; > 83 int B1 = 12; > 84 int G2 = 5; > 85 int S2 = 53; > 86 int B2 = 33; > 87 int _ = 7; > 88 END > 89 CASE(2) > 90 int G1 = 1; > 91 int S1 = 100; > 92 int B1 = 12; > 93 int G2 = 5; > 94 int S2 = 63; > 95 int B2 = 33; > 96 int _ = -1; > 97 END > 98 CASE(3) > 99 int G1 = 5; > 100 int S1 = 10; > 101 int B1 = 12; > 102 int G2 = 3; > 103 int S2 = 7; > 104 int B2 = 9; > 105 int _ = 0; > 106 END > 107 > 108 } > 109 // END CUT HERE
Added SRM/351-U/1B.cpp version [89c00d40736da67f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP5 > 24 { > 25 int N1, N2, N3, N4, N5; > 26 vector<T> data; > 27 DP5(int N1, int N2, int N3, int N4, int N5, const T& t = T()) > 28 : N1(N1), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t > 29 T& operator()(int i1, int i2, int i3, int i4, int i5) > 30 { return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; } > 31 }; > 32 > 33 > 34 class BoxesArrangement { public: > 35 int maxUntouched(string boxes) > 36 { > 37 int NA = count( boxes.begin(), boxes.end(), 'A' ); > 38 int NB = count( boxes.begin(), boxes.end(), 'B' ); > 39 int NC = count( boxes.begin(), boxes.end(), 'C' ); > 40 > 41 DP5<int> dp(NA+1, NB+1, NC+1, 3, 3); > 42 for(int na=NA; na>=0; --na) > 43 for(int nb=NB; nb>=0; --nb) > 44 for(int nc=NC; nc>=0; --nc) > 45 for(int c=0; c<3; ++c) > 46 for(int l=0; l<3; ++l) > 47 { > 48 int n = na+nb+nc; > 49 if( n == boxes.size() ) > 50 continue; > 51 int m = -1; > 52 for(int x=0; x<3; ++x) { > 53 int nna = na+(x==0); > 54 int nnb = nb+(x==1); > 55 int nnc = nc+(x==2); > 56 int nl = (x==c ? l+1 : 1); > 57 if( nna<=NA && nnb<=NB && nnc<=NC && nl<3 && dp( > 58 m = max(m, (boxes[n]=='A'+x) + dp(nna,nn > 59 } > 60 dp(na,nb,nc,c,l) = m; > 61 } > 62 > 63 return dp(0,0,0,0,0); > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const int& Expected, const int& Received) { > 76 bool ok = (Expected == Received); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, BoxesArrangement().maxUntouched(boxes));} > 81 int main(){ > 82 > 83 CASE(0) > 84 string boxes = "AAABBBCCC"; > 85 int _ = 6; > 86 END > 87 CASE(1) > 88 string boxes = "AAAAAAAACBB"; > 89 int _ = 7; > 90 END > 91 CASE(2) > 92 string boxes = "CCCCCB"; > 93 int _ = -1; > 94 END > 95 CASE(3) > 96 string boxes = "BAACAABAACAAA"; > 97 int _ = 5; > 98 END > 99 CASE(4) > 100 string boxes = "CBBABA"; > 101 int _ = 6; > 102 END > 103 } > 104 // END CUT HERE
Added SRM/352-U/1A.cpp version [0f9aa2d57aa32391]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NumberofFiboCalls { public: > 23 vector <int> fiboCallsMade(int n) > 24 { > 25 vector<int> z(n+2), o(n+2); > 26 z[0] = 1; > 27 o[1] = 1; > 28 for(int i=2; i<=n; ++i) { > 29 z[i] = z[i-2] + z[i-1]; > 30 o[i] = o[i-2] + o[i-1]; > 31 } > 32 vector<int> ans(2); > 33 ans[0] = z[n]; > 34 ans[1] = o[n]; > 35 return ans; > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 48 bool ok = (Expected == Received); > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, NumberofFiboCalls().fiboCallsMade(n));} > 53 int main(){ > 54 > 55 CASE(0) > 56 int n = 0; > 57 int __[] = {1, 0 }; > 58 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 59 END > 60 CASE(1) > 61 int n = 3; > 62 int __[] = {1, 2 }; > 63 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 64 END > 65 CASE(2) > 66 int n = 6; > 67 int __[] = {5, 8 }; > 68 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 69 END > 70 CASE(3) > 71 int n = 22; > 72 int __[] = {10946, 17711 }; > 73 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 74 END > 75 > 76 } > 77 // END CUT HERE
Added SRM/352-U/1B-U.cpp version [426e0d13a9708eff]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class FibonacciKnapsack { public: > 23 long long maximalCost(vector <string> items, string C_) > 24 { > 25 LL C; > 26 stringstream(C_) >> C; > 27 > 28 vector< pair<LL,LL> > wp; > 29 for(int i=0; i<items.size(); ++i) { > 30 LL W, P; > 31 stringstream(items[i]) >> W >> P; > 32 wp.push_back( make_pair(W,P) ); > 33 } > 34 sort( wp.begin(), wp.end() ); > 35 > 36 LL lW = 0, lP = 0; > 37 for(int i=0; i<wp.size(); ++i) > 38 lW += wp[i].first, lP += wp[i].second; > 39 > 40 best = 0; > 41 rec( lW, lP, wp, wp.size()-1, C, 0 ); > 42 return best; > 43 } > 44 > 45 LL best; > 46 void rec( LL lW, LL lP, vector< pair<LL,LL> >& wp, int i, LL w, LL cP ) > 47 { > 48 if( lW <= w ) > 49 best = max(best, lP+cP); > 50 if( lP+cP <= best ) > 51 return; > 52 > 53 for(int k=i; k>=0 && wp[k].first==wp[i].first; --k) > 54 { > 55 lW -= wp[k].first; > 56 lP -= wp[k].second; > 57 if( w >= 0 ) > 58 rec(lW, lP, wp, k-1, w, cP); > 59 w -= wp[k].first; > 60 cP += wp[k].second; > 61 } > 62 } > 63 }; > 64 > 65 // BEGIN CUT HERE > 66 #include <ctime> > 67 double start_time; string timer() > 68 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 69 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 70 { os << "{ "; > 71 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 72 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 73 void verify_case(const long long& Expected, const long long& Received) { > 74 bool ok = (Expected == Received); > 75 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 76 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 77 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 78 #define END verify_case(_, FibonacciKnapsack().maximalCost(items, C));} > 79 int main(){ > 80 > 81 CASE(0) > 82 string items_[] = {"5 555", "8 195", "13 651"}; > 83 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 84 string C = "15"; > 85 long long _ = 750LL; > 86 END > 87 CASE(1) > 88 string items_[] = {"5 555", "8 195", "13 751"}; > 89 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 90 string C = "15"; > 91 long long _ = 751LL; > 92 END > 93 CASE(2) > 94 string items_[] = {"55 1562", "5 814", "55 1962", "8 996", "2 716", "34 > 95 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 96 string C = "94"; > 97 long long _ = 4568LL; > 98 END > 99 CASE(3) > 100 string items_[] = {"13 89"}; > 101 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 102 string C = "1"; > 103 long long _ = 0LL; > 104 END > 105 CASE(4) > 106 string items_[] = {"27777890035288 9419696870097445", > 107 "53316291173 6312623457097563", > 108 "165580141 8848283653257131"}; > 109 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 110 string C = "27777900000000"; > 111 long long _ = 15160907110354694LL; > 112 END > 113 CASE(5) > 114 string items_[] = {"89 90", "55 54", "55 54", "34 35", "34 35", "34 35", > 115 vector <string> items(items_, items_+sizeof(items_)/sizeof(*items_)); > 116 string C = "200"; > 117 long long _ = 241LL; > 118 END > 119 > 120 > 121 } > 122 // END CUT HERE
Added SRM/387-U/2U.cpp version [8d99ffa6917f3084]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int NV = 256; > 23 typedef int flow; > 24 typedef int cost; > 25 typedef int vert; > 26 typedef vert edge; > 27 typedef vector<edge> edges; > 28 typedef vector<edges> graph; > 29 typedef flow flow_graph[NV][NV]; > 30 typedef cost cost_graph[NV][NV]; > 31 static const flow FLOW_INF = 0x7FFFFFFF; > 32 static const cost COST_INF = 0x7FFFFFFF; > 33 > 34 // edges(bidi), capacity, cost(s.t. C[x][y]=-C[y][x]) per 1 flow, src, dst > 35 pair<cost,flow> mincostFlow(graph& G, flow_graph F, cost_graph C, vert S, vert D > 36 { > 37 int N = G.size(); > 38 cost total_cost = 0; > 39 flow total_flow = 0; > 40 > 41 vector<cost> h(N, 0); // potential > 42 for(cost RF=FLOW_INF; RF>0; ) // residual flow > 43 { > 44 // Dijkstra -- find the min-cost path > 45 vector<cost> d(N, COST_INF); d[S] = 0; > 46 vector<vert> prev(N, -1); > 47 > 48 typedef pair<cost, pair<vert,vert> > cedge; > 49 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 50 Q.push( cedge(0, make_pair(S,S)) ); > 51 while( !Q.empty() ) { > 52 cedge e = Q.top(); Q.pop(); > 53 if( prev[e.second.second] >= 0 ) > 54 continue; > 55 prev[e.second.second] = e.second.first; > 56 > 57 vert u = e.second.second; > 58 for(int i=0; i<G[u].size(); ++i) { > 59 vert v = G[u][i]; > 60 cost r_cost = C[u][v] + h[u] - h[v]; > 61 if( F[u][v] > 0 && d[v] > d[u]+r_cost ) > 62 Q.push( cedge(d[v]=d[u]+r_cost, make_pai > 63 } > 64 } > 65 > 66 if( prev[D] < 0 ) > 67 break; // Finished > 68 > 69 // As much as possible > 70 flow f = RF; > 71 for(vert u=D; u!=S; u=prev[u]) > 72 f = min(f, F[prev[u]][u]); > 73 RF -= f; > 74 total_flow += f; > 75 > 76 for(vert u=D; u!=S; u=prev[u]) > 77 { > 78 total_cost += f * C[prev[u]][u]; > 79 F[prev[u]][u] -= f; > 80 F[u][prev[u]] += f; > 81 } > 82 > 83 // Update the potential > 84 for(vert u=0; u<N; ++u) > 85 h[u] += d[u]; > 86 } > 87 return make_pair(total_cost, total_flow); > 88 } > 89 > 90 class MarblesRegroupingHard { public: > 91 int minMoves(vector <string> boxes) > 92 { > 93 const int NB = boxes.size(); > 94 vector< vector<int> > bb(NB); > 95 for(int i=0; i<boxes.size(); ++i) > 96 { > 97 stringstream sin(boxes[i]); > 98 for(int v; sin>>v; ) > 99 bb[i].push_back(v); > 100 } > 101 const int NC = bb[0].size(); > 102 > 103 graph G(NB+NC+3); > 104 flow_graph F = {}; > 105 cost_graph C = {}; > 106 for(int i=0; i<NB; ++i) { > 107 G[0].push_back(2+i); > 108 G[2+i].push_back(0); > 109 F[0][2+i] = 1; > 110 } > 111 for(int i=0; i<NC; ++i) { > 112 G[2+NB+i].push_back(1); > 113 G[1].push_back(2+NB+i); > 114 F[2+NB+i][1] = 1; > 115 } > 116 { > 117 G[2+NB+NC].push_back(1); > 118 G[1].push_back(2+NB+NC); > 119 F[2+NB+NC][1] = NB-NC; > 120 } > 121 for(int i=0; i<NB; ++i) > 122 for(int j=0; j<NC; ++j) { > 123 G[2+i].push_back(2+NB+j); > 124 G[2+NB+j].push_back(2+i); > 125 F[2+i][2+NB+j] = 1; > 126 int c = 0; > 127 for(int k=0; k<NB; ++k) if(k!=i) > 128 c += bb[k][j]; > 129 C[2+i][2+NB+j] = c; > 130 C[2+NB+j][2+i] = -c; > 131 } > 132 for(int i=0; i<NB; ++i) { > 133 G[2+i].push_back(2+NB+NC); > 134 G[2+NB+NC].push_back(2+i); > 135 F[2+i][2+NB+NC] = 1; > 136 } > 137 pair<cost,flow> ff = mincostFlow(G, F, C, 0, 1); > 138 return ff.first; > 139 } > 140 }; > 141 > 142 // BEGIN CUT HERE > 143 #include <ctime> > 144 double start_time; string timer() > 145 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 146 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 147 { os << "{ "; > 148 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 149 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 150 void verify_case(const int& Expected, const int& Received) { > 151 bool ok = (Expected == Received); > 152 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 153 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 154 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 155 #define END verify_case(_, MarblesRegroupingHard().minMoves(boxes));} > 156 int main(){ > 157 > 158 CASE(0) > 159 string boxes_[] = {"0"}; > 160 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 161 int _ = 0; > 162 END > 163 CASE(1) > 164 string boxes_[] = {"77 97","8 0"}; > 165 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 166 int _ = 77; > 167 END > 168 CASE(2) > 169 string boxes_[] = {"6 97 7","73 45 0","67 45 63"}; > 170 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 171 int _ = 170; > 172 END > 173 CASE(3) > 174 string boxes_[] = {"97 94 0 99","1 72 46 45","0 10 47 75","0 92 76 20"," > 175 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 176 int _ = 559; > 177 END > 178 CASE(4) > 179 string boxes_[] = {"38 0 0 45 77 61 0 0 8 87 65 53 0 2", > 180 "0 97 79 37 70 0 69 35 95 11 75 96 77 29", > 181 "39 32 0 24 63 22 91 71 0 63 92 59 12 0", > 182 "34 0 0 71 88 13 60 56 0 22 29 89 81 53", > 183 "69 95 65 0 94 98 84 37 0 87 16 0 58 18", > 184 "82 0 36 88 0 54 82 40 6 0 34 44 80 2", > 185 "33 58 7 95 83 87 0 0 79 35 0 51 22 84", > 186 "7 0 30 57 33 4 0 26 44 55 67 31 88 42", > 187 "62 58 62 93 91 0 1 0 44 23 95 0 55 57", > 188 "35 8 22 26 76 0 78 54 0 46 42 0 0 64", > 189 "93 54 58 0 89 89 0 0 57 0 98 3 24 0", > 190 "9 0 0 23 82 18 0 46 0 0 94 84 19 18", > 191 "78 12 6 45 0 80 16 69 59 76 35 0 66 0", > 192 "0 68 77 13 15 0 52 72 0 0 18 65 23 0", > 193 "0 0 73 53 0 95 91 44 27 96 85 0 99 85", > 194 "93 29 4 89 27 44 27 17 21 6 0 8 3 91", > 195 "0 46 73 94 60 59 15 50 18 75 74 88 46 93", > 196 "0 0 0 94 93 26 21 83 54 62 0 89 59 77", > 197 "32 98 0 44 34 38 56 20 0 61 29 94 52 57", > 198 "52 60 0 22 0 5 38 0 50 98 12 75 0 81", > 199 "23 0 41 18 36 87 49 32 62 43 22 74 0 97", > 200 "0 0 30 79 16 73 61 0 75 51 64 13 45 0", > 201 "11 0 14 2 0 1 2 16 84 37 95 45 48 33", > 202 "10 0 0 35 0 85 28 76 99 74 76 32 52 8", > 203 "60 0 96 0 95 26 59 13 0 0 44 42 97 48", > 204 "34 7 77 25 91 85 35 78 32 99 7 29 18 15", > 205 "61 50 43 22 42 63 64 50 9 94 42 22 21 33", > 206 "58 0 41 10 16 0 27 67 83 27 14 37 98 47", > 207 "37 60 60 76 71 2 84 32 27 39 82 84 0 94", > 208 "15 98 69 82 36 66 0 97 62 39 0 65 62 67", > 209 "0 41 0 43 0 0 94 0 0 58 0 0 27 33", > 210 "53 90 71 91 85 0 32 86 40 60 11 0 99 28", > 211 "79 62 0 0 79 0 14 62 87 76 35 0 70 0", > 212 "0 40 73 48 0 63 0 0 63 5 30 18 47 51", > 213 "75 6 58 69 33 57 66 0 12 0 46 0 65 10"}; > 214 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 215 int _ = 18618; > 216 END > 217 /* > 218 CASE(5) > 219 string boxes_[] = ; > 220 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 221 int _ = ; > 222 END > 223 CASE(6) > 224 string boxes_[] = ; > 225 vector <string> boxes(boxes_, boxes_+sizeof(boxes_)/sizeof(*boxes_)); > 226 int _ = ; > 227 END > 228 */ > 229 } > 230 // END CUT HERE
Added SRM/394-U/2C.cpp version [d68ad4a9285e9556]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ProperDivisors { public: > 23 int analyzeInterval(int a, int b, int n) > 24 { > 25 int c = 0; > 26 for(int k=2; k+k<=a+b; ++k) > 27 c += cd(a+b, k, n) - cd(a-1, k, n); > 28 return c; > 29 } > 30 > 31 int cd(int x, int k, int n) > 32 { > 33 LL kn = 1; > 34 for(int i=0; i<n; ++i) > 35 if( (kn*=k) > x ) > 36 return x/k - (x>=k ? 1 : 0); > 37 return x/k - x/kn - (x>=k ? 1 : 0); > 38 } > 39 }; > 40 > 41 // BEGIN CUT HERE > 42 #include <ctime> > 43 double start_time; string timer() > 44 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 45 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 46 { os << "{ "; > 47 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 48 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 49 void verify_case(const int& Expected, const int& Received) { > 50 bool ok = (Expected == Received); > 51 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 52 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 53 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 54 #define END verify_case(_, ProperDivisors().analyzeInterval(a, b, n));} > 55 int main(){ > 56 > 57 CASE(0) > 58 int a = 32; > 59 int b = 1; > 60 int n = 3; > 61 int _ = 5; > 62 END > 63 CASE(1) > 64 int a = 1; > 65 int b = 12; > 66 int n = 2; > 67 int _ = 8; > 68 END > 69 CASE(2) > 70 int a = 1000000; > 71 int b = 10000000; > 72 int n = 10; > 73 int _ = 146066338; > 74 END > 75 /* > 76 CASE(3) > 77 int a = ; > 78 int b = ; > 79 int n = ; > 80 int _ = ; > 81 END > 82 CASE(4) > 83 int a = ; > 84 int b = ; > 85 int n = ; > 86 int _ = ; > 87 END > 88 */ > 89 } > 90 // END CUT HERE
Added SRM/396-U/2C.cpp version [c544017c1bc2bc01]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RemovingDigits { public: > 23 string maxNumber(string number, string digits) > 24 { > 25 vector<int> d(9); > 26 for(int i=0; i<digits.size(); ++i) > 27 d[digits[i]-'1'] ++; > 28 vector<int> n(9); > 29 for(int i=0; i<number.size(); ++i) > 30 n[number[i]-'1'] ++; > 31 return maximize(number, 0, n, d); > 32 } > 33 > 34 map<pair< int, vector<int> >, string> memo; > 35 > 36 string maximize(const string& number, int s, vector<int>& n, vector<int> > 37 { > 38 if( s == number.size() ) > 39 return ""; > 40 > 41 pair< int, vector<int> > key(s, d); > 42 if( memo.count(key) ) > 43 return memo[key]; > 44 > 45 char ch = number[s]-'1'; > 46 string cand = ""; > 47 > 48 n[ch]--; > 49 if( n[ch] >= d[ch] ) > 50 { > 51 string c1 = number[s] + maximize(number, s+1, n, d); > 52 cand = max(cand, c1); > 53 } > 54 if( d[ch] > 0 ) > 55 { > 56 d[ch]--; > 57 string c2 = maximize(number, s+1, n, d); > 58 cand = max(cand, c2); > 59 d[ch]++; > 60 } > 61 n[ch]++; > 62 return memo[key] = cand; > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time; string timer() > 69 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 70 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 71 { os << "{ "; > 72 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 73 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 74 void verify_case(const string& Expected, const string& Received) { > 75 bool ok = (Expected == Received); > 76 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 77 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 78 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 79 #define END verify_case(_, RemovingDigits().maxNumber(number, digits));} > 80 int main(){ > 81 > 82 CASE(0) > 83 string number = "12345"; > 84 string digits = "513"; > 85 string _ = "24"; > 86 END > 87 CASE(1) > 88 string number = "112352"; > 89 string digits = "1123"; > 90 string _ = "52"; > 91 END > 92 CASE(2) > 93 string number = "123456654321"; > 94 string digits = "612534"; > 95 string _ = "654321"; > 96 END > 97 CASE(3) > 98 string number = "654321123456"; > 99 string digits = "612534"; > 100 string _ = "654321"; > 101 END > 102 CASE(4) > 103 string number = "2654982765982365"; > 104 string digits = "2345978"; > 105 string _ = "698265265"; > 106 END > 107 /* > 108 CASE(5) > 109 string number = ; > 110 string digits = ; > 111 string _ = ; > 112 END > 113 CASE(6) > 114 string number = ; > 115 string digits = ; > 116 string _ = ; > 117 END > 118 */ > 119 } > 120 // END CUT HERE
Added SRM/397/1A.cpp version [55345db3fdf9a047]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class SortingGame > 19 { > 20 public: > 21 int fewestMoves(vector<int> board, int k) > 22 { > 23 vector<int> sorted(board); > 24 sort(sorted.begin(), sorted.end()); > 25 > 26 vector< vector<int> > Q; > 27 set< vector<int> > V; > 28 Q.push_back(board), V.insert(board); > 29 for(int N=0; !Q.empty(); ++N) > 30 { > 31 vector< vector<int> > Q2; > 32 for(int i=0; i<Q.size(); ++i) > 33 { > 34 vector<int>& b = Q[i]; > 35 if( b == sorted ) > 36 return N; > 37 for(int i=0; i+k<=b.size(); ++i) > 38 { > 39 reverse(b.begin()+i, b.begin()+i+k); > 40 if( !V.count(b) ) > 41 Q2.push_back(b), V.insert(b); > 42 reverse(b.begin()+i, b.begin()+i+k); > 43 } > 44 } > 45 Q.swap(Q2); > 46 } > 47 return -1; > 48 } > 49 > 50 // BEGIN CUT HERE > 51 public: > 52 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 53 private: > 54 template <typename T> string print_array(const vector<T> &V) { ostringst > 55 void verify_case(int Case, const int &Expected, const int &Received) { c > 56 void test_case_0() { int Arr0[] = {1,2,3}; vector <int> Arg0(Arr0, Arr0 > 57 void test_case_1() { int Arr0[] = {3,2,1}; vector <int> Arg0(Arr0, Arr0 > 58 void test_case_2() { int Arr0[] = {5,4,3,2,1}; vector <int> Arg0(Arr0, A > 59 void test_case_3() { int Arr0[] = {3,2,4,1,5}; vector <int> Arg0(Arr0, A > 60 void test_case_4() { int Arr0[] = {7,2,1,6,8,4,3,5}; vector <int> Arg0(A > 61 > 62 // END CUT HERE > 63 }; > 64 // BEGIN CUT HERE > 65 int main() { > 66 SortingGame().run_test(-1); > 67 } > 68 // END CUT HERE
Added SRM/397/1B.cpp version [148a1a19572b802a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class SumOfPowers > 19 { > 20 public: > 21 int value(int n, int k) > 22 { > 23 // 2^m - 1^m = (1+1)^m - 1^m = mC1 1^(m-1) + ... + mCi 1^(m-i) > 24 // 3^m - 2^m = (2+1)^m - 1^m = mC1 2^(m-1) + ... + mCi 2^(m-i) > 25 //+ ... > 26 //-------------------------------------------------------------- > 27 // (n+1)^m - 1 = mC1 \Sigma n^(m-1) + ... + mCi \Sigma n^(m-i) > 28 > 29 vector<LL> vals; > 30 for(int m=1; m<=k+1; ++m) > 31 { > 32 LL x = SUB(POW(n+1, m), 1); > 33 for(int i=2; i<=m; ++i) > 34 x = SUB( x, MUL(C(m, i), vals[m-i]) ); > 35 x = DIV(x, m); > 36 vals.push_back(x); > 37 } > 38 return (int)vals.back(); > 39 } > 40 LL ADD(LL x, LL y) { return (x+y)%1000000007; } > 41 LL SUB(LL x, LL y) { return (x-y+1000000007)%1000000007; } > 42 LL MUL(LL x, LL y) { return (x*y)%1000000007; } > 43 LL C(LL n, LL k) { > 44 LL v = 1; > 45 for(LL i=1; i<=k; ++i) > 46 v = DIV(MUL(v, n-i+1), i); > 47 return v; > 48 } > 49 LL POW(LL x, LL e) { > 50 LL v = 1; > 51 for(;e;x=MUL(x,x),e>>=1) > 52 if(e&1) > 53 v = MUL(v, x); > 54 return v; > 55 } > 56 LL DIV(LL x, LL y) { > 57 LL iy, _; > 58 xgcd(y, 1000000007, &iy, &_); > 59 return MUL(x, (iy+1000000007)%1000000007); > 60 } > 61 LL xgcd(LL a, LL b, LL* x, LL* y) { // ax+by=g > 62 if(b) { > 63 LL yy, g = xgcd(b,a%b,&yy,x); > 64 *y = yy - a/b**x; > 65 return g; > 66 } > 67 else { > 68 *x=1, *y=0; > 69 return a; > 70 } > 71 } > 72 > 73 // BEGIN CUT HERE > 74 public: > 75 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 76 private: > 77 template <typename T> string print_array(const vector<T> &V) { ostringst > 78 void verify_case(int Case, const int &Expected, const int &Received) { c > 79 void test_case_0() { int Arg0 = 5; int Arg1 = 1; int Arg2 = 15; verify_c > 80 void test_case_1() { int Arg0 = 4; int Arg1 = 2; int Arg2 = 30; verify_c > 81 void test_case_2() { int Arg0 = 13; int Arg1 = 5; int Arg2 = 1002001; ve > 82 void test_case_3() { int Arg0 = 123456789; int Arg1 = 1; int Arg2 = 3834 > 83 > 84 // END CUT HERE > 85 }; > 86 // BEGIN CUT HERE > 87 int main() { > 88 SumOfPowers().run_test(-1); > 89 } > 90 // END CUT HERE
Added SRM/397/1C.cpp version [9cb81db248e7a64c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 typedef int vert; > 19 typedef vert edge; > 20 typedef vector<edge> edges; > 21 typedef vector<edges> graph; > 22 > 23 bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) > 24 { > 25 for(int i=0; i<G[v].size(); ++i) { > 26 vert u = G[v][i]; > 27 if( visited[u] ) continue; > 28 visited[u] = true; > 29 > 30 if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) > 31 { matchTo[v]=u, matchTo[u]=v; return true; } > 32 } > 33 return false; > 34 } > 35 > 36 static const int NV=100; > 37 int biMatch( graph& G, int L ) // [0,L):left, [L,?):right > 38 { > 39 vector<vert> matchTo(G.size(), -1); > 40 int ans = 0; > 41 for(vert v=0; v<L; ++v) { > 42 bool visited[NV] = {}; > 43 if( augment(G, v, matchTo, visited) ) > 44 ++ans; > 45 } > 46 return ans; > 47 } > 48 > 49 class HouseProtection > 50 { > 51 public: > 52 double safetyFactor(vector <int> bx, vector <int> by, vector <int> rx, v > 53 { > 54 double r[50][50]; > 55 set<double> rs; > 56 for(int i=0; i<bx.size(); ++i) > 57 for(int j=0; j<rx.size(); ++j) { > 58 r[i][j] = sqrt((double)(bx[i]-rx[j])*(bx[i]-rx[j]) + (by > 59 rs.insert( min<double>(r[i][j], R) ); > 60 } > 61 rs.insert(R); > 62 > 63 double result = 0; > 64 for(set<double>::iterator it=rs.begin(); it!=rs.end(); ++it) > 65 { > 66 double curR = *it; > 67 graph G(bx.size()+rx.size()); > 68 for(int i=0; i<bx.size(); ++i) > 69 for(int j=0; j<rx.size(); ++j) > 70 if( r[i][j] < curR ) { > 71 G[i].push_back( bx.size()+j ); > 72 } > 73 int Num = bx.size() + rx.size() - biMatch( G, bx.size() > 74 result = max(result, Num*curR*curR); > 75 } > 76 return result * M_PI; > 77 } > 78 > 79 // BEGIN CUT HERE > 80 public: > 81 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 82 private: > 83 template <typename T> string print_array(const vector<T> &V) { ostringst > 84 void verify_case(int Case, const double &Expected, const double &Receive > 85 void test_case_0() { int Arr0[] = { 0, 4 }; vector <int> Arg0(Arr0, Arr0 > 86 void test_case_1() { int Arr0[] = { 1 }; vector <int> Arg0(Arr0, Arr0 + > 87 void test_case_2() { int Arr0[] = { 0 }; vector <int> Arg0(Arr0, Arr0 + > 88 void test_case_3() { int Arr0[] = { 23, 29, 29, 35 }; vector <int> Arg0( > 89 > 90 // END CUT HERE > 91 }; > 92 // BEGIN CUT HERE > 93 int main() { > 94 HouseProtection().run_test(-1); > 95 } > 96 // END CUT HERE
Added SRM/398/1A.cpp version [0a8206adef988889]
> 1 struct CountExpressions > 2 { > 3 static int add(int x, int y) { return x+y; } > 4 static int sub(int x, int y) { return x-y; } > 5 static int mul(int x, int y) { return x*y; } > 6 > 7 int calcExpressions(int x, int y, int val) > 8 { > 9 int num[][4] = { > 10 {x,x,y,y}, > 11 {x,y,x,y}, > 12 {x,y,y,x}, > 13 {y,x,x,y}, > 14 {y,x,y,x}, > 15 {y,y,x,x}, > 16 }; > 17 int (*op[])(int,int) = { &add, &sub, &mul }; > 18 > 19 int cnt = 0; > 20 for(int a=0; a<6; ++a) > 21 for(int i=0; i<3; ++i) > 22 for(int j=0; j<3; ++j) > 23 for(int k=0; k<3; ++k) > 24 if( val == op[k]( op[j]( op[i](num[a][0], num[a][1]), nu > 25 ++cnt; > 26 return cnt; > 27 } > 28 };
Added SRM/398/1B.cpp version [5b09cb848fba3930]
> 1 #include <vector> > 2 #include <map> > 3 #include <cstring> > 4 using namespace std; > 5 > 6 > 7 long long dp[51][51][51][51]; // y,x,n,i > 8 > 9 struct CountPaths > 10 { > 11 vector <int> difPaths(int r, int c, vector <int> fieldrow, vector <int> > 12 { > 13 int k = fieldrow.size(); > 14 map< pair<int,int>, int > f; > 15 for(int i=0; i<fieldrow.size(); ++i) > 16 f[make_pair(fieldrow[i],fieldcol[i])] = i; > 17 > 18 memset(dp, 0, sizeof(dp)); > 19 if( f.count( make_pair(1,1) ) ) > 20 dp[1][1][1][ f[make_pair(1,1)]+1 ] = 1; > 21 else > 22 dp[1][1][0][0] = 1; > 23 > 24 for(int y=1; y<=r; ++y) > 25 for(int x=(y==1?2:1); x<=c; ++x) > 26 { > 27 if( f.count(make_pair(y,x)) ) { > 28 int I = f[make_pair(y,x)]; > 29 for(int n=1; n<=k; ++n) { > 30 int sum = 0; > 31 for(int i=0; i<=I; ++i) { > 32 if( y>=2 ) > 33 sum += dp[y-1][x][n-1][i > 34 if( x>=2 ) > 35 sum += dp[y][x-1][n-1][i > 36 } > 37 dp[y][x][n][I+1] = sum % 1000007; > 38 } > 39 } else { > 40 for(int n=0; n<=k; ++n) { > 41 for(int i=0; i<=k; ++i) { > 42 int sum = 0; > 43 if( y>=2 ) > 44 sum += dp[y-1][x][n][i]; > 45 if( x>=2 ) > 46 sum += dp[y][x-1][n][i]; > 47 dp[y][x][n][i] = sum % 1000007; > 48 } > 49 } > 50 } > 51 } > 52 > 53 vector<int> ans; > 54 for(int n=0; n<=k; ++n) { > 55 int sum = 0; > 56 for(int i=0; i<=k; ++i) > 57 sum += dp[r][c][n][i]; > 58 ans.push_back( sum % 1000007 ); > 59 } > 60 return ans; > 61 } > 62 };
Added SRM/398/1C.cpp version [19f42a31025a41f7]
> 1 #include <vector> > 2 #include <string> > 3 #include <cmath> > 4 #include <algorithm> > 5 using namespace std; > 6 > 7 vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector< > 8 { > 9 vector<double> A(V); > 10 for(int i=0; i<n; ++i) > 11 { > 12 // pivot > 13 if( M[i][i] == 0 ) > 14 for(int j=i+1; j<n; ++j) > 15 if( M[j][i] != 0 ) > 16 {swap(M[i], M[j]); swap(A[i], A[j]); bre > 17 if( M[i][i] == 0 ) > 18 throw "no anser"; > 19 > 20 // M[i][i] <-- 1 > 21 double p = M[i][i]; > 22 for(int j=i; j<n; ++j) > 23 M[i][j] /= p; > 24 A[i] /= p; > 25 > 26 // M[*][i] <-- 0 > 27 for(int j=0; j<n; ++j) if(j!=i) > 28 { > 29 double r = M[j][i]; > 30 for(int k=i; k<n; ++k) > 31 M[j][k] -= M[i][k] * r; > 32 A[j] -= A[i] * r; > 33 } > 34 } > 35 return A; > 36 } > 37 > 38 //------------------------------------------------------------- > 39 // Check the given list can be a degree list of some graph > 40 // O(n^2 log n) > 41 // > 42 // Verified by > 43 // - SRM 398 Div1 LV3 > 44 // > 45 //(( > 46 // Havel-Hakimi > 47 // If G[0], ..., G[n] (decreasing) is graphical, > 48 // then G[1]-1, G[2]-1, ..., G[G[0]]-1, G[G[0]+1], .., G[n] > 49 // is also graphical. > 50 //)) > 51 //------------------------------------------------------------- > 52 > 53 bool isGraphical( vector<int> G ) > 54 { > 55 sort( G.begin(), G.end() ); > 56 > 57 vector<int>::iterator b = lower_bound( G.begin(), G.end(), 1 ); > 58 vector<int>::iterator e = G.end(); > 59 > 60 while( b < e ) > 61 { > 62 int n = *(--e); > 63 if( e-b < n ) > 64 return false; > 65 for(vector<int>::iterator i=e-n; i!=e; ++i) > 66 --*i; > 67 inplace_merge( b, e-n, e ); > 68 b = lower_bound( G.begin(), G.end(), 1 ); > 69 } > 70 return true; > 71 } > 72 > 73 struct MyFriends > 74 { > 75 string calcFriends(vector <int> sumFriends, int k) > 76 { > 77 int n = sumFriends.size(); > 78 > 79 vector< vector<double> > M(n, vector<double>(n)); > 80 for(int i=0; i<n; ++i) > 81 for(int j=0; j<n; ++j) > 82 M[i][j] = (i==j || (i+k)%n==j ? 0 : 1); > 83 > 84 // calc #friends of each kid > 85 vector<double> V( sumFriends.begin(), sumFriends.end() ); > 86 vector<double> Ad = solve_linear_eq( n, M, V ); > 87 vector<int> A; > 88 for(int i=0; i<n; ++i) > 89 A.push_back( (int)floor(Ad[i]+0.5) ); > 90 > 91 // verify > 92 for(int i=0; i<n; ++i) > 93 { > 94 int sum = 0; > 95 for(int j=0; j<n; ++j) > 96 sum += (i==j || (i+k)%n==j ? 0 : A[j]); > 97 if( sum != sumFriends[i] ) > 98 return "IMPOSSIBLE"; > 99 } > 100 > 101 return isGraphical(A) ? "POSSIBLE" : "IMPOSSIBLE"; > 102 } > 103 };
Added SRM/399/1A.cpp version [daf9553b63d995fb]
> 1 #include <iostream> > 2 #include <vector> > 3 #include <cmath> > 4 #include <set> > 5 #include <algorithm> > 6 using namespace std; > 7 > 8 > 9 struct AvoidingProduct > 10 { > 11 vector<int> getTriple(vector<int> a, int n) > 12 { > 13 int best = 1000000000; > 14 vector<int> ans(3); > 15 > 16 set<int> as(a.begin(), a.end()); > 17 for(int x=1;; ++x) if( !as.count(x) ) { > 18 for(int y=x;; ++y) if( !as.count(y) ) { > 19 for(int z=y;; ++z) if( !as.count(z) ) { > 20 int m = x*y*z; > 21 if( abs(m-n) < best ) { > 22 best = abs(m-n); > 23 ans[0] = x; > 24 ans[1] = y; > 25 ans[2] = z; > 26 } > 27 > 28 if( x*y*z>n ) break; > 29 } > 30 if( x*y*y>n ) break; > 31 } > 32 if( x*x*x>n ) break; > 33 } > 34 > 35 return ans; > 36 } > 37 };
Added SRM/399/1B.cpp version [bfb46b6941f2d6d4]
> 1 #include <iostream> > 2 #include <string> > 3 #include <map> > 4 using namespace std; > 5 > 6 string err = "222222222222222222222222222222222222222222222222"; > 7 > 8 void operator&=( string& x, const string& r ) > 9 { > 10 if( x.size() > r.size() ) > 11 x = r; > 12 else if( x.size()==r.size() && x > r ) > 13 x = r; > 14 } > 15 > 16 struct BinarySum > 17 { > 18 map<int, string> memo; > 19 > 20 int rearrange(int a, int b, int c) > 21 { > 22 int an=0,bn=0,cn=0,n=0; > 23 for(unsigned int i=1,t=1; i<=a; i<<=1,++t) {if(a&i) an++; n=max< > 24 for(unsigned int i=1,t=1; i<=b; i<<=1,++t) {if(b&i) bn++; n=max< > 25 for(unsigned int i=1,t=1; i<=c; i<<=1,++t) {if(c&i) cn++; n=max< > 26 string ans = err; > 27 ans &= mini( 0, cn-1, an-1, bn ); // 1+0(+0)=01 > 28 ans &= mini( 0, cn-1, an, bn-1 ); // 0+1(+0)=01 > 29 ans &= mini( 1, cn-1, an, bn ); // 0+0(+1)=01 > 30 if( ans.find('2') != string::npos ) > 31 return -1; > 32 ans = "1"+ans; > 33 if( ans.size() > n ) > 34 return -1; > 35 int v = 0; > 36 for(int i=0; i<ans.size(); ++i) > 37 v = (v<<1) | (ans[i] - '0'); > 38 return v; > 39 } > 40 > 41 string mini(int d, int cn, int an, int bn) > 42 { > 43 if( cn<0 || an<0 || bn<0 ) > 44 return err; > 45 > 46 int key = (cn<<24)+(an<<16)+(bn<<8)+d; > 47 if( memo.count(key) ) > 48 return memo[key]; > 49 > 50 if( d==0 && cn==0 && an==0 && bn==0 ) > 51 return ""; > 52 > 53 string ans = err; > 54 if( d == 0 ) { > 55 ans &= "1"+mini( 0, cn-1, an-1, bn ); // 1+0(+0)=01 > 56 ans &= "1"+mini( 0, cn-1, an, bn-1 ); // 0+1(+0)=01 > 57 ans &= "1"+mini( 1, cn-1, an, bn ); // 0+0(+1)=01 > 58 } else { > 59 ans &= "0"+mini( 0, cn, an-1, bn-1 ); // 1+1(+0)=10 > 60 ans &= "0"+mini( 1, cn, an-1, bn ); // 1+0(+1)=10 > 61 ans &= "0"+mini( 1, cn, an, bn-1 ); // 0+1(+1)=10 > 62 ans &= "1"+mini( 1, cn-1, an-1, bn-1 ); // 1+1(+1)=11 > 63 } > 64 return memo[key] = ans; > 65 } > 66 };
Added SRM/399/1C.cpp version [b357fd2040b9449a]
> 1 #include <vector> > 2 #include <string> > 3 #include <iostream> > 4 #include <cstring> > 5 using namespace std; > 6 > 7 > 8 > 9 > 10 static const int NV = 512; > 11 typedef int flow; > 12 typedef int vert; > 13 typedef vert edge; > 14 typedef vector<edge> edges; > 15 typedef vector<edges> graph; > 16 typedef flow flow_graph[NV][NV]; > 17 > 18 flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, > 19 int LV[], flow flow_in, int blocked[] ) > 20 { > 21 flow flow_out = 0; > 22 for(int i=0; i!=G[v].size(); ++i) > 23 { > 24 int u = G[v][i]; > 25 if( LV[v]+1==LV[u] && F[v][u] ) > 26 { > 27 flow f = min(flow_in-flow_out, F[v][u]); > 28 if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blo > 29 { > 30 F[v][u] -= f; > 31 F[u][v] += f; > 32 flow_out += f; > 33 if( flow_in == flow_out ) return flow_out; > 34 } > 35 } > 36 } > 37 blocked[v] = (flow_out==0); > 38 return flow_out; > 39 } > 40 > 41 flow dinic( graph& G, flow_graph F, vert S, vert D ) > 42 { > 43 for( flow total=0 ;; ) { > 44 int LV[NV] = {0}; > 45 vector<int> Q(1, S); > 46 for(int lv=1; !Q.empty(); ++lv) > 47 { > 48 vector<int> Q2; > 49 for(int i=0; i!=Q.size(); ++i) > 50 { > 51 edges& ne = G[Q[i]]; > 52 for(int j=0; j!=ne.size(); ++j) > 53 if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j > 54 LV[ne[j]]=lv, Q2.push_back(ne[j] > 55 } > 56 Q.swap(Q2); > 57 } > 58 > 59 if( !LV[D] ) > 60 return total; > 61 > 62 int blocked[NV] = {}; > 63 total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); > 64 } > 65 } > 66 > 67 > 68 > 69 struct DancingParty > 70 { > 71 int maxDances(vector<string> likes, int k) > 72 { > 73 int n = likes.size(); > 74 #define SRC 0 > 75 #define DST 1 > 76 #define L(i) 2+n*0+i > 77 #define R(i) 2+n*1+i > 78 #define YL(i) 2+n*2+i > 79 #define NL(i) 2+n*3+i > 80 #define YR(i) 2+n*4+i > 81 #define NR(i) 2+n*5+i > 82 > 83 graph G(2+n*6); > 84 > 85 // Graph > 86 for(int i=0; i<n; ++i) { > 87 G[SRC ].push_back(L(i)); G[L(i) ].push_back(SRC); > 88 G[L(i)].push_back(YL(i)); G[YL(i)].push_back(L(i)); > 89 G[L(i)].push_back(NL(i)); G[NL(i)].push_back(L(i)); > 90 > 91 G[DST ].push_back(R(i)); G[R(i) ].push_back(DST); > 92 G[R(i)].push_back(YR(i)); G[YR(i)].push_back(R(i)); > 93 G[R(i)].push_back(NR(i)); G[NR(i)].push_back(R(i)); > 94 > 95 for(int j=0; j<n; ++j) > 96 if( likes[i][j] == 'Y' ) > 97 G[YL(i)].push_back(YR(j)), G[YR(j)].push > 98 else > 99 G[NL(i)].push_back(NR(j)), G[NR(j)].push > 100 } > 101 > 102 for(int M=1; M<=n; ++M) { > 103 // Flow > 104 flow_graph F = {}; > 105 for(int i=0; i<n; ++i) { > 106 F[SRC][L(i)] = M; > 107 F[L(i)][YL(i)] = M; > 108 F[L(i)][NL(i)] = k; > 109 > 110 F[R(i)][DST] = M; > 111 F[YR(i)][R(i)] = M; > 112 F[NR(i)][R(i)] = k; > 113 > 114 for(int j=0; j<n; ++j) > 115 if( likes[i][j] == 'Y' ) > 116 F[YL(i)][YR(j)] = 1; > 117 else > 118 F[NL(i)][NR(j)] = 1; > 119 } > 120 > 121 // Maxflow > 122 if( dinic(G, F, SRC, DST) < M*n ) > 123 return M-1; > 124 } > 125 return n; > 126 } > 127 };
Added SRM/400/1A.cpp version [8c5bdf5260c77024]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class StrongPrimePower > 20 { > 21 public: > 22 vector <int> baseAndExponent(string n) > 23 { > 24 LL N; > 25 stringstream(n) >> N; > 26 > 27 LL sN = (LL) sqrt( (double)N ); > 28 if( sN*sN == N && isPrime(sN) ) { > 29 vector<int> ans; > 30 ans.push_back(sN); > 31 ans.push_back(2); > 32 return ans; > 33 } > 34 if( (sN+1)*(sN+1) == N && isPrime(sN+1) ) { > 35 vector<int> ans; > 36 ans.push_back(sN+1); > 37 ans.push_back(2); > 38 return ans; > 39 } > 40 > 41 for(LL p=2; p*p*p<=N; ++p) > 42 if( N%p==0 ) { > 43 LL q=0; > 44 while(N%p==0) > 45 N /= p, ++q; > 46 if( N==1 ) { > 47 vector<int> ans; > 48 ans.push_back(p); > 49 ans.push_back(q); > 50 return ans; > 51 } > 52 break; > 53 } > 54 return vector<int>(); > 55 } > 56 > 57 bool isPrime(LL N) { > 58 for(LL p=2; p*p<=N; ++p) > 59 if(N%p==0) > 60 return false; > 61 return true; > 62 } > 63 }; > 64 > 65 // BEGIN CUT HERE > 66 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 67 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 68 > 69 template<int N> struct Case_ {}; > 70 char Test_(...); > 71 int Test_(Case_<0>) { > 72 string n = "27"; > 73 int RetVal_[] = {3, 3 }; > 74 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 75 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 76 int Test_(Case_<1>) { > 77 string n = "10"; > 78 vector <int> RetVal; > 79 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 80 int Test_(Case_<2>) { > 81 string n = "7"; > 82 vector <int> RetVal; > 83 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 84 int Test_(Case_<3>) { > 85 string n = "1296"; > 86 vector <int> RetVal; > 87 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 88 int Test_(Case_<4>) { > 89 string n = "576460752303423488"; > 90 int RetVal_[] = {2, 59 }; > 91 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 92 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 93 int Test_(Case_<5>) { > 94 string n = "999999874000003969"; > 95 int RetVal_[] = {999999937, 2 }; > 96 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 97 return verify_case(RetVal, StrongPrimePower().baseAndExponent(n)); } > 98 > 99 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 100 template<> void Run_<-1>() {} > 101 int main() { Run_<0>(); } > 102 // END CUT HERE > 103
Added SRM/400/1B.cpp version [a8db06433cf2bbb2]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 #include <bitset> > 19 > 20 static const int INF = 3000; > 21 class ReversalChain > 22 { > 23 public: > 24 map<pair<pair<LL,LL>,int>,int> memo; > 25 int minReversal(string init, string goal) > 26 { > 27 LL s=0, sb=0; for(int i=0; i<init.size(); ++i) if(init[i]=='1') > 28 LL g=0, gb=0; for(int i=0; i<goal.size(); ++i) if(goal[i]=='1') > 29 if( sb != gb ) > 30 return -1; > 31 int a = mrev(s, g, init.size()); > 32 return a>=INF ? -1 : a; > 33 } > 34 int mrev(LL s, LL g, int m) > 35 { > 36 #define mask(x) ((x) & (1LL<<(m))-1) > 37 #define ith(x,i) (((x)>>(i))&1) > 38 > 39 if( mask(s) == mask(g) ) > 40 return 0; > 41 > 42 pair<pair<LL,LL>,int> key(make_pair(mask(s),mask(g)),m); > 43 if( memo.count(key) ) > 44 return memo[key]; > 45 > 46 int a = INF; > 47 if( ith(s,m-1)==ith(g,m-1) ) a = min(a, mrev(s, g, m-1)); > 48 if( ith(s,0)==ith(g,0) ) a = min(a, mrev(s>>1, g>>1, m-1)); > 49 > 50 s = bitrev(s,m); > 51 if( ith(s,m-1)==ith(g,m-1) ) a = min(a, 1+mrev(s, g, m-1)); > 52 if( ith(s,0)==ith(g,0) ) a = min(a, 1+mrev(s>>1, g>>1, m-1)) > 53 > 54 return memo[key] = a; > 55 } > 56 LL bitrev(LL x, LL m) > 57 { > 58 LL y = 0; > 59 for(int i=0; i<m; ++i) > 60 y |= (((x>>i)&1) << (m-1-i)); > 61 return y; > 62 } > 63 }; > 64 > 65 // BEGIN CUT HERE > 66 #include <ctime> > 67 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 68 > 69 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 70 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 71 > 72 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 73 char Test_(...); > 74 int Test_(Case_<0>) { > 75 string init = "1100"; > 76 string goal = "0110"; > 77 int RetVal = 1; > 78 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 79 int Test_(Case_<1>) { > 80 string init = "111000"; > 81 string goal = "101010"; > 82 int RetVal = 2; > 83 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 84 int Test_(Case_<2>) { > 85 string init = "0"; > 86 string goal = "1"; > 87 int RetVal = -1; > 88 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 89 int Test_(Case_<3>) { > 90 string init = "10101"; > 91 string goal = "10101"; > 92 int RetVal = 0; > 93 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 94 int Test_(Case_<4>) { > 95 string init = "111000111000"; > 96 string goal = "001100110011"; > 97 int RetVal = 4; > 98 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 99 int Test_(Case_<5>) { > 100 string init = "001000011000000111000000001111"; > 101 string goal = "100100100100100100100100100100"; > 102 int RetVal = 9; > 103 return verify_case(RetVal, ReversalChain().minReversal(init, goal)); } > 104 > 105 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 106 template<> void Run_<-1>() {} > 107 int main() { Run_<0>(); } > 108 // END CUT HERE > 109
Added SRM/400/1C.cpp version [9f9946fdf86740c4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CollectingBonuses { public: > 23 double expectedBuy(string n, string k) > 24 { > 25 LL N; {stringstream ss; ss<<n; ss>>N;} > 26 LL K; {stringstream ss; ss<<k; ss>>K;} > 27 return N * sigma(K,N); > 28 } > 29 double f(LL b) > 30 { > 31 return 0.5772156649015313 + log(b+0.5); > 32 } > 33 double sigma(LL k, LL n) > 34 { > 35 // compute (1/n-k+1 + ... + 1/n) > 36 > 37 if(k<=10000000) { > 38 double p=0.0; > 39 for(LL i=0; i<k; ++i) > 40 p += 1.0 / (n-i); > 41 return p; > 42 } > 43 else if(n-k<=10000000) { > 44 double p=0.0; > 45 for(LL i=1; i<=n-k; ++i) > 46 p += 1.0 / i; > 47 return f(n) - p; > 48 } > 49 LL x = 2*n+1; > 50 LL y = 2*n-2*k+1; > 51 if( x<y*2 ) > 52 return log1p(double(x-y)/y); > 53 else > 54 return log( double(x)/y ); > 55 } > 56 > 57 }; > 58 > 59 // BEGIN CUT HERE > 60 #include <ctime> > 61 double start_time; string timer() > 62 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 63 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 64 { os << "{ "; > 65 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 66 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 67 void verify_case(const double& Expected, const double& Received) { > 68 bool ok = (abs(Expected - Received) < 1e-9); > 69 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 70 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 71 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 72 #define END verify_case(_, CollectingBonuses().expectedBuy(n, k));} > 73 int main(){ > 74 > 75 CASE(0) > 76 string n = "1"; > 77 string k = "1"; > 78 double _ = 1.0; > 79 END > 80 CASE(1) > 81 string n = "2"; > 82 string k = "1"; > 83 double _ = 1.0; > 84 END > 85 CASE(2) > 86 string n = "2"; > 87 string k = "2"; > 88 double _ = 3.0; > 89 END > 90 CASE(3) > 91 string n = "4"; > 92 string k = "3"; > 93 double _ = 4.333333333333333; > 94 END > 95 CASE(4) > 96 string n = "999999999999999999"; > 97 string k = "999999999999999999"; > 98 double _ = 4.202374733879435E19; > 99 END > 100 /* > 101 CASE(5) > 102 string n = ; > 103 string k = ; > 104 double _ = ; > 105 END > 106 */ > 107 } > 108 // END CUT HERE
Added SRM/424/1A-2B.cpp version [28d343930b8a6ef8]
Added SRM/424/1B.cpp version [bc1042fb4b67d356]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 int memo[1001][1001]; > 19 > 20 class StrengthOrIntellect > 21 { > 22 public: > 23 int numberOfMissions(vector <int> strength, vector <int> intellect, vect > 24 { > 25 memset(memo, 0xff, sizeof(memo)); > 26 return rec(1, 1, strength, intellect, points); > 27 } > 28 > 29 int rec(int s, int i, vector<int>& strength, vector<int>& intellect, vec > 30 { > 31 if( memo[s][i] >= 0 ) > 32 return memo[s][i]; > 33 > 34 int pt = 2-s-i, cnt=0; > 35 for(int j=0; j<strength.size(); ++j) > 36 if( strength[j]<=s || intellect[j]<=i ) > 37 pt+=points[j], ++cnt; > 38 > 39 for(int ds=0; ds<=pt; ++ds) { > 40 int ss = min(1000,s + ds); > 41 int ii = min(1000,i + pt-ds); > 42 if( ss>s || ii>i ) > 43 cnt = max(cnt, rec(ss, ii, strength, intellect, > 44 } > 45 return memo[s][i] = cnt; > 46 } > 47 > 48 // BEGIN CUT HERE > 49 public: > 50 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 51 private: > 52 template <typename T> string print_array(const vector<T> &V) { ostringst > 53 void verify_case(int Case, const int &Expected, const int &Received) { c > 54 void test_case_0() { int Arr0[] = {1, 2}; vector <int> Arg0(Arr0, Arr0 + > 55 void test_case_1() { int Arr0[] = {3}; vector <int> Arg0(Arr0, Arr0 + (s > 56 void test_case_2() { int Arr0[] = {1, 3, 1, 10, 3}; vector <int> Arg0(Ar > 57 void test_case_3() { int Arr0[] = {1, 2, 100, 5, 100, 10, 100, 17, 100}; > 58 void test_case_4() { int Arr0[] = {1, 10, 1, 2, 16, 12, 13, 19, 12, 8}; > 59 > 60 // END CUT HERE > 61 }; > 62 // BEGIN CUT HERE > 63 int main() { StrengthOrIntellect().run_test(-1); } > 64 // END CUT HERE
Added SRM/424/1C.cpp version [b256d59b02db33a5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 template<typename T = LL> > 19 struct FenwickTree > 20 { > 21 vector<T> x; > 22 FenwickTree(size_t n, const T& v = T()) : x(n, v) {} > 23 > 24 void incr( int k, const T& a ) { // z[k] += a; > 25 for(; k < x.size(); k|=k+1) > 26 x[k] += a; > 27 } > 28 > 29 T sum(int i, int j) { // z[i]+...+z[j] : inclusive > 30 if(i) > 31 return sum(0, j) - sum(0, i-1); > 32 else { > 33 T v = T(); > 34 for(; j>=0; j=(j&(j+1))-1) > 35 v += x[j]; > 36 return v; > 37 } > 38 } > 39 }; > 40 > 41 static const int MODVAL = 1000000007; > 42 > 43 class ProductOfPrices > 44 { > 45 public: > 46 int product(int N, int L, int X0, int A, int B) > 47 { > 48 LL result = 1; > 49 > 50 FenwickTree<> fw(L), nm(L); > 51 > 52 LL X = X0 % L; > 53 fw.incr(X, X); > 54 nm.incr(X, 1); > 55 for(int i=1; i<N; ++i) > 56 { > 57 X = (X*A+B) % L; > 58 > 59 LL sumL = fw.sum(0, X-1); > 60 LL sumR = fw.sum(X, L-1); > 61 LL numL = nm.sum(0, X-1); > 62 LL numR = nm.sum(X, L-1); > 63 > 64 LL price = ((X*numL - sumL) + (sumR - X*numR)) % MODVAL; > 65 result = (result * price) % MODVAL; > 66 > 67 fw.incr(X, X); > 68 nm.incr(X, 1); > 69 } > 70 return result; > 71 } > 72 > 73 // BEGIN CUT HERE > 74 public: > 75 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 76 private: > 77 template <typename T> string print_array(const vector<T> &V) { ostringst > 78 void verify_case(int Case, const int &Expected, const int &Received) { c > 79 void test_case_0() { int Arg0 = 5; int Arg1 = 10; int Arg2 = 3; int Arg3 > 80 void test_case_1() { int Arg0 = 3; int Arg1 = 20; int Arg2 = 5; int Arg3 > 81 void test_case_2() { int Arg0 = 4; int Arg1 = 21; int Arg2 = 1; int Arg3 > 82 void test_case_3() { int Arg0 = 10; int Arg1 = 100; int Arg2 = 4; int Ar > 83 void test_case_4() { int Arg0 = 5; int Arg1 = 200000; int Arg2 = 9999999 > 84 > 85 // END CUT HERE > 86 }; > 87 // BEGIN CUT HERE > 88 int main() { ProductOfPrices().run_test(-1); } > 89 // END CUT HERE
Added SRM/424/2A.cpp version [d8235f29f64235d2]
> 1 #include <string> > 2 #include <stack> > 3 using namespace std; > 4 > 5 struct MagicSpell > 6 { > 7 string fixTheSpell( string spell ) > 8 { > 9 stack<char> s; > 10 for(int i=0; i<spell.size(); ++i) > 11 if( spell[i]=='A' || spell[i]=='Z' ) > 12 s.push( spell[i] ); > 13 for(int i=0; i<spell.size(); ++i) > 14 if( spell[i]=='A' || spell[i]=='Z' ) > 15 spell[i]=s.top(), s.pop(); > 16 return spell; > 17 } > 18 };
Added SRM/424/2C.cpp version [8b2db66f69d32595]
> 1 #include <vector> > 2 #include <string> > 3 #include <set> > 4 using namespace std; > 5 > 6 struct BestRoads > 7 { > 8 vector<int> numberOfRoads(vector<string> roads, int M) > 9 { > 10 int N = roads.size(); > 11 vector<int> ans(N); > 12 > 13 // Kruskal > 14 typedef pair<int,int> road; > 15 set<road> Q; > 16 for(int i=0; i<N; ++i) > 17 for(int j=i+1; j<N; ++j) > 18 if( roads[i][j]=='Y' ) > 19 Q.insert( road(i,j) ); > 20 > 21 vector<int> p(N,-1), s(N, 1); // Union-Find > 22 int div=N, red=M-N+1; > 23 > 24 for(set<road>::iterator it=Q.begin(); it!=Q.end(); ++it) > 25 { > 26 road r = *it; > 27 int a=r.first; while(p[a]!=-1) a=p[a]; > 28 int b=r.second; while(p[b]!=-1) b=p[b]; > 29 if( a != b ) > 30 { > 31 ans[r.first]++, ans[r.second]++, div--; > 32 if(s[a]<s[b]) p[a]=b, s[b]+=s[a]; > 33 else p[b]=a, s[a]+=s[b]; > 34 } > 35 else if( red ) > 36 ans[r.first]++, ans[r.second]++, red--; > 37 } > 38 > 39 return (red==0 && div==1) ? ans : vector<int>(); > 40 } > 41 };
Added SRM/425/1A.cpp version [637ed2e24dcc1d56]
> 1 #include <vector> > 2 #include <string> > 3 #include <algorithm> > 4 #include <map> > 5 #include <set> > 6 using namespace std; > 7 > 8 typedef pair<int,int> point; > 9 > 10 class CrazyBot > 11 { > 12 public: > 13 double getProbability(int n, int east, int west, int south, int north) > 14 { > 15 set<point> vis; > 16 vis.insert( point(0,0) ); > 17 vis.insert( point(0,1) ); > 18 return rec(vis, point(0,1), n-1, east, west, south, north); > 19 } > 20 > 21 double rec( set<point>& vis, point p, int N, int e, int w, int s, int n) > 22 { > 23 if( N == 0 ) > 24 return 1; > 25 int dx[] = {0,0,1,-1}; > 26 int dy[] = {1,-1,0,0}; > 27 int pr[] = {e,w,s,n}; > 28 double ans = 0; > 29 for(int i=0; i<4; ++i) > 30 { > 31 point q( p.first+dx[i], p.second+dy[i] ); > 32 if( !vis.count(q) ) > 33 { > 34 vis.insert(q); > 35 ans += double(pr[i])/100 * rec(vis, q, N-1, e, w > 36 vis.erase(q); > 37 } > 38 } > 39 return ans; > 40 } > 41 };
Added SRM/425/1B.cpp version [e714e7afac0b764b]
> 1 #include <vector> > 2 #include <string> > 3 #include <algorithm> > 4 #include <set> > 5 #include <iostream> > 6 using namespace std; > 7 > 8 class PiecesMover > 9 { > 10 public: > 11 static bool is_connected(int cur) > 12 { > 13 int py[5], px[5], pn=0; > 14 for(int j=0; j<25; ++j) > 15 if( cur & (1<<j) ) > 16 py[pn]=j/5, px[pn]=j%5, pn++; > 17 > 18 int uf[5] = {0,1,2,3,4}, conn = pn; > 19 > 20 for(int i=0; i<pn; ++i) > 21 for(int j=i+1; j<pn; ++j) > 22 if( px[i]==px[j] && abs(py[i]-py[j])==1 > 23 || py[i]==py[j] && abs(px[i]-px[j])==1 ) { > 24 int a=i; while(uf[a]!=a) a=uf[a]; > 25 int b=j; while(uf[b]!=b) b=uf[b]; > 26 if(a!=b) { uf[a]=b, --conn; } > 27 } > 28 return conn==1; > 29 } > 30 > 31 int getMinimumMoves(vector <string> board) > 32 { > 33 int start = 0; > 34 for(int i=0; i<25; ++i) > 35 if( board[i/5][i%5]=='*' ) > 36 start |= (1<<i); > 37 > 38 vector<bool> visited(1<<25); visited[start] = false; > 39 vector<int> Q(1, start); > 40 int step = 0; > 41 for(;; ++step) > 42 { > 43 vector<int> Q2; > 44 for(int i=0; i!=Q.size(); ++i) > 45 { > 46 int cur = Q[i]; > 47 if( is_connected(cur) ) > 48 return step; > 49 > 50 // next > 51 int dx[] = {1,-1,0,0}; > 52 int dy[] = {0,0,1,-1}; > 53 for(int j=0; j<25; ++j) > 54 if( cur & (1<<j) ) > 55 for(int k=0; k<4; ++k) > 56 { > 57 int jy=j/5+dy[k], jx=j%5 > 58 if(0<=jy && jy<5 && 0<=j > 59 int next = 1 << > 60 if( !(cur&next) > 61 next = c > 62 if( !vis > 63 > 64 > 65 } > 66 } > 67 } > 68 } > 69 } > 70 Q.swap(Q2); > 71 } > 72 return -1; > 73 } > 74 }; > 75 > 76 int main() > 77 { > 78 vector<string> x; > 79 x.push_back("....."); > 80 x.push_back("..**."); > 81 x.push_back("....."); > 82 x.push_back("...*."); > 83 x.push_back("....."); > 84 cout << PiecesMover().getMinimumMoves(x) << endl;; > 85 }
Added SRM/425/1C.cpp version [bd2409ddf241ebbe]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RoadsOfKingdom { public: > 23 vector< vector<double> > P; > 24 > 25 double getProbability(vector <string> roads) > 26 { > 27 P = read_input(roads); > 28 memo = vector<double>(1<<P.size(), -1); > 29 memo0 = vector<double>(P.size()<<P.size(), -1); > 30 memo1 = vector<double>(P.size()<<P.size(), -1); > 31 return prob_tree( (1<<P.size())-1 ); > 32 } > 33 > 34 vector< vector<double> > read_input(const vector<string>& R) > 35 { > 36 int N = R.size(); > 37 vector< vector<double> > P(N, vector<double>(N)); > 38 for(int i=0; i<N; ++i) > 39 for(int j=0; j<N; ++j) > 40 P[i][j] = (R[i][j] - '0') / 8.0; > 41 return P; > 42 } > 43 > 44 vector<double> memo; > 45 double prob_tree(int S) > 46 { > 47 if( memo[S] >= 0 ) > 48 return memo[S]; > 49 > 50 // the first and the second smallest IDs in S > 51 int v, w; > 52 for(v=0; (1<<v)<=S; ++v) if( S & 1<<v ) break; > 53 for(w=v+1; (1<<w)<=S; ++w) if( S & 1<<w ) break; > 54 > 55 // if |S| < 2 then it always forms a tree > 56 if( (1<<w) > S ) > 57 return memo[S] = 1.0; > 58 > 59 // Let's consider v as the "root node" of S, and try all possibl > 60 // The situation is (other nodes)=v-(T where w in T) > 61 double p = 0.0; > 62 for(int T=S; T; T=(T-1)&S) > 63 if( (T & 1<<w) && !(T & 1<<v) ) > 64 p += prob_tree(T) * prob_tree(S&~T) * prob_separ > 65 return memo[S] = p; > 66 } > 67 > 68 double prob_separated(int S1, int S2) > 69 { > 70 double p = 1.0; > 71 for(int v=0; (1<<v)<=S1; ++v) if( S1 & 1<<v ) > 72 p *= prob_zero(v, S2); > 73 return p; > 74 } > 75 > 76 vector<double> memo0; > 77 double prob_zero(int v, int S) // 0 connection between v and S > 78 { > 79 const int key = v<<P.size() | S; > 80 if( memo0[key] >= 0 ) return memo0[key]; > 81 > 82 double p = 1.0; > 83 for(int u=0; (1<<u)<=S; ++u) if( S & 1<<u ) > 84 p *= 1.0 - P[v][u]; > 85 return memo0[key] = p; > 86 } > 87 > 88 vector<double> memo1; > 89 double prob_one(int v, int S) // exactly 1 connection between v and S > 90 { > 91 const int key = v<<P.size() | S; > 92 if( memo1[key] >= 0 ) return memo1[key]; > 93 > 94 double p = 0.0; > 95 for(int c=0; (1<<c)<=S; ++c) if( S & 1<<c ) > 96 { > 97 double q = 1.0; > 98 for(int u=0; (1<<u)<=S; ++u) if( S & 1<<u ) > 99 q *= u==c ? P[v][u] : 1-P[v][u]; > 100 p += q; > 101 } > 102 return memo1[key] = p; > 103 } > 104 }; > 105 > 106 // BEGIN CUT HERE > 107 #include <ctime> > 108 double start_time; string timer() > 109 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 110 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 111 { os << "{ "; > 112 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 113 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 114 void verify_case(const double& Expected, const double& Received) { > 115 bool ok = (abs(Expected - Received) < 1e-9); > 116 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 117 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 118 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 119 #define END verify_case(_, RoadsOfKingdom().getProbability(roads));} > 120 int main(){ > 121 > 122 CASE(0) > 123 string roads_[] = {"04", > 124 "40"}; > 125 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 126 double _ = 0.5; > 127 END > 128 CASE(1) > 129 string roads_[] = {"08", > 130 "80"}; > 131 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 132 double _ = 1.0; > 133 END > 134 CASE(2) > 135 string roads_[] = {"00", > 136 "00"}; > 137 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 138 double _ = 0.0; > 139 END > 140 CASE(3) > 141 string roads_[] = {"088", > 142 "808", > 143 "880"}; > 144 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 145 double _ = 0.0; > 146 END > 147 CASE(4) > 148 string roads_[] = {"044", > 149 "404", > 150 "440"}; > 151 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 152 double _ = 0.375; > 153 END > 154 CASE(5) > 155 string roads_[] = {"0701", > 156 "7071", > 157 "0708", > 158 "1180"}; > 159 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 160 double _ = 0.622314453125; > 161 END > 162 CASE(6) > 163 string roads_[] = {"0622100102300020", "6070000107208008", "270020701011 > 164 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 165 double _ = 5.519032471358341E-5; > 166 END > 167 /* > 168 CASE(7) > 169 string roads_[] = ; > 170 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 171 double _ = ; > 172 END > 173 */ > 174 } > 175 // END CUT HERE
Added SRM/425/2C.cpp version [f7ec079893dd31a3]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PiecesMover { public: > 23 typedef vector< pair<int,int> > state; > 24 int getMinimumMoves(vector <string> board) > 25 { > 26 set<state> V; > 27 vector<state> Q(1, to_state(board)); > 28 V.insert( Q.front() ); > 29 for(int step=0; !Q.empty(); ++step) > 30 { > 31 vector<state> Q_next; > 32 for(int i=0; i<Q.size(); ++i) > 33 { > 34 const state& b = Q[i]; > 35 if( is_connected(b) ) > 36 return step; > 37 const vector<state>& next = next_states(b); > 38 for(int j=0; j<next.size(); ++j) > 39 if( V.insert(next[j]).second ) > 40 Q_next.push_back(next[j]); > 41 } > 42 Q.swap(Q_next); > 43 } > 44 assert(false); > 45 } > 46 > 47 state to_state( const vector<string>& b ) > 48 { > 49 state s; > 50 for(int y=0; y<5; ++y) > 51 for(int x=0; x<5; ++x) > 52 if( b[y][x] == '*' ) > 53 s.push_back( make_pair(y,x) ); > 54 sort( s.begin(), s.end() ); > 55 return s; > 56 } > 57 > 58 bool is_connected( const state& s ) > 59 { > 60 int N = s.size(); > 61 vector< vector<int> > G(N, vector<int>(N)); > 62 for(int i=0; i<N; ++i) > 63 for(int j=0; j<N; ++j) > 64 if( i == j ) > 65 G[i][j] = 0; > 66 else > 67 G[i][j] = (abs(s[i].first-s[j].first)+ab > 68 for(int k=0; k<N; ++k) > 69 for(int i=0; i<N; ++i) > 70 for(int j=0; j<N; ++j) > 71 G[i][j] = min(G[i][j], G[i][k]+G[k][j]); > 72 for(int i=0; i<N; ++i) > 73 for(int j=0; j<N; ++j) > 74 if( G[i][j] > N ) > 75 return false; > 76 return true; > 77 } > 78 > 79 vector<state> next_states( const state& s ) > 80 { > 81 vector<state> result; > 82 for(int i=0; i<s.size(); ++i) > 83 for(int d=0; d<4; ++d) > 84 { > 85 set< pair<int,int> > x; > 86 for(int k=0; k<s.size(); ++k) > 87 if( i != k ) > 88 x.insert( s[k] ); > 89 else > 90 { > 91 static const int dy[] = {-1,+1,0 > 92 static const int dx[] = {0,0,-1, > 93 int ny = s[k].first+dy[d]; > 94 int nx = s[k].second+dx[d]; > 95 if( 0<=ny && ny<5 && 0<=nx && nx > 96 x.insert( make_pair(ny, > 97 } > 98 if( x.size() == s.size() ) > 99 result.push_back( state(x.begin(), x.end > 100 } > 101 return result; > 102 } > 103 }; > 104 > 105 // BEGIN CUT HERE > 106 #include <ctime> > 107 double start_time; string timer() > 108 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 109 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 110 { os << "{ "; > 111 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 112 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 113 void verify_case(const int& Expected, const int& Received) { > 114 bool ok = (Expected == Received); > 115 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 116 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 117 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 118 #define END verify_case(_, PiecesMover().getMinimumMoves(board));} > 119 int main(){ > 120 > 121 CASE(0) > 122 string board_[] = {".....", > 123 "..**.", > 124 ".....", > 125 "...*.", > 126 "....."} > 127 ; > 128 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 129 int _ = 1; > 130 END > 131 CASE(1) > 132 string board_[] = {".....", > 133 ".....", > 134 ".**..", > 135 ".*...", > 136 "**..."} > 137 ; > 138 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 139 int _ = 0; > 140 END > 141 CASE(2) > 142 string board_[] = {"*...*", > 143 ".....", > 144 ".....", > 145 ".....", > 146 "*...*"} > 147 ; > 148 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 149 int _ = 12; > 150 END > 151 /* > 152 CASE(3) > 153 string board_[] = ; > 154 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 155 int _ = ; > 156 END > 157 CASE(4) > 158 string board_[] = ; > 159 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 160 int _ = ; > 161 END > 162 */ > 163 } > 164 // END CUT HERE
Added SRM/426-U/1A.cpp version [4232481aaf925462]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class ShufflingMachine > 19 { > 20 public: > 21 double stackDeck(vector <int> shuffle, int maxShuffles, vector <int> car > 22 { > 23 set<int> my( cardsReceived.begin(), cardsReceived.end() ); > 24 > 25 vector<int> cur(shuffle.size()); > 26 for(int i=0; i<cur.size(); ++i) > 27 cur[i] = i; > 28 > 29 vector<double> prob(shuffle.size()); > 30 for(int N=1; N<=maxShuffles; ++N) > 31 { > 32 for(int i=0; i<cur.size(); ++i) > 33 cur[i] = shuffle[cur[i]]; > 34 > 35 for(int i=0; i<cur.size(); ++i) > 36 if( my.count(cur[i]) ) > 37 prob[i] += 1.0 / maxShuffles; > 38 } > 39 > 40 sort( prob.rbegin(), prob.rend() ); > 41 return accumulate( prob.begin(), prob.begin()+K, 0.0 ); > 42 } > 43 > 44 // BEGIN CUT HERE > 45 public: > 46 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 47 private: > 48 template <typename T> string print_array(const vector<T> &V) { ostringst > 49 void verify_case(int Case, const double &Expected, const double &Receive > 50 void test_case_0() { int Arr0[] = {1,0}; vector <int> Arg0(Arr0, Arr0 + > 51 void test_case_1() { int Arr0[] = {1,2,0}; vector <int> Arg0(Arr0, Arr0 > 52 void test_case_2() { int Arr0[] = {1,2,0,4,3}; vector <int> Arg0(Arr0, A > 53 void test_case_3() { int Arr0[] = {0,4,3,5,2,6,1}; vector <int> Arg0(Arr > 54 void test_case_4() { int Arr0[] = {3,4,7,2,8,5,6,1,0,9}; vector <int> Ar > 55 > 56 // END CUT HERE > 57 }; > 58 // BEGIN CUT HERE > 59 int main() { > 60 ShufflingMachine().run_test(-1); > 61 } > 62 // END CUT HERE
Added SRM/426-U/1B.cpp version [0e4f7f13c5c792cd]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class CatchTheMice > 19 { > 20 public: > 21 double intersect(double b1, double a1, double b2, double a2) > 22 { > 23 // a1x + b1 == a2x + b2 > 24 if( a1 == a2 ) return -1; > 25 return (b2-b1) / (a1-a2); > 26 } > 27 > 28 double largestCage(vector <int> xp, vector <int> yp, vector <int> xv, ve > 29 { > 30 vector<double> t; // collision time > 31 t.push_back(0); > 32 > 33 int n = xp.size(); > 34 for(int i=0; i<n; ++i) > 35 for(int j=i+1; j<n; ++j) > 36 { > 37 double ct = intersect(xp[i], xv[i], xp[j], xv[j] > 38 if(ct>0) t.push_back(ct); > 39 ct = intersect(yp[i], yv[i], yp[j], yv[j]); > 40 if(ct>0) t.push_back(ct); > 41 } > 42 > 43 sort(t.begin(), t.end()); > 44 t.erase(unique(t.begin(), t.end()), t.end()); > 45 t.push_back( t.back()+1.0 ); > 46 > 47 double Lmin = 1e+300; > 48 for(int i=0; i+1<t.size(); ++i) > 49 { > 50 double ti = (t[i]+t[i+1])/2; > 51 > 52 int j_xm=0; double xm = 1e+300; > 53 int j_xM=0; double xM = -1e+300; > 54 int j_ym=0; double ym = 1e+300; > 55 int j_yM=0; double yM = -1e+300; > 56 for(int j=0; j<n; ++j) { > 57 if( xm > xp[j]+xv[j]*ti ) j_xm=j, xm=xp[j]+xv[j] > 58 if( xM < xp[j]+xv[j]*ti ) j_xM=j, xM=xp[j]+xv[j] > 59 if( ym > yp[j]+yv[j]*ti ) j_ym=j, ym=yp[j]+yv[j] > 60 if( yM < yp[j]+yv[j]*ti ) j_yM=j, yM=yp[j]+yv[j] > 61 } > 62 > 63 int Xp = xp[j_xM]-xp[j_xm]; > 64 int Xv = xv[j_xM]-xv[j_xm]; > 65 int Yp = yp[j_yM]-yp[j_ym]; > 66 int Yv = yv[j_yM]-yv[j_ym]; > 67 > 68 // min of max(x,y) > 69 double ct = intersect(Xp,Xv,Yp,Yv); > 70 if( t[i] <= ct && ct <= t[i+1] ) > 71 Lmin = min(Lmin, Xp + Xv*ct); > 72 else { > 73 Lmin = min(Lmin, max(Xp+Xv*t[i+0], Yp+Yv*t[i+0]) > 74 Lmin = min(Lmin, max(Xp+Xv*t[i+1], Yp+Yv*t[i+1]) > 75 } > 76 } > 77 return Lmin; > 78 } > 79 > 80 // BEGIN CUT HERE > 81 public: > 82 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 83 private: > 84 template <typename T> string print_array(const vector<T> &V) { ostringst > 85 void verify_case(int Case, const double &Expected, const double &Receive > 86 void test_case_0() { int Arr0[] = {0,10}; vector <int> Arg0(Arr0, Arr0 + > 87 void test_case_1() { int Arr0[] = {0,10,0}; vector <int> Arg0(Arr0, Arr0 > 88 void test_case_2() { int Arr0[] = {50,10,30,15}; vector <int> Arg0(Arr0, > 89 void test_case_3() { int Arr0[] = {0,10,10,0}; vector <int> Arg0(Arr0, A > 90 void test_case_4() { int Arr0[] = {13,50,100,40,-100}; vector <int> Arg0 > 91 void test_case_5() { int Arr0[] = {0,10}; vector <int> Arg0(Arr0, Arr0 + > 92 void test_case_6() { int Arr0[] = {-49,-463,-212,-204,-557,-67,-374,-335 > 93 > 94 // END CUT HERE > 95 }; > 96 // BEGIN CUT HERE > 97 int main() { > 98 CatchTheMice().run_test(-1); > 99 } > 100 // END CUT HERE
Added SRM/426-U/1C-take1.cpp version [4413ce4dac004874]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector< > 19 { > 20 vector<double> A(V); > 21 for(int i=0; i<n; ++i) > 22 { > 23 // pivot > 24 if( M[i][i] == 0 ) > 25 for(int j=i+1; j<n; ++j) > 26 if( M[j][i] != 0 ) > 27 {swap(M[i], M[j]); swap(A[i], A[j]); bre > 28 if( M[i][i] == 0 ) > 29 throw "no anser"; > 30 > 31 // M[i][i] <-- 1 > 32 double p = M[i][i]; > 33 for(int j=i; j<n; ++j) > 34 M[i][j] /= p; > 35 A[i] /= p; > 36 > 37 // M[*][i] <-- 0 > 38 for(int j=0; j<n; ++j) if(j!=i) > 39 { > 40 double r = M[j][i]; > 41 for(int k=i; k<n; ++k) > 42 M[j][k] -= M[i][k] * r; > 43 A[j] -= A[i] * r; > 44 } > 45 } > 46 // consistency check > 47 for(int i=n; i<M.size(); ++i) { > 48 double s = 0.0; > 49 for(int j=0; j<n; ++j) > 50 s += M[i][j] * A[j]; > 51 if( abs(s-A[i]) > 1e-4 ) > 52 throw "no answer"; > 53 } > 54 return A; > 55 } > 56 > 57 > 58 class LongStraightRoad > 59 { > 60 public: > 61 int distanceToDestination(vector <string> signs, string destination) > 62 { > 63 typedef map< string, vector< pair<double,int> > > MSV; > 64 MSV data; > 65 for(int i=0; i<signs.size(); ++i) > 66 { > 67 stringstream sin(signs[i]); > 68 while(sin) { > 69 string n; int d; sin>>n>>d; > 70 char c; sin>>c; // ';' > 71 data[n].push_back( make_pair(double(d), i) ); > 72 } > 73 } > 74 if( data[destination].empty() ) > 75 return -1; > 76 > 77 vector< vector<double> > M; > 78 vector<double> V; > 79 for(MSV::iterator it=data.begin(); it!=data.end(); ++it) > 80 { > 81 vector< pair<double,int> >& d = it->second; > 82 for(int i=0; i+1<d.size(); ++i) > 83 { > 84 if( d[i].first <= d[i+1].first ) > 85 return -1; > 86 > 87 // S[d[i].second] - S[d[i+1].second] == d[i+1].f > 88 vector<double> Mk(signs.size()); > 89 Mk[d[i].second] = 1; > 90 Mk[d[i+1].second] = -1; > 91 M.push_back(Mk); > 92 V.push_back( d[i+1].first - d[i].first ); > 93 } > 94 } > 95 // S[last] = 0.0 > 96 vector<double> Mk(signs.size()); > 97 Mk[signs.size()-1] = 1; > 98 M.push_back(Mk); > 99 V.push_back(0); > 100 > 101 if( signs.size() > M.size() ) > 102 return -1; > 103 try { > 104 vector<double> S = solve_linear_eq( signs.size(), M, V ) > 105 for(int i=0; i+1<S.size(); ++i) > 106 if( S[i] > S[i+1] ) > 107 return -1; > 108 double result = > 109 S[ data[destination][0].second ] + data[destinat > 110 int rI = (int)floor(result+0.5); > 111 if( abs(result-rI) > 1e-4 ) > 112 return -1; > 113 return ( rI < 0 ? -1 : rI ); > 114 } catch(const char*) { > 115 return -1; > 116 } > 117 } > 118 > 119 // BEGIN CUT HERE > 120 public: > 121 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 122 private: > 123 template <typename T> string print_array(const vector<T> &V) { ostringst > 124 void verify_case(int Case, const int &Expected, const int &Received) { c > 125 void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLB > 126 ,"MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0 > 127 void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLB > 128 ,"GLASTONBURY 13;MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0 > 129 void test_case_2() { string Arr0[] = {"A 25;B 15" > 130 ,"A 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); st > 131 void test_case_3() { string Arr0[] = {"YVO 60;J 62" > 132 ,"K 45" > 133 ,"K 40;MV 17" > 134 ,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80" > 135 ,"YVO 30;B 37;RB 59"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof( > 136 void test_case_4() { string Arr0[] = {"A 200;B 150" > 137 ,"C 45;D 100;E 150" > 138 ,"C 25;E 130" > 139 ,"F 80;G 65" > 140 ,"G 35;H 160" > 141 ,"A 160" > 142 ,"H 130"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); > 143 > 144 // END CUT HERE > 145 }; > 146 // BEGIN CUT HERE > 147 int main() { > 148 LongStraightRoad().run_test(-1); > 149 } > 150 // END CUT HERE
Added SRM/426-U/1C-take2.cpp version [3cd3d111843830ab]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 > 19 class LongStraightRoad > 20 { > 21 public: > 22 typedef pair<int,int> edge; // (index,dist) > 23 int distanceToDestination(vector<string> signs, string destination) > 24 { > 25 int n = signs.size(); > 26 > 27 // parse > 28 vector< map<string, int> > data(n); > 29 map<string, vector<edge> > n2d; // name -> (index, dist) > 30 for(int i=0; i<n; ++i) > 31 { > 32 stringstream sin(signs[i]); > 33 while(sin) { > 34 string n; int d; sin>>n>>d; > 35 char c; sin>>c; // ';' > 36 data[i][n] = d; > 37 n2d[n].push_back( edge(i,d) ); > 38 } > 39 } > 40 > 41 // construct the graph > 42 vector< vector<edge> > G(n); > 43 for(int i=0; i<n; ++i) > 44 { > 45 for(map<string,int>::iterator it=data[i].begin(); it!=da > 46 { > 47 vector<edge>& es = n2d[it->first]; > 48 for(int j=0; j<es.size(); ++j) > 49 if( es[j].first != i ) > 50 G[i].push_back( edge(es[j].first > 51 } > 52 } > 53 > 54 try { > 55 vector<bool> done(n, false); > 56 vector<int> p(n); > 57 { > 58 vector<bool> visited(n, false); > 59 dfs(n-1, 0, G, visited, p); > 60 > 61 vector<int> kn; > 62 for(int i=0; i<n; ++i) > 63 if( visited[i] ) { > 64 kn.push_back( p[i] ); > 65 done[i] = true; > 66 } > 67 if( !sorted(kn) ) { > 68 cerr << "ns1" << endl; > 69 return -1; > 70 } > 71 } > 72 vector<edge>& es = n2d[destination]; > 73 int result = p[es[0].first] + es[0].second; > 74 if( result < 0 ) > 75 return -1; > 76 > 77 for(int v=0; v<n; ++v) > 78 if( !done[v] ) > 79 { > 80 vector<bool> visited(n, false); > 81 dfs(v, 0, G, visited, p); > 82 > 83 vector<int> kn; > 84 for(int i=0; i<n; ++i) > 85 if( visited[i] ) { > 86 kn.push_back( p[i] ); > 87 done[i] = true; > 88 } > 89 if( !sorted(kn) ) { > 90 cerr << "ns@" << v << endl; > 91 return -1; > 92 } > 93 } > 94 return result; > 95 } catch (const char*) { > 96 return -1; > 97 } > 98 } > 99 > 100 bool sorted( vector<int>& kn ) > 101 { > 102 for(int i=0; i+1<kn.size(); ++i) > 103 if( kn[i] >= kn[i+1] ) > 104 return false; > 105 return true; > 106 } > 107 > 108 void dfs(int v, int d, vector< vector<edge> >& G, vector<bool>& visited, > 109 { > 110 if( visited[v] ) { > 111 if( d != p[v] ) { > 112 cerr << d << " " << v << " " << p[v] << endl; > 113 throw "bad"; > 114 } > 115 return; > 116 } > 117 visited[v] = true; > 118 p[v] = d; > 119 for(int i=0; i<G[v].size(); ++i) { > 120 cerr << v << "->" << G[v][i].first << " : " << G[v][i].second << endl; > 121 dfs( G[v][i].first, d+G[v][i].second, G, visited, p ); > 122 } > 123 } > 124 > 125 // BEGIN CUT HERE > 126 public: > 127 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 128 private: > 129 template <typename T> string print_array(const vector<T> &V) { ostringst > 130 void verify_case(int Case, const int &Expected, const int &Received) { c > 131 void test_case_0() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLB > 132 ,"MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0 > 133 void test_case_1() { string Arr0[] = {"COLCHESTER 5;GLASTONBURY 25;MARLB > 134 ,"GLASTONBURY 13;MARLBOROUGH 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0 > 135 void test_case_2() { string Arr0[] = {"A 25;B 15" > 136 ,"A 2"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); st > 137 void test_case_3() { string Arr0[] = {"YVO 60;J 62" > 138 ,"K 45" > 139 ,"K 40;MV 17" > 140 ,"K 37;YVO 44;HY 48;CC 69;D 77;YXF 80" > 141 ,"YVO 30;B 37;RB 59"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof( > 142 void test_case_4() { string Arr0[] = {"A 200;B 150" > 143 ,"C 45;D 100;E 150" > 144 ,"C 25;E 130" > 145 ,"F 80;G 65" > 146 ,"G 35;H 160" > 147 ,"A 160" > 148 ,"H 130"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); > 149 > 150 // END CUT HERE > 151 }; > 152 // BEGIN CUT HERE > 153 int main() { > 154 LongStraightRoad().run_test(-1); > 155 } > 156 // END CUT HERE
Added SRM/427/1A.cpp version [ad7c58f810045566]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 int gcd(int a, int b) > 19 { > 20 while(a) { > 21 int t = a; > 22 a = b%a; > 23 b = t; > 24 } > 25 return b; > 26 } > 27 > 28 class DesignCalendar > 29 { > 30 public: > 31 int shortestPeriod(int dayLength, int yearLength) > 32 { > 33 return dayLength / gcd(yearLength, dayLength); > 34 } > 35 > 36 // BEGIN CUT HERE > 37 public: > 38 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 39 private: > 40 template <typename T> string print_array(const vector<T> &V) { ostringst > 41 void verify_case(int Case, const int &Expected, const int &Received) { c > 42 void test_case_0() { int Arg0 = 4; int Arg1 = 1461; int Arg2 = 4; verify > 43 void test_case_1() { int Arg0 = 86400; int Arg1 = 31558150; int Arg2 = 1 > 44 void test_case_2() { int Arg0 = 98; int Arg1 = 100; int Arg2 = 49; verif > 45 void test_case_3() { int Arg0 = 1000 ; int Arg1 = 50000100; int Arg2 = 1 > 46 void test_case_4() { int Arg0 = 5673; int Arg1 = 28565335; int Arg2 = 56 > 47 void test_case_5() { int Arg0 = 5555; int Arg1 = 846555; int Arg2 = 1111 > 48 > 49 // END CUT HERE > 50 }; > 51 // BEGIN CUT HERE > 52 int main() { DesignCalendar().run_test(-1); } > 53 // END CUT HERE
Added SRM/427/1B.cpp version [cd5512e0c909abdd]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 LL dig(LL x) > 19 { > 20 if(x<=9) return x; > 21 LL s = 0; > 22 while(x) > 23 s += x%10, x/=10; > 24 return dig(s); > 25 } > 26 class LocateTreasure > 27 { > 28 public: > 29 string location(int K, int m) > 30 { > 31 vector<LL> ds; > 32 ds.push_back(1); > 33 LL found[10] = {-1, 0, -1, -1, -1, -1, -1, -1, -1, -1}; > 34 LL loop_beg, loop_end; > 35 for(LL i=1;; ++i) { > 36 LL d = dig( ds.back()*m ); > 37 if( found[d] < 0 ) > 38 { > 39 ds.push_back(d); > 40 found[d] = i; > 41 } > 42 else > 43 { > 44 loop_beg = found[d]; > 45 loop_end = i; > 46 break; > 47 } > 48 } > 49 > 50 vector<LL> ds2 = ds; > 51 ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); > 52 ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); > 53 ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); > 54 ds2.insert(ds2.end(), ds.begin()+loop_beg, ds.begin()+loop_end); > 55 ds = ds2; > 56 > 57 LL x=0, y=0; > 58 > 59 if( K <= loop_beg ) > 60 { > 61 for(LL i=0; i<K; ++i) { > 62 if( i%4==0 ) y += ds[i]; > 63 if( i%4==1 ) x += ds[i]; > 64 if( i%4==2 ) y -= ds[i]; > 65 if( i%4==3 ) x -= ds[i]; > 66 } > 67 } > 68 else > 69 { > 70 for(LL i=0; i<loop_beg; ++i) { > 71 if( i%4==0 ) y += ds[i]; > 72 if( i%4==1 ) x += ds[i]; > 73 if( i%4==2 ) y -= ds[i]; > 74 if( i%4==3 ) x -= ds[i]; > 75 } > 76 > 77 LL loop_len = 4 * (loop_end - loop_beg); > 78 K -= loop_beg; > 79 LL dx=0, dy=0; > 80 for(LL i=0; i<loop_len; ++i) { > 81 if( (loop_beg+i)%4==0 ) dy += ds[loop_beg+i]; > 82 if( (loop_beg+i)%4==1 ) dx += ds[loop_beg+i]; > 83 if( (loop_beg+i)%4==2 ) dy -= ds[loop_beg+i]; > 84 if( (loop_beg+i)%4==3 ) dx -= ds[loop_beg+i]; > 85 } > 86 x += dx * (K / loop_len); > 87 y += dy * (K / loop_len); > 88 > 89 K %= loop_len; > 90 for(LL i=0; i<K; ++i) { > 91 if( (loop_beg+i)%4==0 ) y += ds[loop_beg+i]; > 92 if( (loop_beg+i)%4==1 ) x += ds[loop_beg+i]; > 93 if( (loop_beg+i)%4==2 ) y -= ds[loop_beg+i]; > 94 if( (loop_beg+i)%4==3 ) x -= ds[loop_beg+i]; > 95 } > 96 } > 97 > 98 stringstream sout; > 99 sout << x << " " << y; > 100 return sout.str(); > 101 } > 102 > 103 // BEGIN CUT HERE > 104 public: > 105 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 106 private: > 107 template <typename T> string print_array(const vector<T> &V) { ostringst > 108 void verify_case(int Case, const string &Expected, const string &Receive > 109 void test_case_0() { int Arg0 = 5; int Arg1 = 2; string Arg2 = "-6 4"; v > 110 void test_case_1() { int Arg0 = 99; int Arg1 = 1; string Arg2 = "1 0"; v > 111 void test_case_2() { int Arg0 = 6; int Arg1 = 9; string Arg2 = "9 1"; ve > 112 > 113 // END CUT HERE > 114 }; > 115 // BEGIN CUT HERE > 116 int main() { LocateTreasure().run_test(-1); } > 117 // END CUT HERE
Added SRM/427/1C.cpp version [c7e9f05169f65f76]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 static const LL MODVAL = 1234567891; > 19 > 20 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 21 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 22 LL FACT(LL x) { return x<=1 ? 1 : MUL(x, FACT(x-1)); } > 23 > 24 class PSequence > 25 { > 26 public: > 27 map<pair<vector<LL>,int>, LL> memo; > 28 > 29 int count(vector <int> S, int p) > 30 { > 31 memo.clear(); > 32 > 33 vector<LL> cs; // congruences mod p > 34 > 35 bool used[30] = {}; > 36 for(int i=0; i<S.size(); ++i) > 37 if( !used[i] ) > 38 { > 39 int cnt = 0; > 40 for(int j=i; j<S.size(); ++j) > 41 if( abs(S[i]-S[j]) % p == 0 ) > 42 {used[j]=true; ++cnt;} > 43 cs.push_back(cnt); > 44 } > 45 > 46 LL f = num_perm( cs, -1, S.size() ); > 47 for(int i=0; i<cs.size(); ++i) > 48 f = MUL(f, FACT(cs[i])); > 49 return f; > 50 } > 51 > 52 LL num_perm( vector<LL>& cs, int forbidden, int n ) > 53 { > 54 if( n == 0 ) > 55 return 1; > 56 > 57 int v = (forbidden >= 0 ? cs[forbidden] : -1); > 58 pair<vector<LL>,int> key(cs, forbidden); > 59 sort(key.first.begin(), key.first.end()); > 60 key.second = find(key.first.begin(), key.first.end(), v) - key.f > 61 > 62 if( memo.count(key) ) > 63 return memo[key]; > 64 > 65 LL np = 0; > 66 for(int i=0; i<cs.size(); ++i) > 67 if( cs[i] && i!=forbidden ) { > 68 cs[i]--; > 69 np = ADD(np, num_perm( cs, i, n-1 )); > 70 cs[i]++; > 71 } > 72 return memo[key] = np; > 73 } > 74 > 75 // BEGIN CUT HERE > 76 public: > 77 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 78 private: > 79 template <typename T> string print_array(const vector<T> &V) { ostringst > 80 void verify_case(int Case, const int &Expected, const int &Received) { c > 81 void test_case_0() { int Arr0[] = {-1,0,1,2,3}; vector <int> Arg0(Arr0, > 82 void test_case_1() { int Arr0[] = {6,2}; vector <int> Arg0(Arr0, Arr0 + > 83 void test_case_2() { int Arr0[] = {1,2,3,4}; vector <int> Arg0(Arr0, Arr > 84 void test_case_3() { int Arr0[] = {4,6,8,-3,7}; vector <int> Arg0(Arr0, > 85 void test_case_4() { > 86 int Arr0[] = {0,1,2,3,4,5,6,7,8,9, > 87 10,11,12,13,14,15,16,17,18,19, > 88 20,21,22,23,24,25,26,27,28,29}; > 89 vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); > 90 int Arg1 = 15; int Arg2 = 12; verify_case(4, Arg2, count(Arg0, Arg1)); } > 91 > 92 // END CUT HERE > 93 }; > 94 // BEGIN CUT HERE > 95 int main() { PSequence().run_test(-1); } > 96 // END CUT HERE
Added SRM/428/1A.cpp version [08de86b9b3316798]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 class TheLuckyString > 19 { > 20 public: > 21 int count(string s) > 22 { > 23 int cnt = 0; > 24 > 25 sort(s.begin(), s.end()); > 26 do > 27 if( adjacent_find(s.begin(), s.end()) == s.end() ) > 28 ++cnt; > 29 while( next_permutation(s.begin(), s.end()) ); > 30 > 31 return cnt; > 32 } > 33 > 34 // BEGIN CUT HERE > 35 public: > 36 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 37 private: > 38 template <typename T> string print_array(const vector<T> &V) { ostringst > 39 void verify_case(int Case, const int &Expected, const int &Received) { c > 40 void test_case_0() { string Arg0 = "ab"; int Arg1 = 2; verify_case(0, Ar > 41 void test_case_1() { string Arg0 = "aaab"; int Arg1 = 0; verify_case(1, > 42 void test_case_2() { string Arg0 = "aabbbaa"; int Arg1 = 1; verify_case( > 43 void test_case_3() { string Arg0 = "abcdefghij"; int Arg1 = 3628800; ver > 44 > 45 // END CUT HERE > 46 }; > 47 // BEGIN CUT HERE > 48 int main() { TheLuckyString().run_test(-1); } > 49 // END CUT HERE
Added SRM/428/1B.cpp version [e110c16d9a9bffd5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 using namespace std; > 16 typedef long long LL; > 17 > 18 static const LL MODVAL = 1234567891; > 19 > 20 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 21 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 22 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 23 LL POW(LL x, LL e) { > 24 LL v = 1; > 25 for(;e;x=MUL(x,x),e>>=1) > 26 if(e&1) > 27 v = MUL(v, x); > 28 return v; > 29 } > 30 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 31 LL C(LL n, LL k) { > 32 LL v = 1; > 33 for(LL i=1; i<=k; ++i) > 34 v = DIV(MUL(v, n-i+1), i); > 35 return v; > 36 } > 37 LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e > 38 { > 39 if( b > e ) return 0; > 40 if( k <= 1 ) return k*(e-b+1); > 41 return DIV(SUB(POW(k, e+1), POW(k,b)), k-1); > 42 } > 43 > 44 class TheLongPalindrome > 45 { > 46 public: > 47 int count(int n, int k) > 48 { > 49 LL ans = 0; > 50 for(int i=1; i<=k; ++i) > 51 { > 52 LL aa = f(i,i,n); > 53 for(int j=i-1; j>=1; --j) > 54 aa = (i-j)%2 > 55 ? SUB(aa, MUL(f(j,i,n), C(i,j))) > 56 : ADD(aa, MUL(f(j,i,n), C(i,j))); > 57 ans = ADD(ans, MUL(aa, C(26,i))); > 58 } > 59 return ans; > 60 } > 61 > 62 LL f(LL i, LL b, LL n) // i^b + i^b + i^b+1 + i^b+1 + ... + (nth) > 63 { > 64 LL v = MUL(GSS(i, b, n/2), 2); > 65 if( n%2 == 1 ) > 66 v = ADD(v, POW(i, n/2+1)); > 67 return v; > 68 } > 69 > 70 // BEGIN CUT HERE > 71 public: > 72 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0() > 73 private: > 74 template <typename T> string print_array(const vector<T> &V) { ostringst > 75 void verify_case(int Case, const int &Expected, const int &Received) { c > 76 void test_case_0() { int Arg0 = 1; int Arg1 = 1; int Arg2 = 26; verify_c > 77 void test_case_1() { int Arg0 = 2; int Arg1 = 10; int Arg2 = 52; verify_ > 78 void test_case_2() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 728; verify_ > 79 void test_case_3() { int Arg0 = 44; int Arg1 = 7; int Arg2 = 240249781; > 80 > 81 // END CUT HERE > 82 }; > 83 // BEGIN CUT HERE > 84 int main() { TheLongPalindrome().run_test(-1); } > 85 // END CUT HERE
Added SRM/428/1C.cpp version [0bbb80b9b0468b3d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cstring> > 16 #include <cassert> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 char memo[81*81*81*81]; > 21 int n, X[16]; > 22 static const char NIL = 99; > 23 > 24 bool win[243]; > 25 > 26 class TheStringGame > 27 { > 28 public: > 29 string winner(string s) > 30 { > 31 // construct the winning condition table > 32 for(int mm=0; mm<243; ++mm) > 33 win[mm] = (mm/ 9%3==0 && > 34 ( mm/81%3==2 && mm/27%3==1 // LOX > 35 || mm/27%3==2 && mm/ 3%3==2 // LXL > 36 || mm/ 3%3==1 && mm/ 1%3==2 )); // XOL > 37 > 38 // read the input > 39 int m=0, numX=0; > 40 { > 41 memset(memo, NIL, sizeof(memo)); > 42 n = s.size(); > 43 for(int i=0; i<n; ++i) { > 44 m *= 3; > 45 for(int j=0; j<numX; ++j) > 46 X[j] *= 3; > 47 > 48 if( s[i]=='X' ) > 49 X[numX++] = 1; > 50 else > 51 m += (s[i]=='O' ? 1 : 2); > 52 } > 53 } > 54 > 55 // solve > 56 if( int r = negaMax(m, numX) ) > 57 { > 58 stringstream sout; > 59 sout << (r>0 ? "John" : "Brus") << " " << numX-abs(r)+1; > 60 return sout.str(); > 61 } > 62 return "Draw"; > 63 } > 64 > 65 static int rev(int m) > 66 { > 67 int z = 0; > 68 for(int i=0; i<n; ++i) > 69 z = z*3 + m%3, m/=3; > 70 return z; > 71 } > 72 > 73 static char negaMax(int m, int numX) > 74 { > 75 if( numX == 0 ) > 76 return 0; > 77 if( memo[m] != NIL ) > 78 return memo[m]; > 79 > 80 for(int j=0; j<numX; ++j) > 81 if( win[m*9/X[j]%243] ) > 82 return memo[m] = numX; > 83 > 84 int sc = -numX; > 85 for(int j=0; j<numX; ++j) { > 86 int M = X[j]; > 87 swap(X[j], X[numX-1]); > 88 sc = max(sc, -negaMax(m+M*1, numX-1)); > 89 sc = max(sc, -negaMax(m+M*2, numX-1)); > 90 swap(X[j], X[numX-1]); > 91 } > 92 return memo[m] = memo[rev(m)] = sc; > 93 } > 94 }; > 95 > 96 // BEGIN CUT HERE > 97 #include <ctime> > 98 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 99 > 100 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 101 int verify_case(const string &Expected, const string &Received) { if (Expected = > 102 > 103 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 104 char Test_(...); > 105 int Test_(Case_<0>) { > 106 string s = "XXOXXXLXLX"; > 107 string RetVal = "John 1"; > 108 return verify_case(RetVal, TheStringGame().winner(s)); } > 109 int Test_(Case_<1>) { > 110 string s = "LXXLXXL"; > 111 string RetVal = "Brus 2"; > 112 return verify_case(RetVal, TheStringGame().winner(s)); } > 113 int Test_(Case_<2>) { > 114 string s = "LLOOLLOOLLOOLLOO"; > 115 string RetVal = "Draw"; > 116 return verify_case(RetVal, TheStringGame().winner(s)); } > 117 int Test_(Case_<3>) { > 118 string s = "XXXXXXXXXXXXXXXX"; > 119 string RetVal = "Brus 16"; > 120 return verify_case(RetVal, TheStringGame().winner(s)); } > 121 int Test_(Case_<4>) { > 122 string s = "XXXXXXXXXXXXXXXO"; > 123 string RetVal = "John 15"; > 124 return verify_case(RetVal, TheStringGame().winner(s)); } > 125 > 126 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 127 template<> void Run_<-1>() {} > 128 int main() { Run_<0>(); } > 129 // END CUT HERE
Added SRM/429/1A.cpp version [0c272f575c59737f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class SubrectanglesOfTable > 20 { > 21 public: > 22 vector<long long> getQuantity(vector <string> table) > 23 { > 24 for(int i=0,n=table.size(); i<n; ++i) > 25 table.push_back( table[i]+=table[i] ); > 26 > 27 vector<LL> ans(26); > 28 > 29 LL Y = table.size(), X = table[0].size(); > 30 for(LL y=0; y<Y; ++y) > 31 for(LL x=0; x<X; ++x) > 32 ans[table[y][x]-'A'] += (y+1)*(x+1)*(Y-y)*(X-x); > 33 return ans; > 34 } > 35 }; > 36 > 37 // BEGIN CUT HERE > 38 #include <ctime> > 39 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 40 > 41 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 42 int verify_case(const vector<long long> &Expected, const vector<long long> &Rece > 43 > 44 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 45 char Test_(...); > 46 int Test_(Case_<0>) { > 47 string table_[] = {"OK"}; > 48 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 49 long RetVal_[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 40, 0, 0, 0 > 50 vector<long long> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetV > 51 return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } > 52 int Test_(Case_<1>) { > 53 string table_[] = {"GOOD", "LUCK"}; > 54 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 55 long RetVal_[] = {0, 0, 320, 280, 0, 0, 280, 0, 0, 0, 280, 280, 0, 0, 64 > 56 vector<long long> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetV > 57 return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } > 58 int Test_(Case_<2>) { > 59 string table_[] = {"TANYA", > 60 "HAPPY", > 61 "BIRTH", > 62 "DAYYY"}; > 63 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 64 long RetVal_[] = {5168, 1280, 0, 1120, 0, 0, 0, 2560, 1472, 0, 0, 0, 0, > 65 vector<long long> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetV > 66 return verify_case(RetVal, SubrectanglesOfTable().getQuantity(table)); } > 67 > 68 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 69 template<> void Run_<-1>() {} > 70 int main() { Run_<0>(); } > 71 // END CUT HERE > 72
Added SRM/429/1B.cpp version [61896c32b2eaf928]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 LL gcd(LL a, LL b) > 20 { > 21 while(a) swap(a, b%=a); return b; > 22 } > 23 LL lcm(LL a, LL b) > 24 { > 25 return a/gcd(a,b)*b; > 26 } > 27 > 28 class IngredientProportions > 29 { > 30 public: > 31 vector <int> getMasses(vector <string> proportions) > 32 { > 33 int N = proportions.size() + 1; > 34 vector<LL> a(N-1),b(N-1),p(N-1),q(N-1); > 35 > 36 for(int i=0; i<proportions.size(); ++i) > 37 { > 38 stringstream sin(proportions[i]); > 39 sin.get();//# > 40 sin>>a[i]; > 41 sin.get();sin.get();sin.get();sin.get();sin.get();sin.ge > 42 sin>>b[i]; > 43 sin.get();sin.get();sin.get();sin.get();//" as " > 44 sin>>p[i]; > 45 sin.get(); //: > 46 sin>>q[i]; > 47 } > 48 > 49 vector<LL> g(N, -1); g[0]=1; > 50 for(int _=0; _<N; ++_) > 51 for(int i=0; i<N-1; ++i) > 52 { > 53 LL A=a[i],B=b[i],P=p[i],Q=q[i]; > 54 if( g[A]==-1 && g[B]!=-1 ) > 55 swap(A,B), swap(P,Q); > 56 if( g[A]!=-1 && g[B]==-1 ) > 57 { > 58 LL f = lcm(g[A],P) / g[A]; > 59 for(int j=0; j<N; ++j) > 60 if( g[j] != -1 ) > 61 g[j] *= f; > 62 g[B] = g[A]/P*Q; > 63 } > 64 } > 65 LL gg = accumulate( g.begin(), g.end(), g.back(), gcd ); > 66 vector<int> ans(N); > 67 for(int i=0; i<N; ++i) > 68 ans[i] = g[i]/gg; > 69 return ans; > 70 } > 71 }; > 72 > 73 // BEGIN CUT HERE > 74 #include <ctime> > 75 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 76 > 77 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 78 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 79 > 80 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 81 char Test_(...); > 82 int Test_(Case_<0>) { > 83 string proportions_[] = {"#0 and #1 as 6:4"}; > 84 vector <string> proportions(proportions_, proportions_+sizeof(proporti > 85 int RetVal_[] = {3, 2 }; > 86 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 87 return verify_case(RetVal, IngredientProportions().getMasses(proportions > 88 int Test_(Case_<1>) { > 89 string proportions_[] = {"#0 and #1 as 9:8","#1 and #2 as 9:8"} > 90 ; > 91 vector <string> proportions(proportions_, proportions_+sizeof(proporti > 92 int RetVal_[] = {81, 72, 64 }; > 93 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 94 return verify_case(RetVal, IngredientProportions().getMasses(proportions > 95 int Test_(Case_<2>) { > 96 string proportions_[] = {"#4 and #0 as 1:1", "#4 and #1 as 3:1", "#4 and > 97 vector <string> proportions(proportions_, proportions_+sizeof(proporti > 98 int RetVal_[] = {105, 35, 21, 15, 105 }; > 99 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 100 return verify_case(RetVal, IngredientProportions().getMasses(proportions > 101 int Test_(Case_<3>) { > 102 string proportions_[] = {"#2 and #3 as 6:8", "#0 and #1 as 9:3", "#3 and > 103 vector <string> proportions(proportions_, proportions_+sizeof(proporti > 104 int RetVal_[] = {60, 20, 63, 84 }; > 105 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 106 return verify_case(RetVal, IngredientProportions().getMasses(proportions > 107 > 108 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 109 template<> void Run_<-1>() {} > 110 int main() { Run_<0>(); } > 111 // END CUT HERE > 112
Added SRM/429/1C.cpp version [0b43e0ef88516e58]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class SpecificPolyominoCovering > 20 { > 21 public: > 22 vector <string> findCovering(vector <string> region) > 23 { > 24 try { > 25 rec(0, 0, region); > 26 } catch( vector<string>& r ) { > 27 return r; > 28 } > 29 return vector<string>(); > 30 } > 31 > 32 bool nextX(int &y, int &x, vector<string>& r) > 33 { > 34 while( r[y][x] != 'X' ) { > 35 ++x; > 36 if( x == r[y].size() ) { > 37 x=0, ++y; > 38 if( y == r.size() ) > 39 return false; > 40 } > 41 } > 42 return true; > 43 } > 44 > 45 bool canFill( vector<string> r ) > 46 { > 47 return canFill_rec(0, 0, r); > 48 } > 49 > 50 bool canFill_rec(int y, int x, vector<string>& r) > 51 { > 52 if( !nextX(y, x, r) ) > 53 return true; > 54 > 55 // if it was the pattern fillable both by B and A, > 56 // XX?X > 57 // XXXX > 58 // then, if we can fill starting from A it implies ?=X > 59 // In this case, we must have been able to fill starting > 60 // from B. Thus, we never need to try A if we've failed > 61 // filling by B. The "lexicographically first" will by > 62 // considered elsewhere. > 63 > 64 if( x+1 < r[y].size() && r[y][x+1]=='X' ) > 65 { > 66 r[y][x] = r[y][x+1] = 'B'; > 67 return canFill_rec(y, x, r); > 68 } > 69 > 70 if( x+3 < r[y].size() && y+1 < r.size() ) > 71 { > 72 int py[] = {0,0,1,1,1,1}; > 73 int px[] = {0,3,0,1,2,3}; > 74 for(int i=0; i<6; ++i) > 75 if( r[y+py[i]][x+px[i]]!='X' ) > 76 return false; > 77 else > 78 r[y+py[i]][x+px[i]] = 'A'; > 79 return canFill_rec(y, x, r); > 80 } > 81 > 82 return false; > 83 } > 84 > 85 void rec(int y, int x, vector<string>& r) > 86 { > 87 if( !nextX(y, x, r) ) > 88 throw r; > 89 > 90 // try to fill by A > 91 if( x+3 < r[y].size() && y+1 < r.size() ) > 92 { > 93 int py[] = {0,0,1,1,1,1}; > 94 int px[] = {0,3,0,1,2,3}; > 95 bool bad = false; > 96 for(int i=0; i<6; ++i) > 97 if( r[y+py[i]][x+px[i]]!='X' ) > 98 bad = true; > 99 if( !bad ) > 100 { > 101 for(int i=0; i<6; ++i) > 102 r[y+py[i]][x+px[i]] = 'A'; > 103 if( canFill(r) ) > 104 rec(y, x, r); > 105 for(int i=0; i<6; ++i) > 106 r[y+py[i]][x+px[i]] = 'X'; > 107 } > 108 } > 109 // try to fill by B > 110 if( x+1 < r[y].size() && r[y][x+1]=='X' ) > 111 { > 112 r[y][x] = r[y][x+1] = 'B'; > 113 rec(y, x, r); > 114 r[y][x] = r[y][x+1] = 'X'; > 115 } > 116 } > 117 }; > 118 > 119 // BEGIN CUT HERE > 120 #include <ctime> > 121 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 122 > 123 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 124 int verify_case(const vector <string> &Expected, const vector <string> &Received > 125 > 126 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 127 char Test_(...); > 128 int Test_(Case_<0>) { > 129 string region_[] = {"XXXX", > 130 "XXXX"}; > 131 vector <string> region(region_, region_+sizeof(region_)/sizeof(*region > 132 string RetVal_[] = {"ABBA", "AAAA" }; > 133 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 134 return verify_case(RetVal, SpecificPolyominoCovering().findCovering(regi > 135 int Test_(Case_<1>) { > 136 string region_[] = {"X..XXXX..X", > 137 "XXXX..XXXX"}; > 138 vector <string> region(region_, region_+sizeof(region_)/sizeof(*region > 139 string RetVal_[] = {"A..ABBA..A", "AAAA..AAAA" }; > 140 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 141 return verify_case(RetVal, SpecificPolyominoCovering().findCovering(regi > 142 int Test_(Case_<2>) { > 143 string region_[] = {"XXXXXX", > 144 "XXXXXX", > 145 "XXXXXX"}; > 146 vector <string> region(region_, region_+sizeof(region_)/sizeof(*region > 147 string RetVal_[] = {"ABBABB", "AAAABB", "BBBBBB" }; > 148 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 149 return verify_case(RetVal, SpecificPolyominoCovering().findCovering(regi > 150 int Test_(Case_<3>) { > 151 string region_[] = {"X..XX", > 152 "XXXXX"}; > 153 vector <string> region(region_, region_+sizeof(region_)/sizeof(*region > 154 vector <string> RetVal; > 155 return verify_case(RetVal, SpecificPolyominoCovering().findCovering(regi > 156 int Test_(Case_<4>) { > 157 string region_[] = {"XXXXXXXXXX", > 158 "XXXXXXXXXX", > 159 "XXXXXXXXXX", > 160 "XXXXX..XXX", > 161 "XXXXXXXXXX", > 162 "XXXXXXXXXX", > 163 "XXXXXXXXXX"}; > 164 vector <string> region(region_, region_+sizeof(region_)/sizeof(*region > 165 string RetVal_[] = {"ABBAABBABB", "AAAAAAAABB", "ABBABBBBBB", "AAAAA..AB > 166 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 167 return verify_case(RetVal, SpecificPolyominoCovering().findCovering(regi > 168 > 169 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 170 template<> void Run_<-1>() {} > 171 int main() { Run_<0>(); } > 172 // END CUT HERE > 173
Added SRM/430/1A.cpp version [2990a35eb58be62a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class BitwiseEquations > 20 { > 21 public: > 22 long long kthPlusOrSolution(int x, int k) > 23 { > 24 LL y = 0; > 25 for(LL i=1; k; i<<=1) > 26 if( !(x&i) ) > 27 y |= (k&1)*i, k>>=1; > 28 return y; > 29 } > 30 }; > 31 > 32 // BEGIN CUT HERE > 33 #include <ctime> > 34 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 35 > 36 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 37 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 38 > 39 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 40 char Test_(...); > 41 int Test_(Case_<0>) { > 42 int x = 5; > 43 int k = 1; > 44 long long RetVal = 2LL; > 45 return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); > 46 int Test_(Case_<1>) { > 47 int x = 5; > 48 int k = 5; > 49 long long RetVal = 18LL; > 50 return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); > 51 int Test_(Case_<2>) { > 52 int x = 10; > 53 int k = 3; > 54 long long RetVal = 5LL; > 55 return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); > 56 int Test_(Case_<3>) { > 57 int x = 1; > 58 int k = 1000000000; > 59 long long RetVal = 2000000000LL; > 60 return verify_case(RetVal, BitwiseEquations().kthPlusOrSolution(x, k)); > 61 > 62 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 63 template<> void Run_<-1>() {} > 64 int main() { Run_<0>(); } > 65 // END CUT HERE > 66
Added SRM/430/1B.cpp version [d61bab3b8e785a85]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 int INF = 10000000; > 20 > 21 class TwinTowns > 22 { > 23 public: > 24 vector <int> optimalTwinTowns(vector <int> x, vector <int> y, int maxPar > 25 { > 26 int N = x.size(); > 27 int cost[10][10]; > 28 for(int i=0; i<N; ++i) > 29 for(int j=0; j<N; ++j) > 30 { > 31 int d = abs(x[i]-x[j]) + abs(y[i]-y[j]); > 32 cost[i][j] = (d < minDistance ? INF : d); > 33 } > 34 > 35 vector<int> v(N, maxPartners); > 36 pair<int,int> r = rec(0, N, v, cost); > 37 vector<int> ans(2); > 38 ans[0] = -r.first; > 39 ans[1] = r.second; > 40 return ans; > 41 } > 42 > 43 map< vector<int>,pair<int,int> > memo; > 44 pair<int,int> rec(int i, int N, vector<int>& v, int cost[10][10]) > 45 { > 46 if( i==N ) > 47 return make_pair(0, 0); > 48 vector<int> key(v.begin()+i, v.end()); > 49 if( memo.count(key) ) > 50 return memo[key]; > 51 > 52 // choose at most v[i] js s.t. cost[i][j]!=INF && v[j]!=0 > 53 pair<int,int> best = rec(i+1, N, v, cost); > 54 if( v[i] >= 1 ) > 55 { > 56 for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1 > 57 { > 58 v[j1]--; > 59 pair<int,int> a = rec(i+1, N, v, cost); > 60 a.first -= 1; > 61 a.second += cost[i][j1]; > 62 best = min(best, a); > 63 v[j1]++; > 64 } > 65 } > 66 if( v[i] >= 2 ) > 67 { > 68 for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1 > 69 for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2 > 70 { > 71 v[j1]--; > 72 v[j2]--; > 73 pair<int,int> a = rec(i+1, N, v, cost); > 74 a.first -= 2; > 75 a.second += cost[i][j1] + cost[i][j2]; > 76 best = min(best, a); > 77 v[j1]++; > 78 v[j2]++; > 79 } > 80 } > 81 if( v[i] >= 3 ) > 82 { > 83 for(int j1= i+1; j1<N; ++j1) if(cost[i][j1]!=INF && v[j1 > 84 for(int j2=j1+1; j2<N; ++j2) if(cost[i][j2]!=INF && v[j2 > 85 for(int j3=j2+1; j3<N; ++j3) if(cost[i][j3]!=INF && v[j3 > 86 { > 87 v[j1]--; > 88 v[j2]--; > 89 v[j3]--; > 90 pair<int,int> a = rec(i+1, N, v, cost); > 91 a.first -= 3; > 92 a.second += cost[i][j1] + cost[i][j2] + cost[i][ > 93 best = min(best, a); > 94 v[j1]++; > 95 v[j2]++; > 96 v[j3]++; > 97 } > 98 } > 99 > 100 return memo[key] = best; > 101 } > 102 }; > 103 > 104 // BEGIN CUT HERE > 105 #include <ctime> > 106 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 107 > 108 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 109 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 110 > 111 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 112 char Test_(...); > 113 int Test_(Case_<0>) { > 114 int x_[] = {0,0,10}; > 115 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 116 int y_[] = {0,10,4}; > 117 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 118 int maxPartners = 1; > 119 int minDistance = 1; > 120 int RetVal_[] = {1, 10 }; > 121 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 122 return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartner > 123 int Test_(Case_<1>) { > 124 int x_[] = {0,0,10}; > 125 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 126 int y_[] = {0,10,4}; > 127 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 128 int maxPartners = 1; > 129 int minDistance = 11; > 130 int RetVal_[] = {1, 14 }; > 131 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 132 return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartner > 133 int Test_(Case_<2>) { > 134 int x_[] = {0,10,0,10}; > 135 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 136 int y_[] = {0,0,20,20}; > 137 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 138 int maxPartners = 1; > 139 int minDistance = 1; > 140 int RetVal_[] = {2, 20 }; > 141 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 142 return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartner > 143 int Test_(Case_<3>) { > 144 int x_[] = {0,10,0,10}; > 145 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 146 int y_[] = {0,0,20,20}; > 147 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 148 int maxPartners = 2; > 149 int minDistance = 10; > 150 int RetVal_[] = {4, 60 }; > 151 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 152 return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartner > 153 int Test_(Case_<4>) { > 154 int x_[] = {0,0,0,0,0,0,0,0,0}; > 155 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 156 int y_[] = {1,2,3,4,5,6,7,8,9}; > 157 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 158 int maxPartners = 3; > 159 int minDistance = 6; > 160 int RetVal_[] = {6, 40 }; > 161 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 162 return verify_case(RetVal, TwinTowns().optimalTwinTowns(x, y, maxPartner > 163 > 164 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 165 template<> void Run_<-1>() {} > 166 int main() { Run_<0>(); } > 167 // END CUT HERE > 168
Added SRM/430/1C.cpp version [0d8ab1eb2aa0ceeb]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <functional> > 16 #include <cassert> > 17 using namespace std; > 18 typedef long long LL; > 19 typedef LL BITS; > 20 > 21 class PickingUp > 22 { > 23 public: > 24 vector<int> fairPickingUp(vector<long long> score1, vector<long long> sc > 25 { > 26 LL t = accumulate(score2.begin(), score2.end(), 0LL); > 27 vector<LL> s; > 28 transform(score1.begin(), score1.end(), score2.begin(), back_ins > 29 > 30 int N = s.size() / 2; > 31 > 32 vector< map<LL,BITS> > L = try_all( s.begin(), N ); > 33 vector< map<LL,BITS> > R = try_all( s.begin()+N, N ); > 34 > 35 pair<LL, BITS> best(0x7fffffffffffffffLL, 0); > 36 for(int i=0; i<=N; ++i) > 37 best = min(best, best_possible( L[i], R[N-i], t, N )); > 38 > 39 vector<int> ans; > 40 for(int i=0; i!=s.size(); ++i) > 41 ans.push_back( (-best.second & (1LL<<s.size()-1-i)) ? 1 > 42 return ans; > 43 } > 44 > 45 pair<LL, BITS> best_possible( map<LL,BITS>& L, map<LL,BITS>& R, LL t, in > 46 { > 47 typedef map<LL,BITS>::iterator mit; > 48 > 49 pair<LL, BITS> best(0x7fffffffffffffffLL, 0); > 50 for(mit i=L.begin(); i!=L.end(); ++i) > 51 { > 52 LL as = i->first; > 53 LL am = i->second; > 54 mit j = R.lower_bound(t-as); > 55 if( j!=R.end() ) { > 56 LL bs = j->first; > 57 LL bm = j->second; > 58 best = min(best, make_pair(abs(as+bs-t), -((am<< > 59 } > 60 if( j!=R.begin() ) { > 61 --j; > 62 LL bs = j->first; > 63 LL bm = j->second; > 64 best = min(best, make_pair(abs(as+bs-t), -((am<< > 65 } > 66 } > 67 > 68 return best; > 69 } > 70 > 71 template<typename Ite> > 72 vector< map<LL,BITS> > try_all( Ite s, int N ) > 73 { > 74 vector< map<LL,BITS> > a(N+1); > 75 for(BITS m=(1<<N)-1; m>=0; --m) > 76 { > 77 int nb = 0; > 78 LL sm = 0; > 79 for(int i=0; i<N; ++i) > 80 if( m & (1LL<<N-1-i) ) > 81 ++nb, sm+=*(s+i); > 82 if( !a[nb].count(sm) ) > 83 a[nb][sm] = m; > 84 } > 85 return a; > 86 } > 87 }; > 88 > 89 // BEGIN CUT HERE > 90 #include <ctime> > 91 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 92 > 93 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 94 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 95 > 96 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 97 char Test_(...); > 98 int Test_(Case_<0>) { > 99 long score1_[] = {5,9}; > 100 vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*scor > 101 long score2_[] = {8,6}; > 102 vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*scor > 103 int RetVal_[] = {1, 2 }; > 104 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 105 return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } > 106 int Test_(Case_<1>) { > 107 long score1_[] = {2,3,4,7}; > 108 vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*scor > 109 long score2_[] = {2,4,5,8}; > 110 vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*scor > 111 int RetVal_[] = {1, 2, 2, 1 }; > 112 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 113 return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } > 114 int Test_(Case_<2>) { > 115 long score1_[] = {1,5,6,8}; > 116 vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*scor > 117 long score2_[] = {7,5,3,1}; > 118 vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*scor > 119 int RetVal_[] = {1, 2, 1, 2 }; > 120 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 121 return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } > 122 int Test_(Case_<3>) { > 123 long score1_[] = {300,300,300,300}; > 124 vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*scor > 125 long score2_[] = {600,10,10,10}; > 126 vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*scor > 127 int RetVal_[] = {2, 1, 1, 2 }; > 128 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 129 return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } > 130 int Test_(Case_<4>) { > 131 long score1_[] = {50,50,50,1}; > 132 vector<long long> score1(score1_, score1_+sizeof(score1_)/sizeof(*scor > 133 long score2_[] = {30,30,30,150}; > 134 vector<long long> score2(score2_, score2_+sizeof(score2_)/sizeof(*scor > 135 int RetVal_[] = {1, 2, 2, 1 }; > 136 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 137 return verify_case(RetVal, PickingUp().fairPickingUp(score1, score2)); } > 138 > 139 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 140 template<> void Run_<-1>() {} > 141 int main() { Run_<0>(); } > 142 // END CUT HERE > 143
Added SRM/431/1A.cpp version [b3187b46cfd5d3bb]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 using namespace std; > 17 typedef long long LL; > 18 > 19 class LaserShooting > 20 { > 21 public: > 22 double numberOfHits(vector <int> x, vector <int> y1, vector <int> y2) > 23 { > 24 double ans = 0.0; > 25 for(int i=0; i<x.size(); ++i) > 26 { > 27 double a1 = atan2(min(y1[i],y2[i]), x[i]); > 28 double a2 = atan2(max(y1[i],y2[i]), x[i]); > 29 ans += (a2-a1) / M_PI; > 30 } > 31 return ans; > 32 } > 33 }; > 34 > 35 // BEGIN CUT HERE > 36 #include <ctime> > 37 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 38 > 39 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 40 int verify_case(const double &Expected, const double &Received) { double diff = > 41 > 42 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 43 char Test_(...); > 44 int Test_(Case_<0>) { > 45 int x_[] = {1}; > 46 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 47 int y1_[] = {-1}; > 48 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 49 int y2_[] = {1}; > 50 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 51 double RetVal = 0.5; > 52 return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } > 53 int Test_(Case_<1>) { > 54 int x_[] = {1,2}; > 55 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 56 int y1_[] = {-1,-2}; > 57 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 58 int y2_[] = {1,2}; > 59 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 60 double RetVal = 1.0; > 61 return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } > 62 int Test_(Case_<2>) { > 63 int x_[] = {3,4,7,1}; > 64 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 65 int y1_[] = {1,2,3,4}; > 66 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 67 int y2_[] = {4,3,2,1}; > 68 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 69 double RetVal = 0.4623163952488826; > 70 return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } > 71 int Test_(Case_<3>) { > 72 int x_[] = {134,298,151,942}; > 73 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 74 int y1_[] = {-753,-76,19,568}; > 75 vector <int> y1(y1_, y1_+sizeof(y1_)/sizeof(*y1_)); > 76 int y2_[] = {440,689,-39,672}; > 77 vector <int> y2(y2_, y2_+sizeof(y2_)/sizeof(*y2_)); > 78 double RetVal = 1.444210260641501; > 79 return verify_case(RetVal, LaserShooting().numberOfHits(x, y1, y2)); } > 80 > 81 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 82 template<> void Run_<-1>() {} > 83 int main() { Run_<0>(); } > 84 // END CUT HERE > 85
Added SRM/431/1B.cpp version [b5f1e84102a9aa7f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 static const LL MODVAL = 1000000007; > 21 int memo[10][11][1001][9]; > 22 > 23 class MegaCoolNumbers > 24 { > 25 public: > 26 int count(int N, int A) > 27 { > 28 memset(memo, -1, sizeof(memo)); > 29 return rec(1, 10, N, A); > 30 } > 31 > 32 int rec(int D, int F, int N, int A) > 33 { > 34 if( A==0 ) return N==0 ? 1 : 0; > 35 if( N < A ) return 0; > 36 if( N==1 ) return 10-D - (F==10 ? 0 : 1); > 37 if( 10-D < A ) return 0; > 38 if( memo[D][F][N][A] != -1 ) > 39 return memo[D][F][N][A]; > 40 > 41 LL ans = 0; > 42 for(int d=D; d<=9; ++d) if(d!=F) > 43 for(int k=2; k<=N; ++k) > 44 for(int q=0; d+(k-1)*q<=9; ++q) > 45 ans += rec(d+(k-1)*q, min(10, d+k*q), N- > 46 return memo[D][F][N][A] = int(ans % MODVAL); > 47 } > 48 }; > 49 > 50 // BEGIN CUT HERE > 51 #include <ctime> > 52 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 53 > 54 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 55 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 56 > 57 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 58 char Test_(...); > 59 int Test_(Case_<0>) { > 60 int N = 1; > 61 int A = 1; > 62 int RetVal = 9; > 63 return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } > 64 int Test_(Case_<1>) { > 65 int N = 2; > 66 int A = 1; > 67 int RetVal = 45; > 68 return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } > 69 int Test_(Case_<2>) { > 70 int N = 2; > 71 int A = 2; > 72 int RetVal = 0; > 73 return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } > 74 int Test_(Case_<3>) { > 75 int N = 10; > 76 int A = 3; > 77 int RetVal = 7502; > 78 return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } > 79 int Test_(Case_<4>) { > 80 int N = 1000; > 81 int A = 5; > 82 int RetVal = 7502; > 83 return verify_case(RetVal, MegaCoolNumbers().count(N, A)); } > 84 > 85 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 86 template<> void Run_<-1>() {} > 87 int main() { Run_<0>(); } > 88 // END CUT HERE > 89
Added SRM/431/1C.cpp version [c220b48258d1e633]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 #include <cstdlib> > 18 using namespace std; > 19 typedef long long LL; > 20 > 21 template<typename T> > 22 struct RMQ > 23 { > 24 vector< vector<int> > rm; > 25 T* d; > 26 int n; > 27 > 28 RMQ( T* d, int n ) : d(d), n(n) { > 29 rm.push_back( vector<int>(n) ); > 30 for(int x=0; x<n; ++x) > 31 rm[0][x] = x; > 32 for(int k=1; (1<<k)<=n; ++k) { > 33 rm.push_back( rm[k-1] ); > 34 for(int x=0; x+(1<<k-1)<n; ++x) > 35 if( d[rm[k][x]] > d[rm[k-1][x + (1<<k-1)]] ) > 36 rm[k][x] = rm[k-1][x + (1<<k-1)]; > 37 } > 38 } > 39 > 40 int operator()(int L, int R) const { > 41 int k=0; > 42 for(; L+(1<<k) < R-(1<<k)+1; ++k) {} > 43 LL i = rm[k][L]; > 44 LL j = rm[k][R-(1<<k)+1]; > 45 return (d[i]<=d[j] ? i : j); > 46 } > 47 > 48 vector<int> all(int L, int R) const { > 49 vector<int> ans; > 50 int minValue = d[(*this)(L, R)]; > 51 while( L <= R ) { > 52 int C = (*this)(L, R); > 53 if( minValue < d[C] ) > 54 break; > 55 ans.push_back(C); > 56 L = C+1; > 57 } > 58 return ans; > 59 } > 60 }; > 61 > 62 int B[2048][2048]; > 63 int U[2048][2048]; > 64 int D[2048][2048]; > 65 > 66 class PerfectRectangles > 67 { > 68 public: > 69 int numberOfRectangles(int N, int M, int X0, int A, int _B, int Y0, int > 70 { > 71 // input > 72 int x=X0%N, y=Y0%N; > 73 memset(B, 0, sizeof(B)); > 74 for(int i=0; i<M; ++i) { > 75 B[x][y] = 1; > 76 x = (x*A+_B)%N; > 77 y = (y*C+_D)%N; > 78 } > 79 > 80 // up > 81 for(int y=0; y<N; ++y) > 82 for(int x=0; x<N; ++x) > 83 U[y][x] = (B[y][x] ? 0 : y==0 ? 1 : U[y-1][x]+1) > 84 > 85 // down > 86 for(int y=N-1; y>=0; --y) > 87 for(int x=0; x<N; ++x) > 88 D[y][x] = (B[y][x] ? 0 : y==N-1 ? 1 : D[y+1][x]+ > 89 > 90 // solve > 91 int cnt = 0; > 92 for(int y=0; y<N; ++y) { > 93 RMQ<int> rmU(U[y], N); > 94 RMQ<int> rmD(D[y], N); > 95 > 96 stack< pair<int,int> > S; > 97 S.push( make_pair(0, N-1) ); > 98 while( !S.empty() ) { > 99 int L = S.top().first; > 100 int R = S.top().second; > 101 S.pop(); > 102 > 103 int d = rmD(L,R); > 104 if( D[y][d] == 1 ) > 105 ++cnt; > 106 if( D[y][d] <= 1 ) { > 107 vector<int> m = rmU.all(L, R); > 108 m.push_back(R+1); > 109 for(int i=0; i<m.size(); L=m[i++]+1) > 110 if( L <= m[i]-1 ) > 111 S.push( make_pair(L, m[i > 112 } > 113 } > 114 } > 115 return cnt; > 116 } > 117 }; > 118 > 119 // BEGIN CUT HERE > 120 #include <ctime> > 121 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 122 > 123 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 124 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 125 > 126 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 127 char Test_(...); > 128 int Test_(Case_<0>) { > 129 int N = 5; > 130 int M = 1; > 131 int X0 = 2; > 132 int A = 0; > 133 int B = 0; > 134 int Y0 = 2; > 135 int C = 0; > 136 int D = 0; > 137 int RetVal = 4; > 138 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 139 int Test_(Case_<1>) { > 140 int N = 4; > 141 int M = 4; > 142 int X0 = 0; > 143 int A = 1; > 144 int B = 1; > 145 int Y0 = 0; > 146 int C = 1; > 147 int D = 1; > 148 int RetVal = 6; > 149 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 150 int Test_(Case_<2>) { > 151 int N = 1; > 152 int M = 1000000; > 153 int X0 = 1; > 154 int A = 2; > 155 int B = 3; > 156 int Y0 = 1; > 157 int C = 4; > 158 int D = 7; > 159 int RetVal = 0; > 160 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 161 int Test_(Case_<3>) { > 162 int N = 10; > 163 int M = 20; > 164 int X0 = 4; > 165 int A = 76; > 166 int B = 2; > 167 int Y0 = 6; > 168 int C = 2; > 169 int D = 43; > 170 int RetVal = 12; > 171 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 172 int Test_(Case_<4>) { > 173 int N = 1999; > 174 int M = 4000000; > 175 int X0 = 1000; > 176 int A = 1; > 177 int B = 1; > 178 int Y0 = 0; > 179 int C = 1; > 180 int D = 1; > 181 int RetVal = 1002995; > 182 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 183 int Test_(Case_<5>) { > 184 int N = 2000; > 185 int M = 4000000; > 186 int X0 = 1500; > 187 int A = 1; > 188 int B = 1; > 189 int Y0 = 500; > 190 int C = 1; > 191 int D = 1; > 192 int RetVal = 1003997; > 193 return verify_case(RetVal, PerfectRectangles().numberOfRectangles(N, M, > 194 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 195 template<> void Run_<-1>() {} > 196 int main() { Run_<0>(); } > 197 // END CUT HERE > 198
Added SRM/432/1A.cpp version [329bdbe17af21f45]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class LampsGrid { > 21 public: > 22 int curMax; > 23 > 24 int mostLit(vector <string> initial, int K) > 25 { > 26 // atama-warui > 27 // only meaningful way to flip lights are > 28 // that: exists i {j | initial[i][j]=='0'} > 29 // so at most initial.size() try is sufficient... > 30 > 31 vector<LL> s; > 32 for(int j=0; j<initial[0].size(); ++j) { > 33 LL x = 0; > 34 for(int i=0; i<initial.size(); ++i) > 35 if( initial[i][j]=='1' ) > 36 x |= (1LL << i); > 37 s.push_back(x); > 38 } > 39 > 40 curMax = 0; > 41 for(; K>=0; K-=2) > 42 if( K <= initial[0].size() ) > 43 rec((1LL<<initial.size())-1, s, 0, K); > 44 return curMax; > 45 } > 46 > 47 void rec(LL lit, vector<LL> s, int i, int K) // exact K flips > 48 { > 49 int c = 0; > 50 for(LL j=1; j<=lit; j<<=1) > 51 if( lit&j ) > 52 ++c; > 53 if( c<=curMax ) > 54 return; > 55 if( i == s.size() ) > 56 { > 57 curMax = c; > 58 return; > 59 } > 60 > 61 if( i+K < s.size() ) // noflip > 62 rec( lit&s[i], s, i+1, K ); > 63 if( K ) // flip > 64 rec( lit&~s[i], s, i+1, K-1 ); > 65 } > 66 }; > 67 > 68 // BEGIN CUT HERE > 69 #include <ctime> > 70 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 71 > 72 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 73 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 74 > 75 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 76 char Test_(...); > 77 int Test_(Case_<0>) { > 78 string initial_[] = {"01", > 79 "10", > 80 "10"}; > 81 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 82 int K = 1; > 83 int RetVal = 2; > 84 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 85 int Test_(Case_<1>) { > 86 string initial_[] = {"101010"}; > 87 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 88 int K = 2; > 89 int RetVal = 0; > 90 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 91 int Test_(Case_<2>) { > 92 string initial_[] = {"00", "11"}; > 93 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 94 int K = 999; > 95 int RetVal = 0; > 96 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 97 int Test_(Case_<3>) { > 98 string initial_[] = {"0", "1", "0", "1", "0"} > 99 ; > 100 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 101 int K = 1000; > 102 int RetVal = 2; > 103 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 104 int Test_(Case_<4>) { > 105 string initial_[] = {"001", "101", "001", "000", "111", "001", "101", "1 > 106 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 107 int K = 6; > 108 int RetVal = 4; > 109 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 110 int Test_(Case_<5>) { > 111 string initial_[] = {"01", "10", "01", "01", "10"}; > 112 vector <string> initial(initial_, initial_+sizeof(initial_)/sizeof(*in > 113 int K = 1; > 114 int RetVal = 3; > 115 return verify_case(RetVal, LampsGrid().mostLit(initial, K)); } > 116 > 117 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 118 template<> void Run_<-1>() {} > 119 int main() { Run_<0>(); } > 120 // END CUT HERE > 121
Added SRM/432/1B.cpp version [22831069cfdf7e1f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class GroupedWord { > 21 public: > 22 string restore(vector <string> parts) > 23 { > 24 while( reduce(parts) ) {} > 25 > 26 if( parts.size() == 1 ) > 27 return gd(parts[0]) ? parts[0] : "IMPOSSIBLE"; > 28 > 29 string s = accumulate(parts.begin(), parts.end(), string("")); > 30 return gd(s) ? "MANY" : "IMPOSSIBLE"; > 31 } > 32 > 33 bool reduce( vector<string>& p ) > 34 { > 35 for(int j=0; j<p.size(); ++j) { > 36 string& s = p[j]; > 37 > 38 char last = s[s.size()-1]; > 39 for(int i=0; i<p.size(); ++i) if(i!=j) > 40 if( count(p[i].begin(), p[i].end(), last) == p[i > 41 { > 42 p.push_back(s+p[i]); > 43 p.erase(p.begin()+max(i,j)); > 44 p.erase(p.begin()+min(i,j)); > 45 return true; > 46 } > 47 > 48 for(int i=0; i<p.size(); ++i) if(i!=j) > 49 if( p[i][0] == last ) > 50 { > 51 p.push_back(s+p[i]); > 52 p.erase(p.begin()+max(i,j)); > 53 p.erase(p.begin()+min(i,j)); > 54 return true; > 55 } > 56 } > 57 return false; > 58 } > 59 > 60 bool gd(const string& s) > 61 { > 62 for(int i=0; i<s.size(); ) > 63 { > 64 int j=i; > 65 while( j<s.size() && s[i]==s[j] ) ++j; > 66 if( s.find(s[i], j)!=string::npos ) > 67 return false; > 68 i=j; > 69 } > 70 return true; > 71 } > 72 }; > 73 > 74 // BEGIN CUT HERE > 75 #include <ctime> > 76 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 77 > 78 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 79 int verify_case(const string &Expected, const string &Received) { if (Expected = > 80 > 81 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 82 char Test_(...); > 83 int Test_(Case_<0>) { > 84 string parts_[] = {"aaa", "a", "aa"}; > 85 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 86 string RetVal = "aaaaaa"; > 87 return verify_case(RetVal, GroupedWord().restore(parts)); } > 88 int Test_(Case_<1>) { > 89 string parts_[] = {"ab", "bba"}; > 90 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 91 string RetVal = "IMPOSSIBLE"; > 92 return verify_case(RetVal, GroupedWord().restore(parts)); } > 93 int Test_(Case_<2>) { > 94 string parts_[] = {"te", "st"}; > 95 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 96 string RetVal = "stte"; > 97 return verify_case(RetVal, GroupedWord().restore(parts)); } > 98 int Test_(Case_<3>) { > 99 string parts_[] = {"te", "s", "t"}; > 100 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 101 string RetVal = "MANY"; > 102 return verify_case(RetVal, GroupedWord().restore(parts)); } > 103 int Test_(Case_<4>) { > 104 string parts_[] = {"orr", "rd", "woo", "www"}; > 105 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 106 string RetVal = "wwwwooorrrd"; > 107 return verify_case(RetVal, GroupedWord().restore(parts)); } > 108 int Test_(Case_<5>) { > 109 string parts_[] = {"abcb"}; > 110 vector <string> parts(parts_, parts_+sizeof(parts_)/sizeof(*parts_)); > 111 string RetVal = "IMPOSSIBLE"; > 112 return verify_case(RetVal, GroupedWord().restore(parts)); } > 113 > 114 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 115 template<> void Run_<-1>() {} > 116 int main() { Run_<0>(); } > 117 // END CUT HERE > 118
Added SRM/432/1C.cpp version [31dd781df2b6765d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 struct union_find { > 21 vector<int> p, s; > 22 union_find(int N) : p(N, -1), s(N, 1) {} > 23 > 24 int repr(int u) { > 25 while( p[u]>=0 ) u=p[u]; > 26 return u; > 27 } > 28 bool conn(int u, int v) { > 29 u = repr(u); > 30 v = repr(v); > 31 if( u==v ) return false; > 32 if( s[u] < s[v] ) { p[u]=v; s[v]+=s[u]; } > 33 else { p[v]=u; s[u]+=s[v]; } > 34 return true; > 35 } > 36 }; > 37 > 38 class BuildersCountry { > 39 public: > 40 long long minCost(vector <int> before, vector <int> after, vector <int> > 41 { > 42 int N = before.size(); > 43 > 44 LL baseCost = 0; > 45 > 46 vector< vector<LL> > cost(N, vector<LL>(N)); > 47 for(int i=0; i!=N; ++i) > 48 for(int j=i+1; j!=N; ++j) > 49 { LL cc = LL(roadCost)*(before[i]+before[j]); > 50 > 51 int mi = houseCost[i]<houseCost[j] ? i : j; > 52 int mj = houseCost[i]<houseCost[j] ? j : i; > 53 LL cc2 = LL(houseCost[mj])*(after[mj]-before[mj] > 54 + LL(houseCost[mi])*(after[mi]-before[mi] > 55 > 56 cost[i][j] = cost[j][i] = cc+cc2; > 57 > 58 if( g[i][j] == 'Y' ) > 59 baseCost += cc2; > 60 } > 61 > 62 for(int i=0; i!=N; ++i) > 63 baseCost += LL(houseCost[i]) * LL(before[i]+after[i]-1) > 64 return baseCost + mst(cost, g, N); > 65 } > 66 > 67 LL mst(vector<vector<LL> >& c, vector<string>& g, int N) > 68 { > 69 union_find uf(N); > 70 for(int i=0; i!=N; ++i) > 71 for(int j=i+1; j!=N; ++j) > 72 if( g[i][j]=='Y' ) > 73 uf.conn(i,j); > 74 > 75 typedef pair<LL,pair<int,int> > cedge; > 76 vector<cedge> es; > 77 for(int i=0; i!=N; ++i) > 78 for(int j=i+1; j!=N; ++j) > 79 es.push_back( make_pair( c[i][j], make_pair(i,j) > 80 sort(es.begin(), es.end()); > 81 > 82 LL cc = 0; > 83 for(int i=0; i!=es.size(); ++i) > 84 { > 85 int u = es[i].second.first; > 86 int v = es[i].second.second; > 87 LL c = es[i].first; > 88 if( uf.conn(u,v) ) > 89 cc += c; > 90 } > 91 return cc; > 92 } > 93 }; > 94 > 95 // BEGIN CUT HERE > 96 #include <ctime> > 97 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 98 > 99 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 100 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 101 > 102 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 103 char Test_(...); > 104 int Test_(Case_<0>) { > 105 int before_[] = {2, 1, 3, 5}; > 106 vector <int> before(before_, before_+sizeof(before_)/sizeof(*before_)) > 107 int after_[] = {2, 1, 3, 5}; > 108 vector <int> after(after_, after_+sizeof(after_)/sizeof(*after_)); > 109 int houseCost_[] = {4, 5, 3, 2}; > 110 vector <int> houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeo > 111 string g_[] = {"NNNN", "NNNN", "NNNN", "NNNN"}; > 112 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 113 int roadCost = 1000; > 114 long long RetVal = 13000LL; > 115 return verify_case(RetVal, BuildersCountry().minCost(before, after, hous > 116 int Test_(Case_<1>) { > 117 int before_[] = {1, 1, 1, 1}; > 118 vector <int> before(before_, before_+sizeof(before_)/sizeof(*before_)) > 119 int after_[] = {1, 3, 1, 2}; > 120 vector <int> after(after_, after_+sizeof(after_)/sizeof(*after_)); > 121 int houseCost_[] = {8, 5, 3, 2}; > 122 vector <int> houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeo > 123 string g_[] = {"NYNN", "YNYN", "NYNY", "NNYN"}; > 124 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 125 int roadCost = 100000; > 126 long long RetVal = 39LL; > 127 return verify_case(RetVal, BuildersCountry().minCost(before, after, hous > 128 int Test_(Case_<2>) { > 129 int before_[] = {9, 11}; > 130 vector <int> before(before_, before_+sizeof(before_)/sizeof(*before_)) > 131 int after_[] = {10, 11}; > 132 vector <int> after(after_, after_+sizeof(after_)/sizeof(*after_)); > 133 int houseCost_[] = {5, 1}; > 134 vector <int> houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeo > 135 string g_[] = {"NN", "NN"}; > 136 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 137 int roadCost = 15; > 138 long long RetVal = 400LL; > 139 return verify_case(RetVal, BuildersCountry().minCost(before, after, hous > 140 int Test_(Case_<3>) { > 141 int before_[] = {1}; > 142 vector <int> before(before_, before_+sizeof(before_)/sizeof(*before_)) > 143 int after_[] = {1000}; > 144 vector <int> after(after_, after_+sizeof(after_)/sizeof(*after_)); > 145 int houseCost_[] = {2}; > 146 vector <int> houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeo > 147 string g_[] = {"N"}; > 148 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 149 int roadCost = 888; > 150 long long RetVal = 999000LL; > 151 return verify_case(RetVal, BuildersCountry().minCost(before, after, hous > 152 int Test_(Case_<4>) { > 153 int before_[] = {99, 23, 44, 55, 32}; > 154 vector <int> before(before_, before_+sizeof(before_)/sizeof(*before_)) > 155 int after_[] = {99, 23, 44, 55, 32}; > 156 vector <int> after(after_, after_+sizeof(after_)/sizeof(*after_)); > 157 int houseCost_[] = {39, 32, 11, 23, 89}; > 158 vector <int> houseCost(houseCost_, houseCost_+sizeof(houseCost_)/sizeo > 159 string g_[] = {"NYNNN", "YNNNY", "NNNYY", "NNYNY", "NYYYN"}; > 160 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 161 int roadCost = 54; > 162 long long RetVal = 0LL; > 163 return verify_case(RetVal, BuildersCountry().minCost(before, after, hous > 164 > 165 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 166 template<> void Run_<-1>() {} > 167 int main() { Run_<0>(); } > 168 // END CUT HERE > 169
Added SRM/433/1A.cpp version [2697f802cd1ec7d5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class MagicWords { > 21 public: > 22 int count(vector <string> S, int K) > 23 { > 24 int cnt = 0; > 25 > 26 int idx[] = {0,1,2,3,4,5,6,7}; > 27 do { > 28 string s; for(int i=0; i<S.size(); ++i) s += S[idx[i]]; > 29 > 30 if( s.size()%K==0 && rep_of(s, s.size()/K) ) > 31 { > 32 for(int n=1; n<s.size()/K; ++n) > 33 if( s.size()%n==0 && rep_of(s, n) ) > 34 goto fail; > 35 ++cnt; > 36 fail:; > 37 } > 38 } while( next_permutation(idx+0, idx+S.size()) ); > 39 > 40 return cnt; > 41 } > 42 > 43 bool rep_of( string& s, int n ) { > 44 return s == s.substr(n, s.size()-n) + s.substr(0, n); > 45 } > 46 }; > 47 > 48 // BEGIN CUT HERE > 49 #include <ctime> > 50 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 51 > 52 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 53 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 54 > 55 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 56 char Test_(...); > 57 int Test_(Case_<0>) { > 58 string S_[] = {"CAD","ABRA","ABRA"}; > 59 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 60 int K = 1; > 61 int RetVal = 6; > 62 return verify_case(RetVal, MagicWords().count(S, K)); } > 63 int Test_(Case_<1>) { > 64 string S_[] = {"AB","RAAB","RA"}; > 65 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 66 int K = 2; > 67 int RetVal = 3; > 68 return verify_case(RetVal, MagicWords().count(S, K)); } > 69 int Test_(Case_<2>) { > 70 string S_[] = {"AA","AA","AAA","A"}; > 71 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 72 int K = 1; > 73 int RetVal = 0; > 74 return verify_case(RetVal, MagicWords().count(S, K)); } > 75 int Test_(Case_<3>) { > 76 string S_[] = {"AA","AA","AAA","A","AAA","AAAA"}; > 77 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 78 int K = 15; > 79 int RetVal = 720; > 80 return verify_case(RetVal, MagicWords().count(S, K)); } > 81 int Test_(Case_<4>) { > 82 string S_[] = {"ABC","AB","ABC","CA"}; > 83 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 84 int K = 3; > 85 int RetVal = 0; > 86 return verify_case(RetVal, MagicWords().count(S, K)); } > 87 int Test_(Case_<5>) { > 88 string S_[] = {"A","B","C","A","B","C"}; > 89 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 90 int K = 1; > 91 int RetVal = 672; > 92 return verify_case(RetVal, MagicWords().count(S, K)); } > 93 int Test_(Case_<6>) { > 94 string S_[] = {"AAAAAAAAAAAAAAAAAAAA", > 95 "AAAAAAAAAAAAAAAAAAAA", > 96 "AAAAAAAAAAAAAAAAAAAA", > 97 "AAAAAAAAAAAAAAAAAAAA", > 98 "AAAAAAAAAAAAAAAAAAAA", > 99 "AAAAAAAAAAAAAAAAAAAA", > 100 "AAAAAAAAAAAAAAAAAAAA", > 101 "AAAAAAAAAAAAAAAAAAAB"}; > 102 vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); > 103 int K = 1; > 104 int RetVal = 40320; > 105 return verify_case(RetVal, MagicWords().count(S, K)); } > 106 > 107 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 108 template<> void Run_<-1>() {} > 109 int main() { Run_<0>(); } > 110 // END CUT HERE > 111
Added SRM/433/1B.cpp version [6a8dec603d2a8fec]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int gcd(int a, int b) { while(a) swap(a,b%=a); return b; } > 23 > 24 class SettingTents { > 25 public: > 26 int countSites(int N, int M) > 27 { > 28 int cnt = 0; > 29 for(int w=1; w<=N; ++w) // for each > 30 for(int h=1; h<=M; ++h) // bounding box... > 31 { > 32 int numWH = (N+1-w)*(M+1-h); > 33 > 34 // w/2 - k(h-2j) = x > 35 // h/2 + kw = y > 36 for(int j=1; j<h; ++j) // the case when both two > 37 if( (w*w+h*(h-2*j)) % (2*w) == 0 ) > 38 { > 39 int x = (w*w+h*(h-2*j)) / (2*w); > 40 if( 0<=x && x<=w ) > 41 cnt += numWH; > 42 } > 43 > 44 for(int y=0; 2*y<h; ++y) // the case when only o > 45 if( (w*w - (2*y-h)*h) % (2*w) == 0 ) > 46 { > 47 int x = (w*w - (2*y-h)*h) / (2*w > 48 if( x<w ) > 49 cnt += 2*numWH; > 50 } > 51 > 52 if( w==h ) // special case > 53 cnt += numWH; > 54 } > 55 return cnt; > 56 } > 57 }; > 58 > 59 // BEGIN CUT HERE > 60 #include <ctime> > 61 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 62 > 63 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 64 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 65 > 66 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 67 char Test_(...); > 68 int Test_(Case_<0>) { > 69 int N = 2; > 70 int M = 2; > 71 int RetVal = 6; > 72 return verify_case(RetVal, SettingTents().countSites(N, M)); } > 73 int Test_(Case_<1>) { > 74 int N = 1; > 75 int M = 6; > 76 int RetVal = 6; > 77 return verify_case(RetVal, SettingTents().countSites(N, M)); } > 78 int Test_(Case_<2>) { > 79 int N = 6; > 80 int M = 8; > 81 int RetVal = 527; > 82 return verify_case(RetVal, SettingTents().countSites(N, M)); } > 83 > 84 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 85 template<> void Run_<-1>() {} > 86 int main() { Run_<0>(); } > 87 // END CUT HERE > 88
Added SRM/433/1C.cpp version [bfd8440d84a22d10]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL SHIFT = 1000000000LL; > 23 static const LL INF = 1000001LL; > 24 static const LL MAX = 1000000LL; > 25 static const int NV = 5002; > 26 typedef LL flow; > 27 typedef int vert; > 28 typedef vert edge; > 29 typedef vector<edge> edges; > 30 typedef vector<edges> graph; > 31 //typedef flow flow_graph[NV][NV]; > 32 typedef map< vert,map<vert,flow> > flow_graph; > 33 > 34 flow dinic_dfs( graph& G, flow_graph& F, vert v, vert D, > 35 int LV[], flow flow_in, int blocked[] ) > 36 { > 37 flow flow_out = 0; > 38 for(int i=0; i!=G[v].size(); ++i) { > 39 int u = G[v][i]; > 40 if( LV[v]+1==LV[u] && F[v][u] ) { > 41 flow f = min(flow_in-flow_out, F[v][u]); > 42 if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blo > 43 F[v][u] -= f; > 44 F[u][v] += f; > 45 flow_out += f; > 46 if( flow_in == flow_out ) return flow_out; > 47 } > 48 } > 49 } > 50 blocked[v] = (flow_out==0); > 51 return flow_out; > 52 } > 53 > 54 flow maxFlow( graph& G, flow_graph& F, vert S, vert D ) > 55 { > 56 for( flow total=0 ;; ) { > 57 int LV[NV] = {0}; > 58 vector<int> Q(1, S); > 59 for(int lv=1; !Q.empty(); ++lv) { > 60 vector<int> Q2; > 61 for(int i=0; i!=Q.size(); ++i) { > 62 edges& ne = G[Q[i]]; > 63 for(int j=0; j!=ne.size(); ++j) > 64 if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j > 65 LV[ne[j]]=lv, Q2.push_back(ne[j] > 66 } > 67 Q.swap(Q2); > 68 } > 69 > 70 if( !LV[D] ) > 71 return total; > 72 > 73 int blocked[NV] = {}; > 74 total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); > 75 } > 76 } > 77 > 78 flow_graph F; > 79 class BarbarianInvasion { > 80 public: > 81 int minimalDetachment(vector <string> countryMap, vector <int> detachmen > 82 { > 83 int H = countryMap.size(); > 84 int W = countryMap[0].size(); > 85 vert OUT = 0, CAP = 1; > 86 > 87 graph G(H*W*2+2); > 88 flow_graph F; > 89 > 90 for(int y=0; y<H; ++y) > 91 for(int x=0; x<W; ++x) > 92 { > 93 if( countryMap[y][x] == '-' ) > 94 continue; > 95 > 96 vert CUR_A = countryMap[y][x] == '*' ? CAP : (y*W+x)*2+2 > 97 vert CUR_B = countryMap[y][x] == '*' ? CAP : (y*W+x)*2+3 > 98 if( countryMap[y][x] != '*' && countryMap[y][x] != '-' ) > 99 G[CUR_A].push_back(CUR_B); > 100 G[CUR_B].push_back(CUR_A); > 101 F[CUR_A][CUR_B] = SHIFT + detachmentSize[country > 102 F[CUR_B][CUR_A] = 0; > 103 } > 104 > 105 int DY[] = {-1,+1,0,0}, DX[]={0,0,-1,+1}; > 106 for(int d=0; d<4; ++d) > 107 { > 108 int dy = DY[d], dx = DX[d]; > 109 if( 0<=y+dy && y+dy<H && 0<=x+dx && x+dx<W ) > 110 { > 111 if( countryMap[y+dy][x+dx] == '-' ) > 112 continue; > 113 if( countryMap[y+dy][x+dx] == '*' ) > 114 continue; > 115 vert ADJ_B = ((y+dy)*W+(x+dx))*2+3; > 116 G[ADJ_B].push_back(CUR_A); > 117 G[CUR_A].push_back(ADJ_B); > 118 F[ADJ_B][CUR_A] = SHIFT+INF; > 119 F[CUR_A][ADJ_B] = 0; > 120 } > 121 else > 122 { > 123 G[OUT].push_back(CUR_A); > 124 G[CUR_A].push_back(OUT); > 125 F[OUT][CUR_A] = SHIFT+INF; > 126 F[CUR_A][OUT] = 0; > 127 } > 128 } > 129 } > 130 > 131 flow mf = maxFlow(G, F, OUT, CAP); > 132 return mf%SHIFT; > 133 } > 134 }; > 135 > 136 // BEGIN CUT HERE > 137 #include <ctime> > 138 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 139 > 140 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 141 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 142 > 143 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 144 char Test_(...); > 145 int Test_(Case_<0>) { > 146 string countryMap_[] = {"ABA", > 147 "A*A", > 148 "AAA"}; > 149 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 150 int detachmentSize_[] = {1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 > 151 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 152 int RetVal = 5; > 153 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 154 int Test_(Case_<1>) { > 155 string countryMap_[] = {"CCCC", > 156 "-BAC", > 157 "-*AC", > 158 "--AC"}; > 159 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 160 int detachmentSize_[] = {5,20,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 161 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 162 int RetVal = 25; > 163 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 164 int Test_(Case_<2>) { > 165 string countryMap_[] = {"A----A", > 166 "-AAAA-", > 167 "-AA*A-", > 168 "-AAAA-", > 169 "A----A"}; > 170 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 171 int detachmentSize_[] = {9,8,2,5,3,2,1,2,6,10,4,6,7,1,7,8,8,8,2,9,7,6,5, > 172 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 173 int RetVal = 0; > 174 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 175 int Test_(Case_<3>) { > 176 string countryMap_[] = {"-A-----", > 177 "-BCCC*-", > 178 "-A-----"}; > 179 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 180 int detachmentSize_[] = {1,5,10,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 181 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 182 int RetVal = 5; > 183 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 184 int Test_(Case_<4>) { > 185 string countryMap_[] = {"WANNABRUTEFORCEMEHUH", > 186 "ASUDQWNHIOCASFIUQISA", > 187 "UWQD-ASFFC-AJSQOOWE-", > 188 "-----*Y--AVSSFIUQISA", > 189 "UWQD-ASFFC-AJSQOOWE-", > 190 "JUFDIFD-CHBVISBOOWE-", > 191 "WANNABRUTEFORCEMEHUH"}; > 192 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 193 int detachmentSize_[] = {87,78,20,43,30,12,9,18,57,93,32,60,64,9,69,74,7 > 194 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 195 int RetVal = 218; > 196 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 197 int Test_(Case_<5>) { > 198 string countryMap_[] = { > 199 "WOMP-PVE-LV-NB-J---E", > 200 "K--MKNWSOLFKK-JHP--W", > 201 "BE-K--MPP-YGZI-DMVKG", > 202 "JGWRXQ-UYEROYGC--SZR", > 203 "KFYN--CRX*N--JS-M-S-", > 204 "TBFGG-OL--D-DY--RR-A", > 205 "AMJT-NV--P--XF-YUSBM", > 206 "HJ-C--KHU-KDZHERUUHM", > 207 "-FGQZOI-A--ZIPB--QOT", > 208 "PNLI-RWYZG-MNV-SUNJD", > 209 "YJ-EJLD-H---K-HSVZF-", > 210 "LKPGZGF-IQ-ETYQJ-VAR", > 211 "UGS--V-BOUGG-SMO--Q-", > 212 "DFVWRHC-D-XES-MEQMCQ", > 213 "L-PUXR-C-HL-WX-NFJJR", > 214 "-A-JLUQX--VWPFENHJZ-", > 215 "HNG-NQAF-VVCUU-WU-HL"}; > 216 vector <string> countryMap(countryMap_, countryMap_+sizeof(countryMap_ > 217 int detachmentSize_[] = { > 218 96882, 90320, 19358, 82732, 80637, //ABCDE > 219 88638, 43236, 34294, 49732, 86156, //FGHIJ > 220 1186, 84308, 49312, 92789, 49312, //KLMNO > 221 14324, 27420, 32881, 73865, 91069, //PQRST > 222 7266, 60052, 98451, 84652, 3991, 58948}; //UVWXYZ > 223 vector <int> detachmentSize(detachmentSize_, detachmentSize_+sizeof(de > 224 int RetVal = 69753; > 225 return verify_case(RetVal, BarbarianInvasion().minimalDetachment(country > 226 > 227 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 228 template<> void Run_<-1>() {} > 229 int main() { Run_<0>(); } > 230 // END CUT HERE > 231
Added SRM/434-U/1A.cpp version [ca2bcca06d919fe6]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 bool is_ps( LL v ) > 21 { > 22 LL sv = (LL)sqrt( double(v) ); > 23 return (sv-1)*(sv-1) == v > 24 || sv*sv == v > 25 || (sv+1)*(sv+1) == v; > 26 } > 27 > 28 class FindingSquareInTable { > 29 public: > 30 int findMaximalSquare(vector <string> table) > 31 { > 32 LL ans = -1; > 33 > 34 int Y = table.size(); > 35 int X = table[0].size(); > 36 for(int y=0; y<Y; ++y) > 37 for(int x=0; x<X; ++x) > 38 for(int dy=-Y; dy<Y; ++dy) > 39 for(int dx=-X; dx<X; ++dx) > 40 if(dy||dx) > 41 { > 42 LL v = 0; > 43 for(int i=0;; ++i) > 44 { > 45 int yy = y+dy*i; > 46 int xx = x+dx*i; > 47 if( xx<0 || X<=x > 48 break; > 49 int d = table[yy > 50 v = v*10 + d; > 51 if( is_ps(v) ) > 52 ans = ma > 53 } > 54 } > 55 return ans; > 56 } > 57 }; > 58 > 59 // BEGIN CUT HERE > 60 #include <ctime> > 61 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 62 > 63 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 64 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 65 > 66 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 67 char Test_(...); > 68 int Test_(Case_<0>) { > 69 string table_[] = {"123", > 70 "456"}; > 71 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 72 int RetVal = 64; > 73 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 74 int Test_(Case_<1>) { > 75 string table_[] = {"00000", > 76 "00000", > 77 "00200", > 78 "00000", > 79 "00000"}; > 80 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 81 int RetVal = 0; > 82 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 83 int Test_(Case_<2>) { > 84 string table_[] = {"3791178", > 85 "1283252", > 86 "4103617", > 87 "8233494", > 88 "8725572", > 89 "2937261"}; > 90 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 91 int RetVal = 320356; > 92 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 93 int Test_(Case_<3>) { > 94 string table_[] = {"135791357", > 95 "357913579", > 96 "579135791", > 97 "791357913", > 98 "913579135"} > 99 ; > 100 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 101 int RetVal = 9; > 102 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 103 int Test_(Case_<4>) { > 104 string table_[] = {"553333733", > 105 "775337775", > 106 "777537775", > 107 "777357333", > 108 "755553557", > 109 "355533335", > 110 "373773573", > 111 "337373777", > 112 "775557777"}; > 113 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 114 int RetVal = -1; > 115 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 116 int Test_(Case_<5>) { > 117 string table_[] = {"257240281", > 118 "197510846", > 119 "014345401", > 120 "035562575", > 121 "974935632", > 122 "865865933", > 123 "684684987", > 124 "768934659", > 125 "287493867"}; > 126 vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); > 127 int RetVal = 95481; > 128 return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(tabl > 129 > 130 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 131 template<> void Run_<-1>() {} > 132 int main() { Run_<0>(); } > 133 // END CUT HERE > 134
Added SRM/434-U/1B.cpp version [5e2ee475015aa982]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 static const int N = 60; > 21 > 22 typedef vector<int> Num; > 23 Num conv( const string& s ) > 24 { > 25 Num n(N); > 26 for(int i=0; i<s.size(); ++i) > 27 { > 28 char c = s[s.size()-1-i]; > 29 n[N-1-i] = ('0'<=c&&c<='9' ? c-'0' : c-'A'+10); > 30 } > 31 return n; > 32 } > 33 > 34 Num add( const Num& a, const Num& b ) > 35 { > 36 Num n(N); > 37 int c = 0; > 38 for(int i=0; i<N; ++i) > 39 { > 40 int s = a[N-1-i] + b[N-1-i] + c; > 41 n[N-1-i] = s%36; > 42 c = s/36; > 43 } > 44 return n; > 45 } > 46 > 47 class HexatridecimalSum { > 48 public: > 49 string maximizeSum(vector <string> numbers, int k) > 50 { > 51 vector<Num> ns; > 52 transform( numbers.begin(), numbers.end(), back_inserter(ns), &c > 53 > 54 vector<Num> gain(36); > 55 for(int d=0; d<36; ++d) > 56 { > 57 vector<int> g(N); > 58 for(int i=0; i<ns.size(); ++i) > 59 for(int j=0,f=0; j<N; ++j) { > 60 if( ns[i][j] ) f=1; > 61 if( f && ns[i][j] == d ) > 62 g[j] += (35-d); > 63 } > 64 Num gg(N); > 65 for(int j=0; j<N; ++j) { > 66 int x = g[N-1-j]; > 67 gg[N-1-j] = x % 36; > 68 if(x/36) g[N-1-j-1] += x/36; > 69 } > 70 gain[d] = gg; > 71 } > 72 sort( gain.rbegin(), gain.rend() ); > 73 Num zero(N); > 74 Num n = accumulate( gain.begin(), gain.begin()+k, zero, &add ); > 75 Num m = accumulate( ns.begin(), ns.end(), n, &add ); > 76 > 77 string ans; > 78 for(int i=0,f=0; i<N; ++i) > 79 { > 80 if(m[i]) f=1; > 81 if(f) > 82 if( m[i]<=9 ) > 83 ans += char(m[i]+'0'); > 84 else > 85 ans += char(m[i]-10+'A'); > 86 } > 87 return ans; > 88 } > 89 }; > 90 > 91 // BEGIN CUT HERE > 92 #include <ctime> > 93 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 94 > 95 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 96 int verify_case(const string &Expected, const string &Received) { if (Expected = > 97 > 98 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 99 char Test_(...); > 100 int Test_(Case_<0>) { > 101 string numbers_[] = {"HELLO"}; > 102 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 103 int k = 2; > 104 string RetVal = "ZZLLO"; > 105 return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); > 106 int Test_(Case_<1>) { > 107 string numbers_[] = {"500", "POINTS", "FOR", "THIS", "PROBLEM"}; > 108 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 109 int k = 5; > 110 string RetVal = "1100TC85"; > 111 return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); > 112 int Test_(Case_<2>) { > 113 string numbers_[] = {"TO", "BE", "OR", "NOT", "TO", "BE"}; > 114 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 115 int k = 0; > 116 string RetVal = "QNO"; > 117 return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); > 118 int Test_(Case_<3>) { > 119 string numbers_[] = {"KEQUALS36"}; > 120 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 121 int k = 36; > 122 string RetVal = "ZZZZZZZZZ"; > 123 return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); > 124 int Test_(Case_<4>) { > 125 string numbers_[] = {"GOOD", "LUCK", "AND", "HAVE", "FUN"}; > 126 vector <string> numbers(numbers_, numbers_+sizeof(numbers_)/sizeof(*nu > 127 int k = 7; > 128 string RetVal = "31YUB"; > 129 return verify_case(RetVal, HexatridecimalSum().maximizeSum(numbers, k)); > 130 > 131 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 132 template<> void Run_<-1>() {} > 133 int main() { Run_<0>(); } > 134 // END CUT HERE > 135
Added SRM/434-U/1C-U.cpp version [a7fda6c5ea37e4b7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class IncreasingLists { > 21 public: > 22 string ans; > 23 typedef pair<int,pair<string,string> > key; > 24 map<key,bool> memo; > 25 > 26 vector<string> cm; > 27 > 28 string makeIncreasingList(string mask) > 29 { > 30 string buf(mask); > 31 comma(mask, 0, 1, 0, buf); > 32 > 33 for(int i=0; i<cm.size(); ++i) > 34 { > 35 // try_fill > 36 } > 37 > 38 return rec(mask, 0, "", "", buf) ? ans : "impossible"; > 39 } > 40 > 41 void comma( const string& mask, int i, int prev, int cur, string& buf ) > 42 { > 43 if( i==mask.size() || mask[i]==',' ) > 44 if( prev > cur ) > 45 return; > 46 if( i==mask.size() ) { > 47 cm.push_back(buf); > 48 return; > 49 } > 50 if( mask[i]==',' ) { > 51 buf[i] = ','; > 52 comma( mask, i+1, cur, 0, buf ); > 53 } > 54 else if( mask[i]=='?' ) { > 55 if( prev<=cur ) { > 56 buf[i]=','; > 57 comma( mask, i+1, cur, 0, buf ); > 58 } > 59 buf[i]='?'; > 60 comma( mask, i+1, prev, cur+1, buf ); > 61 } > 62 else { > 63 buf[i] = mask[i]; > 64 comma( mask, i+1, prev, cur+1, buf ); > 65 } > 66 } > 67 > 68 bool rec(const string& mask, int i, const string& prev, const string& cu > 69 { > 70 key k(i, make_pair(prev,cur)); > 71 if( memo.count(k) ) > 72 return false; > 73 if( i==mask.size() || mask[i]==',' ) > 74 { > 75 if( prev.size() > cur.size() > 76 || prev.size() == cur.size() && prev>=cur ) > 77 return false; > 78 } > 79 > 80 if( i==mask.size() ) { > 81 ans = buf; > 82 return true; > 83 } > 84 > 85 if( mask[i]==',' ) > 86 return rec(mask, i+1, cur, "", buf); > 87 > 88 if( '0' <= mask[i] && mask[i] <='9' ) > 89 return rec(mask, i+1, prev, cur+mask[i], buf); > 90 > 91 // try , > 92 if( !(prev.size()>cur.size() || prev.size()==cur.size() && prev> > 93 buf[i] = ','; > 94 if( rec(mask, i+1, cur, "", buf) ) > 95 return true; > 96 } > 97 // try 0 > 98 for(int d=0; d<9; ++d) > 99 if(cur.size()>0 || d>0) { > 100 buf[i] = char(d+'0'); > 101 if( rec(mask, i+1, prev, cur+char(d+'0'), buf) ) > 102 return true; > 103 } > 104 return memo[k] = false; > 105 } > 106 }; > 107 > 108 // BEGIN CUT HERE > 109 #include <ctime> > 110 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 111 > 112 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 113 int verify_case(const string &Expected, const string &Received) { if (Expected = > 114 > 115 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 116 char Test_(...); > 117 int Test_(Case_<0>) { > 118 string mask = "??"; > 119 string RetVal = "10"; > 120 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 121 int Test_(Case_<1>) { > 122 string mask = "???"; > 123 string RetVal = "1,2"; > 124 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 125 int Test_(Case_<2>) { > 126 string mask = "?????????,9"; > 127 string RetVal = "1,2,3,4,5,9"; > 128 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 129 int Test_(Case_<3>) { > 130 string mask = "??????????,9"; > 131 string RetVal = "impossible"; > 132 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 133 int Test_(Case_<4>) { > 134 string mask = "?,10,?????????????????,16,??"; > 135 string RetVal = "impossible"; > 136 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 137 int Test_(Case_<5>) { > 138 string mask = "?2?5??7?,??"; > 139 string RetVal = "12,50,70,71"; > 140 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 141 int Test_(Case_<6>) { > 142 string mask = "???????????????????????????????,???"; > 143 string RetVal = "1,10,11,100,101,102,103,104,105,106"; > 144 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 145 int Test_(Case_<7>) { > 146 string mask = "??????????????????????????????????????????????????"; > 147 string RetVal = "1,10,100,1000,10000,100000,1000000,100000000000000"; > 148 return verify_case(RetVal, IncreasingLists().makeIncreasingList(mask)); > 149 > 150 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 151 template<> void Run_<-1>() {} > 152 int main() { Run_<0>(); } > 153 // END CUT HERE > 154
Added SRM/435/1A.cpp version [90be7959357bf00f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class CellRemoval { > 21 public: > 22 int cellsLeft(vector <int> parent, int deletedCell) > 23 { > 24 vector<bool> dead(parent.size(), false); > 25 > 26 for(int i=0; i<parent.size(); ++i) > 27 { > 28 for(int p=i; p!=-1; p=parent[p], (p!=-1 ? dead[p]=true : > 29 if( p == deletedCell ) > 30 dead[i] = true; > 31 } > 32 int cnt = 0; > 33 for(int i=0; i<dead.size(); ++i) > 34 if( !dead[i] ) > 35 ++cnt; > 36 return cnt; > 37 } > 38 }; > 39 > 40 // BEGIN CUT HERE > 41 #include <ctime> > 42 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 43 > 44 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 45 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 46 > 47 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 48 char Test_(...); > 49 int Test_(Case_<0>) { > 50 int parent_[] = {-1,0,0,1,1}; > 51 vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)) > 52 int deletedCell = 2; > 53 int RetVal = 2; > 54 return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)) > 55 int Test_(Case_<1>) { > 56 int parent_[] = {-1,0,0,1,1}; > 57 vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)) > 58 int deletedCell = 1; > 59 int RetVal = 1; > 60 return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)) > 61 int Test_(Case_<2>) { > 62 int parent_[] = {-1,0,0,1,1}; > 63 vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)) > 64 int deletedCell = 0; > 65 int RetVal = 0; > 66 return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)) > 67 int Test_(Case_<3>) { > 68 int parent_[] = {-1,0,0,2,2,4,4,6,6}; > 69 vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)) > 70 int deletedCell = 4; > 71 int RetVal = 2; > 72 return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)) > 73 int Test_(Case_<4>) { > 74 int parent_[] = {26,2,32,36,40,19,43,24,30,13,21,14,24,21,19,4,30,10,44, > 75 35,18,7,36,10,16,5,38,35,4,13,-1,16,26,1,12,2,5,18,40,1,17,38,44,14}; > 76 vector <int> parent(parent_, parent_+sizeof(parent_)/sizeof(*parent_)) > 77 int deletedCell = 24; > 78 int RetVal = 14; > 79 return verify_case(RetVal, CellRemoval().cellsLeft(parent, deletedCell)) > 80 > 81 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 82 template<> void Run_<-1>() {} > 83 int main() { Run_<0>(); } > 84 // END CUT HERE > 85
Added SRM/435/1B.cpp version [a9aeb90d8d076e62]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 static const LL MODVAL = 1000000007; > 21 > 22 class DNADeletion { > 23 public: > 24 int differentProteins(vector <string> DNASequence, vector <string> codon > 25 { > 26 string dna; > 27 for(int i=0; i<DNASequence.size(); ++i) dna += DNASequence[i]; > 28 int N = dna.size(); > 29 > 30 vector<string> cL, cR; > 31 for(int i=0; i<codonTable.size(); ++i) { > 32 stringstream sin(codonTable[i]); > 33 string s, t; sin>>s>>t; > 34 cL.push_back(s); > 35 cR.push_back(t); > 36 } > 37 int M = cL.size(); > 38 > 39 > 40 vector<LL> dp(N+1); dp[0] = 1; > 41 for(int i=0; i<N; ++i) > 42 { > 43 // there's dp[i] ways to generate some aminoseq from dna > 44 > 45 vector<int> p(M); > 46 > 47 set<string> done; > 48 for(int j=i; j<dna.size(); ++j) > 49 { > 50 for(int k=0; k<M; ++k) > 51 if( p[k]<3 && dna[j]==cL[k][p[k]] ) { > 52 p[k]++; > 53 if( p[k]==3 && !done.count(cR[k] > 54 done.insert(cR[k]); > 55 dp[j+1] = (dp[j+1] + dp[ > 56 } > 57 } > 58 } > 59 } > 60 > 61 LL n = (accumulate(dp.begin(), dp.end(), 0LL) - 1) % MODVAL; > 62 return int(n); > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 69 > 70 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 71 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 72 > 73 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 74 char Test_(...); > 75 int Test_(Case_<0>) { > 76 string DNASequence_[] = {"ACTG"}; > 77 vector <string> DNASequence(DNASequence_, DNASequence_+sizeof(DNASeque > 78 string codonTable_[] = {"ACT gua", "ACG cys", "ATG leu", "CTG thr"}; > 79 vector <string> codonTable(codonTable_, codonTable_+sizeof(codonTable_ > 80 int RetVal = 4; > 81 return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, > 82 int Test_(Case_<1>) { > 83 string DNASequence_[] = {"AAACCC"}; > 84 vector <string> DNASequence(DNASequence_, DNASequence_+sizeof(DNASeque > 85 string codonTable_[] = {"AAA thr", "CCC cys"}; > 86 vector <string> codonTable(codonTable_, codonTable_+sizeof(codonTable_ > 87 int RetVal = 3; > 88 return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, > 89 int Test_(Case_<2>) { > 90 string DNASequence_[] = {"AAATCCC"}; > 91 vector <string> DNASequence(DNASequence_, DNASequence_+sizeof(DNASeque > 92 string codonTable_[] = {"AAA gua","TCC dop","AAT dop","CCC gua"}; > 93 vector <string> codonTable(codonTable_, codonTable_+sizeof(codonTable_ > 94 int RetVal = 5; > 95 return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, > 96 int Test_(Case_<3>) { > 97 string DNASequence_[] = {"ATGCGCATTAACCTCCTACCATGGAAGGGACGTAACCCGGCAATTT > 98 "CTGATGACGGCATAAGCTACCCCTAGAGGTAAAAATGCATACTGCGTGCT", > 99 "ATGCAG"}; > 100 vector <string> DNASequence(DNASequence_, DNASequence_+sizeof(DNASeque > 101 string codonTable_[] = {"AAC RpjZt","AAT ZeiC","GCA ChZwh","TCC RpjZt"," > 102 "TAG ZeiC","CTG dVK","GAG ZeiC","GTG I","AAG q","ATT dVK", > 103 "AGA cJEjM","GGG KONUd","GTC ZRV","GGC ZeiC","TTA KONUd", > 104 "GAC q","CCA q","GCC ZRV","GCG RpjZt","CCT ZRV","ATG dVK", > 105 "ATC ChZwh","CTC cJEjM","CCC q","ATA dWjz","TTG DkEG", > 106 "CAG q","CAA ZRV","ACT dVK","TCG dVK","ACC I","CGC dVK"}; > 107 vector <string> codonTable(codonTable_, codonTable_+sizeof(codonTable_ > 108 int RetVal = 455985264; > 109 return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, > 110 int Test_(Case_<4>) { > 111 string DNASequence_[] = {"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA > 112 vector <string> DNASequence(DNASequence_, DNASequence_+sizeof(DNASeque > 113 string codonTable_[] = {"AAA thr", "CCC cys", "CCC cys", "CCC cys", "CCC > 114 vector <string> codonTable(codonTable_, codonTable_+sizeof(codonTable_ > 115 int RetVal = 3; > 116 return verify_case(RetVal, DNADeletion().differentProteins(DNASequence, > 117 > 118 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 119 template<> void Run_<-1>() {} > 120 int main() { Run_<0>(); } > 121 // END CUT HERE > 122
Added SRM/435/1C.cpp version [0b6298dd57a02ed4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef int vert; > 23 typedef vert edge; > 24 typedef vector<edge> edges; > 25 typedef vector<edges> graph; > 26 > 27 bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) > 28 { > 29 for(int i=0; i<G[v].size(); ++i) { > 30 vert u = G[v][i]; > 31 if( visited[u] ) continue; > 32 visited[u] = true; > 33 > 34 if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) > 35 { matchTo[v]=u, matchTo[u]=v; return true; } > 36 } > 37 return false; > 38 } > 39 > 40 template<int NV> > 41 int biMatch( graph& G, int L ) // [0,L):left, [L,?):right > 42 // only left->right edges are used during computation > 43 { > 44 vector<vert> matchTo(G.size(), -1); > 45 int ans = 0; > 46 for(vert v=0; v<L; ++v) { > 47 bool visited[NV] = {}; > 48 if( augment(G, v, matchTo, visited) ) > 49 ++ans; > 50 } > 51 return ans; > 52 } > 53 > 54 class CompanyRestructuring { > 55 public: > 56 int fewestDivisions(vector <string> hasManaged) > 57 { > 58 int N = hasManaged.size(); > 59 > 60 bool hm[50][50], hmStar[50][50]; > 61 for(int i=0; i<N; ++i) > 62 for(int j=0; j<N; ++j) > 63 hm[i][j] = hmStar[i][j] = hasManaged[i][j]=='Y'; > 64 > 65 for(int k=0; k<N; ++k) > 66 for(int i=0; i<N; ++i) > 67 for(int j=0; j<N; ++j) > 68 hmStar[i][j] |= hmStar[i][k] & hmStar[k] > 69 > 70 graph g(N); > 71 int nextID = N; > 72 for(int p=0; p<N; ++p) > 73 { > 74 vector<int> cs; > 75 vector<int> pi; > 76 > 77 for(int c=0; c<N; ++c) > 78 if( hm[p][c] && !hmStar[c][p] ) > 79 { > 80 int pid = -1; > 81 > 82 for(int j=0; j<cs.size(); ++j) > 83 if( hmStar[c][cs[j]] && hmStar[c > 84 {pid = pi[j]; break;} > 85 > 86 if(pid<0) pid = nextID++; > 87 cs.push_back(c); > 88 pi.push_back(pid); > 89 g[c].push_back(pid); > 90 } > 91 } > 92 g.resize(nextID); > 93 > 94 return N - biMatch<5000>(g, N); > 95 } > 96 }; > 97 > 98 // BEGIN CUT HERE > 99 #include <ctime> > 100 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 101 > 102 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 103 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 104 > 105 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 106 char Test_(...); > 107 int Test_(Case_<0>) { > 108 string hasManaged_[] = {"NNNN","NNYN","NNNN","YYYN"}; > 109 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 110 int RetVal = 1; > 111 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 112 int Test_(Case_<1>) { > 113 string hasManaged_[] = {"NNYN","NNYN","YNNN","YYYN"}; > 114 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 115 int RetVal = 1; > 116 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 117 int Test_(Case_<2>) { > 118 string hasManaged_[] = {"NYNNN","NNYNN","NNNYN","NNNNY","YNNNN"}; > 119 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 120 int RetVal = 5; > 121 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 122 int Test_(Case_<3>) { > 123 string hasManaged_[] = {"NYNYYYNN" > 124 ,"NNNNYYNN" > 125 ,"NYNNYYNN" > 126 ,"NNYNYYNN" > 127 ,"NNNNNNNN" > 128 ,"NYYNYNNN" > 129 ,"YYNYYYNN" > 130 ,"YYNYYYYN"}; > 131 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 132 int RetVal = 1; > 133 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 134 int Test_(Case_<4>) { > 135 string hasManaged_[] = {"NYNYNNYYNYNNNYYNYNYY" > 136 ,"YNNNNNYYNNNYYNYNYNYY" > 137 ,"NNNNNNYYNNNYYNYNNNNY" > 138 ,"YNYNNNNYNNNYNNYNYNNY" > 139 ,"NYNNNNNYYYYNYNYYNNYN" > 140 ,"YYYYNNYNYNNNNNYYNYNY" > 141 ,"NNNNNNNNNNNYYNNNNNYY" > 142 ,"NNNNNNYNNNNYYNYNNNYN" > 143 ,"NYYYNNNYNNNNYYNYYNYY" > 144 ,"NNYNNNYYNNNNYNNNNNYY" > 145 ,"YYNNNYNNYNNNNYNNYNYY" > 146 ,"NNNNNNNYNNNNYNYNNNNY" > 147 ,"NNNNNNYNNNNYNNNNNNNN" > 148 ,"NNYNNNNNNNNYYNYNNYYN" > 149 ,"NNNNNNNNNNNYNNNNNNNY" > 150 ,"YNYYNYYNNNYYNNNNYNYY" > 151 ,"NYNNNNNYNYNYYYYNNNNY" > 152 ,"NNYNNNNYNYNYYYNNNNYY" > 153 ,"NNNNNNNYNNNYNNNNNNNY" > 154 ,"NNNNNNYYNNNYYNYNNNYN"}; > 155 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 156 int RetVal = 4; > 157 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 158 int Test_(Case_<5>) { > 159 string hasManaged_[] = {"NNNYNNNNNNNYNNNNNNNN" > 160 ,"NNNNNNYNNYNNNNNYNYYN" > 161 ,"YNNNNNYNYNNNNNNNNNNY" > 162 ,"YNNNNNNNNNNNNNNNNNNN" > 163 ,"NNYNNNYNYNNYNNYNNNNY" > 164 ,"NNNYNNNNNNNYNNYYYNYY" > 165 ,"NNYYNNNNYNNNNNNNNNNY" > 166 ,"NYNNNYNNYNNNYNYNYNNN" > 167 ,"NNNNNNNNNNNNNNNNNNNN" > 168 ,"YNNNNNNNNNNYNNYYNNYN" > 169 ,"NNYYNYNNYYNNNNYYNYNN" > 170 ,"NNNNNNNNYNNNNNNNNNNN" > 171 ,"NNNYYNYNYYYYNNNNYNYY" > 172 ,"NNYYNYNNYYYYNNNNNYNY" > 173 ,"NNYYYNNNYNNNNNNNNNNN" > 174 ,"YNNYYYNNNNNNNNYNYNYY" > 175 ,"NNNNYNYNNNNYNNYNNNNN" > 176 ,"YNNYYYYNNYNNYNNNYNYN" > 177 ,"YNNYYYYNYNNYNNYYYNNY" > 178 ,"YNYNNNNNYNNYNNNNNNNN"}; > 179 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 180 int RetVal = 2; > 181 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 182 int Test_(Case_<6>) { > 183 string hasManaged_[] = {"NNNNN","NNNNN","NNNNN","NNNNN","NNNNN"}; > 184 vector <string> hasManaged(hasManaged_, hasManaged_+sizeof(hasManaged_ > 185 int RetVal = 5; > 186 return verify_case(RetVal, CompanyRestructuring().fewestDivisions(hasMan > 187 > 188 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 189 template<> void Run_<-1>() {} > 190 int main() { Run_<0>(); } > 191 // END CUT HERE > 192
Added SRM/436/1A.cpp version [4452768ad937b4c9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class BestView { > 21 public: > 22 int numberOfBuildings(vector <int> heights) > 23 { > 24 int maxCnt = -1; > 25 > 26 int N = heights.size(); > 27 for(int from=0; from<N; ++from) > 28 { > 29 int cnt = 0; > 30 for(int to=0; to<N; ++to) if(to != from) > 31 { > 32 bool hidden = false; > 33 for(int h=min(from,to)+1; h<max(from,to); ++h) > 34 { > 35 LL X1 = from; > 36 LL Y1 = heights[from]; > 37 LL X2 = to; > 38 LL Y2 = heights[to]; > 39 LL x = h; > 40 LL y = heights[h]; > 41 // hide = (Y2-Y1)/(X2-X1)*(x-X1)+Y1 <= y > 42 if( X2-X1 > 0 ) { > 43 if( (Y2-Y1)*(x-X1) + Y1*(X2-X1) > 44 hidden = true; > 45 } else { > 46 if( (Y2-Y1)*(x-X1) + Y1*(X2-X1) > 47 hidden = true; > 48 } > 49 } > 50 if(!hidden) cnt++; > 51 } > 52 maxCnt = max(maxCnt, cnt); > 53 } > 54 return maxCnt; > 55 } > 56 }; > 57 > 58 // BEGIN CUT HERE > 59 #include <ctime> > 60 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 61 > 62 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 63 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 64 > 65 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 66 char Test_(...); > 67 int Test_(Case_<0>) { > 68 int heights_[] = {10}; > 69 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 70 int RetVal = 0; > 71 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 72 int Test_(Case_<1>) { > 73 int heights_[] = {5,5,5,5}; > 74 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 75 int RetVal = 2; > 76 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 77 int Test_(Case_<2>) { > 78 int heights_[] = {1,2,7,3,2}; > 79 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 80 int RetVal = 4; > 81 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 82 int Test_(Case_<3>) { > 83 int heights_[] = {1,5,3,2,6,3,2,6,4,2,5,7,3,1,5}; > 84 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 85 int RetVal = 7; > 86 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 87 int Test_(Case_<4>) { > 88 int heights_[] = {1000000000,999999999,999999998,999999997,999999996,1,2 > 89 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 90 int RetVal = 6; > 91 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 92 int Test_(Case_<5>) { > 93 int heights_[] = {244645169,956664793,752259473,577152868,605440232,5693 > 94 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 95 int RetVal = 7; > 96 return verify_case(RetVal, BestView().numberOfBuildings(heights)); } > 97 > 98 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 99 template<> void Run_<-1>() {} > 100 int main() { Run_<0>(); } > 101 // END CUT HERE > 102
Added SRM/436/1B.cpp version [1b05819e2eb7f37b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 bool w[500][500]; > 21 bool v[500][500]; > 22 > 23 class DoNotTurn { > 24 public: > 25 int minimumTurns(int N, int X0, int A, int B, int Y0, int C, int D, int > 26 { > 27 // input > 28 memset(w, 0, sizeof w); > 29 memset(v, 0, sizeof v); > 30 > 31 LL X=X0%P, Y=Y0%P; > 32 for(int i=1; i<=M; ++i) > 33 { > 34 if( !((X%N)==0 && (Y%N)==0 || (X%N)==N-1 && (Y%N)==N-1) > 35 w[X%N][Y%N] = 1; > 36 X = (X*A+B) % P; > 37 Y = (Y*C+D) % P; > 38 } > 39 > 40 // solve > 41 vector< pair<int,int> > Q(1, make_pair(0,0)); v[0][0]=1; > 42 for(int step=-1; !Q.empty(); ++step) > 43 { > 44 vector< pair<int,int> > Q2; > 45 for(int i=0; i<Q.size(); ++i) > 46 { > 47 int x = Q[i].first; > 48 int y = Q[i].second; > 49 if( x==N-1 && y==N-1 ) > 50 return step; > 51 > 52 int dx[] = {-1, +1, 0, 0}; > 53 int dy[] = {0, 0, -1, +1}; > 54 for(int d=0; d<4; ++d) > 55 { > 56 for(int k=1 ;; ++k) { > 57 int xx = x + dx[d]*k; > 58 int yy = y + dy[d]*k; > 59 if( xx<0 || N<=xx || yy<0 || N<= > 60 break; > 61 if( !v[xx][yy] ) { > 62 v[xx][yy] = true; > 63 Q2.push_back( make_pair( > 64 } > 65 } > 66 } > 67 } > 68 Q.swap(Q2); > 69 } > 70 return -1; > 71 } > 72 }; > 73 > 74 // BEGIN CUT HERE > 75 #include <ctime> > 76 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 77 > 78 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 79 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 80 > 81 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 82 char Test_(...); > 83 int Test_(Case_<0>) { > 84 int N = 2; > 85 int X0 = 0; > 86 int A = 0; > 87 int B = 1; > 88 int Y0 = 0; > 89 int C = 0; > 90 int D = 1; > 91 int P = 10; > 92 int M = 2; > 93 int RetVal = 1; > 94 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 95 int Test_(Case_<1>) { > 96 int N = 3; > 97 int X0 = 0; > 98 int A = 1; > 99 int B = 1; > 100 int Y0 = 1; > 101 int C = 1; > 102 int D = 0; > 103 int P = 3; > 104 int M = 3; > 105 int RetVal = -1; > 106 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 107 int Test_(Case_<2>) { > 108 int N = 3; > 109 int X0 = 0; > 110 int A = 1; > 111 int B = 1; > 112 int Y0 = 1; > 113 int C = 1; > 114 int D = 1; > 115 int P = 3; > 116 int M = 3; > 117 int RetVal = 3; > 118 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 119 int Test_(Case_<3>) { > 120 int N = 10; > 121 int X0 = 911111; > 122 int A = 845499; > 123 int B = 866249; > 124 int Y0 = 688029; > 125 int C = 742197; > 126 int D = 312197; > 127 int P = 384409; > 128 int M = 40; > 129 int RetVal = 12; > 130 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 131 int Test_(Case_<4>) { > 132 int N = 5; > 133 int X0 = 23; > 134 int A = 2; > 135 int B = 3; > 136 int Y0 = 35; > 137 int C = 5; > 138 int D = 7; > 139 int P = 9; > 140 int M = 3; > 141 int RetVal = 2; > 142 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 143 int Test_(Case_<5>) { > 144 int N = 2; > 145 int X0 = 0; > 146 int A = 0; > 147 int B = 0; > 148 int Y0 = 0; > 149 int C = 0; > 150 int D = 0; > 151 int P = 1; > 152 int M = 0; > 153 int RetVal = 1; > 154 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 155 int Test_(Case_<6>) { > 156 int N = 500; > 157 int X0 = 0; > 158 int A = 0; > 159 int B = 0; > 160 int Y0 = 0; > 161 int C = 0; > 162 int D = 0; > 163 int P = 1; > 164 int M = 0; > 165 int RetVal = 1; > 166 return verify_case(RetVal, DoNotTurn().minimumTurns(N, X0, A, B, Y0, C, > 167 > 168 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 169 template<> void Run_<-1>() {} > 170 int main() { Run_<0>(); } > 171 // END CUT HERE > 172
Added SRM/436/1C.cpp version [eda6862a8c9a5383]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 CMP tmp[65536*4]; > 23 > 24 void fft_impl( CMP a[], int stride, int n, double Wt ) > 25 { > 26 if( n > 1 ) > 27 { > 28 CMP *ev=a, *od=a+stride; > 29 int s2=stride*2, n2=n/2; > 30 > 31 fft_impl( ev, s2, n2, Wt*2 ); > 32 fft_impl( od, s2, n2, Wt*2 ); > 33 > 34 for(int i=0; i<n; ++i) tmp[i] = ev[s2*(i%n2)] + od[s2*(i%n2)]*po > 35 for(int i=0; i<n; ++i) a[stride*i] = tmp[i]; > 36 } > 37 } > 38 > 39 void fft( vector<CMP>& a ) > 40 { > 41 fft_impl(&a[0], 1, a.size(), +2*M_PI/a.size()); > 42 } > 43 > 44 void ifft( vector<CMP>& a ) > 45 { > 46 fft_impl(&a[0], 1, a.size(), -2*M_PI/a.size()); > 47 for(int i=0; i<a.size(); ++i) > 48 a[i] /= a.size(); > 49 } > 50 > 51 class CircularShifts { > 52 public: > 53 int maxScore(int N, int Z0, int A, int B, int M) > 54 { > 55 // input > 56 LL X[60000], Y[60000], Z=Z0%M; > 57 for(int i=0; i<N+N; ++i) > 58 { > 59 (i<N ? X[i] : Y[i-N]) = Z % 100; > 60 Z = (Z*A+B) % M; > 61 } > 62 > 63 // solve > 64 int NN = 1; > 65 while( NN < N*2 ) NN*=2; > 66 > 67 vector<CMP> CX(NN), CY(NN), CZ(NN); > 68 for(int i=0; i<N; ++i) > 69 CX[i]=CX[i+N]=X[N-i-1], CY[i]=Y[i]; > 70 > 71 fft(CX); > 72 fft(CY); > 73 transform( CX.begin(), CX.end(), CY.begin(), CZ.begin(), multipl > 74 ifft(CZ); > 75 > 76 double m = 0; > 77 for(int i=0; i<NN; ++i) > 78 m = max(m, CZ[i].real()); > 79 return int(m+0.5); > 80 } > 81 }; > 82 > 83 // BEGIN CUT HERE > 84 #include <ctime> > 85 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 86 > 87 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 88 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 89 > 90 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 91 char Test_(...); > 92 int Test_(Case_<0>) { > 93 int N = 5; > 94 int Z0 = 1; > 95 int A = 1; > 96 int B = 0; > 97 int M = 13; > 98 int RetVal = 5; > 99 return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } > 100 int Test_(Case_<1>) { > 101 int N = 4; > 102 int Z0 = 1; > 103 int A = 1; > 104 int B = 1; > 105 int M = 20; > 106 int RetVal = 70; > 107 return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } > 108 int Test_(Case_<2>) { > 109 int N = 10; > 110 int Z0 = 23; > 111 int A = 11; > 112 int B = 51; > 113 int M = 4322; > 114 int RetVal = 28886; > 115 return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } > 116 int Test_(Case_<3>) { > 117 int N = 1000; > 118 int Z0 = 3252; > 119 int A = 3458736; > 120 int B = 233421; > 121 int M = 111111111; > 122 int RetVal = 2585408; > 123 return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } > 124 int Test_(Case_<4>) { > 125 int N = 141; > 126 int Z0 = 96478; > 127 int A = 24834; > 128 int B = 74860; > 129 int M = 92112; > 130 int RetVal = 419992; > 131 return verify_case(RetVal, CircularShifts().maxScore(N, Z0, A, B, M)); } > 132 > 133 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 134 template<> void Run_<-1>() {} > 135 int main() { Run_<0>(); } > 136 // END CUT HERE > 137
Added SRM/437-U/1A.cpp version [570e3d3e6e5277ce]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename D, typename S> > 23 D lexical_cast(const S& s) { D d; stringstream ss; ss<<s; ss>>d; return d; } > 24 > 25 class TheSwap { > 26 public: > 27 int findMax(int n, int k) > 28 { > 29 set<string> canMake; > 30 canMake.insert( lexical_cast<string>(n) ); > 31 > 32 while( k --> 0 ) > 33 { > 34 set<string> prev; > 35 prev.swap(canMake); > 36 > 37 for(set<string>::iterator it=prev.begin(); it!=prev.end( > 38 { > 39 string s = *it; > 40 for(int i=0; i<s.size(); ++i) > 41 for(int j=i+1; j<s.size(); ++j) > 42 if( !(i==0 && s[j]=='0') ) > 43 { > 44 swap(s[i], s[j]); > 45 canMake.insert(s); > 46 swap(s[i], s[j]); > 47 } > 48 } > 49 } > 50 > 51 return canMake.empty() ? -1 : lexical_cast<int>(*canMake.rbegin( > 52 } > 53 }; > 54 > 55 // BEGIN CUT HERE > 56 #include <ctime> > 57 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 58 > 59 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 60 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 61 > 62 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 63 char Test_(...); > 64 int Test_(Case_<0>) { > 65 int n = 16375; > 66 int k = 1; > 67 int RetVal = 76315; > 68 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 69 int Test_(Case_<1>) { > 70 int n = 432; > 71 int k = 1; > 72 int RetVal = 423; > 73 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 74 int Test_(Case_<2>) { > 75 int n = 90; > 76 int k = 4; > 77 int RetVal = -1; > 78 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 79 int Test_(Case_<3>) { > 80 int n = 5; > 81 int k = 2; > 82 int RetVal = -1; > 83 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 84 int Test_(Case_<4>) { > 85 int n = 436659; > 86 int k = 2; > 87 int RetVal = 966354; > 88 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 89 int Test_(Case_<5>) { > 90 int n = 325664; > 91 int k = 2; > 92 int RetVal = 665324; > 93 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 94 int Test_(Case_<6>) { > 95 int n = 325664; > 96 int k = 4; > 97 int RetVal = 665432; > 98 return verify_case(RetVal, TheSwap().findMax(n, k)); } > 99 > 100 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 101 template<> void Run_<-1>() {} > 102 int main() { Run_<0>(); } > 103 // END CUT HERE > 104
Added SRM/437-U/1B.cpp version [fba485787235da94]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheInteger { > 23 public: > 24 long long find(long long n, int k) > 25 { > 26 stringstream ss; > 27 ss << n; > 28 string s; > 29 ss >> s; > 30 > 31 if( s.size() < k ) > 32 s = string(k-s.size(), '0') + s; > 33 > 34 LL a = solve(s, k); > 35 if( a ) > 36 return a; > 37 return solve("0"+s, k); > 38 } > 39 > 40 LL solve(string s, int k) > 41 { > 42 // no change > 43 if( s[0] != '0' ) > 44 { > 45 set<char> d; > 46 for(int i=0; i<s.size(); ++i) > 47 d.insert( s[i] ); > 48 if( d.size() == k ) > 49 return toll(s); > 50 } > 51 > 52 // a-th digit larger > 53 int lim = s[0]=='0' ? 1 : s.size(); > 54 for(int a=lim-1; a>=0; --a) > 55 for(char v=s[a]+1; v<='9'; ++v) > 56 { > 57 set<char> d; > 58 for(int i=0; i<a; ++i) > 59 d.insert(s[i]); > 60 d.insert(v); > 61 if( d.size() > k ) > 62 continue; > 63 else if( d.size() == k ) > 64 { > 65 s[a] = v; > 66 for(int i=a+1; i<s.size(); ++i) > 67 s[i] = *d.begin(); > 68 return toll(s); > 69 } > 70 else > 71 { > 72 int rest = k - d.size(); > 73 if( s.size()-a-1 < rest ) > 74 continue; > 75 s[a] = v; > 76 > 77 int fi = s.size()-rest; > 78 > 79 for(int i=a+1; i<fi; ++i) > 80 s[i] = '0'; > 81 > 82 for(char z='0'; z<='9'; ++z) > 83 if( !d.count(z) ) > 84 { > 85 s[fi++] = z; > 86 if( fi == s.size() ) > 87 break; > 88 } > 89 > 90 return toll(s); > 91 } > 92 } > 93 return 0; > 94 } > 95 > 96 LL toll( string s ) > 97 { > 98 stringstream ss; ss << s; > 99 LL n; ss >> n; > 100 return n; > 101 } > 102 }; > 103 > 104 // BEGIN CUT HERE > 105 #include <ctime> > 106 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 107 > 108 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 109 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 110 > 111 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 112 char Test_(...); > 113 int Test_(Case_<0>) { > 114 long long n = 47LL; > 115 int k = 1; > 116 long long RetVal = 55LL; > 117 return verify_case(RetVal, TheInteger().find(n, k)); } > 118 int Test_(Case_<1>) { > 119 long long n = 7LL; > 120 int k = 3; > 121 long long RetVal = 102LL; > 122 return verify_case(RetVal, TheInteger().find(n, k)); } > 123 int Test_(Case_<2>) { > 124 long long n = 69LL; > 125 int k = 2; > 126 long long RetVal = 69LL; > 127 return verify_case(RetVal, TheInteger().find(n, k)); } > 128 int Test_(Case_<3>) { > 129 long long n = 12364LL; > 130 int k = 3; > 131 long long RetVal = 12411LL; > 132 return verify_case(RetVal, TheInteger().find(n, k)); } > 133 int Test_(Case_<4>) { > 134 long long n = 1LL; > 135 int k = 10; > 136 long long RetVal = 1023456789LL; > 137 return verify_case(RetVal, TheInteger().find(n, k)); } > 138 int Test_(Case_<5>) { > 139 long long n = 111111111111111LL; > 140 int k = 10; > 141 long long RetVal = 111111203456789LL; > 142 return verify_case(RetVal, TheInteger().find(n, k)); } > 143 int Test_(Case_<6>) { > 144 long long n = 111111111111111LL; > 145 int k = 8; > 146 long long RetVal = 111111112034567LL; > 147 return verify_case(RetVal, TheInteger().find(n, k)); } > 148 int Test_(Case_<7>) { > 149 long long n = 100000000000000LL; > 150 int k = 10; > 151 long long RetVal = 100000023456789LL; > 152 return verify_case(RetVal, TheInteger().find(n, k)); } > 153 int Test_(Case_<8>) { > 154 long long n = 100000000000000LL; > 155 int k = 5; > 156 long long RetVal = 100000000000234LL; > 157 return verify_case(RetVal, TheInteger().find(n, k)); } > 158 > 159 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 160 template<> void Run_<-1>() {} > 161 int main() { Run_<0>(); } > 162 // END CUT HERE > 163
Added SRM/438-U/1A.cpp version [65e4989f51362500]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class UnluckyIntervals { > 23 public: > 24 vector <int> getLuckiest(vector <int> luckySet, int n) > 25 { > 26 sort(luckySet.begin(), luckySet.end()); > 27 > 28 // zeros > 29 vector<int> luk = luckySet; > 30 for(LL i=0; i+1<luckySet.size(); ++i) > 31 if( i+1 < luckySet.size() ) > 32 if( luckySet[i]+2 == luckySet[i+1] ) > 33 luk.push_back( luckySet[i]+1 ); > 34 if( luckySet[0]==2 ) > 35 luk.push_back(1); > 36 sort(luk.begin(), luk.end()); > 37 if( luk.size() >= n ) > 38 return vector<int>(luk.begin(), luk.begin()+n); > 39 > 40 // finites > 41 vector< pair<LL, LL> > luk_val; > 42 LL from = 1; > 43 for(int i=0; i<luckySet.size(); ++i) > 44 { > 45 LL to = luckySet[i]; // [from,to) > 46 if( from+2 <= to ) > 47 { > 48 for(LL j=0; j<min<LL>(to-from,n); ++j) { > 49 LL val = (j%2==0 ? from+j/2 : to-1-j/2); > 50 LL unluk = (j/2+1) * (to-from-j/2) - 1; > 51 luk_val.push_back( make_pair(unluk,val) > 52 } > 53 } > 54 from = luckySet[i]+1; > 55 } > 56 sort( luk_val.begin(), luk_val.end() ); > 57 for(int i=0; i<luk_val.size(); ++i) > 58 luk.push_back(luk_val[i].second); > 59 > 60 if( luk.size() >= n ) > 61 return vector<int>(luk.begin(), luk.begin()+n); > 62 > 63 // infinites > 64 int v = luckySet.back()+1; > 65 while( luk.size() < n ) > 66 luk.push_back(v++); > 67 return luk; > 68 } > 69 }; > 70 > 71 // BEGIN CUT HERE > 72 #include <ctime> > 73 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 74 > 75 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 76 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 77 > 78 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 79 char Test_(...); > 80 int Test_(Case_<0>) { > 81 int luckySet_[] = {3}; > 82 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 83 int n = 6; > 84 int RetVal_[] = {3, 1, 2, 4, 5, 6 }; > 85 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 86 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 87 int Test_(Case_<1>) { > 88 int luckySet_[] = {5, 11, 18}; > 89 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 90 int n = 9; > 91 int RetVal_[] = {5, 11, 18, 1, 4, 6, 10, 2, 3 }; > 92 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 93 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 94 int Test_(Case_<2>) { > 95 int luckySet_[] = {7, 13, 18}; > 96 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 97 int n = 9; > 98 int RetVal_[] = {7, 13, 18, 14, 17, 8, 12, 1, 6 }; > 99 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 100 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 101 int Test_(Case_<3>) { > 102 int luckySet_[] = {1000, 1004, 4000, 4003, 5000}; > 103 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 104 int n = 19; > 105 int RetVal_[] = {1000, 1004, 4000, 4003, 5000, 4001, 4002, 1001, 1003, 1 > 106 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 107 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 108 int Test_(Case_<4>) { > 109 int luckySet_[] = {1000000000}; > 110 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 111 int n = 8; > 112 int RetVal_[] = {1000000000, 1, 999999999, 2, 999999998, 3, 999999997, 4 > 113 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 114 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 115 int Test_(Case_<5>) { > 116 int luckySet_[] = {2}; > 117 vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*l > 118 int n = 1; > 119 int RetVal_[] = {1}; > 120 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 121 return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); > 122 > 123 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 124 template<> void Run_<-1>() {} > 125 int main() { Run_<0>(); } > 126 // END CUT HERE > 127
Added SRM/438-U/1B.cpp version [65a8b90d6a216ada]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 string g_input; > 23 string g_program; > 24 LL g_num_d; > 25 LL g_non_d; > 26 > 27 class EndlessStringMachine { > 28 public: > 29 string getFragment(string input, string program, int s_, int min_, int m > 30 { > 31 LL s = s_; > 32 LL min = min_ - 1; > 33 LL max = max_; > 34 > 35 int nd = 0, id = -1; > 36 for(int i=0; i!=program.size(); ++i) > 37 if( program[i]=='$' ) > 38 ++nd, id=i; > 39 > 40 if( nd == 0 ) > 41 { > 42 string result; > 43 for(LL i=min; i<max; ++i) > 44 result += (i < program.size() ? program[i] : '-' > 45 return result; > 46 } > 47 else if( nd == 1 ) > 48 { > 49 // output = program[..id]^s input program[id+1..]^s > 50 string L = program.substr(0, id); > 51 string C = input; > 52 string R = program.substr(id+1); > 53 string result; > 54 for(LL i=min; i<max; ++i) > 55 if( i < L.size()*s ) > 56 result += L[i%L.size()]; > 57 else if( i < L.size()*s + C.size() ) > 58 result += C[i - L.size()*s]; > 59 else if( i < L.size()*s + C.size() + R.size()*s > 60 result += R[(i-L.size()*s-C.size()) % R. > 61 else > 62 result += '-'; > 63 return result; > 64 } > 65 else > 66 { > 67 g_input = input; > 68 g_program = program; > 69 g_num_d = nd; > 70 g_non_d = program.size() - nd; > 71 > 72 string result; > 73 for(int i=min; i<max; ++i) > 74 result += theChar(i, s); > 75 return result; > 76 } > 77 } > 78 > 79 char theChar( LL i, LL s ) > 80 { > 81 if( s == 0 ) > 82 { > 83 return ( i < g_input.size() ? g_input[i] : '-' ); > 84 } > 85 > 86 LL cur = 0; > 87 for(LL ip=0; ip<g_program.size(); ++ip) > 88 { > 89 if( g_program[ip]=='$' ) { > 90 LL len = theLen(s-1); > 91 if( cur<=i && i<cur+len ) > 92 { > 93 i -= cur; > 94 LL theLevel = 0, lvLen = g_input.size(); > 95 while( lvLen <= i ) > 96 theLevel ++, lvLen = lvLen*g_num > 97 return theChar(i, theLevel); > 98 } > 99 else > 100 cur += len; > 101 } else { > 102 if( cur == i ) > 103 return g_program[ip]; > 104 else > 105 cur += 1; > 106 } > 107 } > 108 return '-'; > 109 } > 110 > 111 LL theLen(LL s) > 112 { > 113 if( s == 0 ) > 114 return g_input.size(); > 115 if( s > 32 ) > 116 return 2000000000LL; > 117 LL pr = theLen(s-1); > 118 LL nx = pr*g_num_d + g_non_d; > 119 return min(2000000000LL, nx); > 120 } > 121 }; > 122 > 123 // BEGIN CUT HERE > 124 #include <ctime> > 125 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 126 > 127 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 128 int verify_case(const string &Expected, const string &Received) { if (Expected = > 129 > 130 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 131 char Test_(...); > 132 int Test_(Case_<0>) { > 133 string input = "a"; > 134 string program = "$meric$"; > 135 int s = 6; > 136 int min = 1; > 137 int max = 35; > 138 string RetVal = "americamericamericamericamericameri"; > 139 return verify_case(RetVal, EndlessStringMachine().getFragment(input, pro > 140 int Test_(Case_<1>) { > 141 string input = "top"; > 142 string program = "$coder"; > 143 int s = 1; > 144 int min = 1; > 145 int max = 20; > 146 string RetVal = "topcoder------------"; > 147 return verify_case(RetVal, EndlessStringMachine().getFragment(input, pro > 148 int Test_(Case_<2>) { > 149 string input = "abc"; > 150 string program = "$x$y$z$"; > 151 int s = 10; > 152 int min = 30; > 153 int max = 50; > 154 string RetVal = "bcyabcxabcyabczabczab"; > 155 return verify_case(RetVal, EndlessStringMachine().getFragment(input, pro > 156 int Test_(Case_<3>) { > 157 string input = "xy"; > 158 string program = "$a$bb"; > 159 int s = 12; > 160 int min = 5000; > 161 int max = 5099; > 162 string RetVal = "xybbbbaxyaxybbaxyaxybbbbbbbbaxyaxybbaxyaxybbbbaxyaxybba > 163 return verify_case(RetVal, EndlessStringMachine().getFragment(input, pro > 164 int Test_(Case_<4>) { > 165 string input = "x"; > 166 string program = "$y$"; > 167 int s = 999999999; > 168 int min = 5000; > 169 int max = 5001; > 170 string RetVal = "xx"; > 171 return verify_case(RetVal, EndlessStringMachine().getFragment(input, pro > 172 > 173 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 174 template<> void Run_<-1>() {} > 175 int main() { Run_<0>(); } > 176 // END CUT HERE > 177
Added SRM/439-U/1A.cpp version [403128c17a6bd8b1]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PouringWater { > 23 public: > 24 int nbits(int n) { > 25 int cnt = 0; > 26 while(n) { > 27 if(n&1) ++cnt; > 28 n>>=1; > 29 } > 30 return cnt; > 31 } > 32 int getMinBottles(int N, int K) > 33 { > 34 for(int i=0;;++i) > 35 if( nbits(N+i) <= K ) > 36 return i; > 37 } > 38 }; > 39 > 40 // BEGIN CUT HERE > 41 #include <ctime> > 42 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 43 > 44 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 45 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 46 > 47 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 48 char Test_(...); > 49 int Test_(Case_<0>) { > 50 int N = 3; > 51 int K = 1; > 52 int RetVal = 1; > 53 return verify_case(RetVal, PouringWater().getMinBottles(N, K)); } > 54 int Test_(Case_<1>) { > 55 int N = 13; > 56 int K = 2; > 57 int RetVal = 3; > 58 return verify_case(RetVal, PouringWater().getMinBottles(N, K)); } > 59 int Test_(Case_<2>) { > 60 int N = 1000000; > 61 int K = 5; > 62 int RetVal = 15808; > 63 return verify_case(RetVal, PouringWater().getMinBottles(N, K)); } > 64 > 65 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 66 template<> void Run_<-1>() {} > 67 int main() { Run_<0>(); } > 68 // END CUT HERE > 69
Added SRM/439-U/1B.cpp version [286098999b75ba9e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PalindromePhrases { > 23 public: > 24 vector<string> W; > 25 int N; > 26 map<pair<int,string>, LL> memo1; > 27 map<pair<int,string>, LL> memo2; > 28 map<pair<int,string>, LL> memo3; > 29 map<pair<int,string>, LL> memo4; > 30 map<pair<int,string>, LL> memo5; > 31 > 32 string rev(string s) > 33 { > 34 reverse(s.begin(), s.end()); > 35 return s; > 36 } > 37 > 38 bool prefix_all(const string& p, const string& a) > 39 { > 40 return p.size() <= a.size() && equal(p.begin(), p.end(), a.begin > 41 } > 42 > 43 bool suffix_all(const string& p, const string& a) > 44 { > 45 return p.size() <= a.size() && equal(p.begin(), p.end(), a.begin > 46 } > 47 > 48 long long getAmount(vector <string> words) > 49 { > 50 W = words; > 51 N = W.size(); > 52 > 53 LL cnt = 0; > 54 for(int i=0; i<N; ++i) > 55 { > 56 int mask = (1<<N) - 1; > 57 mask &= ~ (1<<i); > 58 > 59 cnt += rec_startpalin(mask, W[i]); > 60 } > 61 return cnt; > 62 } > 63 > 64 LL rec_startpalin(int mask, const string& w) // #{ws | ws is palin} > 65 { > 66 pair<int,string> key(mask, w); > 67 if( memo1.count(key) ) > 68 return memo1[key]; > 69 > 70 string rw = rev(w); > 71 > 72 LL cnt = 0; > 73 cnt += rec_end(mask, rw); > 74 for(int ov=1; ov<=w.size(); ++ov) > 75 if( w.substr(w.size()-ov) == rw.substr(0,ov) ) > 76 cnt += exact(mask, rw.substr(ov)); > 77 return memo1[key]=cnt; > 78 } > 79 > 80 LL rec_endpalin(int mask, const string& w) // #{sw | sw is palin} > 81 { > 82 pair<int,string> key(mask, w); > 83 if( memo4.count(key) ) > 84 return memo4[key]; > 85 > 86 string rw = rev(w); > 87 > 88 LL cnt = 0; > 89 cnt += rec(mask, rw); > 90 for(int ov=1; ov<=w.size(); ++ov) > 91 if( rw.substr(w.size()-ov) == w.substr(0,ov) ) > 92 cnt += exact(mask, rw.substr(0,w.size()-ov)); > 93 return memo4[key]=cnt; > 94 } > 95 > 96 LL rec(int mask, const string& w) // #{ws | s is palin} > 97 { > 98 pair<int,string> key(mask, w); > 99 if( memo2.count(key) ) > 100 return memo2[key]; > 101 > 102 LL cnt = (w.empty() ? 1 : 0); > 103 for(int i=0; i<N; ++i) > 104 if( mask & (1<<i) ) > 105 { > 106 if( prefix_all(W[i], w) ) > 107 cnt += rec( mask &~ (1<<i), > 108 w.substr(W[i].size()) > 109 ); > 110 else if( prefix_all(w, W[i]) ) > 111 cnt += rec_startpalin( mask &~ (1<<i), > 112 W[i].substr(w.size()) > 113 ); > 114 } > 115 return memo2[key]=cnt; > 116 } > 117 > 118 LL rec_end(int mask, const string& w) // #{sw | s is palin} > 119 { > 120 pair<int,string> key(mask, w); > 121 if( memo5.count(key) ) > 122 return memo5[key]; > 123 > 124 LL cnt = (w.empty() ? 1 : 0); > 125 for(int i=0; i<N; ++i) > 126 if( mask & (1<<i) ) > 127 { > 128 if( suffix_all(W[i], w) ) > 129 cnt += rec_end( mask &~ (1<<i), > 130 w.substr(0,w.size()-W[i].size()) > 131 ); > 132 else if( suffix_all(w, W[i]) ) > 133 cnt += rec_endpalin( mask &~ (1<<i), > 134 W[i].substr(0, W[i].size()-w.siz > 135 ); > 136 } > 137 return memo5[key]=cnt; > 138 } > 139 > 140 LL exact(int mask, const string& w) // # of ways to create w > 141 { > 142 pair<int,string> key(mask, w); > 143 if( memo3.count(key) ) > 144 return memo3[key]; > 145 > 146 LL cnt = (w.empty() ? 1 : 0); > 147 for(int i=0; i<N; ++i) > 148 if( mask & (1<<i) ) > 149 if( prefix_all(W[i], w) ) > 150 cnt += exact( mask &~ (1<<i), w.substr(W > 151 > 152 return memo3[key] = cnt; > 153 } > 154 > 155 > 156 }; > 157 > 158 // BEGIN CUT HERE > 159 #include <ctime> > 160 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 161 > 162 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 163 int verify_case(const long long &Expected, const long long &Received) { if (Expe > 164 > 165 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 166 char Test_(...); > 167 int Test_(Case_<0>) { > 168 string words_[] = {"a","ba"}; > 169 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 170 long long RetVal = 2LL; > 171 return verify_case(RetVal, PalindromePhrases().getAmount(words)); } > 172 int Test_(Case_<1>) { > 173 string words_[] = {"ab","bcd","efg"}; > 174 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 175 long long RetVal = 0LL; > 176 return verify_case(RetVal, PalindromePhrases().getAmount(words)); } > 177 int Test_(Case_<2>) { > 178 string words_[] = {"a", "bba", "abb"}; > 179 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 180 long long RetVal = 7LL; > 181 return verify_case(RetVal, PalindromePhrases().getAmount(words)); } > 182 int Test_(Case_<3>) { > 183 string words_[] = {"aabccc", "ccbbca", "a", "acaabb", "aaa", "aab", "c", > 184 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 185 long long RetVal = 47LL; > 186 return verify_case(RetVal, PalindromePhrases().getAmount(words)); } > 187 int Test_(Case_<4>) { > 188 string words_[] = {"a", "aa", "aaa"}; > 189 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 190 long long RetVal = 47LL; > 191 return verify_case(RetVal, PalindromePhrases().getAmount(words)); } > 192 > 193 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 194 template<> void Run_<-1>() {} > 195 int main() { Run_<0>(); } > 196 // END CUT HERE
Added SRM/440/1A.cpp version [40b011870ac59713]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class IncredibleMachine { > 23 public: > 24 double gravitationalAcceleration(vector <int> x, vector <int> y, int T) > 25 { > 26 double gL = 0.0, gR = 1e+9; > 27 for(int i=0; i<100; ++i) > 28 { > 29 double gC = (gL + gR) / 2; > 30 double tC = est(x, y, gC); > 31 (tC < T ? gR : gL) = gC; > 32 } > 33 return gL; > 34 } > 35 > 36 double est(const vector<int>& x, const vector<int>& y, double g) > 37 { > 38 double v = 0.0; > 39 double tot_t = 0.0; > 40 for(int i=0; i+1<x.size(); ++i) > 41 { > 42 double v0 = v; > 43 double len = sqrt(double((x[i+1]-x[i])*(x[i+1]-x[i]) + ( > 44 double sin_alpha = (y[i]-y[i+1]) / len; > 45 double a = g * sin_alpha; > 46 // len = v0*t + 0.5a t^2 > 47 // 0.5a t^2 + v0 t - len = 0 > 48 double t = (-v0 + sqrt(v0*v0 + 2*a*len)) / a; > 49 v = v0 + a*t; > 50 tot_t += t; > 51 } > 52 return tot_t; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 59 > 60 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 61 int verify_case(const double &Expected, const double &Received) { double diff = > 62 > 63 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 64 char Test_(...); > 65 int Test_(Case_<0>) { > 66 int x_[] = {0,6}; > 67 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 68 int y_[] = {100,22}; > 69 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 70 int T = 4; > 71 double RetVal = 9.807692307692307; > 72 return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration > 73 int Test_(Case_<1>) { > 74 int x_[] = {0,26,100}; > 75 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 76 int y_[] = {50,26,24}; > 77 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 78 int T = 4; > 79 double RetVal = 26.743031720603582; > 80 return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration > 81 int Test_(Case_<2>) { > 82 int x_[] = {0,7,8}; > 83 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 84 int y_[] = {10,6,0}; > 85 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 86 int T = 7; > 87 double RetVal = 1.1076837407708007; > 88 return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration > 89 int Test_(Case_<3>) { > 90 int x_[] = {0,100}; > 91 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 92 int y_[] = {1,0}; > 93 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 94 int T = 1; > 95 double RetVal = 20002; > 96 return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration > 97 int Test_(Case_<4>) { > 98 int x_[] = {0,1}; > 99 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 100 int y_[] = {100,0}; > 101 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 102 int T = 100; > 103 double RetVal = 0.020002; > 104 return verify_case(RetVal, IncredibleMachine().gravitationalAcceleration > 105 > 106 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 107 template<> void Run_<-1>() {} > 108 int main() { Run_<0>(); } > 109 // END CUT HERE > 110
Added SRM/440/1B.cpp version [3dfb717e5a03a7d8]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int dy[] = {-1,+1,0,0}; > 23 static const int dx[] = {0,0,-1,+1}; > 24 > 25 class MazeWandering { > 26 public: > 27 double expectedTime(vector <string> maze) > 28 { > 29 int Y = maze.size(); > 30 int X = maze[0].size(); > 31 > 32 vector< vector<int> > d(Y, vector<int>(X, -1)); > 33 vector< pair<int,int> > q, Q; > 34 > 35 for(int y=0; y<Y; ++y) > 36 for(int x=0; x<X; ++x) > 37 if( maze[y][x] == '*' ) { > 38 d[y][x] = 0; > 39 q.push_back( make_pair(y,x) ); > 40 Q.push_back( make_pair(y,x) ); > 41 } > 42 > 43 for(int step=1; !q.empty(); ++step) > 44 { > 45 vector< pair<int,int> > q2; > 46 for(int j=0; j<q.size(); ++j) > 47 { > 48 int qy = q[j].first; > 49 int qx = q[j].second; > 50 for(int i=0; i<4; ++i) > 51 { > 52 int y = qy+dy[i], x = qx+dx[i]; > 53 if( 0<=y && y<Y && 0<=x && x<X && maze[y > 54 { > 55 d[y][x] = step; > 56 q2.push_back( make_pair(y,x) ); > 57 Q.push_back( make_pair(y,x) ); > 58 } > 59 } > 60 } > 61 q2.swap(q); > 62 } > 63 > 64 vector< vector<int> > w(Y, vector<int>(X, 0)); > 65 for(int j=Q.size()-1; j>0; --j) > 66 { > 67 int qy = Q[j].first; > 68 int qx = Q[j].second; > 69 > 70 for(int i=0; i<4; ++i) > 71 { > 72 int y = qy+dy[i], x = qx+dx[i]; > 73 if( 0<=y && y<Y && 0<=x && x<X && maze[y][x]!='X > 74 w[qy][qx] += w[y][x] + 1; > 75 } > 76 } > 77 > 78 double esum = 0.0; > 79 vector< vector<double> > e(Y, vector<double>(X, 0.0)); > 80 for(int j=0; j<Q.size(); ++j) > 81 { > 82 int qy = Q[j].first; > 83 int qx = Q[j].second; > 84 if( d[qy][qx] > 0 ) > 85 { > 86 for(int i=0; i<4; ++i) > 87 { > 88 int y = qy+dy[i], x = qx+dx[i]; > 89 if( 0<=y && y<Y && 0<=x && x<X && maze[y > 90 { > 91 e[qy][qx] = e[y][x] + 1 + 2*w[qy > 92 esum += e[qy][qx]; > 93 break; > 94 } > 95 } > 96 } > 97 } > 98 return esum / Q.size(); > 99 } > 100 }; > 101 > 102 // BEGIN CUT HERE > 103 #include <ctime> > 104 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 105 > 106 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 107 int verify_case(const double &Expected, const double &Received) { double diff = > 108 > 109 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 110 char Test_(...); > 111 int Test_(Case_<0>) { > 112 string maze_[] = {"*", > 113 "."}; > 114 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 115 double RetVal = 0.5; > 116 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 117 int Test_(Case_<1>) { > 118 string maze_[] = {"*.."}; > 119 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 120 double RetVal = 2.3333333333333335; > 121 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 122 int Test_(Case_<2>) { > 123 string maze_[] = {"...", > 124 "X*X", > 125 "..."}; > 126 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 127 double RetVal = 4.857142857142857; > 128 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 129 int Test_(Case_<3>) { > 130 string maze_[] = {".*.", > 131 ".XX", > 132 "..."}; > 133 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 134 double RetVal = 13.714285714285714; > 135 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 136 int Test_(Case_<4>) { > 137 string maze_[] = {"*........", > 138 "XXX.XXXX.", > 139 ".XX.X....", > 140 ".....XX.X"}; > 141 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 142 double RetVal = 167.2608695652174; > 143 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 144 int Test_(Case_<5>) { > 145 string maze_[] = {"X*X"}; > 146 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 147 double RetVal = 0; > 148 return verify_case(RetVal, MazeWandering().expectedTime(maze)); } > 149 > 150 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 151 template<> void Run_<-1>() {} > 152 int main() { Run_<0>(); } > 153 // END CUT HERE > 154
Added SRM/440/1C.cpp version [29d8b3c8147890db]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int MODVAL = 1000000007; > 23 > 24 bool is_square_free(int n) > 25 { > 26 for(int p=2; p*p<=n; ++p) > 27 if( n%(p*p) == 0 ) > 28 return false; > 29 return true; > 30 } > 31 > 32 static const int SP[] = {2,3,5,7,11,13,17,19}; // <= sqrt(N) > 33 static const int SPN = sizeof(SP)/sizeof(*SP); > 34 > 35 int small_prime_mask(int n) > 36 { > 37 int m = 0; > 38 for(int i=0; i<SPN; ++i) > 39 if( n%SP[i] == 0 ) > 40 m |= 1<<i; > 41 return m; > 42 } > 43 > 44 int big_prime_factor(int n) > 45 { > 46 for(int i=0; i<SPN; ++i) > 47 while( n%SP[i] == 0 ) > 48 n /= SP[i]; > 49 return n; > 50 } > 51 > 52 class SquareFreeSets { > 53 public: > 54 int countPerfect(int N, int K) > 55 { > 56 // way[k][m] = # of ways to make a k-elem psf-set with small pri > 57 int way[501][1<<SPN] = {}; > 58 way[0][0] = 1; > 59 > 60 for(int n=2; n<=N; ++n) > 61 if( is_square_free(n) ) > 62 if( big_prime_factor(n) == 1 ) > 63 { > 64 int mask = small_prime_mask(n); > 65 for(int k=K; k>=1; --k) > 66 for(int m=0; m<(1<<SPN); ++m) > 67 if( (m|mask) == m ) > 68 way[k][m] = (way > 69 } > 70 else if( big_prime_factor(n) == n ) > 71 { > 72 vector<int> mask; > 73 for(int p=1; n*p<=N; ++p) > 74 if( is_square_free(p) ) > 75 mask.push_back( small_pr > 76 > 77 for(int k=K; k>=1; --k) > 78 for(int m=0; m<(1<<SPN); ++m) > 79 for(int j=0; j<mask.size > 80 if( (m|mask[j]) > 81 way[k][m > 82 } > 83 > 84 // summing up > 85 int sum = 0; > 86 for(int k=1; k<=K; ++k) > 87 for(int m=0; m<(1<<SPN); ++m) > 88 sum = (sum + way[k][m]) % MODVAL; > 89 return sum; > 90 } > 91 }; > 92 > 93 // BEGIN CUT HERE > 94 #include <ctime> > 95 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 96 > 97 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 98 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 99 > 100 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 101 char Test_(...); > 102 int Test_(Case_<0>) { > 103 int N = 5; > 104 int K = 1; > 105 int RetVal = 3; > 106 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 107 int Test_(Case_<1>) { > 108 int N = 5; > 109 int K = 2; > 110 int RetVal = 6; > 111 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 112 int Test_(Case_<2>) { > 113 int N = 5; > 114 int K = 3; > 115 int RetVal = 7; > 116 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 117 int Test_(Case_<3>) { > 118 int N = 6; > 119 int K = 3; > 120 int RetVal = 9; > 121 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 122 int Test_(Case_<4>) { > 123 int N = 28; > 124 int K = 41; > 125 int RetVal = 1599; > 126 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 127 int Test_(Case_<5>) { > 128 int N = 500; > 129 int K = 500; > 130 int RetVal = 59964104; > 131 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 132 int Test_(Case_<6>) { > 133 int N = 100; > 134 int K = 1; > 135 int RetVal = 60; > 136 return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } > 137 > 138 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 139 template<> void Run_<-1>() {} > 140 int main() { Run_<0>(); } > 141 // END CUT HERE > 142
Added SRM/441-U/1A.cpp version [e8ca5f00584e6a13]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PerfectPermutation { public: > 23 int reorder(vector <int> P) > 24 { > 25 vector<bool> used(P.size(), false); > 26 > 27 int num_loop = 0; > 28 for(int i=0; i<P.size(); ++i) > 29 if( !used[i] ) { > 30 ++num_loop; > 31 > 32 for(int p=i; !used[p]; p=P[p]) > 33 used[p] = true; > 34 } > 35 return num_loop==1 ? 0 : num_loop; > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const int& Expected, const int& Received) { > 48 bool ok = (Expected == Received); > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, PerfectPermutation().reorder(P));} > 53 int main(){ > 54 > 55 CASE(0) > 56 int P_[] = {2, 0, 1}; > 57 vector <int> P(P_, P_+sizeof(P_)/sizeof(*P_)); > 58 int _ = 0; > 59 END > 60 CASE(1) > 61 int P_[] = {2, 0, 1, 4, 3}; > 62 vector <int> P(P_, P_+sizeof(P_)/sizeof(*P_)); > 63 int _ = 2; > 64 END > 65 CASE(2) > 66 int P_[] = {2, 3, 0, 1}; > 67 vector <int> P(P_, P_+sizeof(P_)/sizeof(*P_)); > 68 int _ = 2; > 69 END > 70 CASE(3) > 71 int P_[] = {0, 5, 3, 2, 1, 4}; > 72 vector <int> P(P_, P_+sizeof(P_)/sizeof(*P_)); > 73 int _ = 3; > 74 END > 75 CASE(4) > 76 int P_[] = {4, 2, 6, 0, 3, 5, 9, 7, 8, 1}; > 77 vector <int> P(P_, P_+sizeof(P_)/sizeof(*P_)); > 78 int _ = 5; > 79 END > 80 > 81 } > 82 // END CUT HERE
Added SRM/441-U/1B.cpp version [607f8314241c61a9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class StrangeCountry { public: > 23 int transform(vector <string> g) > 24 { > 25 int nConn=g.size(), nEdge=0; > 26 > 27 vector<int> uf(g.size(), -1); // union_find > 28 for(int x=0; x<g.size(); ++x) > 29 { > 30 if( g[x].find('Y')==-1 ) > 31 return -1; // orphan > 32 > 33 for(int y=x+1; y<g.size(); ++y) > 34 if( g[x][y]=='Y' ) { > 35 ++nEdge; > 36 int rx=x; while( uf[rx]>=0 ) rx=uf[rx]; > 37 int ry=y; while( uf[ry]>=0 ) ry=uf[ry]; > 38 if( rx != ry ) uf[rx]=ry, --nConn; > 39 } > 40 } > 41 > 42 int redundant = nEdge - (g.size() - nConn); > 43 return redundant>=nConn-1 ? nConn-1 : -1; > 44 } > 45 }; > 46 > 47 // BEGIN CUT HERE > 48 #include <ctime> > 49 double start_time; string timer() > 50 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 51 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 52 { os << "{ "; > 53 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 54 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 55 void verify_case(const int& Expected, const int& Received) { > 56 bool ok = (Expected == Received); > 57 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 58 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 59 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 60 #define END verify_case(_, StrangeCountry().transform(g));} > 61 int main(){ > 62 > 63 CASE(0) > 64 string g_[] = {"NY", > 65 "YN"}; > 66 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 67 int _ = 0; > 68 END > 69 CASE(1) > 70 string g_[] = {"NYYNN", > 71 "YNYNN", > 72 "YYNNN", > 73 "NNNNY", > 74 "NNNYN"}; > 75 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 76 int _ = 1; > 77 END > 78 CASE(2) > 79 string g_[] = {"NYYNNNN", > 80 "YNYNNNN", > 81 "YYNNNNN", > 82 "NNNNYYN", > 83 "NNNYNYY", > 84 "NNNYYNY", > 85 "NNNNYYN"}; > 86 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 87 int _ = 1; > 88 END > 89 CASE(3) > 90 string g_[] = {"NYNYNNNNNNNN", > 91 "YNYNNNNNNNNN", > 92 "NYNYYNNNNNNN", > 93 "YNYNNNNNNNNN", > 94 "NNYNNYYNNNNN", > 95 "NNNNYNYNNNNN", > 96 "NNNNYYNNNNNN", > 97 "NNNNNNNNYYNN", > 98 "NNNNNNNYNYNN", > 99 "NNNNNNNYYNNN", > 100 "NNNNNNNNNNNY", > 101 "NNNNNNNNNNYN"}; > 102 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 103 int _ = 2; > 104 END > 105 CASE(4) > 106 string g_[] = {"NYNNNN", > 107 "YNYNNN", > 108 "NYNYNN", > 109 "NNYNNN", > 110 "NNNNNY", > 111 "NNNNYN"}; > 112 vector <string> g(g_, g_+sizeof(g_)/sizeof(*g_)); > 113 int _ = -1; > 114 END > 115 > 116 } > 117 // END CUT HERE
Added SRM/442-U/1A.cpp version [0aeee53c2fe85791]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int nf(int n) > 23 { > 24 int c = 0; > 25 for(int p=2; p*p<=n; ++p) > 26 if( n%p==0 ) > 27 while(n%p==0) > 28 c++, n/=p; > 29 return c + (n==1 ? 0 : 1); > 30 } > 31 > 32 class Underprimes { public: > 33 int howMany(int A, int B) > 34 { > 35 int cnt = 0; > 36 for(int n=A; n<=B; ++n) > 37 if( nf(nf(n)) == 1 ) > 38 ++cnt; > 39 return cnt; > 40 } > 41 }; > 42 > 43 // BEGIN CUT HERE > 44 #include <ctime> > 45 double start_time; string timer() > 46 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 47 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 48 { os << "{ "; > 49 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 50 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 51 void verify_case(const int& Expected, const int& Received) { > 52 bool ok = (Expected == Received); > 53 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 54 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 55 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 56 #define END verify_case(_, Underprimes().howMany(A, B));} > 57 int main(){ > 58 > 59 CASE(0) > 60 int A = 2; > 61 int B = 10; > 62 int _ = 5; > 63 END > 64 CASE(1) > 65 int A = 100; > 66 int B = 105; > 67 int _ = 2; > 68 END > 69 CASE(2) > 70 int A = 17; > 71 int B = 17; > 72 int _ = 0; > 73 END > 74 CASE(3) > 75 int A = 123; > 76 int B = 456; > 77 int _ = 217; > 78 END > 79 > 80 } > 81 // END CUT HERE
Added SRM/442-U/1B.cpp version [3917b4343fc07af5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BedroomFloor { public: > 23 LL b[6]; > 24 > 25 long long numberOfSticks(int x1, int y1, int x2, int y2) > 26 { > 27 b[1] = b[2] = b[3] = b[4] = b[5] = 0; > 28 > 29 devideX_and_count(x1, x2, y1, y2); > 30 return optimize(); > 31 } > 32 > 33 // utils > 34 LL FLOOR(LL x) { return x/5*5; } > 35 LL CEIL (LL x) { return FLOOR(x+4); } > 36 > 37 // counting routines > 38 void devideX_and_count(LL x1, LL x2, LL y1, LL y2) > 39 { > 40 if( FLOOR(x1) == FLOOR(x2) ) > 41 devideY_and_count(x1, x2, y1, y2, 1); > 42 else > 43 devideY_and_count( x1, CEIL(x1), y1, y2, 1 ), > 44 devideY_and_count( CEIL(x1), CEIL(x1)+5, y1, y2, ((FL > 45 devideY_and_count( CEIL(x1)+5, CEIL(x1)+10, y1, y2, ((FL > 46 devideY_and_count( FLOOR(x2), x2, y1, y2, 1 ); > 47 } > 48 > 49 void devideY_and_count(LL x1, LL x2, LL y1, LL y2, LL rep) > 50 { > 51 if( FLOOR(y1) == FLOOR(y2) ) > 52 count(x1, x2, y1, y2, rep); > 53 else > 54 count(x1, x2, y1, CEIL(y1), rep), > 55 count(x1, x2, CEIL(y1), CEIL(y1)+5, rep*(((FLOOR(y2)- > 56 count(x1, x2, CEIL(y1)+5, CEIL(y1)+10, rep*(((FLOOR(y2)- > 57 count(x1, x2, FLOOR(y2), y2 , rep); > 58 } > 59 > 60 void count(LL x1, LL x2, LL y1, LL y2, LL rep) > 61 { > 62 if( x1/5%2 == y1/5%2 ) // is_horizontal > 63 b[x2-x1] += rep*(y2-y1); > 64 else > 65 b[y2-y1] += rep*(x2-x1); > 66 } > 67 > 68 // compute minimum number of 1x5 blocks required (by greedily removing l > 69 LL optimize() > 70 { > 71 LL total = 0; > 72 for(int K=6*6*6*6*6; --K>0; ) > 73 { > 74 int k[] = {-1, K%6, K/6%6, K/6/6%6, K/6/6/6%6, K/6/6/6/6 > 75 if( 1*k[1] + 2*k[2] + 3*k[3] + 4*k[4] + 5*k[5] <= 5 ) > 76 { > 77 LL n = 1LL<<62; > 78 for(int i=1; i<=5; ++i) > 79 if( k[i] ) > 80 n = min(n, b[i]/k[i]); > 81 for(int i=1; i<=5; ++i) > 82 b[i] -= n*k[i]; > 83 total += n; > 84 } > 85 } > 86 return total; > 87 } > 88 }; > 89 > 90 // BEGIN CUT HERE > 91 #include <ctime> > 92 double start_time; string timer() > 93 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 94 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 95 { os << "{ "; > 96 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 97 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 98 void verify_case(const long long& Expected, const long long& Received) { > 99 bool ok = (Expected == Received); > 100 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 101 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 102 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 103 #define END verify_case(_, BedroomFloor().numberOfSticks(x1, y1, x2, y2));} > 104 int main(){ > 105 > 106 CASE(0) > 107 int x1 = 0; > 108 int y1 = 0; > 109 int x2 = 5; > 110 int y2 = 5; > 111 long long _ = 5LL; > 112 END > 113 CASE(1) > 114 int x1 = 0; > 115 int y1 = 0; > 116 int x2 = 10; > 117 int y2 = 2; > 118 long long _ = 5LL; > 119 END > 120 CASE(2) > 121 int x1 = 2; > 122 int y1 = 2; > 123 int x2 = 8; > 124 int y2 = 8; > 125 long long _ = 12LL; > 126 END > 127 CASE(3) > 128 int x1 = 8; > 129 int y1 = 5; > 130 int x2 = 20; > 131 int y2 = 16; > 132 long long _ = 27LL; > 133 END > 134 CASE(4) > 135 int x1 = 0; > 136 int y1 = 0; > 137 int x2 = 1000000; > 138 int y2 = 1000000; > 139 long long _ = 200000000000LL; > 140 END > 141 CASE(5) > 142 int x1 = 1; > 143 int y1 = 4; > 144 int x2 = 20; > 145 int y2 = 19; > 146 long long _ = 58LL; > 147 END > 148 CASE(5) > 149 int x1 = 49; > 150 int y1 = 51; > 151 int x2 = 64; > 152 int y2 = 70; > 153 long long _ = 58LL; > 154 END > 155 > 156 } > 157 // END CUT HERE
Added SRM/442-U/1C-U.cpp version [e2301b523ba75830]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NowhereLand { public: > 23 int placeGuards(vector <string> cities, int k, vector <string> guards, v > 24 { > 25 int mc = 0; > 26 for(int gid=0; gid<k; ++gid) > 27 mc += minimizeConflict(cities, guards, agencies, gid); > 28 return mc; > 29 } > 30 > 31 int minimizeConflict(vector<string>& C, vector<string>& G_, vector<strin > 32 { > 33 vector<bool> G(C.size()); > 34 vector<bool> A(C.size()); > 35 for(int i=0; i<C.size(); ++i) > 36 { > 37 { stringstream sin(G_[i]); > 38 for(int g; sin>>g; ) if(g==gid) G[i] = true; } > 39 { stringstream sin(A_[i]); > 40 for(int g; sin>>g; ) if(g==gid) A[i] = true; } > 41 } > 42 return -1; > 43 } > 44 }; > 45 > 46 // BEGIN CUT HERE > 47 #include <ctime> > 48 double start_time; string timer() > 49 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 50 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 51 { os << "{ "; > 52 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 53 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 54 void verify_case(const int& Expected, const int& Received) { > 55 bool ok = (Expected == Received); > 56 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 57 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 58 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 59 #define END verify_case(_, NowhereLand().placeGuards(cities, k, guards, age > 60 int main(){ > 61 > 62 CASE(0) > 63 string cities_[] = { "0111", > 64 "1000", > 65 "1000", > 66 "1000" }; > 67 vector <string> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities > 68 int k = 1; > 69 string guards_[] = { "0", "", "", "" }; > 70 vector <string> guards(guards_, guards_+sizeof(guards_)/sizeof(*guards > 71 string agencies_[] = { "0", "0", "", "0" }; > 72 vector <string> agencies(agencies_, agencies_+sizeof(agencies_)/sizeof > 73 int _ = 1; > 74 END > 75 CASE(1) > 76 string cities_[] = { "011", > 77 "101", > 78 "110" }; > 79 vector <string> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities > 80 int k = 1; > 81 string guards_[] = { "0", "", "" }; > 82 vector <string> guards(guards_, guards_+sizeof(guards_)/sizeof(*guards > 83 string agencies_[] = { "0", "", "" }; > 84 vector <string> agencies(agencies_, agencies_+sizeof(agencies_)/sizeof > 85 int _ = 2; > 86 END > 87 CASE(2) > 88 string cities_[] = { "011", > 89 "101", > 90 "110" }; > 91 vector <string> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities > 92 int k = 1; > 93 string guards_[] = { "", "", "" } ; > 94 vector <string> guards(guards_, guards_+sizeof(guards_)/sizeof(*guards > 95 string agencies_[] = { "0", "0", "0" }; > 96 vector <string> agencies(agencies_, agencies_+sizeof(agencies_)/sizeof > 97 int _ = 0; > 98 END > 99 CASE(3) > 100 string cities_[] = { "010100", > 101 "101100", > 102 "010011", > 103 "110010", > 104 "001100", > 105 "001000" }; > 106 vector <string> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities > 107 int k = 3; > 108 string guards_[] = { "1 2", "", "1", "", "0", "0" }; > 109 vector <string> guards(guards_, guards_+sizeof(guards_)/sizeof(*guards > 110 string agencies_[] = { "0 1 2", "0 1", "0 1 2", "1 2", "0", "0" }; > 111 vector <string> agencies(agencies_, agencies_+sizeof(agencies_)/sizeof > 112 int _ = 7; > 113 END > 114 > 115 } > 116 // END CUT HERE
Added SRM/443-U/1A.cpp version [9e9e07b8082e082d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CirclesCountry { public: > 23 int leastBorders(vector <int> X, vector <int> Y, vector <int> R, int x1, > 24 { > 25 int cnt = 0; > 26 for(int i=0; i<X.size(); ++i) > 27 cnt += hypot(X[i]-x1, Y[i]-y1)<R[i] ^ hypot(X[i]-x2, Y[i > 28 return cnt; > 29 } > 30 }; > 31 > 32 // BEGIN CUT HERE > 33 #include <ctime> > 34 double start_time; string timer() > 35 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 36 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 37 { os << "{ "; > 38 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 39 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 40 void verify_case(const int& Expected, const int& Received) { > 41 bool ok = (Expected == Received); > 42 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 43 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 44 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 45 #define END verify_case(_, CirclesCountry().leastBorders(X, Y, R, x1, y1, x > 46 int main(){ > 47 > 48 CASE(0) > 49 int X_[] = {0}; > 50 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 51 int Y_[] = {0}; > 52 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 53 int R_[] = {2}; > 54 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 55 int x1 = -5; > 56 int y1 = 1; > 57 int x2 = 5; > 58 int y2 = 1; > 59 int _ = 0; > 60 END > 61 CASE(1) > 62 int X_[] = {0,-6,6}; > 63 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 64 int Y_[] = {0,1,2}; > 65 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 66 int R_[] = {2,2,2}; > 67 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 68 int x1 = -5; > 69 int y1 = 1; > 70 int x2 = 5; > 71 int y2 = 1; > 72 int _ = 2; > 73 END > 74 CASE(2) > 75 int X_[] = {1,-3,2,5,-4,12,12}; > 76 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 77 int Y_[] = {1,-1,2,5,5,1,1}; > 78 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 79 int R_[] = {8,1,2,1,1,1,2}; > 80 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 81 int x1 = -5; > 82 int y1 = 1; > 83 int x2 = 12; > 84 int y2 = 1; > 85 int _ = 3; > 86 END > 87 CASE(3) > 88 int X_[] = {-3,2,2,0,-4,12,12,12}; > 89 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 90 int Y_[] = {-1,2,3,1,5,1,1,1}; > 91 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 92 int R_[] = {1,3,1,7,1,1,2,3}; > 93 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 94 int x1 = 2; > 95 int y1 = 3; > 96 int x2 = 13; > 97 int y2 = 2; > 98 int _ = 5; > 99 END > 100 CASE(4) > 101 int X_[] = {-107,-38,140,148,-198,172,-179,148,176,153,-56,-187}; > 102 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 103 int Y_[] = {175,-115,23,-2,-49,-151,-52,42,0,68,109,-174}; > 104 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 105 int R_[] = {135,42,70,39,89,39,43,150,10,120,16,8}; > 106 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 107 int x1 = 102; > 108 int y1 = 16; > 109 int x2 = 19; > 110 int y2 = -108; > 111 int _ = 3; > 112 END > 113 CASE(5) > 114 int X_[] = {0}; > 115 vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_)); > 116 int Y_[] = {0}; > 117 vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_)); > 118 int R_[] = {2}; > 119 vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_)); > 120 int x1 = -5; > 121 int y1 = 1; > 122 int x2 = 2; > 123 int y2 = 2; > 124 int _ = 0; > 125 END > 126 > 127 } > 128 // END CUT HERE
Added SRM/443-U/1B.cpp version [1096f6b690aa542d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BinaryFlips { public: > 23 int minimalMoves(int A, int B, int K) > 24 { > 25 vector<int> visitedUpto(A+B+1, -1); > 26 visitedUpto[B] = B; > 27 > 28 // Breadth-first search from the initial state (A,B) > 29 queue< pair<int,int> > q; > 30 q.push( make_pair(B, 0) ); > 31 while( !q.empty() ) > 32 { > 33 // pop state (a,b) > 34 int b=q.front().first, a=A+B-b, step=q.front().second; q > 35 > 36 // if (a,b) == (0,A+B), done > 37 if( b == A+B ) > 38 return step; > 39 > 40 // for all possible next values (nb) of b... > 41 int nb_min = abs(b-K), nb_max = A+B-abs(a-K); > 42 for(int nb=nb_min; nb<=nb_max; nb+=2) > 43 if( visitedUpto[nb] == -1 ) > 44 { > 45 // if not visited, push the new state to > 46 q.push( make_pair(nb, step+1) ); > 47 visitedUpto[nb] = nb_max; > 48 } > 49 else > 50 { > 51 // if visited, skip it > 52 int nb2 = visitedUpto[nb]; > 53 visitedUpto[nb] = max(visitedUpto[nb], n > 54 nb = nb2; > 55 } > 56 } > 57 return -1; > 58 } > 59 }; > 60 > 61 // BEGIN CUT HERE > 62 #include <ctime> > 63 double start_time; string timer() > 64 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 65 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 66 { os << "{ "; > 67 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 68 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 69 void verify_case(const int& Expected, const int& Received) { > 70 bool ok = (Expected == Received); > 71 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 72 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 73 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 74 #define END verify_case(_, BinaryFlips().minimalMoves(A, B, K));} > 75 int main(){ > 76 > 77 CASE(0) > 78 int A = 3; > 79 int B = 0; > 80 int K = 3; > 81 int _ = 1; > 82 END > 83 CASE(1) > 84 int A = 4; > 85 int B = 0; > 86 int K = 3; > 87 int _ = 4; > 88 END > 89 CASE(2) > 90 int A = 4; > 91 int B = 1; > 92 int K = 3; > 93 int _ = 2; > 94 END > 95 CASE(3) > 96 int A = 3; > 97 int B = 2; > 98 int K = 5; > 99 int _ = -1; > 100 END > 101 CASE(4) > 102 int A = 100000; > 103 int B = 100000; > 104 int K = 578; > 105 int _ = 174; > 106 END > 107 CASE(5) > 108 int A = 0; > 109 int B = 0; > 110 int K = 1; > 111 int _ = 0; > 112 END > 113 CASE(6) > 114 int A = 4; > 115 int B = 44; > 116 int K = 50; > 117 int _ = -1; > 118 END > 119 CASE(7) > 120 int A = 11; > 121 int B = 99995; > 122 int K = 99999; > 123 int _ = 14285; > 124 END > 125 CASE(8) > 126 int A = 100000; > 127 int B = 100000; > 128 int K = 15781; > 129 int _ = 8; > 130 END > 131 CASE(9) > 132 int A = 7; > 133 int B = 99128; > 134 int K = 70613; > 135 int _ = 5; > 136 END > 137 CASE(10) > 138 int A = 45903; > 139 int B = 89257; > 140 int K = 98937; > 141 int _ = 3; > 142 END > 143 CASE(11) > 144 int A = 93851; > 145 int B = 72517; > 146 int K = 16031; > 147 int _ = 7; > 148 END > 149 > 150 } > 151 // END CUT HERE
Added SRM/444/1A.cpp version [3aabd385e4b10b4a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class UnfoldingTriangles { public: > 23 int solve(vector <string> grid, int unfoldLimit) > 24 { > 25 int X = grid[0].size(); > 26 int Y = grid.size(); > 27 > 28 int Max = -1; > 29 // top-right > 30 for(int y=0; y<Y; ++y) > 31 for(int x=X; x>=1; --x) > 32 { > 33 int numTri = 0; > 34 // size > 35 for(int n=1; x-n>=0 && y+n<=Y; ++n) > 36 { > 37 // right-check > 38 if( x<X && grid[y+n-1][x]=='#' ) > 39 goto NextTopRight; > 40 // diag-check > 41 if( grid[y+n-1][x-n]!='/' ) > 42 goto NextTopRight; > 43 // inner-check > 44 for(int k=1; k<n; ++k) { > 45 if( grid[y+n-1][x-k] == '.' ) > 46 goto NextTopRight; > 47 if( grid[y+n-1][x-k] == '/' ) > 48 if( ++numTri > unfoldLim > 49 goto NextTopRigh > 50 } > 51 // bottom-check > 52 bool ok = true; > 53 if( y+n == Y ) > 54 ok = true; > 55 else > 56 for(int k=1; k<=n; ++k) { > 57 if( grid[y+n][x-k] == '# > 58 {ok=false; break > 59 } > 60 > 61 // update > 62 if( ok ) > 63 Max = max( Max, n*(n+1)/2 ); > 64 } > 65 NextTopRight:; > 66 } > 67 > 68 return Max; > 69 } > 70 }; > 71 > 72 // BEGIN CUT HERE > 73 #include <ctime> > 74 double start_time; string timer() > 75 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 76 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 77 { os << "{ "; > 78 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 79 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 80 void verify_case(const int& Expected, const int& Received) { > 81 bool ok = (Expected == Received); > 82 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 83 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 84 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 85 #define END verify_case(_, UnfoldingTriangles().solve(grid, unfoldLimit));} > 86 int main(){ > 87 > 88 CASE(0) > 89 string grid_[] = {".../", > 90 "../#", > 91 "./#/", > 92 "/#//"}; > 93 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 94 int unfoldLimit = 4; > 95 int _ = 10; > 96 END > 97 CASE(1) > 98 string grid_[] = {".../", > 99 "../#", > 100 "./#/", > 101 "/#//"}; > 102 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 103 int unfoldLimit = 2; > 104 int _ = 3; > 105 END > 106 CASE(2) > 107 string grid_[] = {"////", > 108 "////", > 109 "////", > 110 "////"}; > 111 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 112 int unfoldLimit = 5; > 113 int _ = 6; > 114 END > 115 CASE(3) > 116 string grid_[] = {".....#...", > 117 "....###.."}; > 118 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 119 int unfoldLimit = 10; > 120 int _ = -1; > 121 END > 122 CASE(4) > 123 string grid_[] = {"#//#", > 124 "#//#", > 125 "####", > 126 "///#"}; > 127 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 128 int unfoldLimit = 4; > 129 int _ = 1; > 130 END > 131 CASE(5) > 132 string grid_[] = {".../.../", > 133 "../#..//", > 134 "./.#.///", > 135 "/###...."}; > 136 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 137 int unfoldLimit = 3; > 138 int _ = 6; > 139 END > 140 CASE(6) > 141 string grid_[] = { > 142 "//////////////////////////////////////////////////", > 143 "//////////////////////////////////////////////////", > 144 "//////////////////////////////////////////////////", > 145 "//////////////////////////////////////////////////", > 146 "//////////////////////////////////////////////////", > 147 "//////////////////////////////////////////////////", > 148 "//////////////////////////////////////////////////", > 149 "//////////////////////////////////////////////////", > 150 "//////////////////////////////////////////////////", > 151 "//////////////////////////////////////////////////", > 152 "//////////////////////////////////////////////////", > 153 "//////////////////////////////////////////////////", > 154 "//////////////////////////////////////////////////", > 155 "//////////////////////////////////////////////////", > 156 "//////////////////////////////////////////////////", > 157 "//////////////////////////////////////////////////", > 158 "//////////////////////////////////////////////////", > 159 "//////////////////////////////////////////////////", > 160 "//////////////////////////////////////////////////", > 161 "//////////////////////////////////////////////////", > 162 "//////////////////////////////////////////////////", > 163 "//////////////////////////////////////////////////", > 164 "//////////////////////////////////////////////////", > 165 "//////////////////////////////////////////////////", > 166 "//////////////////////////////////////////////////", > 167 "//////////////////////////////////////////////////", > 168 "//////////////////////////////////////////////////", > 169 "//////////////////////////////////////////////////", > 170 "//////////////////////////////////////////////////", > 171 "//////////////////////////////////////////////////", > 172 "//////////////////////////////////////////////////", > 173 "//////////////////////////////////////////////////", > 174 "//////////////////////////////////////////////////", > 175 "//////////////////////////////////////////////////", > 176 "//////////////////////////////////////////////////", > 177 "//////////////////////////////////////////////////", > 178 "//////////////////////////////////////////////////", > 179 "//////////////////////////////////////////////////", > 180 "//////////////////////////////////////////////////", > 181 "//////////////////////////////////////////////////", > 182 "//////////////////////////////////////////////////", > 183 "//////////////////////////////////////////////////", > 184 "//////////////////////////////////////////////////", > 185 "//////////////////////////////////////////////////", > 186 "//////////////////////////////////////////////////", > 187 "//////////////////////////////////////////////////", > 188 "//////////////////////////////////////////////////", > 189 "//////////////////////////////////////////////////", > 190 "//////////////////////////////////////////////////", > 191 "//////////////////////////////////////////////////", > 192 }; > 193 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 194 int unfoldLimit = 2500; > 195 int _ = 6; > 196 END > 197 > 198 } > 199 // END CUT HERE
Added SRM/444/1B.cpp version [2b2cc140266eef07]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int memo[1024][30]; > 23 > 24 class FourBlocks { public: > 25 int X, Y; > 26 vector<string> G; > 27 int maxScore(vector <string> grid) > 28 { > 29 X = grid.size(); > 30 Y = grid[0].size(); > 31 G = grid; > 32 > 33 // sentinels > 34 for(int x=0; x<X; ++x) > 35 G[x] += '0'; > 36 G.push_back( string(Y+1,'0') ); > 37 > 38 int baseScore = 0; > 39 for(int x=0; x<X; ++x) > 40 for(int y=0; y<Y; ++y) > 41 baseScore += (G[x][y]=='1'); > 42 > 43 // memoized search > 44 memset(memo, 0xff, sizeof(memo)); > 45 return baseScore + best(0); > 46 } > 47 > 48 int best(int y) > 49 { > 50 if( Y <= y ) > 51 return 0; > 52 > 53 int mask = 0; > 54 for(int x=0; x<X; ++x) > 55 mask |= ((G[x][y]=='.') << x); > 56 if( memo[mask][y] != -1 ) > 57 return memo[mask][y]; > 58 return memo[mask][y] = best(0, y); > 59 } > 60 > 61 int best(int x, int y) > 62 { > 63 if( X <= x ) > 64 return best(y+1); > 65 int Max = 0; > 66 if( G[x][y]=='.' ) > 67 { > 68 if( G[x+1][y]=='.' && G[x][y+1]=='.' && G[x+1][y+1]=='.' > 69 { > 70 // can put 4 > 71 G[x][y]=G[x+1][y]=G[x][y+1]=G[x+1][y+1] = '4'; > 72 Max = max(Max, 16+best(x+2, y)); > 73 G[x][y]=G[x+1][y]=G[x][y+1]=G[x+1][y+1] = '.'; > 74 } > 75 // can put 1 > 76 G[x][y] = '1'; > 77 Max = max(Max, 1+best(x+1, y)); > 78 G[x][y] = '.'; > 79 } > 80 else > 81 { > 82 Max = max(Max, best(x+1, y)); > 83 } > 84 return Max; > 85 } > 86 }; > 87 > 88 // BEGIN CUT HERE > 89 #include <ctime> > 90 double start_time; string timer() > 91 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 92 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 93 { os << "{ "; > 94 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 95 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 96 void verify_case(const int& Expected, const int& Received) { > 97 bool ok = (Expected == Received); > 98 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 99 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 100 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 101 #define END verify_case(_, FourBlocks().maxScore(grid));} > 102 int main(){ > 103 > 104 CASE(0) > 105 string grid_[] = {".....1..1..", > 106 "..1.....1.."}; > 107 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 108 int _ = 70; > 109 END > 110 CASE(1) > 111 string grid_[] = {"...1.", > 112 ".....", > 113 ".1..1", > 114 ".....", > 115 "1...."}; > 116 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 117 int _ = 73; > 118 END > 119 CASE(2) > 120 string grid_[] = {"...1.", > 121 ".1...", > 122 "..1.1", > 123 "1...."}; > 124 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 125 int _ = 20; > 126 END > 127 CASE(3) > 128 string grid_[] = {".....1...", > 129 ".....1...", > 130 "111111111", > 131 ".....1...", > 132 ".....1..."}; > 133 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 134 int _ = 117; > 135 END > 136 CASE(4) > 137 string grid_[] = {".........................", > 138 ".........................", > 139 ".........................", > 140 ".........................", > 141 ".........................", > 142 ".........................", > 143 ".........................", > 144 ".........................", > 145 ".........................", > 146 ".........................", > 147 }; > 148 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 149 int _ = 117; > 150 END > 151 CASE(5) > 152 string grid_[] = { > 153 "1...", "....", "..11", > 154 }; > 155 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 156 int _ = 36; > 157 END > 158 CASE(6) > 159 string grid_[] = { > 160 "1..", "...", "..1", "..1", > 161 }; > 162 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 163 int _ = 36; > 164 END > 165 > 166 } > 167 // END CUT HERE
Added SRM/444/1C.cpp version [f7d0e00a6c0eeef7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 typedef vector< vector<LL> > matrix; > 24 > 25 vector<LL> vMul( const matrix& A, const vector<LL>& B ) > 26 { > 27 const int n = A.size(); > 28 > 29 vector<LL> C(n); > 30 for(int i=0; i<n; ++i) > 31 for(int j=0; j<n; ++j) > 32 C[i] = (C[i] + A[i][j] * B[j]) % MODVAL; > 33 return C; > 34 } > 35 > 36 matrix mMul( const matrix& A, const matrix& B ) > 37 { > 38 const int n = A.size(); > 39 > 40 matrix C(n, vector<LL>(n)); > 41 for(int i=0; i<n; ++i) > 42 for(int j=0; j<n; ++j) { > 43 LL Cij = 0; > 44 for(int k=0; k<n; ++k) > 45 Cij = (Cij + A[i][k] * B[k][j]) % MODVAL; > 46 C[i][j] = Cij; > 47 } > 48 return C; > 49 } > 50 > 51 matrix mPow( matrix M, LL t ) // t>0 > 52 { > 53 matrix R; > 54 for(; t; t>>=1, M=mMul(M,M)) > 55 if( t&1 ) > 56 R = (R.empty() ? M : mMul(R,M)); > 57 return R; > 58 } > 59 > 60 LL GCD(LL a, LL b) { while(a) swap(b%=a,a); return b; } > 61 LL LCM(LL a, LL b) { return a/GCD(a,b)*b; } > 62 > 63 class AvoidFour { public: > 64 int count(long long n) > 65 { > 66 matrix A(6, vector<LL>(6)); > 67 A[1][0] = 8; A[1][1] = 9; A[1][2] = 9; A[1][3] = 9; A[1][4] = 9; > 68 A[2][0] = 1; A[2][1] = 1; > 69 A[3][2] = 1; > 70 A[4][3] = 1; > 71 A[5][1] = 1; A[5][2] = 1; A[5][3] = 1; A[5][4] = 1; A[5][5] = 1 > 72 > 73 vector<LL> v(6); > 74 v[0] = 1; > 75 > 76 > 77 vector<LL> fours; > 78 for(LL f=44; f<=n; f=f*10+4) > 79 fours.push_back(f); > 80 int N = fours.size(); > 81 > 82 LL total = 0; > 83 for(int m=0; m<(1<<N); ++m) > 84 { > 85 LL lcm=1, bitCnt=0; > 86 for(int i=0; (1<<i)<=m; ++i) > 87 if( m & (1<<i) ) { > 88 lcm = LCM(lcm, fours[i]); > 89 if( lcm > n ) > 90 break; > 91 bitCnt ++; > 92 } > 93 if( lcm <= n ) { > 94 matrix Af = mPow(A, lcm); > 95 Af[5] = A[5]; > 96 > 97 LL sub = vMul(mMul(A,mPow(Af,n/lcm)), v)[5]; > 98 total = (bitCnt&1 ? total-sub+MODVAL : total+sub) % MODVAL; > 99 } > 100 } > 101 return total; > 102 } > 103 }; > 104 > 105 // BEGIN CUT HERE > 106 #include <ctime> > 107 double start_time; string timer() > 108 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 109 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 110 { os << "{ "; > 111 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 112 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 113 void verify_case(const int& Expected, const int& Received) { > 114 bool ok = (Expected == Received); > 115 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 116 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 117 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 118 #define END verify_case(_, AvoidFour().count(n));} > 119 int main(){ > 120 > 121 CASE(0) > 122 long long n = 4LL; > 123 int _ = 9998; > 124 END > 125 CASE(1) > 126 long long n = 5LL; > 127 int _ = 99980; > 128 END > 129 CASE(2) > 130 long long n = 87LL; > 131 int _ = 576334228; > 132 END > 133 CASE(3) > 134 long long n = 88LL; > 135 int _ = 576334228; > 136 END > 137 CASE(4) > 138 long long n = 4128LL; > 139 int _ = 547731225; > 140 END > 141 > 142 } > 143 // END CUT HERE
Added SRM/445/1A.cpp version [fc75099a804bb837]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheNewHouseDivOne { public: > 23 double distance(vector <int> x, vector <int> y, int k) > 24 { > 25 double minD = 1e+99; > 26 for(double X=-50; X<=+50; X+=0.5) > 27 for(double Y=-50; Y<=+50; Y+=0.5) > 28 { > 29 vector<double> d; > 30 for(int i=0; i<x.size(); ++i) > 31 d.push_back( abs(x[i]-X) + abs(y[i]-Y) ) > 32 nth_element(d.begin(), d.begin()+k-1, d.end()); > 33 minD = min(minD, d[k-1]); > 34 } > 35 return minD; > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const double& Expected, const double& Received) { > 48 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, TheNewHouseDivOne().distance(x, y, k));} > 53 int main(){ > 54 > 55 CASE(0) > 56 int x_[] = {-1, -1, 1, 1}; > 57 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 58 int y_[] = {-1, 1, -1, 1}; > 59 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 60 int k = 3; > 61 double _ = 2.0; > 62 END > 63 CASE(1) > 64 int x_[] = {-1, -1, 1, 1}; > 65 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 66 int y_[] = {-1, 1, -1, 1}; > 67 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 68 int k = 2; > 69 double _ = 1.0; > 70 END > 71 CASE(2) > 72 int x_[] = {4, 4, 4, 4, 4, 3, 3, 5, 5}; > 73 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 74 int y_[] = {7, 7, 7, 4, 4, 5, 6, 5, 6}; > 75 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 76 int k = 9; > 77 double _ = 1.5; > 78 END > 79 CASE(3) > 80 int x_[] = {30, -15, 24, -23, 43, -8, -6, -47, 23, -11, 43, 6, -18, 44, > 81 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 82 int y_[] = {-45, -7, -33, 31, -8, -33, -20, -14, -50, -48, -31, 35, -24, > 83 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 84 int k = 13; > 85 double _ = 32.0; > 86 END > 87 > 88 } > 89 // END CUT HERE
Added SRM/445/1B.cpp version [299e5c03487b074c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheLockDivOne { public: > 23 string password(int n, long long k) > 24 { > 25 --k; > 26 return lexlast(n-1, k); > 27 } > 28 > 29 string kth(int i, LL k) > 30 { > 31 if( i < 0 ) > 32 return ""; > 33 > 34 if( k & (1LL<<i) ) > 35 return "1" + kth(i-1, k==(1LL<<i) ? (1LL<<i)-1 : k-(1LL< > 36 else > 37 return "0" + kth(i-1, k); > 38 } > 39 > 40 string lexlast(int i, LL k) > 41 { > 42 if( i < 0 ) > 43 return ""; > 44 > 45 if( k & (1LL<<i) ) { > 46 string a = kth(i-1, (1LL<<i)-1); > 47 if( k == (1LL<<i) ) > 48 return "1" + a; > 49 string b = lexlast(i-1, k-(1LL<<i)-1); > 50 return "1" + max(a,b); > 51 } > 52 else > 53 return "0" + lexlast(i-1, k); > 54 } > 55 }; > 56 > 57 // BEGIN CUT HERE > 58 #include <ctime> > 59 double start_time; string timer() > 60 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 61 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 62 { os << "{ "; > 63 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 64 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 65 void verify_case(const string& Expected, const string& Received) { > 66 bool ok = (Expected == Received); > 67 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 68 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 69 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 70 #define END verify_case(_, TheLockDivOne().password(n, k));} > 71 int main(){ > 72 > 73 CASE(0) > 74 int n = 2; > 75 long long k = 4LL; > 76 string _ = "11"; > 77 END > 78 CASE(1) > 79 int n = 3; > 80 long long k = 8LL; > 81 string _ = "111"; > 82 END > 83 CASE(2) > 84 int n = 4; > 85 long long k = 6LL; > 86 string _ = "0110"; > 87 END > 88 CASE(3) > 89 int n = 10; > 90 long long k = 1LL; > 91 string _ = "0000000000"; > 92 END > 93 CASE(4) > 94 int n = 7; > 95 long long k = 100LL; > 96 string _ = "1111110"; > 97 END > 98 CASE(4) > 99 int n = 50; > 100 long long k = 0xffffffffffffLL; > 101 string _ = "1111110"; > 102 END > 103 > 104 } > 105 // END CUT HERE
Added SRM/445/1C.cpp version [b01c730886ab7aca]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 #include <cctype> > 19 using namespace std; > 20 typedef long long LL; > 21 typedef complex<double> CMP; > 22 > 23 static const LL MODVAL = 1234567891; > 24 > 25 class TheEncryptionDivOne { public: > 26 map<vector<int>, LL> memo; > 27 > 28 int count(string msg, string encMsg) > 29 { > 30 // accumulate unused characters > 31 char cs[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY > 32 set<char> ori(cs+0, cs+52), enc(cs+0, cs+52); > 33 for(int i=0; i<msg.size(); ++i) > 34 ori.erase(msg[i]), enc.erase(encMsg[i]); > 35 > 36 // obviously-invalid cases > 37 for(int i=0; i<msg.size(); ++i) > 38 if( toupper(msg[i]) == toupper(encMsg[i]) ) > 39 return 0; > 40 if( ori.size() != enc.size() ) > 41 return 0; > 42 > 43 // count > 44 vector<int> s(9); > 45 for(char c='a'; c<='z'; ++c) > 46 { > 47 int oc = ori.count(c)+ori.count(c+'A'-'a'); > 48 int ec = enc.count(c)+enc.count(c+'A'-'a'); > 49 s[oc*3+ec] ++; > 50 } > 51 return rec(s); > 52 } > 53 > 54 LL rec( const vector<int>& s ) > 55 { > 56 if( memo.count(s) ) > 57 return memo[s]; > 58 > 59 for(int a=1; a<=2; ++a) > 60 for(int b=0; b<=2; ++b) if( s[a*3+b] ) > 61 { > 62 LL val = 0; > 63 for(int c=0; c<=2; ++c) > 64 for(int d=1; d<=2; ++d) if( s[c*3+d] ) > 65 { > 66 vector<int> t = s; > 67 t[a*3+b] --; > 68 if( LL choices = t[c*3+d]*d ) > 69 { > 70 t[(a-1)*3+b] ++; > 71 t[c*3+d] --; > 72 t[c*3+(d-1)] ++; > 73 val += choices * rec(t); > 74 } > 75 } > 76 return memo[s] = val%MODVAL; > 77 } > 78 return 1; > 79 } > 80 }; > 81 > 82 // BEGIN CUT HERE > 83 #include <ctime> > 84 double start_time; string timer() > 85 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 86 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 87 { os << "{ "; > 88 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 89 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 90 void verify_case(const int& Expected, const int& Received) { > 91 bool ok = (Expected == Received); > 92 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 93 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 94 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 95 #define END verify_case(_, TheEncryptionDivOne().count(msg, encMsg));} > 96 int main(){ > 97 > 98 CASE(0) > 99 string msg = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX"; > 100 string encMsg = "cdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; > 101 int _ = 2; > 102 END > 103 CASE(1) > 104 string msg = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX"; > 105 string encMsg = "bcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY"; > 106 int _ = 1; > 107 END > 108 CASE(2) > 109 string msg = "topcoder"; > 110 string encMsg = "TOPCODER"; > 111 int _ = 0; > 112 END > 113 CASE(3) > 114 string msg = "thisisaveryhardproblem"; > 115 string encMsg = "nobodywillsolveittoday"; > 116 int _ = 0; > 117 END > 118 CASE(4) > 119 string msg = "a"; > 120 string encMsg = "W"; > 121 int _ = 710479617; > 122 END > 123 CASE(5) > 124 string msg = "aMgqyMOaWSyaAxOyvOaRAEajOOAWWAqqAAj"; > 125 string encMsg = "qKYEnKaqMgnqGWanlaqkGdquaaGMMGEEGGu"; > 126 int _ = 650838813; > 127 END > 128 > 129 } > 130 // END CUT HERE
Added SRM/447-U/1A.cpp version [a89ac69a75cbec94]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class KnightsTour { public: > 23 int visitedPositions(vector <string> board) > 24 { > 25 // sentinels > 26 for(int yy=0; yy<board.size(); ++yy) > 27 board[yy] = "**"+board[yy]+"**"; > 28 board.push_back( string(board[0].size(),'*') ); > 29 board.push_back( string(board[0].size(),'*') ); > 30 board.insert( board.begin(), string(board[0].size(),'*') ); > 31 board.insert( board.begin(), string(board[0].size(),'*') ); > 32 > 33 // starting point > 34 int y, x; > 35 for(int yy=0; yy<board.size(); ++yy) > 36 for(int xx=0; xx<board[yy].size(); ++xx) > 37 if(board[yy][xx]=='K') > 38 y=yy, x=xx; > 39 > 40 int n = 1; > 41 while( step(board,y,x) ) > 42 ++n; > 43 return n; > 44 } > 45 > 46 bool step(vector<string>& B, int& Y, int& X) > 47 { > 48 int dy[]={-2,-2,-1,-1,+1,+1,+2,+2}; > 49 int dx[]={-1,+1,-2,+2,-2,+2,-1,+1}; > 50 int minAn = 9999; > 51 for(int i=0; i<8; ++i) > 52 if( B[Y+dy[i]][X+dx[i]] == '.' ) > 53 minAn = min(minAn, an(B,Y+dy[i],X+dx[i])); > 54 if( minAn == 9999 ) > 55 return false; > 56 for(int i=0; i<8; ++i) > 57 if( B[Y+dy[i]][X+dx[i]] == '.' ) > 58 if( minAn == an(B,Y+dy[i],X+dx[i]) ) { > 59 Y+=dy[i]; > 60 X+=dx[i]; > 61 B[Y][X] = 'K'; > 62 break; > 63 } > 64 return true; > 65 } > 66 > 67 int an(vector<string>& B, int Y, int X) > 68 { > 69 int dy[]={-2,-2,-1,-1,+1,+1,+2,+2}; > 70 int dx[]={-1,+1,-2,+2,-2,+2,-1,+1}; > 71 int cnt = 0; > 72 for(int i=0; i<8; ++i) > 73 cnt += (B[Y+dy[i]][X+dx[i]] == '.'); > 74 return cnt; > 75 } > 76 }; > 77 > 78 // BEGIN CUT HERE > 79 #include <ctime> > 80 double start_time; string timer() > 81 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 82 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 83 { os << "{ "; > 84 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 85 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 86 void verify_case(const int& Expected, const int& Received) { > 87 bool ok = (Expected == Received); > 88 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 89 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 90 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 91 #define END verify_case(_, KnightsTour().visitedPositions(board));} > 92 int main(){ > 93 > 94 CASE(0) > 95 string board_[] = { > 96 "........" > 97 ,".*.*...." > 98 ,".*......" > 99 ,"..K...*." > 100 ,"*...*..." > 101 ,"...*...." > 102 ,"...*.*.." > 103 ,"........"}; > 104 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 105 int _ = 39; > 106 END > 107 CASE(1) > 108 string board_[] = { > 109 "K......." > 110 ,"........" > 111 ,"........" > 112 ,"........" > 113 ,"........" > 114 ,"........" > 115 ,"........" > 116 ,"........"}; > 117 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 118 int _ = 64; > 119 END > 120 CASE(2) > 121 string board_[] = { > 122 "********" > 123 ,"*******." > 124 ,"********" > 125 ,"**.***.*" > 126 ,"********" > 127 ,"***.*.**" > 128 ,"********" > 129 ,"****K***"}; > 130 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 131 int _ = 3; > 132 END > 133 CASE(3) > 134 string board_[] = { > 135 "*.*....*" > 136 ,".......*" > 137 ,"**...*.." > 138 ,"..***..." > 139 ,".**.*..." > 140 ,"..*.*..K" > 141 ,"..***.*." > 142 ,"**...*.."}; > 143 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 144 int _ = 17; > 145 END > 146 CASE(4) > 147 string board_[] = { > 148 "..*...*." > 149 ,"**.....*" > 150 ,"*..*...." > 151 ,"*..*...." > 152 ,".....*.." > 153 ,"....*..K" > 154 ,"**.*...*" > 155 ,"..**...."}; > 156 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 157 int _ = 27; > 158 END > 159 CASE(5) > 160 string board_[] = { > 161 "**.**" > 162 ,"K****" > 163 ,"**.**" > 164 ,"**.*." > 165 }; > 166 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 167 int _ = 3; > 168 END > 169 > 170 } > 171 // END CUT HERE
Added SRM/447-U/1B.cpp version [89bd12ca6419d95a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 > 20 typedef int vert; > 21 typedef vert edge; > 22 typedef vector<edge> edges; > 23 typedef vector<edges> graph; > 24 > 25 bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) > 26 { > 27 for(int i=0; i<G[v].size(); ++i) { > 28 vert u = G[v][i]; > 29 if( visited[u] ) continue; > 30 visited[u] = true; > 31 > 32 if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) > 33 { matchTo[v]=u, matchTo[u]=v; return true; } > 34 } > 35 return false; > 36 } > 37 > 38 static const int NV = 100; > 39 int biMatch( graph& G, int L ) // [0,L):left, [L,?):right > 40 { > 41 vector<vert> matchTo(G.size(), -1); > 42 int ans = 0; > 43 for(vert v=0; v<L; ++v) { > 44 bool visited[NV] = {}; > 45 if( augment(G, v, matchTo, visited) ) > 46 ++ans; > 47 } > 48 return ans; > 49 } > 50 > 51 class PeopleYouMayKnow { public: > 52 int maximalScore(vector <string> friends, int personA, int personB) > 53 { > 54 int removed = 0; > 55 > 56 // A-C-B : C must be removed > 57 int N = friends.size(); > 58 int A = personA, B = personB; > 59 for(int C=0; C<N; ++C) if(C!=A && C!=B) > 60 if( friends[A][C]=='Y' && friends[C][B]=='Y' ) { > 61 removed ++; > 62 for(int D=0; D<N; ++D) > 63 friends[C][D] = friends[D][C] = 'N'; > 64 } > 65 > 66 // A-A1, B1-B : A1-B1 must be disconnected > 67 vector<int> A1; > 68 for(int C=0; C<N; ++C) if(C!=A && C!=B) > 69 if( friends[A][C]=='Y' ) > 70 A1.push_back(C); > 71 vector<int> B1; > 72 for(int C=0; C<N; ++C) if(C!=A && C!=B) > 73 if( friends[C][B]=='Y' ) > 74 B1.push_back(C); > 75 > 76 // == bimatch > 77 graph G(A1.size()+B1.size()); > 78 for(int a=0; a<A1.size(); ++a) > 79 for(int b=0; b<B1.size(); ++b) > 80 if( friends[A1[a]][B1[b]]=='Y' ) > 81 G[a].push_back( A1.size()+b ); > 82 return removed + biMatch(G, A1.size()); > 83 } > 84 }; > 85 > 86 // BEGIN CUT HERE > 87 #include <ctime> > 88 double start_time; string timer() > 89 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 90 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 91 { os << "{ "; > 92 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 93 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 94 void verify_case(const int& Expected, const int& Received) { > 95 bool ok = (Expected == Received); > 96 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 97 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 98 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 99 #define END verify_case(_, PeopleYouMayKnow().maximalScore(friends, personA > 100 int main(){ > 101 > 102 CASE(0) > 103 string friends_[] = {"NN" > 104 ,"NN"}; > 105 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 106 int personA = 0; > 107 int personB = 1; > 108 int _ = 0; > 109 END > 110 CASE(1) > 111 string friends_[] = {"NYNN" > 112 ,"YNYN" > 113 ,"NYNY" > 114 ,"NNYN"}; > 115 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 116 int personA = 0; > 117 int personB = 3; > 118 int _ = 1; > 119 END > 120 CASE(2) > 121 string friends_[] = {"NYNYYYN" > 122 ,"YNYNNYY" > 123 ,"NYNNNNY" > 124 ,"YNNNNNN" > 125 ,"YNNNNYN" > 126 ,"YYNNYNY" > 127 ,"NYYNNYN"}; > 128 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 129 int personA = 2; > 130 int personB = 3; > 131 int _ = 1; > 132 END > 133 CASE(3) > 134 string friends_[] = {"NYYYYNNNN" > 135 ,"YNNNNYYYN" > 136 ,"YNNNNNNYN" > 137 ,"YNNNNNNYN" > 138 ,"YNNNNNNNY" > 139 ,"NYNNNNNNY" > 140 ,"NYNNNNNNY" > 141 ,"NYYYNNNNY" > 142 ,"NNNNYYYYN"}; > 143 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 144 int personA = 8; > 145 int personB = 0; > 146 int _ = 3; > 147 END > 148 > 149 } > 150 // END CUT HERE
Added SRM/447-U/1C-U.cpp version [1ff60a30e59ecd0b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CellScores { public: > 23 long long countScores(int N, int M, int K, int X0, int A, int B, int Y0, > 24 { > 25 vector<int> X(M+K,X0%N), Y(M+K,Y0%N); > 26 for(int i=1; i<M+K; ++i) > 27 X[i] = (X[i-1]*A+B)%N, > 28 Y[i] = (Y[i-1]*C+D)%N; > 29 > 30 vector< vector<bool> > black(N, vector<bool>(N, false)); > 31 for(int i=0; i<M; ++i) > 32 black[Y[i]][X[i]] = true; > 33 > 34 // how to compute them???? > 35 vector< vector<int> > score_b(N, vector<int>(N, 0)); // # of gd > 36 vector< vector<int> > score_t(N, vector<int>(N, 0)); // top line > 37 vector< vector<int> > score_l(N, vector<int>(N, 0)); // ... > 38 vector< vector<int> > score_r(N, vector<int>(N, 0)); > 39 > 40 // O(1) score computation > 41 vector< vector<int> > score(N, vector<int>(N, 0)); > 42 score[0][0] = score_t[0][0]; > 43 for(int x=1; x<N; ++x) > 44 score[0][x] = score[0][x-1] - score_r[0][x-x] + score_l[ > 45 for(int y=1; y<N; ++y) > 46 for(int x=0; x<N; ++x) > 47 score[y][x] = score[y-1][x] - score_b[y-1][x] + > 48 > 49 LL ss = 0; > 50 for(int i=M; i<M+K; ++i) > 51 ss += score[Y[i]][X[i]]; > 52 return ss; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const long long& Expected, const long long& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, CellScores().countScores(N, M, K, X0, A, B, Y0, > 70 int main(){ > 71 > 72 CASE(0) > 73 int N = 10; > 74 int M = 0; > 75 int K = 1; > 76 int X0 = 0; > 77 int A = 1; > 78 int B = 1; > 79 int Y0 = 0; > 80 int C = 1; > 81 int D = 1; > 82 long long _ = 100LL; > 83 END > 84 CASE(1) > 85 int N = 10; > 86 int M = 1; > 87 int K = 1; > 88 int X0 = 5; > 89 int A = 1; > 90 int B = 5; > 91 int Y0 = 5; > 92 int C = 1; > 93 int D = 5; > 94 long long _ = 75LL; > 95 END > 96 CASE(2) > 97 int N = 7; > 98 int M = 4; > 99 int K = 3; > 100 int X0 = 0; > 101 int A = 1; > 102 int B = 1; > 103 int Y0 = 0; > 104 int C = 1; > 105 int D = 1; > 106 long long _ = 194LL; > 107 END > 108 CASE(3) > 109 int N = 23; > 110 int M = 10; > 111 int K = 30; > 112 int X0 = 26; > 113 int A = 48; > 114 int B = 76; > 115 int Y0 = 231; > 116 int C = 463; > 117 int D = 707; > 118 long long _ = 8088LL; > 119 END > 120 CASE(4) > 121 int N = 211; > 122 int M = 30; > 123 int K = 12; > 124 int X0 = 3; > 125 int A = 35; > 126 int B = 82; > 127 int Y0 = 0; > 128 int C = 43; > 129 int D = 15; > 130 long long _ = 18196443LL; > 131 END > 132 CASE(5) > 133 int N = 3; > 134 int M = 0; > 135 int K = 100; > 136 int X0 = 0; > 137 int A = 0; > 138 int B = 0; > 139 int Y0 = 0; > 140 int C = 0; > 141 int D = 0; > 142 long long _ = 900LL; > 143 END > 144 > 145 } > 146 // END CUT HERE
Added SRM/450-U/1A.cpp version [41eb9ab0d33a154b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class OrderedNim { public: > 23 string winner(vector <int> layout) > 24 { > 25 bool firstWin = false; > 26 for(int i=layout.size()-1; i>=0; --i) > 27 if( layout[i] == 1 ) > 28 firstWin = !firstWin; > 29 else > 30 firstWin = true; > 31 return firstWin ? "Alice" : "Bob"; > 32 } > 33 }; > 34 > 35 // BEGIN CUT HERE > 36 #include <ctime> > 37 double start_time; string timer() > 38 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 39 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 40 { os << "{ "; > 41 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 42 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 43 void verify_case(const string& Expected, const string& Received) { > 44 bool ok = (Expected == Received); > 45 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 46 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 47 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 48 #define END verify_case(_, OrderedNim().winner(layout));} > 49 int main(){ > 50 > 51 CASE(0) > 52 int layout_[] = {5}; > 53 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 54 string _ = "Alice"; > 55 END > 56 CASE(1) > 57 int layout_[] = {1,2}; > 58 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 59 string _ = "Bob"; > 60 END > 61 CASE(2) > 62 int layout_[] = {2,1}; > 63 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 64 string _ = "Alice"; > 65 END > 66 CASE(3) > 67 int layout_[] = {10,9,8,7,6,5,4,3,2,1}; > 68 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 69 string _ = "Alice"; > 70 END > 71 CASE(4) > 72 int layout_[] = {1,1,1,1}; > 73 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 74 string _ = "Bob"; > 75 END > 76 CASE(5) > 77 int layout_[] = {1}; > 78 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 79 string _ = "Alice"; > 80 END > 81 CASE(6) > 82 int layout_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 > 83 vector <int> layout(layout_, layout_+sizeof(layout_)/sizeof(*layout_)) > 84 string _ = "Bob"; > 85 END > 86 > 87 } > 88 // END CUT HERE
Added SRM/450-U/1B.cpp version [076d8405b22e14c4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class StrongEconomy { public: > 23 > 24 long long earn(long long n, long long k, long long price, long long targ > 25 { > 26 if( target/n < k ) > 27 return 1; > 28 > 29 LL best = target; > 30 for(LL money=0, round=0; round+1<best;) > 31 { > 32 // ‚±‚±‚©‚牽‚à‚µ‚È‚©‚Á‚½ê‡‚̃‰ƒEƒ“ƒh” > 33 best = min(best, round + roundsNeededToEarn(target-money > 34 > 35 // (n,k) ‚ð‘‚₹‚é—l‚É‚È‚é‚Ü‚Å‘Ò‚Â > 36 if( money < price ) > 37 { > 38 LL rp = roundsNeededToEarn(price-money, n*k); > 39 round += rp; > 40 money += n*k*rp; > 41 } > 42 > 43 // (n,k) ‚ð‘‚₵‚Ä‚Ý‚é > 44 // - ‚à‚¤‘‚₳‚È‚¢•û‚ª‚¢‚¢‰Â”\«‚Íu‰½‚à‚µ‚È‚©‚Á‚½ê‡v‚Ål—¶Ï‚Ý > 45 // - ¡‘‚₳‚¸Œã‚Å‘‚â‚·•û‚ª‚¢‚¢A‚Æ‚¢‚¤‚±‚Æ‚Í‚ ‚蓾‚È‚¢ > 46 (n<k ? n : k)++; > 47 money -= price; > 48 } > 49 return best; > 50 } > 51 > 52 LL roundsNeededToEarn(LL t, LL v) > 53 { > 54 return (t-1)/v + 1; > 55 } > 56 }; > 57 > 58 // BEGIN CUT HERE > 59 #include <ctime> > 60 double start_time; string timer() > 61 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 62 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 63 { os << "{ "; > 64 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 65 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 66 void verify_case(const long long& Expected, const long long& Received) { > 67 bool ok = (Expected == Received); > 68 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 69 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 70 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 71 #define END verify_case(_, StrongEconomy().earn(n, k, price, target));} > 72 int main(){ > 73 > 74 CASE(0) > 75 long long n = 2LL; > 76 long long k = 1LL; > 77 long long price = 2LL; > 78 long long target = 10LL; > 79 long long _ = 4LL; > 80 END > 81 CASE(1) > 82 long long n = 2LL; > 83 long long k = 1LL; > 84 long long price = 2LL; > 85 long long target = 9LL; > 86 long long _ = 3LL; > 87 END > 88 CASE(2) > 89 long long n = 1LL; > 90 long long k = 1LL; > 91 long long price = 500000LL; > 92 long long target = 1000002LL; > 93 long long _ = 1000001LL; > 94 END > 95 CASE(3) > 96 long long n = 5LL; > 97 long long k = 4LL; > 98 long long price = 15LL; > 99 long long target = 100LL; > 100 long long _ = 5LL; > 101 END > 102 CASE(4) > 103 long long n = 1000000000000LL; > 104 long long k = 1000000000000LL; > 105 long long price = 1000000000000LL; > 106 long long target = 1000000000000LL; > 107 long long _ = 1LL; > 108 END > 109 CASE(5) > 110 long long n = 1LL; > 111 long long k = 1LL; > 112 long long price = 1000000000000LL; > 113 long long target = 1000000000000LL; > 114 long long _ = 1000000000000LL; > 115 END > 116 CASE(6) > 117 long long n = 1LL; > 118 long long k = 1LL; > 119 long long price = 1LL; > 120 long long target = 1000000000000LL; > 121 long long _ = 8LL; > 122 END > 123 > 124 } > 125 // END CUT HERE
Added SRM/450-U/1C-U.cpp version [d0bfb26f428b0c51]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RowGame { public: > 23 long long score(vector <int> board, int k) > 24 { > 25 int N = board.size(); > 26 vector<LL> best(N, -1); > 27 best[0] = 0; > 28 > 29 LL bestOfBest = 0; > 30 LL dif = -1, loopCnt = 0; > 31 > 32 int dir=+1; > 33 for(int ki=0; ki<k; ++ki, dir=-dir) > 34 { > 35 vector<LL> best2(N, -1); > 36 for(int s=0; s<N; ++s) > 37 if( best[s] >= 0 ) > 38 { > 39 LL sum = 0; > 40 for(int t=s; 0<=t && t<N; t+=dir) { > 41 sum += board[t]; > 42 if( best[s]+sum >= 0 ) > 43 best2[t] = max(best2[t], best[s] > 44 } > 45 } > 46 best.swap(best2); > 47 LL bb2 = max(bestOfBest, *max_element(best.begin(), best > 48 { > 49 if( bb2-bestOfBest == dif ) > 50 ++loopCnt; > 51 else { > 52 loopCnt = 0; > 53 dif = bb2-bestOfBest; > 54 } > 55 if( loopCnt >= 2501 ) > 56 return bestOfBest + dif*(k-ki); > 57 } > 58 bestOfBest = bb2; > 59 } > 60 return bestOfBest; > 61 } > 62 }; > 63 > 64 // BEGIN CUT HERE > 65 #include <ctime> > 66 double start_time; string timer() > 67 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 68 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 69 { os << "{ "; > 70 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 71 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 72 void verify_case(const long long& Expected, const long long& Received) { > 73 bool ok = (Expected == Received); > 74 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 75 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 76 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 77 #define END verify_case(_, RowGame().score(board, k));} > 78 int main(){ > 79 > 80 CASE(0) > 81 int board_[] = {2,-2,4,3,-10} ; > 82 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 83 int k = 3; > 84 long long _ = 21LL; > 85 END > 86 CASE(1) > 87 int board_[] = {-6,5}; > 88 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 89 int k = 10; > 90 long long _ = 0LL; > 91 END > 92 CASE(2) > 93 int board_[] = {5,-6}; > 94 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 95 int k = 10; > 96 long long _ = 50LL; > 97 END > 98 CASE(3) > 99 int board_[] = {10,-100,80}; > 100 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 101 int k = 3; > 102 long long _ = 30LL; > 103 END > 104 CASE(4) > 105 int board_[] = {10,-100,80}; > 106 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 107 int k = 4; > 108 long long _ = 90LL; > 109 END > 110 CASE(5) > 111 int board_[] = {-100,1,2,3,4,5,6,7,8,9,10,11,12,13,14}; > 112 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 113 int k = 400000000; > 114 long long _ = 41999999900LL; > 115 END > 116 /* > 117 CASE(6) > 118 int board_[] = ; > 119 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 120 int k = ; > 121 long long _ = LL; > 122 END > 123 CASE(7) > 124 int board_[] = ; > 125 vector <int> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 126 int k = ; > 127 long long _ = LL; > 128 END > 129 */ > 130 } > 131 // END CUT HERE
Added SRM/451-U/1A.cpp version [5ddd5533b318ee93]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class MagicalSource { public: > 23 long long calculate(long long x) > 24 { > 25 LL d[] = { > 26 1LL, > 27 11LL, > 28 111LL, > 29 1111LL, > 30 11111LL, > 31 111111LL, > 32 1111111LL, > 33 11111111LL, > 34 111111111LL, > 35 1111111111LL, > 36 11111111111LL, > 37 111111111111LL, > 38 1111111111111LL, > 39 }; > 40 int N = sizeof(d) / sizeof(*d); > 41 assert( d[N-1] > 1000000000000LL ); > 42 for(int i=N-1; i>=0; --i) > 43 if( x%d[i] == 0 ) > 44 return x/d[i]; > 45 return x; // unreachable > 46 } > 47 }; > 48 > 49 // BEGIN CUT HERE > 50 #include <ctime> > 51 double start_time; string timer() > 52 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 53 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 54 { os << "{ "; > 55 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 56 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 57 void verify_case(const long long& Expected, const long long& Received) { > 58 bool ok = (Expected == Received); > 59 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 60 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 61 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 62 #define END verify_case(_, MagicalSource().calculate(x));} > 63 int main(){ > 64 > 65 CASE(0) > 66 long long x = 1371110974LL; > 67 long long _ = 1234LL; > 68 END > 69 CASE(1) > 70 long long x = 111111LL; > 71 long long _ = 1LL; > 72 END > 73 CASE(2) > 74 long long x = 10989LL; > 75 long long _ = 99LL; > 76 END > 77 CASE(3) > 78 long long x = 120LL; > 79 long long _ = 120LL; > 80 END > 81 CASE(4) > 82 long long x = 109999999989LL; > 83 long long _ = 99LL; > 84 END > 85 CASE(5) > 86 long long x = 1LL; > 87 long long _ = 1LL; > 88 END > 89 CASE(6) > 90 long long x = 1000000000000LL; > 91 long long _ = 1000000000000LL; > 92 END > 93 CASE(7) > 94 long long x = 111111111111LL; > 95 long long _ = 1LL; > 96 END > 97 CASE(8) > 98 long long x = 123456654321LL; > 99 long long _ = 111111LL; > 100 END > 101 CASE(9) > 102 long long x = 12222221LL; > 103 long long _ = 11LL; > 104 END > 105 > 106 } > 107 // END CUT HERE
Added SRM/451-U/1B.cpp version [179a24bc8356088b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BaronsAndCoins { public: > 23 int getMaximum(vector <int> x, vector <int> y) > 24 { > 25 vector< pair<int,int> > P; > 26 for(int i=0; i<x.size(); ++i) > 27 P.push_back( make_pair(y[i], x[i]) ); > 28 P.push_back( make_pair(0, 0) ); > 29 sort( P.begin(), P.end() ); > 30 > 31 return maxDepth(0, 0, P); > 32 } > 33 > 34 map<pair<int,int>,int> memo; > 35 int maxDepth( int i, int k, vector< pair<int,int> >& P ) > 36 { > 37 pair<int,int> key(i,k); > 38 if( memo.count(key) ) > 39 return memo[key]; > 40 > 41 int md = 0; > 42 for(int j=i+1; j<P.size(); ++j) > 43 { > 44 int lk = leastReachK( P[j].first-P[i].first, P[j].second > 45 if( lk > 0 ) > 46 md = max(md, 1 + maxDepth(j, lk, P)); > 47 } > 48 return memo[key] = md; > 49 } > 50 > 51 int leastReachK( int dy, int dx, int k ) > 52 { > 53 if( dy <= 0 || dx <= 0 ) > 54 return -1; > 55 > 56 int minX = ((k+1)+(k+dy))*dy / 2; > 57 if( dx < minX ) > 58 return -1; > 59 > 60 int curK = k+dy; > 61 int rest = dx - minX; > 62 while( rest ) > 63 { > 64 curK += rest / dy; > 65 rest %= dy; > 66 --dy; > 67 } > 68 return curK; > 69 } > 70 }; > 71 > 72 // BEGIN CUT HERE > 73 #include <ctime> > 74 double start_time; string timer() > 75 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 76 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 77 { os << "{ "; > 78 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 79 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 80 void verify_case(const int& Expected, const int& Received) { > 81 bool ok = (Expected == Received); > 82 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 83 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 84 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 85 #define END verify_case(_, BaronsAndCoins().getMaximum(x, y));} > 86 int main(){ > 87 > 88 CASE(0) > 89 int x_[] = {15, 5, 30}; > 90 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 91 int y_[] = {4, 5, 6}; > 92 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 93 int _ = 2; > 94 END > 95 CASE(1) > 96 int x_[] = {10}; > 97 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 98 int y_[] = {10}; > 99 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 100 int _ = 0; > 101 END > 102 CASE(2) > 103 int x_[] = {1, 3, 6, 10, 15, 21}; > 104 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 105 int y_[] = {1, 2, 3, 4, 5, 6}; > 106 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 107 int _ = 6; > 108 END > 109 CASE(3) > 110 int x_[] = {5, 10, 15, 20, 25, 30, 35, 40, 45}; > 111 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 112 int y_[] = {1, 1, 1, 2, 2, 2, 3, 3, 3}; > 113 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 114 int _ = 3; > 115 END > 116 CASE(4) > 117 int x_[] = {1, 3, 6, 10, 22, 35, 50, 66}; > 118 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 119 int y_[] = {1, 2, 3, 1, 2, 3, 4, 5}; > 120 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 121 int _ = 5; > 122 END > 123 CASE(5) > 124 int x_[] = {10}; > 125 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 126 int y_[] = {4}; > 127 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 128 int _ = 1; > 129 END > 130 CASE(6) > 131 int x_[] = {9}; > 132 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 133 int y_[] = {4}; > 134 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 135 int _ = 0; > 136 END > 137 CASE(7) > 138 int x_[] = {11}; > 139 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 140 int y_[] = {4}; > 141 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 142 int _ = 1; > 143 END > 144 CASE(8) > 145 int x_[] = {1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190, > 146 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 147 int y_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 148 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 149 int _ = 50; > 150 END > 151 CASE(9) > 152 int x_[] = {1,30,60,100,150,210,280,360,450,550,660,780,910,1050,1200,13 > 153 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 154 int y_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 155 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 156 int _ = 44; > 157 END > 158 CASE(10) > 159 int x_[] = {999,3,15}; > 160 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 161 int y_[] = {1,2,3}; > 162 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 163 int _ = 2; > 164 END > 165 CASE(11) > 166 int x_[] = {4,12,18}; > 167 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 168 int y_[] = {1,3,4}; > 169 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 170 int _ = 2; > 171 END > 172 > 173 } > 174 // END CUT HERE
Added SRM/452/1A.cpp version [a26c173c3d2bd17e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NotTwo { public: > 23 int maxStones(int width, int height) > 24 { > 25 return ms( (width+1)/2, (height+1)/2 ) > 26 + ms( (width+1)/2, height/2 ) > 27 + ms( width/2, (height+1)/2 ) > 28 + ms( width/2, height/2 ); > 29 } > 30 int ms( int w, int h ) > 31 { > 32 return ((w+1)/2) * ((h+1)/2) + (w/2)*(h/2); > 33 } > 34 }; > 35 > 36 // BEGIN CUT HERE > 37 #include <ctime> > 38 double start_time; string timer() > 39 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 40 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 41 { os << "{ "; > 42 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 43 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 44 void verify_case(const int& Expected, const int& Received) { > 45 bool ok = (Expected == Received); > 46 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 47 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 48 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 49 #define END verify_case(_, NotTwo().maxStones(width, height));} > 50 int main(){ > 51 > 52 CASE(0) > 53 int width = 3; > 54 int height = 2; > 55 int _ = 4; > 56 END > 57 CASE(1) > 58 int width = 3; > 59 int height = 3; > 60 int _ = 5; > 61 END > 62 CASE(2) > 63 int width = 8; > 64 int height = 5; > 65 int _ = 20; > 66 END > 67 CASE(3) > 68 int width = 1; > 69 int height = 1; > 70 int _ = 1; > 71 END > 72 CASE(4) > 73 int width = 1; > 74 int height = 2; > 75 int _ = 2; > 76 END > 77 CASE(5) > 78 int width = 1; > 79 int height = 3; > 80 int _ = 2; > 81 END > 82 > 83 } > 84 // END CUT HERE
Added SRM/452/1B.cpp version [aa7e1a1a6f23a9b6]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL MODVAL = 1000000007LL; > 23 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 24 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 25 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 26 LL POW(LL x, LL e) { > 27 LL v = 1; > 28 for(;e;x=MUL(x,x),e>>=1) > 29 if(e&1) > 30 v = MUL(v, x); > 31 return v; > 32 } > 33 > 34 class IOIString { public: > 35 string s; > 36 LL nI, nQ; > 37 > 38 int countIOIs(vector <string> mask) > 39 { > 40 s = accumulate( mask.begin(), mask.end(), string("") ); > 41 nI = count( s.begin(), s.end(), 'I' ); > 42 nQ = count( s.begin(), s.end(), '?' ); > 43 > 44 return SUB( POW(2,nQ), non_IOI() ); > 45 } > 46 > 47 LL non_IOI() > 48 { > 49 LL ans = 0; > 50 if( nI == 0 ) ans = ADD(ans, 1); // #I == 0 > 51 if( nI == 0 ) ans = ADD(ans, nQ); // #I == 1 > 52 if( nI == 1 ) ans = ADD(ans, 1); // #I == 1 > 53 for(int D=1; D<s.size(); D+=2) > 54 for(int S=0; S<D; ++S) > 55 ans = ADD(ans, non_IOI_2(S, D)); // #I >= 2 > 56 return ans; > 57 } > 58 > 59 LL non_IOI_2( int S, int D ) > 60 { > 61 string t; > 62 for(int i=S; i<s.size(); i+=D) > 63 t += s[i]; > 64 > 65 // t ˆÈŠO‚Ì•”•ª‚Í‘S•” 'O' ‚Å‚È‚¢‚ƃ_ƒ > 66 if( count(t.begin(), t.end(), 'I') < nI ) > 67 return 0; > 68 > 69 // t ‚Í /O*II+O*/ ‚Ƀ}ƒbƒ`‚µ‚È‚¢‚ƃ_ƒB4ó‘Ԃ̃I[ƒgƒ}ƒgƒ“‚ÅDP > 70 LL q0=1, q1=0, q2=0, q3=0; > 71 for(int i=0; i<t.size(); ++i) > 72 { > 73 LL p0 = (t[i]=='O' ? q0 : t[i]=='I' ? 0 > 74 LL p1 = (t[i]=='O' ? 0 : t[i]=='I' ? q0 > 75 LL p2 = (t[i]=='O' ? 0 : t[i]=='I' ? ADD(q1,q2) > 76 LL p3 = (t[i]=='O' ? ADD(q2,q3) : t[i]=='I' ? 0 > 77 q0=p0, q1=p1, q2=p2, q3=p3; > 78 } > 79 return ADD(q2, q3); > 80 } > 81 }; > 82 > 83 // BEGIN CUT HERE > 84 #include <ctime> > 85 double start_time; string timer() > 86 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 87 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 88 { os << "{ "; > 89 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 90 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 91 void verify_case(const int& Expected, const int& Received) { > 92 bool ok = (Expected == Received); > 93 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 94 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 95 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 96 #define END verify_case(_, IOIString().countIOIs(mask));} > 97 int main(){ > 98 > 99 CASE(0) > 100 string mask_[] = {"IO?"}; > 101 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 102 int _ = 1; > 103 END > 104 CASE(1) > 105 string mask_[] = {"????"}; > 106 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 107 int _ = 4; > 108 END > 109 CASE(2) > 110 string mask_[] = {"?II"}; > 111 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 112 int _ = 0; > 113 END > 114 CASE(3) > 115 string mask_[] = {"I??O??I"}; > 116 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 117 int _ = 16; > 118 END > 119 CASE(4) > 120 string mask_[] = {"???I???????O???","???????????O??IO????????I???"}; > 121 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 122 int _ = 438952513; > 123 END > 124 CASE(5) > 125 string mask_[] = {"?"}; > 126 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 127 int _ = 0; > 128 END > 129 CASE(6) > 130 string mask_[] = { > 131 "??????????????????????????????????????????????????", > 132 "??????????????????????????????????????????????????", > 133 "??????????????????????????????????????????????????", > 134 "??????????????????????????????????????????????????", > 135 "??????????????????????????????????????????????????", > 136 "??????????????????????????????????????????????????", > 137 "??????????????????????????????????????????????????", > 138 "??????????????????????????????????????????????????", > 139 "??????????????????????????????????????????????????", > 140 "??????????????????????????????????????????????????", > 141 "??????????????????????????????????????????????????", > 142 "??????????????????????????????????????????????????", > 143 "??????????????????????????????????????????????????", > 144 "??????????????????????????????????????????????????", > 145 "??????????????????????????????????????????????????", > 146 "??????????????????????????????????????????????????", > 147 "??????????????????????????????????????????????????", > 148 "??????????????????????????????????????????????????", > 149 "??????????????????????????????????????????????????", > 150 "??????????????????????????????????????????????????", > 151 "??????????????????????????????????????????????????", > 152 "??????????????????????????????????????????????????", > 153 "??????????????????????????????????????????????????", > 154 "??????????????????????????????????????????????????", > 155 "??????????????????????????????????????????????????", > 156 "??????????????????????????????????????????????????", > 157 "??????????????????????????????????????????????????", > 158 "??????????????????????????????????????????????????", > 159 "??????????????????????????????????????????????????", > 160 "??????????????????????????????????????????????????", > 161 "??????????????????????????????????????????????????", > 162 "??????????????????????????????????????????????????", > 163 "??????????????????????????????????????????????????", > 164 "??????????????????????????????????????????????????", > 165 "??????????????????????????????????????????????????", > 166 "??????????????????????????????????????????????????", > 167 "??????????????????????????????????????????????????", > 168 "??????????????????????????????????????????????????", > 169 "??????????????????????????????????????????????????", > 170 "??????????????????????????????????????????????????", > 171 "??????????????????????????????????????????????????", > 172 "??????????????????????????????????????????????????", > 173 "??????????????????????????????????????????????????", > 174 "??????????????????????????????????????????????????", > 175 "??????????????????????????????????????????????????", > 176 "??????????????????????????????????????????????????", > 177 "??????????????????????????????????????????????????", > 178 "??????????????????????????????????????????????????", > 179 "??????????????????????????????????????????????????", > 180 "??????????????????????????????????????????????????", > 181 }; > 182 vector <string> mask(mask_, mask_+sizeof(mask_)/sizeof(*mask_)); > 183 int _ = 967087276; > 184 END > 185 } > 186 // END CUT HERE
Added SRM/452/1C.cpp version [c255d7f6a0dc7167]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 // mod ‰‰ŽZ—pƒ†[ƒeƒBƒŠƒeƒB > 23 LL MODVAL = 1000000007LL; > 24 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 25 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 26 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 27 LL POW(LL x, LL e) { > 28 LL v = 1; > 29 for(;e;x=MUL(x,x),e>>=1) > 30 if(e&1) > 31 v = MUL(v, x); > 32 return v; > 33 } > 34 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 35 LL C(LL n, LL k) { > 36 LL v = 1; > 37 k = min(k, n-k); > 38 for(LL i=1; i<=k; ++i) > 39 v = DIV(MUL(v, n-i+1), i); > 40 return v; > 41 } > 42 > 43 // –{‘Ì > 44 class IncreasingNumber { public: > 45 int countNumbers(long long digits, int divisor) > 46 { > 47 LL N = digits; > 48 int D = divisor; > 49 > 50 // ‚¿‚傤‚Ç N Œ…‚Ì Increasing number > 51 // > 52 // 0<=ƒ°a<=8 ‚È a[] ‚ðŽg‚Á‚Ä > 53 // 1*a[0] + 11*a[1] + ... + 111..111*(a[N-1]+1) > 54 // ‚ÌŒ`‚Å‘‚¯‚é” > 55 // > 56 //‚È‚Ì‚Å > 57 // > 58 // D ‚ÅŠ„‚ê‚é‚¿‚傤‚Ç N Œ…‚Ì Increasing number ‚̌” > 59 // > 60 // 0<=ƒ°a<=8 ‚È a[] ‚Å > 61 // coeff[0]*a[0] + coeff[1]*a[1] + ... + coeff[N-1]*(a[N-1]+1) = 0 > 62 // ‚È‚à‚̂̌” ( where coeff[i] := "1‚ªi+1ŒÂ•À‚ñ‚¾‚à‚Ì" % D ) > 63 > 64 > 65 // ƒXƒe[ƒW‚P > 66 > 67 vector<LL> nc(D); // nc[m] = (coeff[i]%D==m ‚È i ‚Ì”) > 68 int ceLast; // coeff[N-1] > 69 { > 70 // coeff ‚Í‚X D ‰ñ‚Ń‹[ƒv‚µ‚Ä‚é‚Í‚¸ > 71 vector<int> coeff; > 72 int loop_start = -1; > 73 for(int v=1%D ;;) > 74 { > 75 loop_start = find(coeff.begin(), coeff.end(), v) - coeff.begin(); > 76 if( loop_start != coeff.size() ) > 77 break; > 78 coeff.push_back( v ); > 79 v = (v*10 + 1) % D; > 80 } > 81 int loop_len = coeff.size() - loop_start; > 82 > 83 for(int i=0; i<coeff.size() && i<N; ++i) > 84 if( i < loop_start ) > 85 nc[coeff[i]] = 1; > 86 else > 87 nc[coeff[i]] = (N-i+loop_len-1)/loop_len % MODVAL; > 88 ceLast = N-1 < loop_start ? coeff[N-1] > 89 : coeff[(N-1-loop_start)%loop_len+loop_start] > 90 } > 91 > 92 > 93 // ƒXƒe[ƒW1.5 > 94 > 95 vector< vector<LL> > choice(D, vector<LL>(9)); > 96 for(int m=0; m<D; ++m) > 97 for(int n=0; n<=8; ++n) > 98 // nc[m] Ží—Þ‚Ì‚à‚Ì‚©‚ç n ŒÂŽæ‚éŽæ‚è•û > 99 choice[m][n] = (n==0 ? 1 : nc[m]==0 ? 0 : C(n+nc[m]-1, nc[m]-1)); > 100 > 101 > 102 // ƒXƒe[ƒW‚Q > 103 // dp[m][n][q] = —]‚è [0, 1, ... m] ŒW”‚©‚ç n ŒÂŽg‚Á‚Ä‘˜a‚ð—]‚è q ‚É‚·‚é‚â‚è•û‚͉½’Ê‚èH > 104 > 105 vector< vector< vector<LL> > > dp(D, vector< vector<LL> >(9, vector<LL>(D) > 106 for(int m=0; m<D; ++m) > 107 dp[m][0][ceLast] = 1; > 108 for(int n=1; n<=8; ++n) > 109 dp[0][n][ceLast] = choice[0][n]; > 110 for(int m=1; m<D; ++m) > 111 for(int n=1; n<=8; ++n) > 112 for(int q=0; q<D; ++q) > 113 for(int k=0,q2=q; k<=n; ++k,q2=(q2-m+D)%D) > 114 dp[m][n][q] = ADD( dp[m][n][q], MUL( dp[m-1][n-k][q2], choice[ > 115 > 116 LL ans = 0; > 117 for(int n=0; n<=8; ++n) > 118 ans = ADD(ans, dp[D-1][n][0]); > 119 return ans; > 120 } > 121 }; > 122 > 123 // BEGIN CUT HERE > 124 #include <ctime> > 125 double start_time; string timer() > 126 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 127 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 128 { os << "{ "; > 129 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 130 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 131 void verify_case(const int& Expected, const int& Received) { > 132 bool ok = (Expected == Received); > 133 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 134 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 135 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 136 #define END verify_case(_, IncreasingNumber().countNumbers(digits, divisor)); > 137 int main(){ > 138 CASE(-2) > 139 long long digits = 1LL; > 140 int divisor = 16; > 141 int _ = 0; > 142 END > 143 CASE(-1) > 144 long long digits = 1LL; > 145 int divisor = 1; > 146 int _ = 9; > 147 END > 148 CASE(0) > 149 long long digits = 2LL; > 150 int divisor = 12; > 151 int _ = 4; > 152 END > 153 CASE(1) > 154 long long digits = 3LL; > 155 int divisor = 111; > 156 int _ = 9; > 157 END > 158 CASE(2) > 159 long long digits = 452LL; > 160 int divisor = 10; > 161 int _ = 0; > 162 END > 163 CASE(3) > 164 long long digits = 6LL; > 165 int divisor = 58; > 166 int _ = 38; > 167 END > 168 CASE(4) > 169 long long digits = 1000000000000000000LL; > 170 int divisor = 500; > 171 int _ = 0; > 172 END > 173 CASE(5) > 174 long long digits = 26542766498659LL; > 175 int divisor = 25; > 176 int _ = 766312864; > 177 END > 178 } > 179 // END CUT HERE
Added SRM/453.5/1A.cpp version [264b779c9b54203f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class MazeMaker { public: > 23 int longestPath(vector <string> maze, int startRow, int startCol, vector > 24 { > 25 const int H = maze.size(); > 26 const int W = maze[0].size(); > 27 > 28 typedef pair<int,int> point; > 29 > 30 int cell = 0; > 31 for(int y=0; y<H; ++y) > 32 for(int x=0; x<W; ++x) > 33 if( maze[y][x] == '.' ) > 34 ++cell; > 35 > 36 vector<point> q(1, point(startRow, startCol)); > 37 set<point> visited; visited.insert(q[0]); > 38 > 39 for(int step=0 ;; ++step) > 40 { > 41 vector<point> q2; > 42 for(int i=0; i<q.size(); ++i) > 43 for(int j=0; j<moveRow.size(); ++j) > 44 { > 45 int yy = q[i].first+moveRow[j]; > 46 int xx = q[i].second+moveCol[j]; > 47 point pp( yy, xx ); > 48 if( visited.count(pp) || yy<0 || H<=yy | > 49 continue; > 50 visited.insert(pp); > 51 q2.push_back(pp); > 52 } > 53 q.swap(q2); > 54 > 55 if( q.empty() ) > 56 return (visited.size()==cell ? step : -1); > 57 } > 58 assert(false); > 59 } > 60 }; > 61 > 62 // BEGIN CUT HERE > 63 #include <ctime> > 64 double start_time; string timer() > 65 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 66 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 67 { os << "{ "; > 68 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 69 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 70 void verify_case(const int& Expected, const int& Received) { > 71 bool ok = (Expected == Received); > 72 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 73 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 74 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 75 #define END verify_case(_, MazeMaker().longestPath(maze, startRow, startCol > 76 int main(){ > 77 > 78 CASE(0) > 79 string maze_[] = {"...", > 80 "...", > 81 "..."}; > 82 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 83 int startRow = 0; > 84 int startCol = 1; > 85 int moveRow_[] = {1,0,-1,0}; > 86 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 87 int moveCol_[] = {0,1,0,-1}; > 88 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 89 int _ = 3; > 90 END > 91 CASE(1) > 92 string maze_[] = {"...", > 93 "...", > 94 "..."}; > 95 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 96 int startRow = 0; > 97 int startCol = 1; > 98 int moveRow_[] = {1, 0, -1, 0, 1, 1, -1, -1}; > 99 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 100 int moveCol_[] = {0, 1, 0, -1, 1, -1, 1, -1}; > 101 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 102 int _ = 2; > 103 END > 104 CASE(2) > 105 string maze_[] = {"X.X", > 106 "...", > 107 "XXX", > 108 "X.X"}; > 109 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 110 int startRow = 0; > 111 int startCol = 1; > 112 int moveRow_[] = {1, 0, -1, 0}; > 113 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 114 int moveCol_[] = {0, 1, 0, -1}; > 115 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 116 int _ = -1; > 117 END > 118 CASE(3) > 119 string maze_[] = {".......", > 120 "X.X.X..", > 121 "XXX...X", > 122 "....X..", > 123 "X....X.", > 124 "......."}; > 125 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 126 int startRow = 5; > 127 int startCol = 0; > 128 int moveRow_[] = {1, 0, -1, 0,-2, 1}; > 129 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 130 int moveCol_[] = {0, -1, 0, 1, 3, 0}; > 131 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 132 int _ = 7; > 133 END > 134 CASE(4) > 135 string maze_[] = {"......."}; > 136 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 137 int startRow = 0; > 138 int startCol = 0; > 139 int moveRow_[] = {1, 0, 1, 0, 1, 0}; > 140 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 141 int moveCol_[] = {0, 1, 0, 1, 0, 1}; > 142 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 143 int _ = 6; > 144 END > 145 CASE(5) > 146 string maze_[] = {"..X.X.X.X.X.X."}; > 147 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 148 int startRow = 0; > 149 int startCol = 0; > 150 int moveRow_[] = {2,0,-2,0}; > 151 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 152 int moveCol_[] = {0,2,0,-2}; > 153 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 154 int _ = -1; > 155 END > 156 CASE(6) > 157 string maze_[] = {".X","X."}; > 158 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 159 int startRow = 0; > 160 int startCol = 0; > 161 int moveRow_[] = {1}; > 162 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 163 int moveCol_[] = {1}; > 164 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 165 int _ = 1; > 166 END > 167 CASE(7) > 168 string maze_[] = {".X","X."}; > 169 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 170 int startRow = 0; > 171 int startCol = 0; > 172 int moveRow_[] = {1}; > 173 vector <int> moveRow(moveRow_, moveRow_+sizeof(moveRow_)/sizeof(*moveR > 174 int moveCol_[] = {0}; > 175 vector <int> moveCol(moveCol_, moveCol_+sizeof(moveCol_)/sizeof(*moveC > 176 int _ = -1; > 177 END > 178 > 179 } > 180 // END CUT HERE
Added SRM/453.5/1B.cpp version [c3946bd33dff056f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PlanarGraphShop { public: > 23 int bestCount(int N) > 24 { > 25 set<int> possible; > 26 possible.insert(1*1*1 + 0*0); > 27 possible.insert(2*2*2 + 0*0); > 28 possible.insert(2*2*2 + 1*1); > 29 for(int V=3,EMax=3; V*V*V<=N; ++V,EMax+=3) > 30 for(int E=0; V*V*V+E*E<=N && E<=EMax; ++E) > 31 possible.insert(V*V*V + E*E); > 32 vector<int> p(possible.rbegin(), possible.rend()); > 33 > 34 vector<int> q(1, 0); > 35 vector<bool> visited(N+1); > 36 for(int step=0 ;; ++step) > 37 { > 38 vector<int> q2; > 39 for(int i=0; i!=q.size(); ++i) > 40 { > 41 if( q[i] == N ) > 42 return step; > 43 > 44 for(int j=0; j<p.size(); ++j) > 45 { > 46 int next = q[i] + p[j]; > 47 if( next>N || visited[next] ) > 48 continue; > 49 visited[next] = true; > 50 q2.push_back(next); > 51 } > 52 } > 53 q.swap(q2); > 54 } > 55 assert(false); > 56 } > 57 }; > 58 > 59 // BEGIN CUT HERE > 60 #include <ctime> > 61 double start_time; string timer() > 62 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 63 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 64 { os << "{ "; > 65 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 66 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 67 void verify_case(const int& Expected, const int& Received) { > 68 bool ok = (Expected == Received); > 69 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 70 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 71 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 72 #define END verify_case(_, PlanarGraphShop().bestCount(N));} > 73 int main(){ > 74 > 75 CASE(0) > 76 int N = 36; > 77 int _ = 1; > 78 END > 79 CASE(1) > 80 int N = 7; > 81 int _ = 7; > 82 END > 83 CASE(2) > 84 int N = 72; > 85 int _ = 2; > 86 END > 87 CASE(3) > 88 int N = 46; > 89 int _ = 3; > 90 END > 91 CASE(4) > 92 int N = 50000; > 93 int _ = -1; > 94 END > 95 CASE(5) > 96 int N = 1; > 97 int _ = 1; > 98 END > 99 } > 100 // END CUT HERE
Added SRM/453.5/1C.cpp version [199b4b73c8055eff]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef long long LL; > 23 LL gcd(LL a, LL b) > 24 { > 25 while(a) > 26 swap(a, b%=a); > 27 return b; > 28 } > 29 > 30 LL lcm(LL a, LL b) > 31 { > 32 return a/gcd(a,b)*b; > 33 } > 34 > 35 class TheAlmostLuckyNumbers { public: > 36 void genLuck(vector<LL>& v) > 37 { > 38 // lucky numbers > 39 vector<LL> u; > 40 for(int d=1; d<=10; ++d) > 41 genLuck(d, u); > 42 > 43 // filtering > 44 for(int i=0; i<u.size(); ++i) > 45 { > 46 for(int j=0; j<i; ++j) > 47 if( u[i] % u[j] == 0 ) > 48 goto next; > 49 v.push_back( u[i] ); > 50 next:; > 51 } > 52 sort(v.begin(), v.end()); > 53 } > 54 > 55 void genLuck(int d, vector<LL>& v) > 56 { > 57 // d-digit lucky numbers > 58 for(int b=0; b<(1<<d); ++b) > 59 { > 60 LL n = 0; > 61 for(int i=0; i<d; ++i) > 62 n = n*10 + (b&(1<<i) ? 4 : 7); > 63 v.push_back(n); > 64 } > 65 } > 66 > 67 long long count(long long a, long long b) > 68 { > 69 vector<LL> v; > 70 genLuck(v); > 71 > 72 static const int M = 16; > 73 > 74 // inclusion-exclusion > 75 LL cnt = 0; > 76 { > 77 for(int m=1; m<(1<<M); ++m) > 78 { > 79 int bc = 0; > 80 LL n = 1; > 81 for(int i=0; i<M; ++i) > 82 if( m & (1<<i) ) > 83 ++bc, n=lcm(n, v[i]); > 84 cnt += (bc&1) ? (b/n-(a-1)/n) : -(b/n-(a-1)/n); > 85 } > 86 } > 87 > 88 // sieve > 89 vector<LL> aml; // almost lucky numbers > 90 for(int i=M; i<v.size() && v[i]<=b; ++i) > 91 { > 92 LL u = v[i]; > 93 for(LL s=a%u?(a/u+1)*u:a; s<=b; s+=u) > 94 { > 95 for(int k=0; k<M; ++k) > 96 if( s%v[k]==0 ) > 97 goto next; > 98 aml.push_back(s); > 99 next:; > 100 } > 101 if( aml.size() > 10000000 ) { > 102 sort(aml.begin(), aml.end()); > 103 aml.erase(unique(aml.begin(), aml.end()), aml.end()); > 104 } > 105 } > 106 > 107 sort(aml.begin(), aml.end()); > 108 aml.erase(unique(aml.begin(), aml.end()), aml.end()); > 109 return cnt + aml.size(); > 110 } > 111 }; > 112 > 113 // BEGIN CUT HERE > 114 #include <ctime> > 115 double start_time; string timer() > 116 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 117 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 118 { os << "{ "; > 119 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 120 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 121 void verify_case(const long long& Expected, const long long& Received) { > 122 bool ok = (Expected == Received); > 123 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 124 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 125 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 126 #define END verify_case(_, TheAlmostLuckyNumbers().count(a, b));} > 127 int main(){ > 128 > 129 CASE(0) > 130 long long a = 1LL; > 131 long long b = 10LL; > 132 long long _ = 3LL; > 133 END > 134 CASE(1) > 135 long long a = 14LL; > 136 long long b = 14LL; > 137 long long _ = 1LL; > 138 END > 139 CASE(2) > 140 long long a = 1LL; > 141 long long b = 100LL; > 142 long long _ = 39LL; > 143 END > 144 CASE(3) > 145 long long a = 1234LL; > 146 long long b = 4321LL; > 147 long long _ = 1178LL; > 148 END > 149 CASE(4) > 150 long long a = 1LL; > 151 long long b = 10000000000LL; > 152 long long _ = -1LL; > 153 END > 154 CASE(5) > 155 long long a = 10000000000LL; > 156 long long b = 10000000000LL; > 157 long long _ = -1LL; > 158 END > 159 > 160 } > 161 // END CUT HERE
Added SRM/454-U/1A.cpp version [f520f03e2411db6e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class DoubleXor { public: > 23 int calculate(int N) > 24 { > 25 int x = N; > 26 for(int y=N-1; y>=1; --y) > 27 x = dxor(x, y); > 28 > 29 int z = 1; > 30 for(int y=2; y<=N; ++y) > 31 z = dxor(z, y); > 32 if( x != z ) > 33 cerr << N << ": " << x << " -- " << z << endl; > 34 return x; > 35 } > 36 int dxor(int x, int y) > 37 { > 38 int z = 0; > 39 int d = 1; > 40 while( x>0 || y>0 ) { > 41 z += d * ((x%10 ^ y%10)%10); > 42 x /= 10; > 43 y /= 10; > 44 d *= 10; > 45 } > 46 return z; > 47 } > 48 }; > 49 > 50 // BEGIN CUT HERE > 51 #include <ctime> > 52 double start_time; string timer() > 53 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 54 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 55 { os << "{ "; > 56 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 57 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 58 void verify_case(const int& Expected, const int& Received) { > 59 bool ok = (Expected == Received); > 60 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 61 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 62 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 63 #define END verify_case(_, DoubleXor().calculate(N));} > 64 int main(){ > 65 > 66 for(int N=1;;++N) { > 67 CASE(-2) > 68 int _ = -1; > 69 END > 70 } > 71 CASE(-1) > 72 int N = 8; > 73 int _ = 2; > 74 END > 75 CASE(0) > 76 int N = 1; > 77 int _ = 1; > 78 END > 79 CASE(1) > 80 int N = 2; > 81 int _ = 3; > 82 END > 83 CASE(2) > 84 int N = 7; > 85 int _ = 0; > 86 END > 87 CASE(3) > 88 int N = 10; > 89 int _ = 11; > 90 END > 91 CASE(4) > 92 int N = 102; > 93 int _ = 103; > 94 END > 95 CASE(5) > 96 int N = 1000000; > 97 int _ = -1; > 98 END > 99 CASE(6) > 100 int N = 1; > 101 int _ = 1; > 102 END > 103 CASE(6) > 104 int N = 2; > 105 int _ = 3; > 106 END > 107 CASE(6) > 108 int N = 3; > 109 int _ = 0; > 110 END > 111 CASE(6) > 112 int N = 4; > 113 int _ = 4; > 114 END > 115 CASE(6) > 116 int N = 5; > 117 int _ = 1; > 118 END > 119 CASE(6) > 120 int N = 6; > 121 int _ = 7; > 122 END > 123 CASE(6) > 124 int N = 7; > 125 int _ = 0; > 126 END > 127 CASE(6) > 128 int N = 9; > 129 int _ = 1; > 130 END > 131 > 132 } > 133 // END CUT HERE
Added SRM/454-U/1B.cpp version [54e51fc242068030]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP2 > 24 { > 25 const int N1, N2; > 26 vector<T> data; > 27 DP2(int N1, int N2, const T& t = T()) > 28 : N1(N1), N2(N2), data(N1*N2, t) {} > 29 T& operator()(int i1, int i2) > 30 { return data[ (i1*N2)+i2 ]; } > 31 void swap(DP2& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 class NumbersAndMatches { public: > 36 long long differentNumbers(long long N, int K) > 37 { > 38 string s; > 39 for(; N; N/=10) > 40 s = char('0' + N%10) + s; > 41 > 42 DP2<LL> dp(127,127); > 43 dp(0,0) = 1; > 44 > 45 for(int i=0; i<s.size(); ++i) > 46 { > 47 DP2<LL> dp2(127, 127); > 48 for(int tooMany=0; tooMany<=K; ++tooMany) > 49 for(int tooFew=0; tooFew<=K; ++tooFew) > 50 for(char c='0'; c<='9'; ++c) > 51 { > 52 int tM=tooMany, tF=tooFew; > 53 diff(s[i], c, &tM, &tF); > 54 if( tM<=K && tF<=K ) > 55 dp2(tM,tF) += dp(tooMany,tooFew) > 56 } > 57 dp.swap(dp2); > 58 } > 59 > 60 LL ans = 0; > 61 for(int tMF=0; tMF<=K; ++tMF) > 62 ans += dp(tMF, tMF); > 63 return ans; > 64 } > 65 > 66 void diff(char a, char b, int* tF, int* tM) > 67 { > 68 const char* a7 = seven_seg(a); > 69 const char* b7 = seven_seg(b); > 70 for(int i=0; i<7; ++i) > 71 if( a7[i] < b7[i] ) > 72 ++*tF; > 73 else if( a7[i] > b7[i] ) > 74 ++*tM; > 75 } > 76 > 77 const char* seven_seg(char c) > 78 { > 79 const char* ans[] = { > 80 "1110111", > 81 "0010011", > 82 "1011101", > 83 "1011011", > 84 "0111010", > 85 "1101011", > 86 "1101111", > 87 "1010010", > 88 "1111111", > 89 "1111011", > 90 }; > 91 return ans[c-'0']; > 92 } > 93 }; > 94 > 95 // BEGIN CUT HERE > 96 #include <ctime> > 97 double start_time; string timer() > 98 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 99 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 100 { os << "{ "; > 101 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 102 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 103 void verify_case(const long long& Expected, const long long& Received) { > 104 bool ok = (Expected == Received); > 105 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 106 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 107 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 108 #define END verify_case(_, NumbersAndMatches().differentNumbers(N, K));} > 109 int main(){ > 110 > 111 CASE(0) > 112 long long N = 10LL; > 113 int K = 1; > 114 long long _ = 4LL; > 115 END > 116 CASE(1) > 117 long long N = 23LL; > 118 int K = 1; > 119 long long _ = 4LL; > 120 END > 121 CASE(2) > 122 long long N = 66LL; > 123 int K = 2; > 124 long long _ = 15LL; > 125 END > 126 CASE(3) > 127 long long N = 888888888LL; > 128 int K = 100; > 129 long long _ = 1LL; > 130 END > 131 CASE(4) > 132 long long N = 444444444444444444LL; > 133 int K = 2; > 134 long long _ = 1LL; > 135 END > 136 CASE(5) > 137 long long N = 1LL; > 138 int K = 1; > 139 long long _ = 2LL; > 140 END > 141 CASE(6) > 142 long long N = 12345678901234567LL; > 143 int K = 126; > 144 long long _ = -1LL; > 145 END > 146 > 147 } > 148 // END CUT HERE
Added SRM/454-U/1C-U.cpp version [237d262a30af7167]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007LL; > 23 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 24 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 25 > 26 class MegaSum { public: > 27 int calculate(long long N) > 28 { > 29 LL nth = sq(N); > 30 pair<LL,LL> p = position_of(N); > 31 > 32 LL cnt = 0; > 33 for(LL lane=1; lane<=nth; ++lane) > 34 cnt = ADD(cnt, sum_in_the_lane(p, lane)); > 35 return cnt; > 36 } > 37 > 38 LL sq( LL N ) > 39 { > 40 LL nth=1; > 41 while( !(N <= nth*nth) ) > 42 ++nth; > 43 return nth; > 44 } > 45 > 46 pair<LL,LL> position_of( LL N ) > 47 { > 48 // in which lane? > 49 LL nth = sq(N); > 50 > 51 // position > 52 LL dif = N - (nth-1)*(nth-1); > 53 LL y = (dif<=nth ? dif : nth); > 54 LL x = (dif<=nth ? nth : nth+nth-dif); > 55 if( nth%2 == 1 ) swap(x, y); > 56 > 57 return make_pair(y,x); > 58 } > 59 > 60 LL sum_in_the_lane(pair<LL,LL> p, LL lane) > 61 { > 62 LL y = p.first; > 63 LL x = p.second; > 64 > 65 > 66 if( lane%2 == 0 ) > 67 { > 68 // V > 69 // V > 70 //<<<<<< > 71 LL s = lane<=x ? sumV( 1, lane, min(y,lane), lane, y, x > 72 s = ADD(s, lane<=y ? sumH(lane,min(x,lane-1),lane,1,y,x) > 73 return s; > 74 } > 75 else > 76 { > 77 // A > 78 // A > 79 //>>>>>> > 80 LL s = lane<=y ? sumH( lane, 1, lane, min(x,lane), y, x > 81 s = ADD(s, lane<=x ? sumV(min(y,lane-1),lane,1,lane,y,x) > 82 return s; > 83 } > 84 } > 85 > 86 LL value_of(LL y, LL x) > 87 { > 88 LL lane = max(x, y); > 89 return (lane-1)*(lane-1) + (lane%2==0 ? (y<lane ? y : lane+lane- > 90 : (x<lane ? x : lane+lane- > 91 } > 92 > 93 LL sumH(LL y0, LL x, LL y1, LL _, LL Y, LL X) > 94 { > 95 // \Sigma value_of(y0+i,x) * (X-x+1)*(Y-(y0+i)+1) > 96 //= > 97 // \Sigma (value_of(y0,x)+i) * (X-x+1)*(Y-(y0+i)+1) > 98 //= > 99 // \Sigma (V0 +i) * XX *(YY-i) > 100 //= > 101 // ... > 102 assert(false); > 103 } > 104 > 105 LL sumV(LL y, LL x0, LL _, LL x1, LL Y, LL X) > 106 { > 107 assert(false); > 108 } > 109 }; > 110 > 111 // BEGIN CUT HERE > 112 #include <ctime> > 113 double start_time; string timer() > 114 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 115 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 116 { os << "{ "; > 117 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 118 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 119 void verify_case(const int& Expected, const int& Received) { > 120 bool ok = (Expected == Received); > 121 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 122 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 123 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 124 #define END verify_case(_, MegaSum().calculate(N));} > 125 int main(){ > 126 > 127 CASE(0) > 128 long long N = 8LL; > 129 int _ = 58; > 130 END > 131 CASE(1) > 132 long long N = 12LL; > 133 int _ = 282; > 134 END > 135 CASE(2) > 136 long long N = 11LL; > 137 int _ = 128; > 138 END > 139 CASE(3) > 140 long long N = 6LL; > 141 int _ = 50; > 142 END > 143 CASE(4) > 144 long long N = 34539LL; > 145 int _ = 437909839; > 146 END > 147 CASE(5) > 148 long long N = 10000000000LL; > 149 int _ = -1; > 150 END > 151 CASE(6) > 152 long long N = 123456789LL; > 153 int _ = -1; > 154 END > 155 > 156 } > 157 // END CUT HERE
Added SRM/456-U/1A.cpp version [cddace57bcfd8713]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SilverDistance { public: > 23 int minSteps(int sx, int sy, int gx, int gy) > 24 { > 25 return abs(sx-gx)%2 == abs(sy-gy)%2 > 26 ? max(abs(sx-gx),abs(sy-gy)) : 1+max(abs(sx-gx),abs(sy+1 > 27 } > 28 }; > 29 > 30 // BEGIN CUT HERE > 31 #include <ctime> > 32 double start_time; string timer() > 33 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 34 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 35 { os << "{ "; > 36 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 37 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 38 void verify_case(const int& Expected, const int& Received) { > 39 bool ok = (Expected == Received); > 40 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 41 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 42 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 43 #define END verify_case(_, SilverDistance().minSteps(sx, sy, gx, gy));} > 44 int main(){ > 45 > 46 CASE(0) > 47 int sx = 1; > 48 int sy = 0; > 49 int gx = 1; > 50 int gy = 9; > 51 int _ = 9; > 52 END > 53 CASE(1) > 54 int sx = 0; > 55 int sy = 0; > 56 int gx = -4; > 57 int gy = 3; > 58 int _ = 5; > 59 END > 60 CASE(2) > 61 int sx = 0; > 62 int sy = 0; > 63 int gx = 5; > 64 int gy = 8; > 65 int _ = 8; > 66 END > 67 CASE(3) > 68 int sx = -487617; > 69 int sy = 826524; > 70 int gx = 892309; > 71 int gy = -918045; > 72 int _ = 1744571; > 73 END > 74 CASE(4) > 75 int sx = -27857; > 76 int sy = 31475; > 77 int gx = -27857; > 78 int gy = 31475; > 79 int _ = 0; > 80 END > 81 > 82 } > 83 // END CUT HERE
Added SRM/456-U/1B.cpp version [4a688b8acc36f02d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CutSticks { public: > 23 double maxKth(vector <int> sticks, int Ct, int K) > 24 { > 25 double L=0, R=1000000000; > 26 for(int n=0; n<1000; ++n) > 27 { > 28 double C = (L+R)/2; > 29 > 30 // can we create K sticks of length C from sticks within > 31 int cnt = 0; > 32 int ncut = 0; > 33 for(int i=0; i<sticks.size(); ++i) { > 34 int ct = int( sticks[i] / C )-1; > 35 if( ct >= 0 ) { > 36 ct = min(ct, Ct-ncut); > 37 ncut += ct; > 38 cnt += ct+1; > 39 } > 40 } > 41 > 42 (cnt >= K ? L : R) = C; > 43 } > 44 return L; > 45 } > 46 }; > 47 > 48 // BEGIN CUT HERE > 49 #include <ctime> > 50 double start_time; string timer() > 51 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 52 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 53 { os << "{ "; > 54 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 55 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 56 void verify_case(const double& Expected, const double& Received) { > 57 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 58 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 59 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 60 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 61 #define END verify_case(_, CutSticks().maxKth(sticks, C, K));} > 62 int main(){ > 63 > 64 CASE(0) > 65 int sticks_[] = {5, 8}; > 66 vector <int> sticks(sticks_, sticks_+sizeof(sticks_)/sizeof(*sticks_)) > 67 int C = 1; > 68 int K = 1; > 69 double _ = 8.0; > 70 END > 71 CASE(1) > 72 int sticks_[] = {5, 8}; > 73 vector <int> sticks(sticks_, sticks_+sizeof(sticks_)/sizeof(*sticks_)) > 74 int C = 1; > 75 int K = 2; > 76 double _ = 5.0; > 77 END > 78 CASE(2) > 79 int sticks_[] = {5, 8}; > 80 vector <int> sticks(sticks_, sticks_+sizeof(sticks_)/sizeof(*sticks_)) > 81 int C = 1; > 82 int K = 3; > 83 double _ = 4.0; > 84 END > 85 CASE(3) > 86 int sticks_[] = {1000000000, 1000000000, 1}; > 87 vector <int> sticks(sticks_, sticks_+sizeof(sticks_)/sizeof(*sticks_)) > 88 int C = 2; > 89 int K = 5; > 90 double _ = 1.0; > 91 END > 92 CASE(4) > 93 int sticks_[] = {76, 594, 17, 6984, 26, 57, 9, 876, 5816, 73, 969, 527, > 94 vector <int> sticks(sticks_, sticks_+sizeof(sticks_)/sizeof(*sticks_)) > 95 int C = 789; > 96 int K = 456; > 97 double _ = 34.92; > 98 END > 99 > 100 } > 101 // END CUT HERE
Added SRM/457-U/1A.cpp version [3c8f623356e9f51d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheTriangleBothDivs { public: > 23 string fix(string time) > 24 { > 25 string answer; > 26 > 27 for(int hh= 0; hh<=23; ++hh) > 28 for(int mm= 0; mm<=59; ++mm) > 29 for(int of=-9; of<=+9; ++of) > 30 if( is_possible(hh,mm,of,time) ) { > 31 stringstream ss; > 32 ss << setw(2) << setfill('0') << (hh-of+24)%24 > 33 << ":" > 34 << setw(2) << setfill('0') << mm; > 35 > 36 if( answer.empty() || ss.str()<answer ) > 37 answer = ss.str(); > 38 } > 39 return answer; > 40 } > 41 > 42 bool is_possible(int hh, int mm, int of, const string& time) > 43 { > 44 stringstream ss; > 45 ss << setw(2) << setfill('0') << hh > 46 << ":" > 47 << setw(2) << setfill('0') << mm > 48 << " GMT" > 49 << (of>=0 ? '+' : '-') > 50 << (of>=0 ? of : -of); > 51 > 52 string s = ss.str(); > 53 for(int i=0; i<s.size(); ++i) > 54 if( s[i] != time[i] && time[i]!='?' ) > 55 return false; > 56 return true; > 57 } > 58 }; > 59 > 60 // BEGIN CUT HERE > 61 #include <ctime> > 62 double start_time; string timer() > 63 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 64 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 65 { os << "{ "; > 66 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 67 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 68 void verify_case(const string& Expected, const string& Received) { > 69 bool ok = (Expected == Received); > 70 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 71 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 72 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 73 #define END verify_case(_, TheTriangleBothDivs().fix(time));} > 74 int main(){ > 75 > 76 CASE(0) > 77 string time = "17:45 GMT-4"; > 78 string _ = "21:45"; > 79 END > 80 CASE(1) > 81 string time = "16:?? GMT??"; > 82 string _ = "00:00"; > 83 END > 84 CASE(2) > 85 string time = "?1:34 GMT-9"; > 86 string _ = "06:34"; > 87 END > 88 CASE(3) > 89 string time = "??:?? GMT??"; > 90 string _ = "00:00"; > 91 END > 92 CASE(4) > 93 string time = "01:?? GMT?1"; > 94 string _ = "00:00"; > 95 END > 96 CASE(5) > 97 string time = "00:00 GMT-?"; > 98 string _ = "01:00"; > 99 END > 100 > 101 } > 102 // END CUT HERE
Added SRM/457-U/1B.cpp version [66d130730b42345e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheHexagonsDivOne { public: > 23 long long count(int n) > 24 { > 25 // at least there must be 7<=2*n numbers... > 26 if( n <= 3 ) > 27 return 0; > 28 > 29 // center hex has 2n possibility > 30 // choose others from 2(n-1) candidates > 31 // normalize by /6 to classify by the least number > 32 > 33 LL ans = 0; > 34 vector<int> pat; > 35 count_pat( pat, n-1, ans ); > 36 return ans*2*n/6; > 37 } > 38 > 39 void count_pat( vector<int>& pat, LL n, LL& ans ) > 40 { > 41 int i = pat.size(); > 42 if( i == 6 ) > 43 { > 44 LL distinct = set<int>(pat.begin(), pat.end()).size(); > 45 LL tmp = 1; > 46 for(int d=0; d<distinct; ++d) > 47 tmp *= (n-d); > 48 > 49 LL dup = 6 - distinct; > 50 for(int d=0; d<dup; ++d) > 51 tmp *= 2; > 52 > 53 LL nodup = 6 - 2*dup; > 54 for(int d=0; d<nodup; ++d) > 55 tmp *= 2; > 56 > 57 ans += tmp; > 58 return; > 59 } > 60 > 61 vector<int> use(7); > 62 int next = 1; > 63 for(int k=0; k<pat.size(); ++k) { > 64 use[pat[k]]++; > 65 next = max(next, pat[k]+1); > 66 } > 67 > 68 if( next <= n ) > 69 { > 70 pat.push_back(next); > 71 count_pat( pat, n, ans ); > 72 pat.pop_back(); > 73 } > 74 > 75 for(int p=1; p<next; ++p) > 76 if( use[p]<=1 ) > 77 if( i==0 || (pat[i-1]!=p && (i<5 || pat[0]!=p)) > 78 { > 79 pat.push_back(p); > 80 count_pat( pat, n, ans ); > 81 pat.pop_back(); > 82 } > 83 } > 84 }; > 85 > 86 // BEGIN CUT HERE > 87 #include <ctime> > 88 double start_time; string timer() > 89 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 90 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 91 { os << "{ "; > 92 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 93 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 94 void verify_case(const long long& Expected, const long long& Received) { > 95 bool ok = (Expected == Received); > 96 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 97 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 98 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 99 #define END verify_case(_, TheHexagonsDivOne().count(n));} > 100 int main(){ > 101 > 102 CASE(0) > 103 int n = 3; > 104 long long _ = 0LL; > 105 END > 106 CASE(1) > 107 int n = 4; > 108 long long _ = 256LL; > 109 END > 110 CASE(2) > 111 int n = 8; > 112 long long _ = 3458560LL; > 113 END > 114 CASE(3) > 115 int n = 20; > 116 long long _ = 11193888000LL; > 117 END > 118 CASE(4) > 119 int n = 1; > 120 long long _ = 0LL; > 121 END > 122 CASE(5) > 123 int n = 150; > 124 long long _ = -1LL; > 125 END > 126 > 127 } > 128 // END CUT HERE
Added SRM/457-U/1C-U.cpp version [1f56abd1e071f50d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int NV = 18*18*2+18+2; > 23 typedef int flow; > 24 typedef LL cost; > 25 typedef int vert; > 26 typedef vert edge; > 27 typedef vector<edge> edges; > 28 typedef vector<edges> graph; > 29 typedef flow flow_graph[NV][NV]; > 30 typedef cost cost_graph[NV][NV]; > 31 static const flow FLOW_INF = 0x7FFFFFFF; > 32 static const cost COST_INF = 0x7FFFFFFFFFFFFFFFLL; > 33 > 34 // edges(bidi), capacity, cost(s.t. C[x][y]=-C[y][x]) per 1 flow, src, dst > 35 pair<cost,flow> mincostFlow(graph& G, flow_graph F, cost_graph C, vert S, vert D > 36 { > 37 int N = G.size(); > 38 cost total_cost = 0; > 39 flow total_flow = 0; > 40 > 41 vector<cost> h(N, 0); // potential > 42 for(cost RF=FLOW_INF; RF>0; ) // residual flow > 43 { > 44 // Dijkstra -- find the min-cost path > 45 vector<cost> d(N, COST_INF); d[S] = 0; > 46 vector<vert> prev(N, -1); > 47 > 48 typedef pair<cost, pair<vert,vert> > cedge; > 49 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 50 Q.push( cedge(0, make_pair(S,S)) ); > 51 while( !Q.empty() ) { > 52 cedge e = Q.top(); Q.pop(); > 53 if( prev[e.second.second] >= 0 ) > 54 continue; > 55 prev[e.second.second] = e.second.first; > 56 > 57 vert u = e.second.second; > 58 for(int i=0; i<G[u].size(); ++i) { > 59 vert v = G[u][i]; > 60 cost r_cost = C[u][v] + h[u] - h[v]; > 61 if( F[u][v] > 0 && d[v] > d[u]+r_cost ) > 62 Q.push( cedge(d[v]=d[u]+r_cost, make_pai > 63 } > 64 } > 65 > 66 if( prev[D] < 0 ) > 67 break; // Finished > 68 > 69 // As much as possible > 70 flow f = RF; > 71 for(vert u=D; u!=S; u=prev[u]) > 72 f = min(f, F[prev[u]][u]); > 73 RF -= f; > 74 total_flow += f; > 75 > 76 for(vert u=D; u!=S; u=prev[u]) > 77 { > 78 total_cost += f * C[prev[u]][u]; > 79 F[prev[u]][u] -= f; > 80 F[u][prev[u]] += f; > 81 } > 82 > 83 // Update the potential > 84 for(vert u=0; u<N; ++u) > 85 h[u] += d[u]; > 86 } > 87 return make_pair(total_cost, total_flow); > 88 } > 89 > 90 > 91 flow_graph F; > 92 cost_graph C; > 93 > 94 LL f(LL n) { > 95 return n*n*n*n*n; > 96 } > 97 > 98 class TheSquareDivOne { public: > 99 vector <string> solve(vector <string> board) > 100 { > 101 int n = board.size(); > 102 vector<int> R(n); > 103 for(int y=0; y<n; ++y) > 104 for(int x=0; x<n; ++x) > 105 if( board[y][x]=='C' ) > 106 R[y] ++; > 107 > 108 graph G(2+n+n*n*2); > 109 memset(F, 0, sizeof(F)); > 110 memset(C, 0, sizeof(C)); > 111 vert S = 0; > 112 vert D = 1; > 113 > 114 for(int y=0; y<n; ++y) > 115 for(int x=0; x<n; ++x) { > 116 vert v = (2+n) + y*n + x; > 117 G[S].push_back(v); > 118 G[v].push_back(S); > 119 if( board[y][x]=='C' ) > 120 F[S][v] = 1; > 121 } > 122 > 123 for(int y=0; y<n; ++y) > 124 for(int x=0; x<n; ++x) { > 125 vert v = (2+n) + (n*n) + y*n + x; > 126 vert u = 2+x; > 127 G[v].push_back(u); > 128 G[u].push_back(v); > 129 F[v][u] = 1; > 130 // C[u][v] = -(C[v][u] = (1LL<<(n-y-1))<<30); > 131 } > 132 > 133 for(int x=0; x<n; ++x) { > 134 vert u = 2+x; > 135 G[u].push_back(D); > 136 G[D].push_back(u); > 137 F[u][D] = R[x]; > 138 // C[u][D] = -(C[D][u] = (1LL<<2*(n-x-1))); > 139 } > 140 > 141 for(int y1=0; y1<n; ++y1) > 142 for(int x1=0; x1<n; ++x1) > 143 for(int y2=0; y2<n; ++y2) > 144 for(int x2=0; x2<n; ++x2) { > 145 vert v = (2+n) + y1*n + x1; > 146 vert u = (2+n) + (n*n) + y2*n + > 147 G[u].push_back(v); > 148 G[v].push_back(u); > 149 F[v][u] = 1; > 150 C[u][v] = -(C[v][u] = abs(y1-y2) > 151 // C[u][v] = -(C[v][u] = LL(abs(y1- > 152 } > 153 > 154 pair<cost,flow> mf = mincostFlow(G,F,C,S,D); > 155 // cerr << "Cost: " << (mf.first>>50) << "+" << ((mf.first&(1LL<<50 > 156 > 157 for(int y=0; y<n; ++y) > 158 for(int x=0; x<n; ++x) > 159 board[y][x] = (F[(2+n) + (n*n) + y*n + x][2+x]== > 160 > 161 // not lexicographically first ...orz > 162 return board; > 163 } > 164 }; > 165 > 166 // BEGIN CUT HERE > 167 #include <ctime> > 168 double start_time; string timer() > 169 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 170 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 171 { os << "{ "; > 172 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 173 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 174 void verify_case(const vector <string>& Expected, const vector <string>& Receive > 175 bool ok = (Expected == Received); > 176 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 177 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 178 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 179 #define END verify_case(_, TheSquareDivOne().solve(board));} > 180 int main(){ > 181 > 182 CASE(0) > 183 string board_[] = {"...", > 184 "...", > 185 "C.."}; > 186 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 187 string __[] = {"...", "...", "..C" }; > 188 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 189 END > 190 CASE(1) > 191 string board_[] = {"CCC", > 192 ".C.", > 193 "CCC"}; > 194 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 195 string __[] = {"C.C", "C.C", "CCC" }; > 196 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 197 END > 198 CASE(2) > 199 string board_[] = {"C..", > 200 ".C.", > 201 "..C"}; > 202 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 203 string __[] = {"C..", ".C.", "..C" }; > 204 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 205 END > 206 CASE(3) > 207 string board_[] = {"C....","CCCCC","...CC",".CC..",".C.C."}; > 208 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 209 string __[] = {".C...", "CCCCC", ".C..C", ".CC..", ".C.C." }; > 210 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 211 END > 212 CASE(4) > 213 string board_[] = { > 214 "CCCCCCCCCCCCCCCCCC", > 215 "CCCCCCCCCCCCCCCCCC", > 216 "CCCCCCCCCCCCCCCCCC", > 217 "CCCCCCCCCCCCCCCCCC", > 218 "CCCCCCCCCCCCCCCCCC", > 219 "CCCCCCCCCCCCCCCCCC", > 220 "CCCCCCCCCCCCCCCCCC", > 221 "CCCCCCCCCCCCCCCCCC", > 222 "CCCCCCCCCCCCCCCCCC", > 223 "CCCCCCCCCCCCCCCCCC", > 224 "CCCCCCCCCCCCCCCCCC", > 225 "CCCCCCCCCCCCCCCCCC", > 226 "CCCCCCCCCCCCCCCCCC", > 227 "CCCCCCCCCCCCCCCCCC", > 228 "CCCCCCCCCCCCCCCCCC", > 229 "CCCCCCCCCCCCCCCCCC", > 230 "CCCCCCCCCCCCCCCCCC", > 231 "CCCCCCCCCCCCCCCCCC", > 232 }; > 233 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 234 string __[] = { > 235 "CCCCCCCCCCCCCCCCCC", > 236 "CCCCCCCCCCCCCCCCCC", > 237 "CCCCCCCCCCCCCCCCCC", > 238 "CCCCCCCCCCCCCCCCCC", > 239 "CCCCCCCCCCCCCCCCCC", > 240 "CCCCCCCCCCCCCCCCCC", > 241 "CCCCCCCCCCCCCCCCCC", > 242 "CCCCCCCCCCCCCCCCCC", > 243 "CCCCCCCCCCCCCCCCCC", > 244 "CCCCCCCCCCCCCCCCCC", > 245 "CCCCCCCCCCCCCCCCCC", > 246 "CCCCCCCCCCCCCCCCCC", > 247 "CCCCCCCCCCCCCCCCCC", > 248 "CCCCCCCCCCCCCCCCCC", > 249 "CCCCCCCCCCCCCCCCCC", > 250 "CCCCCCCCCCCCCCCCCC", > 251 "CCCCCCCCCCCCCCCCCC", > 252 "CCCCCCCCCCCCCCCCCC", > 253 }; > 254 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 255 END > 256 /* > 257 CASE(5) > 258 string board_[] = ; > 259 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 260 string __[] = ; > 261 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 262 END > 263 */ > 264 } > 265 // END CUT HERE
Added SRM/458-U/1A.cpp version [dd4898e19fbf1df8]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BouncingBalls { public: > 23 double expectedBounces(vector <int> x, int T) > 24 { > 25 sort(x.begin(), x.end()); > 26 > 27 double total = 0; > 28 > 29 int N = x.size(); > 30 for(int m=0; m<(1<<N); ++m) > 31 { > 32 vector<bool> goLeft(N); > 33 for(int i=0; i<N; ++i) > 34 goLeft[i] = ((1<<i)&m) != 0; > 35 > 36 total += num(x, goLeft, T); > 37 } > 38 > 39 return total / (1<<N); > 40 } > 41 > 42 double num( const vector<int>& x, const vector<bool>& goLeft, int T ) > 43 { > 44 int cnt = 0; > 45 for(int i=0; i<x.size(); ++i) > 46 for(int j=i+1; j<x.size(); ++j) > 47 if( !goLeft[i] && goLeft[j] && x[j]-x[i]<=2*T ) > 48 ++cnt; > 49 return cnt; > 50 } > 51 }; > 52 > 53 // BEGIN CUT HERE > 54 #include <ctime> > 55 double start_time; string timer() > 56 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 57 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 58 { os << "{ "; > 59 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 60 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 61 void verify_case(const double& Expected, const double& Received) { > 62 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 63 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 64 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 65 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 66 #define END verify_case(_, BouncingBalls().expectedBounces(x, T));} > 67 int main(){ > 68 > 69 CASE(0) > 70 int x_[] = {5, 8}; > 71 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 72 int T = 2; > 73 double _ = 0.25; > 74 END > 75 CASE(1) > 76 int x_[] = {5, 8}; > 77 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 78 int T = 1; > 79 double _ = 0.0; > 80 END > 81 CASE(2) > 82 int x_[] = {91, 857, 692, 54, 8679, 83, 792, 86, 9537, 913, 64, 592}; > 83 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 84 int T = 458; > 85 double _ = 11.5; > 86 END > 87 CASE(3) > 88 int x_[] = {75432}; > 89 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 90 int T = 386; > 91 double _ = 0.0; > 92 END > 93 CASE(4) > 94 int x_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; > 95 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 96 int T = 3; > 97 double _ = 12.75; > 98 END > 99 CASE(5) > 100 int x_[] = {1}; > 101 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 102 int T = 100000000; > 103 double _ = 0; > 104 END > 105 CASE(6) > 106 int x_[] = {10000000}; > 107 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 108 int T = 100000; > 109 double _ = 0; > 110 END > 111 > 112 } > 113 // END CUT HERE
Added SRM/458-U/1B.cpp version [4c507d892b5c2673]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NewCoins { public: > 23 int minCoins(vector <int> price) > 24 { > 25 memo.resize(100001, -1); > 26 return mc(1, price); > 27 } > 28 > 29 vector<int> memo; > 30 int mc( int d, const vector<int>& price ) > 31 { > 32 if( memo[d] != -1 ) > 33 return memo[d]; > 34 if( price.size() == 0 ) > 35 return 0; > 36 > 37 // all 1 > 38 int X = accumulate(price.begin(), price.end(), 0); > 39 > 40 int P = *max_element(price.begin(), price.end()); > 41 for(int p=2; p<=P; ++p) > 42 { > 43 int byOne = 0; > 44 > 45 vector<int> p2; > 46 for(int i=0; i<price.size(); ++i) { > 47 byOne += price[i] % p; > 48 if( memo[d*p]==-1 ) > 49 if(price[i] / p) p2.push_back(price[i]/p > 50 } > 51 X = min(X, byOne + mc(d*p, p2)); > 52 } > 53 return memo[d] = X; > 54 } > 55 }; > 56 > 57 // BEGIN CUT HERE > 58 #include <ctime> > 59 double start_time; string timer() > 60 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 61 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 62 { os << "{ "; > 63 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 64 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 65 void verify_case(const int& Expected, const int& Received) { > 66 bool ok = (Expected == Received); > 67 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 68 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 69 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 70 #define END verify_case(_, NewCoins().minCoins(price));} > 71 int main(){ > 72 > 73 CASE(0) > 74 int price_[] = {25, 102}; > 75 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 76 int _ = 4; > 77 END > 78 CASE(1) > 79 int price_[] = {58}; > 80 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 81 int _ = 1; > 82 END > 83 CASE(2) > 84 int price_[] = {1, 4, 5, 9, 16}; > 85 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 86 int _ = 7; > 87 END > 88 CASE(3) > 89 int price_[] = {1, 1, 1, 1, 1}; > 90 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 91 int _ = 5; > 92 END > 93 CASE(4) > 94 int price_[] = {28, 569, 587, 256, 15, 87, 927, 4, 589, 73, 98, 87, 597, > 95 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 96 int _ = 62; > 97 END > 98 CASE(5) > 99 int price_[] = {1}; > 100 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 101 int _ = 1; > 102 END > 103 CASE(6) > 104 int price_[] = {1,2,3,4,5,6,9997,9998,9999,99910,99911,99912,99913,99914 > 105 vector <int> price(price_, price_+sizeof(price_)/sizeof(*price_)); > 106 int _ = 183; > 107 END > 108 > 109 } > 110 // END CUT HERE
Added SRM/458-U/2C.cpp version [fa4ee92d598a5396]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ProductTriplet { public: > 23 long long countTriplets(int minx, int maxx, int miny, int maxy, int minz > 24 { > 25 return comp(minx, maxx, miny, maxy, maxz) - comp(minx, maxx, min > 26 } > 27 > 28 LL comp(LL mx, LL Mx, LL my, LL My, LL Z) > 29 { > 30 LL cnt = 0; > 31 for(LL x=mx; x<=Mx; ) > 32 { > 33 LL Cy = Z / x; > 34 if( Cy < my ) > 35 break; > 36 > 37 // max n such that Cy == Z / x+n > 38 LL n = (Z/Cy)-x; > 39 > 40 n = min(n, Mx-x)+1; > 41 cnt += n * (min(Cy,My) - my + 1); > 42 x += n; > 43 } > 44 return cnt; > 45 } > 46 }; > 47 > 48 // BEGIN CUT HERE > 49 #include <ctime> > 50 double start_time; string timer() > 51 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 52 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 53 { os << "{ "; > 54 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 55 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 56 void verify_case(const long long& Expected, const long long& Received) { > 57 bool ok = (Expected == Received); > 58 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 59 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 60 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 61 #define END verify_case(_, ProductTriplet().countTriplets(minx, maxx, miny, > 62 int main(){ > 63 > 64 CASE(0) > 65 int minx = 2; > 66 int maxx = 2; > 67 int miny = 3; > 68 int maxy = 3; > 69 int minz = 6; > 70 int maxz = 6; > 71 long long _ = 1LL; > 72 END > 73 CASE(1) > 74 int minx = 2; > 75 int maxx = 2; > 76 int miny = 3; > 77 int maxy = 3; > 78 int minz = 7; > 79 int maxz = 7; > 80 long long _ = 0LL; > 81 END > 82 CASE(2) > 83 int minx = 6; > 84 int maxx = 8; > 85 int miny = 4; > 86 int maxy = 5; > 87 int minz = 27; > 88 int maxz = 35; > 89 long long _ = 4LL; > 90 END > 91 CASE(3) > 92 int minx = 1; > 93 int maxx = 458; > 94 int miny = 1; > 95 int maxy = 458; > 96 int minz = 1; > 97 int maxz = 458; > 98 long long _ = 2877LL; > 99 END > 100 CASE(4) > 101 int minx = 8176; > 102 int maxx = 184561; > 103 int miny = 1348; > 104 int maxy = 43168; > 105 int minz = 45814517; > 106 int maxz = 957843164; > 107 long long _ = 2365846085LL; > 108 END > 109 CASE(5) > 110 int minx = 1; > 111 int maxx = 1; > 112 int miny = 1; > 113 int maxy = 1; > 114 int minz = 1; > 115 int maxz = 1; > 116 long long _ = 1LL; > 117 END > 118 CASE(6) > 119 int minx = 1; > 120 int maxx = 1000000000; > 121 int miny = 1; > 122 int maxy = 1000000000; > 123 int minz = 1; > 124 int maxz = 1000000000; > 125 long long _ = -1LL; > 126 END > 127 > 128 } > 129 // END CUT HERE
Added SRM/459-U/1A.cpp version [70860f519d844ba5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Inequalities { public: > 23 int maximumSubset(vector <string> inequalities) > 24 { > 25 set<double> v; > 26 > 27 for(int i=0; i<inequalities.size(); ++i) > 28 { > 29 stringstream ss(inequalities[i]); > 30 char _; string OP; double C; > 31 ss >> _ >> OP >> C; > 32 > 33 v.insert(C); > 34 } > 35 > 36 set<double> vv = v; > 37 vv.insert( *v.begin() - 1 ); > 38 vv.insert( *v.rbegin() + 1 ); > 39 > 40 for(set<double>::iterator it=v.begin(); it!=v.end(); ++it) > 41 for(set<double>::iterator jt=v.begin(); jt!=v.end(); ++jt) > 42 vv.insert( (*it + *jt)/2 ); > 43 > 44 int maxSat = 0; > 45 for(set<double>::iterator it=vv.begin(); it!=vv.end(); ++it) > 46 { > 47 double X = *it; > 48 > 49 int sat = 0; > 50 for(int i=0; i<inequalities.size(); ++i) > 51 { > 52 stringstream ss(inequalities[i]); > 53 char _; string OP; double C; > 54 ss >> _ >> OP >> C; > 55 > 56 if( (OP=="<" && X<C) > 57 || (OP=="<=" && X<=C) > 58 || (OP=="=" && X==C) > 59 || (OP==">" && X>C) > 60 || (OP==">=" && X>=C) ) > 61 ++sat; > 62 } > 63 maxSat = max(maxSat, sat); > 64 } > 65 return maxSat; > 66 } > 67 }; > 68 > 69 // BEGIN CUT HERE > 70 #include <ctime> > 71 double start_time; string timer() > 72 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 73 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 74 { os << "{ "; > 75 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 76 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 77 void verify_case(const int& Expected, const int& Received) { > 78 bool ok = (Expected == Received); > 79 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 80 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 81 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 82 #define END verify_case(_, Inequalities().maximumSubset(inequalities));} > 83 int main(){ > 84 > 85 CASE(0) > 86 string inequalities_[] = {"X <= 12","X = 13","X > 9","X < 10","X >= 14"} > 87 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 88 int _ = 3; > 89 END > 90 CASE(1) > 91 string inequalities_[] = {"X < 0","X <= 0"}; > 92 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 93 int _ = 2; > 94 END > 95 CASE(2) > 96 string inequalities_[] = {"X = 1","X = 2","X = 3","X > 0"}; > 97 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 98 int _ = 2; > 99 END > 100 CASE(3) > 101 string inequalities_[] = {"X <= 521","X >= 521","X = 521","X > 902","X > > 102 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 103 int _ = 5; > 104 END > 105 CASE(4) > 106 string inequalities_[] = {"X > 0"}; > 107 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 108 int _ = 1; > 109 END > 110 CASE(5) > 111 string inequalities_[] = {"X > 0", "X <= 0"}; > 112 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 113 int _ = 1; > 114 END > 115 CASE(6) > 116 string inequalities_[] = {"X = 0", "X = 0", "X = 0", "X = 0", "X = 0", " > 117 vector <string> inequalities(inequalities_, inequalities_+sizeof(inequ > 118 int _ = 50; > 119 END > 120 > 121 } > 122 // END CUT HERE
Added SRM/459-U/1B.cpp version [9ca9b4bf809d337c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NumberPyramids { public: > 23 int count(int baseLength, int top) > 24 { > 25 const int n = baseLength-1; > 26 const int s = top - (1<<n); > 27 if( s<0 || n>20 ) > 28 return 0; > 29 > 30 vector<int> dp(s+1, 0); > 31 dp[0] = 1; > 32 > 33 for(int i=0, nCi=1; i<=n; ++i, nCi=nCi*(n-i+1)/i) > 34 for(int x=nCi; x<=s; ++x) > 35 (dp[x] += dp[x-nCi]) %= 1000000009; > 36 > 37 return dp[s]; > 38 } > 39 }; > 40 > 41 // BEGIN CUT HERE > 42 #include <ctime> > 43 double start_time; string timer() > 44 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 45 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 46 { os << "{ "; > 47 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 48 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 49 void verify_case(const int& Expected, const int& Received) { > 50 bool ok = (Expected == Received); > 51 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 52 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 53 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 54 #define END verify_case(_, NumberPyramids().count(baseLength, top));} > 55 int main(){ > 56 > 57 CASE(0) > 58 int baseLength = 3; > 59 int top = 5; > 60 int _ = 2; > 61 END > 62 CASE(1) > 63 int baseLength = 5; > 64 int top = 16; > 65 int _ = 1; > 66 END > 67 CASE(2) > 68 int baseLength = 4; > 69 int top = 15; > 70 int _ = 24; > 71 END > 72 CASE(3) > 73 int baseLength = 15; > 74 int top = 31556; > 75 int _ = 74280915; > 76 END > 77 CASE(4) > 78 int baseLength = 150; > 79 int top = 500; > 80 int _ = 0; > 81 END > 82 CASE(5) > 83 int baseLength = 2; > 84 int top = 3; > 85 int _ = 2; > 86 END > 87 CASE(6) > 88 int baseLength = 20; > 89 int top = 1000000; > 90 int _ = 503596690; > 91 END > 92 > 93 } > 94 // END CUT HERE
Added SRM/460-U/1A.cpp version [70b68db021e8c302]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL comb(LL n, LL k) > 23 { > 24 k = min(k, n-k); > 25 > 26 LL c = 1; > 27 for(LL i=0; i<k; ++i) > 28 c *= n-i, c /= i+1; > 29 return c; > 30 } > 31 > 32 class TheQuestionsAndAnswersDivOne { public: > 33 int find(int questions, vector <string> answers) > 34 { > 35 int Y = count(answers.begin(), answers.end(), string("Yes")); > 36 int N = count(answers.begin(), answers.end(), string( "No")); > 37 > 38 int cnt = 0; > 39 for(int y=0; y<=questions; ++y) > 40 { > 41 int n = questions - y; > 42 if( y<=Y && n<=N ) > 43 cnt += distr(Y,y)*distr(N,n)*comb(questions, y); > 44 } > 45 return cnt; > 46 } > 47 > 48 int distr(int n, int k) > 49 { > 50 if( k <= 1 ) > 51 return (k==1 || n==0&&k==0) ? 1 : 0; > 52 int sum = 0; > 53 for(int m=1; m<(1<<n); ++m) { > 54 int pop = 0; > 55 for(int i=1; i<=m; i<<=1) > 56 if(m&i) > 57 ++pop; > 58 if( n-pop >= k-1 ) > 59 sum += distr(n-pop, k-1); > 60 } > 61 return sum; > 62 } > 63 }; > 64 > 65 // BEGIN CUT HERE > 66 #include <ctime> > 67 double start_time; string timer() > 68 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 69 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 70 { os << "{ "; > 71 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 72 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 73 void verify_case(const int& Expected, const int& Received) { > 74 bool ok = (Expected == Received); > 75 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 76 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 77 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 78 #define END verify_case(_, TheQuestionsAndAnswersDivOne().find(questions, a > 79 int main(){ > 80 > 81 CASE(0) > 82 int questions = 2; > 83 string answers_[] = {"No", "Yes"}; > 84 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 85 int _ = 2; > 86 END > 87 CASE(1) > 88 int questions = 2; > 89 string answers_[] = {"No", "No", "No"}; > 90 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 91 int _ = 6; > 92 END > 93 CASE(2) > 94 int questions = 3; > 95 string answers_[] = {"Yes", "No", "No", "Yes"}; > 96 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 97 int _ = 12; > 98 END > 99 CASE(3) > 100 int questions = 3; > 101 string answers_[] = {"Yes", "Yes", "Yes", "No"}; > 102 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 103 int _ = 18; > 104 END > 105 CASE(4) > 106 int questions = 2; > 107 string answers_[] = {"Yes","Yes"}; > 108 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 109 int _ = 2; > 110 END > 111 CASE(4) > 112 int questions = 2; > 113 string answers_[] = {"No","No"}; > 114 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 115 int _ = 2; > 116 END > 117 CASE(4) > 118 int questions = 2; > 119 string answers_[] = {"Yes","No"}; > 120 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 121 int _ = 2; > 122 END > 123 CASE(5) > 124 int questions = 7; > 125 string answers_[] = {"Yes","Yes","Yes","Yes","Yes","Yes","Yes","Yes","Ye > 126 vector <string> answers(answers_, answers_+sizeof(answers_)/sizeof(*an > 127 int _ = 40320*9; > 128 END > 129 > 130 } > 131 // END CUT HERE
Added SRM/460-U/1B.cpp version [2b2ccff9854de3ab]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP3 > 24 { > 25 int N1, N2, N3; > 26 vector<T> data; > 27 DP3(int N1, int N2, int N3, const T& t = T()) > 28 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) {} > 29 T& operator()(int i1, int i2, int i3) > 30 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 31 void swap(DP3& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 class TheFansAndMeetingsDivOne { public: > 36 double find(vector <int> minJ, vector <int> maxJ, vector <int> minB, vec > 37 { > 38 const int N = minJ.size(); > 39 const int S = max( accumulate(maxB.begin(), maxB.end(), 0), accu > 40 > 41 DP3<double> dpJ(41,41,1601); > 42 dpJ(0,0,0) = 1.0; > 43 for(int i=1; i<=N; ++i) > 44 for(int vis=0; vis<=k; ++vis) > 45 for(int s=0; s<=S; ++s) > 46 { > 47 double PP = 0; > 48 if(vis) > 49 for(int p=minJ[i-1]; p<=maxJ[i-1] && p<=s; ++p) > 50 PP += dpJ(vis-1, i-1, s-p); > 51 dpJ(vis,i,s) = dpJ(vis, i-1, s) + PP / (maxJ[i-1]-minJ[i > 52 } > 53 > 54 DP3<double> dpB(41,41,1601); > 55 dpB(0,0,0) = 1.0; > 56 for(int i=1; i<=N; ++i) > 57 for(int vis=0; vis<=k; ++vis) > 58 for(int s=0; s<=S; ++s) > 59 { > 60 double PP = 0; > 61 if(vis) > 62 for(int p=minB[i-1]; p<=maxB[i-1] && p<=s; ++p) > 63 PP += dpB(vis-1, i-1, s-p); > 64 dpB(vis,i,s) = dpB(vis, i-1, s) + PP / (maxB[i-1]-minB[i > 65 } > 66 > 67 double ans = 0.0, nB=0.0, nJ=0.0; > 68 for(int s=0; s<=S; ++s) > 69 nB += dpB(k, N, s), nJ += dpJ(k, N, s); > 70 for(int s=0; s<=S; ++s) > 71 ans += dpB(k, N, s) * dpJ(k, N, s) / nB / nJ; > 72 return ans; > 73 } > 74 }; > 75 > 76 // BEGIN CUT HERE > 77 #include <ctime> > 78 double start_time; string timer() > 79 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 80 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 81 { os << "{ "; > 82 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 83 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 84 void verify_case(const double& Expected, const double& Received) { > 85 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 86 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 87 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 88 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 89 #define END verify_case(_, TheFansAndMeetingsDivOne().find(minJ, maxJ, minB > 90 int main(){ > 91 > 92 CASE(0) > 93 int minJ_[] = {1}; > 94 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 95 int maxJ_[] = {9}; > 96 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 97 int minB_[] = {5}; > 98 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 99 int maxB_[] = {5}; > 100 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 101 int k = 1; > 102 double _ = 0.1111111111111111; > 103 END > 104 CASE(0) > 105 int minJ_[] = {5}; > 106 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 107 int maxJ_[] = {5}; > 108 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 109 int minB_[] = {1}; > 110 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 111 int maxB_[] = {9}; > 112 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 113 int k = 1; > 114 double _ = 0.1111111111111111; > 115 END > 116 CASE(1) > 117 int minJ_[] = {5, 2, 5, 1, 1, 2, 4, 1}; > 118 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 119 int maxJ_[] = {7, 6, 7, 3, 4, 3, 5, 1}; > 120 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 121 int minB_[] = {8, 9, 7, 11, 12, 7, 8, 40}; > 122 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 123 int maxB_[] = {9, 10, 9, 33, 14, 7, 11, 40}; > 124 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 125 int k = 2; > 126 double _ = 4.724111866969009E-5; > 127 END > 128 CASE(1) > 129 int minJ_[] = {8, 9, 7, 11, 12, 7, 8, 40}; > 130 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 131 int maxJ_[] = {9, 10, 9, 33, 14, 7, 11, 40}; > 132 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 133 int minB_[] = {5, 2, 5, 1, 1, 2, 4, 1}; > 134 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 135 int maxB_[] = {7, 6, 7, 3, 4, 3, 5, 1}; > 136 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 137 int k = 2; > 138 double _ = 4.724111866969009E-5; > 139 END > 140 CASE(2) > 141 int minJ_[] = {4, 7, 4}; > 142 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 143 int maxJ_[] = {7, 7, 7}; > 144 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 145 int minB_[] = {40, 40, 40}; > 146 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 147 int maxB_[] = {40, 40, 40}; > 148 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 149 int k = 1; > 150 double _ = 0.0; > 151 END > 152 CASE(2) > 153 int minJ_[] = {40, 40, 40}; > 154 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 155 int maxJ_[] = {40, 40, 40}; > 156 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 157 int minB_[] = {4, 7, 4}; > 158 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 159 int maxB_[] = {7, 7, 7}; > 160 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 161 int k = 1; > 162 double _ = 0.0; > 163 END > 164 CASE(3) > 165 int minJ_[] = {3, 6, 2, 1, 1, 10, 3}; > 166 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 167 int maxJ_[] = {6, 9, 5, 6, 5, 10, 9}; > 168 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 169 int minB_[] = {1, 1, 1, 1, 8, 3, 1}; > 170 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 171 int maxB_[] = {3, 9, 7, 3, 10, 6, 5}; > 172 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 173 int k = 4; > 174 double _ = 0.047082056525158976; > 175 END > 176 CASE(4) > 177 int minJ_[] = {40}; > 178 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 179 int maxJ_[] = {40}; > 180 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 181 int minB_[] = {40}; > 182 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 183 int maxB_[] = {40}; > 184 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 185 int k = 1; > 186 double _ = 1.0; > 187 END > 188 CASE(5) > 189 //1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 190 //40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40, > 191 int minJ_[] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8 > 192 vector <int> minJ(minJ_, minJ_+sizeof(minJ_)/sizeof(*minJ_)); > 193 int maxJ_[] = {30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, > 194 vector <int> maxJ(maxJ_, maxJ_+sizeof(maxJ_)/sizeof(*maxJ_)); > 195 int minB_[] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8 > 196 vector <int> minB(minB_, minB_+sizeof(minB_)/sizeof(*minB_)); > 197 int maxB_[] = {30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, > 198 vector <int> maxB(maxB_, maxB_+sizeof(maxB_)/sizeof(*maxB_)); > 199 int k = 40; > 200 double _ = -1; > 201 END > 202 > 203 } > 204 // END CUT HERE
Added SRM/460-U/1C-U.cpp version [94cf308d5baaaf21]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1234567891; > 23 struct UnionFind > 24 { > 25 vector<int> uf, sz, cyc; > 26 int nc; > 27 > 28 bool operator<(const UnionFind& rhs) const { > 29 if( nc != rhs.nc ) return nc < rhs.nc; > 30 if( uf != rhs.uf ) return uf < rhs.uf; > 31 if( sz != rhs.sz ) return sz < rhs.sz; > 32 return cyc < rhs.cyc; > 33 } > 34 > 35 UnionFind(int N): uf(N), sz(N,1), cyc(N), nc(N) > 36 { for(int i=0; i<N; ++i) uf[i] = i; } > 37 int size() > 38 { return nc; } > 39 int Find(int a) > 40 { return uf[a]==a ? a : uf[a]=Find(uf[a]); } > 41 int Union(int a, int b) > 42 { > 43 a = Find(a); > 44 b = Find(b); > 45 if( a != b ) > 46 { > 47 if( sz[a] >= sz[b] ) swap(a, b); > 48 uf[a] = b; > 49 sz[b] += sz[a]; > 50 cyc[b] += cyc[a]; > 51 --nc; > 52 } > 53 else > 54 cyc[a] ++; > 55 return cyc[a]; > 56 } > 57 }; > 58 > 59 class TheCitiesAndRoadsDivOne { public: > 60 int find(vector <string> map) > 61 { > 62 int N = map.size(); > 63 UnionFind uf(N); > 64 > 65 for(int i=0; i<N; ++i) > 66 for(int j=i+1; j<N; ++j) > 67 if( map[i][j]=='Y' ) > 68 if( uf.Union(i, j) >= 2 ) > 69 return 0; > 70 return rec(uf, 0, 1, N, map); > 71 } > 72 > 73 map< pair<UnionFind,int>, int > memo; > 74 int rec(UnionFind& uf, int i, int j , int N, vector<string>& map) > 75 { > 76 if( i == N ) > 77 return uf.nc==1 ? 1 : 0; > 78 if( j == N ) > 79 return rec(uf, i+1, i+2, N, map); > 80 > 81 pair<UnionFind,int> key(uf, i*N+j); > 82 if( memo.count(key) ) > 83 return memo[key]; > 84 > 85 LL cnt = rec(uf, i, j+1, N, map); > 86 if( map[i][j] == 'N' ) > 87 { > 88 UnionFind uf2(uf); > 89 if( uf2.Union(i,j) < 2 ) > 90 cnt = (cnt + rec(uf2,i,j+1,N,map)) % MODVAL; > 91 } > 92 return memo[key] = (int)cnt; > 93 } > 94 }; > 95 > 96 // BEGIN CUT HERE > 97 #include <ctime> > 98 double start_time; string timer() > 99 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 100 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 101 { os << "{ "; > 102 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 103 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 104 void verify_case(const int& Expected, const int& Received) { > 105 bool ok = (Expected == Received); > 106 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 107 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 108 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 109 #define END verify_case(_, TheCitiesAndRoadsDivOne().find(map));} > 110 int main(){ > 111 > 112 CASE(0) > 113 string map_[] = {"NN", > 114 "NN"}; > 115 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 116 int _ = 1; > 117 END > 118 CASE(1) > 119 string map_[] = {"NNY", > 120 "NNN", > 121 "YNN"}; > 122 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 123 int _ = 3; > 124 END > 125 CASE(2) > 126 string map_[] = {"NY", > 127 "YN"}; > 128 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 129 int _ = 1; > 130 END > 131 CASE(3) > 132 string map_[] = {"NYNN", > 133 "YNNN", > 134 "NNNY", > 135 "NNYN"}; > 136 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 137 int _ = 10; > 138 END > 139 CASE(4) > 140 string map_[] = {"NNNNNNNNNNNN", > 141 "NNNNNNNNNNNN", > 142 "NNNNNNNNNNNN", > 143 "NNNNNNNNNNNN", > 144 "NNNNNNNNNNNN", > 145 "NNNNNNNNNNNN", > 146 "NNNNNNNNNNNN", > 147 "NNNNNNNNNNNN", > 148 "NNNNNNNNNNNN", > 149 "NNNNNNNNNNNN", > 150 "NNNNNNNNNNNN", > 151 "NNNNNNNNNNNN"}; > 152 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 153 int _ = 1137797187; > 154 END > 155 CASE(5) > 156 string map_[] = {"N"}; > 157 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 158 int _ = 1; > 159 END > 160 CASE(6) > 161 string map_[] = {"NYYNN", > 162 "YNYNN", > 163 "YYNNN", > 164 "NNNNY", > 165 "NNNYN"}; > 166 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 167 int _ = 6; > 168 END > 169 /* > 170 CASE(7) > 171 string map_[] = ; > 172 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 173 int _ = ; > 174 END > 175 CASE(8) > 176 string map_[] = ; > 177 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 178 int _ = ; > 179 END > 180 */ > 181 } > 182 // END CUT HERE
Added SRM/461-U/1A.cpp version [0fb66fe6be1bd22d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <limits.h> > 18 #include <cstring> > 19 using namespace std; > 20 typedef long long LL; > 21 typedef complex<double> CMP; > 22 > 23 class ColoringRectangle { public: > 24 int chooseDisks(int width, int height, vector <int> red, vector <int> bl > 25 { > 26 vector<double> R; > 27 for(int i=0; i<red.size(); ++i) { > 28 double r = red[i]/2.0; > 29 if( r < height/2 ) > 30 continue; > 31 R.push_back( sqrt(r*r - (height/2.0)*(height/2.0))*2 ); > 32 } > 33 vector<double> B; > 34 for(int i=0; i<blue.size(); ++i) { > 35 double r = blue[i]/2.0; > 36 if( r < height/2 ) > 37 continue; > 38 B.push_back( sqrt(r*r - (height/2.0)*(height/2.0))*2 ); > 39 } > 40 sort(R.rbegin(), R.rend()); > 41 sort(B.rbegin(), B.rend()); > 42 > 43 int m = f(R, B, width); > 44 int n = f(B, R, width); > 45 int a = min(m,n); > 46 if( a == INT_MAX ) > 47 return -1; > 48 return a; > 49 } > 50 int f(vector<double>& R, vector<double>& B, double W) { > 51 double w = 0; > 52 for(int i=0 ;; ++i) { > 53 if( i%2 == 0 ) { > 54 if( i/2>=R.size() ) > 55 return INT_MAX; > 56 w += R[i/2]; > 57 if( W <= w ) > 58 return i+1; > 59 } else { > 60 if( i/2>=B.size() ) > 61 return INT_MAX; > 62 w += B[i/2]; > 63 if( W <= w ) > 64 return i+1; > 65 } > 66 } > 67 } > 68 }; > 69 > 70 // BEGIN CUT HERE > 71 #include <ctime> > 72 double start_time; string timer() > 73 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 74 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 75 { os << "{ "; > 76 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 77 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 78 void verify_case(const int& Expected, const int& Received) { > 79 bool ok = (Expected == Received); > 80 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 81 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 82 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 83 #define END verify_case(_, ColoringRectangle().chooseDisks(width, height, r > 84 int main(){ > 85 > 86 CASE(0) > 87 int width = 11; > 88 int height = 3; > 89 int red_[] = {5,5}; > 90 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 91 int blue_[] = {2,5}; > 92 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 93 int _ = 3; > 94 END > 95 CASE(1) > 96 int width = 30; > 97 int height = 5; > 98 int red_[] = {4,10,7,8,10}; > 99 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 100 int blue_[] = {5,6,11,7,5}; > 101 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 102 int _ = 4; > 103 END > 104 CASE(2) > 105 int width = 16; > 106 int height = 4; > 107 int red_[] = {6,5,7}; > 108 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 109 int blue_[] = {5}; > 110 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 111 int _ = -1; > 112 END > 113 CASE(3) > 114 int width = 4; > 115 int height = 4; > 116 int red_[] = {5}; > 117 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 118 int blue_[] = {6}; > 119 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 120 int _ = 1; > 121 END > 122 CASE(4) > 123 int width = 6; > 124 int height = 2; > 125 int red_[] = {6,6}; > 126 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 127 int blue_[] = {2}; > 128 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 129 int _ = 3; > 130 END > 131 CASE(5) > 132 int width = 16; > 133 int height = 13; > 134 int red_[] = {20}; > 135 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 136 int blue_[] = {20}; > 137 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 138 int _ = -1; > 139 END > 140 /* > 141 CASE(6) > 142 int width = ; > 143 int height = ; > 144 int red_[] = ; > 145 vector <int> red(red_, red_+sizeof(red_)/sizeof(*red_)); > 146 int blue_[] = ; > 147 vector <int> blue(blue_, blue_+sizeof(blue_)/sizeof(*blue_)); > 148 int _ = ; > 149 END > 150 */ > 151 } > 152 // END CUT HERE
Added SRM/461-U/1B.cpp version [800766c7be5a99d9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BuildingCities { public: > 23 int findMinimumCities(int maxDirect, int maxTravel, vector <int> cityX, vecto > 24 { > 25 int result = 0x7fffffff; > 26 > 27 const int N = cityX.size(); > 28 typedef pair<int, int> vert; // city, extra_city_built > 29 typedef pair<double, vert> cedge; > 30 > 31 // dijkstra > 32 set<vert> V; > 33 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 34 Q.push( cedge(0, vert(0,0)) ); > 35 while( !Q.empty() ) > 36 { > 37 double d = Q.top().first; > 38 vert v = Q.top().second; > 39 Q.pop(); > 40 if( V.count(v) ) > 41 continue; > 42 V.insert(v); > 43 > 44 int vc = v.first; > 45 int vs = v.second; > 46 if( vc==N-1 ) > 47 result = min(result, vs); > 48 > 49 for(int j=0; j<N; ++j) if( vc != j ) > 50 { > 51 double dd = hypot( cityX[vc] -cityX[j], cityY[vc] -cityY[j] ); > 52 double dx = hypot( cityX[N-1]-cityX[j], cityY[N-1]-cityY[j] ); > 53 vert u(j, vs+int(dd/maxDirect - 1e-10)); > 54 if( d+dd+dx<=maxTravel && !V.count(u) && u.second<result ) > 55 Q.push( cedge(d+dd, u) ); > 56 } > 57 } > 58 return result==0x7fffffff ? -1 : result; > 59 } > 60 }; > 61 > 62 // BEGIN CUT HERE > 63 #include <ctime> > 64 double start_time; string timer() > 65 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 66 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 67 { os << "{ "; > 68 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 69 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 70 void verify_case(const int& Expected, const int& Received) { > 71 bool ok = (Expected == Received); > 72 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 73 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 74 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 75 #define END verify_case(_, BuildingCities().findMinimumCities(maxDirect, maxT > 76 int main(){ > 77 > 78 CASE(0) > 79 int maxDirect = 1; > 80 int maxTravel = 5; > 81 int cityX_[] = {0,0,0,1,2,2,3}; > 82 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 83 int cityY_[] = {0,1,2,2,2,1,1}; > 84 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 85 int _ = 1; > 86 END > 87 CASE(1) > 88 int maxDirect = 3; > 89 int maxTravel = 15; > 90 int cityX_[] = {2,6,10,14}; > 91 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 92 int cityY_[] = {2,6,2,6}; > 93 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 94 int _ = 3; > 95 END > 96 CASE(2) > 97 int maxDirect = 2; > 98 int maxTravel = 15; > 99 int cityX_[] = {0,5,2,3,1,8,6,4,7,9,10}; > 100 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 101 int cityY_[] = {0,5,2,3,1,8,6,4,7,9,10}; > 102 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 103 int _ = 0; > 104 END > 105 CASE(3) > 106 int maxDirect = 2; > 107 int maxTravel = 5; > 108 int cityX_[] = {0,5}; > 109 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 110 int cityY_[] = {0,5}; > 111 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 112 int _ = -1; > 113 END > 114 CASE(4) > 115 int maxDirect = 5; > 116 int maxTravel = 1500; > 117 int cityX_[] = {0,1000}; > 118 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 119 int cityY_[] = {0,1000}; > 120 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 121 int _ = 282; > 122 END > 123 /* > 124 CASE(5) > 125 int maxDirect = ; > 126 int maxTravel = ; > 127 int cityX_[] = ; > 128 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 129 int cityY_[] = ; > 130 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 131 int _ = ; > 132 END > 133 CASE(6) > 134 int maxDirect = ; > 135 int maxTravel = ; > 136 int cityX_[] = ; > 137 vector <int> cityX(cityX_, cityX_+sizeof(cityX_)/sizeof(*cityX_)); > 138 int cityY_[] = ; > 139 vector <int> cityY(cityY_, cityY_+sizeof(cityY_)/sizeof(*cityY_)); > 140 int _ = ; > 141 END > 142 */ > 143 } > 144 // END CUT HERE
Added SRM/462/1A.cpp version [3ac34d38668840d0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class AgeEncoding { public: > 23 double getRadix(int age, string c) > 24 { > 25 int i = c.find('1'); > 26 if( i==string::npos ) i = c.size(); > 27 c = c.substr(i); > 28 > 29 int N = c.size(); > 30 if( N == 0 ) > 31 return -1; > 32 if( N == 1 ) > 33 return (age==1 ? -2 : -1); > 34 if( age == 1 && c[N-1]=='1' ) > 35 return -1; > 36 > 37 double L=0, R=1; > 38 for(;;R*=2){ > 39 double fR = 0.0; > 40 for(int i=0; i<N; ++i) > 41 fR +=pow(R,i)*(c[N-1-i]=='1'); > 42 if(fR>age) > 43 break; > 44 } > 45 > 46 for(int i=0; i<1000; ++i) > 47 { > 48 double C = (L+R)/2; > 49 > 50 double fL = 0.0; > 51 for(int i=0; i<N; ++i) > 52 fL +=pow(L,i)*(c[N-1-i]=='1'); > 53 double fR = 0.0; > 54 for(int i=0; i<N; ++i) > 55 fR +=pow(R,i)*(c[N-1-i]=='1'); > 56 double fC = 0.0; > 57 for(int i=0; i<N; ++i) > 58 fC +=pow(C,i)*(c[N-1-i]=='1'); > 59 > 60 (age < fC ? R : L) = C; > 61 } > 62 return R; > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time; string timer() > 69 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 70 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 71 { os << "{ "; > 72 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 73 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 74 void verify_case(const double& Expected, const double& Received) { > 75 double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff > 76 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 77 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 78 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 79 #define END verify_case(_, AgeEncoding().getRadix(age, candlesLine));} > 80 int main(){ > 81 > 82 CASE(0) > 83 int age = 10; > 84 string candlesLine = "00010"; > 85 double _ = 10.0; > 86 END > 87 CASE(1) > 88 int age = 21; > 89 string candlesLine = "10101"; > 90 double _ = 2.0; > 91 END > 92 CASE(2) > 93 int age = 6; > 94 string candlesLine = "10100"; > 95 double _ = 1.414213562373095; > 96 END > 97 CASE(3) > 98 int age = 21; > 99 string candlesLine = "10111111110111101111111100111111110111111111111100 > 100 double _ = 0.9685012944510603; > 101 END > 102 CASE(4) > 103 int age = 16; > 104 string candlesLine = "1"; > 105 double _ = -1.0; > 106 END > 107 CASE(5) > 108 int age = 1; > 109 string candlesLine = "1"; > 110 double _ = -2.0; > 111 END > 112 CASE(6) > 113 int age = 1; > 114 string candlesLine = "001000"; > 115 double _ = 1.0; > 116 END > 117 CASE(7) > 118 int age = 1; > 119 string candlesLine = "110"; > 120 double _ = (sqrt(5)-1)/2; > 121 END > 122 CASE(8) > 123 int age = 1; > 124 string candlesLine = "0000"; > 125 double _ = -1; > 126 END > 127 CASE(9) > 128 int age = 1; > 129 string candlesLine = "11"; > 130 double _ = -1; > 131 END > 132 CASE(10) > 133 int age = 1; > 134 string candlesLine = "11"; > 135 double _ = -1; > 136 END > 137 > 138 } > 139 // END CUT HERE
Added SRM/462/1B.cpp version [da8cd7890ede0b8d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 vector< vector<double> > mMul( const vector< vector<double> >& A, const vector< > 23 { > 24 const int n = A.size(); > 25 > 26 vector< vector<double> > C(n, vector<double>(n)); > 27 for(int i=0; i<n; ++i) > 28 for(int j=0; j<n; ++j) { > 29 double Cij = 0; > 30 for(int k=0; k<n; ++k) > 31 Cij += A[i][k] * B[k][j]; > 32 C[i][j] = Cij; > 33 } > 34 return C; > 35 } > 36 > 37 vector< vector<double> > mPow( vector< vector<double> > M, LL t ) > 38 { > 39 vector< vector<double> > R; > 40 if( t == 0 ) { > 41 const int n = M.size(); > 42 R = M; > 43 for(int i=0; i<n; ++i) > 44 for(int j=0; j<n; ++j) > 45 R[i][j] = (i==j); > 46 return R; > 47 } > 48 > 49 for(; t; t>>=1, M=mMul(M,M)) > 50 if( t&1 ) > 51 R = (R.empty() ? M : mMul(R,M)); > 52 return R; > 53 } > 54 > 55 class CandyBox { public: > 56 vector <double> expectedScore(int C, vector <int> score, int S) > 57 { > 58 int N = score.size(); > 59 > 60 vector< vector<double> > m(N, vector<double>(N)); > 61 for(int i=0; i<N; ++i) > 62 for(int j=0; j<N; ++j) { > 63 double pOut = 2.0/(N*(N*C-1)); > 64 m[i][j] = (i==j ? 1-pOut*(N-1) : pOut); > 65 } > 66 m = mPow(m, S); > 67 > 68 double as = accumulate(score.begin(), score.end(), 0.0); > 69 double stay = m[0][0]; > 70 double out = (N==1 ? 0 : m[1][0]); > 71 > 72 vector<double> result(N); > 73 for(int i=0; i<N; ++i) > 74 result[i] = (as-score[i])*out + score[i]*stay; > 75 return result; > 76 } > 77 }; > 78 > 79 // BEGIN CUT HERE > 80 #include <ctime> > 81 double start_time; string timer() > 82 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 83 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 84 { os << "{ "; > 85 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 86 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 87 void verify_case(const vector <double>& Expected, const vector <double>& Receive > 88 bool ok = true; > 89 for(int i=0; i<Expected.size(); ++i) > 90 if( abs(Expected[i]-Received[i])>1e-9 ) > 91 ok = false; > 92 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 93 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 94 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 95 #define END verify_case(_, CandyBox().expectedScore(C, score, S));} > 96 int main(){ > 97 > 98 CASE(0) > 99 int C = 10; > 100 int score_[] = {1, 10}; > 101 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 102 int S = 0; > 103 double __[] = {1.0, 10.0 }; > 104 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 105 END > 106 CASE(1) > 107 int C = 2; > 108 int score_[] = {1, 10}; > 109 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 110 int S = 1; > 111 double __[] = {4.0, 7.000000000000001 }; > 112 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 113 END > 114 CASE(2) > 115 int C = 1; > 116 int score_[] = {1, 4, 10}; > 117 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 118 int S = 1; > 119 double __[] = {5.0, 5.0, 5.0 }; > 120 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 121 END > 122 CASE(3) > 123 int C = 98; > 124 int score_[] = {13, 82, 74, 78, 12, 71, 81, 80, 30}; > 125 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 126 int S = 154; > 127 double __[] = {26.25622829378155, 74.87969915903301, 69.24219529059805, > 128 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 129 END > 130 CASE(4) > 131 int C = 1; > 132 int score_[] = {1,1}; > 133 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 134 int S = 1; > 135 double __[] = {1,1}; > 136 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 137 END > 138 CASE(5) > 139 int C = 2; > 140 int score_[] = {1}; > 141 vector <int> score(score_, score_+sizeof(score_)/sizeof(*score_)); > 142 int S = 10000; > 143 double __[] = {1}; > 144 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 145 END > 146 } > 147 // END CUT HERE
Added SRM/462/1C.cpp version [12341843ad5afdc4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef int vert; > 23 typedef int cost; > 24 typedef pair<cost,vert> edge; > 25 typedef vector<edge> edges; > 26 typedef vector<edges> graph; > 27 > 28 static const cost INF = 0x12345678; // large enough but INF+INF<2^31 > 29 static const vert START = 0; > 30 static const vert GOAL = 1; > 31 > 32 class WarTransportation { public: > 33 int messenger(int n, vector <string> highways) > 34 { > 35 return solve( parse(n, highways) ); > 36 } > 37 > 38 graph parse(int n, vector <string> highways) > 39 { > 40 graph G(n); > 41 > 42 string hi = accumulate(highways.begin(), highways.end(), string( > 43 for(int i=0; i<hi.size(); ) > 44 { > 45 int k = hi.find(',', i); > 46 if( k == string::npos ) k = hi.size(); > 47 > 48 int a, b, c; > 49 stringstream(hi.substr(i,k-i)) >> a >> b >> c; > 50 G[a-1].push_back( edge(c,b-1) ); > 51 > 52 i = k+1; > 53 } > 54 > 55 return G; > 56 } > 57 > 58 int solve( const graph& G ) > 59 { > 60 // Suppose you reached the city "v", and found there the most cr > 61 // How long will it take a detour to the GOAL? It's ukai[v]! > 62 > 63 vector<cost> ukai; > 64 for(int v=0; v<G.size(); ++v) > 65 ukai.push_back( ukaiDist(G,v) ); > 66 > 67 // Compute the least T such that: > 68 // we get to the GOAL, making the worst case time <= T? > 69 > 70 cost L=0, R=99999999; > 71 if( !reachable(G, ukai, R) ) return -1; > 72 if( reachable(G, ukai, L) ) return 0; > 73 > 74 // We can when T=R, and cannot T=L. > 75 // That is, T in (L, R]. Now let's binary-search! > 76 > 77 while( R-L>1 ) > 78 (reachable(G, ukai, (L+R)/2) ? R : L) = (L+R)/2; > 79 return R; > 80 } > 81 > 82 cost ukaiDist( const graph& G, vert v ) > 83 { > 84 if( v == GOAL ) return 0; > 85 if( G[v].size() == 0 ) return INF; > 86 > 87 cost worst = 0; > 88 for(int f=0; f<G[v].size(); ++f) // f : broken road > 89 { > 90 priority_queue< edge, vector<edge>, greater<edge> > Q; > 91 set<vert> V; > 92 V.insert(v); > 93 for(int i=0; i<G[v].size(); ++i) // push all loads from > 94 if( i != f ) > 95 Q.push( G[v][i] ); > 96 worst = max( worst, dijkstra(G,Q,V) ); // start dijkstra > 97 } > 98 return worst; > 99 } > 100 > 101 > 102 bool reachable( const graph& G, const vector<cost>& ukai, cost ukaiLimit > 103 { > 104 priority_queue< edge, vector<edge>, greater<edge> > Q; > 105 set<vert> V; > 106 Q.push( edge(0,START) ); > 107 return dijkstra(G, Q, V, ukai, ukaiLimit) != INF; > 108 } > 109 > 110 cost dijkstra( const graph& G, > 111 priority_queue< edge, vector<edge>, greater<edge> >& Q, set<vert > 112 const vector<cost>& ukai=vector<cost>(), cost ukaiLimit=-1 > 113 ) { > 114 while( !Q.empty() ) > 115 { > 116 // pop > 117 cost c = Q.top().first; > 118 vert v = Q.top().second; > 119 Q.pop(); > 120 > 121 // check > 122 if( V.count(v) || (ukaiLimit>=0 && c+ukai[v]>ukaiLimit) > 123 continue; > 124 if( v == GOAL ) > 125 return c; > 126 V.insert(v); > 127 > 128 // next > 129 for(int i=0; i<G[v].size(); ++i) > 130 if( !V.count(G[v][i].second) ) > 131 Q.push( edge(c+G[v][i].first, G[v][i].se > 132 } > 133 return INF; > 134 } > 135 }; > 136 > 137 // BEGIN CUT HERE > 138 #include <ctime> > 139 double start_time; string timer() > 140 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 141 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 142 { os << "{ "; > 143 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 144 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 145 void verify_case(const int& Expected, const int& Received) { > 146 bool ok = (Expected == Received); > 147 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 148 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 149 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 150 #define END verify_case(_, WarTransportation().messenger(n, highways));} > 151 int main(){ > 152 > 153 CASE(0) > 154 int n = 3; > 155 string highways_[] = {"1 2 1,1 3 2,3 2 3"}; > 156 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 157 int _ = 5; > 158 END > 159 CASE(1) > 160 int n = 8; > 161 string highways_[] = {"1 3 1,1 4 1,3 5 1,4 5 1,5 6 1,6 7 1,6 8 1,7 2 1," > 162 "8 2 1"}; > 163 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 164 int _ = -1; > 165 END > 166 CASE(2) > 167 int n = 4; > 168 string highways_[] = {"1 3 1,1 3 2,3 2 1,1 4 1,4 2 1"}; > 169 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 170 int _ = -1; > 171 END > 172 CASE(3) > 173 int n = 4; > 174 string highways_[] = {"1 3 1,3 2 1,1 4 1,4 2 1,3 4 1"}; > 175 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 176 int _ = 3; > 177 END > 178 CASE(4) > 179 int n = 20; > 180 string highways_[] = {"1 13 3,13 4 7,4 3 4,3 10 8,10 18 9,18 12 6,12 2 3 > 181 "1 17 6,17 13 6,13 9 4,9 10 8,10 7 2,7 5 5,5 19 9,1", > 182 "9 14 6,14 16 9,16 18 7,18 15 5,15 20 3,20 12 9,12 ", > 183 "8 4,8 11 3,11 4 1,4 3 7,3 2 3,20 10 2,1 18 2,16 19", > 184 " 9,4 15 9,13 15 6"}; > 185 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 186 int _ = 23; > 187 END > 188 CASE(5) > 189 int n = 30; > 190 string highways_[] = { > 191 "11 22 752,9 28 573,8 20 549,2 29 96,12 25 835,11 2", "3 443,28 > 192 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 193 int _ = 799; > 194 END > 195 /* > 196 CASE(6) > 197 int n = ; > 198 string highways_[] = ; > 199 vector <string> highways(highways_, highways_+sizeof(highways_)/sizeof > 200 int _ = ; > 201 END > 202 */ > 203 } > 204 // END CUT HERE
Added SRM/463-U/1A.cpp version [9b89d023895fe0e4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 class RabbitNumbering { public: > 24 int theCount(vector <int> maxNumber) > 25 { > 26 sort(maxNumber.begin(), maxNumber.end()); > 27 LL c = 1; > 28 for(int i=0; i<maxNumber.size(); ++i) > 29 if( maxNumber[i]-i <= 0 ) > 30 return 0; > 31 else > 32 c = (c * (maxNumber[i]-i)) % MODVAL; > 33 return c; > 34 } > 35 }; > 36 > 37 // BEGIN CUT HERE > 38 #include <ctime> > 39 double start_time; string timer() > 40 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 41 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 42 { os << "{ "; > 43 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 44 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 45 void verify_case(const int& Expected, const int& Received) { > 46 bool ok = (Expected == Received); > 47 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 48 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 49 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 50 #define END verify_case(_, RabbitNumbering().theCount(maxNumber));} > 51 int main(){ > 52 > 53 CASE(0) > 54 int maxNumber_[] = {5}; > 55 vector <int> maxNumber(maxNumber_, maxNumber_+sizeof(maxNumber_)/sizeo > 56 int _ = 5; > 57 END > 58 CASE(1) > 59 int maxNumber_[] = {4, 4, 4, 4}; > 60 vector <int> maxNumber(maxNumber_, maxNumber_+sizeof(maxNumber_)/sizeo > 61 int _ = 24; > 62 END > 63 CASE(2) > 64 int maxNumber_[] = {5, 8}; > 65 vector <int> maxNumber(maxNumber_, maxNumber_+sizeof(maxNumber_)/sizeo > 66 int _ = 35; > 67 END > 68 CASE(3) > 69 int maxNumber_[] = {2, 1, 2}; > 70 vector <int> maxNumber(maxNumber_, maxNumber_+sizeof(maxNumber_)/sizeo > 71 int _ = 0; > 72 END > 73 CASE(4) > 74 int maxNumber_[] = {25, 489, 76, 98, 704, 98, 768, 39, 697, 8, 56, 74, 3 > 75 vector <int> maxNumber(maxNumber_, maxNumber_+sizeof(maxNumber_)/sizeo > 76 int _ = 676780400; > 77 END > 78 > 79 } > 80 // END CUT HERE
Added SRM/463-U/1B.cpp version [72b87e10d5e14a67]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Nisoku { public: > 23 double theMax(vector <double> cards) > 24 { > 25 sort(cards.begin(), cards.end()); > 26 > 27 double ans = 0.0; > 28 for(int i=0; 2*i<=cards.size(); ++i) > 29 ans = max(ans, > 30 accumulate(cards.begin()+2*i, cards.end(), > 31 inner_product( cards.begin(), cards.begin()+i, > 32 cards.rend()-2*i, > 33 1.0, multiplies<double>(), plus<double>() ), > 34 multiplies<double>() > 35 ) > 36 ); > 37 return ans; > 38 } > 39 }; > 40 > 41 // BEGIN CUT HERE > 42 #include <ctime> > 43 double start_time; string timer() > 44 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 45 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 46 { os << "{ "; > 47 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 48 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 49 void verify_case(const double& Expected, const double& Received) { > 50 bool ok = (abs(Expected - Received) < 1e-9); > 51 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 52 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 53 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 54 #define END verify_case(_, Nisoku().theMax(cards));} > 55 int main(){ > 56 > 57 CASE(0) > 58 double cards_[] = {5, 8}; > 59 vector <double> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); > 60 double _ = 40.0; > 61 END > 62 CASE(1) > 63 double cards_[] = {1.5, 1.8}; > 64 vector <double> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); > 65 double _ = 3.3; > 66 END > 67 CASE(2) > 68 double cards_[] = {8.26, 7.54, 3.2567}; > 69 vector <double> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); > 70 double _ = 202.82857868; > 71 END > 72 CASE(3) > 73 double cards_[] = {1.5, 1.7, 1.6, 1.5}; > 74 vector <double> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); > 75 double _ = 9.920000000000002; > 76 END > 77 CASE(4) > 78 double cards_[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, > 79 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, > 80 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, > 81 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, > 82 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}; > 83 vector <double> cards(cards_, cards_+sizeof(cards_)/sizeof(*cards_)); > 84 double _ = 1.0E50; > 85 END > 86 > 87 } > 88 // END CUT HERE
Added SRM/463-U/1C-U.cpp version [7e42d41150443f38]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int MODVAL = 1000000007; > 23 typedef pair<pair<LL,LL>,LL> state; > 24 > 25 class RabbitPuzzle { public: > 26 int theCount(vector<long long> rabbits, vector<long long> nests, int K) > 27 { > 28 state s(make_pair(rabbits[0],rabbits[1]), rabbits[2]); > 29 state g(make_pair(nests[0],nests[1]), nests[2]); > 30 > 31 map<state,int> ms, mg; > 32 calc(s, K/2, ms); > 33 calc(g, (K+1)/2, mg); > 34 > 35 int result = 0; > 36 for(map<state,int>::iterator it=ms.begin(); it!=ms.end(); ++it) > 37 map<state,int>::iterator jt = mg.find(it->first); > 38 if( jt != mg.end() ) > 39 result = (result + LL(it->second)*jt->second)%MO > 40 } > 41 return result; > 42 } > 43 > 44 void calc(const state& s, int n, map<state,int>& m) > 45 { > 46 if( n == 0 ) > 47 (m[s] += 1) %= MODVAL; > 48 else { > 49 LL a = s.first.first; > 50 LL b = s.first.second; > 51 LL c = s.second; > 52 if( 2*b-a < c ) calc(state(make_pair(b,2*b-a),c), n-1, m > 53 calc(state(make_pair(2*a-b,a),c), n-1, m); > 54 calc(state(make_pair(a,c),2*c-b), n-1, m); > 55 if( a < 2*b-c ) calc(state(make_pair(a,2*b-c),b), n-1, m > 56 } > 57 } > 58 }; > 59 > 60 // BEGIN CUT HERE > 61 #include <ctime> > 62 double start_time; string timer() > 63 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 64 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 65 { os << "{ "; > 66 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 67 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 68 void verify_case(const int& Expected, const int& Received) { > 69 bool ok = (Expected == Received); > 70 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 71 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 72 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 73 #define END verify_case(_, RabbitPuzzle().theCount(rabbits, nests, K));} > 74 int main(){ > 75 > 76 CASE(0) > 77 long rabbits_[] = {0, 5, 8}; > 78 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 79 long nests_[] = {0, 8, 11}; > 80 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 81 int K = 1; > 82 int _ = 1; > 83 END > 84 CASE(1) > 85 long rabbits_[] = {0, 5, 8}; > 86 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 87 long nests_[] = {0, 8, 11}; > 88 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 89 int K = 3; > 90 int _ = 5; > 91 END > 92 CASE(2) > 93 long rabbits_[] = {0, 5, 8}; > 94 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 95 long nests_[] = {0, 8, 11}; > 96 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 97 int K = 2; > 98 int _ = 0; > 99 END > 100 CASE(3) > 101 long rabbits_[] = {5, 8, 58}; > 102 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 103 long nests_[] = {13, 22, 64}; > 104 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 105 int K = 58; > 106 int _ = 0; > 107 END > 108 CASE(4) > 109 long rabbits_[] = {0, 1, 2}; > 110 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 111 long nests_[] = {1, 2, 3}; > 112 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 113 int K = 100; > 114 int _ = 0; > 115 END > 116 CASE(5) > 117 long rabbits_[] = {5, 8, 58}; > 118 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 119 long nests_[] = {20, 26, 61}; > 120 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 121 int K = 58; > 122 int _ = 537851168; > 123 END > 124 CASE(6) > 125 long rabbits_[] = {67, 281, 2348}; > 126 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 127 long nests_[] = {235, 1394, 3293}; > 128 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 129 int K = 83; > 130 int _ = 167142023; > 131 END > 132 CASE(7) > 133 long rabbits_[] = {-1000000000000000000, 999999999999999998, 99999999999 > 134 vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(* > 135 long nests_[] = {-1000000000000000000, 999999999999999999, 1000000000000 > 136 vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)) > 137 int K = 5; > 138 int _ = 29; > 139 END > 140 > 141 } > 142 // END CUT HERE
Added SRM/464/1A.cpp version [22cbd7d786057de9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ColorfulStrings { public: > 23 string getKth(int n, int k) > 24 { > 25 // cannot have 11 distinct digits > 26 // if we have 0, then ... > 27 // if we have 1, then ... > 28 if(n>8) > 29 return ""; > 30 > 31 string s = (n>=2 ? "23456789" : "0123456789"); > 32 do { > 33 if( is_colorful(s,n) ) > 34 if( --k == 0 ) > 35 return s.substr(0,n); > 36 } while( next_perm(s, n>=2 ? 8-n : 10-n) ); > 37 return ""; > 38 } > 39 > 40 bool is_colorful(string& s, int n) { > 41 set<int> ps; > 42 for(int i=0; i<n; ++i) { > 43 int p = s[i]-'0'; > 44 if( !ps.insert(p).second ) > 45 return false; > 46 for(int j=i+1; j<n; ++j) { > 47 p *= s[j]-'0'; > 48 if( !ps.insert(p).second ) > 49 return false; > 50 } > 51 } > 52 return true; > 53 } > 54 > 55 int fact(int n) { > 56 return n ? n*fact(n-1) : 1; > 57 } > 58 bool next_perm(string& s, int nn) { > 59 int f = fact(nn); > 60 while(f--) > 61 if( !next_permutation(s.begin(), s.end()) ) > 62 return false; > 63 return true; > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const string& Expected, const string& Received) { > 76 bool ok = (Expected == Received); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, ColorfulStrings().getKth(n, k));} > 81 int main(){ > 82 > 83 CASE(0) > 84 int n = 3; > 85 int k = 4; > 86 string _ = "238"; > 87 END > 88 CASE(1) > 89 int n = 4; > 90 int k = 2000; > 91 string _ = ""; > 92 END > 93 CASE(2) > 94 int n = 5; > 95 int k = 1; > 96 string _ = "23457"; > 97 END > 98 CASE(3) > 99 int n = 2; > 100 int k = 22; > 101 string _ = "52"; > 102 END > 103 CASE(4) > 104 int n = 6; > 105 int k = 464; > 106 string _ = "257936"; > 107 END > 108 CASE(5) > 109 int n = 1; > 110 int k = 1; > 111 string _ = "0"; > 112 END > 113 CASE(6) > 114 int n = 50; > 115 int k = 1000000000; > 116 string _ = "???"; > 117 END > 118 CASE(7) > 119 int n = 2; > 120 int k = 57; > 121 string _ = "???"; > 122 END > 123 > 124 } > 125 // END CUT HERE
Added SRM/464/1B.cpp version [5d045ee47ad32484]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ColorfulDecoration { public: > 23 int getMaximum(vector <int> xa, vector <int> ya, vector <int> xb, vector > 24 { > 25 int L=0, R=1000000001; > 26 while( R-L>=2 ) > 27 { > 28 int C = (L+R)/2; > 29 (canPlace(C, xa, ya, xb, yb)?L:R) = C; > 30 } > 31 return L; > 32 } > 33 > 34 typedef vector< vector<int> > graph; > 35 > 36 bool canPlace(int C, vector <int> xa, vector <int> ya, vector <int> xb, > 37 { > 38 int N = xa.size(); > 39 > 40 graph G(2*N); > 41 for(int i=0; i<N; ++i) { > 42 for(int j=0; j<N; ++j) if(i!=j) > 43 if( !ok(C, xa[i], ya[i], xa[j], ya[j]) ) > 44 G[i].push_back(N+j); > 45 for(int j=0; j<N; ++j) if(i!=j) > 46 if( !ok(C, xa[i], ya[i], xb[j], yb[j]) ) > 47 G[i].push_back(j); > 48 for(int j=0; j<N; ++j) if(i!=j) > 49 if( !ok(C, xb[i], yb[i], xa[j], ya[j]) ) > 50 G[N+i].push_back(N+j); > 51 for(int j=0; j<N; ++j) if(i!=j) > 52 if( !ok(C, xb[i], yb[i], xb[j], yb[j]) ) > 53 G[N+i].push_back(j); > 54 } > 55 > 56 for(int i=0; i<N; ++i) > 57 if( reachable(G,i,N+i) && reachable(G,N+i,i) ) > 58 return false; > 59 return true; > 60 } > 61 > 62 bool reachable(graph& G, int s, int d) > 63 { > 64 set<int> V; > 65 queue<int> Q; > 66 Q.push(s); > 67 V.insert(s); > 68 while( !Q.empty() ) { > 69 int v = Q.front(); Q.pop(); > 70 for(int i=0; i<G[v].size(); ++i) { > 71 int u = G[v][i]; > 72 if( u == d ) > 73 return true; > 74 if( !V.count(u) ) > 75 V.insert(u), Q.push(u); > 76 } > 77 } > 78 return false; > 79 } > 80 > 81 bool ok(int C, int x1, int y1, int x2, int y2) > 82 { > 83 return C <= max(abs(x1-x2), abs(y1-y2)); > 84 } > 85 }; > 86 > 87 // BEGIN CUT HERE > 88 #include <ctime> > 89 double start_time; string timer() > 90 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 91 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 92 { os << "{ "; > 93 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 94 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 95 void verify_case(const int& Expected, const int& Received) { > 96 bool ok = (Expected == Received); > 97 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 98 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 99 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 100 #define END verify_case(_, ColorfulDecoration().getMaximum(xa, ya, xb, yb)) > 101 int main(){ > 102 > 103 CASE(0) > 104 int xa_[] = { 10, 0, 7 }; > 105 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 106 int ya_[] = { 0, 19, 6 }; > 107 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 108 int xb_[] = { 20, 10, 25 }; > 109 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 110 int yb_[] = { 20, 35, 25 }; > 111 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 112 int _ = 19; > 113 END > 114 CASE(1) > 115 int xa_[] = { 464, 20 }; > 116 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 117 int ya_[] = { 464, 10 }; > 118 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 119 int xb_[] = { 464, 3 }; > 120 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 121 int yb_[] = { 464, 16 }; > 122 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 123 int _ = 461; > 124 END > 125 CASE(2) > 126 int xa_[] = { 0, 0, 1, 1 }; > 127 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 128 int ya_[] = { 0, 0, 1, 1 }; > 129 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 130 int xb_[] = { 1, 1, 0, 0 }; > 131 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 132 int yb_[] = { 1, 1, 0, 0 }; > 133 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 134 int _ = 0; > 135 END > 136 CASE(3) > 137 int xa_[] = { 0, 3, 0, 5, 6 }; > 138 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 139 int ya_[] = { 1, 6, 0, 8, 5 }; > 140 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 141 int xb_[] = { 6, 1, 7, 4, 7 }; > 142 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 143 int yb_[] = { 5, 9, 2, 8, 9 }; > 144 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 145 int _ = 3; > 146 END > 147 CASE(4) > 148 int xa_[] = { 1000000000, 0 }; > 149 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 150 int ya_[] = { 0, 1000000000 }; > 151 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 152 int xb_[] = { 0, 1000000000 }; > 153 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 154 int yb_[] = { 0, 1000000000 }; > 155 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 156 int _ = 1000000000; > 157 END > 158 CASE(5) > 159 int xa_[] = {0,0}; > 160 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 161 int ya_[] = {0,0}; > 162 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 163 int xb_[] = {0,0}; > 164 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 165 int yb_[] = {0,0}; > 166 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 167 int _ = 0; > 168 END > 169 CASE(6) > 170 int xa_[] = {0,0}; > 171 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 172 int ya_[] = {0,0}; > 173 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 174 int xb_[] = {1,1}; > 175 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 176 int yb_[] = {1,1}; > 177 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 178 int _ = 1; > 179 END > 180 CASE(7) > 181 int xa_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9 > 182 vector <int> xa(xa_, xa_+sizeof(xa_)/sizeof(*xa_)); > 183 int ya_[] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0 > 184 vector <int> ya(ya_, ya_+sizeof(ya_)/sizeof(*ya_)); > 185 int xb_[] = {2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1 > 186 vector <int> xb(xb_, xb_+sizeof(xb_)/sizeof(*xb_)); > 187 int yb_[] = {3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2 > 188 vector <int> yb(yb_, yb_+sizeof(yb_)/sizeof(*yb_)); > 189 int _ = -1; > 190 END > 191 > 192 } > 193 // END CUT HERE
Added SRM/464/1C.cpp version [ea1144bd38a3e84c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ColorfulMaze { public: > 23 double getProbability(vector <string> maze, vector <int> trap) > 24 { > 25 return solve(maze, trap, 0); > 26 } > 27 > 28 typedef int vert; > 29 > 30 double solve(const vector<string>& maze, const vector<int>& trap, int da > 31 { > 32 const int H = maze.size(); > 33 const int W = maze[0].size(); > 34 > 35 // R[i] == list of cells colored 'A'+i and reachable from '$' > 36 vector<vert> R[7]; > 37 { > 38 stack<vert> Q; > 39 vector<bool> V(H*W); > 40 for(int y=0; y<H; ++y) > 41 for(int x=0; x<W; ++x) > 42 if( maze[y][x] == '$' ) > 43 {Q.push(y*W+x); V[y*W+x]=1;} > 44 > 45 while( !Q.empty() ) { > 46 vert v=Q.top(); Q.pop(); > 47 int y=v/W, x=v%W; > 48 > 49 int dy[]={-1,+1,0,0}, dx[]={0,0,-1,+1}; > 50 for(int i=0; i<4; ++i) { > 51 int yy=y+dy[i], xx=x+dx[i],u=yy*W+xx; > 52 if( 0<=yy && yy<H && 0<=xx && xx<W && ma > 53 if( maze[yy][xx]=='!' ) > 54 return 1.0; > 55 V[u]=1; > 56 if( maze[yy][xx] == '.' ) > 57 Q.push(u); > 58 else > 59 R[maze[yy][xx]-'A'].push > 60 } > 61 } > 62 } > 63 } > 64 > 65 double pMax = 0.0; > 66 for(int k=0; k<7; ++k) if(!R[k].empty()) { > 67 double p = 0; // prob. to survive when you first step in > 68 > 69 vector<string> m = maze; > 70 { // the case it was not a trap > 71 for(int y=0; y<H; ++y) > 72 for(int x=0; x<W; ++x) > 73 if(maze[y][x]=='A'+k) m[y][x] > 74 else if(maze[y][x]=='$') m[y][x] > 75 for(int i=0; i<R[k].size(); ++i) > 76 m[R[k][i]/W][R[k][i]%W] = '$'; > 77 p = (1-trap[k]/100.0) * solve(m, trap, damage); > 78 } > 79 if( damage == 0 ) { // the case it was a trap > 80 for(int y=0; y<H; ++y) > 81 for(int x=0; x<W; ++x) > 82 if(maze[y][x]=='A'+k && m[y][x]! > 83 p += (trap[k]/100.0) * solve(m, trap, damage+1); > 84 } > 85 > 86 pMax = max(pMax, p); // take the maximum > 87 } > 88 return pMax; > 89 } > 90 }; > 91 > 92 // BEGIN CUT HERE > 93 #include <ctime> > 94 double start_time; string timer() > 95 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 96 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 97 { os << "{ "; > 98 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 99 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 100 void verify_case(const double& Expected, const double& Received) { > 101 bool ok = (abs(Expected - Received) < 1e-9); > 102 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 103 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 104 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 105 #define END verify_case(_, ColorfulMaze().getProbability(maze, trap));} > 106 int main(){ > 107 > 108 CASE(0) > 109 string maze_[] = { ".$.", > 110 "A#B", > 111 "A#B", > 112 ".!." }; > 113 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 114 int trap_[] = { 50, 40, 0, 0, 0, 0, 0 }; > 115 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 116 double _ = 0.8; > 117 END > 118 CASE(1) > 119 string maze_[] = { ".$.", > 120 "A#B", > 121 "A#C", > 122 ".!." }; > 123 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 124 int trap_[] = { 20, 70, 40, 0, 0, 0, 0 }; > 125 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 126 double _ = 0.86; > 127 END > 128 CASE(2) > 129 string maze_[] = { "$A#", > 130 ".#.", > 131 "#B!" }; > 132 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 133 int trap_[] = { 10, 10, 10, 10, 10, 10, 10 }; > 134 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 135 double _ = 0.0; > 136 END > 137 CASE(3) > 138 string maze_[] = { "$A..", > 139 "##.A", > 140 "..B!" }; > 141 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 142 int trap_[] = { 50, 50, 0, 0, 0, 0, 0 }; > 143 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 144 double _ = 0.75; > 145 END > 146 CASE(4) > 147 string maze_[] = { "$C..", > 148 "##.A", > 149 "..B!" }; > 150 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 151 int trap_[] = { 50, 50, 100, 0, 0, 0, 0 }; > 152 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 153 double _ = 0.5; > 154 END > 155 CASE(5) > 156 string maze_[] = { ".$.D.E.F.G.!." }; > 157 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 158 int trap_[] = { 10, 20, 30, 40, 50, 60, 70 }; > 159 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 160 double _ = 0.23400000000000004; > 161 END > 162 CASE(6) > 163 string maze_[] = { "CC...AA", > 164 "C##.##A", > 165 "!.E.E.$", > 166 "D##.##B", > 167 "DD...BB" }; > 168 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 169 int trap_[] = { 90, 90, 25, 50, 75, 0, 0 }; > 170 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 171 double _ = 0.8125; > 172 END > 173 /* > 174 CASE(7) > 175 string maze_[] = {"#BGGCAG#B##EFEBFCBGFD#E##FA##BBF#BCADAFGG#CD#AGFAD", > 176 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 177 int trap_[] = {32, 65, 91, 81, 62, 45, 52}; > 178 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 179 double _ = 0.009185498471999998; > 180 END > 181 */ > 182 CASE(8) > 183 string maze_[] = {"#A####.F...##B..#.B.A#....#.#......#B###.#.#.####.", > 184 vector <string> maze(maze_, maze_+sizeof(maze_)/sizeof(*maze_)); > 185 int trap_[] = {14, 22, 100, 34, 15, 33, 42}; > 186 vector <int> trap(trap_, trap_+sizeof(trap_)/sizeof(*trap_)); > 187 double _ = 0.34491599999999994; > 188 END > 189 } > 190 // END CUT HERE
Added SRM/465-U/1A.cpp version [34029dc0172b7ef9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TurretPlacement { public: > 23 long long count(vector <int> x, vector <int> y) > 24 { > 25 LL cnt = 0; > 26 for(int i=0; i<x.size(); ++i) > 27 for(int j=i+1; j<x.size(); ++j) > 28 cnt += howMany(x[i], y[i], x[j], y[j]); > 29 return cnt; > 30 } > 31 LL howMany(int x, int y, int X, int Y) > 32 { > 33 LL n = int(2*sqrt((X-x)*(X-x) + (Y-y)*(Y-y))); > 34 return (n-1)*n/2; > 35 } > 36 }; > 37 > 38 // BEGIN CUT HERE > 39 #include <ctime> > 40 double start_time; string timer() > 41 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 42 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 43 { os << "{ "; > 44 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 45 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 46 void verify_case(const long long& Expected, const long long& Received) { > 47 bool ok = (Expected == Received); > 48 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 49 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 50 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 51 #define END verify_case(_, TurretPlacement().count(x, y));} > 52 int main(){ > 53 > 54 CASE(0) > 55 int x_[] = {0,2}; > 56 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 57 int y_[] = {0,2}; > 58 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 59 long long _ = 10LL; > 60 END > 61 CASE(1) > 62 int x_[] = {0,1,2}; > 63 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 64 int y_[] = {0,1,0}; > 65 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 66 long long _ = 8LL; > 67 END > 68 CASE(2) > 69 int x_[] = {1,2,3,0}; > 70 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 71 int y_[] = {-1,-5,-7,100}; > 72 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 73 long long _ = 65137LL; > 74 END > 75 CASE(3) > 76 int x_[] = {9998,-10000,10000,0}; > 77 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 78 int y_[] = {9998,10000,10000,0}; > 79 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 80 long long _ = 2799564895LL; > 81 END > 82 CASE(4) > 83 int x_[] = {0,0}; > 84 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 85 int y_[] = {1,0}; > 86 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 87 long long _ = 0LL; > 88 END > 89 CASE(5) > 90 int x_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9, > 91 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 92 int y_[] = {10000,-10000,9000,-9000,8000,-8000,-7000,-7000,6000,-6000,50 > 93 9800,- 9800,9500,-9500,8500,-8500,-7500,-7500,6 > 94 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 95 long long _ = -1LL; > 96 END > 97 > 98 } > 99 // END CUT HERE
Added SRM/465-U/1B.cpp version [93c53f4c26bd697e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int NV = 512; > 23 typedef int flow; > 24 typedef int vert; > 25 typedef vert edge; > 26 typedef vector<edge> edges; > 27 typedef vector<edges> graph; > 28 typedef flow flow_graph[NV][NV]; > 29 static const flow INF = 0x3fffffff; > 30 > 31 flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, > 32 int LV[], flow flow_in, int blocked[] ) > 33 { > 34 flow flow_out = 0; > 35 for(int i=0; i!=G[v].size(); ++i) { > 36 int u = G[v][i]; > 37 if( LV[v]+1==LV[u] && F[v][u] ) { > 38 flow f = min(flow_in-flow_out, F[v][u]); > 39 if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blo > 40 F[v][u] -= f; > 41 F[u][v] += f; > 42 flow_out += f; > 43 if( flow_in == flow_out ) return flow_out; > 44 } > 45 } > 46 } > 47 blocked[v] = (flow_out==0); > 48 return flow_out; > 49 } > 50 > 51 flow maxFlow( graph& G, flow_graph F, vert S, vert D ) > 52 { > 53 for( flow total=0 ;; ) { > 54 int LV[NV] = {0}; > 55 vector<int> Q(1, S); > 56 for(int lv=1; !Q.empty(); ++lv) { > 57 vector<int> Q2; > 58 for(int i=0; i!=Q.size(); ++i) { > 59 edges& ne = G[Q[i]]; > 60 for(int j=0; j!=ne.size(); ++j) > 61 if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j > 62 LV[ne[j]]=lv, Q2.push_back(ne[j] > 63 } > 64 Q.swap(Q2); > 65 } > 66 > 67 if( !LV[D] ) > 68 return total; > 69 > 70 int blocked[NV] = {}; > 71 total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); > 72 } > 73 } > 74 > 75 class GreenWarfare { public: > 76 int minimumEnergyCost(vector <int> canonX, vector <int> canonY, vector < > 77 { > 78 int B = baseX.size(); > 79 int P = plantX.size(); > 80 > 81 graph G(P+P+B+B+2); > 82 flow_graph F = {}; > 83 > 84 for(int x=0; x<P; ++x) { > 85 G[P+P+B+B].push_back(x); > 86 G[x].push_back(P+P+B+B); > 87 F[P+P+B+B][x] = INF; > 88 } > 89 for(int x=0; x<P; ++x) { > 90 G[x].push_back(P+x); > 91 G[P+x].push_back(x); > 92 F[x][P+x] = breakEnergy(canonX, canonY, plantX[x], plant > 93 } > 94 for(int x=0; x<P; ++x) > 95 for(int y=0; y<B; ++y) > 96 if( dist(plantX[x],plantY[x],baseX[y],baseY[y]) > 97 G[P+x].push_back(P+P+y); > 98 G[P+P+y].push_back(P+x); > 99 F[P+x][P+P+y] = INF; > 100 } > 101 for(int x=0; x<B; ++x) { > 102 G[P+P+x].push_back(P+P+B+x); > 103 G[P+P+B+x].push_back(P+P+x); > 104 F[P+P+x][P+P+B+x] = breakEnergy(canonX, canonY, baseX[x] > 105 } > 106 for(int x=0; x<B; ++x) { > 107 G[P+P+B+x].push_back(P+P+B+B+1); > 108 G[P+P+B+B+1].push_back(P+P+B+x); > 109 F[P+P+B+x][P+P+B+B+1] = INF; > 110 } > 111 > 112 return maxFlow(G, F, P+P+B+B, P+P+B+B+1); > 113 } > 114 > 115 int dist(int x, int y, int X, int Y) > 116 { > 117 return (X-x)*(X-x) + (Y-y)*(Y-y); > 118 } > 119 > 120 int breakEnergy(const vector<int>& cx, const vector<int>& cy, int x, int > 121 { > 122 int e = 0x7fffffff; > 123 for(int i=0; i<cx.size(); ++i) > 124 e = min(e, dist(x,y,cx[i],cy[i])); > 125 return e; > 126 } > 127 }; > 128 > 129 // BEGIN CUT HERE > 130 #include <ctime> > 131 double start_time; string timer() > 132 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 133 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 134 { os << "{ "; > 135 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 136 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 137 void verify_case(const int& Expected, const int& Received) { > 138 bool ok = (Expected == Received); > 139 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 140 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 141 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 142 #define END verify_case(_, GreenWarfare().minimumEnergyCost(canonX, canonY, > 143 int main(){ > 144 > 145 CASE(0) > 146 int canonX_[] = { 0 }; > 147 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 148 int canonY_[] = { 0 }; > 149 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 150 int baseX_[] = {1,2,3}; > 151 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 152 int baseY_[] = {0,0,0}; > 153 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 154 int plantX_[] = {3}; > 155 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 156 int plantY_[] = {3}; > 157 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 158 int energySupplyRadius = 4; > 159 int _ = 14; > 160 END > 161 CASE(1) > 162 int canonX_[] = { 0 }; > 163 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 164 int canonY_[] = { 0 }; > 165 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 166 int baseX_[] = {1,2,3}; > 167 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 168 int baseY_[] = {0,0,0}; > 169 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 170 int plantX_[] = {2}; > 171 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 172 int plantY_[] = {2}; > 173 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 174 int energySupplyRadius = 4; > 175 int _ = 8; > 176 END > 177 CASE(2) > 178 int canonX_[] = {3,6}; > 179 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 180 int canonY_[] = {3,6}; > 181 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 182 int baseX_[] = {1,2,3,4,5}; > 183 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 184 int baseY_[] = {5,4,2,3,1}; > 185 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 186 int plantX_[] = {1,2,5}; > 187 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 188 int plantY_[] = {1,2,5}; > 189 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 190 int energySupplyRadius = 5; > 191 int _ = 12; > 192 END > 193 CASE(3) > 194 int canonX_[] = {0}; > 195 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 196 int canonY_[] = {0}; > 197 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 198 int baseX_[] = {-10,-10,10}; > 199 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 200 int baseY_[] = {10,-10,0}; > 201 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 202 int plantX_[] = {10,10,-10}; > 203 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 204 int plantY_[] = {10,-10,0}; > 205 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 206 int energySupplyRadius = 10; > 207 int _ = 200; > 208 END > 209 CASE(4) > 210 int canonX_[] = {0}; > 211 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 212 int canonY_[] = {0}; > 213 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 214 int baseX_[] = {3}; > 215 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 216 int baseY_[] = {3}; > 217 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 218 int plantX_[] = {1,2,3}; > 219 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 220 int plantY_[] = {0,0,0}; > 221 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 222 int energySupplyRadius = 4; > 223 int _ = 14; > 224 END > 225 /* > 226 CASE(5) > 227 int canonX_[] = ; > 228 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 229 int canonY_[] = ; > 230 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 231 int baseX_[] = ; > 232 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 233 int baseY_[] = ; > 234 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 235 int plantX_[] = ; > 236 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 237 int plantY_[] = ; > 238 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 239 int energySupplyRadius = ; > 240 int _ = ; > 241 END > 242 CASE(6) > 243 int canonX_[] = ; > 244 vector <int> canonX(canonX_, canonX_+sizeof(canonX_)/sizeof(*canonX_)) > 245 int canonY_[] = ; > 246 vector <int> canonY(canonY_, canonY_+sizeof(canonY_)/sizeof(*canonY_)) > 247 int baseX_[] = ; > 248 vector <int> baseX(baseX_, baseX_+sizeof(baseX_)/sizeof(*baseX_)); > 249 int baseY_[] = ; > 250 vector <int> baseY(baseY_, baseY_+sizeof(baseY_)/sizeof(*baseY_)); > 251 int plantX_[] = ; > 252 vector <int> plantX(plantX_, plantX_+sizeof(plantX_)/sizeof(*plantX_)) > 253 int plantY_[] = ; > 254 vector <int> plantY(plantY_, plantY_+sizeof(plantY_)/sizeof(*plantY_)) > 255 int energySupplyRadius = ; > 256 int _ = ; > 257 END > 258 */ > 259 } > 260 // END CUT HERE
Added SRM/465-U/1C-U.cpp version [4ab9d94a2abea13a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BouncingDiceGame { public: > 23 double winProbability(int n, int d, int x, int y) > 24 { > 25 int B = n-d; > 26 x = min(B,x); > 27 y = min(B,y); > 28 int Z = min(x,y); > 29 vector< vector<double> > p(B+1, vector<double>(B+1, 0)); > 30 > 31 // solve: > 32 // p[B][k] = 1 - p[k][B] > 33 // p[i][k] = 1/d \Sigma_{b=1..d} (1 - p[k][min(B,i+b)]) > 34 // return p[x][y]; > 35 > 36 for(int ik=B*2; ik>=Z*2; --ik) > 37 for(int i=Z; i<=B; ++i) > 38 { > 39 int k = ik-i; > 40 if( Z<=k && k<=B ) { > 41 if(i==B && k==B) p[B][B] = d / (2*d-1); > 42 else if( i==B ) p[B][k] = 1 - p[k][B]*(d-1)/d; > 43 else { > 44 double q = 0; > 45 for(int b=1; b<=d; ++b) > 46 q += 1 - p[k][min(B,i+b)]; > 47 p[i][k] = q/d; > 48 } > 49 } > 50 } > 51 return p[x][y]; > 52 } > 53 }; > 54 > 55 // BEGIN CUT HERE > 56 #include <ctime> > 57 double start_time; string timer() > 58 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 59 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 60 { os << "{ "; > 61 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 62 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 63 void verify_case(const double& Expected, const double& Received) { > 64 bool ok = (abs(Expected - Received) < 1e-9); > 65 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 66 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 67 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 68 #define END verify_case(_, BouncingDiceGame().winProbability(n, d, x, y));} > 69 int main(){ > 70 > 71 CASE(0) > 72 int n = 10; > 73 int d = 6; > 74 int x = 1; > 75 int y = 1; > 76 double _ = 0.5417251215862328; > 77 END > 78 CASE(1) > 79 int n = 10; > 80 int d = 2; > 81 int x = 1; > 82 int y = 1; > 83 double _ = 0.6090494791666666; > 84 END > 85 CASE(2) > 86 int n = 100; > 87 int d = 20; > 88 int x = 1; > 89 int y = 10; > 90 double _ = 0.49158887163174947; > 91 END > 92 CASE(3) > 93 int n = 10; > 94 int d = 5; > 95 int x = 9; > 96 int y = 1; > 97 double _ = 0.6943018666666667; > 98 END > 99 /* > 100 CASE(4) > 101 int n = ; > 102 int d = ; > 103 int x = ; > 104 int y = ; > 105 double _ = ; > 106 END > 107 CASE(5) > 108 int n = ; > 109 int d = ; > 110 int x = ; > 111 int y = ; > 112 double _ = ; > 113 END > 114 */ > 115 } > 116 // END CUT HERE
Added SRM/466-U/1A.cpp version [b77551cfbb4fa077]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LotteryCheating { public: > 23 int minimalChange(string ID) > 24 { > 25 int minMod = ID.size(); > 26 > 27 for(LL x=0;; ++x) > 28 { > 29 string s; > 30 {stringstream ss; ss << x*x; ss >> s;} > 31 > 32 if( s.size() > ID.size() ) > 33 break; > 34 s = string(ID.size()-s.size(),'0')+s; > 35 int c = 0; > 36 for(int i=0; i<ID.size(); ++i) > 37 if( s[i] != ID[i] ) > 38 ++c; > 39 minMod = min(minMod, c); > 40 } > 41 > 42 return minMod; > 43 } > 44 }; > 45 > 46 // BEGIN CUT HERE > 47 #include <ctime> > 48 double start_time; string timer() > 49 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 50 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 51 { os << "{ "; > 52 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 53 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 54 void verify_case(const int& Expected, const int& Received) { > 55 bool ok = (Expected == Received); > 56 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 57 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 58 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 59 #define END verify_case(_, LotteryCheating().minimalChange(ID));} > 60 int main(){ > 61 > 62 CASE(0) > 63 string ID = "1"; > 64 int _ = 0; > 65 END > 66 CASE(1) > 67 string ID = "1234"; > 68 int _ = 2; > 69 END > 70 CASE(2) > 71 string ID = "9000000000"; > 72 int _ = 1; > 73 END > 74 CASE(3) > 75 string ID = "4294967296"; > 76 int _ = 0; > 77 END > 78 CASE(4) > 79 string ID = "7654321"; > 80 int _ = 3; > 81 END > 82 CASE(5) > 83 string ID = "2"; > 84 int _ = 1; > 85 END > 86 CASE(6) > 87 string ID = "4"; > 88 int _ = 0; > 89 END > 90 CASE(7) > 91 string ID = "8"; > 92 int _ = 1; > 93 END > 94 CASE(8) > 95 string ID = "1111111111"; > 96 int _ = -1; > 97 END > 98 CASE(9) > 99 string ID = "00201"; > 100 int _ = 1; > 101 END > 102 > 103 } > 104 // END CUT HERE
Added SRM/466-U/1B.cpp version [7c6bd3238ef8c0b3]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LotteryPyaterochka { public: > 23 double chanceToWin(int N_) > 24 { > 25 LL N = N_; > 26 LL n5 = N; > 27 LL n4 = N*5*(N-1)*5; > 28 LL n32 = N*10*(N-1)*10; > 29 LL n311 = N*10*((N-1)*(N-2)/2)*5*5; > 30 LL all = (5*N)*(5*N-1)*(5*N-2)*(5*N-3)*(5*N-4)/120; > 31 return double(n5+n4+n32+n311) / all; > 32 } > 33 }; > 34 > 35 // BEGIN CUT HERE > 36 #include <ctime> > 37 double start_time; string timer() > 38 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 39 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 40 { os << "{ "; > 41 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 42 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 43 void verify_case(const double& Expected, const double& Received) { > 44 bool ok = (abs(Expected - Received) < 1e-9); > 45 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 46 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 47 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 48 #define END verify_case(_, LotteryPyaterochka().chanceToWin(N));} > 49 int main(){ > 50 > 51 CASE(0) > 52 int N = 1; > 53 double _ = 1.0; > 54 END > 55 CASE(1) > 56 int N = 2; > 57 double _ = 1.0; > 58 END > 59 CASE(2) > 60 int N = 3; > 61 double _ = 0.5004995004995004; > 62 END > 63 CASE(3) > 64 int N = 6; > 65 double _ = 0.13161551092585574; > 66 END > 67 CASE(4) > 68 int N = 99; > 69 double _ = -1; > 70 END > 71 CASE(5) > 72 int N = 100; > 73 double _ = -1; > 74 END > 75 > 76 } > 77 // END CUT HERE
Added SRM/466-U/1C-U.cpp version [c596015123288fc0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 > 24 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 25 LL FACT(LL x) {return x<=1 ? 1 : MUL(x, FACT(x-1));} > 26 > 27 class DrawingBlackCrosses { public: > 28 map<int, LL> memo; > 29 LL rec(int must, int mustNot, int i, vector<int>& mask, int full) > 30 { > 31 int key = mustNot*20+i; > 32 if( memo.count(key) ) > 33 return memo[key]; > 34 > 35 if( i == mask.size() ) { > 36 if( must==0 ) { > 37 int b = 0; > 38 for(int i=0; (1<<i)<=mustNot; ++i) > 39 if( mustNot & (1<<i) ) > 40 ++b; > 41 return FACT(b); > 42 } > 43 else > 44 return 0; > 45 } > 46 > 47 LL cnt = 0; > 48 > 49 // select this row > 50 int seled = mustNot | mask[i]; > 51 int notDone = full &~ seled; > 52 for(int k=0; (1<<k)<=notDone; ++k) > 53 if( (1<<k) & notDone ) { > 54 LL c = rec(must&~(1<<k), mustNot|(1<<k), i+1, ma > 55 cnt = (cnt + c) % MODVAL; > 56 } > 57 > 58 // don't select > 59 LL c = rec(must|notDone, mustNot, i+1, mask, full); > 60 cnt = (cnt + c) % MODVAL; > 61 return memo[key] = cnt; > 62 } > 63 int count(vector <string> field) > 64 { > 65 vector<int> mask; > 66 for(int i=0; i<field.size(); ++i) { > 67 int m = 0; > 68 for(int j=0; j<field[i].size(); ++j) > 69 m |= (field[i][j]=='B')<<j; > 70 mask.push_back(m); > 71 } > 72 int full = (1<<field[0].size()) - 1; > 73 return rec(0, 0, 0, mask, full); > 74 } > 75 }; > 76 > 77 // BEGIN CUT HERE > 78 #include <ctime> > 79 double start_time; string timer() > 80 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 81 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 82 { os << "{ "; > 83 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 84 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 85 void verify_case(const int& Expected, const int& Received) { > 86 bool ok = (Expected == Received); > 87 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 88 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 89 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 90 #define END verify_case(_, DrawingBlackCrosses().count(field));} > 91 int main(){ > 92 > 93 CASE(0) > 94 string field_[] = {"."}; > 95 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 96 int _ = 1; > 97 END > 98 CASE(1) > 99 string field_[] = {"BBB", > 100 "BBB"}; > 101 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 102 int _ = 1; > 103 END > 104 CASE(2) > 105 string field_[] = {"...", > 106 "BB."}; > 107 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 108 int _ = 5; > 109 END > 110 CASE(3) > 111 string field_[] = {"....................", > 112 "....................", > 113 "....................", > 114 "....................", > 115 "....................", > 116 "....................", > 117 "....................", > 118 "....................", > 119 "....................", > 120 "....................", > 121 "....................", > 122 "....................", > 123 "....................", > 124 "....................", > 125 "....................", > 126 "....................", > 127 "....................", > 128 "....................", > 129 "....................", > 130 "...................."}; > 131 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 132 int _ = 563200757; > 133 END > 134 CASE(4) > 135 string field_[] = {"B..B", > 136 "B.B.", > 137 "...B", > 138 "BB.B", > 139 "...."}; > 140 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 141 int _ = 324; > 142 END > 143 /* > 144 CASE(5) > 145 string field_[] = ; > 146 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 147 int _ = ; > 148 END > 149 CASE(6) > 150 string field_[] = ; > 151 vector <string> field(field_, field_+sizeof(field_)/sizeof(*field_)); > 152 int _ = ; > 153 END > 154 */ > 155 } > 156 // END CUT HERE
Added SRM/467-U/1A.cpp version [0013ea53e2a54780]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LateProfessor { public: > 23 double getProbability(int waitTime, int walkTime, int lateTime, int best > 24 { > 25 if( worstArrival - bestArrival == 0 ) > 26 { > 27 int t = bestArrival % (waitTime+walkTime); > 28 if( t<=waitTime ) > 29 return 0; > 30 if( t+lateTime <= waitTime+walkTime ) > 31 return 1; > 32 return 0; > 33 } > 34 > 35 if( walkTime <= lateTime ) > 36 return 0; > 37 > 38 int bad = 0; > 39 for(int t=0; t<=worstArrival; t+=waitTime+walkTime) > 40 { > 41 int tb = max(bestArrival, t+waitTime); > 42 int te = min(worstArrival, t+waitTime+walkTime-lateTime) > 43 if( tb < te ) > 44 bad += te - tb; > 45 } > 46 return double(bad) / (worstArrival - bestArrival); > 47 } > 48 }; > 49 > 50 // BEGIN CUT HERE > 51 #include <ctime> > 52 double start_time; string timer() > 53 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 54 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 55 { os << "{ "; > 56 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 57 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 58 void verify_case(const double& Expected, const double& Received) { > 59 bool ok = (abs(Expected - Received) < 1e-9); > 60 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 61 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 62 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 63 #define END verify_case(_, LateProfessor().getProbability(waitTime, walkTim > 64 int main(){ > 65 > 66 CASE(0) > 67 int waitTime = 20; > 68 int walkTime = 30; > 69 int lateTime = 10; > 70 int bestArrival = 0; > 71 int worstArrival = 50; > 72 double _ = 0.4; > 73 END > 74 CASE(1) > 75 int waitTime = 20; > 76 int walkTime = 30; > 77 int lateTime = 10; > 78 int bestArrival = 30; > 79 int worstArrival = 100; > 80 double _ = 0.42857142857142855; > 81 END > 82 CASE(2) > 83 int waitTime = 20; > 84 int walkTime = 40; > 85 int lateTime = 50; > 86 int bestArrival = 0; > 87 int worstArrival = 300; > 88 double _ = 0.0; > 89 END > 90 CASE(3) > 91 int waitTime = 101; > 92 int walkTime = 230; > 93 int lateTime = 10; > 94 int bestArrival = 654; > 95 int worstArrival = 17890; > 96 double _ = 0.6637270828498492; > 97 END > 98 CASE(4) > 99 int waitTime = 20; > 100 int walkTime = 30; > 101 int lateTime = 10; > 102 int bestArrival = 90; > 103 int worstArrival = 90; > 104 double _ = 1.0; > 105 END > 106 CASE(5) > 107 int waitTime = 1000; > 108 int walkTime = 600; > 109 int lateTime = 1; > 110 int bestArrival = 17000; > 111 int worstArrival = 17000; > 112 double _ = 0.0; > 113 END > 114 CASE(6) > 115 int waitTime = 1; > 116 int walkTime = 2; > 117 int lateTime = 1; > 118 int bestArrival = 2; > 119 int worstArrival = 2; > 120 double _ = -1; > 121 END > 122 CASE(7) > 123 int waitTime = 1000; > 124 int walkTime = 1000; > 125 int lateTime = 1000; > 126 int bestArrival = 0; > 127 int worstArrival = 10000000; > 128 double _ = -1; > 129 END > 130 > 131 } > 132 // END CUT HERE
Added SRM/467-U/1B.cpp version [b74c3219548cc710]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 > 24 > 25 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 26 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 27 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 28 LL POW(LL x, LL e) { > 29 LL v = 1; > 30 for(;e;x=MUL(x,x),e>>=1) > 31 if(e&1) > 32 v = MUL(v, x); > 33 return v; > 34 } > 35 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 36 > 37 LL C(LL n, LL k) { > 38 LL v = 1; > 39 for(LL i=1; i<=k; ++i) > 40 v = DIV(MUL(v, n-i+1), i); > 41 return v; > 42 } > 43 > 44 class SuperSum { public: > 45 int calculate(int k, int n) > 46 { > 47 return C(n+k, k+1); > 48 } > 49 }; > 50 > 51 // BEGIN CUT HERE > 52 #include <ctime> > 53 double start_time; string timer() > 54 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 55 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 56 { os << "{ "; > 57 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 58 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 59 void verify_case(const int& Expected, const int& Received) { > 60 bool ok = (Expected == Received); > 61 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 62 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 63 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 64 #define END verify_case(_, SuperSum().calculate(k, n));} > 65 int main(){ > 66 > 67 CASE(0) > 68 int k = 1; > 69 int n = 3; > 70 int _ = 6; > 71 END > 72 CASE(1) > 73 int k = 2; > 74 int n = 3; > 75 int _ = 10; > 76 END > 77 CASE(2) > 78 int k = 4; > 79 int n = 10; > 80 int _ = 2002; > 81 END > 82 CASE(3) > 83 int k = 10; > 84 int n = 35; > 85 int _ = 150595840; > 86 END > 87 CASE(4) > 88 int k = 1; > 89 int n = 1; > 90 int _ = 1; > 91 END > 92 CASE(5) > 93 int k = 50; > 94 int n = 1000000000; > 95 int _ = -1; > 96 END > 97 CASE(6) > 98 int k = 50; > 99 int n = 900000000; > 100 int _ = -1; > 101 END > 102 > 103 } > 104 // END CUT HERE
Added SRM/467-U/1C-U.cpp version [02fca67c4fcabadb]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NextHomogeneousStrings { public: > 23 string getNext(int d, int n, string seed, long long k) > 24 { > 25 int M = seed.size(); > 26 > 27 LL L=0, R=1; // [L,R) > 28 while( R-L > 2 ) { > 29 LL C = (R+L)/2; > 30 string t = xth(d,n,C,M); > 31 (t=="" || seed<t ? R : L) = C; > 32 } > 33 return seed==xth(d,n,L,M) ? xth(d,n,L+k-1,M) : xth(d,n,L+k,M); > 34 } > 35 string xth(int d, int n, LL x, int M) > 36 { > 37 return rec( string(), x, d, n, M ); > 38 } > 39 string rec( string hist, LL x, int d, int n, int M ) > 40 { > 41 if( M == 0 ) > 42 return ""; > 43 for(char c='a'; c<='z'; ++c) > 44 { > 45 string hh = hist + c; > 46 // todo: normalize > 47 if( x < cnt(hh,d,n,M) ) > 48 return c + rec(hist+c, x, d, n, M-1); > 49 x -= cnt(hh,d,n,M); > 50 } > 51 return ""; > 52 } > 53 > 54 LL cnt(string hist, int d, int n, int M) > 55 { > 56 if( M == 0 ) > 57 return 1; > 58 > 59 // todo: memoize > 60 > 61 LL ans = 0; > 62 for(char c='a'; c<='z'; ++c) > 63 { > 64 char cc = c; > 65 if( find(hist.begin(), hist.end(), c)==hist.end() ) > 66 cc = *max_element(hist.begin(),hist.end()) + 1; > 67 string h2 = hist+cc; > 68 // todo: check violation > 69 // todo: drop 1st (if needed) > 70 // todo: normalize > 71 //ans += cnt(h2, dd, nn, M-1); > 72 } > 73 return ans; > 74 } > 75 }; > 76 > 77 // BEGIN CUT HERE > 78 #include <ctime> > 79 double start_time; string timer() > 80 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 81 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 82 { os << "{ "; > 83 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 84 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 85 void verify_case(const string& Expected, const string& Received) { > 86 bool ok = (Expected == Received); > 87 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 88 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 89 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 90 #define END verify_case(_, NextHomogeneousStrings().getNext(d, n, seed, k)) > 91 int main(){ > 92 > 93 CASE(0) > 94 int d = 1; > 95 int n = 2; > 96 string seed = "aaa"; > 97 long long k = 3LL; > 98 string _ = "ddd"; > 99 END > 100 CASE(1) > 101 int d = 2; > 102 int n = 3; > 103 string seed = "abc"; > 104 long long k = 0LL; > 105 string _ = "aca"; > 106 END > 107 CASE(2) > 108 int d = 2; > 109 int n = 4; > 110 string seed = "ttrrzz"; > 111 long long k = 6LL; > 112 string _ = "ttsssc"; > 113 END > 114 CASE(3) > 115 int d = 6; > 116 int n = 8; > 117 string seed = "txyaaxaassaaaarghjsdohasdghususdidisisdodo"; > 118 long long k = 10000000000000000LL; > 119 string _ = "txyaaxaassaaaarghjsgaaaaaaaaadntffiniqrddy"; > 120 END > 121 CASE(4) > 122 int d = 2; > 123 int n = 5; > 124 string seed = "zzzzzaa"; > 125 long long k = 100LL; > 126 string _ = ""; > 127 END > 128 /* > 129 CASE(5) > 130 int d = ; > 131 int n = ; > 132 string seed = ; > 133 long long k = LL; > 134 string _ = ; > 135 END > 136 CASE(6) > 137 int d = ; > 138 int n = ; > 139 string seed = ; > 140 long long k = LL; > 141 string _ = ; > 142 END > 143 */ > 144 } > 145 // END CUT HERE
Added SRM/468/1A.cpp version [bb06222c7a60f7be]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class T9 { public: > 23 string message(vector <string> part, vector <string> dict, vector <strin > 24 { > 25 string key; > 26 for(int i=0; i<keystr.size(); ++i) > 27 key += keystr[i]; > 28 > 29 sort(dict.begin(), dict.end()); > 30 > 31 string answer; > 32 > 33 const char* p = key.c_str(); > 34 while( *p ) > 35 { > 36 if( *p == '0' ) { > 37 answer += ' '; > 38 ++p; > 39 } else { > 40 vector<int> alp; > 41 while( '1' <= *p && *p <= '9' ) > 42 alp.push_back( *p++ - '1' ); > 43 int cnt = 0; > 44 while( *p=='*' || *p=='#' ) > 45 cnt += (*p++=='*' ? 5 : 1); > 46 answer += decode(part, dict, alp, cnt); > 47 } > 48 } > 49 return answer; > 50 } > 51 > 52 string decode(const vector<string>& part, const vector<string>& dict, co > 53 { > 54 for(int i=0; i<dict.size(); ++i) > 55 if( match(dict[i], part, alp) ) > 56 if( cnt-- == 0 ) > 57 return dict[i]; > 58 assert(false); > 59 } > 60 > 61 bool match(const string& d, const vector<string>& part, const vector<int > 62 { > 63 if( d.size() != alp.size() ) > 64 return false; > 65 > 66 for(int i=0; i<d.size(); ++i) > 67 if( part[alp[i]].find(d[i]) == string::npos ) > 68 return false; > 69 return true; > 70 } > 71 }; > 72 > 73 // BEGIN CUT HERE > 74 #include <ctime> > 75 double start_time; string timer() > 76 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 77 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 78 { os << "{ "; > 79 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 80 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 81 void verify_case(const string& Expected, const string& Received) { > 82 bool ok = (Expected == Received); > 83 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 84 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 85 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 86 #define END verify_case(_, T9().message(part, dict, keystr));} > 87 int main(){ > 88 > 89 CASE(0) > 90 string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", > 91 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 92 string dict_[] = {"bad"}; > 93 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 94 string keystr_[] = {"2230223"}; > 95 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 96 string _ = "bad bad"; > 97 END > 98 CASE(1) > 99 string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", > 100 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 101 string dict_[] = {"the", "tie"}; > 102 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 103 string keystr_[] = {"0843#000843#000"}; > 104 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 105 string _ = " tie tie "; > 106 END > 107 CASE(2) > 108 string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", > 109 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 110 string dict_[] = {"bad", "ace", "aad", "aae", "aaf", "acf", "acd", "the" > 111 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 112 string keystr_[] = {"223#02", "23*#00843#0"}; > 113 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 114 string _ = "aae bad tie "; > 115 END > 116 CASE(3) > 117 string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", > 118 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 119 string dict_[] = {"the", "tie", "bad", "ace", "aad", "aae", "aaf", "acf" > 120 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 121 string keystr_[] = {"84300223#02", "23#*"}; > 122 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 123 string _ = "the aae bad"; > 124 END > 125 CASE(4) > 126 string part_[] = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", > 127 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 128 string dict_[] = {"bad", "ace", "aad", "aae", "tie", "aaf", "acf", "acd" > 129 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 130 string keystr_[] = {"223#02", "23######"}; > 131 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 132 string _ = "aae bad"; > 133 END > 134 CASE(5) > 135 string part_[] = {"", "rq", "lde", "yoauz", "cbfgn", "tjkpx", "wvs", "ih > 136 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 137 string dict_[] = {"xktgmfmoqlmivm", > 138 "hmthr", > 139 "tpjgmnmaremiwm", > 140 "tpjcmnmyrlmhvm", > 141 "xkpnmgmzqdmhsm", > 142 "wqopvvmiig", > 143 "melbcbqeeg", > 144 "jkxnmbmardmhwm", > 145 "kpxnmcmyqlmism", > 146 "wrztvsmhhf", > 147 "srztssmiic", > 148 "pxtgmfmyrdmhwm", > 149 "vqoxswmiin", > 150 "wryksvmihb", > 151 "ptjfmbmoremhvm"}; > 152 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 153 string keystr_[] = {"00", > 154 "7246779885##00000089682000007246779885##0000724677", > 155 "9885#000089682000093355523350066659594239879###000"}; > 156 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 157 string _ = " wqopvvmiig hmthr wqopvvmiig vqoxswmiin hmth > 158 END > 159 /* > 160 CASE(6) > 161 string part_[] = ; > 162 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 163 string dict_[] = ; > 164 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 165 string keystr_[] = ; > 166 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 167 string _ = ; > 168 END > 169 CASE(7) > 170 string part_[] = ; > 171 vector <string> part(part_, part_+sizeof(part_)/sizeof(*part_)); > 172 string dict_[] = ; > 173 vector <string> dict(dict_, dict_+sizeof(dict_)/sizeof(*dict_)); > 174 string keystr_[] = ; > 175 vector <string> keystr(keystr_, keystr_+sizeof(keystr_)/sizeof(*keystr > 176 string _ = ; > 177 END > 178 */ > 179 } > 180 // END CUT HERE
Added SRM/468/1B.cpp version [9574f58423d94466]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP3x > 24 { > 25 int N1, N2, N3; > 26 vector<T> data; > 27 DP3x(int, int N2, int N3, const T& t = T()) > 28 : N1(2), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()* > 29 T& operator()(int i1, int i2, int i3) > 30 { i1&=1; return data[ ((i1*N2)+i2)*N3+i3 ]; } > 31 void swap(DP3x& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 static const LL inf = (1LL << 60); > 36 > 37 class RoadOrFlightHard { public: > 38 long long minTime(int N, int roadFirst, int roadProd, int roadAdd, int r > 39 { > 40 // input > 41 vector<LL> rt(N), ft(N); > 42 { > 43 rt[0] = roadFirst % roadMod; > 44 for(int i=1; i<N; ++i) > 45 rt[i] = (LL(rt[i-1])*roadProd + roadAdd) % roadM > 46 } > 47 { > 48 ft[0] = flightFirst % flightMod; > 49 for(int i=1; i<N; ++i) > 50 ft[i] = (LL(ft[i-1])*flightProd + flightAdd) % f > 51 } > 52 > 53 DP3x<LL> dp(N+1, K+1, 2); > 54 for(int n=0; n<=N; ++n) > 55 for(int k=0; k<=K; ++k) > 56 if( n == 0 ) { > 57 dp(n,k,0) = 0; > 58 dp(n,k,1) = inf; > 59 } > 60 else { > 61 dp(n,k,0) = min(dp(n-1,k,0), dp(n-1,k,1)) + rt[n > 62 if( k == 0 ) > 63 dp(n,k,1) = inf; > 64 else > 65 dp(n,k,1) = min(dp(n-1,k,1), dp(n-1,k-1, > 66 } > 67 > 68 LL ans = inf; > 69 for(int k=0; k<=K; ++k) > 70 ans = min(ans, min(dp(N,k,0), dp(N,k,1))); > 71 return ans; > 72 } > 73 }; > 74 > 75 // BEGIN CUT HERE > 76 #include <ctime> > 77 double start_time; string timer() > 78 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 79 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 80 { os << "{ "; > 81 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 82 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 83 void verify_case(const long long& Expected, const long long& Received) { > 84 bool ok = (Expected == Received); > 85 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 86 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 87 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 88 #define END verify_case(_, RoadOrFlightHard().minTime(N, roadFirst, roadPro > 89 int main(){ > 90 > 91 CASE(0) > 92 int N = 3; > 93 int roadFirst = 14; > 94 int roadProd = 1; > 95 int roadAdd = 2; > 96 int roadMod = 10; > 97 int flightFirst = 18; > 98 int flightProd = 1; > 99 int flightAdd = 10; > 100 int flightMod = 17; > 101 int K = 1; > 102 long long _ = 14LL; > 103 END > 104 CASE(1) > 105 int N = 3; > 106 int roadFirst = 4; > 107 int roadProd = 1; > 108 int roadAdd = 2; > 109 int roadMod = 10; > 110 int flightFirst = 1; > 111 int flightProd = 1; > 112 int flightAdd = 10; > 113 int flightMod = 17; > 114 int K = 2; > 115 long long _ = 11LL; > 116 END > 117 CASE(2) > 118 int N = 3; > 119 int roadFirst = 4; > 120 int roadProd = 1; > 121 int roadAdd = 2; > 122 int roadMod = 10; > 123 int flightFirst = 1; > 124 int flightProd = 1; > 125 int flightAdd = 6; > 126 int flightMod = 9; > 127 int K = 1; > 128 long long _ = 12LL; > 129 END > 130 CASE(3) > 131 int N = 5; > 132 int roadFirst = 85739; > 133 int roadProd = 94847; > 134 int roadAdd = 93893; > 135 int roadMod = 98392; > 136 int flightFirst = 92840; > 137 int flightProd = 93802; > 138 int flightAdd = 93830; > 139 int flightMod = 92790; > 140 int K = 3; > 141 long long _ = 122365LL; > 142 END > 143 CASE(4) > 144 int N = 1; > 145 int roadFirst = 2; > 146 int roadProd = 1; > 147 int roadAdd = 1; > 148 int roadMod = 100; > 149 int flightFirst = 1; > 150 int flightProd = 1; > 151 int flightAdd = 1; > 152 int flightMod = 2; > 153 int K = 0; > 154 long long _ = 2LL; > 155 END > 156 CASE(4) > 157 int N = 1; > 158 int roadFirst = 2; > 159 int roadProd = 1; > 160 int roadAdd = 1; > 161 int roadMod = 100; > 162 int flightFirst = 1; > 163 int flightProd = 1; > 164 int flightAdd = 1; > 165 int flightMod = 2; > 166 int K = 1; > 167 long long _ = 1LL; > 168 END > 169 CASE(5) > 170 int N = 1; > 171 int roadFirst = 0; > 172 int roadProd = 1; > 173 int roadAdd = 1; > 174 int roadMod = 100; > 175 int flightFirst = 0; > 176 int flightProd = 1; > 177 int flightAdd = 1; > 178 int flightMod = 1; > 179 int K = 1; > 180 long long _ = 0LL; > 181 END > 182 CASE(6) > 183 int N = 400000; > 184 int roadFirst = 1234; > 185 int roadProd = 56789; > 186 int roadAdd = 32323; > 187 int roadMod = 99999; > 188 int flightFirst = 9876; > 189 int flightProd = 44344; > 190 int flightAdd = 7865; > 191 int flightMod = 99999; > 192 int K = 40; > 193 long long _ = -1LL; > 194 END > 195 > 196 } > 197 // END CUT HERE
Added SRM/468/1C.cpp version [ef7e420ef333d5ce]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef int vert; > 23 typedef vert edge; > 24 typedef vector<edge> edges; > 25 typedef vector<edges> graph; > 26 > 27 bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) > 28 { > 29 for(int i=0; i<G[v].size(); ++i) { > 30 vert u = G[v][i]; > 31 if( visited[u] ) continue; > 32 visited[u] = true; > 33 > 34 if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) > 35 { matchTo[v]=u, matchTo[u]=v; return true; } > 36 } > 37 return false; > 38 } > 39 > 40 template<int NV> > 41 int biMatch( graph& G, int L ) // [0,L):left, [L,?):right > 42 { > 43 vector<vert> matchTo(G.size(), -1); > 44 int ans = 0; > 45 for(vert v=0; v<L; ++v) { > 46 bool visited[NV] = {}; > 47 if( augment(G, v, matchTo, visited) ) > 48 ++ans; > 49 } > 50 return ans; > 51 } > 52 > 53 int bitcnt(LL x) > 54 { > 55 int c = 0; > 56 for(; x; x>>=1) > 57 c += x&1; > 58 return c; > 59 } > 60 > 61 class MallSecurity { public: > 62 pair<graph, int> generateBipartiteGraph( > 63 const vector<int>& As, const vector<int>& Bs, const vector<int>& > 64 const vector<int>& Ks, int msf, int mask ) > 65 { > 66 // construct a bipartite graph representation, in which > 67 // - "even"-floor stations go to the left > 68 // - "odd"-floor stations go to the right > 69 // here floor number is normalized by rotating the msf-th floor > 70 #define LR(f) (((f)>msf ? (f)-msf : Ks.size()-msf+(f))%2) > 71 > 72 // assign interger-ID for each "choosable" station, i.e., > 73 // a station which is not at msf and is not connected to a 'mask > 74 map<pair<int,int>,int> ID; > 75 int nextID[2] = {0,0}; // use different sets of IDs for even/odd > 76 { > 77 for(int f=0; f<Ks.size(); ++f) if( f != msf ) > 78 for(int i=0; i<Ks[f]; ++i) > 79 { > 80 bool can_use = true; > 81 for(int j=0; j<As.size(); ++j) > 82 if( Cs[j]==f && As[j]==i && Ds[j]==msf & > 83 || Ds[j]==f && Bs[j]==i && Cs[j]==msf & > 84 can_use = false; > 85 if( can_use ) > 86 ID[make_pair(f,i)] = nextID[LR(f)]++; > 87 } > 88 } > 89 > 90 // then create the graph > 91 graph G( nextID[0] + nextID[1] ); > 92 for(int j=0; j<As.size(); ++j) > 93 { > 94 pair<int,int> p(Cs[j], As[j]); > 95 pair<int,int> q(Ds[j], Bs[j]); > 96 if( ID.count(p) && ID.count(q) ) > 97 { > 98 int pid = ID[p] + (LR(Cs[j]) ? nextID[0] : 0); > 99 int qid = ID[q] + (LR(Ds[j]) ? nextID[0] : 0); > 100 G[pid].push_back(qid); > 101 G[qid].push_back(pid); > 102 } > 103 } > 104 return make_pair(G, nextID[0]); > 105 } > 106 > 107 int maxGuards(int N, vector <string> escDescription) > 108 { > 109 // parse the input > 110 vector<int> As, Bs, Cs, Ds, Ks(N); > 111 { > 112 stringstream sin; > 113 copy( escDescription.begin(), escDescription.end(), ostr > 114 > 115 for(int A,B,C; sin>>A>>B>>C; ) > 116 { > 117 {char comma; sin>>comma;} > 118 --A, --B, --C; int D=(C+1)%N; // to 0-orign > 119 As.push_back(A); > 120 Bs.push_back(B); > 121 Cs.push_back(C); > 122 Ds.push_back(D); > 123 Ks[C] = max(Ks[C], A+1); > 124 Ks[D] = max(Ks[D], B+1); > 125 } > 126 } > 127 > 128 // the most simple floor, where the number Ks[msf] of stations i > 129 int msf = min_element(Ks.begin(), Ks.end()) - Ks.begin(); > 130 > 131 // try all possibilities at the msf-th floor > 132 size_t answer = 0; > 133 for(int mask=0; mask<(1<<Ks[msf]); ++mask) > 134 { > 135 // then the rest of the part becomes a bipartite graph > 136 pair<graph, int> G = generateBipartiteGraph(As,Bs,Cs,Ds, > 137 > 138 // |MaxIndenepdentSet| == |V|-|MinCover| =(for bipartite > 139 answer = max(answer, G.first.size() - biMatch<100>(G.fir > 140 } > 141 return answer; > 142 } > 143 }; > 144 > 145 // BEGIN CUT HERE > 146 #include <ctime> > 147 double start_time; string timer() > 148 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 149 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 150 { os << "{ "; > 151 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 152 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 153 void verify_case(const int& Expected, const int& Received) { > 154 bool ok = (Expected == Received); > 155 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 156 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 157 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 158 #define END verify_case(_, MallSecurity().maxGuards(N, escDescription));} > 159 int main(){ > 160 > 161 CASE(0) > 162 int N = 10; > 163 string escDescription_[] = {"1 1 1,1 1 2,1 1 3,1 1 4,1 1 5,1 1 6,1 1 7,1 > 164 "1 9,1 1 10"}; > 165 vector <string> escDescription(escDescription_, escDescription_+sizeof > 166 int _ = 5; > 167 END > 168 CASE(1) > 169 int N = 11; > 170 string escDescription_[] = {"1 1 1,1 1 2,1 1 3,1 1 4,1 1 5,1 1 6,1 1 7,1 > 171 "1 9,1 1 10"}; > 172 vector <string> escDescription(escDescription_, escDescription_+sizeof > 173 int _ = 6; > 174 END > 175 CASE(2) > 176 int N = 10; > 177 string escDescription_[] = {"1 1 7,1 2 9,2 1", > 178 " 8,1 2 6,1 1 8,1 2 3,1 2 2,2 ", > 179 "2 4,1 1 1,2 1 2,3 2 3,1 1 5,2 1 1,4 ", > 180 "1 7,1 1 10,3 2 5,1 2 5,3 3 1,", > 181 "3 2 8,3 1 2,1 1 3,4 4 2,2", > 182 " 4 6,4 2 5,2 3 3,6 4 1,5 2 8,1 3 6,1 3 7,", > 183 "4 3 8,1 3 8,5 2 3,4 2 8,2 6 7,1 3 9,", > 184 "1 1 4,6 1 1,2 3 1,5 1 5,6 1 8,5 ", > 185 "2 2,3 2 10,3 3 9,1 5 2,4 1 1,1 5 10"}; > 186 vector <string> escDescription(escDescription_, escDescription_+sizeof > 187 int _ = 25; > 188 END > 189 CASE(3) > 190 int N = 13; > 191 string escDescription_[] = {"5 1 11,2 3 9,3 7 10,5 2 11,5 5 8,7 5 6,5 2 > 192 vector <string> escDescription(escDescription_, escDescription_+sizeof > 193 int _ = 50; > 194 END > 195 /* > 196 CASE(4) > 197 int N = ; > 198 string escDescription_[] = ; > 199 vector <string> escDescription(escDescription_, escDescription_+sizeof > 200 int _ = ; > 201 END > 202 */ > 203 } > 204 // END CUT HERE
Added SRM/469/1A.cpp version [d06567508a7c0830]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheMoviesLevelOneDivOne { public: > 23 long long find(int n, int m, vector <int> row, vector <int> seat) > 24 { > 25 LL all = LL(n)*LL(m-1); > 26 > 27 set< pair<int,int> > dead; > 28 for(int i=0; i<row.size(); ++i) > 29 { > 30 int y = row[i]-1; > 31 int x = seat[i]-1; > 32 if(x<m-1)dead.insert( make_pair(y,x) ); > 33 if(x>0) dead.insert( make_pair(y,x-1) ); > 34 } > 35 return all - dead.size(); > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const long long& Expected, const long long& Received) { > 48 bool ok = (Expected == Received); > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, TheMoviesLevelOneDivOne().find(n, m, row, seat)) > 53 int main(){ > 54 > 55 CASE(0) > 56 int n = 2; > 57 int m = 3; > 58 int row_[] = {1, 2}; > 59 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 60 int seat_[] = {2, 3}; > 61 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 62 long long _ = 1LL; > 63 END > 64 CASE(1) > 65 int n = 2; > 66 int m = 3; > 67 int row_[] = {1, 1, 1, 2, 2, 2}; > 68 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 69 int seat_[] = {1, 2, 3, 1, 2, 3}; > 70 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 71 long long _ = 0LL; > 72 END > 73 CASE(2) > 74 int n = 4; > 75 int m = 7; > 76 int row_[] = {1}; > 77 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 78 int seat_[] = {1}; > 79 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 80 long long _ = 23LL; > 81 END > 82 CASE(3) > 83 int n = 10; > 84 int m = 8; > 85 int row_[] = {1, 9, 6, 10, 6, 7, 9, 3, 9, 2}; > 86 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 87 int seat_[] = {7, 7, 3, 3, 7, 1, 5, 1, 6, 2}; > 88 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 89 long long _ = 54LL; > 90 END > 91 CASE(4) > 92 int n = 1; > 93 int m = 1; > 94 int row_[] = {1}; > 95 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 96 int seat_[] = {1}; > 97 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 98 long long _ = 0LL; > 99 END > 100 CASE(5) > 101 int n = 1000000000; > 102 int m = 1; > 103 int row_[] = {1}; > 104 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 105 int seat_[] = {1}; > 106 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 107 long long _ = 0LL; > 108 END > 109 CASE(6) > 110 int n = 1000000000; > 111 int m = 100000001; > 112 int row_[] = {1}; > 113 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 114 int seat_[] = {1}; > 115 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 116 long long _ = 100000000000000000LL - 1; > 117 END > 118 CASE(7) > 119 int n = 1000000000; > 120 int m = 100000001; > 121 int row_[] = {2}; > 122 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 123 int seat_[] = {1}; > 124 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 125 long long _ = 100000000000000000LL - 1; > 126 END > 127 CASE(8) > 128 int n = 1000000000; > 129 int m = 100000001; > 130 int row_[] = {1}; > 131 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 132 int seat_[] = {2}; > 133 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 134 long long _ = 100000000000000000LL - 2; > 135 END > 136 CASE(9) > 137 int n = 1000000000; > 138 int m = 100000001; > 139 int row_[] = {1}; > 140 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 141 int seat_[] = {100000001}; > 142 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 143 long long _ = 100000000000000000LL - 1; > 144 END > 145 CASE(10) > 146 int n = 1000000000; > 147 int m = 100000001; > 148 int row_[] = {1}; > 149 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 150 int seat_[] = {100000000}; > 151 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 152 long long _ = 100000000000000000LL - 2; > 153 END > 154 CASE(11) > 155 int n = 3; > 156 int m = 3; > 157 int row_[] = {1,2,3}; > 158 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 159 int seat_[] = {2,2,2}; > 160 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 161 long long _ = 0LL; > 162 END > 163 CASE(12) > 164 int n = 3; > 165 int m = 3; > 166 int row_[] = {1,2,3,2,2}; > 167 vector <int> row(row_, row_+sizeof(row_)/sizeof(*row_)); > 168 int seat_[] = {1,2,3,1,3}; > 169 vector <int> seat(seat_, seat_+sizeof(seat_)/sizeof(*seat_)); > 170 long long _ = 2LL; > 171 END > 172 } > 173 // END CUT HERE
Added SRM/469/1B.cpp version [bec7920e12c18fbc]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheMoviesLevelTwoDivOne { public: > 23 vector <int> find(vector <int> length, vector <int> scary) > 24 { > 25 int N = length.size(); > 26 > 27 vector<int> lwb(N), inc(N); > 28 for(int i=0; i<N; ++i) > 29 if( scary[i]+47 < length[i] ) > 30 lwb[i] = length[i] - 47; > 31 else > 32 lwb[i] = scary[i]; > 33 for(int i=0; i<N; ++i) > 34 inc[i] = 47 - length[i]; > 35 > 36 memo = vector<bool>(1<<N); > 37 memo[(1<<N)-1] = true; > 38 vector<int> temp; > 39 rec(0, 74, N, lwb, inc, temp); > 40 > 41 set<int> not_used; > 42 for(int i=0; i<N; ++i) not_used.insert(i); > 43 for(int i=0; i<result.size(); ++i) > 44 not_used.erase(result[i]); > 45 result.insert(result.end(), not_used.begin(), not_used.end()); > 46 > 47 return result; > 48 } > 49 > 50 vector<int> result; > 51 vector<bool> memo; > 52 void rec(int mask, int cur, int N, vector<int>& lwb, vector<int>& inc, v > 53 { > 54 if( result.size() < temp.size() ) > 55 result = temp; > 56 if( memo[mask] ) > 57 return; > 58 memo[mask] = true; > 59 > 60 for(int i=0; i<N; ++i) > 61 if( !(mask & (1<<i)) && cur>=lwb[i] ) > 62 { > 63 temp.push_back(i); > 64 rec(mask|(1<<i), cur+inc[i], N, lwb, inc, temp); > 65 temp.pop_back(); > 66 } > 67 } > 68 }; > 69 > 70 // BEGIN CUT HERE > 71 #include <ctime> > 72 double start_time; string timer() > 73 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 74 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 75 { os << "{ "; > 76 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 77 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 78 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 79 bool ok = (Expected == Received); > 80 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 81 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 82 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 83 #define END verify_case(_, TheMoviesLevelTwoDivOne().find(length, scary));} > 84 int main(){ > 85 > 86 CASE(0) > 87 int length_[] = {100, 50}; > 88 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 89 int scary_[] = {1, 1}; > 90 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 91 int __[] = {0, 1 }; > 92 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 93 END > 94 CASE(1) > 95 int length_[] = {100, 50}; > 96 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 97 int scary_[] = {1, 49}; > 98 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 99 int __[] = {1, 0 }; > 100 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 101 END > 102 CASE(2) > 103 int length_[] = {100, 100, 100, 100}; > 104 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 105 int scary_[] = {77, 76, 75, 74}; > 106 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 107 int __[] = {3, 0, 1, 2 }; > 108 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 109 END > 110 CASE(3) > 111 int length_[] = {100}; > 112 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 113 int scary_[] = {99}; > 114 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 115 int __[] = {0 }; > 116 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 117 END > 118 CASE(4) > 119 int length_[] = {2}; > 120 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 121 int scary_[] = {1}; > 122 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 123 int __[] = {0}; > 124 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 125 END > 126 CASE(5) > 127 int length_[] = {3}; > 128 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 129 int scary_[] = {1}; > 130 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 131 int __[] = {0}; > 132 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 133 END > 134 CASE(6) > 135 int length_[] = {5,6,7,8,9,10,11,10,9,8,7,6,5,6,7,8,9,10,11,10}; > 136 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 137 int scary_[] = {3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}; > 138 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 139 int __[] = {-1}; > 140 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 141 END > 142 CASE(7) > 143 int length_[] = {185, 53, 28, 92, 70}; > 144 vector <int> length(length_, length_+sizeof(length_)/sizeof(*length_)) > 145 int scary_[] = {96, 17, 4, 6, 34}; > 146 vector <int> scary(scary_, scary_+sizeof(scary_)/sizeof(*scary_)); > 147 int __[] = {1,2,3,4,0}; > 148 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 149 END > 150 } > 151 // END CUT HERE
Added SRM/469/1C.cpp version [d5918a22f737d339]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheMoviesLevelThreeDivOne { public: > 23 long long find(vector <int> timeJ, vector <int> timeB) > 24 { > 25 int N = timeJ.size(); > 26 return (1LL<<N) - quitJ(N, timeJ, timeB) - quitJ(N, timeB, timeJ > 27 } > 28 > 29 LL quitJ(int N, vector<int>& J, vector<int>& B) // # of patterns in whic > 30 { > 31 typedef pair<int,int> state; > 32 typedef map<state, LL>::iterator iter; > 33 > 34 map<pair<int,int>, LL> M; > 35 M[state(0,-0x3fffffff)] = 1; > 36 > 37 for(int i=0; i<N; ++i) > 38 { > 39 map<state, LL> M2; > 40 for(iter it=M.begin(); it!=M.end(); ++it) > 41 { > 42 int sum = it->first.first; > 43 int bound = it->first.second; > 44 LL n = it->second; > 45 > 46 M2[state(sum+J[i], bound-J[i])] + > 47 M2[state(sum+J[i]-B[i], max(bound, B[i]-sum))] + > 48 } > 49 M2.swap(M); > 50 } > 51 > 52 LL cnt = 0; > 53 for(iter it=M.begin(); it!=M.end(); ++it) > 54 if( 0 < it->first.second ) > 55 cnt += it->second; > 56 return cnt; > 57 } > 58 }; > 59 > 60 // BEGIN CUT HERE > 61 #include <ctime> > 62 double start_time; string timer() > 63 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 64 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 65 { os << "{ "; > 66 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 67 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 68 void verify_case(const long long& Expected, const long long& Received) { > 69 bool ok = (Expected == Received); > 70 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 71 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 72 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 73 #define END verify_case(_, TheMoviesLevelThreeDivOne().find(timeJ, timeB)); > 74 int main(){ > 75 > 76 CASE(0) > 77 int timeJ_[] = {4, 4}; > 78 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 79 int timeB_[] = {4, 4}; > 80 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 81 long long _ = 2LL; > 82 END > 83 CASE(1) > 84 int timeJ_[] = {1, 4}; > 85 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 86 int timeB_[] = {4, 2}; > 87 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 88 long long _ = 1LL; > 89 END > 90 CASE(2) > 91 int timeJ_[] = {10, 10, 10, 10}; > 92 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 93 int timeB_[] = {1, 1, 1, 10}; > 94 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 95 long long _ = 3LL; > 96 END > 97 CASE(3) > 98 int timeJ_[] = {1, 2, 3, 4, 5, 6, 7}; > 99 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 100 int timeB_[] = {7, 6, 5, 4, 3, 2, 1}; > 101 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 102 long long _ = 98LL; > 103 END > 104 CASE(4) > 105 int timeJ_[] = {8,19,17,5,9,15,10,14,13,11,4,1,18,11,10,8,13,8,20,4,5,3, > 106 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 107 int timeB_[] = {9,19,6,12,2,13,19,11,9,12,17,6,7,6,1,17,15,8,7,6,10,6,2, > 108 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 109 long long _ = -1LL; > 110 END > 111 /* > 112 CASE(5) > 113 int timeJ_[] = ; > 114 vector <int> timeJ(timeJ_, timeJ_+sizeof(timeJ_)/sizeof(*timeJ_)); > 115 int timeB_[] = ; > 116 vector <int> timeB(timeB_, timeB_+sizeof(timeB_)/sizeof(*timeB_)); > 117 long long _ = LL; > 118 END > 119 */ > 120 } > 121 // END CUT HERE
Added SRM/470-U/1A.cpp version [24617375bbc033b8]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class DoorsGame { public: > 23 int determineOutcome(string doors, int trophy) > 24 { > 25 vector<int> score(2<<16); > 26 vector<bool> computed(2<<16); > 27 return rec(score, computed, 0, doors, trophy); > 28 } > 29 int rec(vector<int>& score, vector<bool>& computed, int mask, string& do > 30 { > 31 if( computed[mask] ) > 32 return score[mask]; > 33 computed[mask] = true; > 34 > 35 bool Lwin = true; > 36 for(int i=0; i<trophy; ++i) > 37 if( !(mask & (1<<(doors[i]-'A'))) ) > 38 Lwin = false; > 39 bool Rwin = true; > 40 for(int i=doors.size()-1; i>=trophy; --i) > 41 if( !(mask & (1<<(doors[i]-'A'))) ) > 42 Rwin = false; > 43 > 44 if( Lwin && Rwin ) > 45 return score[mask] = 0; > 46 if( Lwin ) > 47 return score[mask] = +bitcnt(mask); > 48 if( Rwin ) > 49 return score[mask] = -bitcnt(mask); > 50 > 51 vector<int> ss; > 52 for(int c=0; c<16; ++c) > 53 if( !(mask & (1<<c)) ) > 54 ss.push_back( rec(score,computed,mask|(1<<c),doo > 55 > 56 bool L = !(bitcnt(mask)&1); > 57 if(!L) > 58 for(int i=0; i<ss.size(); ++i) > 59 ss[i] = -ss[i]; > 60 > 61 int Ms = *max_element(ss.begin(),ss.end()); > 62 if( Ms == 0 ) > 63 return score[mask] = 0; > 64 if( Ms > 0 ) { > 65 int posMin = 99999; > 66 for(int i=0; i<ss.size(); ++i) > 67 if(ss[i]>0) > 68 posMin = min(posMin,ss[i]); > 69 return score[mask] = (L ? posMin : -posMin); > 70 } > 71 int me = *min_element(ss.begin(), ss.end()); > 72 return score[mask] = (L ? me : -me); > 73 } > 74 int bitcnt(int mask) > 75 { > 76 int cnt = 0; > 77 for(; mask; mask>>=1) > 78 cnt += mask&1; > 79 return cnt; > 80 } > 81 }; > 82 > 83 // BEGIN CUT HERE > 84 #include <ctime> > 85 double start_time; string timer() > 86 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 87 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 88 { os << "{ "; > 89 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 90 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 91 void verify_case(const int& Expected, const int& Received) { > 92 bool ok = (Expected == Received); > 93 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 94 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 95 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 96 #define END verify_case(_, DoorsGame().determineOutcome(doors, trophy));} > 97 int main(){ > 98 > 99 CASE(0) > 100 string doors = "ABCD"; > 101 int trophy = 2; > 102 int _ = 3; > 103 END > 104 CASE(1) > 105 string doors = "ABCC"; > 106 int trophy = 2; > 107 int _ = -2; > 108 END > 109 CASE(2) > 110 string doors = "ABABAB"; > 111 int trophy = 3; > 112 int _ = 0; > 113 END > 114 CASE(3) > 115 string doors = "ABAPDCAA"; > 116 int trophy = 5; > 117 int _ = -4; > 118 END > 119 CASE(4) > 120 string doors = "MOCFDCE"; > 121 int trophy = 3; > 122 int _ = 5; > 123 END > 124 CASE(5) > 125 string doors = "ABCCDE"; > 126 int trophy = 3; > 127 int _ = 0; > 128 END > 129 CASE(6) > 130 string doors = "ABCCD"; > 131 int trophy = 3; > 132 int _ = 0; > 133 END > 134 CASE(7) > 135 string doors = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPCC"; > 136 int trophy = 25; > 137 int _ = 0; > 138 END > 139 CASE(8) > 140 string doors = "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPCC"; > 141 int trophy = 7; > 142 int _ = 0; > 143 END > 144 CASE(8) > 145 string doors = "AAAAAAAAAAAAAAAAABCDEFGHIJKLMNOPPPPPPPPPPPPPPPPPP"; > 146 int trophy = 25; > 147 int _ = 0; > 148 END > 149 } > 150 // END CUT HERE
Added SRM/470-U/1B.cpp version [5418de6c5fb4301b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class DrawingLines { public: > 23 double countLineCrossings(int n, vector <int> startDot, vector <int> end > 24 { > 25 int m = n-startDot.size(); > 26 double news = c(m); > 27 double ooolds = 0.0; > 28 for(int i=0; i<startDot.size(); ++i) > 29 for(int j=i+1; j<startDot.size(); ++j) > 30 if( startDot[i]<startDot[j] && endDot[i]>endDot[ > 31 || startDot[i]>startDot[j] && endDot[i]<endDot[ > 32 ooolds += 1; > 33 double olds = 0.0; > 34 > 35 vector<int> sTaken, eTaken; > 36 for(int i=0; i<startDot.size(); ++i) { > 37 sTaken.push_back(startDot[i]); > 38 eTaken.push_back(endDot[i]); > 39 } > 40 sort(sTaken.begin(), sTaken.end()); > 41 sort(eTaken.begin(), eTaken.end()); > 42 > 43 if( n > startDot.size() ) > 44 for(int i=0; i<startDot.size(); ++i) { > 45 vector<int>::iterator si = lower_bound(sTaken.begin(), s > 46 vector<int>::iterator ei = lower_bound(eTaken.begin(), e > 47 olds += f( (startDot[i]-1) - (si-sTaken.begin()), > 48 (n-startDot[i]) - (sTaken.end()-si-1), > 49 (endDot[i]-1) - (ei-eTaken.begin()), > 50 (n-endDot[i]) - (eTaken.end()-ei-1) > 51 ); > 52 } > 53 return news + ooolds + olds; > 54 } > 55 > 56 double c(int m) > 57 { > 58 return m*(m-1) / 4.0; > 59 } > 60 > 61 double f(double a, double b, double c, double d) > 62 { > 63 return a * d/(c+d) + b*c/(c+d); > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const double& Expected, const double& Received) { > 76 bool ok = (abs(Expected - Received) < 1e-9); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, DrawingLines().countLineCrossings(n, startDot, e > 81 int main(){ > 82 > 83 CASE(0) > 84 int n = 3; > 85 int startDot_[] = {2}; > 86 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 87 int endDot_[] = {3}; > 88 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 89 double _ = 1.5; > 90 END > 91 CASE(1) > 92 int n = 5; > 93 int startDot_[] = {1,4}; > 94 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 95 int endDot_[] = {3,1}; > 96 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 97 double _ = 5.5; > 98 END > 99 CASE(2) > 100 int n = 4; > 101 int startDot_[] = {4,1}; > 102 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 103 int endDot_[] = {4,1}; > 104 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 105 double _ = 0.5; > 106 END > 107 CASE(3) > 108 int n = 8; > 109 int startDot_[] = {1,4,3,6,7}; > 110 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 111 int endDot_[] = {1,3,2,4,5}; > 112 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 113 double _ = 7.5; > 114 END > 115 CASE(4) > 116 int n = 2; > 117 int startDot_[] = {1,2} ; > 118 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 119 int endDot_[] = {1,2}; > 120 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 121 double _ = 0; > 122 END > 123 CASE(5) > 124 int n = 2; > 125 int startDot_[] = {1,2} ; > 126 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 127 int endDot_[] = {2,1}; > 128 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 129 double _ = 1; > 130 END > 131 CASE(6) > 132 int n = 3; > 133 int startDot_[] = {1} ; > 134 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 135 int endDot_[] = {1}; > 136 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 137 double _ = 0.5; > 138 END > 139 CASE(7) > 140 int n = 3; > 141 int startDot_[] = {1} ; > 142 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 143 int endDot_[] = {3}; > 144 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 145 double _ = 2.5; > 146 END > 147 CASE(8) > 148 int n = 10000; > 149 int startDot_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; > 150 vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*s > 151 int endDot_[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}; > 152 vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)) > 153 double _ = -1; > 154 END > 155 > 156 } > 157 // END CUT HERE
Added SRM/471-U/1A.cpp version [47ade1da7def4515]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PrimeSequences { public: > 23 int getLargestGenerator(int N, int D) > 24 { > 25 int result = -1; > 26 > 27 vector<int> p(N+1, 1); > 28 p[0] = p[1] = 0; > 29 for(int q=2; q<=N; ++q) > 30 if( p[q] ) { > 31 if(q<=N/q) > 32 for(int qq=q*q; qq<=N; qq+=q) > 33 p[qq] = 0; > 34 p[q] = p[q/2]+1; > 35 if( p[q] >= D ) > 36 result = q; > 37 } > 38 > 39 return result; > 40 } > 41 }; > 42 > 43 // BEGIN CUT HERE > 44 #include <ctime> > 45 double start_time; string timer() > 46 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 47 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 48 { os << "{ "; > 49 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 50 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 51 void verify_case(const int& Expected, const int& Received) { > 52 bool ok = (Expected == Received); > 53 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 54 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 55 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 56 #define END verify_case(_, PrimeSequences().getLargestGenerator(N, D));} > 57 int main(){ > 58 > 59 CASE(0) > 60 int N = 10; > 61 int D = 2; > 62 int _ = 7; > 63 END > 64 CASE(1) > 65 int N = 42; > 66 int D = 3; > 67 int _ = 23; > 68 END > 69 CASE(2) > 70 int N = 666; > 71 int D = 7; > 72 int _ = -1; > 73 END > 74 CASE(3) > 75 int N = 1337; > 76 int D = 5; > 77 int _ = 47; > 78 END > 79 CASE(4) > 80 int N = 100000; > 81 int D = 5; > 82 int _ = 2879; > 83 END > 84 CASE(5) > 85 int N = 40000; > 86 int D = 1; > 87 int _ = 39989; > 88 END > 89 CASE(6) > 90 int N = 10000000; > 91 int D = 10; > 92 int _ = -1; > 93 END > 94 CASE(7) > 95 int N = 2; > 96 int D = 1; > 97 int _ = 2; > 98 END > 99 CASE(7) > 100 int N = 3; > 101 int D = 1; > 102 int _ = 3; > 103 END > 104 CASE(7) > 105 int N = 4; > 106 int D = 1; > 107 int _ = 3; > 108 END > 109 CASE(7) > 110 int N = 5; > 111 int D = 1; > 112 int _ = 5; > 113 END > 114 CASE(7) > 115 int N = 6; > 116 int D = 1; > 117 int _ = 5; > 118 END > 119 CASE(7) > 120 int N = 6; > 121 int D = 2; > 122 int _ = 5; > 123 END > 124 CASE(7) > 125 int N = 6; > 126 int D = 3; > 127 int _ = -1; > 128 END > 129 CASE(7) > 130 int N = 7; > 131 int D = 2; > 132 int _ = 7; > 133 END > 134 > 135 } > 136 // END CUT HERE
Added SRM/471-U/1B.cpp version [bb11249db9cf179a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ThirteenHard { public: > 23 int calcTime(vector <string> city) > 24 { > 25 typedef int vert; > 26 typedef int cost; > 27 typedef pair<cost,vert> cedge; > 28 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 29 Q.push( cedge(0,1*32+0) ); > 30 > 31 set<int> visited; > 32 while( !Q.empty() ) > 33 { > 34 cedge s = Q.top(); > 35 Q.pop(); > 36 if( visited.count(s.second) ) > 37 continue; > 38 visited.insert(s.second); > 39 > 40 int v = s.second%32; > 41 int mask = s.second/32; > 42 int c = s.first; > 43 if( v == (int)city.size()-1 ) > 44 return c; > 45 > 46 for(int i=0; i<city.size(); ++i) > 47 { > 48 char dd = city[v][i]; > 49 int d = ('a'<=dd && dd<='z' ? dd-'a'+27 : dd-'A' > 50 if( dd!='#' && d%13>0 ) > 51 { > 52 int mask2 = mask << (d%13); > 53 if( mask2 & (1<<13) ) > 54 continue; > 55 mask2 = (mask2 & ((1<<13) - 1)) | (mask2 > 56 int s2 = mask2*32 + i; > 57 if( !visited.count(s2) ) > 58 Q.push( cedge(c+d, s2) ); > 59 } > 60 } > 61 } > 62 return -1; > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time; string timer() > 69 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 70 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 71 { os << "{ "; > 72 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 73 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 74 void verify_case(const int& Expected, const int& Received) { > 75 bool ok = (Expected == Received); > 76 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 77 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 78 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 79 #define END verify_case(_, ThirteenHard().calcTime(city));} > 80 int main(){ > 81 > 82 CASE(0) > 83 string city_[] = { "#AB##", > 84 "###A#", > 85 "###C#", > 86 "####K", > 87 "#####" }; > 88 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 89 int _ = 16; > 90 END > 91 CASE(1) > 92 string city_[] = { "#Z", > 93 "Z#" }; > 94 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 95 int _ = -1; > 96 END > 97 CASE(2) > 98 string city_[] = { "Good#####", > 99 "#Luck####", > 100 "##and####", > 101 "##Have###", > 102 "####Fun##", > 103 "#####in##", > 104 "#####the#", > 105 "CHALLENGE", > 106 "##PHASE##" }; > 107 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 108 int _ = 137; > 109 END > 110 CASE(3) > 111 string city_[] = { "###No#####", > 112 "####Zaphod", > 113 "#####Just#", > 114 "######very", > 115 "####very##", > 116 "improbable", > 117 "##########", > 118 "##########", > 119 "##########", > 120 "##########" }; > 121 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 122 int _ = 103; > 123 END > 124 CASE(4) > 125 string city_[] = { "#B#C##T#M", > 126 "##K######", > 127 "########A", > 128 "####R####", > 129 "#####U###", > 130 "########C", > 131 "#######H#", > 132 "########S", > 133 "#########" }; > 134 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 135 int _ = 47; > 136 END > 137 CASE(5) > 138 string city_[] = {"#a","##"}; > 139 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 140 int _ = 27; > 141 END > 142 CASE(5) > 143 string city_[] = {"#z","##"}; > 144 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 145 int _ = -1; > 146 END > 147 CASE(6) > 148 string city_[] = {"abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcde > 149 "abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvw > 150 "abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvw > 151 "abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvw > 152 "abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvwxy","abcdefghijklmnopqrstuvw > 153 vector <string> city(city_, city_+sizeof(city_)/sizeof(*city_)); > 154 int _ = -999; > 155 END > 156 > 157 } > 158 // END CUT HERE
Added SRM/472-U/1A.cpp version [e239d468ae47778c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PotatoGame { public: > 23 string theWinner(int n) > 24 { > 25 return n%5==0 || n%5==2 ? "Hanako" : "Taro"; > 26 } > 27 }; > 28 > 29 // BEGIN CUT HERE > 30 #include <ctime> > 31 double start_time; string timer() > 32 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 33 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 34 { os << "{ "; > 35 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 36 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 37 void verify_case(const string& Expected, const string& Received) { > 38 bool ok = (Expected == Received); > 39 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 40 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 41 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 42 #define END verify_case(_, PotatoGame().theWinner(n));} > 43 int main(){ > 44 > 45 CASE(0) > 46 int n = 1; > 47 string _ = "Taro"; > 48 END > 49 CASE(1) > 50 int n = 2; > 51 string _ = "Hanako"; > 52 END > 53 CASE(2) > 54 int n = 3; > 55 string _ = "Taro"; > 56 END > 57 CASE(3) > 58 int n = 4; > 59 string _ = "Taro"; > 60 END > 61 CASE(4) > 62 int n = 5; > 63 string _ = "??"; > 64 END > 65 CASE(6) > 66 int n = 1000000000; > 67 string _ = "??"; > 68 END > 69 CASE(7) > 70 int n = 8; > 71 string _ = "Taro"; > 72 END > 73 > 74 } > 75 // END CUT HERE
Added SRM/472-U/1B.cpp version [033d37ff367d441d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP4 > 24 { > 25 int N1, N2, N3, N4; > 26 vector<T> data; > 27 DP4(int N1, int N2, int N3, int N4, const T& t = T()) > 28 : N1(N1), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert( > 29 T& operator()(int i1, int i2, int i3, int i4) > 30 { return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } > 31 void swap(DP4& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 static const LL MODVAL = 1000000007; > 36 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 37 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 38 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 39 LL POW(LL x, LL e) { > 40 LL v = 1; > 41 for(;e;x=MUL(x,x),e>>=1) > 42 if(e&1) > 43 v = MUL(v, x); > 44 return v; > 45 } > 46 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 47 LL C(LL n, LL k) { > 48 k = min(n, n-k); > 49 LL v = 1; > 50 for(LL i=1; i<=k; ++i) > 51 v = DIV(MUL(v, n-i+1), i); > 52 return v; > 53 } > 54 LL FACT(LL n) { > 55 return n<=1 ? 1 : MUL(n, FACT(n-1)); > 56 } > 57 > 58 class TwoSidedCards { public: > 59 int theCount(vector <int> taro, vector <int> hanako) > 60 { > 61 int N = taro.size(); > 62 vector<LL> loops; > 63 { > 64 vector<int> next(N); > 65 for(int i=0; i<N; ++i) > 66 next[taro[i]-1] = hanako[i]-1; > 67 > 68 vector<bool> used(N); > 69 for(int s=0; s<N; ++s) > 70 if( !used[s] ) { > 71 used[s] = true; > 72 > 73 LL looplen = 1; > 74 for(int v=next[s]; v!=s; v=next[v]) { > 75 used[v] = true; > 76 ++looplen; > 77 } > 78 loops.push_back(looplen); > 79 } > 80 } > 81 > 82 return (int)solve(N, loops); > 83 } > 84 > 85 LL solve(int N, const vector<LL>& loops) > 86 { > 87 LL result = 1; > 88 > 89 LL rest = N; > 90 for(int i=0; i<loops.size(); ++i) { > 91 result = MUL(result, MUL(singleloop(loops[i]), C(rest,lo > 92 rest -= loops[i]; > 93 } > 94 return result; > 95 } > 96 > 97 map< pair< pair<int,int>,pair<int,int> >, LL > memo; > 98 LL num_downtrig(int first, int last, int i, int n, int c) > 99 { > 100 if( i == n ) > 101 return (last==1 && first==0 ? 1 : 0)==c ? 1 : 0; > 102 if( c < 0 ) > 103 return 0; > 104 pair< pair<int,int>,pair<int,int> > key( make_pair(first,last), > 105 if( memo.count(key) ) > 106 return memo[key]; > 107 return memo[key] = ADD( > 108 num_downtrig(first, 0, i+1, n, c-(last==1)), > 109 num_downtrig(first, 1, i+1, n, c)); > 110 } > 111 LL singleloop(int n) > 112 { > 113 if( n == 1 ) > 114 return 1; > 115 LL fn = FACT(n); > 116 LL result = 0; > 117 memo.clear(); > 118 for(int c=0; c<n; ++c) > 119 { > 120 LL z; > 121 if( c == 0 ) > 122 z = num_downtrig(0, 0, 1, n, c); > 123 else > 124 z = ADD( > 125 num_downtrig(0, 0, 1, n, c), > 126 num_downtrig(1, 1, 1, n, c)); > 127 result = ADD(result, DIV(MUL(z,fn), POW(2,c))); > 128 } > 129 return result; > 130 } > 131 }; > 132 > 133 // BEGIN CUT HERE > 134 #include <ctime> > 135 double start_time; string timer() > 136 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 137 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 138 { os << "{ "; > 139 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 140 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 141 void verify_case(const int& Expected, const int& Received) { > 142 bool ok = (Expected == Received); > 143 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 144 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 145 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 146 #define END verify_case(_, TwoSidedCards().theCount(taro, hanako));} > 147 int main(){ > 148 > 149 CASE(0) > 150 int taro_[] = {1, 2, 3}; > 151 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 152 int hanako_[] = {1, 3, 2}; > 153 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 154 int _ = 12; > 155 END > 156 CASE(1) > 157 int taro_[] = {1, 2, 3}; > 158 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 159 int hanako_[] = {1, 2, 3}; > 160 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 161 int _ = 6; > 162 END > 163 CASE(2) > 164 int taro_[] = {1, 2}; > 165 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 166 int hanako_[] = {2, 1}; > 167 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 168 int _ = 4; > 169 END > 170 CASE(3) > 171 int taro_[] = {5, 8, 1, 2, 3, 4, 6, 7}; > 172 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 173 int hanako_[] = {1, 2, 3, 4, 5, 6, 7, 8}; > 174 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 175 int _ = 2177280; > 176 END > 177 CASE(4) > 178 int taro_[] = {41, 22, 17, 36, 26, 15, 10, 23, 33, 48, 49, 9, 34, 6, 21, > 179 50, 44, 42, 31, 12, 29, 7, 43, 18, 30, 19, 45, 32, 39, 14, 8, 27, 1, 5, 38, 11, > 180 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 181 int hanako_[] = {19, 6, 23, 35, 17, 7, 50, 2, 33, 36, 12, 31, 46, 21, 30 > 182 48, 10, 28, 26, 18, 5, 45, 49, 16, 40, 42, 43, 14, 1, 37, 29, 8, 41, 38, 9, 11, > 183 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 184 int _ = 529165844; > 185 END > 186 CASE(5) > 187 int taro_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, > 188 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; > 189 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 190 int hanako_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,2 > 191 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; > 192 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 193 int _ = -1; > 194 END > 195 CASE(6) > 196 int taro_[] = {1}; > 197 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 198 int hanako_[] = {1}; > 199 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 200 int _ = 1; > 201 END > 202 CASE(7) > 203 int taro_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, > 204 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; > 205 vector <int> taro(taro_, taro_+sizeof(taro_)/sizeof(*taro_)); > 206 int hanako_[] = {50,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2 > 207 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49}; > 208 vector <int> hanako(hanako_, hanako_+sizeof(hanako_)/sizeof(*hanako_)) > 209 int _ = 1; > 210 END > 211 } > 212 // END CUT HERE
Added SRM/473-U/1A.cpp version [54bf3e40d837f13b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SequenceOfCommands { public: > 23 string whatHappens(vector <string> commands) > 24 { > 25 string cmd = accumulate(commands.begin(), commands.end(), string > 26 cmd = cmd+cmd+cmd+cmd; > 27 > 28 int dx=1, dy=0; > 29 int px=0, py=0; > 30 for(int i=0; i<cmd.size(); ++i) > 31 if(cmd[i]=='S') > 32 {px+=dx, py+=dy;} > 33 else if(cmd[i]=='L') > 34 {int t=dx; dx=-dy; dy=t;} > 35 else if(cmd[i]=='R') > 36 {int t=dx; dx=dy; dy=-t;} > 37 return (px==0 && py==0 ? "bounded" : "unbounded"); > 38 } > 39 }; > 40 > 41 // BEGIN CUT HERE > 42 #include <ctime> > 43 double start_time; string timer() > 44 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 45 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 46 { os << "{ "; > 47 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 48 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 49 void verify_case(const string& Expected, const string& Received) { > 50 bool ok = (Expected == Received); > 51 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 52 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 53 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 54 #define END verify_case(_, SequenceOfCommands().whatHappens(commands));} > 55 int main(){ > 56 > 57 CASE(0) > 58 string commands_[] = {"L"}; > 59 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 60 string _ = "bounded"; > 61 END > 62 CASE(1) > 63 string commands_[] = {"SRSL"}; > 64 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 65 string _ = "unbounded"; > 66 END > 67 CASE(2) > 68 string commands_[] = {"SSSS","R"}; > 69 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 70 string _ = "bounded"; > 71 END > 72 CASE(3) > 73 string commands_[] = {"SRSL","LLSSSSSSL","SSSSSS","L"}; > 74 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 75 string _ = "unbounded"; > 76 END > 77 CASE(4) > 78 string commands_[] = {"SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS > 79 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 80 string _ = "bounded"; > 81 END > 82 CASE(5) > 83 string commands_[] = {"R"}; > 84 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 85 string _ = "bounded"; > 86 END > 87 CASE(6) > 88 string commands_[] = {"S"}; > 89 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 90 string _ = "unbounded"; > 91 END > 92 CASE(7) > 93 string commands_[] = {"SRSLSRSL", "RR"}; > 94 vector <string> commands(commands_, commands_+sizeof(commands_)/sizeof > 95 string _ = "bounded"; > 96 END > 97 } > 98 // END CUT HERE
Added SRM/473-U/1B.cpp version [ad649cfd4e78d396]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RightTriangle { public: > 23 long long triangleCount(int places, int points, int a, int b, int c) > 24 { > 25 vector<bool> red(places); > 26 set<int> vacant; > 27 for(int i=0; i<places; ++i) > 28 vacant.insert(i); > 29 > 30 for(LL n=0; n<points; ++n) { > 31 LL p = (a*n*n + b*n + c) % places; > 32 if( red[p] ) { > 33 set<int>::iterator it = vacant.lower_bound(p); > 34 if( it != vacant.end() ) > 35 p = *it; > 36 else { > 37 it = vacant.lower_bound(0); > 38 p = *it; > 39 } > 40 } > 41 red[p] = true; > 42 vacant.erase(p); > 43 } > 44 > 45 if( places%2 == 1 ) > 46 return 0; > 47 > 48 LL result = 0; > 49 for(int a=0,b=a+places/2; b<places; ++a, ++b) > 50 if( red[a] && red[b] ) > 51 result += points-2; > 52 return result; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const long long& Expected, const long long& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, RightTriangle().triangleCount(places, points, a, > 70 int main(){ > 71 > 72 CASE(0) > 73 int places = 9; > 74 int points = 3; > 75 int a = 0; > 76 int b = 3; > 77 int c = 0; > 78 long long _ = 0LL; > 79 END > 80 CASE(1) > 81 int places = 40; > 82 int points = 3; > 83 int a = 5; > 84 int b = 0; > 85 int c = 0; > 86 long long _ = 1LL; > 87 END > 88 CASE(2) > 89 int places = 4; > 90 int points = 4; > 91 int a = 16; > 92 int b = 24; > 93 int c = 17; > 94 long long _ = 4LL; > 95 END > 96 CASE(3) > 97 int places = 1000000; > 98 int points = 47000; > 99 int a = 0; > 100 int b = 2; > 101 int c = 5; > 102 long long _ = 0LL; > 103 END > 104 CASE(4) > 105 int places = 200000; > 106 int points = 700; > 107 int a = 123456; > 108 int b = 789012; > 109 int c = 345678; > 110 long long _ = 6980LL; > 111 END > 112 CASE(5) > 113 int places = 1000000; > 114 int points = 0; > 115 int a = 0; > 116 int b = 0; > 117 int c = 0; > 118 long long _ = 0LL; > 119 END > 120 CASE(6) > 121 int places = 1000000; > 122 int points = 100000; > 123 int a = 0; > 124 int b = 0; > 125 int c = 0; > 126 long long _ = 0LL; > 127 END > 128 CASE(7) > 129 int places = 1; > 130 int points = 0; > 131 int a = 1; > 132 int b = 2; > 133 int c = 3; > 134 long long _ = 0LL; > 135 END > 136 CASE(8) > 137 int places = 1; > 138 int points = 1; > 139 int a = 3; > 140 int b = 2; > 141 int c = 1; > 142 long long _ = 0LL; > 143 END > 144 CASE(9) > 145 int places = 3; > 146 int points = 3; > 147 int a = 1000000; > 148 int b = 1000000; > 149 int c = 1000000; > 150 long long _ = 0LL; > 151 END > 152 CASE(9) > 153 int places = 4; > 154 int points = 4; > 155 int a = 1000000; > 156 int b = 1000000; > 157 int c = 1000000; > 158 long long _ = 4LL; > 159 END > 160 > 161 } > 162 // END CUT HERE
Added SRM/473-U/1C-U.cpp version [35a3e3aba95688f7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000009; > 23 > 24 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 25 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 26 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 27 LL POW(LL x, LL e) { > 28 LL v = 1; > 29 for(;e;x=MUL(x,x),e>>=1) > 30 if(e&1) > 31 v = MUL(v, x); > 32 return v; > 33 } > 34 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 35 LL C_impl(LL n, LL k) { > 36 LL v = 1; > 37 for(LL i=1; i<=k; ++i) > 38 v = DIV(MUL(v, n-i+1), i); > 39 return v; > 40 } > 41 LL C_tbl[40][40]; > 42 LL C(LL n, LL k) { > 43 if( C_tbl[n][k] < 0 ) > 44 C_tbl[n][k] = C_impl(n,k); > 45 return C_tbl[n][k]; > 46 } > 47 > 48 class RooksParty { public: > 49 int countArrangements(int rows, int columns, vector <int> counts) > 50 { > 51 for(int n=0;n<40;++n) > 52 for(int k=0;k<40;++k) > 53 C_tbl[n][k] = -1; > 54 sort(counts.rbegin(), counts.rend()); > 55 return (int)rec(rows, columns, counts, 0); > 56 } > 57 map<pair<pair<int,int>,int>, LL> memo; > 58 LL rec(int H, int W, vector<int>& counts, int i) > 59 { > 60 if( i == counts.size() ) > 61 return 1; > 62 int n = counts[i]; > 63 if( H*W < n ) > 64 return 0; > 65 > 66 if(H>W) > 67 swap(H,W); > 68 > 69 pair<pair<int,int>,int> key(make_pair(H,W),i); > 70 if( memo.count(key) ) > 71 return memo[key]; > 72 > 73 LL cnt = 0; > 74 for(int h=1; h<=min(H,n); ++h) > 75 for(int w=1; w<=min(W,n); ++w) > 76 if( h*w >= n ) > 77 cnt = ADD(cnt, MUL(MUL(C(H,h), C(W,w)), > 78 return memo[key] = cnt; > 79 } > 80 > 81 map<pair<pair<int,int>,int>, LL> memo2; > 82 LL count(int h, int w, int n) > 83 { > 84 if( h*w < n ) > 85 return 0; > 86 pair<pair<int,int>,int> key(make_pair(h,w),n); > 87 if(memo2.count(key)) > 88 return memo2[key]; > 89 LL cnt = freedom(h,w,n); > 90 for(int t=1; t<=w-1; ++t) > 91 cnt = SUB(cnt, MUL(count(h,w-t,n),C(w,t))); > 92 return memo2[key] = cnt; > 93 } > 94 > 95 map<pair<pair<int,int>,int>, LL> memo3; > 96 LL freedom(int h, int w, int n) > 97 { > 98 if(h*w<n) return 0; > 99 //if(h>n) return 0; > 100 if(h==0) return 1; > 101 pair<pair<int,int>,int> key(make_pair(h,w),n); > 102 if(memo3.count(key)) > 103 return memo3[key]; > 104 > 105 LL cnt = 0; > 106 for(int t=0; t<=min(n,w); ++t) > 107 cnt = ADD(cnt, MUL(freedom(h-1, w, n-t), C(w,t))); > 108 return memo3[key]=cnt; > 109 } > 110 }; > 111 > 112 // BEGIN CUT HERE > 113 #include <ctime> > 114 double start_time; string timer() > 115 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 116 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 117 { os << "{ "; > 118 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 119 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 120 void verify_case(const int& Expected, const int& Received) { > 121 bool ok = (Expected == Received); > 122 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 123 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 124 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 125 #define END verify_case(_, RooksParty().countArrangements(rows, columns, co > 126 int main(){ > 127 > 128 CASE(0) > 129 int rows = 2; > 130 int columns = 3; > 131 int counts_[] = {1,1}; > 132 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 133 int _ = 12; > 134 END > 135 CASE(1) > 136 int rows = 5; > 137 int columns = 2; > 138 int counts_[] = {3}; > 139 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 140 int _ = 120; > 141 END > 142 CASE(2) > 143 int rows = 5; > 144 int columns = 2; > 145 int counts_[] = {1,1,1}; > 146 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 147 int _ = 0; > 148 END > 149 CASE(3) > 150 int rows = 8; > 151 int columns = 8; > 152 int counts_[] = {1,1,1,1,1,1,1,1}; > 153 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 154 int _ = 625702391; > 155 END > 156 CASE(4) > 157 int rows = 4; > 158 int columns = 2; > 159 int counts_[] = {3,1}; > 160 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 161 int _ = 8; > 162 END > 163 /* > 164 CASE(5) > 165 int rows = ; > 166 int columns = ; > 167 int counts_[] = ; > 168 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 169 int _ = ; > 170 END > 171 CASE(6) > 172 int rows = ; > 173 int columns = ; > 174 int counts_[] = ; > 175 vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)) > 176 int _ = ; > 177 END > 178 */ > 179 } > 180 // END CUT HERE
Added SRM/474/1A.cpp version [6b0f4c18c12a29a4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RouteIntersection { public: > 23 string isValid(int N, vector <int> coords, string moves) > 24 { > 25 vector< map<int,int> > route; > 26 > 27 map<int,int> cur; > 28 route.push_back(cur); > 29 for(int i=0; i<moves.size(); ++i) { > 30 int c = coords[i]; > 31 int m = moves[i]=='+' ? +1 : -1; > 32 cur[c] += m; > 33 route.push_back(cur); > 34 } > 35 > 36 for(int i=0; i<route.size(); ++i) > 37 for(int j=i+1; j<route.size(); ++j) > 38 if( is_same(route[i], route[j]) ) > 39 return "NOT VALID"; > 40 return "VALID"; > 41 } > 42 > 43 bool is_same(map<int,int>& a, map<int,int>& b) > 44 { > 45 for(map<int,int>::iterator it=b.begin(); it!=b.end(); ++it) > 46 if( a[it->first] != it->second ) > 47 return false; > 48 return true; > 49 } > 50 }; > 51 > 52 // BEGIN CUT HERE > 53 #include <ctime> > 54 double start_time; string timer() > 55 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 56 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 57 { os << "{ "; > 58 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 59 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 60 void verify_case(const string& Expected, const string& Received) { > 61 bool ok = (Expected == Received); > 62 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 63 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 64 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 65 #define END verify_case(_, RouteIntersection().isValid(N, coords, moves));} > 66 int main(){ > 67 > 68 CASE(0) > 69 int N = 1; > 70 int coords_[] = {1}; > 71 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 72 string moves = "+"; > 73 string _ = "VALID"; > 74 END > 75 CASE(1) > 76 int N = 2; > 77 int coords_[] = {1,2,1,2}; > 78 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 79 string moves = "++--"; > 80 string _ = "NOT VALID"; > 81 END > 82 CASE(2) > 83 int N = 3; > 84 int coords_[] = {1,2,3,1,2}; > 85 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 86 string moves = "+++--"; > 87 string _ = "VALID"; > 88 END > 89 CASE(3) > 90 int N = 344447; > 91 int coords_[] = {132,51717,628,344447,628,51717,344447,2}; > 92 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 93 string moves = "+-++-+--"; > 94 string _ = "NOT VALID"; > 95 END > 96 CASE(4) > 97 int N = 1; > 98 int coords_[] = {1,1}; > 99 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 100 string moves = "+-"; > 101 string _ = "NOT VALID"; > 102 END > 103 CASE(5) > 104 int N = 990630; > 105 int coords_[] = {833196,524568,361663,108056,28026,824639,269315,440977, > 106 988451,242440,948414,130873,773990,765458,130873,28026,853121,553636, > 107 581069,82254,735536,833196,898562,898562,940783,988451,540613,317306, > 108 623194,940783,571384,988451,108056,514374,97664}; > 109 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 110 string moves = "--+---+-+++-+-+---++-++-+---+-+--+-++"; > 111 string _ = "NOT VALID"; > 112 END > 113 /* > 114 CASE(6) > 115 int N = ; > 116 int coords_[] = ; > 117 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 118 string moves = ; > 119 string _ = ; > 120 END > 121 CASE(7) > 122 int N = ; > 123 int coords_[] = ; > 124 vector <int> coords(coords_, coords_+sizeof(coords_)/sizeof(*coords_)) > 125 string moves = ; > 126 string _ = ; > 127 END > 128 */ > 129 } > 130 // END CUT HERE
Added SRM/474/1B.cpp version [96cf85c87151dbba]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 typedef int vert; > 23 typedef int cost; > 24 typedef pair<cost,vert> cedge; > 25 > 26 bool byVert( const cedge& e1, const cedge& e2 ) > 27 { > 28 if( e1.second != e2.second ) > 29 return e1.second < e2.second; > 30 return e1.first < e2.first; > 31 } > 32 > 33 class TreesCount { public: > 34 int count(vector <string> graph) > 35 { > 36 LL answer = 1; > 37 > 38 priority_queue< cedge, vector<cedge>, greater<cedge> > Q; > 39 set<vert> V; > 40 Q.push( cedge(0, 0) ); > 41 while( !Q.empty() ) > 42 { > 43 const cost c0 = Q.top().first; > 44 > 45 vector<cedge> cand; > 46 while( !Q.empty() && Q.top().first==c0 ) { > 47 if( !V.count(Q.top().second) ) > 48 cand.push_back( Q.top() ); > 49 Q.pop(); > 50 } > 51 > 52 sort( cand.begin(), cand.end(), byVert ); > 53 for(int i=0; i<cand.size();) { > 54 cost c = cand[i].first; > 55 vert v = cand[i].second; > 56 int j = i; > 57 while( j<cand.size() && cand[j].second==v ) > 58 ++j; > 59 > 60 answer = answer * (j-i) % 1000000007; > 61 V.insert(v); > 62 for(int u=0; u<graph.size(); ++u) > 63 if( graph[v][u]>'0' && !V.count(u) ) > 64 Q.push( cedge(c+graph[v][u]-'0', > 65 > 66 i = j; > 67 } > 68 } > 69 > 70 return (int)answer; > 71 } > 72 }; > 73 > 74 // BEGIN CUT HERE > 75 #include <ctime> > 76 double start_time; string timer() > 77 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 78 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 79 { os << "{ "; > 80 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 81 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 82 void verify_case(const int& Expected, const int& Received) { > 83 bool ok = (Expected == Received); > 84 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 85 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 86 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 87 #define END verify_case(_, TreesCount().count(graph));} > 88 int main(){ > 89 > 90 CASE(0) > 91 string graph_[] = {"01", > 92 "10"}; > 93 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 94 int _ = 1; > 95 END > 96 CASE(1) > 97 string graph_[] = {"011", > 98 "101", > 99 "110"}; > 100 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 101 int _ = 1; > 102 END > 103 CASE(2) > 104 string graph_[] = {"021", > 105 "201", > 106 "110"}; > 107 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 108 int _ = 2; > 109 END > 110 CASE(3) > 111 string graph_[] = {"0123", > 112 "1012", > 113 "2101", > 114 "3210"}; > 115 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 116 int _ = 6; > 117 END > 118 CASE(4) > 119 string graph_[] = {"073542", > 120 "705141", > 121 "350721", > 122 "517031", > 123 "442304", > 124 "211140"}; > 125 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 126 int _ = 2; > 127 END > 128 /* > 129 CASE(5) > 130 string graph_[] = ; > 131 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 132 int _ = ; > 133 END > 134 CASE(6) > 135 string graph_[] = ; > 136 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 137 int _ = ; > 138 END > 139 */ > 140 } > 141 // END CUT HERE
Added SRM/474/1C.cpp version [20f85c4e20ad270e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP3 > 24 { > 25 int N1, N2, N3; > 26 vector<T> data; > 27 DP3(){} > 28 DP3(int N1, int N2, int N3, const T& t = T()) > 29 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size() > 30 T& operator()(int i1, int i2, int i3) > 31 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 32 void swap(DP3& rhs) > 33 { data.swap(rhs.data); } > 34 }; > 35 > 36 class GameWithGraphAndTree { public: > 37 vector<string> graph; > 38 vector< vector<int> > children; > 39 vector<int> subtree_size; > 40 > 41 int calc(vector <string> graph_, vector <string> tree_) > 42 { > 43 int N = graph_.size(); > 44 > 45 graph = graph_; > 46 children.resize(N); > 47 subtree_size.resize(N); > 48 computeChildren(-1, 0, tree_); > 49 > 50 memo = DP3<LL>(N,N,1<<N,-1); > 51 > 52 LL answer = 0; > 53 for(int gv=0; gv<graph.size(); ++gv) > 54 answer += vertical(0, gv, (1<<N)-1); > 55 return int(answer % 1000000007); > 56 } > 57 > 58 void computeChildren(int tparent, int tv, const vector<string>& tree) > 59 { > 60 subtree_size[tv] = 1; > 61 for(int tc=0; tc<tree.size(); ++tc) > 62 if( tree[tv][tc] == 'Y' && tc!=tparent ) { > 63 children[tv].push_back(tc); > 64 computeChildren(tv, tc, tree); > 65 subtree_size[tv] += subtree_size[tc]; > 66 } > 67 } > 68 > 69 LL vertical(int tv, int gv, int mask) > 70 { > 71 return horizontal(tv, gv, mask&~(1<<gv), 0); > 72 } > 73 > 74 DP3<LL> memo; > 75 LL horizontal(int tv, int gv, int mask, int i) > 76 { > 77 if( i == children[tv].size() ) > 78 return 1; > 79 int tu = children[tv][i]; > 80 > 81 if( memo(tv,gv,mask) >= 0 ) > 82 return memo(tv,gv,mask); > 83 > 84 LL answer = 0; > 85 for(int subset=mask; subset; subset=(subset-1)&mask) > 86 if( __builtin_popcount(subset) == subtree_size[tu] ) > 87 for(int gu=0; (1<<gu)<=subset; ++gu) > 88 if( graph[gv][gu]=='Y' && ((1<<gu)&subse > 89 answer += vertical(tu, gu, subse > 90 > 91 return memo(tv,gv,mask) = answer % 1000000007; > 92 } > 93 }; > 94 > 95 // BEGIN CUT HERE > 96 #include <ctime> > 97 double start_time; string timer() > 98 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 99 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 100 { os << "{ "; > 101 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 102 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 103 void verify_case(const int& Expected, const int& Received) { > 104 bool ok = (Expected == Received); > 105 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 106 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 107 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 108 #define END verify_case(_, GameWithGraphAndTree().calc(graph, tree));} > 109 int main(){ > 110 > 111 CASE(0) > 112 string graph_[] = {"NYN", > 113 "YNY", > 114 "NYN"}; > 115 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 116 string tree_[] = {"NYY", > 117 "YNN", > 118 "YNN"}; > 119 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 120 int _ = 2; > 121 END > 122 CASE(1) > 123 string graph_[] = {"NYNNN", > 124 "YNYYY", > 125 "NYNYY", > 126 "NYYNY", > 127 "NYYYN"}; > 128 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 129 string tree_[] = {"NYNNN", > 130 "YNYNN", > 131 "NYNYN", > 132 "NNYNY", > 133 "NNNYN"}; > 134 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 135 int _ = 12; > 136 END > 137 CASE(2) > 138 string graph_[] = {"NYNNNY", > 139 "YNYNNN", > 140 "NYNYNN", > 141 "NNYNYN", > 142 "NNNYNY", > 143 "YNNNYN"}; > 144 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 145 string tree_[] = {"NYNNYN", > 146 "YNNYNY", > 147 "NNNNYN", > 148 "NYNNNN", > 149 "YNYNNN", > 150 "NYNNNN"}; > 151 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 152 int _ = 0; > 153 END > 154 CASE(3) > 155 string graph_[] = {"NYNNYN", > 156 "YNNYNY", > 157 "NNNNYN", > 158 "NYNNNN", > 159 "YNYNNN", > 160 "NYNNNN"}; > 161 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 162 string tree_[] = {"NNNYYN", > 163 "NNYNNN", > 164 "NYNNYY", > 165 "YNNNNN", > 166 "YNYNNN", > 167 "NNYNNN"}; > 168 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 169 int _ = 2; > 170 END > 171 CASE(4) > 172 string graph_[] = {"NYNNNYNNY", > 173 "YNNNNNNYN", > 174 "NNNNYYNYY", > 175 "NNNNNYNNY", > 176 "NNYNNNYNY", > 177 "YNYYNNNYN", > 178 "NNNNYNNYN", > 179 "NYYNNYYNN", > 180 "YNYYYNNNN"}; > 181 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 182 string tree_[] = {"NNYNNNYYN", > 183 "NNNNYNNNN", > 184 "YNNNNNNNN", > 185 "NNNNNNYNN", > 186 "NYNNNNNYY", > 187 "NNNNNNNNY", > 188 "YNNYNNNNN", > 189 "YNNNYNNNN", > 190 "NNNNYYNNN"}; > 191 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 192 int _ = 90; > 193 END > 194 CASE(5) > 195 string graph_[] = {"N"}; > 196 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 197 string tree_[] = {"N"}; > 198 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 199 int _ = 1; > 200 END > 201 CASE(6) > 202 string graph_[] = { > 203 "NYYYYYYYYYYYYY", > 204 "YNYYYYYYYYYYYY", > 205 "YYNYYYYYYYYYYY", > 206 "YYYNYYYYYYYYYY", > 207 "YYYYNYYYYYYYYY", > 208 "YYYYYNYYYYYYYY", > 209 "YYYYYYNYYYYYYY", > 210 "YYYYYYYNYYYYYY", > 211 "YYYYYYYYNYYYYY", > 212 "YYYYYYYYYNYYYY", > 213 "YYYYYYYYYYNYYY", > 214 "YYYYYYYYYYYNYY", > 215 "YYYYYYYYYYYYNY", > 216 "YYYYYYYYYYYYYN"}; > 217 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 218 string tree_[] = { > 219 "NYNNNNNNNNNNNN", > 220 "YNYNNNNNNNNNNN", > 221 "NYNYNNNNNNNNNN", > 222 "NNYNYNNNNNNNNN", > 223 "NNNYNYNNNNNNNN", > 224 "NNNNYNYNNNNNNN", > 225 "NNNNNYNYNNNNNN", > 226 "NNNNNNYNYNNNNN", > 227 "NNNNNNNYNYNNNN", > 228 "NNNNNNNNYNYNNN", > 229 "NNNNNNNNNYNYNN", > 230 "NNNNNNNNNNYNYN", > 231 "NNNNNNNNNNNYNY", > 232 "NNNNNNNNNNNNYN", > 233 }; > 234 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 235 int _ = 178290591; > 236 END > 237 CASE(7) > 238 string graph_[] = { > 239 "NYYYYYYYYYYYYY", > 240 "YNYYYYYYYYYYYY", > 241 "YYNYYYYYYYYYYY", > 242 "YYYNYYYYYYYYYY", > 243 "YYYYNYYYYYYYYY", > 244 "YYYYYNYYYYYYYY", > 245 "YYYYYYNYYYYYYY", > 246 "YYYYYYYNYYYYYY", > 247 "YYYYYYYYNYYYYY", > 248 "YYYYYYYYYNYYYY", > 249 "YYYYYYYYYYNYYY", > 250 "YYYYYYYYYYYNYY", > 251 "YYYYYYYYYYYYNY", > 252 "YYYYYYYYYYYYYN"}; > 253 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 254 string tree_[] = { > 255 "NYYYNNNNNNNNNN", > 256 "YNNNYYYNNNNNNN", > 257 "YNNNNNNYYYNNNN", > 258 "YNNNNNNNNNYYYY", > 259 "NYNNNNNNNNNNNN", > 260 "NYNNNNNNNNNNNN", > 261 "NYNNNNNNNNNNNN", > 262 "NNYNNNNNNNNNNN", > 263 "NNYNNNNNNNNNNN", > 264 "NNYNNNNNNNNNNN", > 265 "NNNYNNNNNNNNNN", > 266 "NNNYNNNNNNNNNN", > 267 "NNNYNNNNNNNNNN", > 268 "NNNYNNNNNNNNNN", > 269 }; > 270 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 271 int _ = 178290591; > 272 END > 273 CASE(8) > 274 string graph_[] = {"NYYYYYYYYYYYYY", "YNYYYYYYYYYYYY", "YYNYYYYYYYYYYY", > 275 vector <string> graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); > 276 string tree_[] = {"NYYYYYYYYYYYYY", "YNNNNNNNNNNNNN", "YNNNNNNNNNNNNN", > 277 vector <string> tree(tree_, tree_+sizeof(tree_)/sizeof(*tree_)); > 278 int _ = 178290591; > 279 END > 280 } > 281 // END CUT HERE
Added SRM/475-U/1A.cpp version [1b625ac1477fd5f0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int bitcnt(int m) > 23 { > 24 int c = 0; > 25 for(;m;m>>=1) > 26 c += (m&1); > 27 return c; > 28 } > 29 > 30 class RabbitStepping { public: > 31 double getExpected(string field, int r) > 32 { > 33 cerr<<field.size()<<endl; > 34 int ex = 0; > 35 int cn = 0; > 36 > 37 int N = field.size(); > 38 for(int mask=1; mask<(1<<N); ++mask) > 39 if( bitcnt(mask) == r ) > 40 { > 41 vector< pair<int,int> > rs; > 42 for(int i=0; i<N; ++i) > 43 if( mask & (1<<i) ) > 44 rs.push_back(make_pair(-1,i)); > 45 int left = run(field, rs, N); > 46 ex += left; > 47 cn += 1; > 48 } > 49 return double(ex) / cn; > 50 } > 51 > 52 int run(const string& field, vector< pair<int,int> >& rs, int size) > 53 { > 54 while( size > 2 && !rs.empty() ) > 55 { > 56 oneStep(field, rs, size); > 57 int cnt[20] = {}; > 58 for(int i=0; i<rs.size(); ++i) > 59 cnt[rs[i].second] ++; > 60 vector< pair<int,int> > rs2; > 61 for(int i=0; i<rs.size(); ++i) > 62 if( cnt[rs[i].second] == 1 ) > 63 rs2.push_back(rs[i]); > 64 rs.swap(rs2); > 65 } > 66 return rs.size(); > 67 } > 68 > 69 void oneStep(const string& field, vector< pair<int,int> >& rs, int& size > 70 { > 71 for(int i=0; i<rs.size(); ++i) { > 72 int prev = rs[i].first; > 73 int& cur = rs[i].second; > 74 rs[i].first = cur; > 75 if( cur == 0 ) > 76 cur ++; > 77 else if( cur >= size-2 ) > 78 cur --; > 79 else if( field[cur]=='W' ) > 80 cur --; > 81 else if( field[cur]=='B' ) > 82 cur ++; > 83 else if( prev == -1 ) > 84 cur --; > 85 else > 86 cur = prev; > 87 } > 88 --size; > 89 } > 90 }; > 91 > 92 // BEGIN CUT HERE > 93 #include <ctime> > 94 double start_time; string timer() > 95 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 96 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 97 { os << "{ "; > 98 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 99 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 100 void verify_case(const double& Expected, const double& Received) { > 101 bool ok = (abs(Expected - Received) < 1e-9); > 102 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 103 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 104 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 105 #define END verify_case(_, RabbitStepping().getExpected(field, r));} > 106 int main(){ > 107 > 108 CASE(0) > 109 string field = "WRBRW"; > 110 int r = 4; > 111 double _ = 0.8; > 112 END > 113 CASE(1) > 114 string field = "WWB"; > 115 int r = 2; > 116 double _ = 1.3333333333333333; > 117 END > 118 CASE(2) > 119 string field = "WW"; > 120 int r = 1; > 121 double _ = 1.0; > 122 END > 123 CASE(3) > 124 string field = "BBBBBBBBBB"; > 125 int r = 4; > 126 double _ = 0.9523809523809523; > 127 END > 128 CASE(4) > 129 string field = "RRBRRWRRBRRW"; > 130 int r = 8; > 131 double _ = 0.9696969696969697; > 132 END > 133 CASE(5) > 134 string field = "RRRRRRRRRRRRRRRRR"; > 135 int r = 8; > 136 double _ = -1; > 137 END > 138 CASE(6) > 139 string field = "BBBBBBBBBBBBBBBBB"; > 140 int r = 8; > 141 double _ = -1; > 142 END > 143 CASE(7) > 144 string field = "WWWWWWWWWWWWWWWWW"; > 145 int r = 8; > 146 double _ = -1; > 147 END > 148 CASE(8) > 149 string field = "BWBWBWBWBWBWBWBWB"; > 150 int r = 8; > 151 double _ = -1; > 152 END > 153 CASE(9) > 154 string field = "RR"; > 155 int r = 2; > 156 double _ = 2; > 157 END > 158 CASE(9) > 159 string field = "RR"; > 160 int r = 1; > 161 double _ = 1; > 162 END > 163 CASE(9) > 164 string field = "BRWWBRBWBWBWBRBWR"; > 165 int r = 1; > 166 double _ = 1; > 167 END > 168 } > 169 // END CUT HERE
Added SRM/475-U/1B.cpp version [0a3d4549914a8f57]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000009; // must fit in 32-bits > 23 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 24 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 25 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 26 LL POW(LL x, LL e) { > 27 LL v = 1; > 28 for(;e;x=MUL(x,x),e>>=1) > 29 if(e&1) > 30 v = MUL(v, x); > 31 return v; > 32 } > 33 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 34 > 35 class RabbitIncreasing { public: > 36 int getNumber(vector <int> leaving, int k) > 37 { > 38 if( leaving.front() <= 1 ) > 39 return 0; > 40 int li = 0; > 41 > 42 LL y[] = {1,0,0}; > 43 LL p[] = {1,0,0}; > 44 LL PP = (1LL << 55); > 45 for(int i=2; i<=k; ++i) > 46 { > 47 LL a=y[0], b=y[1], c=y[2]; > 48 y[0]=y[2]=ADD(b,c); y[1]=a; > 49 LL pa=p[0], pb=p[1], pc=p[2]; > 50 p[0]=p[2]=(pb+pc)%PP; p[1]=pa; > 51 if( li<leaving.size() && i==leaving[li] ) { > 52 ++li; > 53 y[2] = 0; > 54 p[2] = 0; > 55 if( p[1]&1 ) { > 56 y[1]=DIV(SUB(y[1],1),2); > 57 } else { > 58 y[1]=DIV(y[1],2); > 59 } > 60 p[1]=p[1]/2; > 61 PP >>= 1; > 62 } > 63 } > 64 return ADD(y[0],ADD(y[1],y[2])); > 65 } > 66 }; > 67 > 68 // BEGIN CUT HERE > 69 #include <ctime> > 70 double start_time; string timer() > 71 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 72 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 73 { os << "{ "; > 74 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 75 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 76 void verify_case(const int& Expected, const int& Received) { > 77 bool ok = (Expected == Received); > 78 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 79 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 80 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 81 #define END verify_case(_, RabbitIncreasing().getNumber(leaving, k));} > 82 int main(){ > 83 > 84 CASE(0) > 85 int leaving_[] = { 3 }; > 86 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 87 int k = 3; > 88 int _ = 1; > 89 END > 90 CASE(1) > 91 int leaving_[] = { 5, 9 }; > 92 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 93 int k = 10; > 94 int _ = 6; > 95 END > 96 CASE(2) > 97 int leaving_[] = { 5, 10, 15 }; > 98 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 99 int k = 19; > 100 int _ = 212; > 101 END > 102 CASE(3) > 103 int leaving_[] = { 2 }; > 104 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 105 int k = 10000000; > 106 int _ = 0; > 107 END > 108 CASE(4) > 109 int leaving_[] = { 195, 211, 227, 230, 260, > 110 297, 346, 350, 403, 411, > 111 428, 485, 594, 606, 876 } > 112 ; > 113 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 114 int k = 1000; > 115 int _ = 975206486; > 116 END > 117 CASE(5) > 118 int leaving_[] = {2}; > 119 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 120 int k = 1; > 121 int _ = 1; > 122 END > 123 CASE(5) > 124 int leaving_[] = {2}; > 125 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 126 int k = 2; > 127 int _ = 0; > 128 END > 129 CASE(5) > 130 int leaving_[] = {1}; > 131 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 132 int k = 1; > 133 int _ = 0; > 134 END > 135 CASE(6) > 136 int leaving_[] = {999}; > 137 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 138 int k = 10000000; > 139 int _ = 536491299; > 140 END > 141 CASE(7) > 142 int leaving_[] = { > 143 100,200,300,400,500,600,700,800,900,1000, > 144 1100,1200,1300,1400,1500,1600,1700,1800,1900,2000, > 145 3100,3200,3300,3400,3500,3600,3700,3800,3900,31000, > 146 43100,43200,43300,43400,43500,43600,43700,43800,43900,431000, > 147 999100,999200,999300,999400,999500,999600,999700,999800,999900,9991000, > 148 }; > 149 vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leavi > 150 int k = 10000000; > 151 int _ = 260357174; > 152 END > 153 } > 154 // END CUT HERE
Added SRM/476-U/1A.cpp version [e10f449b73402d2c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Badgers { public: > 23 int feedMost(vector <int> hunger, vector <int> greed, int totalFood) > 24 { > 25 for(int m=hunger.size(); m>0; --m) > 26 if( can(m, hunger, greed, totalFood) ) > 27 return m; > 28 return 0; > 29 } > 30 bool can(int m, vector<int> h, const vector<int>& g, int totalFood) > 31 { > 32 for(int i=0; i<h.size(); ++i) > 33 h[i] += g[i]*(m-1); > 34 sort(h.begin(), h.end()); > 35 return accumulate(h.begin(), h.begin()+m, 0) <= totalFood; > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const int& Expected, const int& Received) { > 48 bool ok = (Expected == Received); > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, Badgers().feedMost(hunger, greed, totalFood));} > 53 int main(){ > 54 > 55 CASE(0) > 56 int hunger_[] = {1,2,3}; > 57 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 58 int greed_[] = {2,2,1}; > 59 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 60 int totalFood = 7; > 61 int _ = 2; > 62 END > 63 CASE(1) > 64 int hunger_[] = {5,2,1,5}; > 65 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 66 int greed_[] = {0,2,4,1}; > 67 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 68 int totalFood = 19; > 69 int _ = 3; > 70 END > 71 CASE(2) > 72 int hunger_[] = {1,1,1,1,1}; > 73 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 74 int greed_[] = {1000,1000,1000,1000,1000}; > 75 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 76 int totalFood = 10; > 77 int _ = 1; > 78 END > 79 CASE(3) > 80 int hunger_[] = {1,2,3,4,5,6,7,8,9,10}; > 81 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 82 int greed_[] = {10,9,8,7,6,5,4,3,2,1}; > 83 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 84 int totalFood = 100; > 85 int _ = 5; > 86 END > 87 CASE(4) > 88 int hunger_[] = {2}; > 89 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 90 int greed_[] = {0}; > 91 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 92 int totalFood = {1}; > 93 int _ = 0; > 94 END > 95 CASE(4) > 96 int hunger_[] = {2}; > 97 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 98 int greed_[] = {0}; > 99 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 100 int totalFood = {2}; > 101 int _ = 1; > 102 END > 103 CASE(4) > 104 int hunger_[] = {2}; > 105 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 106 int greed_[] = {0}; > 107 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 108 int totalFood = {1000000}; > 109 int _ = 1; > 110 END > 111 CASE(5) > 112 int hunger_[] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10, > 113 vector <int> hunger(hunger_, hunger_+sizeof(hunger_)/sizeof(*hunger_)) > 114 int greed_[] = {100,100,100,100,100,100,100,100,100,100,100,100,100,10 > 115 vector <int> greed(greed_, greed_+sizeof(greed_)/sizeof(*greed_)); > 116 int totalFood = 10000; > 117 int _ = -1; > 118 END > 119 > 120 } > 121 // END CUT HERE
Added SRM/476-U/1B.cpp version [98c2d671691b0a48]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP2 > 24 { > 25 int N1, N2; > 26 vector<T> data; > 27 DP2(){} > 28 DP2(int N1, int N2, const T& t = T()) > 29 : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)< > 30 T& operator()(int i1, int i2) > 31 { return data[ (i1*N2)+i2 ]; } > 32 void swap(DP2& rhs) > 33 { data.swap(rhs.data); } > 34 }; > 35 > 36 class FriendTour { public: > 37 double tourProbability(vector <string> friends, int K) > 38 { > 39 int N = (int)friends.size(); > 40 vector< vector<int> > f(N); > 41 for(int i=0; i<N; ++i) > 42 { > 43 stringstream sin( friends[i] ); > 44 for(int v; sin>>v; ) > 45 f[i].push_back(v-1); > 46 } > 47 > 48 map<int,int> rename; > 49 rename[0] = 0; > 50 for(int k=0; k<f[0].size(); ++k) > 51 { > 52 int newid = rename.size(); > 53 rename[f[0][k]] = newid; > 54 } > 55 > 56 vector< vector<int> > ff(rename.size()); > 57 for(int i=0; i<N; ++i) > 58 if( rename.count(i) ) > 59 { > 60 int k = rename[i]; > 61 for(int j=0; j<f[i].size(); ++j) > 62 if( f[i][j]!=0 && rename.count(f[i][j]) > 63 ff[k].push_back( rename[f[i][j]] > 64 else > 65 ff[k].push_back( -1 ); > 66 } > 67 return solve(ff, K); > 68 } > 69 > 70 double solve(vector<vector<int> >& f, int K) > 71 { > 72 memo = DP2<double>(f.size(), 1<<f.size(), -1); > 73 return rec(f, K, 0, 1); > 74 } > 75 > 76 double C(int N, int K) > 77 { > 78 double p = 1.0; > 79 for(int i=0; i<min(K,N-K); ++i) > 80 p = p * (N-i) / (1+i); > 81 return p; > 82 } > 83 > 84 DP2<double> memo; > 85 double rec(vector<vector<int> >& f, int K, int v, int mask) > 86 { > 87 if( memo(v,mask) != -1 ) > 88 return memo(v,mask); > 89 > 90 if( mask == (1<<f.size())-1 ) > 91 return 1.0; > 92 > 93 vector<double> nxt; > 94 for(int i=0; i<f[v].size(); ++i) > 95 if( f[v][i]==-1 || (mask & (1<<f[v][i])) ) > 96 nxt.push_back(0); > 97 else > 98 nxt.push_back( rec(f,K,f[v][i],mask|(1<<f[v][i]) > 99 sort(nxt.rbegin(), nxt.rend()); > 100 int N = nxt.size(); > 101 if( N == 0 ) > 102 return memo(v,mask) = 0.0; > 103 > 104 if( N <= K ) > 105 return memo(v,mask) = nxt[0]; > 106 > 107 double p = 0.0; > 108 for(int i=0; i<nxt.size(); ++i) { > 109 if( N-1-i < K ) { > 110 p += C(N-i,K) / C(N,K) * nxt[i]; > 111 break; > 112 } > 113 p += (C(N-i,K) - C(N-1-i,K)) / C(N,K) * nxt[i]; > 114 } > 115 return memo(v,mask) = p; > 116 } > 117 }; > 118 > 119 // BEGIN CUT HERE > 120 #include <ctime> > 121 double start_time; string timer() > 122 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 123 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 124 { os << "{ "; > 125 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 126 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 127 void verify_case(const double& Expected, const double& Received) { > 128 bool ok = (abs(Expected - Received) < 1e-9); > 129 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 130 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 131 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 132 #define END verify_case(_, FriendTour().tourProbability(friends, K));} > 133 int main(){ > 134 > 135 CASE(0) > 136 string friends_[] = {"2 3 4", > 137 "1 3 4", > 138 "1 2 4", > 139 "1 2 3"}; > 140 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 141 int K = 1; > 142 double _ = 0.2222222222222222; > 143 END > 144 CASE(1) > 145 string friends_[] = {"2 3 4", > 146 "1 3 4", > 147 "1 2 4", > 148 "1 2 3"}; > 149 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 150 int K = 2; > 151 double _ = 0.6666666666666666; > 152 END > 153 CASE(2) > 154 string friends_[] = {"3 2 4", > 155 "3 5 1", > 156 "5 2 1 4", > 157 "3 1 5", > 158 "3 2 4"}; > 159 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 160 int K = 2; > 161 double _ = 0.3333333333333333; > 162 END > 163 CASE(3) > 164 string friends_[] = {"3 2 4", > 165 "3 5 1", > 166 "5 2 1 4", > 167 "3 1 5 6", > 168 "3 2 4", > 169 "4"} > 170 ; > 171 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 172 int K = 2; > 173 double _ = 0.3055555555555556; > 174 END > 175 CASE(4) > 176 string friends_[] = {"6 5 4 2", > 177 "1 6 3 5", > 178 "5 4 2", > 179 "3 1 5", > 180 "2 4 3 1 6", > 181 "1 2 5"}; > 182 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 183 int K = 3; > 184 double _ = 0.73125; > 185 END > 186 CASE(5) > 187 string friends_[] = {"2 3 4 5 6 7 8 9 10 11 12 13 14 15 16", "1 3 4 5 6 > 188 vector <string> friends(friends_, friends_+sizeof(friends_)/sizeof(*fr > 189 int K = 3; > 190 double _ = 0.010989546288518458; > 191 END > 192 } > 193 // END CUT HERE
Added SRM/476-U/1C-U.cpp version [c81f1fbeaeaef3fd]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SpaceshipEvacuation { public: > 23 int additionalCabins(int N, vector <string> tunnelNetwork, int crewSize) > 24 { > 25 > 26 } > 27 }; > 28 > 29 // BEGIN CUT HERE > 30 #include <ctime> > 31 double start_time; string timer() > 32 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 33 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 34 { os << "{ "; > 35 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 36 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 37 void verify_case(const int& Expected, const int& Received) { > 38 bool ok = (Expected == Received); > 39 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 40 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 41 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 42 #define END verify_case(_, SpaceshipEvacuation().additionalCabins(N, tunnel > 43 int main(){ > 44 > 45 CASE(0) > 46 int N = 3; > 47 string tunnelNetwork_[] = {"0 1 5 3", > 48 "2 1 0 0"}; > 49 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 50 int crewSize = 5; > 51 int _ = 7; > 52 END > 53 CASE(1) > 54 int N = 3; > 55 string tunnelNetwork_[] = {"0 1 0 2", > 56 "0 2 0 4"}; > 57 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 58 int crewSize = 5; > 59 int _ = 4; > 60 END > 61 CASE(2) > 62 int N = 4; > 63 string tunnelNetwork_[] = {"0 1 0 6", > 64 "3 2 3 1", > 65 "2 1 0 1", > 66 "3 1 2 2"}; > 67 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 68 int crewSize = 6; > 69 int _ = 6; > 70 END > 71 CASE(3) > 72 int N = 10; > 73 string tunnelNetwork_[] = {"0 1 11 101", > 74 "1 2 0 100", > 75 "2 3 20 100", > 76 "3 4 0 107", > 77 "4 1 66 0", > 78 "3 5 104 2", > 79 "4 6 82 0", > 80 "5 7 25 25", > 81 "7 8 14 42", > 82 "8 9 0 94", > 83 "9 5 17 92"}; > 84 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 85 int crewSize = 110; > 86 int _ = 376; > 87 END > 88 CASE(4) > 89 int N = 3; > 90 string tunnelNetwork_[] = {"0 1 0 0"}; > 91 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 92 int crewSize = 1; > 93 int _ = -1; > 94 END > 95 CASE(5) > 96 int N = ; > 97 string tunnelNetwork_[] = ; > 98 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 99 int crewSize = ; > 100 int _ = ; > 101 END > 102 CASE(6) > 103 int N = ; > 104 string tunnelNetwork_[] = ; > 105 vector <string> tunnelNetwork(tunnelNetwork_, tunnelNetwork_+sizeof(tu > 106 int crewSize = ; > 107 int _ = ; > 108 END > 109 > 110 } > 111 // END CUT HERE
Added SRM/478-U/1A.cpp version [b7f0b3a8ec812310]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 class CarrotJumping { public: > 24 int theJump(int init) > 25 { > 26 LL x = (init+1) % MODVAL; > 27 int i = 2; > 28 x = (x*4) % MODVAL; > 29 while(i < 1000000){ > 30 if( x == 1 ) { > 31 int z = i/3 + (i%3 ? 1 : 0); > 32 return z>100000 ? -1 : z; > 33 } > 34 i++; > 35 x = (x*2)% MODVAL; > 36 } > 37 return -1; > 38 } > 39 }; > 40 > 41 // BEGIN CUT HERE > 42 #include <ctime> > 43 double start_time; string timer() > 44 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 45 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 46 { os << "{ "; > 47 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 48 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 49 void verify_case(const int& Expected, const int& Received) { > 50 bool ok = (Expected == Received); > 51 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 52 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 53 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 54 #define END verify_case(_, CarrotJumping().theJump(init));} > 55 int main(){ > 56 > 57 CASE(0) > 58 int init = 125000000; > 59 int _ = 1; > 60 END > 61 CASE(1) > 62 int init = 281250001; > 63 int _ = 2; > 64 END > 65 CASE(2) > 66 int init = 18426114; > 67 int _ = 58; > 68 END > 69 CASE(3) > 70 int init = 4530664; > 71 int _ = 478; > 72 END > 73 CASE(4) > 74 int init = 705616876; > 75 int _ = 100000; > 76 END > 77 CASE(5) > 78 int init = 852808441; > 79 int _ = -1; > 80 END > 81 CASE(6) > 82 int init = 1; > 83 int _ = -2; > 84 END > 85 CASE(7) > 86 int init = 1000000006; > 87 int _ = -2; > 88 END > 89 > 90 } > 91 // END CUT HERE
Added SRM/478-U/1B.cpp version [3cbbfe4224f260a7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class KiwiJuice { public: > 23 int theProfit(int C, vector <int> bottles, vector <int> prices) > 24 { > 25 best.clear(); > 26 best.resize(1<<bottles.size(), -1); > 27 return rec((1<<bottles.size())-1, C, bottles, prices); > 28 } > 29 > 30 vector<int> best; > 31 int rec(int mask, int C, vector<int>& B, const vector<int>& P) > 32 { > 33 if( mask == 0 ) > 34 return 0; > 35 if( best[mask] != -1 ) > 36 return best[mask]; > 37 int maxSc = 0; > 38 for(int subset=mask; subset; subset=((subset-1)&mask)) { > 39 int sum = 0; > 40 int bin = 0; > 41 for(int i=0; i<B.size(); ++i) > 42 if( subset & (1<<i) ) { > 43 sum += B[i]; > 44 bin += 1; > 45 } > 46 int sc = P[C] * (sum/C); > 47 if(sum%C) sc += P[sum%C]; > 48 sc += P[0] * (bin-((sum/C)+(sum%C?1:0))); > 49 sc += rec(mask&~subset, C, B, P); > 50 maxSc = max(maxSc, sc); > 51 } > 52 return best[mask] = maxSc; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const int& Expected, const int& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, KiwiJuice().theProfit(C, bottles, prices));} > 70 int main(){ > 71 > 72 CASE(0) > 73 int C = 10; > 74 int bottles_[] = {5, 8}; > 75 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 76 int prices_[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10}; > 77 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 78 int _ = 10; > 79 END > 80 CASE(1) > 81 int C = 10; > 82 int bottles_[] = {5, 8}; > 83 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 84 int prices_[] = {0, 0, 0, 0, 0, 10, 10, 10, 10, 10, 10}; > 85 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 86 int _ = 20; > 87 END > 88 CASE(2) > 89 int C = 10; > 90 int bottles_[] = {4, 5, 3, 7}; > 91 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 92 int prices_[] = {14, 76, 12, 35, 6, 94, 26, 3, 93, 90, 420}; > 93 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 94 int _ = 625; > 95 END > 96 CASE(3) > 97 int C = 1; > 98 int bottles_[] = {0}; > 99 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 100 int prices_[] = {1000000, 1000000}; > 101 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 102 int _ = 1000000; > 103 END > 104 CASE(4) > 105 int C = 49; > 106 int bottles_[] = {2, 47, 24, 14, 0, 32, 9, 12, 31, 46, 39, 12, 16, 22, 2 > 107 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 108 int prices_[] = {428668, 995687, 128567, 37241, 496916, 238624, 304781, > 109 398570, 951499, 750349, 333625, 927594, 188616, 942498, 259414, 654344, 354809, > 110 25303, 226854, 98578, 207140, 305527, 358343, 393213, 256248, 282644, 103327, > 111 667191, 103402, 609183, 619323, 189127, 518006, 778846, 400460, 128334, 795097, > 112 605203, 669142, 121825, 934404, 837430, 554265, 610818, 546228, 80784, 949440}; > 113 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 114 int _ = 12873962; > 115 END > 116 CASE(5) > 117 int C = 10; > 118 int bottles_[] = {2,3,5}; > 119 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 120 int prices_[] = {0,0,9,9,0,9,0,0,0,0,99}; > 121 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 122 int _ = 99; > 123 END > 124 /* > 125 CASE(6) > 126 int C = ; > 127 int bottles_[] = ; > 128 vector <int> bottles(bottles_, bottles_+sizeof(bottles_)/sizeof(*bottl > 129 int prices_[] = ; > 130 vector <int> prices(prices_, prices_+sizeof(prices_)/sizeof(*prices_)) > 131 int _ = ; > 132 END > 133 */ > 134 } > 135 // END CUT HERE
Added SRM/479-U/1A-U.cpp version [816649b6f120c38e]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheCoffeeTimeDivOne { public: > 23 long long find(int ____n, vector <int> tea) > 24 { > 25 sort(tea.begin(), tea.end()); > 26 > 27 LL n = ____n; > 28 LL t = n*4 + 47LL*((tea.size()+6)/7) + 47LL*((n-tea.size()+6)/7) > 29 > 30 for(int tp=tea.size()-1; tp>=0; ) > 31 { > 32 t += tea[tp]*2; > 33 tp -= 7; > 34 } > 35 > 36 if( n > tea.size() ) > 37 { > 38 LL s = (n-tea.size()) % 7; > 39 if(s==0) s = 7; > 40 for(int tp=0; s<=n; s+=7) > 41 { > 42 while(tp<tea.size() && tea[tp]<=s) > 43 ++s, ++tp; > 44 t += 2*s; > 45 } > 46 } > 47 return t; > 48 } > 49 }; > 50 > 51 // BEGIN CUT HERE > 52 #include <ctime> > 53 double start_time; string timer() > 54 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 55 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 56 { os << "{ "; > 57 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 58 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 59 void verify_case(const long long& Expected, const long long& Received) { > 60 bool ok = (Expected == Received); > 61 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 62 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 63 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 64 #define END verify_case(_, TheCoffeeTimeDivOne().find(n, tea));} > 65 int main(){ > 66 > 67 CASE(0) > 68 int n = 2; > 69 int tea_[] = {1}; > 70 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 71 long long _ = 108LL; > 72 END > 73 CASE(1) > 74 int n = 2; > 75 int tea_[] = {2, 1}; > 76 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 77 long long _ = 59LL; > 78 END > 79 CASE(2) > 80 int n = 15; > 81 int tea_[] = {1, 2, 3, 4, 5, 6, 7}; > 82 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 83 long long _ = 261LL; > 84 END > 85 CASE(3) > 86 int n = 47; > 87 int tea_[] = {1, 10, 6, 2, 4}; > 88 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 89 long long _ = 891LL; > 90 END > 91 CASE(4) > 92 int n = 44777777; > 93 int tea_[] = {1,2,4,8,16,32,64,128,1000,2000,3000,4000,5000,6000,7000,80 > 94 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 95 long long _ = -1LL; > 96 END > 97 /* > 98 CASE(5) > 99 int n = ; > 100 int tea_[] = ; > 101 vector <int> tea(tea_, tea_+sizeof(tea_)/sizeof(*tea_)); > 102 long long _ = LL; > 103 END > 104 */ > 105 } > 106 // END CUT HERE
Added SRM/479-U/1B.cpp version [5d5ec3acb844c5c9]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheAirTripDivOne { public: > 23 struct edge { > 24 int to; > 25 LL F, T, P; > 26 }; > 27 int find(int n, vector <string> flights, int time) > 28 { > 29 vector< vector<edge> > e(n); > 30 { > 31 string input = accumulate(flights.begin(), flights.end() > 32 stringstream sin(input); > 33 for(string line; sin>>line;) { > 34 stringstream sin(line); > 35 int a,b; LL f,t,p; char _; > 36 sin >> a >> _ >> b >> _ >> f >> _ >> t >> _ >> p > 37 edge eg = {b-1,f,t,p}; > 38 e[a-1].push_back(eg); > 39 } > 40 } > 41 > 42 int lf=0, ri=1000000001; > 43 while( ri-lf>1 ) > 44 { > 45 int c = (lf+ri)/2; > 46 (ok(n,e,c,time) ? lf : ri) = c; > 47 } > 48 return lf ? lf : -1; > 49 } > 50 bool ok(int n, vector< vector<edge> >& e, LL transfer, LL TL) > 51 { > 52 typedef int vert; > 53 typedef LL cost; > 54 typedef pair<cost,int> cedge; > 55 priority_queue<cedge, vector<cedge>, greater<cedge> > Q; > 56 set<vert> V; > 57 Q.push( cedge(0,0) ); > 58 while( !Q.empty() ) > 59 { > 60 cost c = Q.top().first; > 61 vert v = Q.top().second; > 62 Q.pop(); > 63 if( V.count(v) ) > 64 continue; > 65 if( v == n-1 ) > 66 return true; > 67 V.insert(v); > 68 vector<edge>& ne = e[v]; > 69 for(int i=0; i<ne.size(); ++i) > 70 { > 71 vert u = ne[i].to; > 72 LL F=ne[i].F, T=ne[i].T, P=ne[i].P; > 73 LL t = nextTime(c, F, P, v==0 ? 0 : transfer); > 74 // cerr<<c<<" "<<F<<" "<<P<<" "<<transfer<<" !! "<< > 75 if( !V.count(u) && t+T<=TL ) > 76 Q.push( cedge(t+T, u) ); > 77 } > 78 } > 79 return false; > 80 } > 81 > 82 LL nextTime(LL cur, LL F, LL P, LL transfer) > 83 { > 84 cur += transfer; > 85 if( cur <= F ) > 86 return F; > 87 return (cur-F)%P==0 ? cur : ((cur-F)/P+1)*P+F; > 88 } > 89 }; > 90 > 91 // BEGIN CUT HERE > 92 #include <ctime> > 93 double start_time; string timer() > 94 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 95 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 96 { os << "{ "; > 97 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 98 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 99 void verify_case(const int& Expected, const int& Received) { > 100 bool ok = (Expected == Received); > 101 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 102 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 103 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 104 #define END verify_case(_, TheAirTripDivOne().find(n, flights, time));} > 105 int main(){ > 106 > 107 CASE(0) > 108 int n = 3; > 109 string flights_[] = {"1,2,1,4,7 ", "2,3,9,1,10"}; > 110 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 111 int time = 20; > 112 int _ = 14; > 113 END > 114 CASE(1) > 115 int n = 3; > 116 string flights_[] = {"1,2,1,1,1 2,3,2,1,98"}; > 117 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 118 int time = 100; > 119 int _ = -1; > 120 END > 121 CASE(2) > 122 int n = 477; > 123 string flights_[] = {"47,74,1,1,1"}; > 124 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 125 int time = 20; > 126 int _ = -1; > 127 END > 128 CASE(3) > 129 int n = 7; > 130 string flights_[] = {"1,3,15,17,11 1,3,14,16,14 5,7,12,18,18 1,6,13,1", > 131 "6,12 1,2,18,14,13 5,6,10,10,18 3,1,10,10,10 1,3", > 132 ",17,16,10 2,3,16,18,16 6,1,15,12,10 2,1,15,18,1", > 133 "0 4,7,10,16,15 6,3,10,14,10 1,6,19,19,15 1,4,12", > 134 ",10,14 4,7,10,18,14 2,3,16,12,12 1,3,14,10,19 3", > 135 ",7,17,10,12 2,1,14,12,16 4,3,19,11,12 1,6,10,18", > 136 ",12 2,3,16,12,10 6,2,10,18,12 5,1,14,18,12 5,1,", > 137 "18,10,10 3,2,19,15,10 7,4,16,19,14 6,3,16,12,10", > 138 " 5,7,14,13,13 1,3,12,10,16 5,7,16,18,15 6,2,18,", > 139 "12,14 3,2,10,18,16 4,2,18,18,14 1,5,10,18,16 2,", > 140 "3,10,19,16 1,4,11,18,15 2,1,15,15,14 7,2,10,12,", > 141 "10"}; > 142 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 143 int time = 74; > 144 int _ = 33; > 145 END > 146 CASE(4) > 147 int n = 7; > 148 string flights_[] = {"1,4,10,8,2 4,6,14,8,2 6,2,8,1", > 149 "0,1 2,7,19,5,1 ", > 150 "1,5,15,17,11 5,3,10,1", > 151 "0,18 3,7,12,18,18"}; > 152 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 153 int time = 147; > 154 int _ = 35; > 155 END > 156 /* > 157 CASE(5) > 158 int n = ; > 159 string flights_[] = ; > 160 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 161 int time = ; > 162 int _ = ; > 163 END > 164 CASE(6) > 165 int n = ; > 166 string flights_[] = ; > 167 vector <string> flights(flights_, flights_+sizeof(flights_)/sizeof(*fl > 168 int time = ; > 169 int _ = ; > 170 END > 171 */ > 172 } > 173 // END CUT HERE
Added SRM/479-U/2A.cpp version [569c7cb5f3c676ef]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheAirTripDivTwo { public: > 23 int find(vector <int> flights, int fuel) > 24 { > 25 partial_sum(flights.begin(), flights.end(), flights.begin()); > 26 return upper_bound(flights.begin(), flights.end(), fuel) - fligh > 27 } > 28 }; > 29 > 30 // BEGIN CUT HERE > 31 #include <ctime> > 32 double start_time; string timer() > 33 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 34 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 35 { os << "{ "; > 36 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 37 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 38 void verify_case(const int& Expected, const int& Received) { > 39 bool ok = (Expected == Received); > 40 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 41 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 42 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 43 #define END verify_case(_, TheAirTripDivTwo().find(flights, fuel));} > 44 int main(){ > 45 > 46 CASE(0) > 47 int flights_[] = {1, 2, 3, 4, 5, 6, 7}; > 48 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 49 int fuel = 10; > 50 int _ = 4; > 51 END > 52 CASE(1) > 53 int flights_[] = {7, 6, 5, 4, 3, 2, 1}; > 54 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 55 int fuel = 10; > 56 int _ = 1; > 57 END > 58 CASE(2) > 59 int flights_[] = {1}; > 60 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 61 int fuel = 1000; > 62 int _ = 1; > 63 END > 64 CASE(3) > 65 int flights_[] = {8, 7, 7, 1, 5, 7, 9}; > 66 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 67 int fuel = 21; > 68 int _ = 2; > 69 END > 70 /* > 71 CASE(4) > 72 int flights_[] = ; > 73 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 74 int fuel = ; > 75 int _ = ; > 76 END > 77 CASE(5) > 78 int flights_[] = ; > 79 vector <int> flights(flights_, flights_+sizeof(flights_)/sizeof(*fligh > 80 int fuel = ; > 81 int _ = ; > 82 END > 83 */ > 84 } > 85 // END CUT HERE
Added SRM/480-U/1A.cpp version [c4cc4d65db9bedbb]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class InternetSecurity { public: > 23 vector <string> determineWebsite(vector <string> address, vector <string > 24 { > 25 int N = address.size(); > 26 > 27 vector< set<string> > kwd(N); > 28 for(int i=0; i<N; ++i) { > 29 stringstream ss(keyword[i]); > 30 for(string s; ss >> s; ) > 31 kwd[i].insert(s); > 32 } > 33 > 34 set<string> dng(dangerous.begin(), dangerous.end()); > 35 > 36 vector<bool> is_danger(N); > 37 > 38 bool changed = true; > 39 while( changed ) > 40 { > 41 changed = false; > 42 for(int i=0; i<N; ++i) > 43 if( !is_danger[i] ) > 44 { > 45 vector<string> buf; > 46 set_intersection( kwd[i].begin(), kwd[i] > 47 if( buf.size() >= threshold ) > 48 { > 49 changed = true; > 50 dng.insert(kwd[i].begin(), kwd[i > 51 is_danger[i] = true; > 52 } > 53 } > 54 } > 55 > 56 vector<string> result; > 57 for(int i=0; i<N; ++i) > 58 if( is_danger[i] ) > 59 result.push_back( address[i] ); > 60 return result; > 61 } > 62 }; > 63 > 64 // BEGIN CUT HERE > 65 #include <ctime> > 66 double start_time; string timer() > 67 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 68 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 69 { os << "{ "; > 70 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 71 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 72 void verify_case(const vector <string>& Expected, const vector <string>& Receive > 73 bool ok = (Expected == Received); > 74 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 75 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 76 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 77 #define END verify_case(_, InternetSecurity().determineWebsite(address, key > 78 int main(){ > 79 > 80 CASE(0) > 81 string address_[] = {"www.topcoder.com", > 82 "www.sindicate_of_evil.com", > 83 "www.happy_citizens.com"}; > 84 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 85 string keyword_[] = {"hack encryption decryption internet algorithm", > 86 "signal interference evil snake poison algorithm", > 87 "flower baloon topcoder blue sky sea"}; > 88 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 89 string dangerous_[] = {"hack","encryption","decryption","interference"," > 90 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 91 int threshold = 3; > 92 string __[] = {"www.topcoder.com", "www.sindicate_of_evil.com" }; > 93 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 94 END > 95 CASE(1) > 96 string address_[] = {"brokenlink","flowerpower.net","purchasedomain.com" > 97 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 98 string keyword_[] = {"broken","rose tulips","cheap free domain biggest g > 99 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 100 string dangerous_[] = {"biggest","enemy","hideout"}; > 101 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 102 int threshold = 2; > 103 vector <string> _; > 104 END > 105 CASE(2) > 106 string address_[] = {"a..a.ab.","...aa.b"}; > 107 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 108 string keyword_[] = {"a bc def","def ghij klmno"}; > 109 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 110 string dangerous_[] = {"a","b","c","d","e"}; > 111 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 112 int threshold = 1; > 113 string __[] = {"a..a.ab.", "...aa.b" }; > 114 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 115 END > 116 CASE(3) > 117 string address_[] = {"www.tsa.gov"}; > 118 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 119 string keyword_[] = {"information assurance signal intelligence research > 120 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 121 string dangerous_[] = {"signal","assurance","penguin"}; > 122 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 123 int threshold = 2; > 124 string __[] = {"www.tsa.gov" }; > 125 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 126 END > 127 CASE(4) > 128 string address_[] = {"","","","","","","","","","","","","","","","","","","","" > 129 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 130 string keyword_[] = {"a b c d e f g h i j k l m n o p q r s t u v w x > 131 "A B C D E F G H I J K L M N O P Q R S T U V W X Y","a b c d e > 132 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 133 string dangerous_[] = {"a","b","c"}; > 134 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 135 int threshold = 3; > 136 string __[] = {"XXX"}; > 137 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 138 END > 139 /* > 140 CASE(5) > 141 string address_[] = ; > 142 vector <string> address(address_, address_+sizeof(address_)/sizeof(*ad > 143 string keyword_[] = ; > 144 vector <string> keyword(keyword_, keyword_+sizeof(keyword_)/sizeof(*ke > 145 string dangerous_[] = ; > 146 vector <string> dangerous(dangerous_, dangerous_+sizeof(dangerous_)/si > 147 int threshold = ; > 148 string __[] = ; > 149 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 150 END > 151 */ > 152 } > 153 // END CUT HERE
Added SRM/480-U/1B.cpp version [4c32e38aec559696]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class NetworkSecurity { public: > 23 int secureNetwork(vector <string> clientCable, vector <string> serverCab > 24 { > 25 int N = clientCable.size(); > 26 int M = serverCable[0].size(); > 27 > 28 vector< vector<int> > cd(N, vector<int>(N)); > 29 for(int i=0; i<N; ++i) > 30 for(int j=0; j<N; ++j) > 31 if( clientCable[i][j]=='Y' ) > 32 cd[i][j] = 1; > 33 for(int k=0; k<N; ++k) > 34 for(int i=0; i<N; ++i) > 35 for(int j=0; j<N; ++j) > 36 cd[i][j] |= cd[i][k] & cd[k][j]; > 37 > 38 int cnt = 0; > 39 for(int i=0; i<N; ++i) > 40 for(int j=0; j<M; ++j) > 41 { > 42 if( serverCable[i][j]=='Y' ) > 43 { > 44 for(int k=0; k<N; ++k) > 45 if( cd[i][k]==1 && serverCable[k > 46 goto next; > 47 ++cnt; > 48 next:; > 49 } > 50 } > 51 return cnt; > 52 } > 53 }; > 54 > 55 > 56 > 57 // Powered by FileEdit > 58 // Powered by TZTester 1.01 [25-Feb-2003] : <cafelier&naoya_t>-custom > 59 // Powered by CodeProcessor
Added SRM/481/1A.cpp version [5beaeafc55fc55b6]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ChickenOracle { public: > 23 string theTruth(int n, int eggCount, int lieCount, int liarCount) > 24 { > 25 bool egg = canTruthTellerBe( eggCount, n, lieCount, liarCount > 26 bool chk = canTruthTellerBe( n-eggCount, n, lieCount, liarCount > 27 if( egg && chk ) return "Ambiguous"; > 28 if( egg ) return "The egg"; > 29 if( chk ) return "The chicken"; > 30 return "The oracle is a lie"; > 31 } > 32 bool canTruthTellerBe( int A, int n, int B, int C ) > 33 { > 34 int two_x = A+B+C - n; > 35 if( (two_x&1) == 1 ) > 36 return false; > 37 int x = two_x/2; > 38 return 0<=x && x<=C && x<=B && (C-x)<=(n-B); > 39 } > 40 }; > 41 > 42 // BEGIN CUT HERE > 43 #include <ctime> > 44 double start_time; string timer() > 45 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 46 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 47 { os << "{ "; > 48 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 49 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 50 void verify_case(const string& Expected, const string& Received) { > 51 bool ok = (Expected == Received); > 52 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 53 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 54 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 55 #define END verify_case(_, ChickenOracle().theTruth(n, eggCount, lieCount, > 56 int main(){ > 57 > 58 CASE(0) > 59 int n = 10; > 60 int eggCount = 10; > 61 int lieCount = 0; > 62 int liarCount = 0; > 63 string _ = "The egg"; > 64 END > 65 CASE(1) > 66 int n = 60; > 67 int eggCount = 40; > 68 int lieCount = 0; > 69 int liarCount = 30; > 70 string _ = "The oracle is a lie"; > 71 END > 72 CASE(2) > 73 int n = 60; > 74 int eggCount = 20; > 75 int lieCount = 5; > 76 int liarCount = 25; > 77 string _ = "The chicken"; > 78 END > 79 CASE(3) > 80 int n = 1000; > 81 int eggCount = 500; > 82 int lieCount = 250; > 83 int liarCount = 250; > 84 string _ = "Ambiguous"; > 85 END > 86 CASE(4) > 87 int n = 338919; > 88 int eggCount = 310645; > 89 int lieCount = 70422; > 90 int liarCount = 304279; > 91 string _ = "The oracle is a lie"; > 92 END > 93 CASE(5) > 94 int n = 1; > 95 int eggCount = 0; > 96 int lieCount = 0; > 97 int liarCount = 0; > 98 string _ = "The chicken"; > 99 END > 100 } > 101 // END CUT HERE
Added SRM/481/1B.cpp version [9a6b2ff88d4819cc]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BatchSystemRoulette { public: > 23 vector <double> expectedFinishTimes(vector <int> duration, vector <strin > 24 { > 25 vector<double> answer(duration.begin(), duration.end()); > 26 > 27 map<string, LL> userSum; > 28 for(int i=0; i<user.size(); ++i) > 29 userSum[user[i]] += duration[i]; > 30 map<LL, vector<string> > usInv; > 31 for(map<string,LL>::iterator it=userSum.begin(); it!=userSum.end > 32 usInv[it->second].push_back(it->first); > 33 > 34 double base = 0; > 35 for(map<LL, vector<string> >::iterator it=usInv.begin(); it!=usI > 36 { > 37 LL t = it->first; > 38 vector<string>& us = it->second; > 39 int n = us.size(); > 40 > 41 double baseAve = base + t*(n-1)/2.0; > 42 for(int u=0; u<n; ++u) > 43 { > 44 vector<int> idxs; > 45 for(int i=0; i<answer.size(); ++i) > 46 if( user[i] == us[u] ) { > 47 answer[i] += baseAve; > 48 idxs.push_back(i); > 49 } > 50 for(int k=0; k<idxs.size(); ++k) { > 51 int i = idxs[k]; > 52 for(int m=0; m<idxs.size(); ++m) if(m!=k > 53 int j = idxs[m]; > 54 answer[i] += duration[j]/2.0; > 55 } > 56 } > 57 } > 58 > 59 base += t*n; > 60 } > 61 > 62 return answer; > 63 } > 64 }; > 65 > 66 // BEGIN CUT HERE > 67 #include <ctime> > 68 double start_time; string timer() > 69 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 70 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 71 { os << "{ "; > 72 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 73 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 74 void verify_case(const vector <double>& Expected, const vector <double>& Receive > 75 bool ok = true; > 76 for(int i=0; i<Expected.size(); ++i) > 77 if( abs(Expected[i]-Received[i]) >= 1e-9 ) > 78 ok = false; > 79 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 80 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 81 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 82 #define END verify_case(_, BatchSystemRoulette().expectedFinishTimes(durati > 83 int main(){ > 84 > 85 CASE(0) > 86 int duration_[] = {3, 2, 4, 1}; > 87 vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*d > 88 string user_[] = {"Gil Grissom", "Sarah Sidle", "Warrick Brown", "Cather > 89 vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); > 90 double __[] = {6.0, 3.0, 10.0, 1.0 }; > 91 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 92 END > 93 CASE(1) > 94 int duration_[] = {3, 2, 4, 1}; > 95 vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*d > 96 string user_[] = {"mac taylor", "Mac Taylor", "Mac Taylor", "Peyton Dris > 97 vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); > 98 double __[] = {4.0, 8.0, 9.0, 1.0 }; > 99 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 100 END > 101 CASE(2) > 102 int duration_[] = {13, 14, 15, 56, 56}; > 103 vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*d > 104 string user_[] = {"Tim Speedle", "Tim Speedle", "Tim Speedle", "Horatio > 105 vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); > 106 double __[] = {27.5, 28.0, 28.5, 126.0, 126.0 }; > 107 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 108 END > 109 /* > 110 CASE(3) > 111 int duration_[] = ; > 112 vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*d > 113 string user_[] = ; > 114 vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); > 115 double __[] = ; > 116 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 117 END > 118 CASE(4) > 119 int duration_[] = ; > 120 vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*d > 121 string user_[] = ; > 122 vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); > 123 double __[] = ; > 124 vector <double> _(__, __+sizeof(__)/sizeof(*__)); > 125 END > 126 */ > 127 } > 128 // END CUT HERE
Added SRM/481/1C.cpp version [33f000c44b39ef2d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP3 > 24 { > 25 int N1, N2, N3; > 26 vector<T> data; > 27 DP3(int N1, int N2, int N3, const T& t = T()) > 28 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size() > 29 T& operator()(int i1, int i2, int i3) > 30 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 31 void swap(DP3& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 int bitcnt(int U) > 36 { > 37 int c = 0; > 38 for(; U; U>>=1) > 39 c += (U&1); > 40 return c; > 41 } > 42 > 43 class TicketPrinters { public: > 44 int minTime(int currentPrinter, vector <int> printerDistance, vector <in > 45 { > 46 int N = startingValues.size(); > 47 > 48 vector<int> d(N); > 49 partial_sum( printerDistance.begin(), printerDistance.end(), d.b > 50 > 51 // left, used, LeftOrRight > 52 DP3<int> dp(N,1<<N,2); > 53 for(int D=(1<<N)-1; D>=0; --D) > 54 { > 55 int bc = bitcnt(D); > 56 for(int L=0; L<N && L+bc<=N; ++L) > 57 for(int C=0; C<=1; ++C) > 58 { > 59 if( bc == N ) > 60 dp(L,D,C) = 0; > 61 else { > 62 int cur = (C==0 ? L : L+bc-1); > 63 int mn = 0x7fffffff; > 64 for(int nextW=0; nextW<N; ++next > 65 if( ((1<<nextW) & D) == > 66 int caseLeft = 0 > 67 if( L>0 ) { > 68 caseLeft > 69 > 70 } > 71 int caseRight = > 72 if( L+bc < N ) { > 73 caseRigh > 74 > 75 } > 76 mn = min(mn, min > 77 } > 78 dp(L,D,C) = mn; > 79 } > 80 } > 81 } > 82 return dp(currentPrinter,0,0); > 83 } > 84 }; > 85 > 86 // BEGIN CUT HERE > 87 #include <ctime> > 88 double start_time; string timer() > 89 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 90 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 91 { os << "{ "; > 92 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 93 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 94 void verify_case(const int& Expected, const int& Received) { > 95 bool ok = (Expected == Received); > 96 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 97 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 98 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 99 #define END verify_case(_, TicketPrinters().minTime(currentPrinter, printer > 100 int main(){ > 101 > 102 CASE(0) > 103 int currentPrinter = 0; > 104 int printerDistance_[] = {100, 20, 50}; > 105 vector <int> printerDistance(printerDistance_, printerDistance_+sizeof > 106 int startingValues_[] = {66, 78, 99, 5}; > 107 vector <int> startingValues(startingValues_, startingValues_+sizeof(st > 108 int wantedValues_[] = {99, 5, 78, 66}; > 109 vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedVa > 110 int _ = 171; > 111 END > 112 CASE(1) > 113 int currentPrinter = 1; > 114 int printerDistance_[] = {50, 50}; > 115 vector <int> printerDistance(printerDistance_, printerDistance_+sizeof > 116 int startingValues_[] = {100, 200, 300}; > 117 vector <int> startingValues(startingValues_, startingValues_+sizeof(st > 118 int wantedValues_[] = {101, 201, 302}; > 119 vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedVa > 120 int _ = 152; > 121 END > 122 CASE(2) > 123 int currentPrinter = 2; > 124 int printerDistance_[] = {13, 26, 39, 13}; > 125 vector <int> printerDistance(printerDistance_, printerDistance_+sizeof > 126 int startingValues_[] = {123, 12, 32, 67, 1015}; > 127 vector <int> startingValues(startingValues_, startingValues_+sizeof(st > 128 int wantedValues_[] = {1, 2, 3, 4, 5}; > 129 vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedVa > 130 int _ = 1063; > 131 END > 132 /* > 133 CASE(3) > 134 int currentPrinter = ; > 135 int printerDistance_[] = ; > 136 vector <int> printerDistance(printerDistance_, printerDistance_+sizeof > 137 int startingValues_[] = ; > 138 vector <int> startingValues(startingValues_, startingValues_+sizeof(st > 139 int wantedValues_[] = ; > 140 vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedVa > 141 int _ = ; > 142 END > 143 CASE(4) > 144 int currentPrinter = ; > 145 int printerDistance_[] = ; > 146 vector <int> printerDistance(printerDistance_, printerDistance_+sizeof > 147 int startingValues_[] = ; > 148 vector <int> startingValues(startingValues_, startingValues_+sizeof(st > 149 int wantedValues_[] = ; > 150 vector <int> wantedValues(wantedValues_, wantedValues_+sizeof(wantedVa > 151 int _ = ; > 152 END > 153 */ > 154 } > 155 // END CUT HERE
Added SRM/482-U/1A.cpp version [622c85edd2a0b0bf]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class LockersDivOne { public: > 23 int lastOpened(int N) > 24 { > 25 vector<int> v; > 26 for(int i=1; i<=N; ++i) > 27 v.push_back(i); > 28 int last = -1; > 29 for(int s=2; !v.empty(); ++s) { > 30 vector<int> vv; > 31 for(int i=0; i<v.size(); ++i) > 32 if( i%s>0 ) > 33 vv.push_back(v[i]); > 34 else > 35 last = v[i]; > 36 v.swap(vv); > 37 } > 38 return last; > 39 } > 40 }; > 41 > 42 // BEGIN CUT HERE > 43 #include <ctime> > 44 double start_time; string timer() > 45 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 46 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 47 { os << "{ "; > 48 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 49 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 50 void verify_case(const int& Expected, const int& Received) { > 51 bool ok = (Expected == Received); > 52 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 53 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 54 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 55 #define END verify_case(_, LockersDivOne().lastOpened(N));} > 56 int main(){ > 57 > 58 CASE(0) > 59 int N = 9; > 60 int _ = 6; > 61 END > 62 CASE(1) > 63 int N = 42; > 64 int _ = 42; > 65 END > 66 CASE(2) > 67 int N = 314; > 68 int _ = 282; > 69 END > 70 CASE(4) > 71 int N = 1; > 72 int _ = 1; > 73 END > 74 CASE(5) > 75 int N = 2; > 76 int _ = 2; > 77 END > 78 CASE(6) > 79 int N = 3; > 80 int _ = 2; > 81 END > 82 CASE(7) > 83 int N = 4; > 84 int _ = 4; > 85 END > 86 CASE(7) > 87 int N = 5; > 88 int _ = 4; > 89 END > 90 CASE(7) > 91 int N = 6; > 92 int _ = 6; > 93 END > 94 CASE(7) > 95 int N = 7; > 96 int _ = 6; > 97 END > 98 CASE(7) > 99 int N = 8; > 100 int _ = 6; > 101 END > 102 CASE(3) > 103 int N = 2000000; > 104 int _ = -1; > 105 END > 106 > 107 } > 108 // END CUT HERE
Added SRM/482-U/1B.cpp version [c9cf49d6b744849f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int powi(int a, int b) > 23 { > 24 int v=1; > 25 for(int i=0; i<b; ++i) > 26 v*=a; > 27 return v; > 28 } > 29 > 30 class HanoiGoodAndBad { public: > 31 int moves(int N, int Dave) > 32 { > 33 if( N == 0 ) > 34 return 0; > 35 if( Dave <= (1<<N-1)-1 ) > 36 return moves2(N-1, Dave); > 37 return (powi(3,N-1))*2+moves3(N-1, Dave - (1<<N-1)); > 38 } > 39 int moves2(int N, int Dave) > 40 { > 41 if( N == 0 ) > 42 return 0; > 43 if( Dave <= (1<<N-1)-1 ) > 44 return moves(N-1, Dave); > 45 return powi(3,N-1)+moves2(N-1, Dave - (1<<N-1)); > 46 } > 47 int moves3(int N, int Dave) > 48 { > 49 if( N == 0 ) > 50 return 0; > 51 if( Dave <= (1<<N-1)-1 ) > 52 return powi(3,N-1)+moves3(N-1, Dave); > 53 return (powi(3,N-1))*2+moves(N-1, Dave - (1<<N-1)); > 54 } > 55 }; > 56 > 57 // BEGIN CUT HERE > 58 #include <ctime> > 59 double start_time; string timer() > 60 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 61 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 62 { os << "{ "; > 63 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 64 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 65 void verify_case(const int& Expected, const int& Received) { > 66 bool ok = (Expected == Received); > 67 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 68 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 69 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 70 #define END verify_case(_, HanoiGoodAndBad().moves(N, Dave));} > 71 int main(){ > 72 > 73 CASE(0) > 74 int N = 1; > 75 int Dave = 1; > 76 int _ = 2; > 77 END > 78 CASE(0) > 79 int N = 2; > 80 int Dave = 1; > 81 int _ = 2; > 82 END > 83 CASE(0) > 84 int N = 3; > 85 int Dave = 1; > 86 int _ = 2; > 87 END > 88 CASE(1) > 89 int N = 4; > 90 int Dave = 15; > 91 int _ = 80; > 92 END > 93 CASE(2) > 94 int N = 9; > 95 int Dave = 265; > 96 int _ = 16418; > 97 END > 98 CASE(3) > 99 int N = 1; > 100 int Dave = 0; > 101 int _ = 0; > 102 END > 103 CASE(3) > 104 int N = 1; > 105 int Dave = 1; > 106 int _ = 2; > 107 END > 108 CASE(4) > 109 int N = 19; > 110 int Dave = (1<<19)-1; > 111 int _ = -1; > 112 END > 113 > 114 } > 115 // END CUT HERE
Added SRM/482-U/1C-U.cpp version [65aad8bb5e7b3849]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BalancingAct { public: > 23 vector <string> recover(vector <int> labeled, vector <int> unlabeled) > 24 { > 25 vector<int> same(unlabeled.size()); > 26 for(int i=0; i<same.size(); ++i) > 27 same[i] = count(unlabeled.begin(), unlabeled.end(), unla > 28 > 29 set<unsigned int> canBeMade; > 30 canBeMade.insert(0); > 31 for(int i=0; i<labeled.size(); ++i) { > 32 set<unsigned int> next; > 33 for(set<unsigned int>::iterator it=canBeMade.begin(); it > 34 if(*it+labeled[i] > *it ) > 35 next.insert(*it+labeled[i]); > 36 } > 37 canBeMade.insert(next.begin(), next.end()); > 38 } > 39 vector<unsigned int> cbm(canBeMade.begin(), canBeMade.end()); > 40 > 41 vector<string> answer(unlabeled.size(), "no"); > 42 for(;;) { > 43 bool updated = false; > 44 for(int i=0; i<unlabeled.size(); ++i) > 45 if( answer[i] == "no" ) { > 46 unsigned int ul = unlabeled[i]; > 47 bool ok = false; > 48 if( canMake(ul, cbm) ) > 49 ok = true; > 50 else if( canMake(ul+1, cbm) && canMake(u > 51 ok = true; > 52 else if( same[i]>=2 ) { > 53 for(int k=2; k<=same[i]; ++k) { > 54 if(canMake(ul*k, cbm)) > 55 {ok=true; break; > 56 bool lok = false, rok=fa > 57 for(int j=1; j<=k; ++j) > 58 if(canMake(ul*k- > 59 lok=true > 60 if(canMake(ul*k+ > 61 rok=true > 62 if(lok&&rok){ok= > 63 } > 64 } > 65 } > 66 > 67 if( ok ) { > 68 // update > 69 updated = true; > 70 answer[i] = "yes"; > 71 for(int k=0,ke=cbm.size(); k!=ke > 72 cbm.push_back(cbm[k]+unl > 73 sort(cbm.begin(), cbm.end()); > 74 cbm.erase(unique(cbm.begin(),cbm > 75 } > 76 } > 77 if( !updated ) > 78 break; > 79 } > 80 return answer; > 81 } > 82 > 83 bool canMake( int t, vector<unsigned int>& cbm ) > 84 { > 85 if( t==0 || t==100000001 ) > 86 return true; > 87 if( binary_search(cbm.begin(), cbm.end(), t) ) > 88 return true; > 89 for(vector<unsigned int>::iterator it=cbm.begin(); it!=cbm.end() > 90 if( binary_search(cbm.begin(), cbm.end(), t+*it) ) > 91 return true; > 92 return false; > 93 } > 94 }; > 95 > 96 // BEGIN CUT HERE > 97 #include <ctime> > 98 double start_time; string timer() > 99 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 100 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 101 { os << "{ "; > 102 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 103 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 104 void verify_case(const vector <string>& Expected, const vector <string>& Receive > 105 bool ok = (Expected == Received); > 106 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 107 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 108 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 109 #define END verify_case(_, BalancingAct().recover(labeled, unlabeled));} > 110 int main(){ > 111 > 112 CASE(0) > 113 int labeled_[] = {9,13,15,16}; > 114 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 115 int unlabeled_[] = {19}; > 116 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 117 string __[] = {"yes" }; > 118 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 119 END > 120 CASE(1) > 121 int labeled_[] = {20}; > 122 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 123 int unlabeled_[] = {10,10}; > 124 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 125 string __[] = {"yes", "yes" }; > 126 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 127 END > 128 CASE(2) > 129 int labeled_[] = {33333332,33333334}; > 130 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 131 int unlabeled_[] = {33333333,73,100000000}; > 132 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 133 string __[] = {"yes", "no", "yes" }; > 134 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 135 END > 136 CASE(3) > 137 int labeled_[] = {12}; > 138 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 139 int unlabeled_[] = {1,1,2,2}; > 140 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 141 string __[] = {"yes", "yes", "yes", "yes" }; > 142 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 143 END > 144 CASE(4) > 145 int labeled_[] = {31415926,5358979,32384626,43383279,50288419, > 146 71693993,75105820,9749445,92307816,40628620, > 147 89986280,34825342,11706798,21480865,13282306}; > 148 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 149 int unlabeled_[] = {64709384,46095505,82231725,35940812}; > 150 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 151 string __[] = {"no", "no", "no", "no" }; > 152 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 153 END > 154 CASE(5) > 155 int labeled_[] = {1,2,4,8,16,32,64,128,256,512,1024,2048*1,2048*2,2048*4,2048*8, > 156 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 157 int unlabeled_[] = {999, 888, 777, 666}; > 158 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 159 string __[] = {"yes","yes","yes","yes"}; > 160 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 161 END > 162 /* > 163 CASE(6) > 164 int labeled_[] = ; > 165 vector <int> labeled(labeled_, labeled_+sizeof(labeled_)/sizeof(*label > 166 int unlabeled_[] = ; > 167 vector <int> unlabeled(unlabeled_, unlabeled_+sizeof(unlabeled_)/sizeo > 168 string __[] = ; > 169 vector <string> _(__, __+sizeof(__)/sizeof(*__)); > 170 END > 171 */ > 172 } > 173 // END CUT HERE
Added SRM/484-U/1A.cpp version [6a42e8ac2f9b115b]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RabbitNumber { public: > 23 LL dsum(LL n) { > 24 LL x = 0; > 25 while(n) { > 26 x+=n%10; > 27 n/=10; > 28 } > 29 return x; > 30 } > 31 bool isRabbit(LL n) { > 32 LL a = dsum(n); > 33 LL b = dsum(n*n); > 34 return b==a*a; > 35 } > 36 void enumerate(int s, LL c, vector<int>& v) > 37 { > 38 if( c > 1000000000 ) > 39 return; > 40 if( s == 0 ) > 41 if( isRabbit(c) ) > 42 v.push_back(int(c)); > 43 for(int i=(c==0?1:0); i<=s && i<=9; ++i) > 44 enumerate(s-i, c*10+i, v); > 45 } > 46 > 47 int theCount(int low, int high) > 48 { > 49 vector<int> v; > 50 for(int s=1; s<=15; ++s) > 51 enumerate(s, 0, v); > 52 sort(v.begin(), v.end()); > 53 return upper_bound(v.begin(), v.end(), high) > 54 - lower_bound(v.begin(), v.end(), low); > 55 } > 56 }; > 57 > 58 // BEGIN CUT HERE > 59 #include <ctime> > 60 double start_time; string timer() > 61 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 62 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 63 { os << "{ "; > 64 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 65 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 66 void verify_case(const int& Expected, const int& Received) { > 67 bool ok = (Expected == Received); > 68 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 69 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 70 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 71 #define END verify_case(_, RabbitNumber().theCount(low, high));} > 72 int main(){ > 73 > 74 CASE(0) > 75 int low = 22; > 76 int high = 22; > 77 int _ = 1; > 78 END > 79 CASE(1) > 80 int low = 484; > 81 int high = 484; > 82 int _ = 0; > 83 END > 84 CASE(2) > 85 int low = 1; > 86 int high = 58; > 87 int _ = 12; > 88 END > 89 CASE(3) > 90 int low = 58; > 91 int high = 484; > 92 int _ = 24; > 93 END > 94 CASE(4) > 95 int low = 1000000000; > 96 int high = 1000000000; > 97 int _ = 1; > 98 END > 99 CASE(5) > 100 int low = 1; > 101 int high = 1; > 102 int _ = 1; > 103 END > 104 CASE(6) > 105 int low = 1; > 106 int high = 1000000; > 107 int _ = 615; > 108 END > 109 CASE(7) > 110 int low = 1; > 111 int high = 1000000000; > 112 int _ = -1; > 113 END > 114 } > 115 // END CUT HERE
Added SRM/484-U/1B.cpp version [be7ddcd5afb0c754]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000007; > 23 > 24 class PuyoPuyo { public: > 25 int theCount(int L, int N) > 26 { > 27 // A4 ::= U* > 28 // U ::= x A3 x A3 x A3 x (U::=U[0], U[i]::=x A3 U[i+1], U[ > 29 > 30 vector<LL> A4(N+1), A3(N+1); > 31 vector< vector<LL> > U(L, vector<LL>(N+1)); > 32 > 33 A4[0] = A3[0] = U[L-1][1] = 1; > 34 for(int n=1; n<=N; ++n) { > 35 for(int k=L-2; k>=0; --k) > 36 for(int z=0; n-z-1>=1; z+=L) > 37 U[k][n] = (U[k][n] + A3[z]*U[k+1][n-z-1] > 38 for(int i=L; i<=n; i+=L) > 39 A4[n] = (A4[n] + 4*U[0][i]*A4[n-i]) % MODVAL; > 40 for(int i=L; i<=n; i+=L) > 41 A3[n] = (A3[n] + 3*U[0][i]*A3[n-i]) % MODVAL; > 42 } > 43 return int(A4[N]); > 44 } > 45 }; > 46 > 47 // BEGIN CUT HERE > 48 #include <ctime> > 49 double start_time; string timer() > 50 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 51 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 52 { os << "{ "; > 53 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 54 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 55 void verify_case(const int& Expected, const int& Received) { > 56 bool ok = (Expected == Received); > 57 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 58 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 59 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 60 #define END verify_case(_, PuyoPuyo().theCount(L, N));} > 61 int main(){ > 62 > 63 CASE(0) > 64 int L = 2; > 65 int N = 2; > 66 int _ = 4; > 67 END > 68 CASE(1) > 69 int L = 2; > 70 int N = 4; > 71 int _ = 28; > 72 END > 73 CASE(2) > 74 int L = 2; > 75 int N = 58; > 76 int _ = 868294620; > 77 END > 78 CASE(3) > 79 int L = 4; > 80 int N = 84; > 81 int _ = 621871151; > 82 END > 83 CASE(4) > 84 int L = 5; > 85 int N = 8; > 86 int _ = 0; > 87 END > 88 CASE(5) > 89 int L = 2; > 90 int N = 1; > 91 int _ = 0; > 92 END > 93 CASE(6) > 94 int L = 2; > 95 int N = 1000; > 96 int _ = -1; > 97 END > 98 CASE(7) > 99 int L = 2; > 100 int N = 6; > 101 int _ = 232; > 102 END > 103 CASE(8) > 104 int L = 2; > 105 int N = 8; > 106 int _ = 2092; > 107 END > 108 } > 109 // END CUT HERE
Added SRM/484-U/2C.cpp version [ff8a7dedf31c21ec]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CubeColoring { public: > 23 long long theCount(vector <string> colors) > 24 { > 25 const int NC = colors[0].size(); > 26 > 27 int c1=0,c3=0,c4=0,c6=0; > 28 for(int i=0; i<NC; ++i) { > 29 c1 += colors[1][i]=='Y'; > 30 c3 += colors[3][i]=='Y'; > 31 c4 += colors[4][i]=='Y'; > 32 c6 += colors[6][i]=='Y'; > 33 } > 34 LL answer = 0; > 35 for(int i0=0; i0<NC; ++i0) if(colors[0][i0]=='Y') > 36 for(int i2=0; i2<NC; ++i2) if(colors[2][i2]=='Y') > 37 for(int i5=0; i5<NC; ++i5) if(colors[5][i5]=='Y') > 38 for(int i7=0; i7<NC; ++i7) if(colors[7][i7]=='Y') > 39 { > 40 int cc1 = c1 - (colors[1][i0]=='Y') - (i0!=i2 && colors[ > 41 int cc3 = c3 - (colors[3][i0]=='Y') - (i0!=i2 && colors[ > 42 int cc4 = c4 - (colors[4][i0]=='Y') - (i0!=i5 && colors[ > 43 int cc6 = c6 - (colors[6][i2]=='Y') - (i2!=i5 && colors[ > 44 answer += cc1*cc3*cc4*cc6; > 45 } > 46 return answer; > 47 } > 48 }; > 49 > 50 // BEGIN CUT HERE > 51 #include <ctime> > 52 double start_time; string timer() > 53 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 54 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 55 { os << "{ "; > 56 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 57 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 58 void verify_case(const long long& Expected, const long long& Received) { > 59 bool ok = (Expected == Received); > 60 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 61 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 62 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 63 #define END verify_case(_, CubeColoring().theCount(colors));} > 64 int main(){ > 65 > 66 CASE(0) > 67 string colors_[] = {"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y"}; > 68 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 69 long long _ = 0LL; > 70 END > 71 CASE(1) > 72 string colors_[] = {"YNNNNNNN", "NYNNNNNN", "NNYNNNNN", "NNNYNNNN", "NNN > 73 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 74 long long _ = 1LL; > 75 END > 76 CASE(2) > 77 string colors_[] = {"YNNYN", "YYYYY", "NYYNY", "YNYYN", "YNNYY", "YNNYY" > 78 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 79 long long _ = 250LL; > 80 END > 81 CASE(3) > 82 string colors_[] = {"YNNYN", "YYYYY", "NNNNN", "YNYYN", "YNNYY", "YNNYY" > 83 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 84 long long _ = 0LL; > 85 END > 86 CASE(4) > 87 string colors_[] = {"YNNYNYYYYYNN", "NNNYNYYNYNNY", "YYNNYYNNNYYN", "YYY > 88 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 89 long long _ = 611480LL; > 90 END > 91 /* > 92 CASE(5) > 93 string colors_[] = ; > 94 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 95 long long _ = LL; > 96 END > 97 CASE(6) > 98 string colors_[] = ; > 99 vector <string> colors(colors_, colors_+sizeof(colors_)/sizeof(*colors > 100 long long _ = LL; > 101 END > 102 */ > 103 } > 104 // END CUT HERE
Added SRM/485-U/1A.cpp version [561cc23e487a1233]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class AfraidOfEven { public: > 23 vector <int> restoreProgression(vector <int> seq) > 24 { > 25 LL a = seq[0]; > 26 LL b = seq[1]; > 27 for(LL aa=a; aa<=0x7fffffff; aa*=2) > 28 for(LL bb=b; bb<=0x7fffffff; bb*=2) > 29 if( possible(seq,aa,bb) ) > 30 return seq; > 31 assert(false); > 32 } > 33 bool possible(vector<int>& seq, LL a, LL b) > 34 { > 35 LL d = b-a; > 36 vector<LL> q(seq.size()); > 37 q[0] = a; > 38 q[1] = b; > 39 for(int i=2; i<seq.size(); ++i) > 40 { > 41 q[i] = q[i-1] + d; > 42 if( q[i] <= 0 ) > 43 return false; > 44 if( q[i] % seq[i] != 0 ) > 45 return false; > 46 LL f = q[i] / seq[i]; > 47 if( f & (f-1) ) > 48 return false; > 49 } > 50 for(int i=0; i<seq.size(); ++i) > 51 seq[i] = int(q[i]); > 52 return true; > 53 } > 54 }; > 55 > 56 // BEGIN CUT HERE > 57 #include <ctime> > 58 double start_time; string timer() > 59 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 60 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 61 { os << "{ "; > 62 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 63 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 64 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 65 bool ok = (Expected == Received); > 66 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 67 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 68 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 69 #define END verify_case(_, AfraidOfEven().restoreProgression(seq));} > 70 int main(){ > 71 > 72 CASE(0) > 73 int seq_[] = {1, 1, 3, 1, 5}; > 74 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 75 int __[] = {1, 2, 3, 4, 5 }; > 76 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 77 END > 78 CASE(1) > 79 int seq_[] = {9, 7, 5, 3, 1}; > 80 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 81 int __[] = {9, 7, 5, 3, 1 }; > 82 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 83 END > 84 CASE(2) > 85 int seq_[] = {999, 999, 999, 999}; > 86 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 87 int __[] = {999, 999, 999, 999 }; > 88 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 89 END > 90 CASE(3) > 91 int seq_[] = {7, 47, 5, 113, 73, 179, 53}; > 92 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 93 int __[] = {14, 47, 80, 113, 146, 179, 212 }; > 94 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 95 END > 96 CASE(4) > 97 int seq_[] = {749, 999, 125, 1}; > 98 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 99 int __[] = {1498, 999, 500, 1 }; > 100 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 101 END > 102 CASE(5) > 103 int seq_[] = {3,7,1,9}; > 104 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 105 int __[] = {6,7,8,9}; > 106 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 107 END > 108 /* > 109 CASE(6) > 110 int seq_[] = ; > 111 vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); > 112 int __[] = ; > 113 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 114 END > 115 */ > 116 } > 117 // END CUT HERE
Added SRM/485-U/1B.cpp version [50efd272f7286bd4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 struct DP3 > 24 { > 25 int N1, N2, N3; > 26 vector<T> data; > 27 DP3(int N1, int N2, int N3, const T& t = T()) > 28 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size() > 29 T& operator()(int i1, int i2, int i3) > 30 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 31 void swap(DP3& rhs) > 32 { data.swap(rhs.data); } > 33 }; > 34 > 35 class RectangleAvoidingColoring { public: > 36 long long count(vector <string> board) > 37 { > 38 int H = board.size(); > 39 int W = board[0].size(); > 40 // o???? > 41 // o???? > 42 // xooxo > 43 // xoxoo > 44 // xxooo > 45 if( H>=5 && W>=5 ) > 46 return 0; > 47 if( H >= 5 ) > 48 transpose(board, H, W); > 49 // H <= 4 > 50 > 51 int BADPAT = H*(H-1)/2; > 52 DP3<LL> dp(1<<BADPAT, 1<<BADPAT, W+1); > 53 dp(0,0,0) = 1; > 54 for(int i=0; i<W; ++i) > 55 for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++) > 56 for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++) > 57 { > 58 for(int pat=0; pat<(1<<H); ++pat) { > 59 int bbb = bb(pat,H); > 60 int wbb = wb(pat,H); > 61 if( possible(pat, board, i, H) && !(bbb&BlackBad > 62 dp(BlackBad|bbb,WhiteBad|wbb,i+1) += dp( > 63 } > 64 } > 65 LL sum = 0; > 66 for(int BlackBad=0; BlackBad<(1<<BADPAT); BlackBad++) > 67 for(int WhiteBad=0; WhiteBad<(1<<BADPAT); WhiteBad++) > 68 sum += dp(BlackBad,WhiteBad,W); > 69 return sum; > 70 } > 71 bool possible(int pat, const vector<string>& board, int i, int H) > 72 { > 73 for(int y=0; y<H; ++y) { > 74 if( board[y][i]=='B' && !(pat&(1<<y))) return false; > 75 if( board[y][i]=='W' && (pat&(1<<y))) return false; > 76 } > 77 return true; > 78 } > 79 int bb(int pat,int H) > 80 { > 81 int mask = 0, idx=0; > 82 for(int i=0; i<H; ++i) > 83 for(int j=i+1; j<H; ++j,++idx) > 84 if(pat & (1<<i)) > 85 if(pat & (1<<j)) > 86 mask |= (1<<idx); > 87 return mask; > 88 } > 89 int wb(int pat,int H) > 90 { > 91 int mask = 0, idx=0; > 92 for(int i=0; i<H; ++i) > 93 for(int j=i+1; j<H; ++j,++idx) > 94 if(!(pat & (1<<i))) > 95 if(!(pat & (1<<j))) > 96 mask |= (1<<idx); > 97 return mask; > 98 } > 99 void transpose(vector<string>& b, int& H, int& W) > 100 { > 101 vector<string> bb(W); > 102 for(int x=0; x<W; ++x) > 103 for(int y=0; y<H; ++y) > 104 bb[x] += b[y][x]; > 105 swap(H, W); > 106 b = bb; > 107 } > 108 }; > 109 > 110 // BEGIN CUT HERE > 111 #include <ctime> > 112 double start_time; string timer() > 113 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 114 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 115 { os << "{ "; > 116 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 117 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 118 void verify_case(const long long& Expected, const long long& Received) { > 119 bool ok = (Expected == Received); > 120 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 121 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 122 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 123 #define END verify_case(_, RectangleAvoidingColoring().count(board));} > 124 int main(){ > 125 > 126 CASE(0) > 127 string board_[] = {"??", > 128 "??"}; > 129 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 130 long long _ = 14LL; > 131 END > 132 CASE(1) > 133 string board_[] = {"B?", > 134 "?B"}; > 135 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 136 long long _ = 3LL; > 137 END > 138 CASE(2) > 139 string board_[] = {"WW", > 140 "WW"}; > 141 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 142 long long _ = 0LL; > 143 END > 144 CASE(3) > 145 string board_[] = {"??B??", > 146 "W???W", > 147 "??B??"}; > 148 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 149 long long _ = 12LL; > 150 END > 151 CASE(4) > 152 string board_[] = {"??", > 153 "W?", > 154 "W?", > 155 "?W", > 156 "W?"}; > 157 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 158 long long _ = 16LL; > 159 END > 160 /* > 161 CASE(5) > 162 string board_[] = {}; > 163 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 164 long long _ = LL; > 165 END > 166 CASE(6) > 167 string board_[] = ; > 168 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 169 long long _ = LL; > 170 END > 171 */ > 172 } > 173 // END CUT HERE
Added SRM/485-U/1C-U.cpp version [27e70b1898243972]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Deposit { public: > 23 vector<CMP> make_polygon(const vector<int>& xs, const vector<int>& ys) > 24 { > 25 vector<CMP> vs; > 26 for(int i=0; i<xs.size(); ++i) > 27 vs.push_back( CMP(xs[i], ys[i]) ); > 28 return vs; > 29 } > 30 > 31 double successProbability(vector <int> siteX, vector <int> siteY, vector > 32 { > 33 const vector<CMP> ss = make_polygon(siteX, siteY); > 34 const vector<CMP> dd = make_polygon(depositX, depositY); > 35 const int SN = ss.size(); > 36 > 37 double totallen=0, hitlen=0; > 38 for(int i=0; i<SN; ++i) > 39 { > 40 const CMP p0 = ss[i]; > 41 const CMP p1 = ss[(i+1)%SN]; > 42 totallen += abs(p1-p0); > 43 > 44 > 45 CMP pv = p1 - p0; pv /= abs(pv); > 46 > 47 // calc furthest on p0 - p1 > 48 double q = 1.0; > 49 for(int k=i+1; k!=i; k=(k+1)%SN) > 50 { > 51 const CMP q0 = ss[k]; > 52 const CMP q1 = ss[(k+1)%SN]; > 53 // compute vertical-eqsplitter of lineseg q0 - q > 54 CMP mid = (q0+q1)/2.0; > 55 CMP vec = (q0-q1)*CMP(0,1);; > 56 // compute crossing with p0 - p1 > 57 double qq = solve(p0,pv,mid,vec); > 58 if( qq < q ) > 59 { > 60 if( qq <= 0.0 ) > 61 qq = 0.0; > 62 // in (qq,q), furthest is q1 > 63 // [todo] compute the fraction of the re > 64 // by ternery search or something like t > 65 double r = computeFractionOfCross(p0+qq* > 66 > 67 hitlen += abs(p0-p1)*(q-qq)*r; > 68 q = qq; > 69 } > 70 if( q == 0.0 ) > 71 break; > 72 } > 73 } > 74 return hitlen / totallen; > 75 } > 76 double solve(CMP a, CMP b, CMP c, CMP d) > 77 { > 78 // solve: a + <return ?>*b == c + ??*d > 79 return -1; > 80 } > 81 double computeFractionOfCross(CMP p0, CMP p1, CMP q, const vector<CMP>& > 82 { > 83 // retur the fraction of k in [0,1] s.t. line (p0 + k(p1-p0)) > 84 return -1; > 85 } > 86 }; > 87 > 88 // BEGIN CUT HERE > 89 #include <ctime> > 90 double start_time; string timer() > 91 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 92 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 93 { os << "{ "; > 94 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 95 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 96 void verify_case(const double& Expected, const double& Received) { > 97 bool ok = (abs(Expected - Received) < 1e-9); > 98 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 99 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 100 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 101 #define END verify_case(_, Deposit().successProbability(siteX, siteY, depos > 102 int main(){ > 103 > 104 CASE(0) > 105 int siteX_[] = {0,4,4,0}; > 106 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 107 int siteY_[] = {0,0,4,4}; > 108 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 109 int depositX_[] = {1,2,2,1}; > 110 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 111 int depositY_[] = {1,1,2,2}; > 112 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 113 double _ = 0.6666666666666666; > 114 END > 115 CASE(1) > 116 int siteX_[] = {0,4,4,0}; > 117 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 118 int siteY_[] = {0,0,4,4}; > 119 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 120 int depositX_[] = {1,3,3,1}; > 121 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 122 int depositY_[] = {1,1,3,3}; > 123 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 124 double _ = 1.0; > 125 END > 126 CASE(2) > 127 int siteX_[] = {5,2,-1,0,4}; > 128 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 129 int siteY_[] = {3,4,2,0,0}; > 130 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 131 int depositX_[] = {3,3,4}; > 132 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 133 int depositY_[] = {3,2,1}; > 134 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 135 double _ = 0.6112700209855423; > 136 END > 137 CASE(3) > 138 int siteX_[] = {200,-99,-405,-601,-708,-494,-300,-88}; > 139 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 140 int siteY_[] = {520,516,407,321,104,-97,-221,-101}; > 141 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 142 int depositX_[] = {-101,-201,-296,-400,-402}; > 143 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 144 int depositY_[] = {318,396,402,305,200}; > 145 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 146 double _ = 0.49892756207100747; > 147 END > 148 CASE(4) > 149 int siteX_[] = {-3,6,10,10,-2}; > 150 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 151 int siteY_[] = {-1,-10,-9,7,3}; > 152 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 153 int depositX_[] = {3,5,6,4}; > 154 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 155 int depositY_[] = {-5,-7,-6,-4}; > 156 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 157 double _ = 0.16641662251521538; > 158 END > 159 CASE(5) > 160 int siteX_[] = {-2,4,-2} > 161 ; > 162 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 163 int siteY_[] = {-2,-2,8} > 164 ; > 165 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 166 int depositX_[] = {-1,0,0} > 167 ; > 168 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 169 int depositY_[] = {0,-1,0} > 170 ; > 171 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 172 double _ = 0.04518850219072291; > 173 END > 174 /* > 175 CASE(6) > 176 int siteX_[] = ; > 177 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 178 int siteY_[] = ; > 179 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 180 int depositX_[] = ; > 181 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 182 int depositY_[] = ; > 183 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 184 double _ = ; > 185 END > 186 CASE(7) > 187 int siteX_[] = ; > 188 vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); > 189 int siteY_[] = ; > 190 vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); > 191 int depositX_[] = ; > 192 vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*d > 193 int depositY_[] = ; > 194 vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*d > 195 double _ = ; > 196 END > 197 */ > 198 } > 199 // END CUT HERE
Added SRM/486/1A.cpp version [a62787f87989e0f0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class OneRegister { public: > 23 string getProgram(int s, int t) > 24 { > 25 string x = run("",s,t); > 26 string y = run("/",1, t); > 27 string z = takeMin(x,y); > 28 return z=="NO" ? ":-(" : z; > 29 } > 30 > 31 string takeMin(const string& x, const string& y) > 32 { > 33 if( x == "NO" ) > 34 return y; > 35 else if( y == "NO" ) > 36 return x; > 37 else > 38 return (x.size()<y.size() ? x : (x.size()==y.size() ? mi > 39 } > 40 > 41 // by using only '*' and '+' > 42 string run(const string& buf, LL s, LL t) > 43 { > 44 if( s == t ) > 45 return buf; > 46 if( s > t ) > 47 return "NO"; > 48 string x = run(buf+"+", s+s, t); > 49 string y = (s==1 ? "NO" : run(buf+"*", s*s, t)); > 50 return takeMin(x,y); > 51 } > 52 }; > 53 > 54 // BEGIN CUT HERE > 55 #include <ctime> > 56 double start_time; string timer() > 57 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 58 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 59 { os << "{ "; > 60 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 61 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 62 void verify_case(const string& Expected, const string& Received) { > 63 bool ok = (Expected == Received); > 64 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 65 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 66 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 67 #define END verify_case(_, OneRegister().getProgram(s, t));} > 68 int main(){ > 69 > 70 CASE(0) > 71 int s = 7; > 72 int t = 392; > 73 string _ = "+*+"; > 74 END > 75 CASE(1) > 76 int s = 7; > 77 int t = 256; > 78 string _ = "/+***"; > 79 END > 80 CASE(2) > 81 int s = 4; > 82 int t = 256; > 83 string _ = "**"; > 84 END > 85 CASE(3) > 86 int s = 7; > 87 int t = 7; > 88 string _ = ""; > 89 END > 90 CASE(4) > 91 int s = 7; > 92 int t = 9; > 93 string _ = ":-("; > 94 END > 95 CASE(5) > 96 int s = 10; > 97 int t = 1; > 98 string _ = "/"; > 99 END > 100 CASE(6) > 101 int s = 1; > 102 int t = 1000000000; > 103 string _ = "dummy"; > 104 END > 105 CASE(6) > 106 int s = 2; > 107 int t = 1<<30; > 108 string _ = "dummy"; > 109 END > 110 CASE(7) > 111 int s = 1000000000; > 112 int t = 1; > 113 string _ = "dummy"; > 114 END > 115 CASE(8) > 116 int s = 1; > 117 int t = 1; > 118 string _ = ""; > 119 END > 120 CASE(9) > 121 int s = 2; > 122 int t = 2; > 123 string _ = ""; > 124 END > 125 CASE(10) > 126 int s = 1; > 127 int t = 2; > 128 string _ = "+"; > 129 END > 130 CASE(11) > 131 int s = 512; > 132 int t = 65536; > 133 string _ = "*"; > 134 END > 135 > 136 } > 137 // END CUT HERE
Added SRM/486/1B.cpp version [937144173b305ccc]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class QuickSort { public: > 23 double getEval(vector <int> L) > 24 { > 25 int m = *min_element(L.begin(), L.end()); > 26 int M = *max_element(L.begin(), L.end()); > 27 return rec(L, m, M+1); > 28 } > 29 map<pair<int,int>, double> memo; > 30 double rec(const vector<int>& L, int m, int M) > 31 { > 32 pair<int,int> key(m,M); > 33 if(memo.count(key)) > 34 return memo[key]; > 35 > 36 vector<int> c; > 37 for(int i=0; i<L.size(); ++i) > 38 if( m<=L[i] && L[i]<M ) > 39 c.push_back(L[i]); > 40 > 41 if( c.size() <= 1 ) > 42 return memo[key] = 0; > 43 > 44 double e = 0; > 45 for(int k=0; k<c.size(); ++k) > 46 { > 47 int p = c[k]; > 48 int cnt = 0; > 49 for(int i=0; i<k; ++i) > 50 if( c[i] > p ) > 51 ++cnt; > 52 for(int i=k+1; i<c.size(); ++i) > 53 if( p > c[i] ) > 54 ++cnt; > 55 e += rec(L, m, p) + rec(L, p+1, M) + cnt; > 56 } > 57 > 58 return memo[key] = e / c.size(); > 59 } > 60 }; > 61 > 62 // BEGIN CUT HERE > 63 #include <ctime> > 64 double start_time; string timer() > 65 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 66 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 67 { os << "{ "; > 68 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 69 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 70 void verify_case(const double& Expected, const double& Received) { > 71 bool ok = (abs(Expected - Received) < 1e-9); > 72 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 73 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 74 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 75 #define END verify_case(_, QuickSort().getEval(L));} > 76 int main(){ > 77 > 78 CASE(0) > 79 int L_[] = {1,2,3,4,5}; > 80 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 81 double _ = 0.0; > 82 END > 83 CASE(1) > 84 int L_[] = {1,2,4,3,5,6}; > 85 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 86 double _ = 1.0; > 87 END > 88 CASE(2) > 89 int L_[] = {3,2,1}; > 90 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 91 double _ = 2.6666666666666665; > 92 END > 93 CASE(3) > 94 int L_[] = {50,40,30,20,10,15,25,35,45}; > 95 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 96 double _ = 11.07698412698413; > 97 END > 98 CASE(4) > 99 int L_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 100 31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}; > 101 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 102 double _ = -1; > 103 END > 104 CASE(5) > 105 int L_[] = {48,49,50,10,11,12,40,41,42,5,39,32,33,34,43,44,45,46,6,7,8,9 > 106 31,13,1,2,3,4,35,36,37,38,47,14,15,16,17,18,19,20,21}; > 107 vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_)); > 108 double _ = -1; > 109 END > 110 } > 111 // END CUT HERE
Added SRM/486/1C.cpp version [0a15ef1d15a672ee]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } > 23 double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } > 24 > 25 int ccw(const CMP& a, CMP b, CMP c) { > 26 b -= a; c -= a; > 27 if( outer_prod(b,c) > 0 ) return +1; // counter clockwise > 28 if( outer_prod(b,c) < 0 ) return -1; // clockwise > 29 if( inner_prod(b,c) < 0 ) return +2; // c--a--b on line > 30 if( norm(b) < norm(c) ) return -2; // a--b--c on line > 31 return 0; > 32 } > 33 > 34 bool byX( const CMP& a, const CMP& b ) { > 35 if( a.real() != b.real() ) > 36 return a.real() < b.real(); > 37 return a.imag() < b.imag(); > 38 } > 39 > 40 vector<CMP> convex_hull( vector<CMP> p ) > 41 { > 42 #define IS_RIGHT <0 // skip on-line verts > 43 //#define IS_RIGHT ==-1 // take all > 44 > 45 sort(p.begin(), p.end(), &byX); > 46 > 47 vector<CMP> ans; > 48 for(int i=0; i<p.size(); ans.push_back(p[i++])) // left-to-right > 49 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 50 ans.pop_back(); > 51 if( ans.size() == p.size() ) > 52 return ans; > 53 for(int i=p.size()-2; i>=0; ans.push_back(p[i--])) // right-to-left > 54 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 55 ans.pop_back(); > 56 ans.pop_back(); > 57 return ans; > 58 } > 59 > 60 double area( const vector<CMP>& q ) > 61 { > 62 double a = 0.0; > 63 > 64 CMP o = q[0]; > 65 for(int i=1; i+1<q.size(); ++i) > 66 a += outer_prod(q[i]-o, q[i+1]-o); > 67 return abs(a)/2; > 68 } > 69 > 70 class BatmanAndRobin { public: > 71 double minArea(vector <int> x, vector <int> y) > 72 { > 73 int N = x.size(); > 74 vector<CMP> p; > 75 for(int i=0; i<N; ++i) > 76 p.push_back(CMP(x[i],y[i])); > 77 > 78 const double eps = 1e-6; > 79 CMP diff[] = {CMP(eps,0), CMP(0,eps), CMP(-eps,0), CMP(0,-eps)}; > 80 double answer = area(convex_hull(p)); > 81 for(int i=0; i<N; ++i) > 82 for(int j=i+1; j<N; ++j) { > 83 for(int di=0; di<4; ++di) > 84 for(int dj=0; dj<4; ++dj) { > 85 vector<CMP> a = left_of(p, p[i]+ > 86 vector<CMP> b = right_of(p, p[i] > 87 double aa = a.size()<=2 ? 0 : ar > 88 double bb = b.size()<=2 ? 0 : ar > 89 answer = min(answer, max(aa,bb)) > 90 } > 91 } > 92 return answer; > 93 } > 94 > 95 vector<CMP> left_of(const vector<CMP>& p, const CMP& o, CMP v) > 96 { > 97 v -= o; > 98 vector<CMP> r; > 99 for(int i=0; i<p.size(); ++i) > 100 if( is_left(p[i]-o, v) ) > 101 r.push_back(p[i]); > 102 return r; > 103 } > 104 vector<CMP> right_of(const vector<CMP>& p, const CMP& o, CMP v) > 105 { > 106 v -= o; > 107 vector<CMP> r; > 108 for(int i=0; i<p.size(); ++i) > 109 if( !is_left(p[i]-o, v) ) > 110 r.push_back(p[i]); > 111 return r; > 112 } > 113 bool is_left(const CMP& p, const CMP& v) > 114 { > 115 return arg(p/v)>0; > 116 } > 117 }; > 118 > 119 // BEGIN CUT HERE > 120 #include <ctime> > 121 double start_time; string timer() > 122 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 123 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 124 { os << "{ "; > 125 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 126 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 127 void verify_case(const double& Expected, const double& Received) { > 128 bool ok = (abs(Expected - Received) < 1e-9); > 129 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 130 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 131 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 132 #define END verify_case(_, BatmanAndRobin().minArea(x, y));} > 133 int main(){ > 134 > 135 CASE(0) > 136 int x_[] = {100,100,90,90,-100,-100,-90,-90}; > 137 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 138 int y_[] = {100,90,100,90,-100,-90,-100,-90}; > 139 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 140 double _ = 100.0; > 141 END > 142 CASE(1) > 143 int x_[] = {-1000,-1000,1000,1000,1000,-1000}; > 144 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 145 int y_[] = {-1000,1000,-1000,1000,0,0}; > 146 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 147 double _ = 0.0; > 148 END > 149 CASE(2) > 150 int x_[] = {-1000,-1000,1000,1000,0}; > 151 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 152 int y_[] = {-1000,1000,-1000,1000,0}; > 153 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 154 double _ = 1000000.0; > 155 END > 156 CASE(3) > 157 int x_[] = {-904,-812,-763,-735,-692,-614,-602,-563,-435,-243,-87,-52,-2 > 158 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 159 int y_[] = {786,10,-144,949,37,-857,-446,-969,-861,-712,5,-972,-3,-202,- > 160 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 161 double _ = 1067472.0; > 162 END > 163 CASE(4) > 164 int x_[] = {-904,-812,-763,-735,-692,-614,-602,-563,-435,-243,-87,-52,-2 > 165 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 166 int y_[] = {786,10,-144,949,37,-857,-446,-969,-861,-712,5,-972,-3,-202,- > 167 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 168 double _ = -1; > 169 END > 170 /* > 171 CASE(5) > 172 int x_[] = ; > 173 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 174 int y_[] = ; > 175 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 176 double _ = ; > 177 END > 178 */ > 179 } > 180 // END CUT HERE
Added SRM/486/2C.cpp version [894982b1719fd8db]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 struct UnionFind > 23 { > 24 vector<int> uf, sz; > 25 int nc; > 26 > 27 UnionFind(int N): uf(N), sz(N,1), nc(N) > 28 { for(int i=0; i<N; ++i) uf[i] = i; } > 29 int size() > 30 { return nc; } > 31 int Find(int a) > 32 { return uf[a]==a ? a : uf[a]=Find(uf[a]); } > 33 bool Union(int a, int b) > 34 { > 35 a = Find(a); > 36 b = Find(b); > 37 if( a != b ) > 38 { > 39 if( sz[a] >= sz[b] ) swap(a, b); > 40 uf[a] = b; > 41 sz[b] += sz[a]; > 42 --nc; > 43 } > 44 return (a!=b); > 45 } > 46 }; > 47 > 48 class CrazyLine { public: > 49 int maxCrazyness(vector <int> heights) > 50 { > 51 typedef pair< int, pair<int,int> > cand; > 52 priority_queue<cand> Q; > 53 for(int i=0; i<heights.size(); ++i) > 54 for(int j=i+1; j<heights.size(); ++j) > 55 Q.push( cand(abs(heights[i]-heights[j]), make_pair(i,j)) > 56 > 57 UnionFind uf(heights.size()); > 58 > 59 int score=0; > 60 vector<int> used(heights.size()); > 61 while( uf.size()>1 ) > 62 { > 63 int s = Q.top().first; > 64 int i = Q.top().second.first; > 65 int j = Q.top().second.second; > 66 Q.pop(); > 67 if( used[i]<=1 && used[j]<=1 && uf.Find(i)!=uf.Find(j) ) > 68 { > 69 score += s; > 70 used[i]++; > 71 used[j]++; > 72 uf.Union(i,j); > 73 } > 74 } > 75 return score; > 76 } > 77 }; > 78 > 79 // BEGIN CUT HERE > 80 #include <ctime> > 81 double start_time; string timer() > 82 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 83 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 84 { os << "{ "; > 85 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 86 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 87 void verify_case(const int& Expected, const int& Received) { > 88 bool ok = (Expected == Received); > 89 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 90 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 91 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 92 #define END verify_case(_, CrazyLine().maxCrazyness(heights));} > 93 int main(){ > 94 > 95 CASE(0) > 96 int heights_[] = {5,10,25,40,25}; > 97 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 98 int _ = 100; > 99 END > 100 CASE(1) > 101 int heights_[] = {2,3,5,7,11,13,17,19}; > 102 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 103 int _ = 82; > 104 END > 105 CASE(2) > 106 int heights_[] = {1,1,1,1,1,1,501}; > 107 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 108 int _ = 1000; > 109 END > 110 CASE(3) > 111 int heights_[] = {1000,1000,1000,1000,1000,1000,1000,1000,1000}; > 112 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 113 int _ = 0; > 114 END > 115 /* > 116 CASE(4) > 117 int heights_[] = ; > 118 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 119 int _ = ; > 120 END > 121 CASE(5) > 122 int heights_[] = ; > 123 vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heigh > 124 int _ = ; > 125 END > 126 */ > 127 } > 128 // END CUT HERE
Added SRM/487/1A.cpp version [4c5e2466526b5e9f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BunnyComputer { public: > 23 int getMaximum(vector <int> preference, int k) > 24 { > 25 int sum = 0; > 26 > 27 int N = preference.size(); > 28 for(int s=0; s<=k && s<N; ++s) > 29 { > 30 vector<int> sub; > 31 for(int i=s; i<N; i+=1+k) > 32 sub.push_back( preference[i] ); > 33 sum += accumulate(sub.begin(), sub.end(), 0); > 34 if( sub.size() % 2 == 1 ) > 35 { > 36 vector<int> skip; > 37 for(int i=0; i<sub.size(); i+=2) > 38 skip.push_back( sub[i] ); > 39 sum -= *min_element(skip.begin(), skip.end()); > 40 } > 41 } > 42 > 43 return sum; > 44 } > 45 }; > 46 > 47 // BEGIN CUT HERE > 48 #include <ctime> > 49 double start_time; string timer() > 50 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 51 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 52 { os << "{ "; > 53 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 54 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 55 void verify_case(const int& Expected, const int& Received) { > 56 bool ok = (Expected == Received); > 57 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 58 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 59 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 60 #define END verify_case(_, BunnyComputer().getMaximum(preference, k));} > 61 int main(){ > 62 > 63 CASE(0) > 64 int preference_[] = { 3, 1, 4, 1, 5, 9, 2, 6 }; > 65 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 66 int k = 2; > 67 int _ = 28; > 68 END > 69 CASE(1) > 70 int preference_[] = { 3, 1, 4, 1, 5, 9, 2, 6 }; > 71 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 72 int k = 1; > 73 int _ = 31; > 74 END > 75 CASE(2) > 76 int preference_[] = { 1, 2, 3, 4, 5, 6 }; > 77 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 78 int k = 3; > 79 int _ = 14; > 80 END > 81 CASE(3) > 82 int preference_[] = { 487, 2010 }; > 83 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 84 int k = 2; > 85 int _ = 0; > 86 END > 87 /* > 88 CASE(4) > 89 int preference_[] = ; > 90 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 91 int k = ; > 92 int _ = ; > 93 END > 94 CASE(5) > 95 int preference_[] = ; > 96 vector <int> preference(preference_, preference_+sizeof(preference_)/s > 97 int k = ; > 98 int _ = ; > 99 END > 100 */ > 101 } > 102 // END CUT HERE
Added SRM/487/1B.cpp version [adf3912f0e556cd5]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BunnyExam { public: > 23 double getExpected(int m, int k, vector <int> linkage) > 24 { > 25 // trivially impossible cases > 26 if( k==1 && m>1 ) > 27 return -1; > 28 if( k==2 ) > 29 for(int i=0; i<linkage.size(); i+=2) > 30 if( (linkage[i] ^ linkage[i+1])&1 ) > 31 return -1; > 32 for(int i=0; i<linkage.size(); i+=2) > 33 if( abs(linkage[i] - linkage[i+1]) == 1 ) > 34 return -1; > 35 > 36 // interference graph > 37 int M = linkage.size()/2; > 38 vector< vector<int> > conflict(M); > 39 for(int i=0; i<M; ++i) > 40 for(int j=0; j<M; ++j) > 41 if( abs(linkage[2*i+0]-linkage[2*j+0])==1 || abs(linkage > 42 || abs(linkage[2*i+0]-linkage[2*j+1])==1 || abs(linkage > 43 conflict[i].push_back(j); > 44 > 45 // check k-colorability > 46 if( colorable(conflict, k) ) > 47 return double(m)/k; > 48 else > 49 return -1; > 50 } > 51 > 52 bool colorable(vector< vector<int> >& G, int k) > 53 { > 54 if( G.size() <= k ) > 55 return true; > 56 vector<int> color(G.size(), (1<<k)-1); > 57 color[0] = 1; > 58 return rec(G, color, 0); > 59 } > 60 > 61 bool rec(vector< vector<int> >& G, const vector<int>& color, int i) > 62 { > 63 if( i == color.size() ) > 64 return true; > 65 for(int k=1; k<=color[i]; k<<=1) > 66 if( color[i] & k ) // use color k > 67 { > 68 vector<int> c2 = color; > 69 for(int j=0; j<G[i].size(); ++j) // G[i][j] cann > 70 c2[G[i][j]] &= ~k; > 71 if( rec(G, c2, i+1) ) > 72 return true; > 73 } > 74 return false; > 75 } > 76 }; > 77 > 78 // BEGIN CUT HERE > 79 #include <ctime> > 80 double start_time; string timer() > 81 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 82 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 83 { os << "{ "; > 84 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 85 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 86 void verify_case(const double& Expected, const double& Received) { > 87 bool ok = (abs(Expected - Received) < 1e-9); > 88 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 89 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 90 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 91 #define END verify_case(_, BunnyExam().getExpected(m, k, linkage));} > 92 int main(){ > 93 > 94 CASE(0) > 95 int m = 3; > 96 int k = 2; > 97 int linkage_[] = { 1, 3 }; > 98 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 99 double _ = 1.5; > 100 END > 101 CASE(1) > 102 int m = 4; > 103 int k = 2; > 104 int linkage_[] = { 1, 4 }; > 105 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 106 double _ = -1.0; > 107 END > 108 CASE(2) > 109 int m = 2; > 110 int k = 8; > 111 vector <int> linkage; > 112 double _ = 0.25; > 113 END > 114 CASE(3) > 115 int m = 1000000000; > 116 int k = 1; > 117 int linkage_[] = { 11, 13, 2010, 487 }; > 118 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 119 double _ = -1.0; > 120 END > 121 CASE(4) > 122 int m = 128; > 123 int k = 64; > 124 int linkage_[] = { 32, 16, 8, 4, 2, 1 } > 125 ; > 126 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 127 double _ = -1.0; > 128 END > 129 CASE(5) > 130 int m = 13; > 131 int k = 3; > 132 int linkage_[] = { 1, 3, 7, 9, 13, 10, 6, 2 } > 133 ; > 134 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 135 double _ = 4.333333333333333; > 136 END > 137 CASE(6) > 138 int m = 8; > 139 int k = 3; > 140 int linkage_[] = {1,4,2,6,3,8,5,7}; > 141 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 142 double _ = -1; > 143 END > 144 CASE(7) > 145 int m = 1000000000; > 146 int k = 4; > 147 int linkage_[] = {1,4,2,6,3,8,5,7,9,19, 10,20,11,21,12,22,13,23,14,24}; > 148 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 149 double _ = -1; > 150 END > 151 CASE(8) > 152 int m = 20; > 153 int k = 3; > 154 int linkage_[] = {19, 6, 1, 14, 13, 9, 20, 10, 15, 8, 17, 5, 12, 7, 16, > 155 vector <int> linkage(linkage_, linkage_+sizeof(linkage_)/sizeof(*linka > 156 double _ = 6.666666666666667; > 157 END > 158 } > 159 // END CUT HERE
Added SRM/487/1C.cpp version [42380d118339650a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL MODVAL = 1000000009; > 23 > 24 class BunnySequence { public: > 25 int getNumber(int m, int n) > 26 { > 27 vector<LL> tbl(n+1); > 28 vector<LL> tbl_sum(n+1); > 29 tbl[0] = 1; > 30 tbl_sum[0] = 1; > 31 for(int i=1; i<=n; ++i) { > 32 tbl[i] = (tbl_sum[i-1] - (i-m-1>=0 ? tbl_sum[i-m-1] : 0) > 33 tbl_sum[i] = (tbl_sum[i-1] + tbl[i]) % MODVAL; > 34 } > 35 return (tbl[n] - (n>=m ? tbl[n-m] : 0) + MODVAL) % MODVAL; > 36 } > 37 }; > 38 > 39 // BEGIN CUT HERE > 40 #include <ctime> > 41 double start_time; string timer() > 42 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 43 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 44 { os << "{ "; > 45 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 46 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 47 void verify_case(const int& Expected, const int& Received) { > 48 bool ok = (Expected == Received); > 49 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 50 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 51 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 52 #define END verify_case(_, BunnySequence().getNumber(m, n));} > 53 int main(){ > 54 > 55 CASE(0) > 56 int m = 5; > 57 int n = 3; > 58 int _ = 4; > 59 END > 60 CASE(1) > 61 int m = 487; > 62 int n = 0; > 63 int _ = 1; > 64 END > 65 CASE(2) > 66 int m = 19; > 67 int n = 10; > 68 int _ = 512; > 69 END > 70 CASE(3) > 71 int m = 3; > 72 int n = 4; > 73 int _ = 6; > 74 END > 75 CASE(4) > 76 int m = 10; > 77 int n = 487; > 78 int _ = 829515032; > 79 END > 80 /* > 81 CASE(5) > 82 int m = ; > 83 int n = ; > 84 int _ = ; > 85 END > 86 CASE(6) > 87 int m = ; > 88 int n = ; > 89 int _ = ; > 90 END > 91 */ > 92 } > 93 // END CUT HERE
Added SRM/487/2C.cpp version [707c8d59bdc01e9a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BunnyConverter { public: > 23 vector< vector<int> > n_sqrt; > 24 > 25 int getMinimum(int n, int z, int start, int goal) > 26 { > 27 n_sqrt.resize(n); > 28 for(LL y=0; y<n; ++y) > 29 n_sqrt[y*y%n].push_back(int(y?y:n)); > 30 > 31 set<int> v; > 32 v.insert(start); > 33 vector<int> q(1,start); > 34 for(int step=0; !q.empty(); ++step) > 35 { > 36 vector<int> qNext; > 37 for(int i=0; i<q.size(); ++i) > 38 { > 39 int x = q[i]; > 40 if( x == goal ) > 41 return step; > 42 const vector<int>& ys = next(x,z,n); > 43 for(int j=0; j<ys.size(); ++j) > 44 { > 45 int y = ys[j]; > 46 if( v.insert(y).second ) > 47 qNext.push_back(y); > 48 } > 49 } > 50 q.swap(qNext); > 51 } > 52 return -1; > 53 } > 54 > 55 > 56 const vector<int>& next(LL x, LL z, LL n) > 57 { > 58 x %= n; > 59 z %= n; > 60 #define SUB(a,b) (((a)+n-(b))%n) > 61 #define MUL(a,b) ((a)*(b)%n) > 62 LL y2 = SUB(SUB(0, x), MUL(z,MUL(z,z))); > 63 return n_sqrt[int(y2)]; > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const int& Expected, const int& Received) { > 76 bool ok = (Expected == Received); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, BunnyConverter().getMinimum(n, z, start, goal)); > 81 int main(){ > 82 > 83 CASE(0) > 84 int n = 5; > 85 int z = 1; > 86 int start = 5; > 87 int goal = 3; > 88 int _ = 1; > 89 END > 90 CASE(1) > 91 int n = 5; > 92 int z = 1; > 93 int start = 5; > 94 int goal = 1; > 95 int _ = 2; > 96 END > 97 CASE(2) > 98 int n = 6; > 99 int z = 2; > 100 int start = 3; > 101 int goal = 4; > 102 int _ = -1; > 103 END > 104 CASE(3) > 105 int n = 7; > 106 int z = 7; > 107 int start = 7; > 108 int goal = 7; > 109 int _ = 0; > 110 END > 111 CASE(4) > 112 int n = 499979; > 113 int z = 499979; > 114 int start = 499976; > 115 int goal = 3; > 116 int _ = 249988; > 117 END > 118 /* > 119 CASE(5) > 120 int n = ; > 121 int z = ; > 122 int start = ; > 123 int goal = ; > 124 int _ = ; > 125 END > 126 CASE(6) > 127 int n = ; > 128 int z = ; > 129 int start = ; > 130 int goal = ; > 131 int _ = ; > 132 END > 133 */ > 134 } > 135 // END CUT HERE
Added SRM/489-U/1A.cpp version [7de7b5143ca31562]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheBoredomDivOne { public: > 23 double find(int n, int m) > 24 { > 25 double total = (n+m)*(n+m-1); > 26 > 27 vector<double> e(n+m+1); > 28 e[n+m] = 0; > 29 for(int i=n+m-1; i>=n; --i) > 30 { > 31 double p2 = (n+m-i)*(n+m-i-1) / total; > 32 double p1 = 2*i*(n+m-i) / total; > 33 double p0 = 1-p2-p1; > 34 > 35 // e[i] = 1 + p0*e[i] + p1*e[i+1] + p2*e[i+2]; > 36 e[i] = 1/(1-p0) + p1/(1-p0)*e[i+1] + (i==n+m-1 ? 0 : p2/ > 37 } > 38 return e[n]; > 39 } > 40 }; > 41 > 42 // BEGIN CUT HERE > 43 #include <ctime> > 44 double start_time; string timer() > 45 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 46 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 47 { os << "{ "; > 48 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 49 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 50 void verify_case(const double& Expected, const double& Received) { > 51 bool ok = (abs(Expected - Received) < 1e-9); > 52 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 53 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 54 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 55 #define END verify_case(_, TheBoredomDivOne().find(n, m));} > 56 int main(){ > 57 > 58 CASE(0) > 59 int n = 1; > 60 int m = 1; > 61 double _ = 1.0; > 62 END > 63 CASE(1) > 64 int n = 2; > 65 int m = 1; > 66 double _ = 1.5; > 67 END > 68 CASE(2) > 69 int n = 1; > 70 int m = 2; > 71 double _ = 2.0; > 72 END > 73 CASE(3) > 74 int n = 4; > 75 int m = 7; > 76 double _ = 13.831068977298521; > 77 END > 78 /* > 79 CASE(4) > 80 int n = ; > 81 int m = ; > 82 double _ = ; > 83 END > 84 CASE(5) > 85 int n = ; > 86 int m = ; > 87 double _ = ; > 88 END > 89 */ > 90 } > 91 // END CUT HERE
Added SRM/489-U/1B.cpp version [4377d1219d057c6f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheBoringStoreDivOne { public: > 23 // entry point > 24 string J, B; > 25 string find(string J, string B) > 26 { > 27 this->J=J; this->B=B; return solve(); > 28 } > 29 > 30 // just for clarifying types > 31 typedef int J_index, B_index; > 32 typedef pair<int,int> J_sub, B_sub; > 33 string jsub(J_index js, J_index je) { return J.substr(js, je-js); } > 34 string bsub(B_index bs, B_index be) { return B.substr(bs, be-bs); } > 35 > 36 // update if better > 37 void update(string* a, const string& b) > 38 { > 39 if( a->size()<b.size() || (a->size()==b.size() && *a>b) ) > 40 *a = b; > 41 } > 42 > 43 // O(|J|^4) in total > 44 string solve() > 45 { > 46 string result; > 47 for(J_index js=0; js< J.size(); ++js) > 48 for(J_index je=js+1; je<=J.size(); ++je) // for each J[js, je) > 49 for(J_index ks=0; ks< J.size(); ++ks) // try each J[ks, ks+k) > 50 { > 51 // ... is a maximal nonempty prefix of J[js, je) not int > 52 int k; > 53 for(k=0; js+k<je && ks+k<J.size() && (ks+k<js || je<=ks) > 54 if( J[js+k] != J[ks+k] ) > 55 break; > 56 if( k==0 ) > 57 continue; > 58 > 59 // we have J[js, js+k)+J[js+k, je) in J, so we want > 60 // J[ks, ks+k) J[js > 61 string m = findInB(J_sub(js+k,je)); > 62 if( !m.empty() ) > 63 update(&result, jsub(js,je)+m); > 64 } > 65 return result; > 66 } > 67 > 68 // O(|J|^2 |B|^2) in total > 69 map<J_sub, string> memo_fib; > 70 string findInB(J_sub key) > 71 { > 72 if( memo_fib.count(key) ) > 73 return memo_fib[key]; > 74 > 75 string result; > 76 for(string::iterator it=B.begin(); it!=B.end(); ++it) // for eac > 77 { > 78 it = search(it, B.end(), J.begin()+key.first, J.begin()+ > 79 if( it == B.end() ) > 80 break; > 81 B_index ks = it - B.begin(); > 82 B_index ke = ks + (key.second - key.first); > 83 > 84 // try all B[ks, ke)+B[ke, kee), and try to find another > 85 for(B_index kee=ke+1; kee<=B.size(); ++kee) > 86 if( canFindBefore(ks, B_sub(ke,kee)) || canFindA > 87 update(&result, bsub(ke,kee)); > 88 } > 89 return memo_fib[key] = result; > 90 } > 91 > 92 // O(|B|^4) in total : for each substring m of B, the first and last occ > 93 bool canFindBefore(B_index bi, B_sub m) { return firstOccurence(m)+(m.se > 94 bool canFindAfter (B_index bi, B_sub m) { return bi <= lastOccurence(m)- > 95 > 96 map<B_sub, B_index> memo_fo; > 97 B_index firstOccurence(B_sub m) > 98 { > 99 if( memo_fo.count(m) ) > 100 return memo_fo[m]; > 101 return memo_fo[m] = search(B.begin(), B.end(), B.begin()+m.first > 102 } > 103 > 104 map<B_sub, B_index> memo_lo; > 105 B_index lastOccurence(B_sub m) > 106 { > 107 if( memo_lo.count(m) ) > 108 return memo_lo[m]; > 109 int mrs = B.size() - m.second; > 110 int mre = B.size() - m.first; > 111 return memo_lo[m] = B.size() - (search(B.rbegin(), B.rend(), B.r > 112 } > 113 }; > 114 > 115 // BEGIN CUT HERE > 116 #include <ctime> > 117 double start_time; string timer() > 118 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 119 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 120 { os << "{ "; > 121 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 122 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 123 void verify_case(const string& Expected, const string& Received) { > 124 bool ok = (Expected == Received); > 125 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 126 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 127 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 128 #define END verify_case(_, TheBoringStoreDivOne().find(J, B));} > 129 int main(){ > 130 > 131 CASE(0) > 132 string J = "StoreOfJohn"; > 133 string B = "StoreOfBrus"; > 134 string _ = "or"; > 135 END > 136 CASE(1) > 137 string J = "JohnAndJohn"; > 138 string B = "John"; > 139 string _ = ""; > 140 END > 141 CASE(2) > 142 string J = "JohnLikesToPlayGames"; > 143 string B = "BrusAlsoLikesToPlayGames"; > 144 string _ = "esToPlayGames"; > 145 END > 146 CASE(3) > 147 string J = "abacaba"; > 148 string B = "abacabadabacaba"; > 149 string _ = "abaabacaba"; > 150 END > 151 CASE(4) > 152 string J = "abacabaabacabaabacabaabacabaabacabaabacabaabacabaab"; > 153 string B = "abacabaabacabaabacabaabacabaabacabaabacabaabacabaab"; > 154 string _ = "abacabaabacabaabacabaabacabaabacabaabacabaabacabaab"; > 155 END > 156 CASE(5) > 157 string J = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; > 158 string B = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; > 159 string _ = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; > 160 END > 161 } > 162 // END CUT HERE
Added SRM/489-U/1C-U.cpp version [d8218ea4a0fb855c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheBoringGameDivOne { public: > 23 vector <int> find(int sJ, int kJ, int sB, int kB, int sF, int kF) > 24 { > 25 // init: > 26 // ( 0, 0, 0, 0, 0, 0) > 27 // possible transitions: > 28 // (+1,+1, 0, 0, 0, 0) > 29 // ( 0, 0,+1,+1, 0, 0) > 30 // ( 0,+2, 0, 0, 0, 0) > 31 // ( 0, 0, 0,+2, 0, 0) > 32 // (+1,+1, 0, 0,+1,+1) > 33 // ( 0, 0,+1,+1,+1,+1) > 34 // ( 0, 0, 0, 0,+2,+2) > 35 // (-1,+1, 0, 0,+1,+1) > 36 // ( 0, 0,-1,+1,+1,+1) > 37 vector<int> result; > 38 if(sF!=kF) > 39 return result; > 40 > 41 for(int f=0; 2*f<=sF; ++f) > 42 { > 43 int ff = sF - 2*f; > 44 if( kJ-ff>=0 && sB-ff>=0 ) > 45 { > 46 vector<int> c = calc(sJ+ff, kJ-ff, sB+ff, kB-ff) > 47 if( !c.empty() ) > 48 { > 49 c[0] += f+ff; > 50 c[1] += f+ff; > 51 if( result.empty() ) > 52 result = c; > 53 else { > 54 result[0] = min(result[0],c[0]); > 55 result[1] = max(result[1],c[1]); > 56 } > 57 } > 58 } > 59 } > 60 return result; > 61 } > 62 vector<int> calc(int sJ, int kJ, int sB, int kB) > 63 { > 64 // init: > 65 // ( 0, 0, 0, 0,) > 66 // possible transitions: > 67 // (+1,+1, 0, 0) > 68 // ( 0, 0,+1,+1) > 69 // ( 0,+2, 0, 0) > 70 // ( 0, 0, 0,+2) > 71 if(kJ<sJ || kB<sB || (kJ-sJ)%2==1 || (kB-sB)%2==1) > 72 return vector<int>(); > 73 return vector<int>(2, sJ+(kJ-sJ)/2 + sB+(kB-sB)/2); > 74 } > 75 }; > 76 > 77 // BEGIN CUT HERE > 78 #include <ctime> > 79 double start_time; string timer() > 80 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 81 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 82 { os << "{ "; > 83 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 84 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 85 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 86 bool ok = (Expected == Received); > 87 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 88 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 89 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 90 #define END verify_case(_, TheBoringGameDivOne().find(scoreJ, killedJ, scor > 91 int main(){ > 92 > 93 CASE(0) > 94 int scoreJ = 1; > 95 int killedJ = 1; > 96 int scoreB = 1; > 97 int killedB = 1; > 98 int scoreF = 2; > 99 int killedF = 2; > 100 int __[] = {2, 3 }; > 101 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 102 END > 103 CASE(1) > 104 int scoreJ = 0; > 105 int killedJ = 0; > 106 int scoreB = 0; > 107 int killedB = 0; > 108 int scoreF = 0; > 109 int killedF = 0; > 110 int __[] = {0, 0 }; > 111 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 112 END > 113 CASE(2) > 114 int scoreJ = 4; > 115 int killedJ = 7; > 116 int scoreB = -2; > 117 int killedB = 5; > 118 int scoreF = 1; > 119 int killedF = 9; > 120 vector <int> _; > 121 END > 122 CASE(3) > 123 int scoreJ = 1; > 124 int killedJ = 5; > 125 int scoreB = -1; > 126 int killedB = 4; > 127 int scoreF = 3; > 128 int killedF = 6; > 129 int __[] = {8, 9 }; > 130 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 131 END > 132 /* > 133 CASE(4) > 134 int scoreJ = ; > 135 int killedJ = ; > 136 int scoreB = ; > 137 int killedB = ; > 138 int scoreF = ; > 139 int killedF = ; > 140 int __[] = ; > 141 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 142 END > 143 CASE(5) > 144 int scoreJ = ; > 145 int killedJ = ; > 146 int scoreB = ; > 147 int killedB = ; > 148 int scoreF = ; > 149 int killedF = ; > 150 int __[] = ; > 151 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 152 END > 153 */ > 154 } > 155 // END CUT HERE
Added SRM/490-U/1A.cpp version [d56c04a6bea2df82]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 int gcd(int a, int b) > 23 { > 24 while(a) > 25 swap(a, b%=a); > 26 return b; > 27 } > 28 > 29 class Starport { public: > 30 double getExpectedTime(int N, int M) > 31 { > 32 return (N-gcd(N,M)) / 2.0; > 33 } > 34 }; > 35 > 36 // BEGIN CUT HERE > 37 #include <ctime> > 38 double start_time; string timer() > 39 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 40 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 41 { os << "{ "; > 42 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 43 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 44 void verify_case(const double& Expected, const double& Received) { > 45 bool ok = (abs(Expected - Received) < 1e-9); > 46 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 47 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 48 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 49 #define END verify_case(_, Starport().getExpectedTime(N, M));} > 50 int main(){ > 51 > 52 CASE(0) > 53 int N = 4; > 54 int M = 2; > 55 double _ = 1.0; > 56 END > 57 CASE(1) > 58 int N = 5; > 59 int M = 3; > 60 double _ = 2.0; > 61 END > 62 CASE(2) > 63 int N = 6; > 64 int M = 1; > 65 double _ = 2.5; > 66 END > 67 CASE(3) > 68 int N = 12345; > 69 int M = 2345; > 70 double _ = 6170.0; > 71 END > 72 CASE(3) > 73 int N = 1; > 74 int M = 1; > 75 double _ = 0; > 76 END > 77 CASE(3) > 78 int N = 1; > 79 int M = 100; > 80 double _ = 0; > 81 END > 82 CASE(3) > 83 int N = 2; > 84 int M = 1; > 85 double _ = 0.5; > 86 END > 87 CASE(7) > 88 int N = 6; > 89 int M = 4; > 90 double _ = 2; > 91 END > 92 CASE(7) > 93 int N = 4; > 94 int M = 6; > 95 double _ = 1; > 96 END > 97 CASE(4) > 98 int N = 3; > 99 int M = 1000000000; > 100 double _ = -1; > 101 END > 102 CASE(5) > 103 int N = 1000000000; > 104 int M = 3; > 105 double _ = -1; > 106 END > 107 CASE(6) > 108 int N = 1000000000; > 109 int M = 1000000000; > 110 double _ = -1; > 111 END > 112 > 113 } > 114 // END CUT HERE
Added SRM/490-U/1B.cpp version [67cc5158338e6e25]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class QuickT9 { public: > 23 int minimumPressings(vector <string> t9, string word) > 24 { > 25 map< string, set<string> > D2U; > 26 for(int i=0; i<t9.size(); ++i) { > 27 stringstream sin(t9[i]); > 28 for(string u;sin>>u;) { > 29 string d; > 30 for(int i=0; i<u.size(); ++i) { > 31 if(u[i]<='c') d+='2'; > 32 else if(u[i]<='f') d+='3'; > 33 else if(u[i]<='i') d+='4'; > 34 else if(u[i]<='l') d+='5'; > 35 else if(u[i]<='o') d+='6'; > 36 else if(u[i]<='s') d+='7'; > 37 else if(u[i]<='v') d+='8'; > 38 else if(u[i]<='z') d+='9'; > 39 D2U[d].insert( u.substr(0,i+1) ); > 40 } > 41 } > 42 } > 43 return rec(D2U, word, 0, "", ""); > 44 } > 45 > 46 // Now, the state is F=word[0..i), D=D, U=U. What is the minimal number > 47 // It is computed by simply implementing the problem statement, with mem > 48 map<pair<int,string>, int> memo; > 49 int rec( map< string, set<string> >& D2U, const string& word, int i, con > 50 { > 51 if( i == word.size() ) return 0; > 52 if( i < 0 ) return -1; > 53 > 54 pair<int,string> key(i,U); > 55 if( memo.count(key) ) > 56 return memo[key]; > 57 memo[key] = -1; // avoid cyclic dependency > 58 > 59 int best = -1; > 60 #define UPDATE(s,k) { if(s!=-1 && (best==-1 || s+(k)<best)) best > 61 > 62 // try RIGHT > 63 { > 64 int cp = 0; > 65 while( i+cp<word.size() && cp<U.size() && word[i+cp] == > 66 ++cp; > 67 int k = U.size()-cp; > 68 int s = rec(D2U, word, i+U.size()-k, "", ""); > 69 UPDATE(s,k+1); > 70 } > 71 // try C > 72 { > 73 int cp = 0; > 74 while( i+cp<word.size() && cp<U.size() && word[i+cp] == > 75 ++cp; > 76 int k = U.size()-cp; > 77 if(k==0) k=1; // the only difference is here > 78 int s = rec(D2U, word, i+U.size()-k, "", ""); > 79 UPDATE(s,k); > 80 } > 81 // try * > 82 if( !D.empty() ) { > 83 set<string>& Us = D2U[D]; > 84 set<string>::iterator it = Us.find(U); > 85 ++it; > 86 if( it == Us.end() ) > 87 it = Us.begin(); > 88 int s = rec(D2U, word, i, D, *it); > 89 UPDATE(s,1); > 90 } > 91 // try 2-9 > 92 for(char c='2'; c<='9'; ++c) > 93 { > 94 string nextD = D + c; > 95 if( D2U.count(nextD) ) { > 96 set<string>& Us = D2U[nextD]; > 97 int s = rec(D2U, word, i, nextD, *Us.begin()); > 98 UPDATE(s,1); > 99 } > 100 } > 101 // done > 102 return memo[key] = best; > 103 } > 104 }; > 105 > 106 // BEGIN CUT HERE > 107 #include <ctime> > 108 double start_time; string timer() > 109 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 110 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 111 { os << "{ "; > 112 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 113 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 114 void verify_case(const int& Expected, const int& Received) { > 115 bool ok = (Expected == Received); > 116 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 117 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 118 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 119 #define END verify_case(_, QuickT9().minimumPressings(t9, word));} > 120 int main(){ > 121 > 122 CASE(0) > 123 string t9_[] = {"aae", "bab", "abad", "bdbd", "beta"}; > 124 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 125 string word = "babe"; > 126 int _ = 9; > 127 END > 128 CASE(1) > 129 string t9_[] = {"ann","ie"}; > 130 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 131 string word = "annie"; > 132 int _ = 7; > 133 END > 134 CASE(2) > 135 string t9_[] = {"ann","amm"}; > 136 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 137 string word = "annie"; > 138 int _ = -1; > 139 END > 140 CASE(3) > 141 string t9_[] = {"aaa aab","aac aba abb ccca"}; > 142 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 143 string word = "aba"; > 144 int _ = 6; > 145 END > 146 CASE(4) > 147 string t9_[] = {"acac aba aaab","aab aa baa","bba bacade abb","baba"}; > 148 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 149 string word = "abbaca"; > 150 int _ = 10; > 151 END > 152 CASE(5) > 153 string t9_[] = {"aaa aab aac","aba abb","ccca"}; > 154 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 155 string word = "ccc"; > 156 int _ = 5; > 157 END > 158 CASE(6) > 159 string t9_[] = {"abc"}; > 160 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 161 string word = "a"; > 162 int _ = 2; > 163 END > 164 /* > 165 CASE(7) > 166 string t9_[] = ; > 167 vector <string> t9(t9_, t9_+sizeof(t9_)/sizeof(*t9_)); > 168 string word = ; > 169 int _ = ; > 170 END > 171 */ > 172 } > 173 // END CUT HERE
Added SRM/490-U/1C-U.cpp version [2698a1e7c9451719]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class InfiniteLab { public: > 23 long long getDistance(vector <string> map, long long r1, int c1, long lo > 24 { > 25 LL H = map.size(); > 26 LL W = map[0].size(); > 27 > 28 if( r1 > r2 ) { > 29 swap(r1,r2); > 30 swap(c1,c2); > 31 } > 32 if( r1 < 0 ) { > 33 // check!!!! > 34 LL Hx = (-r1/H + 1)*H; > 35 r1 += Hx; > 36 r2 += Hx; > 37 } > 38 cerr << r1 << " " << r2 << endl; > 39 > 40 vector<string> mmm(map); > 41 mmm.insert(mmm.end(), map.begin(), map.end()); > 42 mmm.insert(mmm.end(), map.begin(), map.end()); > 43 > 44 if( r1/H == r2/H ) > 45 return dist(mmm, H+(r1%H), c1, H+(r2%H), c2); > 46 return -1; > 47 > 48 // top to top > 49 vector<vector<LL> > tochu(W, vector<LL>(W, -1)); > 50 for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' ) > 51 for(int x2=0; x2<W; ++x2) if( map[0][x2] != '#' ) > 52 tochu[x1][x2] = dist( mmm, H, x1, H*2, x2 ); > 53 > 54 // r1c1 to top > 55 vector<LL> from(W, -1); > 56 for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' ) > 57 from[W] = dist( mmm, H+(r1%H), c1, H*2, x1 ); > 58 > 59 // top to r2c2 > 60 vector<LL> to(W, -1); > 61 for(int x1=0; x1<W; ++x1) if( map[0][x1] != '#' ) > 62 to[W] = dist( mmm, H, x1, H*2+(r2%H), c2 ); > 63 > 64 LL best = -1; > 65 for(int i=0; i<from.size(); ++i) if( from[i]>=0 ) > 66 for(int j=0; j<to.size(); ++j) if( to[j]>=0 ) > 67 { > 68 LL sc = from[i] + to[j]; > 69 LL s2 = -1; // TODO! > 70 if( s2 != -1 && (best == -1 || s2+sc<best) ) > 71 best = s2+sc; > 72 } > 73 return best; > 74 } > 75 > 76 LL dist(vector<string>& mmm, LL y1, LL x1, LL y2, LL x2) > 77 { > 78 vector< pair<LL,LL> > Q; > 79 set< pair<LL,LL> > V; > 80 Q.push_back( make_pair(y1,x1) ); > 81 V.insert( make_pair(y1,x1) ); > 82 for(int d=0; !Q.empty(); ++d) > 83 { > 84 vector< pair<LL,LL> > Q_next; > 85 for(int i=0; i<Q.size(); ++i) > 86 { > 87 LL y = Q[i].first; > 88 LL x = Q[i].second; > 89 if( y==y2 && x==x2 ) > 90 return d; > 91 int dy[] = {-1,+1,0,0}; > 92 int dx[] = {0,0,-1,+1}; > 93 for(int i=0; i<4; ++i) { > 94 LL yy = y+dy[i]; > 95 LL xx = x+dx[i]; > 96 if( 0<=yy && yy<mmm.size() && 0<=xx && x > 97 pair<LL,LL> p(yy,xx); > 98 if( ! V.count(p) ) { > 99 V.insert(p); > 100 Q_next.push_back(p); > 101 } > 102 } > 103 } > 104 if( mmm[y][x] == 'T' ) { > 105 for(int xt=0; xt<mmm[y].size(); ++xt) if > 106 pair<LL,LL> p(y,xt); > 107 if( ! V.count(p) ) { > 108 V.insert(p); > 109 Q_next.push_back(p); > 110 } > 111 break; > 112 } > 113 } > 114 } > 115 Q_next.swap(Q); > 116 } > 117 return -1; > 118 } > 119 }; > 120 > 121 // BEGIN CUT HERE > 122 #include <ctime> > 123 double start_time; string timer() > 124 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 125 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 126 { os << "{ "; > 127 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 128 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 129 void verify_case(const long long& Expected, const long long& Received) { > 130 bool ok = (Expected == Received); > 131 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 132 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 133 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 134 #define END verify_case(_, InfiniteLab().getDistance(map, r1, c1, r2, c2)); > 135 int main(){ > 136 > 137 CASE(0) > 138 string map_[] = {"#...##", > 139 ".##...", > 140 "..#.##", > 141 "#.#.##"}; > 142 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 143 long long r1 = 1LL; > 144 int c1 = 0; > 145 long long r2 = 5LL; > 146 int c2 = 3; > 147 long long _ = 7LL; > 148 END > 149 CASE(1) > 150 string map_[] = {"##.#.", > 151 ".#T#T", > 152 "...#.", > 153 "##.#."}; > 154 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 155 long long r1 = 7LL; > 156 int c1 = 4; > 157 long long r2 = 1LL; > 158 int c2 = 0; > 159 long long _ = 9LL; > 160 END > 161 CASE(2) > 162 string map_[] = {"..######.#", > 163 ".###T###.T", > 164 "..T#.##T##", > 165 ".######..#"}; > 166 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 167 long long r1 = 1LL; > 168 int c1 = 0; > 169 long long r2 = 6LL; > 170 int c2 = 4; > 171 long long _ = 11LL; > 172 END > 173 CASE(3) > 174 string map_[] = {"..#..", > 175 ".#.#.", > 176 "....."}; > 177 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 178 long long r1 = -29LL; > 179 int c1 = 2; > 180 long long r2 = 19LL; > 181 int c2 = 2; > 182 long long _ = 54LL; > 183 END > 184 CASE(4) > 185 string map_[] = {".#.#.", > 186 "..#..", > 187 ".....", > 188 ".....", > 189 "..#.."}; > 190 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 191 long long r1 = -999LL; > 192 int c1 = 3; > 193 long long r2 = 100LL; > 194 int c2 = 2; > 195 long long _ = -1LL; > 196 END > 197 /* > 198 CASE(5) > 199 string map_[] = ; > 200 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 201 long long r1 = LL; > 202 int c1 = ; > 203 long long r2 = LL; > 204 int c2 = ; > 205 long long _ = LL; > 206 END > 207 CASE(6) > 208 string map_[] = ; > 209 vector <string> map(map_, map_+sizeof(map_)/sizeof(*map_)); > 210 long long r1 = LL; > 211 int c1 = ; > 212 long long r2 = LL; > 213 int c2 = ; > 214 long long _ = LL; > 215 END > 216 */ > 217 } > 218 // END CUT HERE
Added SRM/491/1A.cpp version [ec3c7b925e5f4d41]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class FoxMakingDice { public: > 23 long long theCount(int N, int K) > 24 { > 25 LL cnt = 0; > 26 for(int sum=K; sum<=N+N; ++sum) > 27 { > 28 LL v = 0; > 29 for(int s=1; s<=N; ++s) > 30 if( s<sum-s && 1<=sum-s && sum-s<=N ) > 31 ++v; > 32 cnt += v*(v-1)*(v-2)/6*2; > 33 } > 34 return cnt; > 35 } > 36 }; > 37 > 38 // BEGIN CUT HERE > 39 #include <ctime> > 40 double start_time; string timer() > 41 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 42 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 43 { os << "{ "; > 44 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 45 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 46 void verify_case(const long long& Expected, const long long& Received) { > 47 bool ok = (Expected == Received); > 48 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 49 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 50 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 51 #define END verify_case(_, FoxMakingDice().theCount(N, K));} > 52 int main(){ > 53 > 54 CASE(0) > 55 int N = 6; > 56 int K = 7; > 57 long long _ = 2LL; > 58 END > 59 CASE(1) > 60 int N = 5; > 61 int K = 7; > 62 long long _ = 0LL; > 63 END > 64 CASE(2) > 65 int N = 1000; > 66 int K = 1; > 67 long long _ = 20625666000LL; > 68 END > 69 CASE(3) > 70 int N = 456; > 71 int K = 123; > 72 long long _ = 879075732LL; > 73 END > 74 CASE(4) > 75 int N = 913; > 76 int K = 1014; > 77 long long _ = 4506149340LL; > 78 END > 79 CASE(5) > 80 int N = 1000; > 81 int K = 2000; > 82 long long _ = -1LL; > 83 END > 84 CASE(6) > 85 int N = 1; > 86 int K = 7; > 87 long long _ = 0LL; > 88 END > 89 CASE(6) > 90 int N = 2; > 91 int K = 7; > 92 long long _ = 0LL; > 93 END > 94 CASE(6) > 95 int N = 3; > 96 int K = 7; > 97 long long _ = 0LL; > 98 END > 99 CASE(6) > 100 int N = 4; > 101 int K = 7; > 102 long long _ = 0LL; > 103 END > 104 CASE(6) > 105 int N = 5; > 106 int K = 7; > 107 long long _ = 0LL; > 108 END > 109 > 110 } > 111 // END CUT HERE
Added SRM/491/1B.cpp version [258b2dbb2f8262f7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PrefixTree { public: > 23 static vector<int> wordToFreq( const string& word ) > 24 { > 25 vector<int> freq(26); > 26 for(int i=0; i<word.size(); ++i) > 27 freq[word[i]-'a']++; > 28 return freq; > 29 } > 30 > 31 static vector< vector<int> > wordsToFreqs( const vector<string>& words ) > 32 { > 33 vector< vector<int> > freqs; > 34 transform(words.begin(), words.end(), back_inserter(freqs), &wor > 35 return freqs; > 36 } > 37 > 38 static int commonPrefix(const vector< vector<int> >& freqs, int s) > 39 { > 40 vector<int> common; > 41 for(int first=1,i=0; (1<<i)<=s; ++i) > 42 if( s & (1<<i) ) > 43 if( first ) { > 44 common = freqs[i]; > 45 first = 0; > 46 } else { > 47 for(int k=0; k<26; ++k) > 48 common[k] = min(common[k], freqs > 49 } > 50 return accumulate(common.begin(), common.end(), 0); > 51 } > 52 > 53 int getNumNodes(vector<string> words) > 54 { > 55 const int N = words.size(); > 56 const vector< vector<int> >& freqs = wordsToFreqs(words); > 57 > 58 vector<int> dp(1<<N); > 59 for(int s=1; s<(1<<N); ++s) > 60 { > 61 dp[s] = 1; > 62 for(int i=0; i<N; ++i) > 63 if( s & (1<<i) ) > 64 dp[s] += words[i].size(); > 65 > 66 int cp = commonPrefix(freqs, s); > 67 for(int ss=s-1; ss&=s; --ss) > 68 dp[s] = min(dp[s], dp[ss]+dp[s&~ss]-cp-1); > 69 } > 70 return dp[(1<<N)-1]; > 71 } > 72 }; > 73 > 74 // BEGIN CUT HERE > 75 #include <ctime> > 76 double start_time; string timer() > 77 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 78 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 79 { os << "{ "; > 80 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 81 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 82 void verify_case(const int& Expected, const int& Received) { > 83 bool ok = (Expected == Received); > 84 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 85 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 86 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 87 #define END verify_case(_, PrefixTree().getNumNodes(words));} > 88 int main(){ > 89 > 90 CASE(0) > 91 string words_[] = {"topcoder"}; > 92 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 93 int _ = 9; > 94 END > 95 CASE(1) > 96 string words_[] = {"topcoder","topcoder"}; > 97 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 98 int _ = 9; > 99 END > 100 CASE(2) > 101 string words_[] = {"aab","ca"}; > 102 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 103 int _ = 5; > 104 END > 105 CASE(3) > 106 string words_[] = {"aab","ca","ba"}; > 107 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 108 int _ = 5; > 109 END > 110 CASE(4) > 111 string words_[] = {"ab","cd","ef"}; > 112 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 113 int _ = 7; > 114 END > 115 CASE(5) > 116 string words_[] = {"a","aa","aaa"}; > 117 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 118 int _ = 4; > 119 END > 120 /* > 121 CASE(6) > 122 string words_[] = ; > 123 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 124 int _ = ; > 125 END > 126 CASE(7) > 127 string words_[] = ; > 128 vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); > 129 int _ = ; > 130 END > 131 */ > 132 } > 133 // END CUT HERE
Added SRM/491/1C.cpp version [fc13d82ca1ace550]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 template<typename T> > 23 class IdGen > 24 { > 25 map<T, int> v2id_; > 26 vector<T> id2v_; > 27 public: > 28 int v2id(const T& v) { > 29 if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } > 30 return v2id_[v]; > 31 } > 32 const T& id2v(int i) const { return id2v_[i]; } > 33 int size() const { return id2v_.size(); } > 34 }; > 35 > 36 template<typename Vert, typename Cost, typename Flow, int NV=256> > 37 class MinCostFlow > 38 { > 39 IdGen<Vert> idgen; > 40 > 41 vector<int> G[NV]; > 42 Flow F[NV][NV]; > 43 Cost C[NV][NV]; > 44 > 45 public: > 46 void addEdge( Vert s_, Vert t_, Cost c, Flow f ) > 47 { > 48 int s = idgen.v2id(s_), t = idgen.v2id(t_); > 49 G[s].push_back(t); > 50 G[t].push_back(s); > 51 C[s][t] = c; > 52 C[t][s] = -c; > 53 F[s][t] = f; > 54 F[t][s] = 0; > 55 } > 56 > 57 pair<Cost, Flow> calc( Vert s_, Vert t_ ) > 58 { > 59 const int N=idgen.size(), S=idgen.v2id(s_), T=idgen.v2id(t_); > 60 static const Cost COST_INF = 1e+300; // !!EDIT HERE!! > 61 static const Flow FLOW_INF = 0x7fffffff; > 62 > 63 Cost total_cost = 0; > 64 Flow total_flow = 0; > 65 vector<Cost> h(N, 0); // potential > 66 for(Flow RF=FLOW_INF; RF>0; ) // residual flow > 67 { > 68 // Dijkstra -- find the min-cost path > 69 vector<Cost> d(N, COST_INF); d[S] = 0; > 70 vector<int> prev(N, -1); > 71 > 72 typedef pair< Cost, pair<int,int> > cedge; > 73 priority_queue< cedge, vector<cedge>, greater<cedge> > Q > 74 Q.push( cedge(0, make_pair(S,S)) ); > 75 while( !Q.empty() ) { > 76 cedge e = Q.top(); Q.pop(); > 77 if( prev[e.second.second] >= 0 ) > 78 continue; > 79 prev[e.second.second] = e.second.first; > 80 > 81 int u = e.second.second; > 82 for(int i=0; i<G[u].size(); ++i) { > 83 int v = G[u][i]; > 84 Cost r_cost = C[u][v] + h[u] - h[v]; > 85 if( F[u][v] > 0 && d[v] > d[u]+r_cost ) > 86 Q.push( cedge(d[v]=d[u]+r_cost, > 87 } > 88 } > 89 > 90 if( prev[T] < 0 ) > 91 break; // Finished > 92 > 93 // Run the flow as much as possible > 94 Flow f = RF; > 95 for(int u=T; u!=S; u=prev[u]) > 96 f = min(f, F[prev[u]][u]); > 97 RF -= f; > 98 total_flow += f; > 99 > 100 for(int u=T; u!=S; u=prev[u]) > 101 { > 102 total_cost += f * C[prev[u]][u]; > 103 F[prev[u]][u] -= f; > 104 F[u][prev[u]] += f; > 105 } > 106 > 107 // Update the potential > 108 for(int u=0; u<N; ++u) > 109 h[u] += d[u]; > 110 } > 111 return make_pair(total_cost, total_flow); > 112 } > 113 }; > 114 > 115 class FoxCardGame { public: > 116 double theMaxProportion(vector <double> pileA, vector <double> pileB, in > 117 { > 118 double L=1, R=50; > 119 for(int i=0; i<50; ++i) > 120 (possible(pileA, pileB, k, (L+R)/2) ? L : R) = (L+R)/2; > 121 return L; > 122 } > 123 > 124 enum Tag {Src, Left, Right, Target, Goal}; > 125 bool possible(vector <double> pileA, vector <double> pileB, int k, doubl > 126 { > 127 MinCostFlow<pair<Tag,int>, double, int> mcf; > 128 > 129 for(int a=0; a<pileA.size(); ++a) > 130 mcf.addEdge( make_pair(Src,0), make_pair(Left,a), 0.0, 1 > 131 for(int a=0; a<pileA.size(); ++a) > 132 for(int b=0; b<pileB.size(); ++b) > 133 { > 134 double x_ = pileA[a]+pileB[b], y_ = pileA[a]*pileB[b]; > 135 double x = max(x_,y_), y = min(x_,y_); > 136 mcf.addEdge( make_pair(Left,a), make_pair(Right,b), R*y- > 137 } > 138 for(int b=0; b<pileB.size(); ++b) > 139 mcf.addEdge( make_pair(Right,b), make_pair(Target,0), 0. > 140 mcf.addEdge( make_pair(Target,0), make_pair(Goal,0), 0.0, k ); > 141 > 142 return mcf.calc( make_pair(Src,0), make_pair(Goal,0) ).first <= > 143 } > 144 }; > 145 > 146 // BEGIN CUT HERE > 147 #include <ctime> > 148 double start_time; string timer() > 149 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 150 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 151 { os << "{ "; > 152 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 153 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 154 void verify_case(const double& Expected, const double& Received) { > 155 bool ok = (abs(Expected - Received) < 1e-9); > 156 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 157 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 158 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 159 #define END verify_case(_, FoxCardGame().theMaxProportion(pileA, pileB, k)) > 160 int main(){ > 161 > 162 CASE(0) > 163 double pileA_[] = {1, 2, 3}; > 164 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 165 double pileB_[] = {4, 5, 6}; > 166 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 167 int k = 2; > 168 double _ = 1.7692307692309948; > 169 END > 170 CASE(1) > 171 double pileA_[] = {1.234, 5.678, 9.012, 3.456, 7.89}; > 172 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 173 double pileB_[] = {2.345, 6.789, 9.876, 5.432, 1.012}; > 174 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 175 int k = 3; > 176 double _ = 4.159424420079586; > 177 END > 178 CASE(2) > 179 double pileA_[] = {1, 1.1, 1.2, 1.3, 1.4, 1.5}; > 180 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 181 double pileB_[] = {5, 10, 15, 20, 25, 30}; > 182 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 183 int k = 2; > 184 double _ = 1.3972602739726827; > 185 END > 186 CASE(3) > 187 double pileA_[] = {85.302, 92.798, 76.813, 37.994, 36.737, 98.659}; > 188 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 189 double pileB_[] = {13.352, 7.3094, 54.761, 8.2706, 63.223, 37.486}; > 190 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 191 int k = 3; > 192 double _ = 33.58603889836175; > 193 END > 194 CASE(4) > 195 double pileA_[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11 > 196 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 197 double pileB_[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11 > 198 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 199 int k = 50; > 200 double _ = 16.846938775510203; > 201 END > 202 /* > 203 CASE(5) > 204 double pileA_[] = ; > 205 vector <double> pileA(pileA_, pileA_+sizeof(pileA_)/sizeof(*pileA_)); > 206 double pileB_[] = ; > 207 vector <double> pileB(pileB_, pileB_+sizeof(pileB_)/sizeof(*pileB_)); > 208 int k = ; > 209 double _ = ; > 210 END > 211 */ > 212 } > 213 // END CUT HERE
Added SRM/491/2C.cpp version [ba05691b3037f752]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL gcd(LL a, LL b) > 23 { > 24 while(a) > 25 swap(a, b%=a); > 26 return b; > 27 } > 28 > 29 LL lcm(LL a, LL b) > 30 { > 31 return a/gcd(a,b)*b; > 32 } > 33 > 34 class BottlesOnShelf { public: > 35 int getNumBroken(int N, vector <int> left, vector <int> right, vector <i > 36 { > 37 int total = 0; > 38 for(int mask=1; mask<(1<<left.size()); ++mask) > 39 total += (bitcnt(mask)%2==0 ? -1 : +1) * hit(N, left, ri > 40 return total; > 41 } > 42 > 43 int bitcnt(int m) > 44 { > 45 int c = 0; > 46 for(; m; m>>=1) > 47 c += (m&1); > 48 return c; > 49 } > 50 > 51 LL ceil(LL x, LL q) > 52 { > 53 return (x + q - 1) / q * q; > 54 } > 55 > 56 int hit(int N, vector <int> left, vector <int> right, vector <int> damag > 57 { > 58 LL theLeft = 1; > 59 LL theRight = N+1; > 60 LL theDamage = 1; > 61 for(int i=0; i<left.size(); ++i) > 62 if( mask & (1<<i) ) > 63 { > 64 theLeft = max<LL>(theLeft, left[i]); > 65 theRight = min<LL>(theRight, right[i]+1); > 66 theDamage = lcm(theDamage, damage[i]); > 67 } > 68 return int( max(0LL, ceil(theRight,theDamage)-ceil(theLeft,theDa > 69 } > 70 }; > 71 > 72 // BEGIN CUT HERE > 73 #include <ctime> > 74 double start_time; string timer() > 75 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 76 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 77 { os << "{ "; > 78 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 79 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 80 void verify_case(const int& Expected, const int& Received) { > 81 bool ok = (Expected == Received); > 82 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 83 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 84 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 85 #define END verify_case(_, BottlesOnShelf().getNumBroken(N, left, right, da > 86 int main(){ > 87 > 88 CASE(0) > 89 int N = 7; > 90 int left_[] = {1}; > 91 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 92 int right_[] = {7}; > 93 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 94 int damage_[] = {2}; > 95 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 96 int _ = 3; > 97 END > 98 CASE(1) > 99 int N = 7; > 100 int left_[] = {1,1}; > 101 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 102 int right_[] = {7,7}; > 103 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 104 int damage_[] = {2,3}; > 105 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 106 int _ = 4; > 107 END > 108 CASE(2) > 109 int N = 7; > 110 int left_[] = {1,1,1}; > 111 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 112 int right_[] = {7,7,7}; > 113 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 114 int damage_[] = {2,3,6}; > 115 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 116 int _ = 4; > 117 END > 118 CASE(3) > 119 int N = 10; > 120 int left_[] = {1,6}; > 121 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 122 int right_[] = {5,10}; > 123 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 124 int damage_[] = {1,7}; > 125 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 126 int _ = 6; > 127 END > 128 CASE(4) > 129 int N = 5; > 130 int left_[] = {4}; > 131 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 132 int right_[] = {4}; > 133 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 134 int damage_[] = {7}; > 135 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 136 int _ = 0; > 137 END > 138 CASE(5) > 139 int N = 1000000000; > 140 int left_[] = {1}; > 141 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 142 int right_[] = {1000000000}; > 143 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 144 int damage_[] = {1}; > 145 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 146 int _ = 1000000000; > 147 END > 148 /* > 149 CASE(6) > 150 int N = ; > 151 int left_[] = ; > 152 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 153 int right_[] = ; > 154 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 155 int damage_[] = ; > 156 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 157 int _ = ; > 158 END > 159 CASE(7) > 160 int N = ; > 161 int left_[] = ; > 162 vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_)); > 163 int right_[] = ; > 164 vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_)); > 165 int damage_[] = ; > 166 vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_)) > 167 int _ = ; > 168 END > 169 */ > 170 } > 171 // END CUT HERE
Added SRM/492-U/1A.cpp version [49693ab4e53ef682]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } > 23 double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } > 24 int ccw(const CMP& a, CMP b, CMP c) { > 25 b -= a; c -= a; > 26 if( outer_prod(b,c) > 0 ) return +1; // counter clockwise > 27 if( outer_prod(b,c) < 0 ) return -1; // clockwise > 28 if( inner_prod(b,c) < 0 ) return +2; // c--[a--b] on line > 29 if( norm(b) < norm(c) ) return -2; // [a--b]--c on line > 30 return 0; // [a--c--b] on line > 31 } > 32 > 33 class TimeTravellingGardener { public: > 34 int determineUsage(vector <int> distance, vector <int> height) > 35 { > 36 const int N = height.size(); > 37 > 38 distance.insert(distance.begin(), 0); > 39 partial_sum(distance.begin(), distance.end(), distance.begin()); > 40 > 41 vector<CMP> p; > 42 for(int i=0; i<N; ++i) > 43 p.push_back( CMP(distance[i], height[i]) ); > 44 > 45 int best = N-1; > 46 for(int i=0; i<N; ++i) > 47 for(int j=i+1; j<N; ++j) > 48 { > 49 best = min(best, score(p[i], p[j], p)); > 50 best = min(best, score(p[i].real(), p[j], p)); > 51 best = min(best, score(p[i], p[j].real(), p)); > 52 } > 53 return best; > 54 } > 55 > 56 int score(const CMP& a, const CMP& b, const vector<CMP>& p) > 57 { > 58 int s = 0; > 59 for(int i=0; i<p.size(); ++i) > 60 { > 61 if( ccw(a, b, p[i]) == -1 ) return INT_MAX; > 62 if( ccw(a, b, p[i].real()) == +1 ) return INT_MAX; > 63 if( ccw(a, b, p[i]) == +1 ) ++s; > 64 } > 65 return s; > 66 } > 67 }; > 68 > 69 // BEGIN CUT HERE > 70 #include <ctime> > 71 double start_time; string timer() > 72 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 73 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 74 { os << "{ "; > 75 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 76 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 77 void verify_case(const int& Expected, const int& Received) { > 78 bool ok = (Expected == Received); > 79 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 80 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 81 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 82 #define END verify_case(_, TimeTravellingGardener().determineUsage(distance > 83 int main(){ > 84 > 85 CASE(0) > 86 int distance_[] = {2,2}; > 87 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 88 int height_[] = {1,3,10}; > 89 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 90 int _ = 1; > 91 END > 92 CASE(1) > 93 int distance_[] = {3,3}; > 94 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 95 int height_[] = {3,1,3}; > 96 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 97 int _ = 2; > 98 END > 99 CASE(2) > 100 int distance_[] = {1,3}; > 101 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 102 int height_[] = {4,4,4}; > 103 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 104 int _ = 0; > 105 END > 106 CASE(3) > 107 int distance_[] = {4,2}; > 108 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 109 int height_[] = {9,8,5}; > 110 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 111 int _ = 1; > 112 END > 113 CASE(4) > 114 int distance_[] = {476,465,260,484}; > 115 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 116 int height_[] = {39,13,8,72,80}; > 117 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 118 int _ = 3; > 119 END > 120 CASE(5) > 121 int distance_[] = {173,36,668,79,26,544}; > 122 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 123 int height_[] = {488,743,203,446,444,91,453}; > 124 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 125 int _ = 5; > 126 END > 127 CASE(6) > 128 int distance_[] = {2,4,2,2,4,2,4,2,2,4}; > 129 vector <int> distance(distance_, distance_+sizeof(distance_)/sizeof(*d > 130 int height_[] = {2,2,10,10,10,16,16,22,22,28,28}; > 131 vector <int> height(height_, height_+sizeof(height_)/sizeof(*height_)) > 132 int _ = 6; > 133 END > 134 } > 135 // END CUT HERE
Added SRM/492-U/1B.cpp version [90d5ca1902401bc0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const LL INF = 0x1fffffffffffffffLL; > 23 > 24 template<typename T> > 25 struct DP3 > 26 { > 27 int N1, N2, N3; > 28 vector<T> data; > 29 DP3(int N1, int N2, int N3, const T& t = T()) > 30 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size() > 31 T& operator()(int i1, int i2, int i3) > 32 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 33 void swap(DP3& rhs) > 34 { data.swap(rhs.data); } > 35 }; > 36 > 37 class TimeTravellingTour { public: > 38 long long determineCost(int N, vector <int> cities, vector <string> road > 39 { > 40 vector< vector<LL> > G(N, vector<LL>(N,INF)); > 41 { > 42 string str = accumulate(roads.begin(), roads.end(), stri > 43 for(int i=0; i<str.size(); ++i) > 44 if( str[i] == ',' ) > 45 str[i] = ' '; > 46 stringstream sin(str); > 47 for(int a,b,cost; sin>>a>>b>>cost; ) > 48 G[a][b] = G[b][a] = cost; > 49 } > 50 for(int i=0; i<N; ++i) > 51 G[i][i] = 0; > 52 for(int k=0; k<N; ++k) > 53 for(int i=0; i<N; ++i) > 54 for(int j=0; j<N; ++j) > 55 G[i][j] = min(G[i][j], G[i][k]+G[k][j]); > 56 > 57 DP3<LL> memo(N, cities.size()+1, cities.size()+1, -1); > 58 LL a = rec( 0, 0, cities.size(), cities, G, memo ); > 59 return a>=INF ? -1 : a; > 60 } > 61 > 62 LL rec(int cur, int s, int e, const vector<int>& C, const vector< vector > 63 { > 64 if( s+1 == e ) > 65 return G[cur][C[s]]; > 66 if( memo(cur,s,e) >= 0 ) > 67 return memo(cur,s,e); > 68 > 69 LL best = INF; > 70 for(int v=0; v<G[cur].size(); ++v) > 71 if( G[cur][v] < INF ) > 72 for(int m=s+1; m<e; ++m) > 73 best = min(best, G[cur][v]+rec(v,s,m,C,G > 74 return memo(cur,s,e) = best; > 75 } > 76 }; > 77 > 78 // BEGIN CUT HERE > 79 #include <ctime> > 80 double start_time; string timer() > 81 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 82 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 83 { os << "{ "; > 84 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 85 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 86 void verify_case(const long long& Expected, const long long& Received) { > 87 bool ok = (Expected == Received); > 88 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 89 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 90 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 91 #define END verify_case(_, TimeTravellingTour().determineCost(N, cities, ro > 92 int main(){ > 93 > 94 CASE(0) > 95 int N = 5; > 96 int cities_[] = {2,3,2,4}; > 97 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 98 string roads_[] = {"0,2,4 0,1,2 2,1,2 1,3,3 4,0,4"}; > 99 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 100 long long _ = 13LL; > 101 END > 102 CASE(1) > 103 int N = 3; > 104 int cities_[] = {1,0,1}; > 105 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 106 string roads_[] = {"0,2,1"," 2",",1,5"}; > 107 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 108 long long _ = 12LL; > 109 END > 110 CASE(2) > 111 int N = 3; > 112 int cities_[] = {2}; > 113 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 114 string roads_[] = {"0,1,2"}; > 115 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 116 long long _ = -1LL; > 117 END > 118 CASE(3) > 119 int N = 6; > 120 int cities_[] = {4, 1, 3, 2}; > 121 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 122 string roads_[] = {"0,1,5 0,2,5 0,5,2 2,3,5 2,4,2 3,4,4 3,5,1"}; > 123 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 124 long long _ = 19LL; > 125 END > 126 /* > 127 CASE(4) > 128 int N = ; > 129 int cities_[] = ; > 130 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 131 string roads_[] = ; > 132 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 133 long long _ = LL; > 134 END > 135 CASE(5) > 136 int N = ; > 137 int cities_[] = ; > 138 vector <int> cities(cities_, cities_+sizeof(cities_)/sizeof(*cities_)) > 139 string roads_[] = ; > 140 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 141 long long _ = LL; > 142 END > 143 */ > 144 } > 145 // END CUT HERE
Added SRM/493-U/1A.cpp version [295e562b1438d93f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class StonesGame { public: > 23 string winner(int N, int M, int K, int L) > 24 { > 25 if( isOneStep(N,M,K,L) ) // M ‚©‚ç L ‚ɈêŽè‚Å‚¢‚¯‚é‚È‚çæŽè‚ÌŸ‚¿ > 26 return "Romeo"; > 27 for(int x=1; x<=N; ++x) > 28 if( isOneStep(N,M,K,x) && !isOneStep(N,x,K,L) ) > 29 return "Draw"; // æŽè‚àŒãŽè‚àŸ‚Ä‚È‚¢ê‡ > 30 return "Strangelet"; // M ‚©‚ç‚ÍuL‚ɈêŽè‚Ås‚¯‚¿‚Ⴄxv‚É‚µ‚©s‚¯‚È‚¢‚È‚çAŒãŽè‚ÌŸ‚¿ > 31 } > 32 > 33 bool isOneStep(int N, int M, int K, int L) > 34 { > 35 int d = abs(M-L); > 36 if( d<K && (d%2)!=(K%2) ) > 37 { > 38 int mid = (M+L)/2; // M ‚Æ L ‚Ì^‚ñ’†‚ð’†S‚É > 39 int e = mid + K/2; // ‰E‚É K/2 > 40 int b = e - K+1; // ¶‚É K/2 L‚΂µ‚½”͈͂ðreverse‚·‚ê‚ÎM‚ªL‚És‚¯‚é‚Í‚¸c > 41 return 1<=b && e<=N; // ‚»‚ñ‚Ȕ͈͂őåä•v‚© > 42 } > 43 return false; > 44 } > 45 }; > 46 > 47 // BEGIN CUT HERE > 48 #include <ctime> > 49 double start_time; string timer() > 50 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 51 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 52 { os << "{ "; > 53 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 54 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 55 void verify_case(const string& Expected, const string& Received) { > 56 bool ok = (Expected == Received); > 57 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 58 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 59 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 60 #define END verify_case(_, StonesGame().winner(N, M, K, L));} > 61 int main(){ > 62 > 63 CASE(0) > 64 int N = 3; > 65 int M = 1; > 66 int K = 1; > 67 int L = 2; > 68 string _ = "Draw"; > 69 END > 70 CASE(1) > 71 int N = 5; > 72 int M = 1; > 73 int K = 2; > 74 int L = 2; > 75 string _ = "Romeo"; > 76 END > 77 CASE(2) > 78 int N = 5; > 79 int M = 5; > 80 int K = 2; > 81 int L = 3; > 82 string _ = "Strangelet"; > 83 END > 84 CASE(3) > 85 int N = 5; > 86 int M = 5; > 87 int K = 2; > 88 int L = 2; > 89 string _ = "Draw"; > 90 END > 91 CASE(4) > 92 int N = 1000000; > 93 int M = 804588; > 94 int K = 705444; > 95 int L = 292263; > 96 string _ = "Romeo"; > 97 END > 98 CASE(5) > 99 int N = 1000000; > 100 int M = 100000; > 101 int K = 500000; > 102 int L = 600000; > 103 string _ = "Strangelet"; > 104 END > 105 CASE(6) > 106 int N = 5; > 107 int M = 1; > 108 int K = 3; > 109 int L = 5; > 110 string _ = "Strangelet"; > 111 END > 112 /* > 113 CASE(7) > 114 int N = ; > 115 int M = ; > 116 int K = ; > 117 int L = ; > 118 string _ = ; > 119 END > 120 */ > 121 } > 122 // END CUT HERE
Added SRM/493-U/1B.cpp version [13f8fe20f39799a2]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class AmoebaCode { public: > 23 // ƒƒCƒ“ƒ‹[ƒ`ƒ“ > 24 int find(string code, int K) > 25 { > 26 int answer = 1; > 27 for(int D=2; D<=K; ++D) > 28 if( can(code, K, 0, string(D-1,'X')) ) // Ŭ‹——£‚ª D ˆÈã‚É‚È‚é‚悤‚É‚Å‚«‚éH > 29 answer = max(answer, D); > 30 return answer; // ‚Å‚«‚é‚悤‚È D ‚ÌÅ‘å’l‚ð•Ô‚· > 31 } > 32 > 33 // ucode[i..$) ‚É 1 ` K ‚ð–„‚ß‚ç‚ê‚é‚©Hv‚ðÄ‹A“I‚É”»’è > 34 // prev ‚É‚Í code[i] ‚Ì’¼‘O D-1 •¶Žš‚ð“ü‚ê‚ČĂÑo‚·B > 35 // ‚±‚Ì D-1 •¶Žš‚Í”ð‚¯‚È‚¢‚ÆŬ‹——£ D ˆÈã‚É‚È‚ç‚È‚¢‚Ì‚Å”ð‚¯‚Ü‚µ‚傤 > 36 map<pair<size_t,string>, bool> memo; > 37 bool can(const string& code, const int K, size_t i, const string& prev) > 38 { > 39 if( i == code.size() ) > 40 return true; // ÅŒã‚Ü‚Å–„‚Ü‚Á‚½BOK! > 41 > 42 pair<int,string> key(i, prev); > 43 if( memo.count(key) ) > 44 return memo[key]; // ƒƒ‚‰»B‚·‚Å‚ÉŒ©‚½ˆø”‚È‚çŠo‚¦‚Ă錋‰Ê‚ð•Ô‚· > 45 > 46 // code[i] ‚É“ü‚ê‚镶Žš‚ÌŒó•â‚ð—ñ‹“ > 47 vector<char> cand; > 48 if( code[i] == '0' ) // '0' ‚È‚ç 1 ‚©‚ç K ‚Ì‚Ç‚ê‚É’u‚«Š·‚¦‚Ä‚à‚¢‚¢ > 49 for(int k=1; k<=K; ++k) > 50 cand.push_back( char('0'+k) ); > 51 else // ‚»‚êˆÈŠO‚È‚ç’u‚«Š·‚¦‚ç‚ê‚È‚¢ > 52 cand.push_back( code[i] ); > 53 > 54 bool ok = false; > 55 for(int k=0; k<cand.size(); ++k) // Œó•â‚ð‘S•”ŽŽ‚µ‚Ä‚Ý‚é > 56 if( count(prev.begin(), prev.end(), cand[k]) // prev > 57 && can(code, K, i+1, prev.substr(1)+cand[k]) ) // code[ > 58 ok |= true; > 59 > 60 return memo[key] = ok; // ƒƒ‚‰»‚Ì•\‚É‹L‰¯‚µ‚‚ return > 61 } > 62 }; > 63 > 64 // BEGIN CUT HERE > 65 #include <ctime> > 66 double start_time; string timer() > 67 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 68 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 69 { os << "{ "; > 70 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 71 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 72 void verify_case(const int& Expected, const int& Received) { > 73 bool ok = (Expected == Received); > 74 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 75 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 76 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 77 #define END verify_case(_, AmoebaCode().find(code, K));} > 78 int main(){ > 79 > 80 CASE(0) > 81 string code = "01"; > 82 int K = 1; > 83 int _ = 1; > 84 END > 85 CASE(1) > 86 string code = "1001"; > 87 int K = 2; > 88 int _ = 1; > 89 END > 90 CASE(2) > 91 string code = "1010"; > 92 int K = 2; > 93 int _ = 2; > 94 END > 95 CASE(3) > 96 string code = "01001"; > 97 int K = 3; > 98 int _ = 3; > 99 END > 100 CASE(4) > 101 string code = "10012031001"; > 102 int K = 3; > 103 int _ = 2; > 104 END > 105 CASE(5) > 106 string code = "00000000000000000000000000000000000000000000000000"; > 107 int K = 7; > 108 int _ = -1; > 109 END > 110 /* > 111 CASE(6) > 112 string code = ; > 113 int K = ; > 114 int _ = ; > 115 END > 116 */ > 117 } > 118 // END CUT HERE
Added SRM/493-U/2C.cpp version [11f187ebddae9d97]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CrouchingAmoebas { public: > 23 int count(vector <int> x, vector <int> y, int A, int T) > 24 { > 25 int answer = 0; > 26 > 27 const int N = x.size(); > 28 > 29 vector<int> ys, xs; > 30 for(int i=0; i<N; ++i) > 31 for(int dy=-T; dy<=+T; ++dy) > 32 ys.push_back(y[i]+dy); > 33 for(int i=0; i<N; ++i) > 34 for(int dx=-T; dx<=+T; ++dx) > 35 xs.push_back(x[i]+dx); > 36 > 37 for(int i=0; i<xs.size(); ++i) > 38 for(int k=0; k<ys.size(); ++k) > 39 { > 40 int Y = ys[k]; > 41 int X = xs[i]; > 42 vector<unsigned int> ds; > 43 for(int j=0; j<N; ++j) { > 44 unsigned int d = distance(Y,X,A,y[j],x[j]); > 45 if( d <= T ) > 46 ds.push_back(d); > 47 } > 48 sort( ds.begin(), ds.end() ); > 49 int e=T, c=0; > 50 for(int j=0; j<ds.size() && ds[j]<=e; ++j) > 51 e-=ds[j], ++c; > 52 answer = max(answer, c); > 53 } > 54 return answer; > 55 } > 56 > 57 static unsigned int distance(int Y, int X, int A, int y, int x) > 58 { > 59 unsigned int yd = (y<Y ? Y-y : Y+A<y ? y-(Y+A) : 0); > 60 unsigned int xd = (x<X ? X-x : X+A<x ? x-(X+A) : 0); > 61 return xd + yd; > 62 } > 63 }; > 64 > 65 // BEGIN CUT HERE > 66 #include <ctime> > 67 double start_time; string timer() > 68 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 69 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 70 { os << "{ "; > 71 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 72 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 73 void verify_case(const int& Expected, const int& Received) { > 74 bool ok = (Expected == Received); > 75 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 76 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 77 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 78 #define END verify_case(_, CrouchingAmoebas().count(x, y, A, T));} > 79 int main(){ > 80 > 81 CASE(0) > 82 int x_[] = {0,0}; > 83 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 84 int y_[] = {0,1}; > 85 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 86 int A = 1; > 87 int T = 1; > 88 int _ = 2; > 89 END > 90 CASE(1) > 91 int x_[] = {0,1,2}; > 92 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 93 int y_[] = {1,2,0}; > 94 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 95 int A = 1; > 96 int T = 1; > 97 int _ = 2; > 98 END > 99 CASE(2) > 100 int x_[] = {0,1,2}; > 101 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 102 int y_[] = {1,2,0}; > 103 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 104 int A = 1; > 105 int T = 2; > 106 int _ = 3; > 107 END > 108 CASE(3) > 109 int x_[] = {0,0,3,3}; > 110 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 111 int y_[] = {0,3,0,3}; > 112 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 113 int A = 2; > 114 int T = 4; > 115 int _ = 4; > 116 END > 117 CASE(4) > 118 int x_[] = {-1000000000,1000000000}; > 119 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 120 int y_[] = {-1000000000,1000000000}; > 121 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 122 int A = 1; > 123 int T = 15; > 124 int _ = 1; > 125 END > 126 /* > 127 CASE(5) > 128 int x_[] = ; > 129 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 130 int y_[] = ; > 131 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 132 int A = ; > 133 int T = ; > 134 int _ = ; > 135 END > 136 CASE(6) > 137 int x_[] = ; > 138 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 139 int y_[] = ; > 140 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 141 int A = ; > 142 int T = ; > 143 int _ = ; > 144 END > 145 */ > 146 } > 147 // END CUT HERE
Added SRM/494-U/1A.cpp version [7486de33876cf546]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class Painting { public: > 23 int largestBrush(vector <string> picture) > 24 { > 25 const int Y = picture.size(); > 26 const int X = picture[0].size(); > 27 > 28 int best = 1; > 29 for(int S=2; S<=min(Y,X); ++S) > 30 { > 31 vector<string> duty = picture; > 32 for(int y=0; y+S<=Y; ++y) > 33 for(int x=0; x+S<=X; ++x) > 34 if( all<'B'>(picture, y, x, y+S, x+S) ) // SxS ‚ª‘S > 35 fill<'W'>(duty, y, x, y+S, x+S); // “h‚é > 36 if( all<'W'>(duty, 0, 0, Y, X) ) > 37 best = max(best, S); // ‘S•”“h‚ê‚Ä‚½‚çƒxƒXƒgƒXƒRƒAXV > 38 } > 39 return best; > 40 } > 41 > 42 template<char color> > 43 bool all(const vector<string>& p, int ys, int xs, int ye, int xe) > 44 { > 45 int cnt = 0; > 46 for(int y=ys; y<ye; ++y) > 47 for(int x=xs; x<xe; ++x) > 48 cnt += (p[y][x]==color); > 49 return cnt == (ye-ys)*(xe-xs); > 50 } > 51 > 52 template<char color> > 53 void fill(vector<string>& p, int ys, int xs, int ye, int xe) > 54 { > 55 for(int y=ys; y<ye; ++y) > 56 for(int x=xs; x<xe; ++x) > 57 p[y][x] = color; > 58 } > 59 }; > 60 > 61 // BEGIN CUT HERE > 62 #include <ctime> > 63 double start_time; string timer() > 64 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 65 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 66 { os << "{ "; > 67 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 68 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 69 void verify_case(const int& Expected, const int& Received) { > 70 bool ok = (Expected == Received); > 71 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 72 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 73 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 74 #define END verify_case(_, Painting().largestBrush(picture));} > 75 int main(){ > 76 > 77 CASE(0) > 78 string picture_[] = {"BBBB", > 79 "BBBB", > 80 "BBBB", > 81 "BBBB"}; > 82 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 83 int _ = 4; > 84 END > 85 CASE(1) > 86 string picture_[] = {"BBBB", > 87 "BWWB", > 88 "BWWB", > 89 "BBBB"}; > 90 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 91 int _ = 1; > 92 END > 93 CASE(2) > 94 string picture_[] = { > 95 "WBBBBB", > 96 "BBBBBB", > 97 "BBBBBB", > 98 "BBBBBB"} > 99 ; > 100 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 101 int _ = 3; > 102 END > 103 CASE(3) > 104 string picture_[] = {"BBBB", > 105 "BBBB", > 106 "WBBB", > 107 "BBBB", > 108 "BBBB", > 109 "BBBB"} > 110 ; > 111 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 112 int _ = 2; > 113 END > 114 CASE(4) > 115 string picture_[] = { > 116 "WBBBBBWWWWWWWWW", > 117 "WBBBBBBWWWWWWWW", > 118 "WBBBBBBBBBBBWWW", > 119 "WBBBBBBBBBBBWWW", > 120 "BBBBBBBBBBBBBBB", > 121 "BBBBBBBBBBBBBBB", > 122 "BBBBBBBBBBBBBBB", > 123 "BBBBBBBBWWBBBBB", > 124 "BBBBBBBBWBBBBBB", > 125 "WBBBBBBBWBBBBBW", > 126 "BBBBBBBWWBBBBBW", > 127 "BBBBBBBWWBBBBBW", > 128 "BBBBBBWWWBBBBBW", > 129 "BBBBBWWWWWWWWWW", > 130 "BBBBBWWWWWWWWWW"} > 131 ; > 132 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 133 int _ = 5; > 134 END > 135 CASE(5) > 136 string picture_[] = {"B"}; > 137 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 138 int _ = 1; > 139 END > 140 CASE(6) > 141 string picture_[] = {"WWWWWWBWWWWW"}; > 142 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 143 int _ = 1; > 144 END > 145 CASE(7) > 146 string picture_[] = { > 147 "WWBBWW", > 148 "WBBBBW", > 149 "BBWWBB", > 150 "BBWWBB", > 151 "WBBBBW", > 152 "WWBBWW", > 153 }; > 154 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 155 int _ = 1; > 156 END > 157 CASE(8) > 158 string picture_[] = { > 159 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 160 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 161 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 162 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 163 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 164 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 165 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 166 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 167 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 168 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 169 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 170 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 171 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 172 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 173 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 174 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 175 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 176 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 177 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 178 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 179 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 180 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 181 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 182 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 183 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 184 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 185 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 186 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 187 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 188 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 189 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 190 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 191 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 192 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 193 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 194 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 195 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 196 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 197 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 198 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 199 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 200 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 201 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 202 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 203 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 204 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 205 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 206 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 207 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 208 "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB", > 209 }; > 210 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 211 int _ = 1; > 212 END > 213 > 214 } > 215 // END CUT HERE
Added SRM/494-U/1B.cpp version [7ca249caa65bea30]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class AlternatingLane { public: > 23 double expectedBeauty(vector <int> low, vector <int> high) > 24 { > 25 double eSum = 0.0; > 26 for(int i=1; i<low.size(); ++i) > 27 { > 28 // ƒ‰ƒ“ƒ_ƒ€‚É > 29 // h ¸ [low[i-1], high[i-1]] > 30 // g ¸ [low[i], high[i] ] > 31 // ‚ðŽæ‚Á‚Ä‚«‚½‚Æ‚«‚ÌA|h-g| ‚ÌŠú‘Ò’lc > 32 double e = 0; > 33 for(int h=low[i-1]; h<=high[i-1]; ++h) > 34 if( h <= low[i] ) > 35 e += sequenceSum(low[i]-h, high[i]-h); > 36 else if( high[i] <= h ) > 37 e += sequenceSum(h-high[i], h-low[i]); > 38 else > 39 e += sequenceSum(0, h-low[i]) + sequence > 40 eSum += e / (high[i]-low[i]+1) / (high[i-1]-low[i-1]+1); > 41 } > 42 return eSum; // c‚̘aA‚ª“š‚¦ > 43 } > 44 > 45 LL sequenceSum(LL a, LL b) // a+(a+1)+...+b > 46 { > 47 return (a+b) * (b-a+1) / 2; > 48 } > 49 }; > 50 > 51 // BEGIN CUT HERE > 52 #include <ctime> > 53 double start_time; string timer() > 54 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 55 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 56 { os << "{ "; > 57 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 58 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 59 void verify_case(const double& Expected, const double& Received) { > 60 bool ok = (abs(Expected - Received) < 1e-9); > 61 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 62 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 63 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 64 #define END verify_case(_, AlternatingLane().expectedBeauty(low, high));} > 65 int main(){ > 66 > 67 CASE(0) > 68 int low_[] = {1}; > 69 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 70 int high_[] = {100}; > 71 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 72 double _ = 0.0; > 73 END > 74 CASE(1) > 75 int low_[] = {1, 1, 1}; > 76 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 77 int high_[] = {2, 2, 2}; > 78 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 79 double _ = 1.0; > 80 END > 81 CASE(2) > 82 int low_[] = {1, 3, 5, 7, 9}; > 83 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 84 int high_[] = {2, 4, 6, 8, 10}; > 85 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 86 double _ = 8.0; > 87 END > 88 CASE(3) > 89 int low_[] = {4, 3, 3, 7}; > 90 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 91 int high_[] = {10, 7, 7, 7}; > 92 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 93 double _ = 6.171428571428572; > 94 END > 95 CASE(4) > 96 int low_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 97 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 98 int high_[] = {100000,100000,100000,100000,100000,100000,100000,100000 > 99 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 100 double _ = -1; > 101 END > 102 /* > 103 CASE(5) > 104 int low_[] = ; > 105 vector <int> low(low_, low_+sizeof(low_)/sizeof(*low_)); > 106 int high_[] = ; > 107 vector <int> high(high_, high_+sizeof(high_)/sizeof(*high_)); > 108 double _ = ; > 109 END > 110 */ > 111 } > 112 // END CUT HERE
Added SRM/495-U/1A.cpp version [87e839b8017e092a]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ColorfulCards { public: > 23 vector <int> theCards(int N, string colors) > 24 { > 25 vector<bool> isPrime(N+1, true); > 26 isPrime[1] = false; > 27 for(int p=2; p<=N; ++p) > 28 if( isPrime[p] ) > 29 for(int q=p+p; q<=N; q+=p) > 30 isPrime[q] = false; > 31 > 32 vector<int> result(colors.size(), 0); > 33 for(int i=0; i<colors.size(); ++i) > 34 for(int n=1; n<=N; ++n) > 35 if( canItBe(i, n, colors, isPrime) ) > 36 if( result[i] > 0 ) > 37 {result[i] = -1; break;} > 38 else > 39 result[i] = n; > 40 return result; > 41 } > 42 > 43 bool canItBe(int i, int n, const string& colors, vector<bool>& isPrime) > 44 { > 45 if( colors[i]=='R' && !isPrime[n] ) > 46 return false; > 47 if( colors[i]=='B' && isPrime[n] ) > 48 return false; > 49 return greedyDown(i-1, n-1, colors, isPrime) && greedyUp(i+1, n+ > 50 } > 51 > 52 bool greedyUp(int i, int n, const string& colors, vector<bool>& isPrime) > 53 { > 54 while( i<colors.size() ) > 55 { > 56 if( n >= isPrime.size() ) > 57 return false; > 58 if( colors[i]=='R' && isPrime[n] ) > 59 {++i; ++n;} > 60 else > 61 if( colors[i]=='B' && !isPrime[n] ) > 62 {++i; ++n;} > 63 else > 64 ++n; > 65 } > 66 return true; > 67 } > 68 > 69 bool greedyDown(int i, int n, const string& colors, vector<bool>& isPrim > 70 { > 71 while( i>=0 ) > 72 { > 73 if( n < 1 ) > 74 return false; > 75 if( colors[i]=='R' && isPrime[n] ) > 76 {--i; --n;} > 77 else > 78 if( colors[i]=='B' && !isPrime[n] ) > 79 {--i; --n;} > 80 else > 81 --n; > 82 } > 83 return true; > 84 } > 85 }; > 86 > 87 // BEGIN CUT HERE > 88 #include <ctime> > 89 double start_time; string timer() > 90 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 91 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 92 { os << "{ "; > 93 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 94 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 95 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 96 bool ok = (Expected == Received); > 97 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 98 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 99 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 100 #define END verify_case(_, ColorfulCards().theCards(N, colors));} > 101 int main(){ > 102 > 103 CASE(0) > 104 int N = 5; > 105 string colors = "RRR"; > 106 int __[] = {2, 3, 5 }; > 107 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 108 END > 109 CASE(1) > 110 int N = 7; > 111 string colors = "BBB"; > 112 int __[] = {1, 4, 6 }; > 113 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 114 END > 115 CASE(2) > 116 int N = 6; > 117 string colors = "RBR"; > 118 int __[] = {-1, 4, 5 }; > 119 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 120 END > 121 CASE(3) > 122 int N = 58; > 123 string colors = "RBRRBRBBRBRRBBRRBBBRRBBBRR"; > 124 int __[] = {-1, -1, -1, -1, -1, -1, -1, -1, 17, 18, 19, 23, -1, -1, -1, > 125 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 126 END > 127 CASE(4) > 128 int N = 495; > 129 string colors = "RBRRBRBBRBRRBBRRBBBRRBBBRR"; > 130 int __[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, > 131 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 132 END > 133 CASE(5) > 134 int N = 1000; > 135 string colors = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; > 136 int __[] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, > 137 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 138 END > 139 CASE(6) > 140 int N = 1; > 141 string colors = "B"; > 142 int __[] = {1}; > 143 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 144 END > 145 CASE(6) > 146 int N = 2; > 147 string colors = "R"; > 148 int __[] = {2}; > 149 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 150 END > 151 CASE(6) > 152 int N = 2; > 153 string colors = "B"; > 154 int __[] = {1}; > 155 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 156 END > 157 } > 158 // END CUT HERE
Added SRM/495-U/1B.cpp version [3f7b3fa46030db3f]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CarrotBoxes { public: > 23 double theProbability(vector <string> info) > 24 { > 25 info = warshall_floyd(info); > 26 > 27 double answer=0.0, p=1.0; > 28 > 29 set<int> boxes; > 30 for(int v=0; v<info.size(); ++v) > 31 boxes.insert(v); > 32 > 33 while( !boxes.empty() ) > 34 { > 35 for(set<int>::iterator it=boxes.begin(); it!=boxes.end() > 36 if( isRoot(*it, info, boxes) && !isSingleton(*it > 37 { > 38 int N = boxes.size(); > 39 int nr = 0; > 40 for(int u=0; u<info.size(); ++u) > 41 if( info[*it][u]=='Y' && boxes.c > 42 {++nr; boxes.erase(u);} > 43 answer += p * (nr-1) / N; > 44 p *= double(N-nr) / N; > 45 goto nextIter; > 46 } > 47 > 48 // ‘S•”‚ª stand alone > 49 answer += p / boxes.size(); > 50 break; > 51 > 52 nextIter:; > 53 } > 54 return answer; > 55 } > 56 > 57 bool isRoot(int v, const vector<string>& info, const set<int>& boxes) > 58 { > 59 for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); > 60 if( info[v][*jt]=='N' && info[*jt][v]=='Y' ) > 61 return false; > 62 return true; > 63 } > 64 > 65 bool isSingleton(int v, const vector<string>& info, const set<int>& boxe > 66 { > 67 for(set<int>::const_iterator jt=boxes.begin(); jt!=boxes.end(); > 68 if( v!=*jt && info[v][*jt]=='Y' ) > 69 return false; > 70 return true; > 71 } > 72 > 73 vector<string> warshall_floyd(const vector<string>& gg) > 74 { > 75 vector<string> g = gg; > 76 for(int k=0; k<g.size(); ++k) > 77 for(int i=0; i<g.size(); ++i) > 78 for(int j=0; j<g.size(); ++j) > 79 if( g[i][k]=='Y' && g[k][j]=='Y' ) > 80 g[i][j] = 'Y'; > 81 return g; > 82 } > 83 }; > 84 > 85 // BEGIN CUT HERE > 86 #include <ctime> > 87 double start_time; string timer() > 88 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 89 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 90 { os << "{ "; > 91 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 92 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 93 void verify_case(const double& Expected, const double& Received) { > 94 bool ok = (abs(Expected - Received) < 1e-9); > 95 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 96 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 97 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 98 #define END verify_case(_, CarrotBoxes().theProbability(information));} > 99 int main(){ > 100 > 101 CASE(0) > 102 string information_[] = {"YYYYY", > 103 "NYNNN", > 104 "NNYNN", > 105 "NNNYN", > 106 "NNNNY"} > 107 ; > 108 vector <string> information(information_, information_+sizeof(informat > 109 double _ = 0.8; > 110 END > 111 CASE(1) > 112 string information_[] = {"YNNNN", > 113 "NYNNN", > 114 "NNYNN", > 115 "NNNYN", > 116 "NNNNY"}; > 117 vector <string> information(information_, information_+sizeof(informat > 118 double _ = 0.2; > 119 END > 120 CASE(2) > 121 string information_[] = {"Y"}; > 122 vector <string> information(information_, information_+sizeof(informat > 123 double _ = 1.0; > 124 END > 125 CASE(3) > 126 string information_[] = {"YNNNN", > 127 "YYNNN", > 128 "YNYNN", > 129 "NNNYY", > 130 "NNNYY"} > 131 ; > 132 vector <string> information(information_, information_+sizeof(informat > 133 double _ = 0.6; > 134 END > 135 CASE(4) > 136 string information_[] = {"YYYNNNYN", > 137 "NYNNNNYN", > 138 "NNYNNNNN", > 139 "NYNYNNNN", > 140 "YNNNYNNY", > 141 "NNYNNYNN", > 142 "NNNNYNYN", > 143 "NNYNNNNY"} > 144 ; > 145 vector <string> information(information_, information_+sizeof(informat > 146 double _ = 0.875; > 147 END > 148 CASE(5) > 149 string information_[] = {"YNNNNNNNNYNNNNNNNNNN", > 150 "NYNNNNNNNNNNNNNNNNNN", > 151 "NNYNNNNNNNYNNNNNYNNN", > 152 "NNNYNYNNNNNNNNYNNNNN", > 153 "NNNNYNNNNNNNNNYNNNNY", > 154 "NNNNNYNNNNNNNNNNNNNY", > 155 "NNNNYNYNYNNNNNNNNNNN", > 156 "NNNNNNNYNNNYYNNNNNNN", > 157 "NNNNNNNNYNNNNNNNNNNN", > 158 "YNNNNNNNNYNNNNNYNNNN", > 159 "NNNNNNNNNNYNNNNNNNNN", > 160 "NYNNNNNNNNNYNNNNNNNN", > 161 "NNNNNNNYNNNNYNNNNNNN", > 162 "NNNNNNNNNNNNNYNNNYNN", > 163 "NNNNNNNNNNNYNNYNNNYN", > 164 "NYNNNNNNNNNNNNNYNNNN", > 165 "NNYNNNNNNNNNNNNNYNNN", > 166 "NNNNNNNNNNNNNYNYNYNN", > 167 "NNNNNNNNYNYNNNNNNNYY", > 168 "NNNYNNNNNNNNNNNNNNNY"}; > 169 vector <string> information(information_, information_+sizeof(informat > 170 double _ = 0.75; > 171 END > 172 /* > 173 CASE(6) > 174 string information_[] = ; > 175 vector <string> information(information_, information_+sizeof(informat > 176 double _ = ; > 177 END > 178 CASE(7) > 179 string information_[] = ; > 180 vector <string> information(information_, information_+sizeof(informat > 181 double _ = ; > 182 END > 183 */ > 184 } > 185 // END CUT HERE
Added SRM/495-U/2C.cpp version [6fa6be636b9ebdef]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 struct UnionFind > 23 { > 24 vector<int> uf, sz; > 25 int nc; > 26 > 27 UnionFind(int N): uf(N), sz(N,1), nc(N) > 28 { for(int i=0; i<N; ++i) uf[i] = i; } > 29 int size() > 30 { return nc; } > 31 int Find(int a) > 32 { return uf[a]==a ? a : uf[a]=Find(uf[a]); } > 33 bool Union(int a, int b) > 34 { > 35 a = Find(a); > 36 b = Find(b); > 37 if( a != b ) > 38 { > 39 if( sz[a] >= sz[b] ) swap(a, b); > 40 uf[a] = b; > 41 sz[b] += sz[a]; > 42 --nc; > 43 } > 44 return (a!=b); > 45 } > 46 }; > 47 > 48 class HexagonPuzzle { public: > 49 int theCount(vector <string> board) > 50 { > 51 vector< pair<int,int> > id2pos; > 52 map<pair<int,int>, int> pos2id; > 53 int N = board.size(); > 54 for(int i=0; i<N; ++i) > 55 for(int k=0; k<=i; ++k) { > 56 id2pos.push_back( make_pair(i,k) ); > 57 pos2id[make_pair(i,k)] = id2pos.size()-1; > 58 } > 59 > 60 int M = id2pos.size(); > 61 UnionFind uf(M); > 62 > 63 for(int i=0; i<N; ++i) > 64 for(int k=0; k<=i; ++k) > 65 if( board[i][k] == '.' ) > 66 { > 67 if( i+1<N && board[i+1][k]=='.' && board > 68 { > 69 int a = pos2id[ make_pair(i,k) ] > 70 int b = pos2id[ make_pair(i+1,k) > 71 int c = pos2id[ make_pair(i+1,k+ > 72 uf.Union(a,b); > 73 uf.Union(b,c); > 74 } > 75 if( i+1<N && k+1<=i && board[i][k+1]=='. > 76 { > 77 int a = pos2id[ make_pair(i,k) ] > 78 int b = pos2id[ make_pair(i,k+1) > 79 int c = pos2id[ make_pair(i+1,k+ > 80 uf.Union(a,b); > 81 uf.Union(b,c); > 82 } > 83 } > 84 > 85 map<int, int> sc; > 86 for(int i=0; i<N; ++i) > 87 for(int k=0; k<=i; ++k) > 88 if( board[i][k] == '.' ) > 89 { > 90 int a = pos2id[ make_pair(i,k) ]; > 91 int ra = uf.Find(a); > 92 sc[ra] = uf.sz[ra]; > 93 } > 94 > 95 static const int MODVAL = 1000000007; > 96 LL result = 1; > 97 for(map<int,int>::iterator it=sc.begin(); it!=sc.end(); ++it) > 98 { > 99 int n = it->second; > 100 LL r = 1; > 101 for(int i=3; i<=n; ++i) > 102 r = (r*i)%MODVAL; > 103 result = (result * r) % MODVAL; > 104 } > 105 return int(result); > 106 } > 107 }; > 108 > 109 // BEGIN CUT HERE > 110 #include <ctime> > 111 double start_time; string timer() > 112 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 113 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 114 { os << "{ "; > 115 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 116 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 117 void verify_case(const int& Expected, const int& Received) { > 118 bool ok = (Expected == Received); > 119 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 120 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 121 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 122 #define END verify_case(_, HexagonPuzzle().theCount(board));} > 123 int main(){ > 124 > 125 CASE(0) > 126 string board_[] = {".", > 127 ".X", > 128 "X..", > 129 ".X.X"} > 130 ; > 131 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 132 int _ = 3; > 133 END > 134 CASE(1) > 135 string board_[] = {"X"} > 136 ; > 137 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 138 int _ = 1; > 139 END > 140 CASE(2) > 141 string board_[] = {".", > 142 "..", > 143 "...", > 144 ".X.."} > 145 ; > 146 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 147 int _ = 20160; > 148 END > 149 CASE(3) > 150 string board_[] = {".", > 151 "..", > 152 "XXX", > 153 "..X.", > 154 ".X..X", > 155 "XXXX..", > 156 "..X.X.X", > 157 "..X.XX.."} > 158 ; > 159 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 160 int _ = 108; > 161 END > 162 CASE(4) > 163 string board_[] = {".", > 164 "..", > 165 "...", > 166 "....", > 167 ".....", > 168 "......", > 169 ".......", > 170 "........"}; > 171 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 172 int _ = 261547992; > 173 END > 174 /* > 175 CASE(5) > 176 string board_[] = ; > 177 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 178 int _ = ; > 179 END > 180 CASE(6) > 181 string board_[] = ; > 182 vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); > 183 int _ = ; > 184 END > 185 */ > 186 } > 187 // END CUT HERE
Added SRM/496/1A.cpp version [02521b08a944dd23]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class ColoredStrokes { public: > 23 int getLeast(vector <string> picture) > 24 { > 25 const int H = picture.size(); > 26 const int W = picture[0].size(); > 27 > 28 int cnt = 0; > 29 for(int y=0; y<H; ++y) > 30 { > 31 string line = picture[y]; > 32 for(int i=0; i<line.size(); ++i) > 33 if( line[i]=='G' ) line[i] = 'R'; > 34 else if(line[i]=='B') line[i]='.'; > 35 cnt += howMany('R', line); > 36 } > 37 for(int x=0; x<W; ++x) > 38 { > 39 string line; > 40 for(int y=0; y<H; ++y) > 41 line += picture[y][x]; > 42 for(int i=0; i<line.size(); ++i) > 43 if( line[i]=='G' ) line[i] = 'B'; > 44 else if(line[i]=='R') line[i]='.'; > 45 cnt += howMany('B', line); > 46 } > 47 return cnt; > 48 } > 49 > 50 int howMany(char c, const string& s) > 51 { > 52 int cnt = 0; > 53 for(int i=0; i<s.size(); ) > 54 { > 55 while(i<s.size() && s[i]!=c) > 56 ++i; > 57 if( i == s.size() ) > 58 break; > 59 ++cnt; > 60 while(i<s.size() && s[i]==c) > 61 ++i; > 62 } > 63 return cnt; > 64 } > 65 }; > 66 > 67 // BEGIN CUT HERE > 68 #include <ctime> > 69 double start_time; string timer() > 70 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 71 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 72 { os << "{ "; > 73 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 74 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 75 void verify_case(const int& Expected, const int& Received) { > 76 bool ok = (Expected == Received); > 77 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 78 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 79 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 80 #define END verify_case(_, ColoredStrokes().getLeast(picture));} > 81 int main(){ > 82 > 83 CASE(0) > 84 string picture_[] = {"...", > 85 "..."}; > 86 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 87 int _ = 0; > 88 END > 89 CASE(1) > 90 string picture_[] = {"..B.", > 91 "..B."}; > 92 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 93 int _ = 1; > 94 END > 95 CASE(2) > 96 string picture_[] = {".BB."}; > 97 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 98 int _ = 2; > 99 END > 100 CASE(3) > 101 string picture_[] = {"...B..", > 102 ".BRGRR", > 103 ".B.B.."}; > 104 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 105 int _ = 3; > 106 END > 107 CASE(4) > 108 string picture_[] = {"...B..", > 109 ".BRBRR", > 110 ".B.B.."}; > 111 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 112 int _ = 4; > 113 END > 114 CASE(5) > 115 string picture_[] = {"GR", > 116 "BG"}; > 117 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 118 int _ = 4; > 119 END > 120 /* > 121 CASE(6) > 122 string picture_[] = ; > 123 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 124 int _ = ; > 125 END > 126 CASE(7) > 127 string picture_[] = ; > 128 vector <string> picture(picture_, picture_+sizeof(picture_)/sizeof(*pi > 129 int _ = ; > 130 END > 131 */ > 132 } > 133 // END CUT HERE
Added SRM/496/1B.cpp version [85211b8e1f2074c0]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class OneDimensionalBalls { public: > 23 long long countValidGuesses(vector <int> firstPicture, vector <int> seco > 24 { > 25 LL answer = 0; > 26 > 27 const int N = firstPicture.size(); > 28 set<int> sp(secondPicture.begin(), secondPicture.end()); > 29 > 30 for(set<int>::iterator it=sp.begin(); it!=sp.end(); ++it) > 31 { > 32 if( firstPicture[0] != *it ) > 33 { > 34 const int d = abs(firstPicture[0] - *it); > 35 > 36 set<int> avail = sp; > 37 avail.erase(*it); > 38 > 39 vector<bool> done(N, false); > 40 done[0] = true; > 41 > 42 for(;;) > 43 { > 44 bool upd = false; > 45 for(int k=0; k<N; ++k) > 46 if( !done[k] ) > 47 { > 48 bool aL = avail.count(fi > 49 bool aR = avail.count(fi > 50 if( !aL && !aR ) > 51 goto nextIt; > 52 else if( aL && !aR ) > 53 {upd=done[k]=tru > 54 else if( !aL && aR ) > 55 {upd=done[k]=tru > 56 } > 57 if( !upd ) > 58 break; > 59 } > 60 > 61 set<int> notDone; > 62 for(int k=0; k<N; ++k) > 63 if( !done[k] ) > 64 notDone.insert(firstPicture[k]); > 65 > 66 LL cnt = 1; > 67 > 68 set<int> nnd; > 69 for(set<int>::iterator jt=notDone.begin(); jt!=n > 70 if( !nnd.count(*jt) ) > 71 { > 72 int chain = 1; > 73 for(int q=*jt+2*d; notDone.count > 74 {nnd.insert(q); ++chain; > 75 cnt *= chain+1; > 76 } > 77 answer += cnt; > 78 } > 79 nextIt:; > 80 } > 81 > 82 return answer; > 83 } > 84 }; > 85 > 86 // BEGIN CUT HERE > 87 #include <ctime> > 88 double start_time; string timer() > 89 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 90 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 91 { os << "{ "; > 92 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 93 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 94 void verify_case(const long long& Expected, const long long& Received) { > 95 bool ok = (Expected == Received); > 96 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 97 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 98 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 99 #define END verify_case(_, OneDimensionalBalls().countValidGuesses(firstPic > 100 int main(){ > 101 > 102 CASE(0) > 103 int firstPicture_[] = {12,11}; > 104 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 105 int secondPicture_[] = {10,11,13}; > 106 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 107 long long _ = 3LL; > 108 END > 109 CASE(1) > 110 int firstPicture_[] = {1,2,3}; > 111 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 112 int secondPicture_[] = {1,2,3}; > 113 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 114 long long _ = 0LL; > 115 END > 116 CASE(2) > 117 int firstPicture_[] = {1,3}; > 118 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 119 int secondPicture_[] = {1,3}; > 120 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 121 long long _ = 1LL; > 122 END > 123 CASE(3) > 124 int firstPicture_[] = {7234}; > 125 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 126 int secondPicture_[] = {6316,689156,689160,689161,800000,1000001}; > 127 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 128 long long _ = 6LL; > 129 END > 130 CASE(4) > 131 int firstPicture_[] = {6,2,4}; > 132 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 133 int secondPicture_[] = {1,2,3,4,5,7,8}; > 134 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 135 long long _ = 7LL; > 136 END > 137 CASE(5) > 138 int firstPicture_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22, > 139 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 140 int secondPicture_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, > 141 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 142 long long _ = -1LL; > 143 END > 144 CASE(6) > 145 int firstPicture_[] = {1,2,3,4}; > 146 vector <int> firstPicture(firstPicture_, firstPicture_+sizeof(firstPic > 147 int secondPicture_[] = {1,2,3,4}; > 148 vector <int> secondPicture(secondPicture_, secondPicture_+sizeof(secon > 149 long long _ = 2LL; > 150 END > 151 } > 152 // END CUT HERE
Added SRM/496/1C.cpp version [243d0d4a58986591]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class YetAnotherHamiltonianPath { public: > 23 int leastCost(vector <string> label) > 24 { > 25 int cost = 0; > 26 for(int i=0; i<label.size(); ++i) > 27 cost += label[i].size()*label[i].size() * (i<=1 ? 1 : 2) > 28 return cost - radixSort(label); // ‚ ‚Æ‚Í max (ƒ° LCP^2) ‚ðˆø‚«ŽZ > 29 } > 30 > 31 int radixSort(const vector<string>& v, int i=0, bool single=true, bool h > 32 { > 33 // Œ´‘¥“I‚ÉA0•¶Žš–Ú‚ª“¯‚¶‚à‚Ì‚Ç‚¤‚µ‚ðˆêƒJŠ‚É‚Ü‚Æ‚ß‚½•û‚ª“–‘Rƒxƒ^[ > 34 // 0•¶Žš–Ú‚ª“¯‚¶’†‚Å‚Í1•¶Žš–Ú‚ª“¯‚¶‚à‚Ì‚Ç‚¤‚µ‚ðˆêƒJŠ‚Éc‚Æ‚¢‚¤Ä‹A > 35 map< char, vector<string> > classify; > 36 for(int k=0; k<v.size(); ++k) > 37 classify[v[k].c_str()[i]].push_back( v[k] ); > 38 > 39 // N ŒÂ‚̃uƒƒbƒN‚É•ª‚©‚ꂽ‚çA•ª‚©‚ê–Ú‚Å‚ÍLCP‚ª i ‚È‚Ì‚ÅA‚»‚Ì•ª‚Ì ƒ° LCP^2 ‚Í i*i*(N-1)B > 40 int score = i*i * (classify.size()-1); > 41 > 42 // ‚ ‚Ƃ̓uƒƒbƒN‚²‚Æ‚ÉÄ‹A“I‚É LCP^2 ‚̘a‚ð‹‚ß‚Ä‘«‚µ‚Ä‚¢‚ > 43 for(map<char, vector<string> >::iterator it=classify.begin(); it > 44 { > 45 bool sgl = single && classify.size()==1; > 46 bool hzo = hasBothZeroOne && it->first==v[0].c_str()[i] > 47 if( it->first ) > 48 score += radixSort( it->second, i+1, sgl, hzo ); > 49 else > 50 score += i*i * (it->second.size()-1 + (sgl?1:0) > 51 } > 52 > 53 // ‚½‚¾‚µAlabel[0] ‚Æ label[1] ‚ª¶‚«•Ê‚ê‚ɂȂ镪‚©‚ê–Ú‚Íiʼn‚©‚番‚©‚ê‚Ä‚é‚Ì‚Åj”‚¦‚È‚¢ > 54 if( hasBothZeroOne && v[0].c_str()[i]!=v[1].c_str()[i] ) score - > 55 > 56 // ‹t‚ÉA‘S‘Ì‚ª2‚ˆÈã‚̃uƒƒbƒN‚É•ª‚©‚ê‚é‚Æ‚«‚É‚Í label[0] ‚Æ label[1] ‚ÌŠÔ‚É‹²‚Ü‚é‚Ì‚Å ƒ°LCP^2 ‚ª‚»‚Ì•ª‘½‚¢ > 57 if( single && classify.size()>1 ) score += i*i; > 58 > 59 // ˆÈã > 60 return score; > 61 } > 62 }; > 63 > 64 // BEGIN CUT HERE > 65 #include <ctime> > 66 double start_time; string timer() > 67 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 68 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 69 { os << "{ "; > 70 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 71 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 72 void verify_case(const int& Expected, const int& Received) { > 73 bool ok = (Expected == Received); > 74 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 75 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 76 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 77 #define END verify_case(_, YetAnotherHamiltonianPath().leastCost(label));} > 78 int main(){ > 79 > 80 CASE(0) > 81 string label_[] = {"home", "school", "pub"} ; > 82 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 83 int _ = 70; > 84 END > 85 CASE(1) > 86 string label_[] = {"school", "home", "pub", "stadium"}; > 87 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 88 int _ = 167; > 89 END > 90 CASE(2) > 91 string label_[] = {"abcd","aecgh","abef","aecd"}; > 92 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 93 int _ = 91; > 94 END > 95 CASE(3) > 96 string label_[] = {"canada", "cyprus", "croatia", "colombia", "chile", " > 97 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 98 int _ = 509; > 99 END > 100 CASE(4) > 101 string label_[] = {"aaa", "aab", "b"}; > 102 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 103 int _ = 9+1 + 1+9; > 104 END > 105 CASE(4) > 106 string label_[] = {"caaa", "caab", "cb"}; > 107 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 108 int _ = 16+4 + 4+16 - 1 - 1; > 109 END > 110 /* > 111 CASE(5) > 112 string label_[] = ; > 113 vector <string> label(label_, label_+sizeof(label_)/sizeof(*label_)); > 114 int _ = ; > 115 END > 116 */ > 117 } > 118 // END CUT HERE
Added SRM/496/2C.cpp version [b8edf4ca77074ee3]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PalindromfulString { public: > 23 long long count(int N, int M, int K) > 24 { > 25 vector<int> tmp; > 26 return rec(N, M, K, tmp, 0); > 27 } > 28 > 29 static LL rec(int N, int M, int K, vector<int>& cur, int usedMax) > 30 { > 31 if( cur.size() == N ) > 32 { > 33 int pc=0; > 34 for(int s=0; s+M<=N; ++s) > 35 if( isPalin(cur, s, s+M) ) > 36 pc++; > 37 return (pc>=K ? P(26, usedMax) : 0); > 38 } > 39 else > 40 { > 41 LL sum = 0; > 42 for(int k=1; k<=usedMax+1; ++k) > 43 { > 44 cur.push_back(k); > 45 sum += rec(N, M, K, cur, max(usedMax,k)); > 46 cur.pop_back(); > 47 } > 48 return sum; > 49 } > 50 } > 51 > 52 static bool isPalin(const vector<int>& v, int s, int e) > 53 { > 54 for( ; s<=--e; ++s) > 55 if( v[s] != v[e] ) > 56 return false; > 57 return true; > 58 } > 59 > 60 static LL P(int v, int n) > 61 { > 62 LL x = 1; > 63 for(int i=0; i<n; ++i) > 64 x *= v-i; > 65 return x; > 66 } > 67 }; > 68 > 69 // BEGIN CUT HERE > 70 #include <ctime> > 71 double start_time; string timer() > 72 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 73 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 74 { os << "{ "; > 75 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 76 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 77 void verify_case(const long long& Expected, const long long& Received) { > 78 bool ok = (Expected == Received); > 79 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 80 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 81 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 82 #define END verify_case(_, PalindromfulString().count(N, M, K));} > 83 int main(){ > 84 > 85 CASE(0) > 86 int N = 2; > 87 int M = 2; > 88 int K = 1; > 89 long long _ = 26LL; > 90 END > 91 CASE(1) > 92 int N = 2; > 93 int M = 2; > 94 int K = 0; > 95 long long _ = 676LL; > 96 END > 97 CASE(2) > 98 int N = 3; > 99 int M = 2; > 100 int K = 1; > 101 long long _ = 1326LL; > 102 END > 103 CASE(3) > 104 int N = 4; > 105 int M = 4; > 106 int K = 1; > 107 long long _ = 676LL; > 108 END > 109 CASE(4) > 110 int N = 7; > 111 int M = 3; > 112 int K = 3; > 113 long long _ = 4310176LL; > 114 END > 115 CASE(5) > 116 int N = 11; > 117 int M = 2; > 118 int K = 0; > 119 long long _ = -1LL; > 120 END > 121 CASE(6) > 122 int N = 11; > 123 int M = 5; > 124 int K = 0; > 125 long long _ = -1LL; > 126 END > 127 > 128 } > 129 // END CUT HERE
Added SRM/497-U/1A.cpp version [6442f8ba618ca353]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class PermutationSignature { public: > 23 vector <int> reconstruct(string signature) > 24 { > 25 deque<int> cur(1, 1); > 26 for(int i=signature.size()-1; i>=0; --i) > 27 { > 28 int ins = (signature[i]=='I' ? 1 : cur.front()+1); > 29 cur.push_front(ins); > 30 for(int k=1; k<cur.size(); ++k) > 31 if( cur[k] >= ins ) > 32 cur[k] ++; > 33 } > 34 return vector<int>(cur.begin(), cur.end()); > 35 } > 36 }; > 37 > 38 // BEGIN CUT HERE > 39 #include <ctime> > 40 double start_time; string timer() > 41 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 42 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 43 { os << "{ "; > 44 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 45 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 46 void verify_case(const vector <int>& Expected, const vector <int>& Received) { > 47 bool ok = (Expected == Received); > 48 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 49 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } > 50 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 51 #define END verify_case(_, PermutationSignature().reconstruct(signature));} > 52 int main(){ > 53 > 54 CASE(0) > 55 string signature = "IIIII"; > 56 int __[] = {1, 2, 3, 4, 5, 6 }; > 57 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 58 END > 59 CASE(1) > 60 string signature = "DI"; > 61 int __[] = {2, 1, 3 }; > 62 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 63 END > 64 CASE(2) > 65 string signature = "IIIID"; > 66 int __[] = {1, 2, 3, 4, 6, 5 }; > 67 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 68 END > 69 CASE(3) > 70 string signature = "DIIDID"; > 71 int __[] = {2, 1, 3, 5, 4, 7, 6 }; > 72 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 73 END > 74 CASE(4) > 75 string signature = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"; > 76 int __[] = {-1}; > 77 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 78 END > 79 CASE(5) > 80 string signature = "IDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDIDID"; > 81 int __[] = {-1}; > 82 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 83 END > 84 CASE(6) > 85 string signature = "I"; > 86 int __[] = {1,2}; > 87 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 88 END > 89 CASE(7) > 90 string signature = "D"; > 91 int __[] = {2,1}; > 92 vector <int> _(__, __+sizeof(__)/sizeof(*__)); > 93 END > 94 > 95 } > 96 // END CUT HERE
Added SRM/497-U/1B.cpp version [1dd2adf40bd7c456]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class CssRules { public: > 23 int getMinimalCssRuleCount(vector <string> xthml) > 24 { > 25 tree virtual_root; > 26 { > 27 num_node = 0; > 28 string xs = accumulate(xthml.begin(), xthml.end(), strin > 29 const char* p = xs.c_str(); > 30 virtual_root.children = parse_tags(p); > 31 } > 32 vector<int> memo(num_node*8*8*8, -1); > 33 return rec(virtual_root.children, 7*64+7*8+7, memo); > 34 } > 35 > 36 struct tree > 37 { > 38 int id; > 39 int tag; > 40 int color; > 41 vector<tree*> children; > 42 ~tree() { for(int i=0; i<children.size(); ++i) delete children[i > 43 }; > 44 > 45 // solver ------------------------------------------------------------- > 46 > 47 int rec(vector<tree*>& ts, int tag_to_color, vector<int>& memo) > 48 { > 49 int sum = 0; > 50 for(int i=0; i<ts.size(); ++i) > 51 sum += rec(ts[i], tag_to_color, memo); > 52 return sum; > 53 } > 54 > 55 int rec(tree* t, int tag_to_color, vector<int>& memo) > 56 { > 57 const int key = t->id * 8*8*8 + tag_to_color; > 58 if( memo[key] >= 0 ) > 59 return memo[key]; > 60 > 61 int me = ((tag_to_color>>(3*t->tag))&7) != t->color; > 62 int best = 0x3fffffff; > 63 for(int ttc=0; ttc<8*8*8; ++ttc) > 64 { > 65 int cost = 0; > 66 cost += ((tag_to_color>>0)&7) != ((ttc>>0)&7); > 67 cost += ((tag_to_color>>3)&7) != ((ttc>>3)&7); > 68 cost += ((tag_to_color>>6)&7) != ((ttc>>6)&7); > 69 best = min(best, cost + srec(t->children, ttc, memo)); > 70 } > 71 return memo[key] = me + best; > 72 } > 73 > 74 // parser ------------------------------------------------------ > 75 > 76 int num_node; > 77 > 78 vector<tree*> parse_tags( const char*& p ) > 79 { > 80 vector<tree*> res; > 81 while(*p && p[1]!='/') > 82 res.push_back(parse_tag(p)); > 83 return res; > 84 } > 85 > 86 tree* parse_tag( const char*& p ) > 87 { > 88 static map<string, int> color_map; > 89 if( color_map.empty() ) > 90 { > 91 color_map["black"] = 0; > 92 color_map["blue"] = 1; > 93 color_map["gray"] = 2; > 94 color_map["green"] = 3; > 95 color_map["red"] = 4; > 96 color_map["white"] = 5; > 97 color_map["yellow"] = 6; > 98 } > 99 static map<string, int> tag_map; > 100 if( tag_map.empty() ) > 101 { > 102 tag_map["b"] = 0; > 103 tag_map["u"] = 1; > 104 tag_map["i"] = 2; > 105 } > 106 > 107 // <TAG id='ID' style='color:COLOR'>tagContent</TAG> > 108 p++; // < > 109 string tag; while(*p!=' ') tag+=*p++; // TAG > 110 while(*p==' ') ++p; // _sp_ > 111 p += 4; // id=' > 112 string id; while(*p!='\'') id+=*p++; // ID > 113 ++p; // ' > 114 while(*p==' ') ++p; // _sp_ > 115 p += 13; // style='color: > 116 string cl; while(*p!='\'') cl+=*p++; // COLOR > 117 p += 2; // '> > 118 vector<tree*> ch = parse_tags(p); // tagContent > 119 p += 4; // </TAG> > 120 > 121 tree* t = new tree; > 122 t->id = num_node++; > 123 t->tag = tag_map[tag]; > 124 t->color = color_map[cl]; > 125 t->children = ch; > 126 return t; > 127 } > 128 }; > 129 > 130 // BEGIN CUT HERE > 131 #include <ctime> > 132 double start_time; string timer() > 133 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 134 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 135 { os << "{ "; > 136 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 137 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 138 void verify_case(const int& Expected, const int& Received) { > 139 bool ok = (Expected == Received); > 140 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 141 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 142 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 143 #define END verify_case(_, CssRules().getMinimalCssRuleCount(xthml));} > 144 int main(){ > 145 > 146 CASE(6) > 147 string xthml_[] = {"<i id='b' style='color:white'><u id='f' styl", "e='c > 148 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 149 int _ = 5; > 150 END > 151 CASE(0) > 152 string xthml_[] = {"<b id='x' style='color:red'></b>"}; > 153 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 154 int _ = 1; > 155 END > 156 CASE(1) > 157 string xthml_[] = {"<b id='x' style='color:red'>","<b id='y' style='colo > 158 "<b id='z' style='color:red'>","</b></b></b>"} > 159 ; > 160 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 161 int _ = 2; > 162 END > 163 CASE(2) > 164 string xthml_[] = {"<b id='x' style='color:red'>", > 165 "<b id='y' style='color:red'>", > 166 "<b id='w' style='color:red'>", > 167 "</b>", > 168 "</b>", > 169 "<u id='z' style='color:red'>", > 170 "</u>", > 171 "</b>"}; > 172 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 173 int _ = 3; > 174 END > 175 CASE(3) > 176 string xthml_[] = {"<b id='x' style='color:red'>", > 177 "<i id='y' style='color:black'>", > 178 "<u id='w' style='color:white'>", > 179 "</u>", > 180 "</i>", > 181 "<u id='z' style='color:yellow'>", > 182 "</u>", > 183 "</b>"}; > 184 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 185 int _ = 4; > 186 END > 187 CASE(4) > 188 string xthml_[] = {"<b id='x' style='col", "or:red'></b>", "<b id=", "'x > 189 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 190 int _ = 2; > 191 END > 192 CASE(5) > 193 string xthml_[] = { > 194 "<b id='aa' style='color:red'><b id='ab' style='col", > 195 "or:red'><b id='ac' style='color:red'><b id='ad' st", > 196 "yle='color:red'><b id='ae' style='color:red'><b id", > 197 "='af' style='color:red'><b id='ag' style='color:re", > 198 "d'><b id='ah' style='color:red'><b id='ai' style='", > 199 "color:red'><b id='aj' style='color:red'><b id='ak'", > 200 " style='color:red'><b id='al' style='color:red'><b", > 201 " id='am' style='color:red'><b id='an' style='color", > 202 ":red'><b id='ao' style='color:red'><b id='ap' styl", > 203 "e='color:red'><b id='aq' style='color:red'><b id='", > 204 "ar' style='color:red'><b id='as' style='color:red'", > 205 "><b id='at' style='color:red'><b id='au' style='co", > 206 "lor:red'><b id='av' style='color:red'><b id='aw' s", > 207 "tyle='color:red'><b id='ax' style='color:red'><b i", > 208 "d='ay' style='color:red'><b id='az' style='color:r", > 209 "ed'><b id='ba' style='color:red'><b id='bb' style=", > 210 "'color:red'><b id='bc' style='color:red'><b id='bd", > 211 "' style='color:red'><b id='be' style='color:red'><", > 212 "b id='bf' style='color:red'><b id='bg' style='colo", > 213 "r:red'><b id='bh' style='color:red'><b id='bi' sty", > 214 "le='color:red'><b id='bj' style='color:red'><b id=", > 215 "'bk' style='color:red'><b id='bl' style='color:red", > 216 "'><b id='bm' style='color:red'><b id='bn' style='c", > 217 "olor:red'><b id='bo' style='color:red'><b id='bp' ", > 218 "style='color:red'><b id='bq' style='color:red'><b ", > 219 "id='br' style='color:red'><b id='bs' style='color:", > 220 "red'><b id='bt' style='color:red'><b id='bu' style", > 221 "='color:red'><b id='bv' style='color:red'><b id='b", > 222 "w' style='color:red'><b id='bx' style='color:red'>", > 223 "<b id='by' style='color:red'><b id='bz' style='col", > 224 "or:red'><b id='ca' style='color:red'><b id='cb' st", > 225 "yle='color:red'><b id='cc' style='color:red'><b id", > 226 "='cd' style='color:red'><b id='ce' style='color:re", > 227 "d'><b id='cf' style='color:red'><b id='cg' style='", > 228 "color:red'><b id='ch' style='color:red'><b id='ci'", > 229 " style='color:red'><b id='cj' style='color:red'><b", > 230 " id='ck' style='color:red'><b id='cl' style='color", > 231 ":red'><b id='cm' style='color:red'><b id='cn' styl", > 232 "e='color:red'><b id='co' style='color:red'><b id='", > 233 "cp' style='color:red'><b id='cq' style='color:red'", > 234 "><b id='cr' style='color:red'><b id='cs' style='co", > 235 "lor:red'><b id='ct' style='color:red'><b id='cu' s", > 236 "tyle='color:red'><b id='cv' style='color:red'><b i", > 237 "d='cw' style='color:red'></b></b></b></b></b></b><", > 238 "/b></b></b></b></b></b></b></b></b></b></b></b></b", > 239 "></b></b></b></b></b></b></b></b></b></b></b></b><", > 240 "/b></b></b></b></b></b></b></b></b></b></b></b></b", > 241 "></b></b></b></b></b></b></b></b></b></b></b></b><", > 242 "/b></b></b></b></b></b></b></b></b></b></b></b></b", > 243 "></b></b></b></b></b></b>" > 244 }; > 245 vector <string> xthml(xthml_, xthml_+sizeof(xthml_)/sizeof(*xthml_)); > 246 int _ = -1; > 247 END > 248 } > 249 // END CUT HERE
Added SRM/TCO10-1-U/1A.cpp version [86891d068367bcdf]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class EqualizeStrings { public: > 23 string getEq(string s, string t) > 24 { > 25 string answer; > 26 for(int i=0; i<s.size(); ++i) > 27 answer += char(adjust(s[i]-'a', t[i]-'a')+'a'); > 28 return answer; > 29 } > 30 int adjust(int x, int y) > 31 { > 32 int minC = 9999; > 33 int minZ = 0; > 34 for(int z=0; z<26; ++z) > 35 { > 36 int c = min(abs(x-z), 26-abs(x-z)) + min(abs(y-z), 26-ab > 37 if( c < minC ) { > 38 minC = c; > 39 minZ = z; > 40 } > 41 } > 42 return minZ; > 43 } > 44 }; > 45 > 46 // BEGIN CUT HERE > 47 #include <ctime> > 48 double start_time; string timer() > 49 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 50 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 51 { os << "{ "; > 52 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 53 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 54 void verify_case(const string& Expected, const string& Received) { > 55 bool ok = (Expected == Received); > 56 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 57 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 58 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 59 #define END verify_case(_, EqualizeStrings().getEq(s, t));} > 60 int main(){ > 61 > 62 CASE(0) > 63 string s = "cat"; > 64 string t = "dog"; > 65 string _ = "caa"; > 66 END > 67 CASE(1) > 68 string s = "abcdefghijklmnopqrstuvwxyz"; > 69 string t = "bcdefghijklmnopqrstuvwxyza"; > 70 string _ = "abcdefghijklmnopqrstuvwxya"; > 71 END > 72 CASE(2) > 73 string s = "programmingcompetitionsrule"; > 74 string t = "programmingcompetitionsrule"; > 75 string _ = "programmingcompetitionsrule"; > 76 END > 77 CASE(3) > 78 string s = "topcoderopen"; > 79 string t = "onlinerounds"; > 80 string _ = "onlcndaoondn"; > 81 END > 82 CASE(4) > 83 string s = "a"; > 84 string t = "z"; > 85 string _ = "a"; > 86 END > 87 CASE(5) > 88 string s = "y"; > 89 string t = "z"; > 90 string _ = "y"; > 91 END > 92 CASE(5) > 93 string s = "z"; > 94 string t = "b"; > 95 string _ = "a"; > 96 END > 97 CASE(5) > 98 string s = "h"; > 99 string t = "t"; > 100 string _ = "h"; > 101 END > 102 CASE(5) > 103 string s = "b"; > 104 string t = "o"; > 105 string _ = "a"; > 106 END > 107 CASE(5) > 108 string s = "b"; > 109 string t = "n"; > 110 string _ = "b"; > 111 END > 112 CASE(5) > 113 string s = "b"; > 114 string t = "q"; > 115 string _ = "a"; > 116 END > 117 } > 118 // END CUT HERE
Added SRM/TCO10-1-U/1B.cpp version [56eab9a0d5dca1c4]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TwoRegisters { public: > 23 string minProg(int r) > 24 { > 25 int minn = 0x7fffffff; > 26 vector<int> miny; > 27 for(int y=1; y<r; ++y) > 28 { > 29 int n = cnt(r,y); > 30 if( n != -1 ) > 31 if( n <= minn ) { > 32 if( n < minn ) > 33 miny.clear(); > 34 minn = n; > 35 miny.push_back(y); > 36 } > 37 } > 38 > 39 string answer; > 40 for(int i=0; i<miny.size(); ++i) > 41 { > 42 int y = miny[i]; > 43 string s; > 44 compute(r, y, false, s); > 45 if( i==0 || s < answer ) > 46 answer = s; > 47 } > 48 return answer; > 49 } > 50 > 51 int cnt(int r, int y) > 52 { > 53 if( y==0 ) > 54 return -1; > 55 if( y==1 ) > 56 return r-1; > 57 int k = r/y; > 58 int kk = cnt(y, r%y); > 59 if( kk == -1 ) > 60 return -1; > 61 return k+kk; > 62 } > 63 > 64 void compute(int r, int y, bool isY, string& s) > 65 { > 66 if( y==1 ) { > 67 for(int i=0; i<r-1; ++i) > 68 s += (isY ? 'Y' : 'X'); > 69 return; > 70 } > 71 int k = r/y; > 72 compute(y, r%y, !isY, s); > 73 while( k --> 0 ) > 74 s += (isY ? 'Y' : 'X'); > 75 } > 76 }; > 77 > 78 // BEGIN CUT HERE > 79 #include <ctime> > 80 double start_time; string timer() > 81 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 82 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 83 { os << "{ "; > 84 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 85 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 86 void verify_case(const string& Expected, const string& Received) { > 87 bool ok = (Expected == Received); > 88 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 89 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 90 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 91 #define END verify_case(_, TwoRegisters().minProg(r));} > 92 int main(){ > 93 > 94 CASE(0) > 95 int r = 10; > 96 string _ = "XXYYX"; > 97 END > 98 CASE(1) > 99 int r = 3; > 100 string _ = "XX"; > 101 END > 102 CASE(2) > 103 int r = 20; > 104 string _ = "XYYYYXX"; > 105 END > 106 CASE(3) > 107 int r = 34; > 108 string _ = "XYXYXYX"; > 109 END > 110 CASE(4) > 111 int r = 1; > 112 string _ = ""; > 113 END > 114 CASE(5) > 115 int r = 1000000; > 116 string _ = "??"; > 117 END > 118 CASE(6) > 119 int r = 2; > 120 string _ = "X"; > 121 END > 122 CASE(6) > 123 int r = 999999; > 124 string _ = "??"; > 125 END > 126 CASE(6) > 127 int r = 999998; > 128 string _ = "??"; > 129 END > 130 CASE(6) > 131 int r = 999997; > 132 string _ = "??"; > 133 END > 134 CASE(6) > 135 int r = 999996; > 136 string _ = "??"; > 137 END > 138 CASE(6) > 139 int r = 999995; > 140 string _ = "??"; > 141 END > 142 CASE(6) > 143 int r = 5; > 144 string _ = "??"; > 145 END > 146 > 147 } > 148 // END CUT HERE
Added SRM/TCO10-1-U/1C-U.cpp version [a16249ed416e3f88]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class VacationTours { public: > 23 int getIncome(vector <string> c, vector <string> d, int fee) > 24 { > 25 int N = c.size(); > 26 vector< vector<int> > G(N, vector<int>(N)); > 27 for(int i=0; i<N; ++i) > 28 for(int j=0; j<N; ++j) > 29 G[i][j] = dec(c[i][j])*64 + dec(d[i][j]); > 30 > 31 int basefee = fee * (N-1); > 32 for(int i=1; i<N; ++i) > 33 basefee -= G[0][i] + G[i][0]; > 34 > 35 typedef pair< int, pair<int,int> > edge; > 36 set< edge, greater<edge> > es; > 37 vector< vector<int> > P(N, vector<int>(N)); > 38 for(int i=1; i<N; ++i) > 39 for(int j=1; j<N; ++j) if(i!=j) { > 40 P[i][j] = -fee - G[i][j] + G[i][0] + G[0][j]; > 41 es.insert( edge(P[i][j], make_pair(i,j)) ); > 42 } > 43 > 44 int gain = 0; > 45 > 46 vector<bool> out(N,true), in(N,true); > 47 vector<int> uf(N), sz(N); > 48 for(int v=0; v<N; ++v) uf[v]=v, sz[v]=1; > 49 > 50 for(set< edge, greater<edge> >::iterator it=es.begin(); it!=es.e > 51 { > 52 int c = it->first; > 53 int a = it->second.first; > 54 int b = it->second.second; > 55 if( c <= 0 ) > 56 break; > 57 if( !out[a] || !in[b] ) > 58 continue; > 59 // repr > 60 int ra=a; while(uf[ra]!=ra) ra=uf[ra]; > 61 int rb=b; while(uf[rb]!=rb) rb=uf[rb]; > 62 if( ra != rb ) { > 63 gain += c; > 64 // union > 65 if( sz[ra] > sz[rb] ) swap(ra,rb); > 66 uf[ra] = rb; > 67 sz[rb] += sz[ra]; > 68 out[a] = false; > 69 in[b] = false; > 70 } > 71 } > 72 > 73 return max(0, basefee+gain); > 74 } > 75 > 76 int dec(char c) > 77 { > 78 if('A'<=c && c<='Z') return c-'A'; > 79 if('a'<=c && c<='z') return c-'a'+26; > 80 if('0'<=c && c<='9') return c-'0'+52; > 81 if(c=='+')return 62; > 82 return 63; > 83 } > 84 }; > 85 > 86 // BEGIN CUT HERE > 87 #include <ctime> > 88 double start_time; string timer() > 89 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 90 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 91 { os << "{ "; > 92 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 93 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 94 void verify_case(const int& Expected, const int& Received) { > 95 bool ok = (Expected == Received); > 96 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 97 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 98 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 99 #define END verify_case(_, VacationTours().getIncome(c, d, fee));} > 100 int main(){ > 101 > 102 CASE(0) > 103 string c_[] = {"AAA", > 104 "AAA", > 105 "AAA"}; > 106 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 107 string d_[] = {"ABJ", > 108 "JAB", > 109 "BJA"}; > 110 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 111 int fee = 15; > 112 int _ = 12; > 113 END > 114 CASE(1) > 115 string c_[] = {"AAAA", > 116 "AAAA", > 117 "AAAA", > 118 "AAAA"}; > 119 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 120 string d_[] = {"AAAA", > 121 "AAAA", > 122 "AAAA", > 123 "AAAA"}; > 124 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 125 int fee = 100; > 126 int _ = 300; > 127 END > 128 CASE(2) > 129 string c_[] = {"A//", > 130 "/A/", > 131 "//A"}; > 132 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 133 string d_[] = {"A//", > 134 "/A/", > 135 "//A"}; > 136 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 137 int fee = 1000; > 138 int _ = 0; > 139 END > 140 CASE(3) > 141 string c_[] = {"AAA////", > 142 "/AA/A//", > 143 "//AA/A/", > 144 "A//AA//", > 145 "///AAA/", > 146 "///A/AA", > 147 "AA////A"}; > 148 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 149 string d_[] = {"AKo////", > 150 "/AU/X//", > 151 "//AZ/o/", > 152 "j//AK//", > 153 "///XAo/", > 154 "///y/AK", > 155 "KP////A"}; > 156 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 157 int fee = 1000; > 158 int _ = 1809; > 159 END > 160 /* > 161 CASE(4) > 162 string c_[] = ; > 163 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 164 string d_[] = ; > 165 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 166 int fee = ; > 167 int _ = ; > 168 END > 169 CASE(5) > 170 string c_[] = ; > 171 vector <string> c(c_, c_+sizeof(c_)/sizeof(*c_)); > 172 string d_[] = ; > 173 vector <string> d(d_, d_+sizeof(d_)/sizeof(*d_)); > 174 int fee = ; > 175 int _ = ; > 176 END > 177 */ > 178 } > 179 // END CUT HERE
Added SRM/TCO10-2-U/1A.cpp version [d7e39504fab93869]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SnowPlow { public: > 23 int solve(vector <string> roads) > 24 { > 25 int N = roads.size(); > 26 > 27 set< pair<int,int> > edges; > 28 int sum = 0; > 29 for(int i=0; i<N; ++i) > 30 for(int j=i+1; j<N; ++j) > 31 if( roads[i][j] > '0' ) { > 32 edges.insert( make_pair(i,j) ); > 33 sum += 2*(roads[i][j]-'0'); > 34 } > 35 > 36 set<int> vis; > 37 queue<int> q; q.push(0); > 38 while( !q.empty() ) { > 39 int v = q.front(); > 40 q.pop(); > 41 for(int j=0; j<N; ++j) if( j!=v ) > 42 if( roads[v][j]>'0' ) { > 43 edges.erase( make_pair(min(j,v), max(j,v > 44 if( !vis.count(j) ) { > 45 q.push(j); > 46 vis.insert(j); > 47 } > 48 } > 49 } > 50 return edges.empty() ? sum : -1; > 51 } > 52 }; > 53 > 54 // BEGIN CUT HERE > 55 #include <ctime> > 56 double start_time; string timer() > 57 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 58 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 59 { os << "{ "; > 60 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 61 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 62 void verify_case(const int& Expected, const int& Received) { > 63 bool ok = (Expected == Received); > 64 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 65 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 66 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 67 #define END verify_case(_, SnowPlow().solve(roads));} > 68 int main(){ > 69 > 70 CASE(0) > 71 string roads_[] = {"010000", > 72 "101000", > 73 "010100", > 74 "001010", > 75 "000101", > 76 "000010"}; > 77 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 78 int _ = 10; > 79 END > 80 CASE(1) > 81 string roads_[] = {"010000", > 82 "101000", > 83 "010100", > 84 "001020", > 85 "000201", > 86 "000010"}; > 87 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 88 int _ = 12; > 89 END > 90 CASE(2) > 91 string roads_[] = {"031415", > 92 "300000", > 93 "100000", > 94 "400000", > 95 "100000", > 96 "500000"}; > 97 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 98 int _ = 28; > 99 END > 100 CASE(3) > 101 string roads_[] = {"0100", > 102 "1000", > 103 "0001", > 104 "0010"}; > 105 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 106 int _ = -1; > 107 END > 108 CASE(4) > 109 string roads_[] = {"0101", > 110 "1001", > 111 "0000", > 112 "1100"}; > 113 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 114 int _ = 6; > 115 END > 116 /* > 117 CASE(5) > 118 string roads_[] = ; > 119 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 120 int _ = ; > 121 END > 122 CASE(6) > 123 string roads_[] = ; > 124 vector <string> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_)); > 125 int _ = ; > 126 END > 127 */ > 128 } > 129 // END CUT HERE
Added SRM/TCO10-2-U/1B.cpp version [cd08fa40861c2ec8]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class RepresentableNumbers { public: > 23 int getNext(int X) > 24 { > 25 for(;; ++X) { > 26 string s; > 27 {stringstream sio; sio<<X; sio>>s;} > 28 memo.clear(); > 29 memo.resize(200, -1); > 30 if( rec(s, 0, false, false) ) > 31 return X; > 32 if( s[0]=='1' ) { > 33 if( rec(s, 1, false, true) ) > 34 return X; > 35 } > 36 } > 37 assert(false); > 38 } > 39 > 40 vector<int> memo; > 41 bool rec(const string& s, int i, bool mustUseBoth, bool needCarry) > 42 { > 43 if( i == (int)s.size() ) > 44 return !needCarry; > 45 if( i+1 == (int)s.size() ) > 46 mustUseBoth = true; > 47 > 48 int key = (i*2+mustUseBoth)*2+needCarry; > 49 if( memo[key]>=0 ) > 50 return memo[key]; > 51 int t = (s[i]-'0') + (needCarry ? 10 : 0); > 52 > 53 if( !mustUseBoth && (t==1 || t==3 || t==5 || t==7 || t==9) ) > 54 if( rec(s, i+1, false, false) ) > 55 return memo[key]=true; > 56 if( !mustUseBoth && (t==2 || t==4 || t==6 || t==8 || t==10) ) > 57 if( rec(s, i+1, false, true) ) > 58 return memo[key]=true; > 59 if( t<=1 ) > 60 return memo[key]=false; > 61 if( t%2==0 ) > 62 return memo[key]=rec(s, i+1, true, false); > 63 else > 64 return memo[key]=rec(s, i+1, true, true); > 65 } > 66 }; > 67 > 68 // BEGIN CUT HERE > 69 #include <ctime> > 70 double start_time; string timer() > 71 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 72 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 73 { os << "{ "; > 74 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 75 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 76 void verify_case(const int& Expected, const int& Received) { > 77 bool ok = (Expected == Received); > 78 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 79 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 80 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 81 #define END verify_case(_, RepresentableNumbers().getNext(X));} > 82 int main(){ > 83 > 84 CASE(0) > 85 int X = 1; > 86 int _ = 2; > 87 END > 88 CASE(1) > 89 int X = 999; > 90 int _ = 1000; > 91 END > 92 CASE(2) > 93 int X = 2000; > 94 int _ = 2000; > 95 END > 96 CASE(3) > 97 int X = 4201234; > 98 int _ = 4222222; > 99 END > 100 CASE(4) > 101 int X = 10101010; > 102 int _ = 10102222; > 103 END > 104 CASE(5) > 105 int X = 2; > 106 int _ = 2; > 107 END > 108 CASE(5) > 109 int X = 3; > 110 int _ = 4; > 111 END > 112 CASE(5) > 113 int X = 4; > 114 int _ = 4; > 115 END > 116 CASE(5) > 117 int X = 5; > 118 int _ = 6; > 119 END > 120 CASE(5) > 121 int X = 6; > 122 int _ = 6; > 123 END > 124 CASE(5) > 125 int X = 20; > 126 int _ = 20; > 127 END > 128 CASE(6) > 129 int X = 100000000; > 130 int _ = -1; > 131 END > 132 CASE(6) > 133 int X = 42012345; > 134 int _ = -1; > 135 END > 136 CASE(6) > 137 int X = 21999999; > 138 int _ = -1; > 139 END > 140 > 141 > 142 } > 143 // END CUT HERE
Added SRM/TCO10-2-U/1C-U.cpp version [09d7f34f11d09ccc]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class BreakingChocolate { public: > 23 int minSteps(int W, int H, vector <int> sx, vector <int> sy) > 24 { > 25 > 26 } > 27 }; > 28 > 29 // BEGIN CUT HERE > 30 #include <ctime> > 31 double start_time; string timer() > 32 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 33 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 34 { os << "{ "; > 35 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 36 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 37 void verify_case(const int& Expected, const int& Received) { > 38 bool ok = (Expected == Received); > 39 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 40 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 41 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 42 #define END verify_case(_, BreakingChocolate().minSteps(W, H, sx, sy));} > 43 int main(){ > 44 > 45 CASE(0) > 46 int W = 3; > 47 int H = 3; > 48 int sx_[] = {2}; > 49 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 50 int sy_[] = {2}; > 51 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 52 int _ = 4; > 53 END > 54 CASE(1) > 55 int W = 2; > 56 int H = 2; > 57 int sx_[] = {1,2}; > 58 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 59 int sy_[] = {2,1}; > 60 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 61 int _ = 3; > 62 END > 63 CASE(2) > 64 int W = 10; > 65 int H = 10; > 66 int sx_[] = {1}; > 67 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 68 int sy_[] = {1}; > 69 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 70 int _ = 2; > 71 END > 72 CASE(3) > 73 int W = 10; > 74 int H = 10; > 75 int sx_[] = {3,5,6}; > 76 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 77 int sy_[] = {5,5,5}; > 78 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 79 int _ = 6; > 80 END > 81 CASE(4) > 82 int W = 3; > 83 int H = 3; > 84 int sx_[] = {1,1,1,2,2,3,3,3}; > 85 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 86 int sy_[] = {1,2,3,1,3,1,2,3}; > 87 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 88 int _ = 4; > 89 END > 90 CASE(5) > 91 int W = ; > 92 int H = ; > 93 int sx_[] = ; > 94 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 95 int sy_[] = ; > 96 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 97 int _ = ; > 98 END > 99 CASE(6) > 100 int W = ; > 101 int H = ; > 102 int sx_[] = ; > 103 vector <int> sx(sx_, sx_+sizeof(sx_)/sizeof(*sx_)); > 104 int sy_[] = ; > 105 vector <int> sy(sy_, sy_+sizeof(sy_)/sizeof(*sy_)); > 106 int _ = ; > 107 END > 108 > 109 } > 110 // END CUT HERE
Added SRM/TCO10-3/1A.cpp version [4a490e7a696723ed]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SieveOfEratosthenes { public: > 23 vector<int> eratos(int M) > 24 { > 25 vector<bool> isComposite(M+1); > 26 vector<int> ps; > 27 for(int p=2; p<=M; ++p) > 28 if( !isComposite[p] ) > 29 { > 30 ps.push_back(p); > 31 for(LL q=LL(p)*p; q<=M; q+=p) > 32 isComposite[q] = true; > 33 } > 34 return ps; > 35 } > 36 > 37 int minFactor(int N, const vector<int>& ps) > 38 { > 39 for(int i=0; i<ps.size(); ++i) > 40 if( N % ps[i] == 0 ) > 41 return ps[i]; > 42 return INT_MAX; > 43 } > 44 > 45 int lastScratch(int maxNum) > 46 { > 47 vector<int> ps = eratos(int(sqrt(double(maxNum)))); > 48 int P = ps.back(); > 49 int M = P*P; > 50 for(int Q=P+1; P*Q<=maxNum; ++Q) > 51 if( minFactor(Q,ps) >= P ) > 52 M = P*Q; > 53 return M; > 54 } > 55 }; > 56 > 57 // BEGIN CUT HERE > 58 #include <ctime> > 59 double start_time; string timer() > 60 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 61 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 62 { os << "{ "; > 63 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 64 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 65 void verify_case(const int& Expected, const int& Received) { > 66 bool ok = (Expected == Received); > 67 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 68 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 69 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 70 #define END verify_case(_, SieveOfEratosthenes().lastScratch(maxNum));} > 71 int main(){ > 72 > 73 CASE(0) > 74 int maxNum = 18; > 75 int _ = 15; > 76 END > 77 CASE(1) > 78 int maxNum = 5; > 79 int _ = 4; > 80 END > 81 CASE(2) > 82 int maxNum = 100; > 83 int _ = 91; > 84 END > 85 CASE(3) > 86 int maxNum = 400; > 87 int _ = 361; > 88 END > 89 CASE(4) > 90 int maxNum = 2000000000; > 91 int _ = -1; > 92 END > 93 CASE(5) > 94 int maxNum = 8; > 95 int _ = 8; > 96 END > 97 } > 98 // END CUT HERE
Added SRM/TCO10-3/1B.cpp version [c7168a3555d839b7]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class TheChroniclesOfAmber { public: > 23 double minimumTime(vector <int> princeX, vector <int> princeY, vector <i > 24 { > 25 int N = princeX.size(); > 26 > 27 // d[s][g] := the distance from prince[s] to dest[g] > 28 vector< vector<double> > d(N, vector<double>(N)); > 29 for(int s=0; s<N; ++s) > 30 for(int g=0; g<N; ++g) > 31 d[s][g] = hypot(princeX[s]-destinationX[g], prin > 32 > 33 // no teleportation > 34 double noTelepo = 0; > 35 for(int i=0; i<N; ++i) > 36 noTelepo = max(noTelepo, d[i][i]); > 37 > 38 // with teleportation: at least one position must be empty > 39 double answer = noTelepo; > 40 for(int unused_s=0; unused_s<N; ++unused_s) { > 41 double withTelepo = 0; > 42 for(int g=0; g<N; ++g) { // for each goal > 43 double t = 1e+300; > 44 for(int s=0; s<N; ++s) if( s != unused_s ) > 45 t = min(t, d[s][g]); // find the nearest > 46 withTelepo = max(withTelepo, t); > 47 } > 48 answer = min(answer, withTelepo); // take the minimum > 49 } > 50 return answer; > 51 } > 52 }; > 53 > 54 // BEGIN CUT HERE > 55 #include <ctime> > 56 double start_time; string timer() > 57 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 58 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 59 { os << "{ "; > 60 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 61 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 62 void verify_case(const double& Expected, const double& Received) { > 63 bool ok = (abs(Expected - Received) < 1e-9); > 64 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 65 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 66 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 67 #define END verify_case(_, TheChroniclesOfAmber().minimumTime(princeX, prin > 68 int main(){ > 69 > 70 CASE(0) > 71 int princeX_[] = {1,5,5}; > 72 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 73 int princeY_[] = {0,0,0}; > 74 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 75 int destinationX_[] = {1,1,0}; > 76 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 77 int destinationY_[] = {4,2,3}; > 78 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 79 double _ = 4.0; > 80 END > 81 CASE(1) > 82 int princeX_[] = {0,0,0}; > 83 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 84 int princeY_[] = {1,2,3}; > 85 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 86 int destinationX_[] = {0,0,0}; > 87 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 88 int destinationY_[] = {0,2,4}; > 89 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 90 double _ = 1.0; > 91 END > 92 CASE(2) > 93 int princeX_[] = {0,0,0}; > 94 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 95 int princeY_[] = {1,2,3}; > 96 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 97 int destinationX_[] = {0,0,0}; > 98 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 99 int destinationY_[] = {4,2,0}; > 100 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 101 double _ = 1.0; > 102 END > 103 CASE(3) > 104 int princeX_[] = {0,3,5}; > 105 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 106 int princeY_[] = {0,4,0}; > 107 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 108 int destinationX_[] = {3,5,0}; > 109 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 110 int destinationY_[] = {4,0,0}; > 111 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 112 double _ = 4.47213595499958; > 113 END > 114 CASE(4) > 115 int princeX_[] = {3629,6751,8655,5115,7809,6759,7133,1810,6102,2539,1777 > 116 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 117 int princeY_[] = {5294,180,988,7780,1635,7904,845,7405,4800,2567,4795,23 > 118 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 119 int destinationX_[] = {8723,9275,6705,5875,7981,7666,1158,4135,17,2984,5 > 120 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 121 int destinationY_[] = {6166,53,5980,4499,412,9074,8190,847,650,9158,9116 > 122 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 123 double _ = 2622.582696503582; > 124 END > 125 CASE(5) > 126 int princeX_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0, > 127 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 128 int princeY_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6, > 129 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 130 int destinationX_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 > 131 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 132 int destinationY_[] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4 > 133 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 134 double _ = 0; > 135 END > 136 /* > 137 CASE(6) > 138 int princeX_[] = ; > 139 vector <int> princeX(princeX_, princeX_+sizeof(princeX_)/sizeof(*princ > 140 int princeY_[] = ; > 141 vector <int> princeY(princeY_, princeY_+sizeof(princeY_)/sizeof(*princ > 142 int destinationX_[] = ; > 143 vector <int> destinationX(destinationX_, destinationX_+sizeof(destinat > 144 int destinationY_[] = ; > 145 vector <int> destinationY(destinationY_, destinationY_+sizeof(destinat > 146 double _ = ; > 147 END > 148 */ > 149 } > 150 // END CUT HERE
Added SRM/TCO10-3/1C.cpp version [244c2d068d085457]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int MODVAL = 1000000009; > 23 struct mint > 24 { > 25 int val; > 26 mint():val(0){} > 27 mint(int x):val(x%MODVAL) {} > 28 mint(LL x):val(x%MODVAL) {} > 29 }; > 30 > 31 mint operator+(mint x, mint y) { return x.val+y.val; } > 32 mint operator-(mint x, mint y) { return x.val-y.val+MODVAL; } > 33 mint operator*(mint x, mint y) { return LL(x.val)*y.val; } > 34 mint POW(mint x, int e) { > 35 mint v = 1; > 36 for(;e;x=x*x,e>>=1) > 37 if(e&1) > 38 v=v*x; > 39 return v; > 40 } > 41 mint operator/(mint x, mint y) { return x * POW(y, MODVAL-2); } > 42 > 43 vector<mint> FAC_(1,1); > 44 void FAC_INIT(int n) { for(int i=1; i<=n; ++i) FAC_.push_back( FAC_.back()*i ); > 45 mint FAC(mint x) { return FAC_[x.val]; } > 46 mint C(mint n, mint k) { return k.val<0 || n.val<k.val ? 0 : FAC(n) / (FAC(k) * > 47 > 48 class Passwords { public: > 49 int countValid(int N, int L, int U, int D) > 50 { > 51 FAC_INIT(200000); > 52 > 53 // Sigma_{D<=d<=N-U-L} [C(N,d) 10^d Sigma_{L<=l,U<=u,u+l=N-d}[ > 54 //= Sigma_{D<=d<=N-U-L} [C(N,d) 10^d 26^(N-d) Sigma_{U<=u<=N-d-L > 55 // let f(N') = Sigma_{U<=u<=N'-L}[ C(N',u) ] > 56 // then by Pascal's triangle, f(N') = 2*f(N'-1)+C(N'-1,U-1) > 57 > 58 mint answer=0, f=C(U+L,U); > 59 for(int d=N-U-L; d>=D; --d) > 60 { > 61 answer = answer + C(N,d)*POW(10,d)*POW(26,N-d)*f; > 62 f = f*2 + C(N-d,U-1) + C(N-d, N-d-L+1); > 63 } > 64 return answer.val; > 65 } > 66 }; > 67 > 68 // BEGIN CUT HERE > 69 #include <ctime> > 70 double start_time; string timer() > 71 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) > 72 template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) > 73 { os << "{ "; > 74 for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) > 75 os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return > 76 void verify_case(const int& Expected, const int& Received) { > 77 bool ok = (Expected == Received); > 78 if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() > 79 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' > 80 #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock( > 81 #define END verify_case(_, Passwords().countValid(N, L, U, D));} > 82 int main(){ > 83 > 84 CASE(0) > 85 int N = 2; > 86 int L = 0; > 87 int U = 0; > 88 int D = 2; > 89 int _ = 100; > 90 END > 91 CASE(1) > 92 int N = 3; > 93 int L = 1; > 94 int U = 1; > 95 int D = 1; > 96 int _ = 40560; > 97 END > 98 CASE(2) > 99 int N = 4; > 100 int L = 1; > 101 int U = 1; > 102 int D = 1; > 103 int _ = 5029440; > 104 END > 105 CASE(3) > 106 int N = 10; > 107 int L = 1; > 108 int U = 3; > 109 int D = 3; > 110 int _ = 818019214; > 111 END > 112 CASE(4) > 113 int N = 5; > 114 int L = 2; > 115 int U = 2; > 116 int D = 2; > 117 int _ = 0; > 118 END > 119 CASE(5) > 120 int N = 199201; > 121 int L = 0; > 122 int U = 70000; > 123 int D = 0; > 124 int _ = 943311211; > 125 END > 126 CASE(6) > 127 int N = 200000; > 128 int L = 11; > 129 int U = 111; > 130 int D = 1111; > 131 int _ = 298235838; > 132 END > 133 } > 134 // END CUT HERE
Added SRM/TCO9-1/A.cpp version [07008cbdf3a5db57]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class SequenceSums { > 21 public: > 22 vector <int> sequence(int N, int LL) > 23 { > 24 for(int L=LL; L<=100; ++L) > 25 { > 26 int Z = N - L*(L-1)/2; > 27 if( Z % L ) > 28 continue; > 29 int a = Z / L; > 30 if( a < 0 ) > 31 continue; > 32 > 33 vector<int> ans; > 34 for(int x=a; x<a+L; ++x) > 35 ans.push_back(x); > 36 return ans; > 37 } > 38 return vector<int>(); > 39 } > 40 }; > 41 > 42 // BEGIN CUT HERE > 43 #include <ctime> > 44 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 45 > 46 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 47 int verify_case(const vector <int> &Expected, const vector <int> &Received) { if > 48 > 49 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 50 char Test_(...); > 51 int Test_(Case_<0>) { > 52 int N = 18; > 53 int L = 2; > 54 int RetVal_[] = {5, 6, 7 }; > 55 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 56 return verify_case(RetVal, SequenceSums().sequence(N, L)); } > 57 int Test_(Case_<1>) { > 58 int N = 18; > 59 int L = 4; > 60 int RetVal_[] = {3, 4, 5, 6 }; > 61 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 62 return verify_case(RetVal, SequenceSums().sequence(N, L)); } > 63 int Test_(Case_<2>) { > 64 int N = 18; > 65 int L = 5; > 66 vector <int> RetVal; > 67 return verify_case(RetVal, SequenceSums().sequence(N, L)); } > 68 int Test_(Case_<3>) { > 69 int N = 45; > 70 int L = 10; > 71 int RetVal_[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; > 72 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 73 return verify_case(RetVal, SequenceSums().sequence(N, L)); } > 74 int Test_(Case_<4>) { > 75 int N = 1000000000; > 76 int L = 2; > 77 int RetVal_[] = {199999998, 199999999, 200000000, 200000001, 200000002 } > 78 vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)) > 79 return verify_case(RetVal, SequenceSums().sequence(N, L)); } > 80 > 81 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 82 template<> void Run_<-1>() {} > 83 int main() { Run_<0>(); } > 84 // END CUT HERE > 85
Added SRM/TCO9-1/B.cpp version [9180662f25d62836]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 class KthProbableElement { > 21 public: > 22 double probability(int M, int lowerBound, int upperBound, int N, int K) > 23 { > 24 double dp1[101][101]={0}, dp2[101][101], (*P)[101]=dp1, (*Q)[101]=dp2; > 25 > 26 P[0][0] = 1.0; > 27 for(int i=0; i<M; ++i) > 28 { > 29 memset( Q, 0, sizeof(double)*101*101 ); > 30 for(int a=0; a<=i; ++a) > 31 for(int b=0; a+b<=i; ++b) > 32 { > 33 Q[a+1][b] += P[a][b] * (N-lowerBound) / (upperBound-lowerBound+1); > 34 Q[a][b+1] += P[a][b] * 1 / (upperBound-lowerBound+1); > 35 Q[a][b] += P[a][b] * (upperBound-N) / (upperBound-lowerBound+1); > 36 } > 37 swap(P, Q); > 38 } > 39 > 40 double p = 0.0; > 41 for(int a=0; a<=M; ++a) > 42 for(int b=0; a+b<=M; ++b) > 43 if( a<K && K<=a+b ) > 44 p += P[a][b]; > 45 return p; > 46 } > 47 }; > 48 > 49 // BEGIN CUT HERE > 50 #include <ctime> > 51 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 52 > 53 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 54 int verify_case(const double &Expected, const double &Received) { double diff = > 55 > 56 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 57 char Test_(...); > 58 int Test_(Case_<0>) { > 59 int M = 1; > 60 int lowerBound = 1; > 61 int upperBound = 10; > 62 int N = 3; > 63 int K = 1; > 64 double RetVal = 0.1; > 65 return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upp > 66 int Test_(Case_<1>) { > 67 int M = 3; > 68 int lowerBound = 1; > 69 int upperBound = 2; > 70 int N = 2; > 71 int K = 2; > 72 double RetVal = 0.5; > 73 return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upp > 74 int Test_(Case_<2>) { > 75 int M = 3; > 76 int lowerBound = 1; > 77 int upperBound = 3; > 78 int N = 2; > 79 int K = 2; > 80 double RetVal = 0.48148148148148145; > 81 return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upp > 82 int Test_(Case_<3>) { > 83 int M = 10; > 84 int lowerBound = 1; > 85 int upperBound = 10; > 86 int N = 1; > 87 int K = 10; > 88 double RetVal = 1.0000000000000003E-10; > 89 return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upp > 90 int Test_(Case_<4>) { > 91 int M = 4; > 92 int lowerBound = 61; > 93 int upperBound = 65; > 94 int N = 62; > 95 int K = 3; > 96 double RetVal = 0.15200000000000002; > 97 return verify_case(RetVal, KthProbableElement().probability(M, lowerBound, upp > 98 > 99 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 100 template<> void Run_<-1>() {} > 101 int main() { Run_<0>(); } > 102 // END CUT HERE > 103
Added SRM/TCO9-1/C.cpp version [850feccc28db3e68]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <complex> > 12 #include <queue> > 13 #include <stack> > 14 #include <cmath> > 15 #include <cassert> > 16 #include <cstring> > 17 using namespace std; > 18 typedef long long LL; > 19 > 20 static const int MODVAL = 1000000007; > 21 int ADD(int x, int y) { return (x+y)%MODVAL; } > 22 int SUB(int x, int y) { return (x-y+MODVAL)%MODVAL; } > 23 > 24 class Unicorn { > 25 public: > 26 int countWays(int R, int C, int L, int seed, string word) > 27 { > 28 // input... > 29 char B[300][300]; > 30 { > 31 int x = seed; > 32 int d = (65535 / L)+1; > 33 for (int r=0; r<R; r++) > 34 for (int c=0; c<C; c++) { > 35 x = (x * 25173 + 13849) % 65536; > 36 B[r][c] = (65+(x / d)); > 37 } > 38 } > 39 > 40 // solve > 41 vector<int> cnt(C*R); > 42 for(int r=0; r<R; ++r) > 43 for(int c=0; c<C; ++c) > 44 if( B[r][c] == word[0] ) > 45 cnt[r*C+c] = 1; > 46 > 47 for(int i=1; i<word.size(); ++i) > 48 { > 49 int total_cnt = 0; > 50 vector<int> c_cnt(C); > 51 vector<int> r_cnt(R); > 52 for(int r=0; r<R; ++r) > 53 for(int c=0; c<C; ++c) > 54 r_cnt[r] = ADD( r_cnt[r], cnt[r*C+c]), > 55 c_cnt[c] = ADD( c_cnt[c], cnt[r*C+c]), > 56 total_cnt = ADD(total_cnt, cnt[r*C+c]); > 57 > 58 vector<int> cnt2(C*R); > 59 for(int r=0; r<R; ++r) > 60 for(int c=0; c<C; ++c) > 61 if( B[r][c] == word[i] ) > 62 { > 63 int x = total_cnt; > 64 > 65 for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr > 66 x = SUB(x, r_cnt[rr]); > 67 for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc > 68 x = SUB(x, c_cnt[cc]); > 69 > 70 for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr > 71 for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc > 72 x = ADD(x, cnt[rr*C+cc]); > 73 > 74 for(int rr=r-2; rr<=r+2; rr+=4) if(0<=rr > 75 for(int cc=c-2; cc<=c+2; cc+=4) if(0<=cc > 76 x = SUB(x, cnt[rr*C+cc]); > 77 > 78 cnt2[r*C+c] = x; > 79 } > 80 cnt.swap(cnt2); > 81 } > 82 > 83 int live = 0; > 84 for(int r=0; r<R; ++r) > 85 for(int c=0; c<C; ++c) > 86 live = ADD(live, cnt[r*C+c]); > 87 return live; > 88 } > 89 }; > 90 > 91 // BEGIN CUT HERE > 92 #include <ctime> > 93 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 94 > 95 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 96 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 97 > 98 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 99 char Test_(...); > 100 int Test_(Case_<0>) { > 101 int R = 3; > 102 int C = 4; > 103 int L = 2; > 104 int seed = 47; > 105 string word = "AB"; > 106 int RetVal = 2; > 107 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 108 int Test_(Case_<1>) { > 109 int R = 5; > 110 int C = 5; > 111 int L = 2; > 112 int seed = 47; > 113 string word = "CD"; > 114 int RetVal = 0; > 115 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 116 int Test_(Case_<2>) { > 117 int R = 4; > 118 int C = 4; > 119 int L = 1; > 120 int seed = 42; > 121 string word = "AA"; > 122 int RetVal = 20; > 123 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 124 int Test_(Case_<3>) { > 125 int R = 4; > 126 int C = 4; > 127 int L = 1; > 128 int seed = 42; > 129 string word = "AAAAA"; > 130 int RetVal = 172; > 131 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 132 int Test_(Case_<4>) { > 133 int R = 1; > 134 int C = 1; > 135 int L = 5; > 136 int seed = 54321; > 137 string word = "ABCDE"; > 138 int RetVal = 0; > 139 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 140 int Test_(Case_<5>) { > 141 int R = 8; > 142 int C = 8; > 143 int L = 26; > 144 int seed = 226; > 145 string word = "TOPCODER"; > 146 int RetVal = 1; > 147 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 148 int Test_(Case_<6>) { > 149 int R = 20; > 150 int C = 20; > 151 int L = 2; > 152 int seed = 47; > 153 string word = "AAAAA"; > 154 int RetVal = 373977054; > 155 return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } > 156 > 157 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 158 template<> void Run_<-1>() {} > 159 int main() { Run_<0>(); } > 160 // END CUT HERE > 161
Added SRM/TCO9-2/1A.cpp version [b8cf68d0e0d741f2]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 LL s, N, K; > 23 > 24 class PlaneFractal { > 25 public: > 26 vector <string> getPattern(int _s, int _N, int _K, int r1, int r2, int c > 27 { > 28 s = _s; > 29 N = _N; > 30 K = _K; > 31 > 32 LL Ns=1; for(int i=0; i<s-1; ++i) Ns*=N; > 33 > 34 vector<string> ans(r2-r1+1, string(c2-c1+1, '0')); > 35 for(int r=r1; r<=r2; ++r) > 36 for(int c=c1; c<=c2; ++c) > 37 if( s>0 && black(r,c,Ns) ) > 38 ans[r-r1][c-c1] = '1'; > 39 return ans; > 40 } > 41 > 42 bool black(int y, int x, LL Ns) const > 43 { > 44 if( Ns == 0 ) > 45 return false; > 46 > 47 int Y = y/Ns, X = x/Ns; > 48 y%=Ns, x%=Ns; > 49 if( (Y<(N-K)/2 || N-(N-K)/2<=Y) || (X<(N-K)/2 || N-(N-K)/2<=X) ) > 50 return black(y, x, Ns/N); > 51 return true; > 52 } > 53 }; > 54 > 55 // BEGIN CUT HERE > 56 #include <ctime> > 57 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 58 > 59 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 60 int verify_case(const vector <string> &Expected, const vector <string> &Received > 61 > 62 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 63 char Test_(...); > 64 int Test_(Case_<0>) { > 65 int s = 1; > 66 int N = 5; > 67 int K = 3; > 68 int r1 = 0; > 69 int r2 = 4; > 70 int c1 = 0; > 71 int c2 = 4; > 72 string RetVal_[] = {"00000", "01110", "01110", "01110", "00000" }; > 73 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 74 return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1 > 75 int Test_(Case_<1>) { > 76 int s = 2; > 77 int N = 3; > 78 int K = 1; > 79 int r1 = 0; > 80 int r2 = 8; > 81 int c1 = 0; > 82 int c2 = 8; > 83 string RetVal_[] = {"000000000", "010010010", "000000000", "000111000", > 84 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 85 return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1 > 86 int Test_(Case_<2>) { > 87 int s = 3; > 88 int N = 3; > 89 int K = 1; > 90 int r1 = 4; > 91 int r2 = 11; > 92 int c1 = 5; > 93 int c2 = 10; > 94 string RetVal_[] = {"101001", "100000", "000000", "001001", "000000", "0 > 95 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 96 return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1 > 97 int Test_(Case_<3>) { > 98 int s = 2; > 99 int N = 8; > 100 int K = 4; > 101 int r1 = 56; > 102 int r2 = 61; > 103 int c1 = 33; > 104 int c2 = 56; > 105 string RetVal_[] = {"000000000000000000000000", "00000000000000000000000 > 106 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 107 return verify_case(RetVal, PlaneFractal().getPattern(s, N, K, r1, r2, c1 > 108 > 109 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 110 template<> void Run_<-1>() {} > 111 int main() { Run_<0>(); } > 112 // END CUT HERE > 113
Added SRM/TCO9-2/1B.cpp version [5c2f54954f3e8b6d]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<int> CMP; > 21 > 22 double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } > 23 double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } > 24 > 25 int ccw(const CMP& a, CMP b, CMP c) { > 26 b -= a; c -= a; > 27 if( outer_prod(b,c) > 0 ) return +1; // counter clockwise > 28 if( outer_prod(b,c) < 0 ) return -1; // clockwise > 29 if( inner_prod(b,c) < 0 ) return +2; // c--a--b on line > 30 if( norm(b) < norm(c) ) return -2; // a--b--c on line > 31 return 0; > 32 } > 33 > 34 bool byX( const CMP& a, const CMP& b ) { > 35 if( a.real() != b.real() ) > 36 return a.real() < b.real(); > 37 return a.imag() < b.imag(); > 38 } > 39 > 40 vector<CMP> convex_hull( vector<CMP> p ) > 41 { > 42 //#define IS_RIGHT <0 // skip on-line verts > 43 #define IS_RIGHT ==-1 // take all > 44 > 45 sort(p.begin(), p.end(), &byX); > 46 > 47 vector<CMP> ans; > 48 for(int i=0; i<p.size(); ans.push_back(p[i++])) // left-to-right > 49 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 50 ans.pop_back(); > 51 if( ans.size() == p.size() ) > 52 return ans; > 53 for(int i=p.size()-2; i>=0; ans.push_back(p[i--])) // right-to-left > 54 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 55 ans.pop_back(); > 56 ans.pop_back(); > 57 return ans; > 58 } > 59 > 60 int area(const CMP& a, const CMP& b, const CMP& c) > 61 { > 62 return (int) abs( outer_prod(b-a,c-a) ); > 63 } > 64 > 65 class ExtendableTriangles { > 66 public: > 67 int getCount(vector <string> grid) > 68 { > 69 vector<CMP> r, g, b; > 70 for(int y=0; y<grid.size(); ++y) > 71 for(int x=0; x<grid[0].size(); ++x) > 72 (grid[y][x]=='R' ? r : grid[y][x]=='G' ? g : b). > 73 > 74 int cnt = r.size() * g.size() * b.size(); > 75 > 76 r = convex_hull(r); > 77 g = convex_hull(g); > 78 b = convex_hull(b); > 79 > 80 bool (*ext)[200][200] = (bool(*)[200][200])calloc(200*200*200, 1 > 81 for(int i=0; i<r.size(); ++i) > 82 for(int j=0; j<g.size(); ++j) > 83 { > 84 int ma = -1; > 85 for(int k=0; k<b.size(); ++k) > 86 ma = max(ma, area(r[i], g[j], b[k])); > 87 for(int k=0; k<b.size(); ++k) > 88 if( area(r[i], g[j], b[k]) < ma ) > 89 ext[i][j][k] = true; > 90 } > 91 for(int j=0; j<g.size(); ++j) > 92 for(int k=0; k<b.size(); ++k) > 93 { > 94 int ma = -1; > 95 for(int i=0; i<r.size(); ++i) > 96 ma = max(ma, area(r[i], g[j], b[k])); > 97 for(int i=0; i<r.size(); ++i) > 98 if( area(r[i], g[j], b[k]) < ma ) > 99 ext[i][j][k] = true; > 100 } > 101 for(int k=0; k<b.size(); ++k) > 102 for(int i=0; i<r.size(); ++i) > 103 { > 104 int ma = -1; > 105 for(int j=0; j<g.size(); ++j) > 106 ma = max(ma, area(r[i], g[j], b[k])); > 107 for(int j=0; j<g.size(); ++j) > 108 if( area(r[i], g[j], b[k]) < ma ) > 109 ext[i][j][k] = true; > 110 } > 111 for(int i=0; i<r.size(); ++i) > 112 for(int j=0; j<g.size(); ++j) > 113 for(int k=0; k<b.size(); ++k) > 114 if( !ext[i][j][k] ) > 115 --cnt; > 116 free(ext); > 117 return cnt; > 118 } > 119 }; > 120 > 121 // BEGIN CUT HERE > 122 #include <ctime> > 123 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 124 > 125 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 126 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 127 > 128 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 129 char Test_(...); > 130 > 131 int Test_(Case_<0>) { > 132 string grid_[] = {"RGB"}; > 133 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 134 int RetVal = 0; > 135 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 136 int Test_(Case_<1>) { > 137 string grid_[] = {"RGB", > 138 "RGB"}; > 139 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 140 int RetVal = 6; > 141 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 142 int Test_(Case_<2>) { > 143 string grid_[] = {"RRRRRRRR", > 144 "GGGGBBBB", > 145 "RRRRRRRR"}; > 146 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 147 int RetVal = 240; > 148 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 149 int Test_(Case_<3>) { > 150 string grid_[] = {"RBRBRBRB", > 151 "BRBRBRBR", > 152 "RBRBRBRB", > 153 "BRBRBRBR", > 154 "RRRRRRRR", > 155 "BBBBBBBB", > 156 "RRRRBBBB", > 157 "BBBBRRRR"}; > 158 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 159 int RetVal = 0; > 160 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 161 int Test_(Case_<4>) { > 162 string grid_[] = {"RGB", > 163 "RBG", > 164 "GRB", > 165 "GBR", > 166 "BRG", > 167 "BGR"}; > 168 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 169 int RetVal = 208; > 170 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 171 int Test_(Case_<5>) { > 172 string grid_[] = {"BBRRBBGRBG"}; > 173 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 174 int RetVal = 0; > 175 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 176 int Test_(Case_<6>) { > 177 string grid_[] = {"RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR", > 178 vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); > 179 int RetVal = 565039154; > 180 return verify_case(RetVal, ExtendableTriangles().getCount(grid)); } > 181 > 182 > 183 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 184 template<> void Run_<-1>() {} > 185 int main() { Run_<0>(); } > 186 // END CUT HERE > 187
Added SRM/TCO9-2/1C.cpp version [f35e47b64486be8c]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 static const int NV = 128; > 23 typedef int flow; > 24 typedef int vert; > 25 typedef vert edge; > 26 typedef vector<edge> edges; > 27 typedef vector<edges> graph; > 28 typedef flow flow_graph[NV][NV]; > 29 > 30 flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, > 31 int LV[], flow flow_in, int blocked[] ) > 32 { > 33 flow flow_out = 0; > 34 for(int i=0; i!=G[v].size(); ++i) { > 35 int u = G[v][i]; > 36 if( LV[v]+1==LV[u] && F[v][u] ) { > 37 flow f = min(flow_in-flow_out, F[v][u]); > 38 if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blo > 39 F[v][u] -= f; > 40 F[u][v] += f; > 41 flow_out += f; > 42 if( flow_in == flow_out ) return flow_out; > 43 } > 44 } > 45 } > 46 blocked[v] = (flow_out==0); > 47 return flow_out; > 48 } > 49 > 50 flow maxFlow( graph& G, flow_graph F, vert S, vert D ) > 51 { > 52 for( flow total=0 ;; ) { > 53 int LV[NV] = {0}; > 54 vector<int> Q(1, S); > 55 for(int lv=1; !Q.empty(); ++lv) { > 56 vector<int> Q2; > 57 for(int i=0; i!=Q.size(); ++i) { > 58 edges& ne = G[Q[i]]; > 59 for(int j=0; j!=ne.size(); ++j) > 60 if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j > 61 LV[ne[j]]=lv, Q2.push_back(ne[j] > 62 } > 63 Q.swap(Q2); > 64 } > 65 > 66 if( !LV[D] ) > 67 return total; > 68 > 69 int blocked[NV] = {}; > 70 total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); > 71 } > 72 } > 73 > 74 flow_graph F, XXF; > 75 > 76 class ConnectingAirports { > 77 public: > 78 vector <string> getSchedule(vector <int> capacityA, vector <int> capacit > 79 { > 80 int NA = capacityA.size(); > 81 int NB = capacityB.size(); > 82 int CAsum = accumulate(capacityA.begin(), capacityA.end(), 0); > 83 int CBsum = accumulate(capacityB.begin(), capacityB.end(), 0); > 84 if( CAsum != CBsum ) > 85 return vector<string>(); > 86 > 87 graph G(2+NA+NB); > 88 memset(XXF, 0, sizeof XXF); > 89 for(int i=0; i<capacityA.size(); ++i) { > 90 G[0].push_back( 2+i ); > 91 G[2+i].push_back( 0 ); > 92 XXF[0][2+i] = capacityA[i]; > 93 } > 94 for(int i=0; i<capacityB.size(); ++i) { > 95 G[2+NA+i].push_back( 1 ); > 96 G[1].push_back( 2+NA+i ); > 97 XXF[2+NA+i][1] = capacityB[i]; > 98 } > 99 for(int i=0; i<capacityA.size(); ++i) > 100 for(int j=0; j<capacityB.size(); ++j) { > 101 G[2+i].push_back( 2+NA+j ); > 102 G[2+NA+j].push_back( 2+i ); > 103 XXF[2+i][2+NA+j] = 1; > 104 } > 105 > 106 memcpy(F, XXF, sizeof F); > 107 flow mf = maxFlow(G,F,0,1); > 108 if( mf != CAsum ) > 109 return vector<string>(); > 110 > 111 // find lex-best > 112 int rest = CAsum; > 113 vector<string> ans(NA, string(NB, '0')); > 114 for(int i=0; i<capacityA.size(); ++i) > 115 for(int j=0; j<capacityB.size(); ++j) > 116 { > 117 if( rest==0 ) > 118 return ans; > 119 > 120 memcpy(F, XXF, sizeof F); > 121 bool toUse = true; > 122 > 123 F[2+i][2+NA+j] --; > 124 flow mf = maxFlow(G,F,0,1); > 125 toUse = (mf < rest); > 126 > 127 ans[i][j] = toUse ? '1' : '0'; > 128 if( toUse ) { > 129 XXF[0][2+i] --; > 130 XXF[2+NA+j][1] --; > 131 rest--; > 132 } > 133 XXF[2+i][2+NA+j] --; > 134 } > 135 return ans; > 136 } > 137 }; > 138 > 139 // BEGIN CUT HERE > 140 #include <ctime> > 141 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 142 > 143 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 144 int verify_case(const vector <string> &Expected, const vector <string> &Received > 145 > 146 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 147 char Test_(...); > 148 int Test_(Case_<0>) { > 149 int capacityA_[] = {1,2,3}; > 150 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 151 int capacityU_[] = {3,1,2}; > 152 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 153 string RetVal_[] = {"100", "101", "111" }; > 154 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 155 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 156 int Test_(Case_<1>) { > 157 int capacityA_[] = {3,2,1,1}; > 158 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 159 int capacityU_[] = {1,3,1,2}; > 160 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 161 string RetVal_[] = {"0111", "0101", "0100", "1000" }; > 162 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 163 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 164 int Test_(Case_<2>) { > 165 int capacityA_[] = {1,2,3,4}; > 166 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 167 int capacityU_[] = {5,6,7,8}; > 168 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 169 vector <string> RetVal; > 170 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 171 int Test_(Case_<3>) { > 172 int capacityA_[] = {47,47}; > 173 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 174 int capacityU_[] = {47,40,7}; > 175 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 176 vector <string> RetVal; > 177 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 178 int Test_(Case_<4>) { > 179 int capacityA_[] = {5,5}; > 180 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 181 int capacityU_[] = {1,1,2,1,1,1,1,1,1}; > 182 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 183 string RetVal_[] = {"001001111", "111110000" }; > 184 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 185 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 186 int Test_(Case_<5>) { > 187 int capacityA_[] = {0,0,0,0}; > 188 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 189 int capacityU_[] = {0,0,0,0,0,0}; > 190 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 191 string RetVal_[] = {"000000", "000000", "000000", "000000" }; > 192 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 193 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 194 int Test_(Case_<6>) { > 195 int capacityA_[] = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,5 > 196 vector <int> capacityA(capacityA_, capacityA_+sizeof(capacityA_)/sizeo > 197 int capacityU_[] = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,5 > 198 vector <int> capacityU(capacityU_, capacityU_+sizeof(capacityU_)/sizeo > 199 string RetVal_[] = {"000000", "000000", "000000", "000000" }; > 200 vector <string> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal > 201 return verify_case(RetVal, ConnectingAirports().getSchedule(capacityA, c > 202 > 203 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 204 template<> void Run_<-1>() {} > 205 int main() { Run_<0>(); } > 206 // END CUT HERE > 207
Added SRM/TCO9-3-U/1A.cpp version [7f6a0aa40df322bf]
> 1 #include <iostream> > 2 #include <sstream> > 3 #include <iomanip> > 4 #include <vector> > 5 #include <string> > 6 #include <map> > 7 #include <set> > 8 #include <algorithm> > 9 #include <numeric> > 10 #include <iterator> > 11 #include <functional> > 12 #include <complex> > 13 #include <queue> > 14 #include <stack> > 15 #include <cmath> > 16 #include <cassert> > 17 #include <cstring> > 18 using namespace std; > 19 typedef long long LL; > 20 typedef complex<double> CMP; > 21 > 22 class SaveTheTrees { > 23 public: > 24 int minimumCut(vector <int> x, vector <int> y, vector <int> h) > 25 { > 26 int N = x.size(); > 27 int ans = N-1; > 28 for(int i1=0; i1<N; ++i1) > 29 for(int i2=0; i2<N; ++i2) > 30 for(int k1=0; k1<N; ++k1) > 31 for(int k2=0; k2<N; ++k2) { > 32 int mx = min(x[i1], x[i2]); > 33 int Mx = max(x[i1], x[i2]); > 34 int my = min(y[k1], y[k2]); > 35 int My = max(y[k1], y[k2]); > 36 > 37 int hh[40], q=0; > 38 int cut = 0, need = ((Mx-mx) + (My-my))*2; > 39 for(int j=0; j<N; ++j) > 40 if( mx<=x[j] && x[j]<=Mx && my<=y[j] && y[j]<=My > 41 hh[q++] = h[j]; > 42 else { > 43 cut++, need-=h[j]; > 44 if(cut>=ans) goto next; > 45 } > 46 if( need > 0 ) > 47 { > 48 sort( &hh[0], &hh[q], greater<int>() ); > 49 // partial_sum( &hh[0], &hh[q], &hh[0] ); > 50 // cut += lower_bound( &hh[0], &hh[q], need ) - &hh > 51 for(int p=0; p<q; ++p) { > 52 ++cut, need -= hh[p]; > 53 if(cut>=ans) goto next; > 54 if(need <= 0) break; > 55 } > 56 } > 57 ans = cut; > 58 next:; > 59 } > 60 return ans; > 61 } > 62 }; > 63 > 64 // BEGIN CUT HERE > 65 #include <ctime> > 66 double start_time;string timer() { ostringstream os; os << " (" << int((clock()- > 67 > 68 template <typename T> string print_array(const vector<T> &V) { ostringstream os; > 69 int verify_case(const int &Expected, const int &Received) { if (Expected == Rece > 70 > 71 template<int N> struct Case_ { Case_(){start_time=clock();} }; > 72 char Test_(...); > 73 int Test_(Case_<0>) { > 74 int x_[] = {1, 2, 8, 9, 5}; > 75 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 76 int y_[] = {1, 8, 2, 9, 5}; > 77 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 78 int h_[] = {1, 1, 1, 1, 32}; > 79 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 80 int RetVal = 1; > 81 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 82 int Test_(Case_<1>) { > 83 int x_[] = {1, 2, 8, 9, 5}; > 84 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 85 int y_[] = {1, 8, 2, 9, 5}; > 86 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 87 int h_[] = {1, 1, 1, 1, 27}; > 88 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 89 int RetVal = 2; > 90 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 91 int Test_(Case_<2>) { > 92 int x_[] = {1, 2, 8, 9, 5}; > 93 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 94 int y_[] = {1, 8, 2, 9, 5}; > 95 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 96 int h_[] = {4, 4, 4, 4, 4}; > 97 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 98 int RetVal = 3; > 99 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 100 int Test_(Case_<3>) { > 101 int x_[] = {1, 2, 8, 9, 5}; > 102 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 103 int y_[] = {1, 8, 2, 9, 5}; > 104 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 105 int h_[] = {3, 3, 3, 3, 3}; > 106 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 107 int RetVal = 4; > 108 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 109 int Test_(Case_<4>) { > 110 int x_[] = {3, 10, 6, 2, 8, 7, 5, 4, 9, 1}; > 111 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 112 int y_[] = {4, 10, 6, 9, 8, 7, 3, 2, 5, 1}; > 113 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 114 int h_[] = {4, 2, 2, 4, 4, 5, 4, 4, 2, 3}; > 115 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 116 int RetVal = 5; > 117 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 118 int Test_(Case_<5>) { > 119 int x_[] = {55, 67, 100, 38, 80, 98, 47, 58, 61, 33}; > 120 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 121 int y_[] = {87, 17, 20, 7, 57, 19, 23, 68, 27, 39}; > 122 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 123 int h_[] = {6, 2, 8, 8, 31, 25, 23, 19, 45, 4}; > 124 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 125 int RetVal = 6; > 126 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 127 int Test_(Case_<6>) { > 128 int x_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 129 vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); > 130 int y_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23, > 131 vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_)); > 132 int h_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, > 133 vector <int> h(h_, h_+sizeof(h_)/sizeof(*h_)); > 134 int RetVal = 6; > 135 return verify_case(RetVal, SaveTheTrees().minimumCut(x, y, h)); } > 136 > 137 template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test > 138 template<> void Run_<-1>() {} > 139 int main() { Run_<0>(); } > 140 // END CUT HERE > 141
Added _lib/dataStructure/fenwickTree.cpp version [5a7c5bc1ec0fdf30]
> 1 //------------------------------------------------------------- > 2 // Fenwick Tree > 3 // O(log N) per each operation > 4 // > 5 // Verified by > 6 // - SRM424 Div1 LV3 > 7 //------------------------------------------------------------- > 8 > 9 template<typename T = LL> > 10 struct FenwickTree > 11 { > 12 vector<T> x; > 13 FenwickTree(size_t n, const T& v = T()) : x(n, v) {} > 14 > 15 void incr( int k, const T& a ) { // z[k] += a; > 16 for(; k < x.size(); k|=k+1) > 17 x[k] += a; > 18 } > 19 > 20 T sum(int i, int j) { // z[i]+...+z[j] : inclusive > 21 if(i) > 22 return sum(0, j) - sum(0, i-1); > 23 else { > 24 T v = T(); > 25 for(; j>=0; j=(j&(j+1))-1) > 26 v += x[j]; > 27 return v; > 28 } > 29 } > 30 };
Added _lib/dataStructure/rmq.cpp version [a0dd84df47a73891]
> 1 //------------------------------------------------------------- > 2 // Range Minimum Query > 3 // O(N log N) construction > 4 // O(1) per each query > 5 // returns the LEFTMOST/ALL minimum index (I hope) > 6 // > 7 // Verified by > 8 // - SRM337 Div1 LV2 > 9 // - SRM431 Div1 LV3 > 10 //------------------------------------------------------------- > 11 > 12 template<typename T> > 13 struct RMQ > 14 { > 15 vector< vector<int> > rm; > 16 vector<T> d; > 17 > 18 RMQ( const vector<T>& d ) : d(d) { > 19 int n = d.size(); > 20 > 21 // rm[k][x] = i s.t. d[i] is the minimum in [x, x+2^k) > 22 rm.push_back( vector<int>(n) ); > 23 for(int x=0; x<n; ++x) > 24 rm[0][x] = x; > 25 for(int k=1; (1<<k)<=n; ++k) { > 26 rm.push_back( rm[k-1] ); > 27 for(int x=0; x+(1<<k-1)<n; ++x) > 28 if( d[rm[k][x]] > d[rm[k-1][x + (1<<k-1)]] ) > 29 rm[k][x] = rm[k-1][x + (1<<k-1)]; > 30 } > 31 } > 32 > 33 int operator()(int L, int R) const { // inclusive > 34 int k=0; > 35 for(; L+(1<<k) < R-(1<<k)+1; ++k) {} > 36 LL i = rm[k][L]; > 37 LL j = rm[k][R-(1<<k)+1]; > 38 return (d[i]<=d[j] ? i : j); > 39 } > 40 > 41 vector<int> all(int L, int R) const { > 42 vector<int> ans; > 43 int minValue = d[(*this)(L, R)]; > 44 while( L <= R ) { > 45 int C = (*this)(L, R); > 46 if( minValue < d[C] ) > 47 break; > 48 ans.push_back(C); > 49 L = C+1; > 50 } > 51 return ans; > 52 } > 53 };
Added _lib/doc/hitori-sharp.pdf version [7c589d0ad4748936]
cannot compute difference between binary files
Added _lib/doc/lb_ub.txt version [6993ff5a52ea6cbc]
> 1 lb = lower_bound(k) > 2 ... k-1] lb [k ... > 3 > 4 > 5 ub = upper_bound(k) > 6 .., k] ub [k+1 ... > 7 > 8 > 9 ãªã®ã§ä»»æ„ã®é–‹é–‰åŒºé–“をイテレータ㮠[) 区間ã«ç›´ã™ã«ã¯ > 10 > 11 [A, B] > 12 = [ lower_bound(A), upper_bound(B) ) > 13 > 14 [A, B) > 15 = [ lower_bound(A), lower_bound(B) ) > 16 > 17 (A, B] > 18 = [ upper_bound(A), upper_bound(B) ) > 19 > 20 (A, B) > 21 = [ upper_bound(A), lower_bound(B) )
Added _lib/doc/nim.txt version [d63874d4c871cfe6]
> 1 // SRM338 Div1 LV3 > 2 > 3 定ç†ã®ã‚¹ãƒ†ãƒ¼ãƒˆãƒ¡ãƒ³ãƒˆ > 4 > 5 対象ã¨ãªã‚‹ã‚²ãƒ¼ãƒ > 6 - 二人ゲーム> 7 - 二人ã¨ã‚‚å‹•ã‹ã›ã‚‹æ‰‹ã®é›†åˆã¯åŒã˜ > 8 - å‹•ã‹ã›ãªããªã£ãŸæ–¹ãŒè² ã‘ > 9 - ç„¡é™ãƒ«ãƒ¼ãƒ—ã—ãªã„ > 10 > 11 nim > 12 - サイズ s1, s2, .., sk ã®å±±ãŒã‚ã‚‹ > 13 - ã©ã‚Œã‹ä¸€ã¤å±±ã‚’é¸ã‚“㧠1 ~ 山サイズ ã®ä»»æ„個å–り除ã‘ã‚‹ > 14 - 最後ã®å±±ã‚’å–ã£ãŸæ–¹ãŒå‹ã¡ï¼ˆå±±ãŒãªããªã£ã¦æ‰“ã¤æ‰‹ãŒç„¡ããªã£ãŸæ–¹ãŒè² ã‘) > 15 > 16 *n をサイズ n ã®å±±ãŒ1個ã ã‘ã® nim ã¨ã™ã‚‹ > 17 - *0 ã¯è² ã‘ > 18 - *n ã¯å‹ã¡ n>=1 > 19 > 20 ゲーム> 21 - 状態 G ã‹ã‚‰æ‰“ã¦ã‚‹æ‰‹ã§é·ç§»ã™ã‚‹å…ˆã‚’ G1, ..., Gk ã¨ã™ã‚‹ > 22 G = {G1, ..., Gk} ã¨æ›¸ã > 23 > 24 ç‰ä¾¡ > 25 - 二ã¤ã®ã‚²ãƒ¼ãƒ G, FãŒç‰ä¾¡ãªã“ã¨ã‚’ G ≡ F ã¨æ›¸ã > 26 ç‰ä¾¡ã¨ã„ã†ã®ã¯å‹ã¡è² ã‘一致よりもã£ã¨å¼·ã„。二ã¤ã®ã‚²ãƒ¼ãƒ ã®å’Œã‚’å–ã‚‹ã¨å¿…æ•—ã«ãªã‚‹ã“㨠> 27 *n 㨠*n ã¯åŒã‚µã‚¤ã‚ºã®å±±2ã¤ã®nimã¯å¿…æ•—ãªã®ã§ç‰ä¾¡ã€‚åŒã‚µã‚¤ã‚ºã§ãªã‘ã‚Œã°å¿…å‹ãªã®ã§ç‰ä¾¡ã§ãªã„ > 28 > 29 å®šç† > 30 - G1≡*n1, ..., Gk≡*nk ãªã‚‰ã°ã€G={G1, ..., Gk} ≡ *mex(n1,...,nk) > 31 where mex(S) = min(nat \setminus S) > 32 帰ç´çš„ã«ã€å…¨ã¦ã®ã‚²ãƒ¼ãƒ ã¯ãªã‚“らã‹ã® *n ã¨ç‰ä¾¡ > 33 > 34 > 35 ãŠã¾ã‘ > 36 @G ã‚’ G ã®nim値ã¨ã™ã‚‹ã€‚ã¤ã¾ã‚ŠG≡*@G > 37 G = X + Y ãªã‚‰ã° @G = @X xor @Y > 38 å±±ãŒè¤‡æ•°ã®nimã®å‹æ•—ã¯å…¨éƒ¨xorã¨ã£ãŸ1ã¤å±±ã®nimã«ç‰ã—ã„ã®ã¨åŒã˜åŽŸç†ã€‚
Added _lib/gcj/!gcj-smpl.aml version [9caa644d5228a4f9]
> 1 (**************************************************************************** > 2 * Written in > 3 * Alice 1.4 > 4 * http://www.ps.uni-saarland.de/alice/ > 5 ****************************************************************************) > 6 > 7 fun solve (n : int, k : int) : bool = > 8 let > 9 open Word > 10 infix 5 << andb > 11 val mask = (0wx1 << fromInt n) - 0wx1 > 12 in > 13 (fromInt k) andb mask = mask > 14 end > 15 > 16 fun main () = > 17 let > 18 fun readLine () = > 19 case TextIO.inputLine TextIO.stdIn of > 20 | NONE => [] > 21 | SOME(s) => map Int.fromString (String.tokens (fn x => > 22 > 23 fun parseOne c = > 24 case readLine() of > 25 | [SOME n, SOME k] => (c, (n, k)) > 26 | _ => assert false > 27 > 28 fun spawn solveOne (c, inp) = > 29 (c, solve inp) > 30 > 31 fun printOne (c, ans) = > 32 print ("Case #" ^ Int.toString (c+1) ^ ": " ^ (if ans th > 33 in > 34 case readLine () of > 35 | [SOME t] => List.app printOne (List.map solveOne (List.tabulat > 36 | _ => assert false > 37 end > 38 > 39 do main () > 40 do OS.Process.exit 0
Added _lib/gcj/!gcj-smpl.as version [a762a8ed088b0770]
> 1 ///////////////////////////////////////////////////////////////////////////// > 2 // Written in > 3 // ActionScript (using mtasc 1.14) > 4 // http://www.mtasc.org/ > 5 ///////////////////////////////////////////////////////////////////////////// > 6 > 7 class A > 8 { > 9 static function main(mc) > 10 { > 11 var input_string = "<paste the input data here>"; > 12 var input = input_string.split("\n"); > 13 var T = Number(input[0]); > 14 > 15 var output_string = "" > 16 for(var C=1; C<=T; ++C) > 17 { > 18 var theCase = input[C].split(" "); > 19 var N = Number(theCase[0]); > 20 var K = Number(theCase[1]); > 21 output_string += "Case #" + C + ": " + (solve(N,K)?"ON": > 22 } > 23 > 24 _root.createTextField("tf",0,0,0,800,600); > 25 _root.tf.text = output_string; > 26 } > 27 > 28 static function solve(N, K) > 29 { > 30 var mask = (1<<N) - 1; > 31 return (K & mask) == mask; > 32 } > 33 }
Added _lib/gcj/!gcj-smpl.bl version [e21b6366bf8728d9]
> 1 ############################################################################# > 2 # Written in > 3 # Blue 1.7.5 > 4 # http://www.lechak.info/blue/ > 5 ############################################################################# > 6 > 7 global FROM_STRING = func { > 8 arg s; > 9 > 10 a = []; > 11 i = 0; > 12 loop { > 13 a = a.append( s.substr(i,i+1).num() ); > 14 (i=i+1) >= s.length() ? return; > 15 }; > 16 return a; > 17 }; > 18 > 19 global TO_STRING = func { > 20 arg x; > 21 lexical s = ""; > 22 x.map( func{ s = s + this; } ); > 23 return s; > 24 }; > 25 > 26 global LESS = func { > 27 arg x; > 28 arg y; > 29 > 30 xl = x.length(); > 31 yl = y.length(); > 32 (xl < yl) ? return 1; > 33 (xl > yl) ? return 0; > 34 i = 0; > 35 return loop { > 36 (i == xl) ? return 0; > 37 (x[i] < y[i]) ? return 1; > 38 (x[i] > y[i]) ? return 0; > 39 i = i+1; > 40 }; > 41 }; > 42 > 43 global SUB = func { > 44 arg x; > 45 arg y; > 46 > 47 x.length() > y.length() ? (y = [].resize(x.length()-y.length(),0).merge( > 48 z = [].resize(x.length(), 0); > 49 i = x.length()-1; > 50 carry = 0; > 51 loop { > 52 z[i] = x[i] - y[i] - carry; > 53 carry = z[i] < 0 ? (z[i]=z[i]+10; 1) : 0; > 54 (i=i-1) < 0 ? return; > 55 }; > 56 head = 0; > 57 loop { > 58 head == z.length()-1 ? return; > 59 z[head] != 0 ? return; > 60 head = head + 1; > 61 }; > 62 return z.slice(head, z.length()); > 63 }; > 64 > 65 global DBL = func { > 66 arg x; > 67 > 68 z = [].resize(x.length(), 0); > 69 i = x.length()-1; > 70 carry = 0; > 71 loop { > 72 z[i] = x[i] + x[i] + carry; > 73 carry = z[i] > 9 ? (z[i]=z[i]-10; 1) : 0; > 74 ((i=i-1) < 0) ? return; > 75 }; > 76 return (carry==1 ? [1].merge(z) : z); > 77 }; > 78 > 79 global DIF = func { > 80 arg x; > 81 arg y; > 82 return LESS(x, y) ? SUB(y, x) : SUB(x, y); > 83 }; > 84 > 85 global MOD = func { > 86 arg x; > 87 arg y; > 88 > 89 LESS(x,y) ? return x; > 90 z = MOD(x, DBL(y)); > 91 LESS(z,y) ? return z; > 92 return SUB(z,y); > 93 }; > 94 > 95 global ISZERO = func { > 96 arg x; > 97 return x[0] == 0; > 98 }; > 99 > 100 global GCD = func { > 101 arg x; > 102 arg y; > 103 return ISZERO(x) ? y : GCD(MOD(y,x),x); > 104 }; > 105 > 106 ############################################################################# > 107 > 108 solve = func { > 109 arg N; > 110 arg t; > 111 t = t.map( func{return FROM_STRING(this);} ); > 112 > 113 lexical t0 = t[0]; > 114 lexical g = DIF(t0, t[1]); > 115 t.slice(2, t.length()).map(func{ > 116 g = GCD(g, DIF(t0, this)); > 117 }); > 118 r = MOD(t[0], g); > 119 return TO_STRING( ISZERO(r) ? r : DIF(g,r) ); > 120 }; > 121 > 122 input = sys.library("streams").stdio().read(1000000000).split("\n").map(func{ret > 123 C = input[0][0].num(); > 124 > 125 CaseID = (args.length() >= 2 ? args[1].num() : 1); > 126 loop { > 127 "Case #".print(); > 128 CaseID.print(); > 129 ": ".print(); > 130 solve( input[CaseID][0].num(), input[CaseID].slice(1,input[CaseID].lengt > 131 "\n".print(); > 132 ((CaseID = CaseID+1) > C) ? return; > 133 };
Added _lib/gcj/!gcj-smpl.cl version [5615f7e126fc9cd7]
> 1 ///////////////////////////////////////////////////////////////////////////// > 2 // Writte in > 3 // Claire v3.3.37 > 4 // http://www.claire-language.com/ > 5 ///////////////////////////////////////////////////////////////////////////// > 6 > 7 D :: 1000000000 > 8 > 9 long <: ephemeral_object(hi:integer, lo:integer) > 10 > 11 [long!(x: integer) : long -> > 12 long(hi = x / D, lo = x mod D) > 13 ] > 14 > 15 [+(x:long, y:long) : long -> > 16 let lolo := x.lo - D + y.lo in > 17 ( > 18 if ( lolo < 0 ) > 19 long(hi = x.hi + y.hi, lo = lolo + D) > 20 else > 21 long(hi = x.hi + y.hi + 1, lo = lolo) > 22 ) > 23 ] > 24 > 25 [-(x:long, y:long) : long -> > 26 let lolo := x.lo - y.lo in > 27 ( > 28 if ( lolo < 0 ) > 29 long(hi = x.hi - y.hi - 1, lo = lolo + D) > 30 else > 31 long(hi = x.hi - y.hi, lo = lolo) > 32 ) > 33 ] > 34 > 35 [*(x:long, y:integer) : long -> > 36 let r := long!(0) in > 37 let c := x in > 38 (while (y > 0) ( > 39 (if (y mod 2 = 1) r :+ c), > 40 c := c + c, > 41 y :/ 2 > 42 ), > 43 r) > 44 ] > 45 > 46 [+(x:long, y:integer) : long -> > 47 x + long!(y) > 48 ] > 49 > 50 [<(x:long, y:long) : boolean -> > 51 if (x.hi < y.hi) > 52 true > 53 else if (x.hi > y.hi) > 54 false > 55 else > 56 x.lo < y.lo > 57 ] > 58 [>=(x:long, y:long) : boolean -> not(x < y)] > 59 [<=(x:long, y:long) : boolean -> not(y < x)] > 60 [>(x:long, y:long) : boolean -> (y < x)] > 61 > 62 [>=(x:long, y:integer) : boolean -> not(x < long!(y))] > 63 [<=(x:long, y:integer) : boolean -> not(long!(y) < x)] > 64 [<(x:long, y:integer) : boolean -> (x < long!(y))] > 65 [>(x:long, y:integer) : boolean -> (long!(y) < x)] > 66 > 67 [>=(x:integer, y:long) : boolean -> not(long!(x) < y)] > 68 [<=(x:integer, y:long) : boolean -> not(y < long!(x))] > 69 [<(x:integer, y:long) : boolean -> (long!(x) < y)] > 70 [>(x:integer, y:long) : boolean -> (y < long!(x))] > 71 > 72 > 73 [string!(x: long) : string -> > 74 if (x.hi > 0) > 75 let los := string!(x.lo) in > 76 string!(x.hi) /+ make_string(9 - length(los), '0') /+ lo > 77 else > 78 string!(x.lo) > 79 ] > 80 > 81 ///////////////////////////////////////////////////////////////////////////// > 82 > 83 [readLine() : list<integer> -> > 84 list<integer>{integer!(s) | s in explode(freadline(stdin), " ")} > 85 ] > 86 > 87 [solve(R:integer, k:integer, N:integer, g:list<integer>) -> > 88 let dest := make_list(N, 0) in > 89 let earn := make_list(N, long!(0)) in > 90 ( > 91 for s in (0 .. N - 1) ( > 92 let ride := long!(0) in > 93 let i := 0 in > 94 ( > 95 while (i < N) > 96 ( > 97 let ride2 := ride + g[((s + i) mod N) + > 98 (if (ride2 > k) > 99 break() > 100 else > 101 (ride := ride2, i :+ 1)) > 102 ), > 103 earn[s + 1] := ride, > 104 dest[s + 1] := ((s + i) mod N) + 1 > 105 ) > 106 ), > 107 let firstVisitTime := make_list(N, -1) in > 108 let firstVisitEarn := make_list(N, long!(0)) in > 109 let q := 1 in > 110 let totalEarn := long!(0) in > 111 let i := 0 in > 112 ( > 113 (while (i < R) ( > 114 if (firstVisitTime[q] = -1) > 115 ( > 116 firstVisitTime[q] := i, > 117 firstVisitEarn[q] := totalEarn, > 118 totalEarn :+ earn[q], > 119 q := dest[q], > 120 i :+ 1 > 121 ) > 122 else > 123 ( > 124 let loopSize := i - firstVisitTime[q] in > 125 let loopEarn := totalEarn - firstVisitEa > 126 let rest := R - i in > 127 ( > 128 totalEarn :+ loopEarn * (rest / > 129 // clear > 130 firstVisitTime := make_list(N, - > 131 i := R - (rest mod > 132 ) > 133 ) > 134 )), > 135 princ(string!(totalEarn)) > 136 ) > 137 ) > 138 ] > 139 > 140 [main() -> > 141 let T := readLine()[1] in > 142 for C in (1 .. T) > 143 ( > 144 printf("Case #~A: ", C), > 145 let RkN := readLine() in solve(RkN[1], RkN[2], RkN[3], r > 146 princ("\n") > 147 ) > 148 ] > 149 > 150 (main())
Added _lib/gcj/!gcj-smpl.cs version [2cc6fccce968278d]
> 1 using System; > 2 using System.Linq; > 3 > 4 class C > 5 { > 6 static void Main() > 7 { > 8 long[] Ts = readLongArray(); > 9 long T = Ts[0]; > 10 > 11 for(long C=1; C<=T; ++C) > 12 { > 13 long[] RkN = readLongArray(); > 14 long R = RkN[0]; > 15 long k = RkN[1]; > 16 long N = RkN[2]; > 17 long[] g = readLongArray(); > 18 > 19 Console.WriteLine("Case #{0}: {1}", C, solveSmall(R,k,N, > 20 } > 21 } > 22 > 23 static long[] readLongArray() > 24 { > 25 return (from s in Console.ReadLine().Split(' ') select long.Pars > 26 } > 27 > 28 static long solveSmall(long R, long k, long N, long[] g) > 29 { > 30 long totalEarn = 0; > 31 > 32 for(int i=0; i<R; ++i) > 33 { > 34 long ride = 0; > 35 for(int q=0; q<g.Length; ++q) > 36 if( ride + g[q] > k ) > 37 { > 38 g = rotate(g, q); > 39 break; > 40 } > 41 else > 42 { > 43 ride += g[q]; > 44 } > 45 totalEarn += ride; > 46 } > 47 > 48 return totalEarn; > 49 } > 50 > 51 static long[] rotate(long[] g, int s) > 52 { > 53 long[] g2 = new long[g.Length]; > 54 for(int i=s; i<g.Length; ++i) > 55 g2[i-s] = g[i]; > 56 for(int i=0; i<s; ++i) > 57 g2[i+g.Length-s] = g[i]; > 58 return g2; > 59 } > 60 }
Added _lib/gcj/!gcj-smpl.vbs version [4dbab301b915fb0d]
> 1 ' > 2 ' Written in VBScript > 3 ' > 4 > 5 Function ReadLine() > 6 Dim s > 7 s = Split(WScript.StdIn.ReadLine, " ") > 8 For i = LBound(s) To UBound(s) > 9 s(i) = CLng(s(i)) > 10 Next > 11 ReadLine = s > 12 End Function > 13 > 14 Function Gcd(a, b) > 15 If a = 0 Then > 16 Gcd = b > 17 Else > 18 Gcd = Gcd(b mod a, a) > 19 End If > 20 End Function > 21 > 22 Function Solve(T) > 23 Dim g, r > 24 g = Abs(T(1) - T(2)) > 25 For i = 2 To UBound(T) > 26 g = Gcd(g, Abs(T(1) - T(i))) > 27 Next > 28 r = T(1) mod g > 29 If r = 0 Then > 30 Solve = 0 > 31 Else > 32 Solve = g - r > 33 End If > 34 End Function > 35 > 36 C = ReadLine()(0) > 37 For CaseID = 1 To C > 38 WScript.StdOut.WriteLine "Case #" & CaseID & ": " & Solve(ReadLine) > 39 Next
Added _lib/gcj/!gcj-tmpl.cpp version [8b7e23cf8b869fd6]
> 1 //----------------------------------------------------------------------------- > 2 // >>Code Template<< (for Visual C++) > 3 > 4 #include <iostream> > 5 #include <sstream> > 6 #include <iomanip> > 7 #include <string> > 8 #include <vector> > 9 #include <set> > 10 #include <map> > 11 #include <algorithm> > 12 #include <numeric> > 13 #include <iterator> > 14 #include <complex> > 15 #include <functional> > 16 #include <queue> > 17 #include <stack> > 18 #include <cmath> > 19 #include <cassert> > 20 #include <cstring> > 21 #define cout os > 22 using namespace std; > 23 typedef long long LL; > 24 typedef complex<double> CMP; > 25 void END_OF_INPUT_FOR_THIS_TEST_CASE(); // stub for multi-threading > 26 > 27 //----------------------------------------------------------------------------- > 28 // >>Main<< > 29 > 30 void case_main( ostream& os ) > 31 { > 32 END_OF_INPUT_FOR_THIS_TEST_CASE(); > 33 } > 34 > 35 //----------------------------------------------------------------------------- > 36 // >>Code Template<< (Multi-Thread Solver) > 37 > 38 #if 1 > 39 #undef cout > 40 #include <windows.h> > 41 #include <process.h> > 42 > 43 static const int THREAD_NUM = 2; > 44 volatile int g_id; > 45 int g_nCase; > 46 CRITICAL_SECTION g_cs; > 47 vector<string> g_output; > 48 > 49 unsigned __stdcall thread_main( void* t_id ) { > 50 for(;;) { > 51 EnterCriticalSection(&g_cs); > 52 const int id = ++g_id; > 53 if(id>g_nCase) { LeaveCriticalSection(&g_cs); break; } > 54 cerr << setw(4) << id << " @ " << (int)t_id << " start" << endl; > 55 > 56 ostringstream ss; > 57 ss << "Case #" << id << ": "; > 58 case_main( ss ); > 59 > 60 EnterCriticalSection(&g_cs); > 61 if(g_output.size()<id) g_output.resize(id); > 62 g_output[id-1] = ss.str(); > 63 cerr << setw(4) << id << " @ " << (int)t_id << " end" << endl; > 64 LeaveCriticalSection(&g_cs); > 65 } > 66 return 0; > 67 } > 68 > 69 void END_OF_INPUT_FOR_THIS_TEST_CASE() { LeaveCriticalSection(&g_cs); } > 70 > 71 int main() { > 72 cin >> g_nCase; > 73 string dummy; getline(cin, dummy); > 74 > 75 InitializeCriticalSection(&g_cs); > 76 vector<HANDLE> thread; > 77 for(int i=0; i<THREAD_NUM; ++i) > 78 thread.push_back( (HANDLE)_beginthreadex(0, 0, &thread_main, (vo > 79 WaitForMultipleObjects( thread.size(), &thread[0], TRUE, INFINITE ); > 80 DeleteCriticalSection(&g_cs); > 81 > 82 copy( g_output.begin(), g_output.end(), ostream_iterator<string>(cout) ) > 83 } > 84 > 85 //----------------------------------------------------------------------------- > 86 // >>Code Template<< (Single-Thread Solver) > 87 > 88 #else > 89 #undef cout > 90 void END_OF_INPUT_FOR_THIS_TEST_CASE() {} > 91 int main() { > 92 int nCase; cin >> nCase; > 93 string dummy; getline(cin, dummy); > 94 > 95 for(int id=1; id<=nCase; ++id) { > 96 cout << "Case #" << id << ": "; > 97 case_main( cout ); > 98 } > 99 } > 100 #endif
Added _lib/gcj/!gcj-tmpl.java version [e9cb1325b5300e50]
> 1 import java.io.*; > 2 import java.util.*; > 3 > 4 public class <CLASSNAME> > 5 { > 6 public static void main(String[] arg) > 7 { > 8 Scanner sc = new Scanner(System.in); > 9 int T = sc.nextInt(); > 10 for(int C=1; C<=T; ++C) > 11 { > 12 System.out.printf("Case #%d: ", C); > 13 (new <CLASSNAME>(sc)).caseMain(); > 14 } > 15 } > 16 > 17 Scanner sc; > 18 <CLASSNAME>( Scanner sc ) { this.sc = sc; } > 19 > 20 void caseMain() > 21 { > 22 int n = sc.nextInt(); > 23 long[] x = new long[n]; > 24 for(int i=0; i<n; ++i) x[i] = sc.nextInt(); > 25 long[] y = new long[n]; > 26 for(int i=0; i<n; ++i) y[i] = sc.nextInt(); > 27 > 28 System.out.println(solve(x, y, n)); > 29 } > 30 > 31 long solve(long[] x, long[] y, int n) > 32 { > 33 Arrays.sort(x); > 34 Arrays.sort(y); > 35 long s = 0; > 36 for(int i=0; i<n; ++i) > 37 s += x[i] * y[n-1-i]; > 38 return s; > 39 } > 40 }
Added _lib/gcj/!gcj-tmpl.lua version [a2c96590b21d65bc]
> 1 CN = io.stdin:read("*n") > 2 for C=1, CN, 1 do > 3 io.stdout:write( string.format("Case #%d: ", C) ) > 4 > 5 N = io.stdin:read("*n") > 6 M = io.stdin:read("*n") > 7 > 8 fav = {} > 9 for i=1, M, 1 do > 10 T = io.stdin:read("*n") > 11 fav[i] = {} > 12 for j=1, T, 1 do > 13 X, Y = io.stdin:read("*n", "*n") > 14 fav[i][j] = {X, Y} > 15 end > 16 end > 17 > 18 io.stdout:write( string.format("%d %d\n", N, M) ) > 19 end
Added _lib/gcj/!gcj-tmpl.py version [da8352105d588b93]
Added _lib/geo/area.cpp version [959cda21c7e42a70]
> 1 //------------------------------------------------------------- > 2 // Area of a polygon > 3 // O(N) > 4 // > 5 // Verified by > 6 // - SRM 337 Div1 LV3 > 7 // - SRM 486 Div1 LV3 > 8 //------------------------------------------------------------- > 9 > 10 double outer_prod( pt a, pt b ) > 11 { > 12 return (a.real()*b.imag() - b.real()*a.imag())/2; > 13 } > 14 > 15 double area( const vector<pt>& q ) > 16 { > 17 double a = 0.0; > 18 > 19 pt o = q[0]; > 20 for(int i=1; i+1<q.size(); ++i) > 21 a += outer_prod(q[i]-o, q[i+1]-o); > 22 return abs(a) / 2; > 23 }
Added _lib/geo/ccw.cpp version [81dbefbd30f15e79]
> 1 //------------------------------------------------------------- > 2 // ccw > 3 // > 4 // Verified by > 5 // - SRM 492 Div1 LV1 > 6 //------------------------------------------------------------- > 7 > 8 double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } > 9 double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } > 10 > 11 int ccw(const CMP& a, CMP b, CMP c) { > 12 b -= a; c -= a; > 13 if( outer_prod(b,c) > 0 ) return +1; // counter clockwise > 14 if( outer_prod(b,c) < 0 ) return -1; // clockwise > 15 if( inner_prod(b,c) < 0 ) return +2; // c--[a--b] on line > 16 if( norm(b) < norm(c) ) return -2; // [a--b]--c on line > 17 return 0; // [a--c--b] on line > 18 }
Added _lib/geo/circle_3pt.cpp version [40885cae01887a2c]
> 1 > 2 //------------------------------------------------------------- > 3 // The circle passing three points > 4 // > 5 // Verified by > 6 // - AOJ 0010 > 7 //------------------------------------------------------------- > 8 > 9 vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector< > 10 { > 11 vector<double> A(V); > 12 for(int i=0; i<n; ++i) > 13 { > 14 // pivot > 15 if( M[i][i] == 0 ) > 16 for(int j=i+1; j<n; ++j) > 17 if( M[j][i] != 0 ) > 18 {swap(M[i], M[j]); swap(A[i], A[j]); bre > 19 if( M[i][i] == 0 ) > 20 throw "no anser"; > 21 > 22 // M[i][i] <-- 1 > 23 double p = M[i][i]; > 24 for(int j=i; j<n; ++j) > 25 M[i][j] /= p; > 26 A[i] /= p; > 27 > 28 // M[*][i] <-- 0 > 29 for(int j=0; j<n; ++j) if(j!=i) > 30 { > 31 double r = M[j][i]; > 32 for(int k=i; k<n; ++k) > 33 M[j][k] -= M[i][k] * r; > 34 A[j] -= A[i] * r; > 35 } > 36 } > 37 return A; > 38 } > 39 > 40 void circle_3pt( CMP p1, CMP p2, CMP p3, CMP* c, double* r ) > 41 { > 42 p2 -= p1; > 43 p3 -= p1; > 44 // c == p2/2 + A p2 i == p3/2 + B p3 i > 45 > 46 vector< vector<double> > M(2, vector<double>(2)); > 47 vector<double> V(2); > 48 M[0][0] = -p2.imag(); M[0][1] = +p3.imag(); V[0] = (p3.real()-p2.real()) > 49 M[1][0] = +p2.real(); M[1][1] = -p3.real(); V[1] = (p3.imag()-p2.imag()) > 50 V = solve_linear_eq(2, M, V); > 51 > 52 double A=V[0], B=V[1]; > 53 *c = p1 + p2/2.0 + A * p2 * CMP(0,1); > 54 *r = abs(*c - p1); > 55 }
Added _lib/geo/convexHull.cpp version [75743e110118c49c]
> 1 > 2 //------------------------------------------------------------- > 3 // Convex Hull -- Andrew's Monotone Chain > 4 // O(N log N) > 5 // > 6 // Verified by > 7 // - SRM 336 Div1 LV3 > 8 // - TCO09 Round2 LV2 > 9 // - SRM 486 Div1 LV3 > 10 //------------------------------------------------------------- > 11 > 12 double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } > 13 double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } > 14 > 15 int ccw(const CMP& a, CMP b, CMP c) { > 16 b -= a; c -= a; > 17 if( outer_prod(b,c) > 0 ) return +1; // counter clockwise > 18 if( outer_prod(b,c) < 0 ) return -1; // clockwise > 19 if( inner_prod(b,c) < 0 ) return +2; // c--a--b on line > 20 if( norm(b) < norm(c) ) return -2; // a--b--c on line > 21 return 0; > 22 } > 23 > 24 bool byX( const CMP& a, const CMP& b ) { > 25 if( a.real() != b.real() ) > 26 return a.real() < b.real(); > 27 return a.imag() < b.imag(); > 28 } > 29 > 30 vector<CMP> convex_hull( vector<CMP> p ) > 31 { > 32 #define IS_RIGHT <0 // skip on-line verts > 33 //#define IS_RIGHT ==-1 // take all > 34 > 35 sort(p.begin(), p.end(), &byX); > 36 > 37 vector<CMP> ans; > 38 for(int i=0; i<p.size(); ans.push_back(p[i++])) // left-to-right > 39 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 40 ans.pop_back(); > 41 if( ans.size() == p.size() ) > 42 return ans; > 43 for(int i=p.size()-2; i>=0; ans.push_back(p[i--])) // right-to-left > 44 while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1] > 45 ans.pop_back(); > 46 ans.pop_back(); > 47 return ans; > 48 }
Added _lib/geo/convexHull_old.cpp version [9ffbc36d1d4aa6a7]
> 1 > 2 //------------------------------------------------------------- > 3 // Convex Hull -- Gift Wrapping > 4 // O(N log N) > 5 // > 6 // ToDo : Rewrite by CCW... > 7 // > 8 // Verified by > 9 // - SRM 336 Div1 LV3 > 10 //------------------------------------------------------------- > 11 > 12 bool byY( CMP a, CMP b ) > 13 { > 14 if( a.imag() == b.imag() ) > 15 return a.real() < b.real(); > 16 return a.imag() < b.imag(); > 17 } > 18 > 19 bool byArg( CMP a, CMP b ) > 20 { > 21 return arg(a) < arg(b); > 22 } > 23 > 24 bool isRight( CMP a, CMP b, CMP c ) > 25 { > 26 return arg((c-b) / (b-a)) < 0; > 27 } > 28 > 29 vector<CMP> convex_hull( vector<CMP> p ) > 30 { > 31 vector<CMP>::iterator it = min_element( p.begin(), p.end(), byY ); > 32 CMP o = *it; > 33 p.erase(it); > 34 for(int i=0; i<p.size(); ++i) > 35 p[i] -= o; > 36 sort( p.begin(), p.end(), byArg ); > 37 > 38 vector<CMP> q; > 39 q.push_back( CMP(0,0) ); > 40 q.push_back( p[0] ); > 41 for(int i=1; i<p.size(); ++i) { > 42 while( isRight(q[q.size()-2], q[q.size()-1], p[i]) ) > 43 q.pop_back(); > 44 q.push_back( p[i] ); > 45 } > 46 for(int i=0; i<q.size(); ++i) > 47 q[i] += o; > 48 return q; > 49 }
Added _lib/geo/pt_in_poly.cpp version [05f672d15009c4b5]
> 1 > 2 //------------------------------------------------------------- > 3 // The circle passing three points > 4 // > 5 // Verified by > 6 // - AOJ 0012 (only triangles) > 7 //------------------------------------------------------------- > 8 > 9 double outer_prod( CMP a, CMP b ) > 10 { > 11 return (a.real()*b.imag() - b.real()*a.imag())/2; > 12 } > 13 > 14 bool point_in_polygon( vector<CMP>& ps, CMP p ) > 15 { > 16 bool in = false; > 17 for(int i=0; i<ps.size(); ++i) { > 18 CMP a = ps[i] - p; > 19 CMP b = ps[(i+1)%ps.size()] - p; > 20 if(a.imag() > b.imag()) swap(a,b); > 21 if(a.imag()<=0 && 0<b.imag()) { > 22 if( outer_prod(a,b) < 0 ) > 23 in = !in; > 24 } > 25 //if( outer_prod(a,b)==0 && inner_prod(a,b)<=0 ) return ON; > 26 } > 27 return in; > 28 }
Added _lib/graph/__goldberg.cpp version [3f97582085a591c3]
> 1 //---------------------------------------------------------------------------- > 2 // Goldberg(-Tarjan?) –@ (Å‘å—¬) > 3 // > 4 // —e—Ê•t‚«ƒOƒ‰ƒt G ã‚Å‚Ì Src ‚©‚ç Dest ‚Ö‚ÌÅ‘å—¬—Ê‚ð‹‚ß‚é > 5 // - ƒOƒ‰ƒt‚Í—×Ús—ñ‚Å“n‚·‚±‚ÆBG ‚Í”j‰ó‚³‚ê‚Ü‚·B > 6 // - —¬—Ê‚¾‚¯‚Å‚È‚FlowŽ©‘Ì‚ª—~‚µ‚¢‚Æ‚«‚Í G ‚Ì—e—Ê‚ªŒ¸‚Á‚Ä‚é•”•ª‚ðŒ©‚é > 7 // > 8 // ŒvŽZ—Ê‚Í > 9 // - O(V^3) > 10 // # Ø–¾‚Í‚Ü‚¾—‰ð‚µ‚Ä‚¢‚È‚¢ > 11 // > 12 // ƒAƒ‹ƒSƒŠƒYƒ€‚ÌŠT—ª > 13 // Šî–{Œ`‚͈ȉº‚Ì’Ê‚è > 14 // 1. ‚Æ‚è‚ ‚¦‚¸Src‚©‚ço‚Ä‚é•Ó‘S•”‚É—e—ÊŒÀŠE‚Ü‚Å—¬‚· > 15 // 2. —¬“ü > —¬o ‚ȃm[ƒh‚ðŒ©‚Â‚¯‚ăoƒ‰ƒ“ƒX‚ðŽæ‚é > 16 // - ‚»‚±‚©‚çDest‚Ü‚Å“ž’B‚·‚éƒpƒX‚ª‚ ‚é‚È‚çA‚æ‚è > 17 // Dest‚É‹ß‚¢•ûŒü‚Öi‚Þ•Ó‚Ö‚Ì—¬o—Ê‚ð‘‚â‚· > 18 // - ‚È‚¢‚È‚çA‚æ‚èSrc‚Ö‹ß‚¢•ûŒü‚©‚ç—ˆ‚é•Ó‚©‚ç‚Ì > 19 // —¬“ü—Ê‚ðŒ¸‚ç‚· > 20 // 3. 2.‚ðƒAƒ“ƒoƒ‰ƒ“ƒX‚È’¸“_‚ª‚È‚‚È‚é‚Ü‚ÅŒJ‚è•Ô‚· > 21 // u—¬“ü > —¬o ‚ȃm[ƒh‚ðŒ©‚Â‚¯‚Äv‚̈—‚ÍA—¬o“üƒoƒ‰ƒ“ƒX‚ð•Ï‚¦‚½Žž‚É > 22 // Queue‚É“Ë‚Áž‚ñ‚Å‚¨‚‚±‚Æ‚ÅFIFO‚Å‚â‚éB > 23 // uDest‚É‹ß‚¢•ûŒüvuSrc‚É‹ß‚¢•ûŒüv‚ðí‚ÉŒµ–§‚É•]‰¿‚·‚é•K—v‚Í‚È‚‚ÄA > 24 // ‚ ‚é’ö“x‚̋ߎ—‚Å‚à\•ªB‰º‚̃R[ƒh‚Å‚Í‚»‚̋ߎ—’l‚ð d[] ‚ÅŽ‚Á‚Ä‚¢‚éB > 25 // d[] ‚Ì•]‰¿‚ð“K“x‚É‚¿‚á‚ñ‚Æ‚â‚Á‚½‚è‚·‚éheuristics‚Å‘¬‚‚È‚é‚炵‚¢B > 26 // ‚Æ‚¢‚¤‚©‚â‚ç‚È‚¢‚ÆŽÀÛã‚ÍŽg‚¢•¨‚É‚È‚ç‚È‚¢‚炵‚¢B‚ ‚Æ‚Å‚¿‚á‚ñ‚Æ‚â‚éB > 27 //---------------------------------------------------------------------------- > 28 > 29 #include <vector> > 30 #include <queue> > 31 #include <limits> > 32 using namespace std; > 33 > 34 typedef int Vert; > 35 typedef int Capacity; > 36 typedef vector< vector<Capacity> > Graph; > 37 > 38 Capacity goldberg_tarjan( Graph& G, Vert Src, Vert Dest ) > 39 { > 40 vector<Capacity> e( G.size() ); // excess : —¬“ü—Ê - —¬o—Ê > 41 vector<int> d( G.size() ); // distance : Dest‚Ö‚Ì‹——£||Src‚Ö‚Ì‹——£-N‚̉ºŒÀ > 42 d[Src] = G.size(); > 43 > 44 queue<Vert> Q; // e[v]>0 ‚ȃm[ƒh‚ð“ü‚ê‚Ä‚¨‚ƒLƒ…[ > 45 > 46 // ‚Æ‚è‚ ‚¦‚¸Src‚©‚ç‘S—Í‚Å—¬‚· > 47 for(int v=0; v!=G.size(); ++v) > 48 if( G[Src][v] ) > 49 { > 50 G[v][Src] += G[Src][v]; > 51 e[v] += G[Src][v]; > 52 G[Src][v] = 0; > 53 Q.push(v); > 54 } > 55 > 56 // e[v]>0 ‚ȃm[ƒh‚ª‚È‚‚È‚é‚Ü‚ÅŒJ‚è•Ô‚· > 57 while( !Q.empty() ) > 58 { > 59 Vert v = Q.front(); > 60 > 61 for(int u=0; u!=G.size() && e[v]; ++u) > 62 if( G[v][u] && d[v]>d[u] ) // “KØ‚È•ûŒü‚É—¬‚· "PUSH" > 63 { > 64 Capacity f = min(e[v], G[v][u]); > 65 G[v][u] -= f; > 66 G[u][v] += f; > 67 e[v] -= f; > 68 e[u] += f; > 69 if( e[u]==f && u!=Src && u!=Dest ) Q.push(u); > 70 } > 71 > 72 if( e[v] == 0 ) // ƒoƒ‰ƒ“ƒXŽæ‚ꂽ‚̂ŃLƒ…[‚©‚眂 > 73 Q.pop(); > 74 else // ƒoƒ‰ƒ“ƒXŽæ‚ê‚Ä‚È‚¢‚Ì‚Í‚¨‚©‚µ‚¢‚Ì‚Åd[v]‚ð’²®‚µ‚Ä‚â‚è’¼‚µ "RELABEL" > 75 { > 76 Capacity m = numeric_limits<Capacity>::max(); > 77 for(int u=0; u!=G.size(); ++u) > 78 if( G[v][u] ) > 79 m = min(m, d[u]+1); > 80 d[v] = m; > 81 } > 82 } > 83 > 84 // Dest ‚Ö‚Ì—¬“ü—Ê‚ð•Ô‚· > 85 return e[Dest]; > 86 } > 87 > 88 //---------------------------------------------------------------------------- > 89 // PKU 1459 > 90 //---------------------------------------------------------------------------- > 91 > 92 #include <iostream> > 93 #include <locale> > 94 > 95 int main() > 96 { > 97 cin.imbue( std::locale("C") ); > 98 for(int n,np,nc,m; cin>>n>>np>>nc>>m;) > 99 { > 100 // 0: src, 1: dst, 2: ... > 101 Graph g(n+2, vector<Capacity>(n+2)); > 102 > 103 while(m--) > 104 { > 105 int u, v, z; char _; > 106 cin >> _ >> u >> _ >> v >> _ >> z; > 107 g[u+2][v+2] = z; > 108 } > 109 > 110 while(np--) > 111 { > 112 int u, z; char _; > 113 cin >> _ >> u >> _ >> z; > 114 g[0][u+2] = z; > 115 } > 116 > 117 while(nc--) > 118 { > 119 int u, z; char _; > 120 cin >> _ >> u >> _ >> z; > 121 g[u+2][1] = z; > 122 } > 123 > 124 cout << goldberg_tarjan(g, 0, 1) << endl; > 125 } > 126 }
Added _lib/graph/biMatch.cpp version [de8fb2bb9f6a1b6d]
> 1 > 2 //------------------------------------------------------------- > 3 // Bipertite max-matching (by duality, min-vert-cover) > 4 // O(V E) > 5 // > 6 // Verified by > 7 // SRM 397 Div1 Lv3 > 8 //------------------------------------------------------------- > 9 > 10 typedef int vert; > 11 typedef vert edge; > 12 typedef vector<edge> edges; > 13 typedef vector<edges> graph; > 14 > 15 bool augment( graph& G, int v, vector<vert>& matchTo, bool visited[] ) > 16 { > 17 for(int i=0; i<G[v].size(); ++i) { > 18 vert u = G[v][i]; > 19 if( visited[u] ) continue; > 20 visited[u] = true; > 21 > 22 if( matchTo[u]<0 || augment(G, matchTo[u], matchTo, visited) ) > 23 { matchTo[v]=u, matchTo[u]=v; return true; } > 24 } > 25 return false; > 26 } > 27 > 28 template<int NV> > 29 int biMatch( graph& G, int L ) // [0,L):left, [L,?):right > 30 // only left->right edges are used during computation > 31 { > 32 vector<vert> matchTo(G.size(), -1); > 33 int ans = 0; > 34 for(vert v=0; v<L; ++v) { > 35 bool visited[NV] = {}; > 36 if( augment(G, v, matchTo, visited) ) > 37 ++ans; > 38 } > 39 return ans; > 40 }
Added _lib/graph/isGraphical.cpp version [5fae6e092a92d7c8]
> 1 //------------------------------------------------------------- > 2 // Check the given list can be a degree list of some graph > 3 // O(n^2) (I suspect it can be made faster, though...) > 4 // > 5 // Verified by > 6 // - SRM 398 Div1 LV3 > 7 // > 8 //(( > 9 // Havel-Hakimi > 10 // If G[0], ..., G[n] (decreasing) is graphical, > 11 // then G[1]-1, G[2]-1, ..., G[G[0]]-1, G[G[0]+1], .., G[n] > 12 // is also graphical. > 13 //)) > 14 //------------------------------------------------------------- > 15 > 16 bool isGraphical( vector<int> G ) > 17 { > 18 sort( G.begin(), G.end() ); > 19 > 20 vector<int>::iterator b = lower_bound( G.begin(), G.end(), 1 ); > 21 vector<int>::iterator e = G.end(); > 22 > 23 while( b < e ) > 24 { > 25 int n = *(--e); > 26 if( e-b < n ) > 27 return false; > 28 for(vector<int>::iterator i=e-n; i!=e; ++i) > 29 --*i; > 30 inplace_merge( b, e-n, e ); > 31 b = lower_bound( G.begin(), G.end(), 1 ); > 32 } > 33 return true; > 34 }
Added _lib/graph/maxFlow.cpp version [c6d3325f9b7a789b]
> 1 > 2 //------------------------------------------------------------- > 3 // Dinic's Algorithm > 4 // O(V E) > 5 // > 6 // G : bidirectional (G[i].has(j) <==> G[j].has(i)) > 7 // F : flow-capacity F[i][j] = Capacity, F[j][i] = 0 > 8 // > 9 // Verified by > 10 // - SRM 399 Div1 LV3 > 11 // - PKU 1459 > 12 // - CodeCraft 09 CUTS > 13 // - SRM 465 Div1 LV2 > 14 //------------------------------------------------------------- > 15 > 16 static const int NV = 512; > 17 typedef int flow; > 18 typedef int vert; > 19 typedef vert edge; > 20 typedef vector<edge> edges; > 21 typedef vector<edges> graph; > 22 typedef flow flow_graph[NV][NV]; > 23 > 24 flow dinic_dfs( graph& G, flow_graph F, vert v, vert D, > 25 int LV[], flow flow_in, int blocked[] ) > 26 { > 27 flow flow_out = 0; > 28 for(int i=0; i!=G[v].size(); ++i) { > 29 int u = G[v][i]; > 30 if( LV[v]+1==LV[u] && F[v][u] ) { > 31 flow f = min(flow_in-flow_out, F[v][u]); > 32 if( u==D || !blocked[u] && (f=dinic_dfs(G,F,u,D,LV,f,blo > 33 F[v][u] -= f; > 34 F[u][v] += f; > 35 flow_out += f; > 36 if( flow_in == flow_out ) return flow_out; > 37 } > 38 } > 39 } > 40 blocked[v] = (flow_out==0); > 41 return flow_out; > 42 } > 43 > 44 flow maxFlow( graph& G, flow_graph F, vert S, vert D ) > 45 { > 46 for( flow total=0 ;; ) { > 47 int LV[NV] = {0}; > 48 vector<int> Q(1, S); > 49 for(int lv=1; !Q.empty(); ++lv) { > 50 vector<int> Q2; > 51 for(int i=0; i!=Q.size(); ++i) { > 52 edges& ne = G[Q[i]]; > 53 for(int j=0; j!=ne.size(); ++j) > 54 if( F[Q[i]][ne[j]] && !LV[ne[j]] && ne[j > 55 LV[ne[j]]=lv, Q2.push_back(ne[j] > 56 } > 57 Q.swap(Q2); > 58 } > 59 > 60 if( !LV[D] ) > 61 return total; > 62 > 63 int blocked[NV] = {}; > 64 total += dinic_dfs( G, F, S, D, LV, 0x7fffffff, blocked ); > 65 } > 66 }
Added _lib/graph/mincostFlow.cpp version [a86509e21e3c5ec5]
> 1 //------------------------------------------------------------- > 2 // MinCost-MaxFlow > 3 // O(??) > 4 // > 5 // Verified by > 6 // - SRM 487 Div2 LV3 > 7 // - SRM 491 Div1 LV3 > 8 //------------------------------------------------------------- > 9 > 10 #include <iostream> > 11 #include <string> > 12 #include <vector> > 13 #include <map> > 14 #include <queue> > 15 using namespace std; > 16 > 17 template<typename T> > 18 class IdGen > 19 { > 20 map<T, int> v2id_; > 21 vector<T> id2v_; > 22 public: > 23 int v2id(const T& v) { > 24 if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } > 25 return v2id_[v]; > 26 } > 27 const T& id2v(int i) const { return id2v_[i]; } > 28 int size() const { return id2v_.size(); } > 29 }; > 30 > 31 template<typename Vert, typename Cost, typename Flow, int NV=256> > 32 class MinCostFlow > 33 { > 34 IdGen<Vert> idgen; > 35 > 36 vector<int> G[NV]; > 37 Flow F[NV][NV]; > 38 Cost C[NV][NV]; > 39 > 40 public: > 41 void addEdge( Vert s_, Vert t_, Cost c, Flow f ) > 42 { > 43 int s = idgen.v2id(s_), t = idgen.v2id(t_); > 44 G[s].push_back(t); > 45 G[t].push_back(s); > 46 C[s][t] = c; > 47 C[t][s] = -c; > 48 F[s][t] = f; > 49 F[t][s] = 0; > 50 } > 51 > 52 pair<Cost, Flow> calc( Vert s_, Vert t_ ) > 53 { > 54 const int N=idgen.size(), S=idgen.v2id(s_), T=idgen.v2id(t_); > 55 static const Cost COST_INF = 1e+300; // !!EDIT HERE!! > 56 static const Flow FLOW_INF = 0x7fffffff; > 57 > 58 Cost total_cost = 0; > 59 Flow total_flow = 0; > 60 vector<Cost> h(N, 0); // potential > 61 for(Flow RF=FLOW_INF; RF>0; ) // residual flow > 62 { > 63 // Dijkstra -- find the min-cost path > 64 vector<Cost> d(N, COST_INF); d[S] = 0; > 65 vector<int> prev(N, -1); > 66 > 67 typedef pair< Cost, pair<int,int> > cedge; > 68 priority_queue< cedge, vector<cedge>, greater<cedge> > Q > 69 Q.push( cedge(0, make_pair(S,S)) ); > 70 while( !Q.empty() ) { > 71 cedge e = Q.top(); Q.pop(); > 72 if( prev[e.second.second] >= 0 ) > 73 continue; > 74 prev[e.second.second] = e.second.first; > 75 > 76 int u = e.second.second; > 77 for(int i=0; i<G[u].size(); ++i) { > 78 int v = G[u][i]; > 79 Cost r_cost = C[u][v] + h[u] - h[v]; > 80 if( F[u][v] > 0 && d[v] > d[u]+r_cost ) > 81 Q.push( cedge(d[v]=d[u]+r_cost, > 82 } > 83 } > 84 > 85 if( prev[T] < 0 ) > 86 break; // Finished > 87 > 88 // Run the flow as much as possible > 89 Flow f = RF; > 90 for(int u=T; u!=S; u=prev[u]) > 91 f = min(f, F[prev[u]][u]); > 92 RF -= f; > 93 total_flow += f; > 94 > 95 for(int u=T; u!=S; u=prev[u]) > 96 { > 97 total_cost += f * C[prev[u]][u]; > 98 F[prev[u]][u] -= f; > 99 F[u][prev[u]] += f; > 100 } > 101 > 102 // Update the potential > 103 for(int u=0; u<N; ++u) > 104 h[u] += d[u]; > 105 } > 106 return make_pair(total_cost, total_flow); > 107 } > 108 };
Added _lib/graph/undAllPairMinCut.cpp version [08ac17a6157e8d09]
> 1 > 2 //------------------------------------------------------------- > 3 // Gomory-Hu > 4 // All Pair Minimum Cuts for an Undirected Graph > 5 // O(V^4) > 6 // > 7 // G : undirected (G[i].has(j) <==> G[j].has(i)) > 8 // F : flow-capacity F[i][j] = F[j][i] = Capacity > 9 // > 10 // Verified by > 11 // - CodeCraft 09 CUTS > 12 //------------------------------------------------------------- > 13 > 14 static const int NV = 512; > 15 typedef int flow; > 16 typedef int vert; > 17 typedef vert edge; > 18 typedef vector<edge> edges; > 19 typedef vector<edges> graph; > 20 typedef flow flow_graph[NV][NV]; > 21 > 22 flow_graph GHF; > 23 void GomoryHu( graph& G, flow_graph F, flow_graph AllPairMinCut ) > 24 { > 25 int N = G.size(); > 26 vector<vert> parent(N, 0); > 27 vector<flow> weight(N, 0); > 28 > 29 // construct the Gomory-Hu Tree > 30 for(vert s=1; s<N; ++s) > 31 { > 32 vert t = parent[s]; > 33 > 34 // mincut between s and t > 35 memcpy(GHF, F, sizeof(GHF)); > 36 flow mincut = 0; > 37 for(;;) { > 38 // bfs > 39 vector<vert> prev(N, -1); prev[s]=s; > 40 queue<vert> Q; Q.push(s); > 41 while( !Q.empty() ) { > 42 vert v = Q.front(); Q.pop(); > 43 for(int i=0; i<G[v].size(); ++i) { > 44 vert u = G[v][i]; > 45 if( prev[u]==-1 && GHF[v][u] ) > 46 prev[u]=v, Q.push(u); > 47 } > 48 } > 49 > 50 if( prev[t] == -1 ) > 51 { > 52 // done: separeted to {v|prev[v]!=-1} and {v|pre > 53 // split t's children > 54 weight[s] = mincut; > 55 for(vert v=0; v<N; ++v) > 56 if( parent[v]==t && prev[v]!=-1 && v!=s > 57 parent[v] = s; > 58 vert pt = parent[t]; > 59 if( prev[pt]!=-1 ) > 60 parent[s]=pt, parent[t]=s, weight[s]=wei > 61 break; > 62 } > 63 > 64 // flow... > 65 flow cur = 0x7fffffff; > 66 for(vert v=t; v!=s; v=prev[v]) > 67 cur = min(cur, GHF[prev[v]][v]); > 68 for(vert v=t; v!=s; v=prev[v]) > 69 GHF[prev[v]][v] -= cur, GHF[v][prev[v]] += cur; > 70 mincut += cur; > 71 } > 72 } > 73 > 74 // AllPairMinCuts over the G-H tree > 75 for(vert s=0; s<N; ++s) { > 76 vector<vert> ps_s; > 77 for(vert v=s ;; v=parent[v]) > 78 { ps_s.push_back(v); if( v==parent[v] ) break; } > 79 reverse(ps_s.begin(), ps_s.end()); > 80 > 81 for(vert t=s+1; t<N; ++t) { > 82 vector<vert> ps_t; > 83 for(vert v=t ;; v=parent[v]) > 84 { ps_t.push_back(v); if( v==parent[v] ) break; } > 85 reverse(ps_t.begin(), ps_t.end()); > 86 > 87 vert ca; > 88 for(int i=0; i<ps_s.size() && i<ps_t.size() && ps_s[i]== > 89 ca = ps_s[i]; > 90 > 91 flow s_to_root = 0x7fffffff, t_to_root = 0x7fffffff; > 92 for(vert v=s; v!=ca; v=parent[v]) > 93 s_to_root = min(s_to_root, weight[v]); > 94 for(vert v=t; v!=ca; v=parent[v]) > 95 t_to_root = min(t_to_root, weight[v]); > 96 AllPairMinCut[s][t] = AllPairMinCut[t][s] = min(s_to_roo > 97 } > 98 } > 99 }
Added _lib/graph/undMinCut.cpp version [27b1f3ac721526bc]
> 1 > 2 //------------------------------------------------------------- > 3 // Stoer-Wagner > 4 // Minimum cost for making the graph unconnected > 5 // O(V^3) > 6 // > 7 // G : bidirectional (G[v][u] == G[u][v]) > 8 // > 9 // Verified by > 10 // - SRM 340 Div1 LV3 > 11 //------------------------------------------------------------- > 12 > 13 typedef int cost; > 14 typedef int vert; > 15 typedef vector< vector<cost> > graph; > 16 > 17 cost bidi_minCut( graph G, int N ) > 18 { > 19 vector<vert> V; > 20 for(int v=0; v<N; ++v) > 21 V.push_back(v); > 22 > 23 cost result = 0x7fffffff; > 24 for(int n=N; n>1; --n) > 25 { > 26 // invariant: > 27 // the current set of vertices = {V[0] .. V[n-1]} > 28 > 29 // order the vertices > 30 // v0 = 0 (arbitrary), > 31 // v1 = (u that maximizes \Sigma G[v0][u]) > 32 // v2 = (u that maximizes \Sigma G[v0][u]+G[v1][u]) > 33 // ... > 34 // vn-2 > 35 // vn-1 = (maximizes lastCut = \Sigma G[v0][u]+G[vn-2][u]) > 36 vector<int> vs; > 37 cost lastCut = 0; > 38 { > 39 vector<cost> ws(n, 0); > 40 ws[0] = 1; > 41 > 42 for(int i=0; i<n; ++i) { > 43 int j = max_element(ws.begin(), ws.end()) - ws.b > 44 lastCut = ws[j]; > 45 > 46 vs.push_back(j); > 47 ws[j] = -1; > 48 for(int k=0; k<n; ++k) > 49 if( ws[k] != -1 ) > 50 ws[k] += G[V[k]][V[j]]; > 51 } > 52 } > 53 > 54 // update mincut > 55 // lemma: "the min cost for separating vn-2 and vn-1"==lastCut > 56 result = min(result, lastCut); > 57 > 58 // reduce the graph (unify vn-2 and vn-1) > 59 // for testing the case vn-2 and vn-1 is connected > 60 vert v2=vs[n-2], v1=vs[n-1]; > 61 for(int i=0; i<n; ++i) { > 62 G[V[v2]][V[i]] += G[V[v1]][V[i]]; > 63 G[V[i]][V[v2]] += G[V[i]][V[v1]]; > 64 } > 65 V.erase( V.begin() + v1 ); > 66 } > 67 return result; > 68 }
Added _lib/numeric/bigintFake.cpp version [029a85274b27ccda]
> 1 > 2 string add(const string& a, const string& b) > 3 { > 4 int n = max(a.size(), b.size()), carry=0; > 5 string c(n, '0'); > 6 for(int i=0; i<n; ++i) { > 7 int x = (a.size()<=i ? 0 : a[a.size()-1-i]-'0') > 8 + (b.size()<=i ? 0 : b[b.size()-1-i]-'0') + carry; > 9 c[n-1-i] = char('0'+x%10); > 10 carry = x/10; > 11 } > 12 if( carry ) c = char('0'+carry)+c; > 13 return c; > 14 }
Added _lib/numeric/erathos.cpp version [451164162aaf4750]
Added _lib/numeric/fft.cpp version [30aac07db1a49b69]
> 1 > 2 //------------------------------------------------------------- > 3 // Fast Discrete Fourier Transform > 4 // O(n log n) > 5 // n must be a power of 2. > 6 // > 7 // Verified by > 8 // - SRM 436 LV3 > 9 //------------------------------------------------------------- > 10 > 11 CMP tmp[65536*4]; > 12 > 13 template<int F> > 14 void fft_impl( CMP a[], int n, int stride = 1 ) > 15 { > 16 if( n > 1 ) > 17 { > 18 CMP *ev=a, *od=a+stride; > 19 int s2=stride*2, n2=n/2; > 20 > 21 fft_impl<F>( ev, n2, s2 ); > 22 fft_impl<F>( od, n2, s2 ); > 23 > 24 for(int i=0; i<n; ++i) tmp[i] = ev[s2*(i%n2)] + od[s2*(i%n2)]*po > 25 for(int i=0; i<n; ++i) a[stride*i] = tmp[i]; > 26 } > 27 } > 28 > 29 void fft( vector<CMP>& a ) > 30 { > 31 fft_impl<+1>(&a[0], a.size()); > 32 } > 33 > 34 void ifft( vector<CMP>& a ) > 35 { > 36 fft_impl<-1>(&a[0], a.size()); > 37 for(int i=0; i<a.size(); ++i) > 38 a[i] /= a.size(); > 39 }
Added _lib/numeric/gcd_lcm.cpp version [9e6d3cbd67078cb9]
> 1 LL gcd(LL a, LL b) > 2 { > 3 while(a) > 4 swap(a, b%=a); > 5 return b; > 6 } > 7 > 8 LL lcm(LL a, LL b) > 9 { > 10 return a/gcd(a,b)*b; > 11 } > 12 > 13 // Assumption: (a/b)*b+(a%b) == a > 14 // For typical C/C++ compilers, % on negatives satisfies this. > 15 // To be sure, replace a%b by a-a/b*b > 16 > 17 LL xgcd(LL a, LL b, LL* x, LL* y) // ax+by=g > 18 { > 19 if(b) { > 20 LL yy, g = xgcd(b,a%b,&yy,x); > 21 *y = yy - a/b**x; > 22 return g; > 23 } > 24 else { > 25 *x=1, *y=0; > 26 return a; > 27 } > 28 }
Added _lib/numeric/linearEq.cpp version [908cbaac8fbb72e4]
> 1 > 2 //------------------------------------------------------------- > 3 // Linear Equation Solver > 4 // O(n^3) > 5 // > 6 // Verified by > 7 // - SRM 398 Div1 LV3 > 8 // - AOJ 0004 > 9 //------------------------------------------------------------- > 10 > 11 vector<double> solve_linear_eq( int n, vector< vector<double> > M, const vector< > 12 { > 13 vector<double> A(V); > 14 for(int i=0; i<n; ++i) > 15 { > 16 // pivot > 17 if( M[i][i] == 0 ) > 18 for(int j=i+1; j<n; ++j) > 19 if( M[j][i] != 0 ) > 20 {swap(M[i], M[j]); swap(A[i], A[j]); bre > 21 if( M[i][i] == 0 ) > 22 throw "no anser"; > 23 > 24 // M[i][i] <-- 1 > 25 double p = M[i][i]; > 26 for(int j=i; j<n; ++j) > 27 M[i][j] /= p; > 28 A[i] /= p; > 29 > 30 // M[*][i] <-- 0 > 31 for(int j=0; j<n; ++j) if(j!=i) > 32 { > 33 double r = M[j][i]; > 34 for(int k=i; k<n; ++k) > 35 M[j][k] -= M[i][k] * r; > 36 A[j] -= A[i] * r; > 37 } > 38 } > 39 return A; > 40 }
Added _lib/numeric/linearEq2.cpp version [bf7f1d164fa34636]
> 1 //------------------------------------------------------------- > 2 // Linear Equation Solver for Teplitz Matrix > 3 // O(n^2) > 4 // > 5 // Faster solver for matrices satisfying > 6 // M[i][j] == M[i+1][j+1] > 7 // for all i, j>0 > 8 // Be careful that this implementation does not support the > 9 // case M[0][0]=M[1][1]=...=0. (zero-division) > 10 // > 11 // TODO: support M[0][0]=M[1][1]=...=0 > 12 // QUICKHACK: > 13 // find the least degenerated k and > 14 // calc initial vectors by gauss_jordan... > 15 // > 16 // Verified by > 17 // - (Checked by random data to match with linearEq.cpp) > 18 //------------------------------------------------------------- > 19 > 20 vector<double> solve_teplitz( int n, vector< vector<double> > M, const vector<do > 21 { > 22 vector<double> f, b, x; > 23 > 24 // invariants > 25 // M|k f|k = (1 0 ... 0) > 26 // M|k b|k = (0 ... 0 1) > 27 // M|k x|k = V|k > 28 f.push_back( 1 / M[0][0] ); > 29 b.push_back( 1 / M[0][0] ); > 30 x.push_back( V[0] / M[0][0] ); > 31 > 32 for(int k=2; k<=n; ++k) > 33 { > 34 // M|k (f|k-1 0) = (1 0 ... 0 ea) > 35 double ea = 0; > 36 for(int i=0; i<k-1; ++i) > 37 ea += M[k-1][i] * f[i]; > 38 > 39 // M|k (0 b|k-1) = (eb 0 ... 0 1) > 40 double eb = 0; > 41 for(int i=1; i<k; ++i) > 42 eb += M[0][i] * b[i-1]; > 43 > 44 // u(1 ea) + v(eb 1) = (1 0) > 45 //==> u = 1 / (1-ea.eb), v = -ea /(1-ea.eb) > 46 double u = 1/(1-ea*eb), v = -ea/(1-ea*eb); > 47 vector<double> ff; > 48 for(int i=0; i<k; ++i) > 49 ff.push_back( u*(i<k-1 ? f[i] : 0) + v*(i>0 ? b[i-1] : 0 > 50 > 51 // similarly... > 52 u = -eb/(1-ea*eb), v = 1/(1-ea*eb); > 53 vector<double> bb; > 54 for(int i=0; i<k; ++i) > 55 bb.push_back( u*(i<k-1 ? f[i] : 0) + v*(i>0 ? b[i-1] : 0 > 56 > 57 // update > 58 f.swap(ff); > 59 b.swap(bb); > 60 > 61 // M|k (x|k 0) (V|k ec) > 62 double ec = 0; > 63 for(int i=0; i<k-1; ++i) > 64 ec += M[k-1][i] * x[i]; > 65 x.push_back(0); > 66 for(int i=0; i<k; ++i) > 67 x[i] -= b[i] * (ec-V[k-1]); > 68 } > 69 > 70 return x; > 71 }
Added _lib/numeric/matrix.cpp version [2c92483914a46a56]
> 1 > 2 //------------------------------------------------------------- > 3 // Matrix Operations > 4 // > 5 // Verified by > 6 // - SRM342 Div1 LV3 > 7 // - SRM341 Div1 LV3 > 8 // - SRM338 Div1 LV2 > 9 //------------------------------------------------------------- > 10 > 11 vector<LL> vMul( const vector< vector<LL> >& A, const vector<LL>& B ) > 12 { > 13 const int n = A.size(); > 14 > 15 vector<LL> C(n); > 16 for(int i=0; i<n; ++i) > 17 for(int j=0; j<n; ++j) > 18 C[i] += A[i][j] * B[j]; > 19 return C; > 20 } > 21 > 22 vector< vector<LL> > mMul( const vector< vector<LL> >& A, const vector< vector<L > 23 { > 24 const int n = A.size(); > 25 > 26 vector< vector<LL> > C(n, vector<LL>(n)); > 27 for(int i=0; i<n; ++i) > 28 for(int j=0; j<n; ++j) { > 29 LL Cij = 0; > 30 for(int k=0; k<n; ++k) > 31 Cij += A[i][k] * B[k][j]; > 32 C[i][j] = Cij; > 33 } > 34 return C; > 35 } > 36 > 37 vector< vector<LL> > mPow( vector< vector<LL> > M, LL t ) // t>0 > 38 { > 39 vector< vector<LL> > R; > 40 for(; t; t>>=1, M=mMul(M,M)) > 41 if( t&1 ) > 42 R = (R.empty() ? M : mMul(R,M)); > 43 return R; > 44 } > 45 > 46 vector< vector<LL> > mAdd( const vector< vector<LL> >& A, const vector< vector<L > 47 { > 48 const int n = A.size(); > 49 > 50 vector< vector<LL> > C(n, vector<LL>(n)); > 51 for(int i=0; i<n; ++i) > 52 for(int j=0; j<n; ++j) > 53 C[i][j] = A[i][j] + B[i][j]; > 54 return C; > 55 }
Added _lib/numeric/mebius.cpp version [daa89aa8f6eb3e5c]
> 1 > 2 //------------------------------------------------------------- > 3 // Mebius Function > 4 // O( sqrt(N) ) > 5 // > 6 // mebius(n) = 0 if n=0 mod p*p for some p > 7 // mebius(n) = (-1)^k otherwise, where k is the number of n's prime factors > 8 // > 9 // properties: > 10 // 1: mebius(nm) = mebius(n) mebius(m) if gcd(n,m)=1 > 11 // 2: f(n) = Sigma_{d|n} g(d) > 12 // <=> > 13 // g(n) = Sigma_{d|n} f(d)mebius(n/d) > 14 // > 15 // Verified by > 16 // - CodeCraft '11 PSTR > 17 // > 18 // Further memoization or more might be needed. > 19 //------------------------------------------------------------- > 20 > 21 int mebius(int n) > 22 { > 23 int f = 1; > 24 for(int q=2; q*q<=n; ++q) > 25 if( n%q == 0 ) > 26 { > 27 if( n%(q*q) == 0 ) > 28 return 0; > 29 n /= q; > 30 f *= -1; > 31 } > 32 if(n>1) > 33 f *= -1; > 34 return f; > 35 } > 36
Added _lib/numeric/modArith.cpp version [b348fa4dbce4653d]
> 1 > 2 //------------------------------------------------------------- > 3 // Modulo Arithmetics > 4 // > 5 // Verified by > 6 // - TCO10 R3 LV3 > 7 //------------------------------------------------------------- > 8 > 9 static const int MODVAL = 1000000007; // op/ depends on primarity of the MODVAL > 10 struct mint > 11 { > 12 int val; > 13 mint():val(0){} > 14 mint(int x):val(x%MODVAL) {} > 15 mint(LL x):val(x%MODVAL) {} > 16 }; > 17 > 18 mint operator+(mint x, mint y) { return x.val+y.val; } > 19 mint operator-(mint x, mint y) { return x.val-y.val+MODVAL; } > 20 mint operator*(mint x, mint y) { return LL(x.val)*y.val; } > 21 mint POW(mint x, int e) { > 22 mint v = 1; > 23 for(;e;x=x*x,e>>=1) > 24 if(e&1) > 25 v=v*x; > 26 return v; > 27 } > 28 mint operator/(mint x, mint y) { return x * POW(y, MODVAL-2); } > 29 > 30 vector<mint> FAC_(1,1); > 31 void FAC_INIT(int n) { for(int i=1; i<=n; ++i) FAC_.push_back( FAC_.back()*i ); > 32 mint FAC(mint x) { return FAC_[x.val]; } > 33 mint C(mint n, mint k) { return k.val<0 || n.val<k.val ? 0 : FAC(n) / (FAC(k) * > 34 > 35 /* > 36 // MODVAL must be a prime!! > 37 LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e > 38 { > 39 if( b > e ) return 0; > 40 if( k <= 1 ) return k*(e-b+1); > 41 return DIV(SUB(POW(k, e+1), POW(k,b)), k-1); > 42 } > 43 > 44 LL Cpascal(LL n, LL k) > 45 { > 46 vector< vector<LL> > c(n+1, vector<LL>(k+1)); > 47 for(LL nn=1; nn<=n; ++nn) > 48 for(LL kk=0; kk<=min(nn,k); ++kk) > 49 c[nn][kk] = kk==0 || kk==nn ? 1 : ADD(c[nn-1][kk-1], c[n > 50 return c[n][k]; > 51 } > 52 > 53 vector< vector<LL> > MATMUL(vector< vector<LL> >& a, vector< vector<LL> >& b) > 54 { > 55 int N = a.size(); > 56 vector< vector<LL> > c(N, vector<LL>(N)); > 57 for(int i=0; i<N; ++i) > 58 for(int j=0; j<N; ++j) > 59 for(int k=0; k<N; ++k) > 60 c[i][j] = ADD(c[i][j], MUL(a[i][k],b[k][j])); > 61 return c; > 62 } > 63 > 64 // works for non-prime MODVAL > 65 LL GEO(LL x_, LL e) // x^0 + x^1 + ... + x^e-1 > 66 { > 67 vector< vector<LL> > v(2, vector<LL>(2)); > 68 vector< vector<LL> > x(2, vector<LL>(2)); > 69 v[0][0] = v[1][1] = 1; > 70 x[0][0] = x_; x[0][1] = 0; > 71 x[1][0] = 1 ; x[1][1] = 1; > 72 for(;e;x=MATMUL(x,x),e>>=1) > 73 if(e&1) > 74 v = MATMUL(v, x); > 75 return v[1][0]; > 76 } > 77 > 78 // works for non-prime MODVAL > 79 LL HYP(LL x_, LL e) // e x^0 + (e-1) x^1 + ... + 1 x^e-1 = GEO(x,1)+GEO(x,2)+... > 80 { > 81 vector< vector<LL> > v(3, vector<LL>(3)); > 82 vector< vector<LL> > x(3, vector<LL>(3)); > 83 v[0][0] = v[1][1] = v[2][2] = 1; > 84 x[0][0] = x_; x[0][1] = 0; x[0][2] = 0; > 85 x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0; > 86 x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1; > 87 e++; > 88 for(;e;x=MATMUL(x,x),e>>=1) > 89 if(e&1) > 90 v = MATMUL(v, x); > 91 return v[2][0]; > 92 } > 93 */
Added _lib/numeric/modArithOld.cpp version [b669f317babaf285]
> 1 > 2 //------------------------------------------------------------- > 3 // Modulo Arithmetics > 4 // > 5 // Verified by > 6 // - SRM 397 Div1 LV2 > 7 // - SRM 428 Div1 LV2 > 8 // - CodeCraft 2010 CNTINT DRAW > 9 //------------------------------------------------------------- > 10 > 11 static const LL MODVAL = 1000000007; // must fit in 32-bits > 12 > 13 LL ADD(LL x, LL y) { return (x+y)%MODVAL; } > 14 LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } > 15 LL MUL(LL x, LL y) { return (x*y)%MODVAL; } > 16 LL POW(LL x, LL e) { > 17 LL v = 1; > 18 for(;e;x=MUL(x,x),e>>=1) > 19 if(e&1) > 20 v = MUL(v, x); > 21 return v; > 22 } > 23 > 24 // MODVAL must be a prime!! > 25 LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } > 26 > 27 // MODVAL must be a prime!! > 28 LL C(LL n, LL k) { > 29 LL v = 1; > 30 for(LL i=1; i<=k; ++i) > 31 v = DIV(MUL(v, n-i+1), i); > 32 return v; > 33 } > 34 > 35 // MODVAL must be a prime!! > 36 LL GSS(LL k, LL b, LL e) // k^b + k^b+1 + ... + k^e > 37 { > 38 if( b > e ) return 0; > 39 if( k <= 1 ) return k*(e-b+1); > 40 return DIV(SUB(POW(k, e+1), POW(k,b)), k-1); > 41 } > 42 > 43 > 44 > 45 > 46 LL Cpascal(LL n, LL k) > 47 { > 48 vector< vector<LL> > c(n+1, vector<LL>(k+1)); > 49 for(LL nn=1; nn<=n; ++nn) > 50 for(LL kk=0; kk<=min(nn,k); ++kk) > 51 c[nn][kk] = kk==0 || kk==nn ? 1 : ADD(c[nn-1][kk-1], c[n > 52 return c[n][k]; > 53 } > 54 > 55 vector< vector<LL> > MATMUL(vector< vector<LL> >& a, vector< vector<LL> >& b) > 56 { > 57 int N = a.size(); > 58 vector< vector<LL> > c(N, vector<LL>(N)); > 59 for(int i=0; i<N; ++i) > 60 for(int j=0; j<N; ++j) > 61 for(int k=0; k<N; ++k) > 62 c[i][j] = ADD(c[i][j], MUL(a[i][k],b[k][j])); > 63 return c; > 64 } > 65 > 66 // works for non-prime MODVAL > 67 LL GEO(LL x_, LL e) // x^0 + x^1 + ... + x^e-1 > 68 { > 69 vector< vector<LL> > v(2, vector<LL>(2)); > 70 vector< vector<LL> > x(2, vector<LL>(2)); > 71 v[0][0] = v[1][1] = 1; > 72 x[0][0] = x_; x[0][1] = 0; > 73 x[1][0] = 1 ; x[1][1] = 1; > 74 for(;e;x=MATMUL(x,x),e>>=1) > 75 if(e&1) > 76 v = MATMUL(v, x); > 77 return v[1][0]; > 78 } > 79 > 80 // works for non-prime MODVAL > 81 LL HYP(LL x_, LL e) // e x^0 + (e-1) x^1 + ... + 1 x^e-1 = GEO(x,1)+GEO(x,2)+... > 82 { > 83 vector< vector<LL> > v(3, vector<LL>(3)); > 84 vector< vector<LL> > x(3, vector<LL>(3)); > 85 v[0][0] = v[1][1] = v[2][2] = 1; > 86 x[0][0] = x_; x[0][1] = 0; x[0][2] = 0; > 87 x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0; > 88 x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1; > 89 e++; > 90 for(;e;x=MATMUL(x,x),e>>=1) > 91 if(e&1) > 92 v = MATMUL(v, x); > 93 return v[2][0]; > 94 }
Added _lib/numeric/nextComb.cpp version [291928c806737930]
> 1 //------------------------------------------------------------- > 2 // Next Combination > 3 // > 4 // Verified by > 5 // - SRM345 Div1 LV3 > 6 //------------------------------------------------------------- > 7 > 8 LL next_combination(LL p) > 9 { > 10 assert( p > 0 ); > 11 LL lsb = p & -p; > 12 LL rem = p + lsb; > 13 LL rit = rem & ~p; > 14 return rem | (rit/lsb >> 1)-1; > 15 }
Added _lib/numeric/sternBrocot.cpp version [bfa0be1fa6ea3c75]
> 1 > 2 //------------------------------------------------------------- > 3 // Enumerate all positive reduced fractions in a > 4 // systematic manner. > 5 // [pl/ql, pr/qr] --> [pl/ql, pm/qm] + [pm/qm, pr/qr] > 6 // --> ... > 7 // > 8 // Verified by > 9 // - CodeCraft 09 FRACTION > 10 //------------------------------------------------------------- > 11 > 12 void sternBrocot(LL pl = 0, LL ql = 1, LL pr = 1, LL qr = 0) > 13 { > 14 LL pm = pl + pr, qm = ql + qr; > 15 sternBrocot(pl, ql, pm, qm); > 16 sternBrocot(pm, qm, pr, qr); > 17 } > 18 > 19 /* > 20 bool fleq(LL p1, LL q1, LL p2, LL q2) > 21 { > 22 return p1*q2 <= p2*q1; > 23 } > 24 > 25 void sternBrocot(LL pl = 0, LL ql = 1, LL pr = 1, LL qr = 0) > 26 { > 27 for(;;) { > 28 LL pm = pl + pr, qm = ql + qr; > 29 > 30 if( fleq(c,d,pm,qm) ) { > 31 // pr = pm, qr = qm; // [pl/ql, pm/qm] > 32 LL X = c*ql - d*pl; > 33 LL Y = d*pr - c*qr; > 34 LL k = max(1LL, Y/X); > 35 pr += k*pl; > 36 qr += k*ql; > 37 } > 38 else if( fleq(pm,qm,a,b) ) { > 39 // pl = pm, ql = qm; // [pm/qm, pr/qr] > 40 LL X = b*pr - a*qr; > 41 LL Y = a*ql - b*pl; > 42 LL k = max(1LL, Y/X); > 43 pl += k*pr; > 44 ql += k*qr; > 45 } > 46 else { > 47 cout << pm+qm*base << "/" << qm << endl; > 48 break; > 49 } > 50 } > 51 } > 52 */
Added _lib/typical/amap.cpp version [8e84d319ff86cb61]
> 1 // > 2 // accumulative map > 3 // basically a map<K,V>, but it eliminates duplication lazily > 4 // first keep all inserted elements in a vector. only when the buffer > 5 // overflows, it eliminates duplication > 6 // > 7 > 8 template<typename K, typename V, int LIMIT = (1<<26)/3/sizeof(pair<K,V>)> > 9 struct amap > 10 { > 11 typedef typename vector< pair<K,V> >::iterator iterator; > 12 > 13 vector< pair<K,V> > kv; > 14 amap() { kv.reserve(LIMIT); } > 15 > 16 void add(const K& k, const V& v) > 17 { > 18 kv.push_back( make_pair(k,v) ); > 19 if( kv.size() == LIMIT ) > 20 normalize(); > 21 } > 22 > 23 iterator begin() { return kv.begin(); } > 24 iterator end() { return kv.end(); } > 25 void swap( amap& rhs ) { kv.swap(rhs.kv); } > 26 > 27 // Tested: SRM 469 Lv3 > 28 void normalize() > 29 { > 30 sort(kv.begin(), kv.end()); > 31 int i=0; > 32 for(int j=0; j<kv.size(); ++i) > 33 { > 34 int k = j; > 35 kv[i] = kv[k]; > 36 while( ++j<kv.size() && kv[k].first==kv[j].first ) > 37 kv[i].second += kv[j].second; > 38 } > 39 kv.resize(i); > 40 } > 41 /* > 42 // Not Tested (Prefer First) > 43 void normalize() > 44 { > 45 sort(kv.begin(), kv.end()); > 46 int i=0; > 47 for(int j=0; j<kv.size(); ++i) > 48 { > 49 int k = j; > 50 kv[i] = kv[k]; > 51 while( ++j<kv.size() && kv[k].first==kv[j].first ) > 52 {} > 53 } > 54 kv.resize(i); > 55 } > 56 > 57 // Not Tested (Prefer Last) > 58 void normalize() > 59 { > 60 sort(kv.begin(), kv.end()); > 61 int i=0; > 62 for(int j=0; j<kv.size(); ++i) > 63 { > 64 int k = j; > 65 while( ++j<kv.size() && kv[k].first==kv[j].first ) > 66 {} > 67 kv[i] = kv[j-1]; > 68 } > 69 kv.resize(i); > 70 } > 71 */ > 72 };
Added _lib/typical/bfs.cpp version [b584e6b7fe45956b]
> 1 // code template for BFS > 2 > 3 State start = /*start*/; > 4 vector<State> Q( 1, start ); > 5 set<State> V; V.insert(start); > 6 > 7 for(int step=0; !Q.empty(); ++step) > 8 { > 9 vector<State> Qold; Qold.swap(Q); > 10 for(int qi=0; qi<Qold.size(); ++qi) > 11 { > 12 State& cur = Qold[qi]; > 13 if( /*isGoal(cur)*/ ) > 14 { > 15 } > 16 > 17 foreach(next) > 18 { > 19 if( !V.count(next) ) > 20 { > 21 V.insert(next); > 22 Q.push_back(next); > 23 } > 24 } > 25 } > 26 }
Added _lib/typical/bitop.cpp version [80d6730d2c161aa4]
Added _lib/typical/comb.cpp version [d63ada0513560bce]
> 1 //------------------------------------------------------------- > 2 // number of combinations choosing k out of n > 3 // # you might better consider to use Pascal's triangle > 4 // # for comb modulo some number... > 5 // > 6 // Verified by > 7 // - SRM 350 Div1 LV2 > 8 //------------------------------------------------------------- > 9 > 10 LL comb(LL n, LL k) > 11 { > 12 k = min(k, n-k); > 13 > 14 LL c = 1; > 15 for(LL i=0; i<k; ++i) > 16 c *= n-i, c /= i+1; > 17 return c; > 18 }
Added _lib/typical/dp.cpp version [81322e1ce041bb24]
> 1 // Tested: SRM 454 Lv2 > 2 template<typename T> > 3 struct DP2 > 4 { > 5 const int N1, N2; > 6 vector<T> data; > 7 DP2(int N1, int N2, const T& t = T()) > 8 : N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)< > 9 T& operator()(int i1, int i2) > 10 { return data[ (i1*N2)+i2 ]; } > 11 void swap(DP2& rhs) > 12 { data.swap(rhs.data); } > 13 }; > 14 > 15 // Tested: Codeforces #13 C > 16 template<typename T> > 17 struct DP2x > 18 { > 19 const int N1, N2; > 20 vector<T> data; > 21 DP2x(int, int N2, const T& t = T()) > 22 : N1(2), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<( > 23 T& operator()(int i1, int i2) > 24 { i1&=1; return data[ (i1*N2)+i2 ]; } > 25 void swap(DP2x& rhs) > 26 { data.swap(rhs.data); } > 27 }; > 28 > 29 // Tested: SRM 452 Lv3 > 30 template<typename T> > 31 struct DP3 > 32 { > 33 int N1, N2, N3; > 34 vector<T> data; > 35 DP3(int N1, int N2, int N3, const T& t = T()) > 36 : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size() > 37 T& operator()(int i1, int i2, int i3) > 38 { return data[ ((i1*N2)+i2)*N3+i3 ]; } > 39 void swap(DP3& rhs) > 40 { data.swap(rhs.data); } > 41 }; > 42 > 43 // Tested: SRM 468 Lv2 > 44 template<typename T> > 45 struct DP3x > 46 { > 47 int N1, N2, N3; > 48 vector<T> data; > 49 DP3x(int, int N2, int N3, const T& t = T()) > 50 : N1(2), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()* > 51 T& operator()(int i1, int i2, int i3) > 52 { i1&=1; return data[ ((i1*N2)+i2)*N3+i3 ]; } > 53 void swap(DP3x& rhs) > 54 { data.swap(rhs.data); } > 55 }; > 56 > 57 // Not Tested > 58 template<typename T> > 59 struct DP4 > 60 { > 61 int N1, N2, N3, N4; > 62 vector<T> data; > 63 DP4(int N1, int N2, int N3, int N4, const T& t = T()) > 64 : N1(N1), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert( > 65 T& operator()(int i1, int i2, int i3, int i4) > 66 { return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } > 67 void swap(DP4& rhs) > 68 { data.swap(rhs.data); } > 69 }; > 70 > 71 // Not Tested > 72 template<typename T> > 73 struct DP4x > 74 { > 75 int N1, N2, N3, N4; > 76 vector<T> data; > 77 DP4x(int, int N2, int N3, int N4, const T& t = T()) > 78 : N1(2), N2(N2), N3(N3), N4(N4), data(N1*N2*N3*N4, t) { assert(d > 79 T& operator()(int i1, int i2, int i3, int i4) > 80 { i1&=1; return data[ (((i1*N2)+i2)*N3+i3)*N4+i4 ]; } > 81 void swap(DP4x& rhs) > 82 { data.swap(rhs.data); } > 83 }; > 84 > 85 > 86 // Tested: SRM 351 Lv2 > 87 template<typename T> > 88 struct DP5 > 89 { > 90 int N1, N2, N3, N4, N5; > 91 vector<T> data; > 92 DP5(int N1, int N2, int N3, int N4, int N5, const T& t = T()) > 93 : N1(N1), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t > 94 T& operator()(int i1, int i2, int i3, int i4, int i5) > 95 { return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; } > 96 void swap(DP5& rhs) > 97 { data.swap(rhs.data); } > 98 }; > 99 > 100 // Not Tested > 101 template<typename T> > 102 struct DP5x > 103 { > 104 int N1, N2, N3, N4, N5; > 105 vector<T> data; > 106 DP5x(int, int N2, int N3, int N4, int N5, const T& t = T()) > 107 : N1(2), N2(N2), N3(N3), N4(N4), N5(N5), data(N1*N2*N3*N4*N5, t) > 108 T& operator()(int i1, int i2, int i3, int i4, int i5) > 109 { i1&=1; return data[ ((((i1*N2)+i2)*N3+i3)*N4+i4)*N5+i5 ]; } > 110 void swap(DP5x& rhs) > 111 { data.swap(rhs.data); } > 112 };
Added _lib/typical/idGen.cpp version [01a2602f05b036ac]
> 1 //------------------------------------------------------------- > 2 // ID Assignment > 3 // > 4 // Verified by > 5 // - ACM/ICPC Tokyo 2010 A > 6 // - SRM 491 Div1 LV3 > 7 //------------------------------------------------------------- > 8 > 9 template<typename T> > 10 class IdGen > 11 { > 12 map<T, int> v2id_; > 13 vector<T> id2v_; > 14 public: > 15 int v2id(const T& v) { > 16 if( !v2id_.count(v) ) { v2id_[v] = size(); id2v_.push_back(v); } > 17 return v2id_[v]; > 18 } > 19 const T& id2v(int i) const { return id2v_[i]; } > 20 int size() const { return id2v_.size(); } > 21 };
Added _lib/typical/inMap.cpp version [a5a448928ab96673]
Added _lib/typical/maxRectArea.cpp version [73000b9fbf0fd9f6]
> 1 //------------------------------------------------------------- > 2 // h[] : list of heights > 3 // i-th rectangle is located at (i,0)--(i+1,h[i]) > 4 // > 5 // calculate the area of maximum sub-rectangle > 6 // > 7 // Verified by > 8 // - SRM 337 Div1 LV2 > 9 //------------------------------------------------------------- > 10 > 11 // solve > 12 vector<int> left(n); > 13 { > 14 map<LL, int> h; h[-1] = -1; > 15 for(int i=0; i<n; ++i) { > 16 // position of the highest building < R[i] > 17 map<LL,int>::iterator it = h.lower_bound(R[i]); > 18 left[i] = (--it)->second+1; > 19 h.erase( ++it, h.end() ); > 20 h[ R[i] ] = i; > 21 } > 22 } > 23 vector<int> right(n); > 24 { > 25 map<LL, int> h; h[-1] = n; > 26 for(int i=n-1; i>=0; --i) { > 27 // position of the highest building < R[i] > 28 map<LL,int>::iterator it = h.lower_bound(R[i]); > 29 right[i] = (--it)->second-1; > 30 h.erase( ++it, h.end() ); > 31 h[ R[i] ] = i; > 32 } > 33 } > 34 LL ans = 0; > 35 for(int i=0; i<n; ++i) { > 36 LL area = R[i] * (right[i] - left[i] + 1); > 37 ans = max(ans, area); > 38 } > 39 return ans;
Added _lib/typical/setCover.cpp version [3071774087420287]
> 1 > 2 //------------------------------------------------------------- > 3 // (Minimum) Set Cover > 4 // NP-complete > 5 // > 6 // Consider using SetCoverEasy, when mask is downward closed. > 7 // > 8 // Verified by > 9 // - SRM345 Div1 LV3 > 10 //------------------------------------------------------------- > 11 > 12 LL next_combination(LL p) > 13 { > 14 LL lsb = p & -p; > 15 LL rem = p + lsb; > 16 LL rit = rem & ~p; > 17 return rem|(((rit/lsb)>>1)-1); > 18 } > 19 > 20 // Is it possible to cover the goal by at most k elements from mask? > 21 // Assumption: goal \subseteq \bigcup mask > 22 bool canCover( LL goal, set<LL>& mask, int k ) > 23 { > 24 // optimizer > 25 for(bool update=true; update; ) { > 26 update = false; > 27 > 28 // if *it \subseteq *jt for some jt, then we NEVER use it > 29 // if *it is the only mask covering some city, we ALWAYS use it > 30 for(set<LL>::iterator it=mask.begin(); it!=mask.end(); ) { > 31 bool isSubset = false; > 32 LL onlyByMe = *it & goal; > 33 for(set<LL>::iterator jt=mask.begin(); jt!=mask.end(); + > 34 if( it!=jt ) { > 35 onlyByMe &= ~*jt; > 36 isSubset |= (*it & *jt & goal) == (*it & > 37 } > 38 > 39 update |= isSubset | !!onlyByMe; > 40 > 41 if( isSubset ) > 42 mask.erase(it++); > 43 else if( onlyByMe ) { > 44 if( --k < 0 ) > 45 return false; > 46 goal &= ~*it; > 47 mask.erase(it++); > 48 } > 49 else > 50 ++it; > 51 } > 52 > 53 if( mask.size()<=k || goal==0 ) > 54 return true; > 55 } > 56 > 57 // exhaustive search > 58 vector<LL> ms(mask.begin(), mask.end()); > 59 for(LL i=(1LL<<k)-1; i<(1LL<<ms.size()); i=next_combination(i)) > 60 { > 61 LL gg = goal; > 62 for(LL j=0; (1LL<<j)<=i; ++j) > 63 if( i & (1<<j) ) > 64 gg &= ~ms[j]; > 65 if( gg == 0 ) > 66 return true; > 67 } > 68 return false; > 69 }
Added _lib/typical/setCoverEasy.cpp version [d2930c13625ebb92]
> 1 > 2 //------------------------------------------------------------- > 3 // (Minimum) Set Cover > 4 // NP-complete > 5 // > 6 // Assumption: "mask" must be downward closerd, > 7 // i.e., X subset Y & Y in mask => X in mask > 8 // > 9 // Verified by > 10 // - Google Code Jam 09 Round2 C > 11 // (The "Only" part never tested, in the test cases of > 12 // the problem, that optimization had never used. > 13 // Be careful to use that; to be on the safer side, > 14 // consider using the <true,false> mode.) > 15 //------------------------------------------------------------- > 16 > 17 int minCover_exhaustive( int goal, const set<int>& mask ) > 18 { > 19 typedef int STATE; > 20 > 21 vector<STATE> Q( 1, 0 ); > 22 set<STATE> visited; > 23 for(int step=0; step<=mask.size(); ++step) > 24 { > 25 vector<STATE> Q2; > 26 for(int i=0; i<Q.size(); ++i) > 27 { > 28 STATE s = Q[i]; > 29 if( s == goal ) > 30 return step; > 31 for(set<int>::const_iterator it=mask.begin(); it!=mask.e > 32 if( !visited.count( s|*it ) ) > 33 { > 34 visited.insert( s|*it ); > 35 Q2.push_back( s|*it ); > 36 } > 37 } > 38 Q.swap(Q2); > 39 } > 40 return -1; > 41 } > 42 > 43 template<bool Subs, bool Only> > 44 int minCover( int goal, set<int> mask ) > 45 { > 46 if( Subs ) > 47 for(set<int>::iterator it=mask.begin(); it!=mask.end(); ) > 48 { > 49 bool needed = true; > 50 for(int m=1; m<=goal; m<<=1) > 51 if( (goal&m) && !(*it&m) && mask.count(*it|m)) > 52 {needed = false; break;} > 53 > 54 if( needed ) > 55 ++it; > 56 else > 57 mask.erase(it++); > 58 } > 59 > 60 if( Only ) > 61 for(set<int>::iterator it=mask.begin(); it!=mask.end(); ) > 62 { > 63 int onlyByMe = goal & *it; > 64 for(set<int>::iterator jt=mask.begin(); jt!=mask.end(); > 65 onlyByMe &= ~*jt; > 66 > 67 if( !onlyByMe ) > 68 ++it; > 69 else > 70 mask.erase(it++), goal &= ~onlyByMe; > 71 } > 72 > 73 return minCover_exhaustive(goal, mask); > 74 }
Added _lib/typical/ternery.cpp version [c1b48b3999a9f65a]
> 1 // ternery search (for shita-ni-totsu) > 2 { > 3 double x = ~min~; > 4 double w = ~max~; > 5 for(int i=0; i<~iteration~; ++i) // or, while( w-x > ~eps~ ) be careful > 6 { > 7 double y = 2*x/3 + w/3; > 8 double z = x/3 + 2*w/3; > 9 double fx = f(x); > 10 double fy = f(y); > 11 double fz = f(z); > 12 double fw = f(w); > 13 if( fx < fy ) w = y; > 14 else if( fz > fw ) x = z; > 15 else if( fy < fz ) w = z; > 16 else x = y; > 17 } > 18 }
Added _lib/typical/unionfind.cpp version [734bdff2cfc3cb9d]
> 1 //------------------------------------------------------------- > 2 // Union-Find > 3 // Balancing + Path-Compression : O(invAckermann) > 4 // > 5 // Verified by > 6 // - SRM Member Pilot 2 Div1 LV2 > 7 //------------------------------------------------------------- > 8 > 9 struct UnionFind > 10 { > 11 vector<int> uf, sz; > 12 int nc; > 13 > 14 UnionFind(int N): uf(N), sz(N,1), nc(N) > 15 { for(int i=0; i<N; ++i) uf[i] = i; } > 16 int size() > 17 { return nc; } > 18 int Find(int a) > 19 { return uf[a]==a ? a : uf[a]=Find(uf[a]); } > 20 bool Union(int a, int b) > 21 { > 22 a = Find(a); > 23 b = Find(b); > 24 if( a != b ) > 25 { > 26 if( sz[a] >= sz[b] ) swap(a, b); > 27 uf[a] = b; > 28 sz[b] += sz[a]; > 29 --nc; > 30 } > 31 return (a!=b); > 32 } > 33 };