[Roy&October之取石子]

P4018 4860

[Roy&October之取石子]

题目大意:在题目条件下,有没有先手必胜的策略

做法:从小到大找到第一个先手第一次取不完石子且为合数的数(h),如果(n)(h)的倍数,则先手必败,则先手必胜。

证明:

1:考虑若(n<h)则先手可以一次取完

2:若(n=h),则先手一次取不完,则后手必胜

3:若(n>h)且同时(n=k×h),那么先手取多少个,后手就可以给他凑够(h)的某个倍数,则最后面临的就是(2)中的情况,先手必败

4:若(n>h)且同时(n≠k×h),那么先手可以考虑取某个数,使得剩下的数是(h)的倍数,则面临(3)中的情况。

所以结论得证。

Roy&October之取石子1

#include <cstdio>
#include <iostream>
using namespace std;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(x % 6 == 0)//因为$p^{k}$,$k$为自然数,所以第一个先手不能一次取完的合数为6
			printf("Roy wins!
");
		else
			printf("October wins!
");
	}
	return 0;
}

Roy&October之取石子2

#include <cstdio>
#include <iostream>
using namespace std;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(x % 4 == 0)//因为$p^{k}$,$k$只能为$0/1$,所以第一个先手不能一次取完的合数为4
			printf("Roy wins!
");
		else
			printf("October wins!
");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/-Wind-/p/11851628.html