HDU-5973 Game of Taking Stones(威佐夫博弈+高精度)(C++高精度)

题目传送门
威佐夫博弈传送门
思路:由于题目数据范围非常大,需要做高精度处理,难点在于计算(n-m)*(sqrt(5)+1)/2的整数部分值,可以采用特殊办法预处理(sqrt(5)+1)/2的值,再做大数乘法,题目难度会小很多,matlab有非常强大的高精度计算功能,在命令行输入 :
vpa((1 + sqrt(sym(5)))/2,100)
即可计算(sqrt(5)+1)/2精确到小数点后100位。

完整代码如下:

#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef unsigned long ul;
string n,m;

//if a>=b return true;
bool compare(string a,string b)
{
    if(a[0] == '-')
        return false;
    if(a.size()>b.size())
        return true;
    else if(a.size()==b.size())
        return a>=b;
    return false;
}
string add(string,string);
string sub(string,string);
string mul(string,int);
string mul(string,string);

bool Wythoff(int n,int m)
{
    if(nint k=n-m;
    n=(int)(k*(1+sqrt(5))/2.0);
    if(n==m)
        return 0;
    else
        return 1;
}

int main()
{
    string s("61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748");
    while(cin>>n>>m)
    {
        if(n=="0"&&m=="0")
        {
            cout<<0<continue;
        }
        if(compare(m, n))
            swap(n, m);


        string k = sub(n, m);

        string ss(101-k.length(),'0');
        string a(k);
        a+=ss;
        a = mul(a, s);
        string cnt;
        cnt = a.substr(0,a.length()+k.length()-202);
        k = add(k , cnt);

        if(k==m)
            cout<<0<else
            cout<<1<return 0;
}

string add(string a,string b)
{
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    if(a.size()bool f = 0;
    for(int i=0;i48+f;

        if(a[i]>'9')
        {
            f = true;
            a[i] = a[i] -10;
        }
        else
            f = false;
    }

    for(ul i=b.size();iif(a[i]>'9')
        {
            f = true;
            a[i] = a[i] -10;
        }
        else
            f = false;
    }


    if(f)
        a = a+"1";

    reverse(a.begin(),a.end());
    return a;

}

string sub(string a,string b)
{
    bool flag = compare(a, b);
    if(!flag)
        swap(a,b);

    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());

    bool f = false;
    for(int i=0;i'0' - b[i] -f;
        if(a[i]<'0')
        {
            f = true;
            a[i] = a[i] + 10;
        }
        else
            f = false;
    }

    for(ul i=b.size();iif(a[i]<'0')
        {
            f = true;
            a[i] = a[i] + 10;
        }
        else
            f = false;
    }
    reverse(a.begin(),a.end());


    while(*a.begin()=='0')
        a.erase(a.begin());
    if(a.size()==0)
        a = "0";


    if(!flag)
        a = "-"+a;

    return a;

}


string mul(string a,int b)
{
    int flag = 0;
    for(int i=a.size()-1;i>=0;i--)
    {
        int str = (a[i]-48)*b + flag;
        a[i] = str%10 +48;
        flag = str/10;
    }

    if(flag)
        a.insert(a.begin(), flag+48);

    while(*a.begin()=='0')
        a.erase(a.begin());
    if(a.size()==0)
        a = "0";
    return a;
}


string mul(string a,string b)
{
    string c;
    if(a.size()1]-'0');
    string k = "0";
    for(int i=b.size()-2;i>=0;i--)
    {
        string f = mul(a,b[i]-'0');
        f = f+k;
        c = add(c, f);
        k = k + "0";
    }

    return c;
}

你可能感兴趣的:(HDU)