Codeforces Gym-100985C: MaratonIME plays Nim(交互题&博弈)

C. MaratonIME plays Nim

time limit per test : 2.0 s
memory limit per test : 64 MB
inputstandard input
outputstandard output
Ai Fox!
UNIONFIND, GERMANO
You open your eyes, but everything remains dark. The world is dark, and everything shakes. You realize you are locked in, but before desperation takes hold, you hear the door opening and the light invades your sight and blinds you for a few moments.

They help you out, you had been locked inside a trunk. You don't know the masked faces before you, but remember that in the last competitive programming practice they told you that "the beginning is yet to come". "So this is the fabled MaratonIME's initiation challenge", you had heard rumors of this event, and you feel honored to be chosen.

After walking into and abandoned building, they sit you on an old chair. The first test is to watch a soccer game without any show of excitement. Easy. The second is to install Linux on a notebook in less than 5 minutes. You were prepared, carrying the Arch Linux pendrive as usual, just in case. You face more tests, and manage to pass all of them despite a few difficulties.

Hours go by, the members remove their masks, and each take a coin out of their pocket. "I won! And I even got rich" you think, but realize they place the coins in a table in front of you, divided in two piles. Renzo, MaratonIME's great boss, takes a chair and sits in front of you. You will play a match of Nim, and if you win you will become an honorary member of MaratonIME, that is, you win a balloon.

Nim is a game of two players, alternating their turns. Two piles of coins are placed on a table and in each turn you can remove any non zero quantity of coins from one of the piles. The last player to take their turn (leaving both piles empty) wins.

You start the game. So it would not be unfair, it is guaranteed that it is possible for you to win. Write a program than beats Renzo 100% of the time.

Input

In the first line, two integers, x and y, the size of the piles, such that 0 ≤ x, y ≤ 104. It is guaranteed that you can win the game.

Interaction

In your turn, print two integers, i and x, where i is the number of the pile from which you will remove the coins (), and x is the number of coins you will remove (x ≥ 1, such that i has at least x coins).

In Renzo's turn, read two integers, in the same format as in your turn.

Example

input

2 1
1 1

output

1 1
2 1

Note
Of course we do not do an initiation challenge like this :P

In the example, we have a pile with 2 coins and another with 1. You remove 1 coin from the first pile, and now no matter what coin Renzo removes, you can remove the other and win.

Remember, after printing your play, flush the output, like: fflush(stdout); in C, cout.flush(); in C++, or sys.stdout.flush() in Python.

题意

 A和B进行nim博弈,A先手,并且当前局势A是必胜的,怎么拿可以保证结果是A必胜的

Input的第一行是原本两堆的石子数,接下来有若干行,每行第一个数字是B取第一堆或第二堆,第二个数字是取的个数

output输出每次A拿石子的方案,第一个数代表拿第几堆,第二个数是拿走的个数

思路

每次A取完石子后,保证剩下的两堆石子数目相同就可以保证B必败

代码

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=1e6+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 int main(int argc, char const *argv[])
12 {
13     ios::sync_with_stdio(false);
14     cin.tie(0);
15     int x,y,i,n;
16     cin>>x>>y;
17     if(x==0)
18         cout<<2<<" "<<y<<endl;
19     else if(y==0)
20         cout<<1<<" "<<x<<endl;
21     else
22     {
23         if(x>y)
24             cout<<1<<" "<<x-y<<endl;
25         else if(x<y)
26             cout<<2<<" "<<y-x<<endl;
27         while(cin>>i>>n)
28         {
29             if(i==1)
30                 cout<<2<<" "<<n<<endl;
31             else if(i==2)
32                 cout<<1<<" "<<n<<endl;
33         }
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/Friends-A/p/11337871.html