Game of Taking Stones HDU - 5973 (二分)开根号

版权声明:如有错误,请指出,不胜感激。 https://blog.csdn.net/qq_36424540/article/details/82843073

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
import java.math.*;
import java.util.*;
/*
java中的三种取整函数
 舍掉小数取整:Math.floor(3.5)=3
 四舍五入取整:Math.rint(3.5)=4
 进位取整 :Math.ceil(3.1)=4

BigDecimal。setScale(精度:小数点后多少位,舍入方式)
各个roundingMode详解如下:
ROUND_UP:非0时,舍弃小数后(整数部分)加1,比如12.49结果为13,-12.49结果为 -13
ROUND_DOWN:直接舍弃小数
ROUND_CEILING:如果 BigDecimal 是正的,则做 ROUND_UP 操作;如果为负,则做 ROUND_DOWN 操作 (一句话:取附近较大的整数)
ROUND_FLOOR: 如果 BigDecimal 是正的,则做 ROUND_DOWN 操作;如果为负,则做 ROUND_UP 操作(一句话:取附近较小的整数)
ROUND_HALF_UP:四舍五入(取更近的整数)
ROUND_HALF_DOWN:跟ROUND_HALF_UP 差别仅在于0.5时会向下取整
ROUND_HALF_EVEN:取最近的偶数
ROUND_UNNECESSARY:不需要取整,如果存在小数位,就抛ArithmeticException 异常
*/
public class Main {
	public static void main(String[] args) {
		BigDecimal one=BigDecimal.valueOf(1),two=BigDecimal.valueOf(2),five=BigDecimal.valueOf(5);
		BigDecimal t=one.add(sqrt(five,500)).divide(two);
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()) {
			BigDecimal a,b,tmp=null;
			a=sc.nextBigDecimal();b=sc.nextBigDecimal();
			
			if(a.compareTo(b)>0) {
				tmp=a;a=b;b=tmp;
			}
			//System.out.println(a.setScale(2, BigDecimal.ROUND_DOWN));
			if(b.subtract(a).multiply(t).setScale(0,BigDecimal.ROUND_DOWN).equals(a)) {
				System.out.println(0);
			}
			else
				System.out.println(1);
			
		}
		sc.close();
	}
/*
	//对常数开方保留多位小数,返回高精度小数,特别慢,但是精度极高
    private static BigDecimal sqrt(BigDecimal x, int n) {
        BigDecimal ans = BigDecimal.ZERO;
        BigDecimal eps = BigDecimal.ONE;
        for (int i = 0; i < n; ++i) {
            while (ans.pow(2).compareTo(x) <=0) {
                ans = ans.add(eps);
            }
            ans = ans.subtract(eps);//最后一定是大了,我们减掉最后的eps
            eps = eps.divide(BigDecimal.TEN);
        }
        return ans;
    }
 */
	//需要合理控制迭代次数
	private static BigDecimal sqrt(BigDecimal x,int n) {
		BigDecimal l=BigDecimal.ZERO,r=x,mid;
		BigDecimal two=BigDecimal.valueOf(2);
		for(int i=0;i<=n;i++) {
			mid=l.add(r).divide(two);
			if(mid.pow(2).compareTo(x)<=0)l=mid;
			else r=mid;
		}
		return l;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/82843073
今日推荐