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); } } }