AT_code_festival_relay_j Color Game 的题解
AT传送门
题目大意
有 n n n 个距离相等的白色石头,有两人用这些石头做游戏。规则就是每个玩家轮流选择白色石头并将其变为黑色,但是不能从前一回合变黑的石头中选择距离在 k k k 内的石头。无法选择白色石头的玩家失败。在这个游戏中,双方采用最优策略,询问先涂必胜或者后涂必胜。
思路
这是一道较为简单的思维题,我们可以画图分析:
当 n = 7 n=7 n=7, k = 5 k=5 k=5 时,只需要涂黑中间的石头:
当 n = 15 n=15 n=15, k = 3 k=3 k=3 时,如图:
注:涂灰的是对手的,涂黑的是自己的。
能看到自己有 4 4 4 块涂黑的,对手有 3 3 3 块涂黑的,最后一块可涂黑的石头是自己涂的,因此也可以先出手必胜。
我们能总结出这些结论:
-
如果 k ≥ n ÷ 2 k \ge n \div 2 k≥n÷2 ,先手方直接把中间的石子涂黑,对方就输了。
-
如果 n n n 为奇数,那么最后一定会先手方涂黑一个石子后,后手方无处可下。
-
如果 n n n 为偶数,那么最后一定会后手方涂黑一个石子后,先手方无处可下。
代码
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
inline int read() {
register int x = 0, f = 1;
register char c = getchar();
while (c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
inline void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
return;
}
}
using namespace fastIO;
int main() {
int n, k;
cin >> n >> k;
if (k >= n / 2) {
cout << "first" << endl;
}
else if (n % 2 == 1) {
cout << "first" << endl;
}
else {
cout << "second" << endl;
}
return 0;
}
PS:岛国题目输出后要加空格。