zoj1039 Number Game

16 篇文章 0 订阅
Number Game

Time Limit: 10 Seconds Memory Limit: 32768 KB

Background

Christiane and Matthias are playing a new game, the Number Game. The rules of the Number Game are:

Christian and Matthias take turns in choosing integer numbers greater than or equal to 2. The following rules restrict the set of numbers which may be chosen:

R1 A number which has already been chosen by one of the players or a multiple of such a number cannot be chosen. (A number z is a multiple of a number y if z can be written as y * x and x is a positive integer.)

R2 A sum of two such multiples cannot be chosen either.

R3 For simplicity, a number which is greater than 20 cannot be chosen either. This enables a lot more NPCs (Non-Personal-Computers) to play this game.

The player who cannot choose any number anymore looses the Number Game.

Here is an example: Matthias starts by choosing 4. Then Christiane is not allowed to choose 4, 8, 12, etc. Let us assume her 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 not available. So, in fact, the only numbers left are 2 and 5. Matthias now says 2. Since 5 = 2 + 3 is now forbidden, too, he wins because there is no number for Christiane's move left.

Your task is to write a program which will help to play the Number Game. In general, i.e., without rule R3, this game may go on forever. However, with rule R3, it is possible to write a program that finds a strategy to win the game.


Problem

Given a game situation (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 whose turn it is can force a win no matter what the other player will do. Now we define these terms more formally:

A loosing position is a position in which either

1. all numbers are forbidden, or

2. no winning move exists.

A winning position is a position in which a winning move exists.

A winning move is a move after which the position is a loosing position.


Input

The first line contains the number of scenarios.

The input for each scenario describes a game position. It begins with a line containing the number a, 0 <= a < 20 of numbers which are still available. Next follows a single line with the a numbers still available, separated by single blanks.

You may assume that all game positions in the input could really occur in the Number Game (for example, if 3 is not in the list of numbers available, 6 will not be, either).


Output

The output for each scenario begins with a line containing "Scenario #i:" where i is the number of the scenario starting at 1. In the next line either print "There is no winning move." if this is true for the position of the current scenario, or "The winning moves are: w1 w2 . . . wk." where the wi are all the winning moves, in ascending order, separated by single blanks. The output for each scenario should be followed by a blank line.


Sample Input

2
1
2
2
2 3


Sample Output

Scenario #1:
The winning moves are: 2.

Scenario #2:
There is no winning move

打表,用状态压缩就可以表示出所有的状态,然后主要就是位运算了,其它的也就是找所有的后继状态,这很平常不在多说!

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define M 1<<20
#define mem(a,b) memset(a,b,sizeof(a))
int pri[33],dp[M];
int gstate(int s,int x){
    for(int i=x;i<=20;i=i+x){
        if(s&(1<<(i-2))){
            s=s&(~(1<<(i-2)));
        }
    }
    for(int i=2;i<=20;i++){
        if(s&(1<<(i-2)))
        for(int j=x;i-j>=2;j=j+x){
            if(!(s&(1<<(i-j-2)))){
                 s=s&(~(1<<(i-2)));
                 break;
               }
        }
    }
    return s;
}
int getsg(int x){
    if(dp[x]!=-1)return dp[x];
    int ans=0;
    for(int i=2;i<=20;i++){
        if(x&(1<<(i-2))){
            int k=gstate(x,i);
            if(!getsg(k))
            return dp[x]=1;
        }
    }
    return dp[x]=0;
}
int main()
{
    mem(dp,-1);
    int tcase,n,i,state,tt=1;
    scanf("%d",&tcase);
    while(tcase--){
        scanf("%d",&n);
        for(i=0,state=0;i<n;i++){
            scanf("%d",&pri[i]);
            state|=(1<<(pri[i]-2));
        }
        sort(pri,pri+n);
        printf("Scenario #%d:\n",tt++);
        if(!getsg(state))printf("There is no winning move.\n");
        else {
            printf("The winning moves are:");
            for(i=0;i<n;i++){
                int s=gstate(state,pri[i]);
                if(!getsg(s))
                    printf(" %d",pri[i]);
            }
            printf(".\n");
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值