题意:给定两堆石子,每个人可以从任意一堆拿任意个,也可以从两堆中拿相同的数量,问谁赢。
析:直接运用威佐夫博弈,floor(abs(a, b) * (sqrt(5)+1)/2) == min(a, b) 是必败态。用java的BigDecimal,是很好用的,要十分求Sqrt(5).
代码如下:
import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Main{ public static final int maxn = 3000 + 5; public static void main(String []args){ Scanner cin = new Scanner(System.in); BigDecimal l = new BigDecimal("2"); BigDecimal r = new BigDecimal("3"); BigDecimal f = new BigDecimal("5"); BigDecimal x = l; for(int i = 0; i < 500; ++i){ BigDecimal mid = l.add(r).divide(x); if(mid.multiply(mid).compareTo(f) < 0) l = mid; else r = mid; } l = l.add(BigDecimal.ONE); while(cin.hasNext()){ BigDecimal a = cin.nextBigDecimal(); BigDecimal b = cin.nextBigDecimal(); if(a.compareTo(b) > 0){ BigDecimal tmp = a; a = b; b = tmp; } BigDecimal t = b.subtract(a).multiply(l).divide(BigDecimal.valueOf(2)); BigInteger p = t.toBigInteger(); BigInteger q = a.toBigInteger(); if(p.equals(q)) System.out.println("0"); else System.out.println("1"); } } }