博弈论5

Queer Game

 

描述

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?  

 

输入

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

 

输出

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

 

样例输入

5 1
8 2
6 3

 

样例输出

LOSE
LOSE
WIN

分析:

大致题意:Caspar和Rapsac在一堆石头中取石子,最少取一个,最多取k个;如果轮到Rapsac,并且剩余的石子的数目为素数的话,Rapsac可以取到k+1个

思路:其实前面的叙述就是巴什博弈,所以先判断最基本的情况,如果n%(k+1)==0,Rapsac就按巴什博弈的方法走,Caspar也必输;否则,Caspar肯定会按巴什博弈的方法走,不然他肯定也会输

但如果他按巴什博弈的方法走,最后留给Rapsac肯定为k+1个(因为前面留给Rapsac的都是(k+1)的倍数,那肯定不是素数),所以只需现在判断(k+1)是否为素数就行了,如果是,Rapsac就能一起取完它们,Caspar就输了;如果不是,Caspar就能赢了

代码如下:

 

 1 # include<stdio.h>
 2 # include<math.h>
 3 int Judge(int num)
 4 {
 5     int i,leap=1;
 6     int len=sqrt((double)num);
 7     for(i=2;i<=len;i++)
 8     {
 9         if(num%i==0)
10         {
11             leap=0;
12             break;
13         }
14     }
15     return leap;
16 }
17 int main()
18 {
19     int n,k;
20     while(scanf("%d %d",&n,&k)!=EOF)
21     {
22         if(n%(k+1)==0)
23         {
24             printf("LOSE\n");
25             continue;
26         }
27         if(n>k&&Judge(k+1))//这里要保证n>k,不然Caspar就能一次取完
28         {
29             printf("LOSE\n");
30         }
31         else printf("WIN\n");
32     }
33     return 0;
34 }

 

posted on 2014-09-16 15:21  会敲键盘的猩猩  阅读(157)  评论(0编辑  收藏  举报