Loading

「The 2016 ACM-ICPC Asia Dalian Regional Contest」C.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?

输入

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

输出

For each test data,output answer on one line.1 means you are the winner,otherwise output 0.

样例

输入

2 1
8 4
4 7

输出

0
1
0

思路

太依赖榜单了,过的人数比较少的题目甚至直接没去看,很严重的一个问题

这道题其实就是一道裸的威佐夫博弈,难点就在于处理大数,由于a,b的值都特别大,所以在用公式计算的时候5的精度必须要保证,否则与如此大的数相乘以后产生的误差是非常可怕的。

代码

import java.math.*;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        BigDecimal one = new BigDecimal(1);
        BigDecimal two = new BigDecimal(2);
        BigDecimal three = new BigDecimal(3);
        BigDecimal five = new BigDecimal(5);
        BigDecimal l = new BigDecimal(2);
        BigDecimal r = new BigDecimal(3);
        BigDecimal a = new BigDecimal(1);
        BigDecimal b = new BigDecimal(1);
        // 求出足够精度
        for(int i = 1;i <= 666;i++){
            BigDecimal mid = l.add(r).divide(two);
            if(mid.multiply(mid).compareTo(five) < 0){
                l = mid;
            }
            else{
                r = mid;
            }
        }
        l = l.add(one).divide(two);
        // System.out.println(l);
        while(in.hasNext()){
            a = in.nextBigDecimal();
            b = in.nextBigDecimal();
            if(a.compareTo(b) > 0){
                BigDecimal tmp = a;
                a = b;
                b = tmp;
            }
            // 向下取整
            a = a.setScale(0, BigDecimal.ROUND_DOWN);
            b = b.subtract(a).multiply(l);
            b = b.setScale(0, BigDecimal.ROUND_DOWN);
            if(b.compareTo(a) == 0){
                System.out.println("0");
            }
            else{
                System.out.println("1");
            }
        }
        in.close();
    }
}

 

posted @ 2021-01-20 21:39  Yiduuannng  阅读(67)  评论(0编辑  收藏  举报