• HDU5973 Game of Geting Stone(威佐夫博弈)


    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?

    InputInput 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.OutputFor 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大数;
    对于一个奇异状态满足,a[k]=k*(sqrt(5)+1)/2;b[k]=a[k]+k;
    参考代码:
     1 import java.math.BigDecimal;
     2 import java.util.Scanner;
     3 
     4 public class Main{
     5     public static void main(String[] args){
     6         BigDecimal two=new BigDecimal(2);
     7         BigDecimal three=new BigDecimal(3);
     8         BigDecimal five=new BigDecimal(5);
     9         BigDecimal l=two, r=three;
    10         for(int i=0; i<500; i++){
    11             BigDecimal mid=l.add(r).divide(two);
    12             if(mid.multiply(mid).compareTo(five)<0) l=mid;
    13             else r=mid;
    14         }
    15         BigDecimal gold=l.add(BigDecimal.ONE).divide(two);
    16         BigDecimal a, b;
    17         Scanner cin=new Scanner(System.in);
    18         while(cin.hasNext()){
    19             a=cin.nextBigDecimal();
    20             b=cin.nextBigDecimal();
    21             if(a.compareTo(b)>0){
    22                 BigDecimal tmp=a;
    23                 a=b;b=tmp;
    24             }
    25             a=a.setScale(0, BigDecimal.ROUND_DOWN);
    26             b=b.subtract(a).multiply(gold);
    27             b=b.setScale(0, BigDecimal.ROUND_DOWN);
    28             if(a.compareTo(b)==0) System.out.println("0");
    29             else System.out.println("1");
    30         }
    31     }
    32 }
    View Code
  • 相关阅读:
    信号signal的监听与处理
    oracle 12cR1&12cR2核心高实用性新特性
    Tomcat 7服务器线程模型
    抓取awr、语句级awr、ashrpt
    从percona server 5.7换到mariadb 10.2
    关于typeid和typeof
    mysql查询INFORMATION_SCHEMA表很慢的性能优化
    使用ccache大幅度加速gcc编译速度至少1倍以上(不需要修改任何编译选项)
    c++ linux下输出中文
    visual studio 2015下使用gcc调试linux c++开发环境搭建完整详解
  • 原文地址:https://www.cnblogs.com/csushl/p/10331498.html
Copyright © 2020-2023  润新知