题解
真tm麻烦
先考虑只有一个数的情况
假如我是后手,由于每次可以减123,无论对手减多少,我总可以使这一轮这个数总共减去的值为四的倍数
恰好当n位4的时候先手必败,所以如果一个数为四的倍数时,先手必败
考虑多个数
数组里,有的数是4的倍数,有的不是。
此时假设我是先手,遇到四的倍数,我要尽可能地减慢这个数变小的速度,对于非四倍数的数,我要尽可能加快变小的速度
code
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100005;
const int MAX_P = 5000035;
const int MAX_V = 5000006;
int nums[MAX_N];
int primes[MAX_P];
int isComp[MAX_P] = {0};
int pCount = 1;
vector<int> pMod1, pMod2, pMod3;
void sieve() {
primes[1] = 1;
for (int i = 2; i <= MAX_V; i++) {
if (!isComp[i]) {
primes[++pCount] = i;
if (i % 4 == 1) pMod1.push_back(i);
if (i % 4 == 2) pMod2.push_back(i);
if (i % 4 == 3) pMod3.push_back(i);
}
for (int j = 2; j <= pCount; j++) {
if (primes[j] * i >= MAX_V) break;
isComp[i * primes[j]] = 1;
if (i % primes[j] == 0) break;
}
}
}
int main() {
sieve();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> nums[i];
int minStepsNhoj = INT_MAX, minStepsJohn = INT_MAX;
int minIdxNhoj = -1, minIdxJohn = -1;
for (int i = 1; i <= n; i++) {
int num = nums[i];
if (num % 4 == 0) {
int steps = num / 4 + 1;
if (steps < minStepsNhoj) {
minStepsNhoj = steps;
minIdxNhoj = i;
}
} else if (num % 4 == 1) {
auto upper = upper_bound(pMod1.begin(), pMod1.end(), num);
if (upper != pMod1.end()) {
int steps = (num - *(upper - 1)) / 4 + 1;
if (steps < minStepsJohn) {
minStepsJohn = steps;
minIdxJohn = i;
}
}
} else if (num % 4 == 2) {
auto upper = upper_bound(pMod2.begin(), pMod2.end(), num);
if (upper != pMod2.end()) {
int steps = (num - *(upper - 1)) / 4 + 1;
if (steps < minStepsJohn) {
minStepsJohn = steps;
minIdxJohn = i;
}
}
} else if (num % 4 == 3) {
auto upper = upper_bound(pMod3.begin(), pMod3.end(), num);
if (upper != pMod3.end()) {
int steps = (num - *(upper - 1)) / 4 + 1;
if (steps < minStepsJohn) {
minStepsJohn = steps;
minIdxJohn = i;
}
}
}
}
if (minStepsNhoj < minStepsJohn) {
puts("Farmer Nhoj");
} else if (minStepsNhoj > minStepsJohn) {
puts("Farmer John");
} else if (minIdxNhoj < minIdxJohn) {
puts("Farmer Nhoj");
} else {
puts("Farmer John");
}
}
return 0;
}
标签:const,int,MAX,P8901,倍数,Farmer,primes,Barn,Circular
From: https://www.cnblogs.com/pure4knowledge/p/18238769