Timus 2068. Game of Nuts 解题报告

1.题目描述:

2068. Game of Nuts

Time limit: 1.0 second
Memory limit: 64 MB
The war for Westeros is still in process, manpower and supplies are coming to an end and the winter is as near as never before. The game of thrones is unpredictable so Daenerys and Stannis decided to determine the true ruler of Seven Kingdoms playing more predictable and shorter game. For example, the game of nuts is the ideal candidate.
Rules of this game are quite simple. Players are given  n heaps of nuts. There is an odd number of nuts in each heap. Players alternate turns. In each turn player chooses an arbitrary heap and divides it into three non-empty heaps so as there is again an odd number of nuts in each heap. The player who cannot make a move loses.
Daenerys has dragons so she moves first. Your task is to determine the winner assuming both Daenerys and Stannis play optimally. Please, do it and stop the war for Westeros!

Input

In the first line there is an odd integer  n (1 ≤  n ≤ 777).
In the second line there are  n integers separated by spaces. These are the amounts of nuts in each heap at the beginning of the game. It is guaranteed that each heap contains not less than one and not more than 54321 nuts and this amount is an odd number.

Output

Output "Daenerys" (without quotes) in case of Daenerys’ win. Otherwise output "Stannis".

Samples

inputoutput
1
3
Daenerys
3
1 1 1
Stannis
5
777 313 465 99 1
Daenerys

 

2.题目分析:

又是一道博弈问题。给定一个奇数k,和k个奇数,博弈双方轮流将一个奇数nn分为三个奇数a,b,c,a+b+c==nn,最后拿到全1的那个人输。

 

1.先假设k=1,这时我们可以用一个bool win[maxn]的数组从i=1开始记录博弈方抽到i时的输赢。

遵循如下规则:win[i] = false, if only if 对每个abc的组合,满足 a+b+c = i, 存在win[a或b或c] = true。

通俗的讲,就是不管如何拆分i,都会给后手留下赢的机会。

现在可以递推一下:

win[1] = false;

win[3] = win[1+1+1] = true;

win[5] = false;

win[7] = win[1+1+5] = true;

win[9] = false;

win[11] = win[1+1+9] = true;

......

win[4n+1] = false && win[4n+3] = true

2.考虑k>1的情况,根据1,我们已经知道拆分一个能赢的数,得到的数都会输,如果能够拆分奇数次,我就会赢,反之会输。设在k个数中,能赢的数有m个,

sum = (k-m)(4n+1)+m(4n+3) = 4nk+k+2m

(sum-k)/2 = 2nk+m

(sum-k)/2 = m(mod2)

3.代码

#include <iostream>

using namespace std;

int main() {
	int n, sum, tmp;
	cin >> n;
	int i;
	sum = 0;
	for (i = 0; i < n; i++) {
		cin >> tmp;
		sum += tmp;
	}
	if ((sum-n)/2 % 2) {
		cout << "Daenerys";
	} else {
		cout << "Stannis";
	}
	cout << endl;
}

 4.心得体会

这种博弈问题,一般都能通过从最简单情况找规律得到一个简单的解法。应该注意观察分析。

转载于:https://www.cnblogs.com/IVY-BUG/p/5254227.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值