题意:输入a, b表示两堆石头数目,威佐夫博弈,问:先手胜负?
a, b <= 1e100.
高精度。当a > b时, a = (a-b)*黄金分割比 时是先手败状态。因为a, b <= 1e100,所以黄金分割比至少要保留小数点后100位。
BigDecimal没有开方操作,所以二分算出黄金分割比。
import java.util.*;
import java.io.BufferedInputStream;
import java.math.*;
public class Main {
public static void main(String[] args){
Scanner cin = new Scanner(new BufferedInputStream(System.in));
BigDecimal L = new BigDecimal(1.6180339887), R = new BigDecimal(1.6180339888);
BigDecimal eps = new BigDecimal(0.1).pow(101);
while( R.subtract(L).subtract(eps).compareTo(BigDecimal.ZERO) > 0){
BigDecimal M = L.add(R).divide(BigDecimal.valueOf(2));
BigDecimal ret = M.multiply(BigDecimal.valueOf(2)).subtract(BigDecimal.ONE).pow(2);
if(ret.compareTo(BigDecimal.valueOf(5)) > 0)
R = M;
else
L = M;
}
BigDecimal t = L;//黄金分割数精确到1e-100
while(cin.hasNext()){
BigInteger a = cin.nextBigInteger(), b = cin.nextBigInteger();
int com = a.compareTo(b), ccom = 0;
if(com == 0){
ccom = a.compareTo(BigInteger.ZERO);
if(ccom == 0)
System.out.println(0);
else
System.out.println(1);
continue ;
}
if(com == -1){
BigInteger c = a;
a = b;
b = c;
}
BigInteger k = a.subtract(b);
BigDecimal kk = new BigDecimal(k).multiply(t).setScale(0, BigDecimal.ROUND_DOWN);
k = kk.toBigInteger();
ccom = k.compareTo(b);
if(ccom == 0)
System.out.println(0);
else
System.out.println(1);
}
}
}