题解:P11010 『STA - R7』Divide and Merge Game
比赛结束前最后几分钟过的 T4。
我们发现,Alice 的最优策略显然为:先构造一个长度为
而 Bob 的最优策略为:最小化
我们分析完最优策略后,观察上面列出的等式
直接
奉上代码(赛后修整了一下):
#include <bits/stdc++.h>
using namespace std;
int primes[4] = {2, 3, 11, 97};
inline int gcd(int a, int b) {
int az = __builtin_ctzll(a);
int bz = __builtin_ctzll(b);
int z = min(az, bz);
b >>= bz;
while (a) {
a >>= az;
int diff = a - b;
az = __builtin_ctzll(diff);
b = min(a, b), a = abs(diff);
}
return b << z;
}
inline int power(int a, int b, int p) {
int res = 1;
a %= p;
while (b) {
if (b & 1) {
res = (int)((long long)res * (long long)a % (long long)p);
}
a = (int)((long long)a * (long long)a % (long long)p);
b >>= 1;
}
return res;
}
inline bool pr(int n) {
if (n < 2) return 0;
if (n == 2 || n == 3) return 1;
if ((n & 1) ^ 1) return 0;
int d = n - 1;
int r = 0;
while ((d & 1) ^ 1) {
d >>= 1;
++r;
}
for (int nums : primes) {
if (nums >= n) break;
int x = power(nums, d, n);
if (x == 1 || x == n - 1) continue;
bool ok = 0;
for (int i = 1; i < r; ++i) {
x = (int)((long long)x * (long long)x % (long long)n);
if (x == n - 1) {
ok = 1;
break;
}
}
if (ok) continue;
return 0;
}
return 1;
}
inline int getfac(int n) {
if ((n & 1) ^ 1) return 2;
int x = rand() % (n - 2) + 2;
int y = x;
int c = rand() % (n - 1) + 1;
int d = 1;
while (d == 1) {
x = ((int)((long long)x * (long long)x % (long long)n) + c) % n;
y = ((int)((long long)y * (long long)y % (long long)n) + c) % n;
y = ((int)((long long)y * (long long)y % (long long)n) + c) % n;
d = gcd(abs(x - y), n);
}
return d == n ? getfac(n) : d;
}
inline int solve(int n) {
if (pr(n)) return n;
int factor = getfac(n);
return max(solve(factor), solve(n / factor));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
if (1ll * k * k <= 1ll * n) {
cout << "Alice\n";
continue;
}
if (n % 2 == 0) {
if (k >= n / 2 + 1) {
cout << "Bob\n";
} else {
cout << "Alice\n";
}
} else {
long long up = 0;
int ttt = n;
int mul = 1;
while (!pr(ttt)) {
int x = solve(ttt);
mul *= x;
ttt /= x;
}
up -= mul - n;
if (k > up) {
cout << "Bob\n";
} else {
cout << "Alice\n";
}
}
}
return 0;
}