Ural1087

来源:互联网 发布:网络利大于弊辩论赛 编辑:程序博客网 时间:2024/05/16 03:00

1087. The Time to Take Stones

Time limit: 1.0 second
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 k1, k2, …, 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 andm (1 ≤ n ≤ 10000; 1 ≤ m ≤ 50) — they are an initial amount of stones in the pile and an amount of numbersk1, …, km. The second line consists of the numbersk1, …, km, separated with a space (1 ≤kin).

Output

Output 1, if the first player (the first to take stones) wins in a perfect game.Otherwise, output 2.

Sample

inputoutput
17 31 3 4
2

题意很明确的哈,这里不多讲了,dp做的好的孩纸看到这题会很开心的A掉,像我这种渣渣dp不太会的就只能挣扎辣
思路:
 dp[i]表示n==i时的赢家,i<=k的最小值时dp[i]=2;当且仅当存在m使得dp[i-k[m]]=2,才会dp[i]=1;

//dp是由整体来考虑的,不是局部。。。 #include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int a[60];int dp[11000]; //dp[i]表示n=i时谁赢  int main(){int n,k;int i,j;cin>>n>>k;int sum=0;memset(dp,0,sizeof(dp));for(i=0;i<k;i++)  cin>>a[i];sort(a,a+k);     for(i=1;i<=n;i++)    {    if(i<=a[0])dp[i]=2;    for(j=0;j<k;j++)    {    if(i>a[j])    {    if(dp[i-a[j]]==2)    {    dp[i]=1;    break;    }    }    }    if(j==k)dp[i]=2;    }    cout<<dp[n]<<endl;return 0;}


0 0
原创粉丝点击