HDU 5973 Aninteresting game 威佐夫博奕(Wythoff Game)

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 5973 Aninteresting game 威佐夫博奕(Wythoff Game)相关的知识,希望对你有一定的参考价值。

HDU 5973:http://acm.hdu.edu.cn/showproblem.php?pid=5975

题意:

  有两堆石子,每次可以从一堆石子中取任意个,或者从两堆石子中取相同个数的石子。两个人轮流用这种策略取石子,谁取完所有的石子就算胜利。问先手胜还是后手胜。

思路:

  一模一样的威佐夫博奕(Wythoff Game),结论的是,假设a>b,那么如果((1+sqrt(5))* (a - b))/2 == b ,那么先手必输。但是这道题的数据比较大,所以需要java做高精度。

技术分享图片
import javax.swing.*;
import java.util.Scanner;
import java.math.BigDecimal;


public class Main{
    public static void main(String[] args) {
        BigDecimal One = new BigDecimal(1);
        BigDecimal Two = new BigDecimal(2);
        BigDecimal Five = new BigDecimal(5);
//        BigDecimal two = new BigDecimal(2);
        
//        System.out.println(One.compareTo(Two));//-1
        BigDecimal le = new BigDecimal(2);
        BigDecimal ri = new BigDecimal(3);
        BigDecimal mid = le.add(ri).divide(Two);
        
        for(int i= 1; i<=400; i++) {
            mid = le.add(ri).divide(Two);
            if(mid.multiply(mid).compareTo(Five) == -1 ) {
                le = mid;
            }
            else ri = mid;
        }

        BigDecimal k = (One.add(mid)).divide(Two);
        //System.out.println(k);
        Scanner in = new Scanner(System.in);
        
        while(in.hasNext()) {
            BigDecimal a = in.nextBigDecimal();
            BigDecimal b = in.nextBigDecimal();
            BigDecimal det = new BigDecimal(0);
            int flag = 1;
            if(a.compareTo(b) == -1)
            {
                det = b.subtract(a);
                det = det.multiply(k);
                //long l  = bd.setScale( 0, BigDecimal.ROUND_DOWN ).longValue(); // 向下取整
                det = det.setScale( 0, BigDecimal.ROUND_DOWN );
                if(det.compareTo(a) == 0) {
                    flag = 0;
                }
            }
            else {
                det = a.subtract(b);
                det = det.multiply(k);
                //long l  = bd.setScale( 0, BigDecimal.ROUND_DOWN ).longValue(); // 向下取整
                det = det.setScale( 0, BigDecimal.ROUND_DOWN );
                if(det.compareTo(b) == 0) {
                    flag = 0;
                }
            }
            System.out.println(flag);
                
            
        }
        
            
    }
}
HDU 5973

 

以上是关于HDU 5973 Aninteresting game 威佐夫博奕(Wythoff Game)的主要内容,如果未能解决你的问题,请参考以下文章

hdu 5975---Aninteresting game(树状数组)

Aninteresting game HDU - 5975 (数学+lowbit)

hdu 5973 Game of Taking Stones

HDU5973 Game of Geting Stone(威佐夫博弈)

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

spoj5973