Problem Description
After hh has learned how to play Nim game, he begins to try another coin game which seems much easier.

The game goes like this: 
Two players start the game with a circle of n coins. 
They take coins from the circle in turn and every time they could take 1~K continuous coins. 
(imagining that ten coins numbered from 1 to 10 and K equal to 3, since 1 and 10 are continuous, you could take away the continuous 10 , 1 , 2 , but if 2 was taken away, you couldn't take 1, 3, 4, because 1 and 3 aren't continuous)
The player who takes the last coin wins the game. 
Suppose that those two players always take the best moves and never make mistakes. 
Your job is to find out who will definitely win the game.
 
Input
The first line is a number T(1<=T<=100), represents the number of case. The next T blocks follow each indicates a case.
Each case contains two integers N(3<=N<=109,1<=K<=10).
 
Output
For each case, output the number of case and the winner "first" or "second".(as shown in the sample output)
 
Sample Input
2
3 1
3 2
Sample Output
Case 1: first Case 2: second
 
题目意思:对于t组样例,有n硬币,编好号,组成环,每一次可以连续的取k个,谁最后取完谁赢。
解题思路:这是一道博弈问题,游戏刚开始的时候所有石子为一条环,先手不可能一次全部取完的情况下,石子就会变成一条链,然而后手只需要创建一个对称的局势就可以取得胜利,就是把这条链分成两条相等的链。我们发现当k=1时,对称局势对游戏没有任何影响,胜负取决于奇偶性;而k>=2时,后手利用对称局势可以取得胜利,先手必输。
#include<stdio.h>
int main()
{
int t,i,k,n,flag;
scanf("%d",&t);
i=;
while(t--)
{
scanf("%d%d",&n,&k);
if(k>=n)//先手胜利
{
flag=;
}
else if(k==)
{
if(n%==)///奇数先手必胜
flag=;
else
flag=;///偶数后手对称拆,必胜
}
else
flag=;///k>1时后手利用对称局势,必胜
if(flag==)
printf("Case %d: first\n",i);
else
printf("Case %d: second\n",i);
i++;
}
return ;
}

Coin Game的更多相关文章

  1. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  2. 洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  3. [luogu2964][USACO09NOV][硬币的游戏A Coin Game] (博弈+动态规划)

    题目描述 Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game c ...

  4. LeetCode Coin Change

    原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...

  5. ACM Coin Test

    Coin Test 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 As is known to all,if you throw a coin up and let ...

  6. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. leetcode:Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  8. UVa 674 Coin Change【记忆化搜索】

    题意:给出1,5,10,25,50五种硬币,再给出n,问有多少种不同的方案能够凑齐n 自己写的时候写出来方案数老是更少(用的一维的) 后来搜题解发现,要用二维的来写 http://blog.csdn. ...

  9. Epic - Coin Change

    Something cost $10.25 and the customer pays with a $20 bill, the program will print out the most eff ...

  10. UVA 674 Coin Change (DP)

    Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make c ...

随机推荐

  1. 详解Map集合体系及方法entrySet、keySet、values

    简单回顾Map集合: Map表示映射关系,以键值对的方式来保存数据.key和value一一对应.key是唯一的,不可重复,而value是可重复的,可以被多个key关联.虽然Map是放入两个数据,但是却 ...

  2. PHP字符转码

    最近手里面有一个新的项目,下载的程序用的是 gbk, 可是我需要UTF8的格式,因为只有这个的格式才可以加入百度的MIP项目. 来此学习了解php编码的一些内容,还请多多指教.

  3. day 33 线程

    1.线程理论 什么是线程:线程是cpu的最小执行单位(实体),进程是操作系统的数据资源分配单位   2.线程的两种创建方式(重点) 查看线程的pid:使用os模块查看id,线程的id应该是相同的 3. ...

  4. spring boot2整合dubbox全注解

    前题 dubbox是dubbo的一个升级版,简单说就是本来dubbo是阿里开发的,现在阿里不维护了,当当网拿过去继续开发.本来阿里的dubbo维护到2.6版本,而再高版本的都是当当网维护的就叫成dub ...

  5. Kafka解惑之时间轮 (TimingWheel)

    Kafka中存在大量的延迟操作,比如延迟生产.延迟拉取以及延迟删除等.Kafka并没有使用JDK自带的Timer或者DelayQueue来实现延迟的功能,而是基于时间轮自定义了一个用于实现延迟功能的定 ...

  6. 《Act with Prudence》读后感

    <97 Things Every Should Know>中第一个编程方面的建议 文章链接:行事谨慎 很赞同文章中的观点,在做项目中是要谨慎行事和考虑后果.一直在项目前期考虑不够周到,以至 ...

  7. C++:bitset用法

    std::bitset是STL的一部分,准确地说,std::bitset是一个模板类,它的模板参数不是类型,而整形的数值(这一特性是ISO C++2003的新特性),有了它我们可以像使用数组一样使用位 ...

  8. 防360TAB页面的样式页面

    今天给朋友做了一个仿照360新tab页面的效果,主要就是一些样式和JQUERY的应用,超级简单,现在把源码放出来 源码下载

  9. 20155230 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告

    练习题: 凯撒密码: import java.util.Scanner; import java.io.*; public class exp1 { public static void main(S ...

  10. 20155339 《Java程序设计》实验五网络编程与安全实验报告

    20155339 <Java程序设计>实验五网络编程与安全实验报告 实验内容 实验一 1.两人一组结对编程: 参考http://www.cnblogs.com/rocedu/p/67667 ...