2016 ACM/ICPC大连区域赛 C—Game of Taking Stones【ava大数+威佐夫博弈】

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1003&cid=736

Game of Taking Stones

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 542    Accepted Submission(s): 80

 

Problem Description

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

题意:

有两堆各若干个物品,两个人轮流从任一堆取至少一个或同时从两堆中取同样多的物品,规定每次至少取一个,多者不限,最后取光者得胜。

分析:

java大数+威佐夫博弈 公式 fabs(a,b)*(1+√5)/2 向下取整 结果跟小的一样就是先手输,否则先手赢

注意根号下5的精度

代码:

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Mainn {
	public static void main(String[] args)
	{
		Scanner cin = new Scanner(System.in);
		BigDecimal two,three,five,a,b;
		two = BigDecimal.valueOf(2);
		three = BigDecimal.valueOf(3);
		five = BigDecimal.valueOf(5);
		BigDecimal l = two,r = three;
		for(int i=0;i<500;i++)
		{
			BigDecimal mid = l.add(r).divide(two);
			if(mid.multiply(mid).compareTo(five)<0)
				l = mid;
			else r = mid;
		}
		BigDecimal g = l.add(BigDecimal.ONE).divide(two);
		while(cin.hasNext())
		{
			a = cin.nextBigDecimal();
			b = cin.nextBigDecimal();
			if(a.compareTo(b)<0) 
			{
				BigDecimal tmp = a;
				a=b;
				b=tmp;
			}
			BigDecimal tmp = a.subtract(b);
			tmp = tmp.multiply(g);
			tmp = tmp.setScale(0,BigDecimal.ROUND_FLOOR);
			if(tmp.compareTo(b)==0)
				System.out.println("0");
			else System.out.println("1");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/lml11111/article/details/82933551