hdu 4111 Alice and Bob---sg dp記憶化sgを求める
Alice and Bob
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 598 Accepted Submission(s): 207
Problem Description
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they usually did.
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1. Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
Input
The first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A
1 ....A
N(1 <= A
i <= 1000), represents the numbers they write down at the beginning of the game.
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice"or "Bob".
Sample Input
Sample Output
Source
2011 Asia ChengDu Regional Contest
コードはこれを参照してください.http://blog.csdn.net/ahero_happy/article/details/6955595
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 598 Accepted Submission(s): 207
Problem Description
Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they always find the best strategy, and that's why they feel bored again and again. They just invented a new game, as they usually did.
The rule of the new game is quite simple. At the beginning of the game, they write down N random positive integers, then they take turns (Alice first) to either:
1. Decrease a number by one.
2. Erase any two numbers and write down their sum.
Whenever a number is decreased to 0, it will be erased automatically. The game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
Input
The first line contains an integer T(1 <= T <= 4000), indicating the number of test cases.
Each test case contains several lines.
The first line contains an integer N(1 <= N <= 50).
The next line contains N positive integers A
1 ....A
N(1 <= A
i <= 1000), represents the numbers they write down at the beginning of the game.
Output
For each test case in the input, print one line: "Case #X: Y", where X is the test case number (starting with 1) and Y is either "Alice"or "Bob".
Sample Input
3
3
1 1 2
2
3 4
3
2 3 5
Sample Output
Case #1: Alice
Case #2: Bob
Case #3: Bob
Source
2011 Asia ChengDu Regional Contest
コードはこれを参照してください.http://blog.csdn.net/ahero_happy/article/details/6955595
/*
: N , , , :
1、 ( 0 , );2、
。
: 1, , 。
。 1, 。
1 , , 1 ,
( 2 , 1 )。
dp[i][j] i 1 , j。 1 , 0
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int dp[55][55000];
int a[55];
int DP(int a,int b)
{
if(dp[a][b]!=-1) return dp[a][b];
if(b==1) return dp[a][b]=DP(a+1,0);// , 1
dp[a][b]=0;
if(a>=1&&!DP(a-1,b))// 1
dp[a][b]=1;
if(a>=1&&b&&!DP(a-1,b+1))// 1
dp[a][b]=1;
if(a>=2&&((b&&!DP(a-2,b+3))||(b==0&&!DP(a-2,2))))// 2 1
dp[a][b]=1;
if(b>=2&&!DP(a,b-1))//
dp[a][b]=1;
return dp[a][b];
}
int main()
{
memset(dp,-1,sizeof(dp));
int t,test=1,n,i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(j=0,k=0,i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==1) j++;
else k+=a[i]+1;//+1 ,
}
if(k) k--;//
DP(j,k);
printf("Case #%d: ",test++);
if(dp[j][k]) printf("Alice
");
else printf("Bob
");
}
return 0;
}