Skip to content

Commit efd75de

Browse files
unknownunknown
authored andcommitted
srm 575
1 parent 9217423 commit efd75de

4 files changed

Lines changed: 424 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
#include <vector>
9+
#include <queue>
10+
#include <set>
11+
#include <map>
12+
#include <cstdio>
13+
#include <cstdlib>
14+
#include <cctype>
15+
#include <cmath>
16+
#include <string>
17+
#include <cstring>
18+
using namespace std;
19+
20+
// BEGIN CUT HERE
21+
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
22+
23+
template<typename T> void print( T a ) {
24+
cerr << a;
25+
}
26+
static void print( long long a ) {
27+
cerr << a << "L";
28+
}
29+
static void print( string a ) {
30+
cerr << '"' << a << '"';
31+
}
32+
template<typename T> void print( vector<T> a ) {
33+
cerr << "{";
34+
for ( int i = 0 ; i != a.size() ; i++ ) {
35+
if ( i != 0 ) cerr << ", ";
36+
print( a[i] );
37+
}
38+
cerr << "}" << endl;
39+
}
40+
template<typename T> void eq( int n, T have, T need ) {
41+
if ( have == need ) {
42+
cerr << "Case " << n << " passed." << endl;
43+
} else {
44+
cerr << "Case " << n << " failed: expected ";
45+
print( need );
46+
cerr << " received ";
47+
print( have );
48+
cerr << "." << endl;
49+
}
50+
}
51+
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
52+
if( have.size() != need.size() ) {
53+
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
54+
print( have );
55+
print( need );
56+
return;
57+
}
58+
for( int i= 0; i < have.size(); i++ ) {
59+
if( have[i] != need[i] ) {
60+
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
61+
print( have );
62+
print( need );
63+
return;
64+
}
65+
}
66+
cerr << "Case " << n << " passed." << endl;
67+
}
68+
static void eq( int n, string have, string need ) {
69+
if ( have == need ) {
70+
cerr << "Case " << n << " passed." << endl;
71+
} else {
72+
cerr << "Case " << n << " failed: expected ";
73+
print( need );
74+
cerr << " received ";
75+
print( have );
76+
cerr << "." << endl;
77+
}
78+
}
79+
// END CUT HERE
80+
81+
#define REP(i,n) for(int i=0;i<(n);++i)
82+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
83+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
84+
#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it)
85+
#define CLR(x) memset((x),0,sizeof((x)))
86+
typedef long long LL;
87+
typedef vector<int> VI;
88+
typedef vector<string> VS;
89+
90+
// BEGIN CUT HERE
91+
vector<string> split( const string& s, const string& delim =" " ) {
92+
vector<string> res;
93+
string t;
94+
for ( int i = 0 ; i != s.size() ; i++ ) {
95+
if ( delim.find( s[i] ) != string::npos ) {
96+
if ( !t.empty() ) {
97+
res.push_back( t );
98+
t = "";
99+
}
100+
} else {
101+
t += s[i];
102+
}
103+
}
104+
if ( !t.empty() ) {
105+
res.push_back(t);
106+
}
107+
return res;
108+
}
109+
110+
vector<int> splitInt( const string& s, const string& delim =" " ) {
111+
vector<string> tok = split( s, delim );
112+
vector<int> res;
113+
for ( int i = 0 ; i != tok.size(); i++ )
114+
res.push_back( atoi( tok[i].c_str() ) );
115+
return res;
116+
}
117+
// END CUT HERE
118+
119+
// BEGIN CUT HERE
120+
int s2i(string s) {
121+
stringstream ss;
122+
ss << s;
123+
int res;
124+
ss >> res;
125+
return res;
126+
}
127+
128+
string i2s(int n) {
129+
stringstream ss;
130+
ss << n;
131+
string res;
132+
ss >> res;
133+
return res;
134+
}
135+
// END CUT HERE
136+
137+
class TheNumberGameDivOne {
138+
public:
139+
string find(long long n) {
140+
if (n & 1) return "Brus";
141+
if (n & (n - 1)) return "John";
142+
int cnt = 0;
143+
while (n != 1) {
144+
++cnt;
145+
n >>= 1;
146+
}
147+
if (cnt & 1) return "Brus";
148+
else return "John";
149+
}
150+
};
151+
// BEGIN CUT HERE
152+
int main() {
153+
{
154+
TheNumberGameDivOne theObject;
155+
eq(0, theObject.find(6L),"John");
156+
}
157+
{
158+
TheNumberGameDivOne theObject;
159+
eq(1, theObject.find(2L),"Brus");
160+
}
161+
{
162+
TheNumberGameDivOne theObject;
163+
eq(2, theObject.find(747L),"Brus");
164+
}
165+
{
166+
TheNumberGameDivOne theObject;
167+
eq(3, theObject.find(128L),"Brus");
168+
}
169+
return 0;
170+
}
171+
// END CUT HERE
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>John and Brus play a game with a number.
2+
The game starts with a positive integer <b>n</b>.
3+
The two players take alternating turns, John starts.
4+
Each move looks as follows:
5+
Let C be the current value of the integer.
6+
The current player has to choose a positive divisor of the number C, other than 1 and C.
7+
Once he chooses the divisor, he has to subtract it from C.
8+
The result is the new number with which the other player now starts his move.
9+
If a player cannot make a valid move, he loses the game.
10+
<br></br><br></br>
11+
12+
For example, if they start with <b>n</b>=15, one possible gameplay can look as follows:
13+
<ul>
14+
<li>John takes the number 15, chooses its divisor 3, and decreases the number to 15-3 = 12.</li>
15+
<li>Brus takes the number 12, chooses its divisor 4, and decreases the number to 12-4 = 8.</li>
16+
<li>John takes the number 8, chooses its divisor 2, and decreases the number to 8-2 = 6.</li>
17+
<li>Brus takes the number 6, chooses its divisor 3, and decreases the number to 6-3 = 3.</li>
18+
<li>John takes the number 3, and as there are no divisors other than 1 and 3, he has no valid move and thus he loses the game.</li>
19+
</ul>
20+
<br></br><br></br>
21+
22+
You are given the long long <b>n</b>.
23+
Assume that both players use the optimal strategy while playing the game.
24+
Return "John" (quotes for clarity) if John wins the game and "Brus" otherwise.
25+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>TheNumberGameDivOne</td></tr><tr><td>Method:</td><td>find</td></tr><tr><td>Parameters:</td><td>long long</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string find(long long n)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>&#160;&#160;&#160;&#160;</td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>n</b> will be between 1 and 10^18, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;John&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">John has two possible moves: either decrease 6 by 2 or decrease 6 by 3.
26+
If he chooses the second option, Brus will have no possible moves, hence John will win the game.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Brus&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>747</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Brus&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>128</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Brus&quot;</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// BEGIN CUT HERE
2+
3+
// END CUT HERE
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <sstream>
7+
#include <string>
8+
#include <vector>
9+
#include <queue>
10+
#include <set>
11+
#include <map>
12+
#include <cstdio>
13+
#include <cstdlib>
14+
#include <cctype>
15+
#include <cmath>
16+
#include <string>
17+
#include <cstring>
18+
using namespace std;
19+
20+
// BEGIN CUT HERE
21+
#define ARRSIZE(x) (sizeof(x)/sizeof(x[0]))
22+
23+
template<typename T> void print( T a ) {
24+
cerr << a;
25+
}
26+
static void print( long long a ) {
27+
cerr << a << "L";
28+
}
29+
static void print( string a ) {
30+
cerr << '"' << a << '"';
31+
}
32+
template<typename T> void print( vector<T> a ) {
33+
cerr << "{";
34+
for ( int i = 0 ; i != a.size() ; i++ ) {
35+
if ( i != 0 ) cerr << ", ";
36+
print( a[i] );
37+
}
38+
cerr << "}" << endl;
39+
}
40+
template<typename T> void eq( int n, T have, T need ) {
41+
if ( have == need ) {
42+
cerr << "Case " << n << " passed." << endl;
43+
} else {
44+
cerr << "Case " << n << " failed: expected ";
45+
print( need );
46+
cerr << " received ";
47+
print( have );
48+
cerr << "." << endl;
49+
}
50+
}
51+
template<typename T> void eq( int n, vector<T> have, vector<T> need ) {
52+
if( have.size() != need.size() ) {
53+
cerr << "Case " << n << " failed: returned " << have.size() << " elements; expected " << need.size() << " elements.";
54+
print( have );
55+
print( need );
56+
return;
57+
}
58+
for( int i= 0; i < have.size(); i++ ) {
59+
if( have[i] != need[i] ) {
60+
cerr << "Case " << n << " failed. Expected and returned array differ in position " << i << ".";
61+
print( have );
62+
print( need );
63+
return;
64+
}
65+
}
66+
cerr << "Case " << n << " passed." << endl;
67+
}
68+
static void eq( int n, string have, string need ) {
69+
if ( have == need ) {
70+
cerr << "Case " << n << " passed." << endl;
71+
} else {
72+
cerr << "Case " << n << " failed: expected ";
73+
print( need );
74+
cerr << " received ";
75+
print( have );
76+
cerr << "." << endl;
77+
}
78+
}
79+
// END CUT HERE
80+
81+
#define REP(i,n) for(int i=0;i<(n);++i)
82+
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
83+
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
84+
#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it)
85+
#define CLR(x) memset((x),0,sizeof((x)))
86+
typedef long long LL;
87+
typedef vector<int> VI;
88+
typedef vector<string> VS;
89+
90+
// BEGIN CUT HERE
91+
vector<string> split( const string& s, const string& delim =" " ) {
92+
vector<string> res;
93+
string t;
94+
for ( int i = 0 ; i != s.size() ; i++ ) {
95+
if ( delim.find( s[i] ) != string::npos ) {
96+
if ( !t.empty() ) {
97+
res.push_back( t );
98+
t = "";
99+
}
100+
} else {
101+
t += s[i];
102+
}
103+
}
104+
if ( !t.empty() ) {
105+
res.push_back(t);
106+
}
107+
return res;
108+
}
109+
110+
vector<int> splitInt( const string& s, const string& delim =" " ) {
111+
vector<string> tok = split( s, delim );
112+
vector<int> res;
113+
for ( int i = 0 ; i != tok.size(); i++ )
114+
res.push_back( atoi( tok[i].c_str() ) );
115+
return res;
116+
}
117+
// END CUT HERE
118+
119+
// BEGIN CUT HERE
120+
int s2i(string s) {
121+
stringstream ss;
122+
ss << s;
123+
int res;
124+
ss >> res;
125+
return res;
126+
}
127+
128+
string i2s(int n) {
129+
stringstream ss;
130+
ss << n;
131+
string res;
132+
ss >> res;
133+
return res;
134+
}
135+
// END CUT HERE
136+
137+
int mm[2500];
138+
double vv[2500];
139+
140+
class TheSwapsDivOne {
141+
public:
142+
double find(vector <string> sequence, int k) {
143+
string str = "";
144+
REP(i,sequence.size()) str += sequence[i];
145+
int n = str.length();
146+
REP(i,n) mm[i] = str[i] - '0';
147+
148+
double pn = (n - 2) * 1.0 / n, pc = 1 - pn;
149+
double p1 = 1.0, p2 = 1 - p1;
150+
FOR(i,1,k) {
151+
p1 = p1 * pn + p2 * pc / (n - 1);
152+
p2 = 1 - p1;
153+
}
154+
155+
double sum = 0;
156+
REP(i,n) {
157+
vv[i] = (i + 1) * (n - i);
158+
sum += vv[i];
159+
}
160+
161+
double total = 0.0;
162+
REP(i,n) total += mm[i] * ((sum - vv[i]) * p2 / (n - 1) + vv[i] * p1);
163+
return total / (n * (n - 1) / 2 + n);
164+
}
165+
};
166+
// BEGIN CUT HERE
167+
int main() {
168+
{
169+
string sequenceARRAY[] = {"4", "77"};
170+
vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) );
171+
TheSwapsDivOne theObject;
172+
eq(0, theObject.find(sequence, 1),10.0);
173+
}
174+
{
175+
string sequenceARRAY[] = {"4", "77"};
176+
vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) );
177+
TheSwapsDivOne theObject;
178+
eq(1, theObject.find(sequence, 47),10.0);
179+
}
180+
{
181+
string sequenceARRAY[] = {"1", "1", "1", "1", "1", "1", "1"};
182+
vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) );
183+
TheSwapsDivOne theObject;
184+
eq(2, theObject.find(sequence, 1000000),3.0);
185+
}
186+
{
187+
string sequenceARRAY[] = {"572685085149095989026478064633266980348504469", "19720257361", "9", "69"};
188+
vector <string> sequence( sequenceARRAY, sequenceARRAY+ARRSIZE(sequenceARRAY) );
189+
TheSwapsDivOne theObject;
190+
eq(3, theObject.find(sequence, 7),98.3238536775161);
191+
}
192+
return 0;
193+
}
194+
// END CUT HERE

0 commit comments

Comments
 (0)