POJ3922 、HDU2486、HDU2580坑爹的博弈,一般人想五个小时也想不出啊!!
来源:互联网 发布:网络万能解锁 编辑:程序博客网 时间:2024/06/10 04:01
转载请注明出处:http://blog.csdn.net/cxb569262726/article/details/7841521
题目链接:
http://poj.org/problem?id=3922
http://acm.hdu.edu.cn/showproblem.php?pid=2486
http://acm.hdu.edu.cn/showproblem.php?pid=2580
A simple stone game
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 174 Accepted Submission(s): 79
Problem Description
After he has learned how to play Nim game, Mike begins to try another stone game which seems much easier.
The game goes like this: Two players start the game with a pile of n stones. They take stones from the pile in turn and every time they take at least one stone. The one who goes first can take at most n-1 stones for his first move. From then on a player can take at most k times as many stones as his opponent has taken last time. For example, if one player take m stones in his turn, then the other player can take at most k × m stones next time. The player who takes the last stone 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.
The game goes like this: Two players start the game with a pile of n stones. They take stones from the pile in turn and every time they take at least one stone. The one who goes first can take at most n-1 stones for his first move. From then on a player can take at most k times as many stones as his opponent has taken last time. For example, if one player take m stones in his turn, then the other player can take at most k × m stones next time. The player who takes the last stone 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 contains a integer t, indicating that there are t test cases following.(t<=20).
Each test case is a line consisting of two integer n and k.(2<=n<=10^8,1<=k<=10^5).
Each test case is a line consisting of two integer n and k.(2<=n<=10^8,1<=k<=10^5).
Output
For each test case, output one line starting with “Case N: ”, N is the case number. And then, if the first player can ensure a winning, print the minimum number of stones he should take in his first turn. Otherwise, print "lose". Please note that there is a blank following the colon.
Sample Inpu
516 111 132 234 219 3
Sample Output
Case 1: loseCase 2: 1Case 3: 3Case 4: loseCase 5: 4
题目大意:给定整数n和k。有n个石头,两个人来取。先手可以取1~n-1个。接下来每次最多能取上一个人取的k倍,即1~k*n; 求先手是否一定能赢!能赢输出第一次最少取几个。否则输出lose。
PS:我想这种变态题目的原因: 今天早上比赛时没人做出来,到了晚上貌似还是没人会,不幸抽签中奖,由我讲解这题。。。。O M G!! 参照网上代码,找了好久的规律!最后时刻想出来了。可惜还是准备不充分,没能表达清楚。。。
思想:找规律 然后 博弈,还有点dp的思想。。。
以下是代码以及解答思路:附上涂鸦一副
#include<iostream>#include<algorithm>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>#include<math.h>#include<set>#include<vector>#define MAXN 15#define INF 1000using namespace std;int no[2000000],maxok[2000000];//no储存的是lose的数。 maxok【i】存的是在i之前能组成的最大的数; int main(){int i,j,n,m,k,t,c,ca=0;for(scanf("%d",&t);t--;){ca++;scanf("%d%d",&n,&k);if(n<=k+1)//n<=k+1时一定是输; {printf("Case %d: lose\n",ca);continue;}int x=0,y=0;no[0]=maxok[0]=1;while(no[x]<n){x++;no[x]=maxok[x-1]+1;//在x之前,最大的能符合的数是maxok【x-1】、加1后就不符合。 while(no[y+1]*k<no[x])//找小于no[x]的最大数:例子k=3时有15得到4 y++;if(no[y]*k<no[x])//前面得到的数可能不符合条件 maxok[x]=no[x]+maxok[y];//如果符合直接得no[x]+maxok[y];例子中推得15+maxok[3] elsemaxok[x]=no[x];//否则能组成最大的数为本身。 }if(no[x]==n)//刚好组成n 说明这个数属于no数组。即不能由符合条件的数组成,lose! {printf("Case %d: lose\n",ca);continue;}int ans;while(n){if(n>=no[x])//不断的减去最大的数,剩下的最小的数就是了。 {ans=no[x];n-=no[x];}x--;}printf("Case %d: %d\n",ca,ans);} return 0;}/*k=3时: no={1,2,3,4,6,8,11,15,21,29maxok=1,2,3,5,7,10,14,20*/
- POJ3922 、HDU2486、HDU2580坑爹的博弈,一般人想五个小时也想不出啊!!
- hdu2486 hdu2580 poj3922 A simple stone game--K倍动态减法游戏 博弈
- HDU 2486 HDU2580 POJ3922 a simple stone game
- [K倍动态减法问题] HDU 2486 & HDU2580 & POJ3922 a simple stone game
- A simple stone game (poj3922,博弈)
- 一般人看不懂,看懂的绝对不是一般人
- 兄弟连,一般人来不起,来的肯定不是一般人!
- 兄弟连,一般人来不起,来的肯定不是一般人!
- 兄弟连,一般人来不起,来的肯定不是一般人!
- 一般人都会做错的问题
- 一般人不清楚的JavaScript概念
- poj 1001 Exponentiation 五个小时。。。回来重写
- Linux真的不一般人用的。
- 一般人都不会的电脑绝技
- 一般人不知道的平凡事情
- 一般人不会的电脑小绝技
- 敏捷不是一般人玩的游戏
- 牛人和一般人的思维(图)
- 排序算法总结
- SQL Server 存储过程详解
- android Parcelable
- 用C#操纵IIS
- VS2008 Release模式下的调试设置
- POJ3922 、HDU2486、HDU2580坑爹的博弈,一般人想五个小时也想不出啊!!
- git tutorial
- uva 10125 Sumsets
- 面向对象编程和结构化编程
- 够您学习一辈子的生活经典
- 开开心心学算法--快速排序之会场安排问题
- uva 188 - Perfect Hash
- 再谈多线程信号量同步(协调)方法的本质
- 2.8 标准数组——向量