跳到正文

题解

题解:P11010 『STA - R7』Divide and Merge Game

lkrqwq约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟
获赞
5
题目
P11010

版本

  1. v1当前

比赛结束前最后几分钟过的 T4。

我们发现,Alice 的最优策略显然为:先构造一个长度为 kk 的全 11 序列,再找一个地方加上 n−kn - k,这样可以尽量让 1+(n−k)>nm1 + (n - k) > \frac{n}{m},从而达到胜利的目的。

而 Bob 的最优策略为:最小化 mm 的取值,因为这样可以更好的让 1+(n−k)1 + (n - k) 和其他 11 组成 nm\frac{n}{m}。

我们分析完最优策略后,观察上面列出的等式 1+(n−k)>nm1 + (n - k) > \frac{n}{m},再移个项就可以转化为 n−nm≥kn - \frac{n}{m} \ge k,此时 Alice 必胜。再根据 Bob 的最优策略,nm\frac{n}{m} 就是 nn 的最大因子(不含 nn)。

直接 O(n)O(\sqrt{n}) 枚举质因数是会超时的,那么我们直接上个 Pollard-Rho 分解质因数就做完了。

奉上代码(赛后修整了一下):

C++
#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;
}
原文