zoukankan      html  css  js  c++  java
  • POJ 3922 A simple stone game

    题目:

    E - A simple stone game
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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.

    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).

    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 Input

    5 
    16 1 
    11 1 
    32 2 
    34 2 
    19 3

    Sample Output

    Case 1: lose
    Case 2: 1
    Case 3: 3
    Case 4: lose
    Case 5: 4

    Hint

    When k = 1, the first player will definitely lose if the initial amount of stones is in the set {2, 4, 8, 16, 32, ...}. Let's call this kind of set “first-player-lose set”. 

    When k = 2, the first-player-lose set is {2, 3, 5, 8, 13, 21, 34, 57 ...} , which happens to be the Fibonacci sequence starting from 2.
     
     
     

    两人取一堆n个石子 先手不能全部取完 之后每人取的个数不能超过另一个人上轮取的数*K

    给n,K判断先手必胜并求第一步

    博弈题

    这题的思考过程非常有意义。

    当k=1的时候 可知必败局面都是2^i  将n分解成二进制,然后先手取掉最后一个1.然后对方必然无法去掉更高的1,而对方取完我方至少还能拿掉最后一个1 导致对方永远取不完。

    当k=2的时候,必败局面都是斐波那契数列。利用“先手去掉最后一个1,则后手必不能去掉更高阶的1导致取不完”的思想,斐波那契数列有一个非常好的性质就是:任意一个整数可以写成斐波那契数列中的不相邻的项的和,于是将n写成这种形式,先取走最后一个1,对方能取的数是这个数*2,小于高2位的1,所以取不完。

    当K的时候, 想办法构造数列,将n写成数列中一些项的和,使得这些被取到的项的相邻两个倍数差距>k 那么每次去掉最后一个1 还是符合上面的条件。设这个数列已经被构造了i 项,第 i 项为a[ i ],前 i 项可以完美对1..b[ i ] 编码使得每个编码的任意两项倍数>K 那么有

    a[ i+1 ] = b[ i ] + 1;这是显然的 因为b[ i ] + 1没法构造出来,只能新建一项表示

    然后计算b[ i+1] 既然要使用 a[ i+1 ] 那么下一项最多只能是某个 a[ t ] 使得 a[ t ] * K < a[ i+1 ] 于是

    b[ i ] = b[ t ] + a[ i+1 ]

    然后判断n是否在这个数列里面

    如果在,那么先手必败。否则不停的减掉数列a中的项构造出n的分解,最后一位就是了。

    //看了好久啊~~~~~~= =

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 using namespace std;
     5 const int maxn=2000000;
     6 int a[maxn],b[maxn];
     7 int main()
     8 {
     9          int n,k;
    10          int cas=0,cass;
    11          for (scanf("%d",&cass);cass--;)
    12          {
    13                    scanf("%d%d",&n,&k);
    14                    ++cas;
    15                    printf("Case %d: ",cas);
    16                    int i=0,j=0;
    17                    a[0]=b[0]=1;
    18                    while (a[i]<n)
    19                    {
    20                             i++;
    21                             a[i]=b[i-1]+1;
    22                             while (a[j+1]*k<a[i])
    23                                      j++;
    24                             if (a[j]*k<a[i])
    25                                      b[i]=a[i]+b[j];
    26                             else
    27                                      b[i]=a[i];
    28                                      cout<<a[i]<<" ";
    29                    }
    30                    if (a[i]==n)
    31                             puts("lose");
    32                    else
    33                    {
    34                             int last=-1;
    35                             while (n)
    36                             {
    37                                      if (n>=a[i])
    38                                      {
    39                                                last=a[i];
    40                                                n-=a[i];
    41                                      }
    42                                      i--;
    43                             }
    44                             printf("%d
    ",last);
    45                    }
    46          }
    47          return 0;
    48 }
    49 
    50 //大神的代码~
  • 相关阅读:
    rsync+inotify实现全网自动化数据备份-技术流ken
    高可用集群之keepalived+lvs实战-技术流ken
    高负载集群实战之lvs负载均衡-技术流ken
    实战!基于lamp安装Discuz论坛-技术流ken
    iptables实战案例详解-技术流ken
    (3)编译安装lamp三部曲之php-技术流ken
    (2)编译安装lamp三部曲之mysql-技术流ken
    (1)编译安装lamp三部曲之apache-技术流ken
    实战!基于lamp安装wordpress详解-技术流ken
    yum一键安装企业级lamp服务环境-技术流ken
  • 原文地址:https://www.cnblogs.com/teilawll/p/3221311.html
Copyright © 2011-2022 走看看