HDU 5953 Game of Taking Stones(威佐夫博弈+高精度+二分)——The 2016 ACM-ICPC Asia Dalian Regional Contest

xiaoxiao2021-02-28  10


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 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.   Output For 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 个, A B 两个人进行一场游戏,每个人可以从一堆石子中拿任意个,或者从两堆石子中拿相同的个数,谁没得拿谁输。就是一个威佐夫博弈,但是数据范围太大, a,b10100

解题思路: 二分精度,把精度精确到一百位左右就OK了,用 java 操作比较方便, 有 BigDecimal ,直接二分,然后判断以下就OK了。


import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args){ BigDecimal a, b, tmp, gold; BigDecimal l, r, eps, mid; l = BigDecimal.valueOf(2.236067); r = BigDecimal.valueOf(2.236068); mid = BigDecimal.valueOf(2.236067); eps = BigDecimal.valueOf(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001); while(r.subtract(l).compareTo(eps) > 0){ mid = l.add(r).divide(BigDecimal.valueOf(2)); if(BigDecimal.valueOf(5).subtract(mid.multiply(mid)).compareTo(eps)<0) r = mid; else l = mid; } gold = mid.add(BigDecimal.ONE).divide(BigDecimal.valueOf(2)); Scanner in = new Scanner(System.in); while(in.hasNextBigDecimal()){ a = in.nextBigDecimal(); b = in.nextBigDecimal(); if(a.compareTo(b)>0){ tmp = a; a = b; b = tmp; } BigDecimal c = b.subtract(a).multiply(gold); if(c.setScale(0,BigDecimal.ROUND_DOWN).equals(a)==true) System.out.println("0"); else System.out.println("1"); } } }
转载请注明原文地址: https://www.6miu.com/read-1150036.html