hdu5973Game of Taking Stones+威佐夫博弈+高精度

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

直接SG打个表。。发现是威佐夫博弈。。然后直接Java高精度乱搞。。

打表代码
#include
using namespace std;
int SG[100][100];
int getSG(int a,int b){
    if(a>b) swap(a,b);
    if(a==0&&b==0) return 0;
    if(a==0) return 1;
    if(SG[a][b]!=-1) return SG[a][b];
    bool visit[1000];
    memset(visit,false,sizeof(visit));
    for(int i=1;i<=a;i++){
        for(int j=1;j<=b;j++){
            visit[getSG(a-i,b)]=true;
            visit[getSG(a,b-j)]=true;
            visit[getSG(a-i,b-i)]=true;
        }
    }
    for(int i=0;;i++){
        if(visit[i]==false) return i;
    }
}

int main(){
    memset(SG,-1,sizeof(SG));
    SG[0][0]=0;
    SG[0][1]=1;
    for(int i=0;i<=50;i++){
        for(int j=i;j<100;j++){
            SG[i][j]=getSG(i,j);
            if(SG[i][j]==0) cout<" "<//威佐夫博弈
//    if(n
//    int k=n-m;
//    n=(int)(k*(1+sqrt(5))/2.0);
//    if(n==m) printf("0\n");
//    else printf("1\n");
}

高精度里没有开方。。so。。手写了一个。。开p次也可以,在哪个枚举哪里吧2次方,改成p次方。。

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        BigDecimal sqrt5 = sqrt(new BigDecimal("5"), 120);
        BigDecimal _2 = new BigDecimal("2");
        Scanner in = new Scanner(System.in);
        BigDecimal n, m;
        while (in.hasNextBigDecimal()) {
            n = in.nextBigDecimal();
            m = in.nextBigDecimal();
            if (n.compareTo(m) < 0) {
                BigDecimal t = n;
                n = m;
                m = t;
            }
            BigDecimal k = n.subtract(m);

            n = k.multiply(BigDecimal.ONE.add(sqrt5)).divide(_2);
            BigInteger n2 = n.toBigInteger();
            BigInteger m2 = m.toBigInteger();
            System.out.println(n2.compareTo(m2) == 0 ? "0" : "1");

        }
    }
    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.divide(BigDecimal.TEN);
        }
        return ans;
    }
}

你可能感兴趣的:(博弈)