Skip to content

Commit 5d55db1

Browse files
committed
srm 652 div 2 500
1 parent 45def23 commit 5d55db1

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

ThePermutationGameDiv2.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
#define sz size()
19+
#define mp(x, y) make_pair(x, y)
20+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
21+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
22+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
23+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
24+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
25+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
26+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
27+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
28+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
29+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
30+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
31+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
32+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
33+
34+
class ThePermutationGameDiv2{
35+
public:
36+
ll gcd(ll a, ll b)
37+
{
38+
for (;;)
39+
{
40+
if (a == 0) return b;
41+
b %= a;
42+
if (b == 0) return a;
43+
a %= b;
44+
}
45+
}
46+
47+
ll lcm(ll a, ll b)
48+
{
49+
ll temp = gcd(a, b);
50+
return (max(a, b) / temp) * min(a, b);
51+
}
52+
ll findMin(int N){
53+
ll res = 1;
54+
for(ll i = 1; i <= N; i++){
55+
res = lcm(res, i);
56+
}
57+
return res;
58+
}
59+
60+
61+
// BEGIN CUT HERE
62+
public:
63+
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(); }
64+
private:
65+
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(); }
66+
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; } }
67+
void test_case_0() { int Arg0 = 2; long long Arg1 = 2LL; verify_case(0, Arg1, findMin(Arg0)); }
68+
void test_case_1() { int Arg0 = 3; long long Arg1 = 6LL; verify_case(1, Arg1, findMin(Arg0)); }
69+
void test_case_2() { int Arg0 = 6; long long Arg1 = 60LL; verify_case(2, Arg1, findMin(Arg0)); }
70+
void test_case_3() { int Arg0 = 11; long long Arg1 = 27720LL; verify_case(3, Arg1, findMin(Arg0)); }
71+
void test_case_4() { int Arg0 = 25; long long Arg1 = 26771144400LL; verify_case(4, Arg1, findMin(Arg0)); }
72+
73+
// END CUT HERE
74+
75+
};
76+
77+
// BEGIN CUT HERE
78+
int main(){
79+
80+
ThePermutationGameDiv2 ___test;
81+
___test.run_test(-1);
82+
}
83+
// END CUT HERE

ThePermutationGameDiv2.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
Alice and Bob are playing a game called "The Permutation Game".
3+
The game is parameterized with the int <b>N</b>.
4+
At the start of the game, Alice chooses a positive integer x, and Bob chooses a permutation of the first <b>N</b> positive integers.
5+
Let p be Bob's permutation.
6+
Alice will start at 1, and apply the permutation to this value x times.
7+
More formally, let f(1) = p[1], and f(m) = p[f(m-1)] for all m >= 2.
8+
Alice's final value will be f(x).
9+
10+
Alice wants to choose the smallest x such that f(x) = 1 for any permutation Bob can provide.
11+
12+
Compute and return the value of such x.
13+
</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>ThePermutationGameDiv2</td></tr><tr><td>Method:</td><td>findMin</td></tr><tr><td>Parameters:</td><td>int</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long findMin(int N)</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>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>The return value will fit into a signed 64-bit integer.</td></tr><tr><td align="center" valign="top">-</td><td>A permutation of the first N positive integers is a sequence of length N that contains each of the integers 1 through N exactly once. The i-th (1-indexed) element of a permutation p is denoted by p[i].</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 35 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>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 2</pre></td></tr><tr><td><table><tr><td colspan="2"><p>
14+
Bob can choose the permutations {1,2} or {2,1}.
15+
If Alice chooses 1, then, Bob can choose the permutation {2,1}, which would would make f(1) = 2.
16+
However, if Alice chooses 2, no matter which permutation Bob chooses, Alice will get f(2) = 1.
17+
Thus the answer in this case is 2.
18+
</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>3</pre></td></tr></table></td></tr><tr><td><pre>Returns: 6</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>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: 60</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>11</pre></td></tr></table></td></tr><tr><td><pre>Returns: 27720</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>25</pre></td></tr></table></td></tr><tr><td><pre>Returns: 26771144400</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)