Skip to content

Commit d1cfdc3

Browse files
committed
srm 640 div 2 600
1 parent 6e97ad9 commit d1cfdc3

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Diff for: NumberGameAgain.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
#include <cmath>
12+
using namespace std;
13+
typedef vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
typedef long long ll;
18+
typedef pair<ll, ll> pi;
19+
20+
21+
class NumberGameAgain{
22+
public:
23+
long long solve(int k, vector<long long> table) {
24+
sort(table.begin(), table.end());
25+
set<pi> S;
26+
ll r = (1LL << k) - 2;
27+
// cout<<"r = "<<r<<endl;
28+
for(ll n : table){
29+
ll x = log2(n), l = k-x;
30+
// cout<<n<<" "<<x<<" "<<l<<endl;
31+
32+
bool done = true;
33+
for(pi p : S){
34+
if(n >= p.first && n <= p.second){
35+
done = false;
36+
break;
37+
}
38+
}
39+
40+
if(done){
41+
r -= ((1LL << l) - 1);
42+
ll low = n, high = n;
43+
S.insert(make_pair(low, high));
44+
while(--l){
45+
high = 2*high+1;
46+
low = 2*low;
47+
// cout<<"[ "<<low<<" , "<<high<<" ]"<<endl;
48+
S.insert(make_pair(low, high));
49+
}
50+
}
51+
52+
}
53+
return r;
54+
}
55+
56+
// BEGIN CUT HERE
57+
public:
58+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
59+
private:
60+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
61+
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
62+
void test_case_0() { int Arg0 = 3; long Arr1[] = {2,4,6}; vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 2LL; verify_case(0, Arg2, solve(Arg0, Arg1)); }
63+
void test_case_1() { int Arg0 = 5; long Arr1[] = {2,3}; vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 0LL; verify_case(1, Arg2, solve(Arg0, Arg1)); }
64+
void test_case_2() { int Arg0 = 20; long Arr1[] = {88, 138390, 363924, 18595, 148, 179170, 29, 490195, 28, 67, 533, 980413, 1033224, 227290, 652478, 786172, 27, 5, 768}; vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 561093LL; verify_case(2, Arg2, solve(Arg0, Arg1)); }
65+
void test_case_3() { int Arg0 = 40; long Arr1[] = {2,4,8,16,32141531,2324577,1099511627775,2222222222,33333333333,4444444444,2135}; vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 549755748288LL; verify_case(3, Arg2, solve(Arg0, Arg1)); }
66+
void test_case_4() { int Arg0 = 40; long Arr1[] = {}; vector<long long> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); long long Arg2 = 1099511627774LL; verify_case(4, Arg2, solve(Arg0, Arg1)); }
67+
68+
// END CUT HERE
69+
70+
};
71+
72+
// BEGIN CUT HERE
73+
int main(){
74+
75+
NumberGameAgain ___test;
76+
___test.run_test(-1);
77+
}
78+
// END CUT HERE

Diff for: NumberGameAgain.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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><p>
2+
In this problem, you are going to play a simple number game.
3+
The rules of the game are as follows:
4+
<ul>
5+
<li>You have a single variable called x. Initially, x is set to 1.</li>
6+
<li>In each step, you can change the value of x either to 2x or to 2x+1.</li>
7+
<li>You are given a list of forbidden values. If at any moment your x is on the list, you lose the game.</li>
8+
<li>You are also given a target value y. If at any moment your x is equal to y, you win the game. (Note that the previous item applies sooner: if y is forbidden, you lose the game when you reach it.)</li>
9+
<li>If at any moment winning the game becomes impossible, you lose the game.</li>
10+
</ul>
11+
</p>
12+
13+
<p>
14+
For example, assume that the forbidden values are 4 and 7, and the goal is 12.
15+
You can win the game as follows: change x from 1 to 2*1+1=3, then from 3 to 2*3=6, and then from 6 to 2*6=12.
16+
</p>
17+
18+
<p>
19+
You are given a vector&lt;long long&gt; <b>table</b>.
20+
The elements of <b>table</b> are the forbidden values.
21+
</p>
22+
23+
<p>
24+
You are also given a int <b>k</b>.
25+
Consider all possible goals y between 2 and (2^<b>k</b>)-1, inclusive.
26+
For how many of these goals is it possible to win the game?
27+
Compute and return the answer to that question.
28+
</p></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>NumberGameAgain</td></tr><tr><td>Method:</td><td>solve</td></tr><tr><td>Parameters:</td><td>int, vector&lt;long long&gt;</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long solve(int k, vector&lt;long long&gt; table)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>256</td></tr><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>k</b> will be between 2 and 40, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>The number of elements in <b>table</b> will be between 0 and 20, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>all numbers in <b>table</b> will be between 2 and 2^<b>k</b> - 1, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>all numbers in <b>table</b> will be distinct.</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>3</pre></td></tr><tr><td><pre>{2,4,6}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2">There are three forbidden values: 2, 4, and 6.
29+
As k=3, we are considering y between 2 and (2^3)-1 = 7.
30+
This is how the game would end for each of these y's:
31+
<ul>
32+
<li>For y=2 we cannot win the game because 2 is forbidden.</li>
33+
<li>For y=3 we can win the game: we change x from 1 to 3.</li>
34+
<li>For y=4 we cannot win the game because 4 is forbidden.</li>
35+
<li>For y=5 we cannot win the game. We would need to change x from 1 to 2 and then from 2 to 5, but we cannot do that because 2 is forbidden.</li>
36+
<li>For y=6 we cannot win the game because 6 is forbidden.</li>
37+
<li>For y=7 we can win the game: we change x from 1 to 3, and then from 3 to 7.</li>
38+
</ul>
39+
Thus, within the specified range there are two values of y for which we can win the game.
40+
<p><img src="http://www.topcoder.com/contest/problem/NumberGameAgain/pic.png"></img></p></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>5</pre></td></tr><tr><td><pre>{2,3}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2">In this case, we will always reach a forbidden value after the very first step of the game. Therefore, there is no y for which we can win the game.</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>5</pre></td></tr><tr><td><pre>{}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 30</pre></td></tr><tr><td><table><tr><td colspan="2">With no forbidden values we can win this game for any y between 2 and 31, inclusive.</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>40</pre></td></tr><tr><td><pre>{2,4,8,16,32141531,2324577,1099511627775,2222222222,33333333333,4444444444,2135}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 549755748288</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>40</pre></td></tr><tr><td><pre>{}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 1099511627774</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>

0 commit comments

Comments
 (0)