题目:
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.
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.
第一次遇到这种类型,感觉很好玩。但是我发现可能又是我的臆想,我先是以为两堆石子,一堆为2个,一堆为1个,我从第一堆取了一个,计算机知道了,然后它也从第一个取了一个,我再从第二个取一个获胜。有点人机游戏的感觉,当然,这是看了原题连接以后产生的想法,甚至以为input第二行的数字是自动显示出来的。。。扎肺了。。原题连接:原题
关于flush()
第一点:引用网上的回答:flush() 是把缓冲区的数据强行输出,(注意不要和frush()刷新混淆了)
主要用在IO中,即清空缓冲区数据,一般在读写流(stream)的时候,数据是先被读到了内存中,再把数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还有一部分有可能会留在内存这个缓冲区中。这时候如果你调用了close()方法关闭了读写流,那么这部分数据就会丢失,所以应该在关闭读写流之前先flush()。
第二点:
当我没有用cout.flush()的时候也可以ac。。。所以为什么题中会提醒写这一行代码,现在还不是很清楚。
题中既然已经说了你有获胜的可能,所以就不会出现两堆石子数量相同的情况,而自己轮完之后,肯定是自己会赢,此时需要做的就是,对方取几个石子自己就从另一堆取几个石子,直到获胜。
但还有一点疑问:如果此时两堆情况为2 2,对方从1堆中取了3,这显然是不符合实际情况,但我们的程序依然会和对方一起取3,没有尽头的感觉
代码:
#include<iostream>
using namespace std;
int main( )
{
int a,b;
cin>>a>>b;
if(a==0)
{
cout<<2<<" "<<b<<endl;
cout.flush();
return 0;
}
if(b==0)
{
cout<<1<<" "<<a<<endl;
cout.flush();
return 0;
}
if(a>b)
{
cout<<"1"<<" "<<a-b<<endl;
cout.flush();
}
else if(a<b)
{
cout<<"2"<<" "<<b-a<<endl;
cout.flush();
}
int x;
while(cin>>x>>b)
{
cout<<3-x<<" "<<b<<endl;
cout.flush();
}
return 0;
}