Timus - 1639. Chocolate 2 二分法 题解

题意:
有一个巧克力由很多小方块组成,每次掰开一半,最后一次没巧克力掰的人算输(所有巧克力都被掰开成一个最小的方块了)。

Little Boy is mad at Karlsson, who ate all the sweets in the flat and even went to the neighbours to eat their sweets too. Now Little Boy's parents brought home a chocolate bar, and, sure enough, Karlsson is here already and wants to eat it. However, this time Little Boy has firmly decided that not a single piece of chocolate will go to this glutton. Little Boy wants to use Karlsson's addiction to the games of chance and suggests playing the following game. A chocolate bar can be considered as a rectangle of square “units” arranged in mrows and ncolumns and separated by “lines”. Two players take alternate turns. At his turn, a player must take one piece of chocolate and split it into two along one of the lines. If a player can't make a legal move (which happens when all pieces of chocolate consist of a single unit square), he loses, and the winner takes all the chocolate.
But Karlsson is smart enough! He immediately understood who should make the first turn in order for Karlsson to win, assuming that players take optimal turns. Can you guess that?

Input

The only line of the input contains space-separated integers mand n<nobr>(1 ≤<em>m</em>,<em>n</em>≤ 50)</nobr>.

Output

If Karlsson should start the game in order to win, output “[:=[first]”; otherwise, output “[second]=:]”.

Samples

input output
2 4
[:=[first]
1 3
[second]=:]


这里使用减治法,时间效率就是O(lg(min(m,n)),min(m,n)是指行和列最小的值,因为下面程序特殊处理了一下:

//认真注意几处计算的地方
int chocolateDivisor(int m, int n)
{
	if (m <= 1) return n-1;
	int sum = chocolateDivisor(m>>1, n);
	sum <<= 1;
	if (m % 2) sum += n;
	return sum+1;
}

void Chocolate2()
{
	int m, n;
	cin>>m>>n;
	if (m > n) swap(m, n);
	if (chocolateDivisor(m, n) % 2) cout<<"[:=[first]";
	else cout<<"[second]=:]";
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值