Skip to content

Commit 1c3961b

Browse files
committed
srm 650 div 2 250
1 parent e2b2c6e commit 1c3961b

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

TaroJiroDividing.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
typedef long long ll;
18+
class TaroJiroDividing{
19+
public:
20+
int getNumber(int A, int B){
21+
vi a, b;
22+
while(A%2 == 0){
23+
a.push_back(A);
24+
A /= 2;
25+
}
26+
a.push_back(A);
27+
while(B%2 == 0){
28+
b.push_back(B);
29+
B /= 2;
30+
}
31+
b.push_back(B);
32+
int c = 0;
33+
for(int i : a)
34+
for(int j : b)
35+
if(i == j) c++;
36+
return c;
37+
}
38+
39+
40+
// BEGIN CUT HERE
41+
public:
42+
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(); }
43+
private:
44+
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(); }
45+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
46+
void test_case_0() { int Arg0 = 8; int Arg1 = 4; int Arg2 = 3; verify_case(0, Arg2, getNumber(Arg0, Arg1)); }
47+
void test_case_1() { int Arg0 = 4; int Arg1 = 7; int Arg2 = 0; verify_case(1, Arg2, getNumber(Arg0, Arg1)); }
48+
void test_case_2() { int Arg0 = 12; int Arg1 = 12; int Arg2 = 3; verify_case(2, Arg2, getNumber(Arg0, Arg1)); }
49+
void test_case_3() { int Arg0 = 24; int Arg1 = 96; int Arg2 = 4; verify_case(3, Arg2, getNumber(Arg0, Arg1)); }
50+
void test_case_4() { int Arg0 = 1000000000; int Arg1 = 999999999; int Arg2 = 0; verify_case(4, Arg2, getNumber(Arg0, Arg1)); }
51+
52+
// END CUT HERE
53+
54+
};
55+
56+
// BEGIN CUT HERE
57+
int main(){
58+
59+
TaroJiroDividing ___test;
60+
___test.run_test(-1);
61+
}
62+
// END CUT HERE

TaroJiroDividing.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
The dividing game is played as follows:
3+
You start by taking a clean sheet of paper and writing down some positive integer.
4+
Then you repeat the following process:
5+
Let X be the last integer you wrote.
6+
If X is odd, the game ends.
7+
Otherwise, divide X by 2 and write down the result.
8+
</p>
9+
<p>
10+
</p>
11+
<p>
12+
For example, if you start the game by writing 12 you will then write 12/2 = 6, followed by 6/2 = 3, and as 3 is odd, the game ends there.
13+
Your paper now contains the numbers 12, 6, and 3.
14+
</p>
15+
<p>
16+
</p>
17+
<p>
18+
Cat Taro has just played the game starting with the integer <b>A</b>.
19+
Jiro has also played the game but he started with the integer <b>B</b>.
20+
You are given the ints <b>A</b> and <b>B</b>.
21+
Return the number of integers that were written both by Taro and by Jiro.
22+
</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>TaroJiroDividing</td></tr><tr><td>Method:</td><td>getNumber</td></tr><tr><td>Parameters:</td><td>int, int</td></tr><tr><td>Returns:</td><td>int</td></tr><tr><td>Method signature:</td><td>int getNumber(int A, int B)</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>A</b> and <b>B</b> will be between 1 and 1,000,000,000, 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>8</pre></td></tr><tr><td><pre>4</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</pre></td></tr><tr><td><table><tr><td colspan="2">Taro will write the integers {8,4,2,1}. Jiro will write {4,2,1}. The three integers written by both of them are 4, 2, and 1.</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>4</pre></td></tr><tr><td><pre>7</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</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>12</pre></td></tr><tr><td><pre>12</pre></td></tr></table></td></tr><tr><td><pre>Returns: 3</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>24</pre></td></tr><tr><td><pre>96</pre></td></tr></table></td></tr><tr><td><pre>Returns: 4</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>1000000000</pre></td></tr><tr><td><pre>999999999</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</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)