codeforces 15C Industrial Nim(NIM 博弈)
題目連結: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
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
Sample Input
2 2 1 3 2
tolik
4 1 1 1 1 1 1 1 1
bolik
每次從所有的元素 中取>=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;
}
相關文章
- 博弈論入門之nim遊戲遊戲
- 博弈學習(一) NIM + SG函式函式
- Nim遊戲(一堆/N堆)-博弈遊戲
- 博弈論基礎之sg函式與nim函式
- Nim遊戲遊戲
- 洛谷P2197 nim遊戲(Nim遊戲)遊戲
- Nim教程【六】
- Nim教程【二】
- Nim教程【三】
- Nim教程【四】
- Nim教程【五】
- Nim教程【九】
- Nim教程【十】
- Nim教程【十一】
- Nim教程【十二】
- Nim教程【十四】
- HDU 1850-Being a Good Boy in Spring Festival(Nim博弈)GoSpring
- 博弈論:公平組合遊戲(Nim 遊戲 & SG 定理)學習筆記遊戲筆記
- E. Not a Nim Problem
- NIM遊戲/SG函式遊戲函式
- Nim教程【十五】【完結】
- nim的引用和指標指標
- LeetCode-Nim GameLeetCodeGAM
- Nim遊戲2(臺階型)遊戲
- Leetcode 292. Nim GameLeetCodeGAM
- 成功進行NIM安裝(轉)
- AIX errpt:NIM thread blockedAIthreadBloC
- POJ 2311-Cutting Game(Nim博弈-sg函式/記憶化搜尋)GAM函式
- 292. Nim Game--LeetCode RecordGAMLeetCode
- Nim語言的模組化程式設計程式設計
- 使用 Nim 進行基礎影像識別
- LeetCode每日一題:Nim遊戲(No.292)LeetCode每日一題遊戲
- ACM-ICPC 2018 南京賽區網路預賽__K The Great Nim Game【博弈論+費馬小定理+DP】ACMGAM
- HDU 5795 A Simple Nim (SG函式+打表找規律)函式
- [原創][連載]nim與python的異同1Python
- Nim 0.10.2 釋出,指令式程式設計語言程式設計
- 用Nim語言開發windows GUI圖形介面程式WindowsGUI
- IDENTIFIER: 864D2CE3 NIM thread blockedIDEthreadBloC