Skip to content

Commit a9f7892

Browse files
Add files via upload
1 parent abc2953 commit a9f7892

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
-------------------------------------
3+
4+
First, let's solve the problem for 1 pile. After that take the nim-sum of all the piles' grundy numbers.
5+
6+
From n stones, we can go to (n - fibo[i]), if fibo[i] <= n.
7+
8+
So, grundy(n) = mex{grundy(n - fibo[n])}
9+
10+
There are only 35 fibonacci numbers in the given range ... So, we can perform the operations in time.
11+
12+
----------------------------------------------------------------------
13+
14+
int get_mex(set <int> &state_possible)
15+
{
16+
int i = 0;
17+
18+
while(true)
19+
{
20+
if(state_possible.count(i) == 0)
21+
return i;
22+
23+
i++;
24+
}
25+
}
26+
27+
void compute(vector <int> &grundy, vector <int> &fibonacci, int MAX_STONES)
28+
{
29+
grundy[0] = 0;
30+
31+
for(int i = 1; i <= MAX_STONES; i++)
32+
{
33+
set <int> state_possible;
34+
35+
for(int j = 0; fibonacci[j] <= i; j++)
36+
{
37+
state_possible.insert(grundy[i - fibonacci[j]]);
38+
}
39+
40+
grundy[i] = get_mex(state_possible);
41+
}
42+
}
43+
44+
int main()
45+
{
46+
vector <int> fibonacci(35);
47+
fibonacci[0] = fibonacci[1] = 1;
48+
for(int i = 2; i < 35; i++)
49+
{
50+
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
51+
}
52+
53+
const int MAX_STONES = 3e6;
54+
vector <int> grundy(MAX_STONES);
55+
compute(grundy, fibonacci, MAX_STONES);
56+
57+
int no_of_piles;
58+
scanf("%d", &no_of_piles);
59+
60+
int nim_sum = 0;
61+
while(no_of_piles--)
62+
{
63+
int stones;
64+
scanf("%d", &stones);
65+
66+
nim_sum ^= grundy[stones];
67+
}
68+
69+
printf(nim_sum == 0 ? "Vinit\n" : "Ada\n");
70+
return 0;
71+
}

0 commit comments

Comments
 (0)