HDU 5973

Game of Taking Stones

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


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
 

Source


思路:一个大数博弈,除去大数外算是威佐夫博弈的模板题目,利用Java用BigDecimal类来保证高精度,用二分法来求解sqrt(5),先说下关于二分法求解开根号的思想,先把给定的数均分,然后相乘判断与要开方的数的关系,若前者大于后者,则左移,否则右移。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.math.BigDecimal;  
  2. import java.util.Scanner;  
  3.   
  4. public class Main {  
  5.   
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         Scanner cin = new Scanner(System.in);  
  9.         BigDecimal two,three,five,a,b;  
  10.         two = BigDecimal.valueOf(2);  
  11.         three = BigDecimal.valueOf(3);  
  12.         five = BigDecimal.valueOf(5);  
  13.         //二分法求sqrt(5)  
  14.         BigDecimal l = two, r = three;  
  15.         for(int i = 0; i < 500; i ++){  
  16.             BigDecimal mid = l.add(r).divide(two);  
  17.             if(mid.multiply(mid).compareTo(five) < 0)  
  18.                 l = mid; //右移  
  19.             else  
  20.                 r = mid; //左移  
  21.         }  
  22.         BigDecimal gold = l.add(BigDecimal.ONE).divide(two);//威佐夫博弈黄金分割率  
  23.         while(cin.hasNext()){  
  24.             a = cin.nextBigDecimal();  
  25.             b = cin.nextBigDecimal();  
  26.             if(a.compareTo(b) < 0){//a>b  
  27.                 BigDecimal temp = a;  
  28.                 a = b;  
  29.                 b = temp;  
  30.             }  
  31.             BigDecimal temp = a.subtract(b);  
  32.             temp = temp.multiply(gold);  
  33.             temp = temp.setScale(0, BigDecimal.ROUND_FLOOR);//向下取整  
  34.             if(temp.compareTo(b) == 0)  
  35.                 System.out.println("0");  
  36.             else  
  37.                 System.out.println("1");  
  38.         }  
  39.     }  
  40.   
  41. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值