Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3078 | Accepted: 1235 |
Description
The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a number, then Christine again, and so on. The following rules restrict how new numbers may be chosen by the two players:
- A number which has already been selected by Christine or Matt, or a multiple of such a number,cannot be chosen.
- A sum of such multiples cannot be chosen, either.
If a player cannot choose any new number according to these rules, then that player loses the game.
Here is an example: Christine starts by choosing 4. This prevents Matt from choosing 4, 8, 12, etc.Let's assume that his move is 3. Now the numbers 3, 6, 9, etc. are excluded, too; furthermore, numbers like: 7 = 3+4;10 = 2*3+4;11 = 3+2*4;13 = 3*3+4;... are also not available. So, in fact, the only numbers left are 2 and 5. Christine now selects 2. Since 5=2+3 is now forbidden, she wins because there is no number left for Matt to choose.
Your task is to write a program which will help play (and win!) the Number Game. Of course, there might be an infinite number of choices for a player, so it may not be easy to find the best move among these possibilities. But after playing for some time, the number of remaining choices becomes finite, and that is the point where your program can help. Given a game position (a list of numbers which are not yet forbidden), your program should output all winning moves.
A winning move is a move by which the player who is about to move can force a win, no matter what the other player will do afterwards. More formally, a winning move can be defined as follows.
- A winning move is a move after which the game position is a losing position.
- A winning position is a position in which a winning move exists. A losing position is a position in which no winning move exists.
- In particular, the position in which all numbers are forbidden is a losing position. (This makes sense since the player who would have to move in that case loses the game.)
Input
Each line will start with a number n (1 <= n <= 20), the number of integers which are still available. The remainder of this line contains the list of these numbers a1;...;an(2 <= ai <= 20).
The positions described in this way will always be positions which can really occur in the actual Number Game. For example, if 3 is not in the list of allowed numbers, 6 is not in the list, either.
At the end of the input, there will be a line containing only a zero (instead of n); this line should not be processed.
Output
Sample Input
2 2 5 2 2 3 5 2 3 4 5 6 0
Sample Output
Test Case #1 The winning moves are: 2 Test Case #2 There's no winning move. Test Case #3 The winning moves are: 4 5 6
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1000005
#define mod 100007
using namespace std;
typedef long long ll;
bool aa[25];
int dp[1<<20][2];
int dfs(int first,int x,int pre,int a[],int times)
{
int cur=pre-(1<<(x-1));
if(dp[cur][times%2]!=-1){
return dp[cur][times%2];
}
vector<int> g;
a[x]=0;
g.push_back(x);
int countt=0;
/*while(tmp<=20){ //消去倍數
if(a[tmp]==1){
a[tmp]=0;
g.push_back(tmp);
}
tmp+=x;
}*/
for(int i=2;i<=20;i+=1){ //消
if(a[i]==0){
int tmp=x+i;
if(a[tmp]==1){
a[tmp]=0;
g.push_back(tmp);
}
}
}
int flag=0;
for(int i=2;i<=20;i+=1){ //找出還未被消去的數進行遞歸
if(a[i]==1){
if(dfs(first,i,cur,a,times+1)){
flag=1;
}
countt+=1;
}
}
int sz=g.size();
for(int i=0;i<sz;i+=1){
a[g[i]]=1;
}
if(!countt){
return dp[cur][times%2]=(times%2!=0?1:0);
}
return dp[cur][times%2]=(times%2!=0?flag:!flag);
}
int main()
{
int n,t=0;
memset(dp,-1,sizeof(dp));
while(scanf("%d",&n)!=EOF){
int cur=0;
if(!n) break;
t+=1;
int flag=0;
int a[25];
memset(aa,false,sizeof(aa));
memset(a,0,sizeof(a));
for(int i=0;i<n;i+=1){
int num;
scanf("%d",&num);
cur|=(1<<(num-1));
a[num]=1;
}
for(int i=2;i<=20;i+=1){
if(a[i]==1&&dfs(i,i,cur,a,1)){
aa[i]=true;
flag=1;
}
}
printf("Test Case #%d\n",t);
if(!flag){
printf("There's no winning move.\n");
continue;
}
printf("The winning moves are: ");
for(int i=2;i<=20;i+=1){
if(aa[i]==true){
printf("%d ",i);
}
}
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<math.h>
int dp[1048600];
int find(int a[],int n,int k)
{
int i,b[21];
for(i=2;i<21;i++)
b[i]=a[i];
b[n]=0;
k=k-(int )pow(2,n-2);
for(i=2;i<21-n;i++)
{
if(!b[i])
if(b[i+n])
{
b[i+n]=0;
k=k-(int )pow(2,i+n-2);
}
}
if(dp[k]!=-1) return dp[k];
for(i=2;i<21;i++)
if(b[i])
break;
if(i<21)
{
for(i=2;i<21;i++)
{
if(b[i])
if(find(b,i,k))
break;
}
if(i<21)
return dp[k]=0;
else
return dp[k]=1;
}
else
{
return dp[k]=1;
}
}
int main()
{
int i,n,num,count,k,a[21],b[21],Case=0;
for(i=0;i<1048600;i++)
dp[i]=-1;
while(scanf("%d",&n)&&n)
{
Case++;
for(i=2;i<21;i++)
a[i]=0;
k=0;
for(i=0;i<n;i++)
{
scanf("%d",&num);
a[num]=1;
k=k+(int )pow(2,num-2);
}
count=0;
for(i=2;i<=20;i++)
{
if(a[i])
{
if(find(a,i,k))
{
b[count]=i;
count++;
}
}
}
if(count>0)
{
printf("Test Case #%d\nThe winning moves are:",Case);
for(i=0;i<count;i++)
printf(" %d",b[i]);
putchar('\n');
}
else
{
printf("Test Case #%d\nThere's no winning move.\n",Case);
}
putchar('\n');
}
}