巴什博弈

来源:互联网 发布:淘宝能看买家退货率吗 编辑:程序博客网 时间:2024/04/30 04:39

什么叫巴什博弈:有一堆n个物品,两个人轮流从中拿取,每次可以拿取1-k件之间,最后取完的那个人胜利;

有了定义现在来分析一下如何胜利:定义中说的一次最多拿取k件 ,这里我们动一下脑筋就会得到 ,胜利的条件是这样的推出来的,我们

拿取一定数量的物品使得跟对方留下k+1个物品,对方一次拿不完 ,而此时无论对方拿多少, 之后剩下的你都可以一次拿完,ok,你胜利了

于是我们扩展一下就是这样的:每次都跟对方剩下(k+1)*r件物品(r为自然数);

比如下一步对方拿取w件物品,此时你该拿取多少呢,k+1-w;这样就能使剩下的数量一致保持为k+1的整数倍,

这样就能保证胜利了,记住是每次都要依据这个条件干,如果那一次不这样那对方也就会这样做,你就输了;

生活中很多这样的事情,也有很多事情可以转换成这样的问题来解决

Description

Caspar is queer boy. One day, his queer girlfriend Rapsac queerly asks him to play a queer game with her. This game called “queer game”. Suppose that there are N queer stones. Caspar and Rapsac pick up some (at least 1 while at most K) of them successively. And if there is no stone in someone’s turn, he/she will lose.

Additionally, there are some queer rules would be added into this game. First of all, Caspar would always be the first one to pick up stones. Secondly, if there are M stones in Rapsac’s turn and M is a prime number, Rapsac could pick up to K+1 stones up in this turn.

The question is, assuming that Caspar and Rapsac are smart enough, Caspar would win or not?

Input

THERE ARE MULTIPLE TESTCASES. In each testcase, there are two positive integers N and K in one line. (1<=N<=1000)

Output

For every testcase, you should print the final outcome of Caspar(WIN or LOSE) in one line.

Sample Input

5 1

8 2

6 3

Sample Output

LOSE

LOSE

WIN


这道题的大意是:Caspar和Rapsac玩一个取石头游戏,Caspar先取,在巴什博弈定义中加了一个规则是:如果剩下的k+1个数为一个素数则Rapsac就可以一次取走了;


思路分析:首先判断一个基本条件:如果 n%(k+1)==0,这种情况相当于是对方给Caspar留下了k+1的整数倍,对方按照巴什博弈走哪Caspar必输,

    如果不满足上面的条件 那么Caspar就会按照巴什博弈走,那么到最后要判断的是k+1是否为素数,如果是那Rapsac一次取走,Caspar也输

    如果k+1不是素数那么Caspar胜利;


根据上面的思路写出代码:

#include<stdio.h>bool isPrime(int f){     int i;     for(i=2;i*i<=f;++i)     {          if(f%i==0)          return false;     }     return true;}int main(){    int n,k;    while(scanf("%d%d",&n,&k)!=EOF)    {          if(n%(k+1)==0)          {             printf("LOSE\n");             continue;                     }          if(isPrime(k+1)&&n>k)  //这个地方一定要n>k           {              printf("LOSE\n");              continue;                        }               printf("WIN\n");    }    return 0;}





原创粉丝点击