Slime and Stones(威佐夫博弈变形)

Slime and Stones

题目来源:2020年HDU Multi-University Training Contest 9 1003题
题目描述 :
Orac and Slime are playing a game.
There are two groups of stones, the first group contains a stones and the second contains b stones. Orac and Slime operate them by turns in the game. For each operation, they have two choices:

  1. Pick up any number of stones from a certain group;
  2. Pick up x stones from the first group and y from the second group, which x,y satisfy |x−y|≤k. k is a given constant.
    Notice that not picking up any stone in an operation is not allowed. If there is no stone left in both groups at the beginning of one’s turn, he loses the game.
    Orac would like to know whether there exists a winning strategy for him if he operates first. They will play many times, so he will make multiple queries.

输入 :
The first line contains one integer T(1≤T≤1e5), which stands for the number of queries that Orac makes.
In the following T lines, each line contains three integer a,b,k(1≤a≤1e8,1≤b≤1e8,0≤k≤1e8).
输出 :
The output contains T lines, with an integer 0 or 1 in each line, stand for there exist/not exist a winning strategy for the given situation.
样例输入:
4
1 2 0
2 4 0
1 2 1
2 6 1
样例输出:
0
1
1
0
提示 :
In the first query, if Orac picks up all the stones from a group, Slime will pick up all the stones from the other group, and if Orac picks up a stone from the second group, Slime will pick up a stone from both groups.
题目大意:给定a,b两堆石头,Orac 和 Slime 轮流取,取的规则有以下两种:
1.拿其中一堆石头的任意个;
2.两堆都拿,但两堆个数差的绝对值要小于等于k。

刚拿到这个题的时候,我们发现当k=0时,这个题会退化成威佐夫博弈。
但是我们都只记得威佐夫博弈的公式是

	n*(√5+2)/2

但是这公式咋来的忘了……于是从BEATTY函数开始重新推(BEATTY函数,若a,b两堆数,a和b不相交且a并b等于R,则1/a+1/b=1)
顺便在这里推一边威佐夫博弈吧:

先手的必败态:
(0,0)  差为0
(1,2)  差为1  
(3,5)  差为2
(4,7)  差为3
(8,12)  差为4
……
我们可以大概看出差是在递增的,若a=an,则b=an+n
根据BEATTY函数,1/α+1/(α+1)=1
可解得α=(√5+2)/2
当差*α==a,则先手必败

用同样的方法,我们来推这道题:

先手只有在差为k+1时才可能必败
其根据BEATTY函数,1/α+1/(α+k+1)=1
α=(√(k^2+2*k+5)+1-k)/2
当差==(k+1)的倍数,且差/(k+1)*α==a,则先手必败

代码如下:

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<int,int> m_i_i;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
const ll INF=0x3f3f3f3f;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 100005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set::iterator
#define fs(n) fixed<< setprecision(n)
//const double e=2.71828182845;
#define max(a,b) a>b?a:b
#define min(a,b) a
const double pi = acos(-1.0);
const ll mod=998244353;
inline int read(){
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
inline void write(ll x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
float SqrtByCarmack( float number )
{
    int i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = * ( int * ) &y;
    i  = 0x5f375a86 - ( i >> 1 );
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
    ll sum=1;
    while(b)
    {
        if(b%2==1)
        {
            sum=sum*a%mod;
        }
        b/=2;
        a=a*a%mod;
    }
    return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
    int mid=(start+endd)/2;
    if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
        return mid;
    else if(a[mid]<=l)
        return erfen(a,mid+1,endd,l);
    else if(a[mid]>l)
    {
        if(start!=mid)
            return erfen(a,start,mid,l);
        else
            return start-1;
    }
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
    return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
    if(p < 2)
        return 0;
    if(p != 2 && p % 2 == 0)
        return 0;
    ll s = p - 1;
    while(! (s & 1))
        s >>= 1;
    for(int i = 0; i < 5; ++i)
    {
        if(p == prime[i])
            return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1)
        {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1))
            return 0;
    }
    return 1;
}
ll gcd(ll x,ll y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        ll a,b,k;
        cin>>a>>b>>k;
        int p=a;
        if(a>b)
        {
            a=b;
            b=p;
        }
        int cha=b-a;
        double num=((1-k)+sqrt(k*k+2*k+5))/2;
        if(cha%(k+1)!=0)
        {
            cout<<1<<'\n';
            continue;
        }
        cha=cha/(k+1);
        ll nm=floor(num*cha);
        if(nm==a)
            cout<<0<<'\n';
        else
            cout<<1<<'\n';
    }
    return 0;
}

你可能感兴趣的:(博弈论,训练营,c++,博弈论)