1087. The Time to Take Stones
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
You probably know the game where two players in turns take 1 to 3 stones from a pile. Looses the one who takes the last stone. We'll generalize this well known game. Assume that both of the players can take not 1, 2 or 3 stones, but
k
1,
k
2, …,
km ones. Again we'll be interested in one question: who wins in the perfect game. It is guaranteed that it is possible to make next move irrespective to already made moves.
Input
The first line contains two integers:
n and
m (
1 ≤
n ≤ 10000;
1 ≤
m ≤ 50
) — they are an initial amount of stones in the pile and an amount of numbers
k
1, …,
km. The second line consists of the numbers
k
1, …,
km, separated with a space (
1 ≤
ki ≤
n
).
Output
Output 1, if the first player (the first to take stones) wins in a perfect game. Otherwise, output 2.
Sample
input | output |
---|---|
17 3 1 3 4 | 2 |
Problem Author: Anton Botov
Problem Source: The 3rd high school children programming contest, USU, Yekaterinburg, Russia, March 4, 2001
Problem Source: The 3rd high school children programming contest, USU, Yekaterinburg, Russia, March 4, 2001
//2个人轮流从一堆石子中取石子,每次可以取走k1, k2,...,km 颗
//递推出必胜态,注意从0开始
#include <iostream>
using namespace std;
int k[55], p[10005];
int main()
{
int n, m, i, j;
cin>>n>>m;
for(i = 1; i <= m; i ++)
cin>>k[i];
p[0] = 1;
for(i = 1; i <= n; i ++)
{
for(j = 1; j <= m; j ++)
{
if(i >= k[j] && p[i-k[j]] == 0)
{
p[i] = 1;
}
}
}
if(p[n])
cout<<"1"<<endl;
else
cout<<"2"<<endl;
return 0;
}