链接:https://ac.nowcoder.com/acm/problem/15867
来源:牛客网
题目描述
Tony and Macle are good friends. One day they joined a birthday party together. Fortunately, they got the opportunity to have dinner with the princess, but only one person had the chance. So they decided to play a game to decide who could have dinner with the princess.
The rules of the game are very simple. At first, there are n sticks. Each of exactly m meters in length. Each person takes turns. For one move the player chooses just one stick and cut it into several equal length sticks, and the length of each stick is an integer and no less than k. Each resulting part is also a stick which can be chosen in the future. The player who can’t make a move loses the game ,thus the other one win.
Macle make the first move.
输入描述:
the first line contains tree integer n,m,k(1 <= n,m,k <= 109)
输出描述:
print ‘Macle’ if Macle wins or ‘Tony’ if Tony wins,you should print everything without quotes.
示例1
输入
1 15 4
输出
Macle
示例2
输入
4 9 5
输出
Tony
题意
n 个 m 米长的木棍,每次可以把他截为多个相同长度的木棍,但是长度没整数,并且大于等于 k ,不能能截取者败,Macle为先手,Tony后手,输出胜利者
思路
多个相同NIM博弈变形,首先 偶数个相同时数异或值为 0 ,所以当 n%2 == 0 时,Yony胜。
当 n%2 == 1 时,因为0 ^ n = n,所以我们只需要看一根木棍就行了,而他必败态时当木棍的长度为 [k ,2 * k - 1],也就是 m / k <=1,每次最截取,所以 我们从 m/k ~ 2 中寻找必胜态,如果 m 是 [k,2 * k - 1]中某个数的倍数,也就是存在 i ∈ [m/k,2],如果 m%i == 0 就是先手必胜态,反之必败。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<ctype.h>
#include<vector>
#include<algorithm>
#include<sstream>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;
const int maxn = 1010;
const int k = 1000;
int main(void)
{
int n,m,k;
cin>>n>>m>>k;
if(n%2==0) printf("Tony\n");
else{
bool ans = false;
for(int i=m/k;i>1;i--){
if(m%i==0){
ans = true;
break;
}
}
if(ans) printf("Macle\n");
else printf("Tony\n");
}
return 0;
}