HDU - 5973 Game of Taking Stones (威佐夫博弈 高精度)

题目描述:

Two people face two piles of stones and make a game. They take turns to take stones. As game rules, there are two different methods of taking stones: One scheme is that you can take any number of stones in any one pile while the alternative is to take the same amount of stones at the same time in two piles. In the end, the first person taking all the stones is winner.Now,giving the initial number of two stones, can you win this game if you are the first to take stones and both sides have taken the best strategy?

InputInput contains multiple sets of test data.Each test data occupies one line,containing two non-negative integers a andb,representing the number of two stones.a and b are not more than 10^100.OutputFor each test data,output answer on one line.1 means you are the winner,otherwise output 0.Sample Input

2 1
8 4
4 7

Sample Output

0
1
0
题目大意:两个人抓石子,有两堆石子,石子数量分别是a,b,每次可以从一堆中抓若干石子,也可以从两堆同时抓取相同数量的石子,谁先抓完谁获胜,现在你先手,问是否必胜,必胜输出1否则输出0.

题解:裸的威佐夫博弈和队友谈论了半天没看出来,神奇的是队友竟然推出了威佐夫博弈奇异局势的数列(0,0) (1,2) (3,5) (4,7) (6,10(8,13(9,15(11,18)(12,20)……可以说她是一只可爱的小仙女了。
但算不出通项公式也算是没辙,话说威佐夫博弈中根号5怎么来的竟然和黄金分割数有关。

【威佐夫博弈】
 威佐夫博弈:有两堆石子,每次一个人可以两堆同时取相同数量的石子,也可以只取其中一堆的石子,最后谁取完谁获胜,请问先手还是后手胜?

奇异局势:
让先手必输的局势,那么由这些局势在规定范围内拓展的局势也是先手必输的局势(但在这里双方自由选取,不适用)。我们可以得出一些局势使A必输:(0,0) (1,2) (3,5) (4,7) (6,10(8,13(9,15(11,18)(12,20)……我们称这些局势为奇异局势

   对于奇异局势来说,有以下性质:

  1. 任何自然数都一定包含在一个奇异局势中。
  2. 任意操作都可以将奇异局势转变为非奇异局势。
  3. 可以将非奇异局势转变为奇异局势。

     那么,当我们面对下列情况时,可以这样应对:

     当a=b时,两堆同时取a

     当a=ak,b>bk时,2堆取b-bk个

     当a=ak,b<bk时,2堆取a-a(b-a)个

     当a>ak,b=bk(ak+k)时,1堆取a-ak个

     当a<ak,b=bk(ak+k)时,从2堆中拿走若干变成奇异局势

    如何判断一个数对是不是奇异局势呢?

    当a=(下取整)k*(1+√5)/2,b=ak+k时(k为任意非负整数)局势为奇异局势

【威佐夫博弈高精度模板】JAVA版本

import java.math.BigInteger;
import java.util.Scanner;

import java.math.BigDecimal ;
public class Main {
    
    //对常数开方保留多位小数,返回高精度小数
    private static BigDecimal sqrt(BigDecimal x, int n) {
        BigDecimal ans = BigDecimal.ZERO;
        BigDecimal eps = BigDecimal.ONE;
        for (int i = 0; i < n; ++i) {
            while (ans.pow(2).compareTo(x) < 0) {
                ans = ans.add(eps);
            }
            ans = ans.subtract(eps);
            eps = eps.divide(BigDecimal.TEN);
        }
        return ans;
    }
    
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            BigDecimal a = cin.nextBigDecimal();
            BigDecimal b = cin.nextBigDecimal();
            BigDecimal c = sqrt(new BigDecimal(5), 120);
            c = c.add(BigDecimal.ONE).divide(new BigDecimal(2));
            BigDecimal t = null;
            
            if(a.compareTo(b) == 1) {
                t = a;
                a = b;
                b = t;
            }
            //计算(bk - ak ) * (1+sqrt(5))/2 == ak是否成立 左边向下取整
            if( b.subtract(a).multiply(c).setScale(0, BigDecimal.ROUND_DOWN).equals(a)) {
                System.out.println(0);
            }
            else
                System.out.println(1);
        }
        
        cin.close();
    }
    
}
 

猜你喜欢

转载自www.cnblogs.com/czsharecode/p/9595399.html