HDU 5973 威佐夫博弈+JAVA高精度

来源:互联网 发布:scratch趣味编程 ppt 编辑:程序博客网 时间:2024/06/10 04:02

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 18 44 7
Sample Output
010


import java.util.*;import java.math.*;public class Main {public static void main(String[] args) {Scanner scan=new Scanner(System.in);BigDecimal TWO=new BigDecimal("2");BigDecimal FIVE=new BigDecimal("5");BigDecimal EPS=new BigDecimal("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001");BigDecimal l=new BigDecimal("0");BigDecimal r=new BigDecimal("5");BigDecimal m=new BigDecimal("2.5");BigDecimal ONE= new BigDecimal("1");while(l.subtract(r).compareTo(EPS)<0){              m = l.add(r).divide(TWO);              if(m.multiply(m).subtract(FIVE).abs().compareTo(EPS.abs())<0) break;              if(m.multiply(m).subtract(FIVE).compareTo(EPS)<0) l = m;              else r = m;          }  //System.out.println(r);r=r.add(ONE).divide(TWO);while(scan.hasNext()){String str1=scan.next();String str2=scan.next();BigDecimal x=new BigDecimal(str1);BigDecimal y=new BigDecimal(str2);if(x.compareTo(y)>0){TWO=x;x=y;y=TWO;}BigDecimal z=y.subtract(x);BigDecimal w=r.multiply(z);w=w.setScale(0, BigDecimal.ROUND_DOWN);if(w.equals(x))System.out.println(0);elseSystem.out.println(1);}}}