codeforces 15C Industrial Nim(NIM 博弈)

bigbigship發表於2014-06-15

題目連結:http://vjudge.net/contest/view.action?cid=47997#problem/C

Description

There are n stone quarries in Petrograd.

Each quarry owns mi dumpers (1 ≤ i ≤ n). It is known that the first dumper of the i-th quarry has xi stones in it, the second dumper hasxi + 1 stones in it, the third has xi + 2, and the mi-th dumper (the last for the i-th quarry) has xi + mi - 1 stones in it.

Two oligarchs play a well-known game Nim. Players take turns removing stones from dumpers. On each turn, a player can select any dumper and remove any non-zero amount of stones from it. The player who cannot take a stone loses.

Your task is to find out which oligarch will win, provided that both of them play optimally. The oligarchs asked you not to reveal their names. So, let's call the one who takes the first stone «tolik» and the other one «bolik».

Input

<p< p="">

The first line of the input contains one integer number n (1 ≤ n ≤ 105) — the amount of quarries. Then there follow n lines, each of them contains two space-separated integers xi and mi (1 ≤ xi, mi ≤ 1016) — the amount of stones in the first dumper of the i-th quarry and the number of dumpers at the i-th quarry.

Output

<p< p="">tolik

Sample Input

Input
2
2 1
3 2
Output
tolik
Input
4
1 1
1 1
1 1
1 1
Output
bolik
題目大意:給定你n個序列 每個序列的第一個元素為x  一個序列有m個元素 這個序列的公差為1;

每次從所有的元素 中取>=1的數 判斷最後是先手贏還是後手贏;

由於元素個數較多 我們需要對其進行整理 不能簡單的進行異或

異或有如下的運演算法則 1^1=0  1^0=1 ,0^0=0  n^(n+1)=1(n為偶數的時候);

因此我們可以對一個序列進行一次處理

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long LL;
LL get(LL x,LL m) //n^(n+1)=1(n為偶數)  1^1=0  0^0=0
{
    LL ans;
    if(m&1){
        if(x&1)
            ans=x;//後面的可以配成對
        else
            ans=x+m-1;//除去最後一項前面的可以配成對
        m--;
    }
    else{
        if(x&1){
            ans=x^(x+m-1);//中間的可以配成對
            m-=2;
        }
        else
            ans=0;
    }
    if(m%4)//判斷是否為偶數對
       return ans^1;
    else
        return ans;
}
int main()
{
    int n;
    long long a,b;
    while(~scanf("%d",&n)){
        long long  ans=0;
        for(int i=0;i<n;i++){
            scanf("%lld%lld",&a,&b);
            ans^=get(a,b);
        }
        if(ans)
            puts("tolik");
        else
            puts("bolik");
    }
    return 0;
}

 

相關文章