【hdu 3951】Coin Game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1851 Accepted Submission(s): 1065
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
【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=3951
【题解】
会发现如果k!=1
先手不管怎样选一段;
我们总能在这n个硬币所围成的对角线上选取同样的一段(或者比它多一个或少一个->n为奇数的情况);这样就分成了两段硬币;
这两段硬币的个数相同;
->如果对应两个游戏的话,对应的sg函数必然是相同的;
则sg[x]^sg[x]==0
所以先手输;
我们总能让第一个人面对这种情况;
所以第一个人总是输的;
但是k=1的时候就不一定可以;
只有当n为偶数的时候第一个人输;n为奇数的话第一个人无论如何都会赢的.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
int T;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(T);
rep1(ii,1,T)
{
int n,k;
rei(n);rei(k);
printf("Case %d: ",ii);
if (k>=n || (k==1 && n&1))
puts("first");
else
puts("second");
}
return 0;
}
【hdu 3951】Coin Game的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 3537】Daizhenyang's Coin
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- 26.SpringBoot事务注解详解
转自:https://www.cnblogs.com/kesimin/p/9546225.html @Transactional spring 事务注解 1.简单开启事务管理 @EnableTrans ...
- Lintcode 将整数A转换为B
例子 如把31转换为14,须要改变2个bit位. ()10=()2 ()10=()2 贴代码 class Solution { public: /** *@param a, b: Two intege ...
- iOS_01_C语言简介
1.先学C语言的原因 * oc基于C. * oc 跟 C的思想和语法很多地方不太一样,而且OC能和C混用. * C 是所有编程语言中的经典,很多高级语言都是从C语言中衍生出来的,比如 C++,C#.O ...
- Fragment Summary 2/2
出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API ...
- angular 创建服务
一:新建服务模块和服务文件 ng g module services --spec=false ng g service services/quote --spec=false 二:在quote.se ...
- java——数组
数组是多个同样数据类型数组组合,当中数据类型是不论什么数据类型. 数组变量是引用类型变量,数组能够作为对象,数组中的每个元素相当于对象的成员变量,所以数组元素能够默认初始化.(博客java--变量分类 ...
- Eclipse 出错 Error:Could not create the Java Virtual Machine Error:A fatal exception has occurred
提示如下: scala compile server. error:could not create the java machine.Error: A fatal exception has occ ...
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER;
错误提示内容及上下文环境: SQL> grant sysdba to weng;grant sysdba to weng*第 1 行出现错误:ORA-01034: ORACLE not avai ...
- UCloud上LAMP小型站点搭建与測试
文件夹 介绍 LAMP环境搭建 打开UCloud防火墙 WordPress安装 应用測试 介绍 本篇博客旨在通过介绍搭建一个WordPress博客的过程介绍在UCloud的云主机(UHOST)上搭建单 ...
- bootstrap课程2 bootstrap的栅格系统的主要作用是什么
bootstrap课程2 bootstrap的栅格系统的主要作用是什么 一.总结 一句话总结:响应式布局(就是适应不同的屏幕,手机,平板,电脑) 1.bootstrap的栅格系统如何使用? row ...