题解 P4464 【[国家集训队]JZPKIL】

· · 题解

这是一道毒瘤题

需要求的是

\sum_{i=1}^n(i,n)^x[i,n]^y

由 xy=(x,y)[x,y]

\sum_{i=1}^n(i,n)^{x-y}i^yn^y $$\sum_{i=1}^n(i,n)^{x-y}i^y$$ 枚举 $\gcd \sum_{d|n}d^{x-y}\sum_{i=1}^{\frac nd}[(id,n)=d](id)^y

把 d^y 提出,顺便套上 Möbius 反演公式

\sum_{d|n}d^x\sum_{i=1}^{\frac nd}[\left(i,\frac nd\right)=1]i^y \sum_{d|n}d^x\sum_{i=1}^{\frac nd}\sum_{e|\left(i, \frac nd\right)}\mu(e)i^y

交换求和顺序

\sum_{d|n}d^x\sum_{e|\frac nd}\mu(e)\sum_{i=1}^{\frac n{de}}(ie)^y

将 e^y 提出

\sum_{d|n}d^x\sum_{e|\frac nd}\mu(e)e^y\sum_{i=1}^{\frac n{de}}i^y

众所周知,自然数 k 次幂和是 k+1 次多项式

假设已经求出了这个多项式的系数 t_i

\sum_{i=1}^ni^k=\sum_{i=0}^{k+1}t_in^i

进一步化简

\sum_{d|n}d^x\sum_{e|\frac nd}\mu(e)e^y\sum_{i=0}^{y+1}t_i\left(\frac n{de}\right)^i

再一次交换求和顺序

\sum_{i=0}^{y+1}t_i\sum_{d|n}d^x\sum_{e|\frac nd}\mu(e)e^y\left(\frac n{de}\right)^i

后面相当于三个积性函数的狄利克雷卷积

F(n)=n^x, G(n)=\mu(n)n^y, H(n)=n^i, Z(n)=(F*G*H)(n)

那么卷积后的还是积性函数

于是将 n 分解质因数(使用 Pollard-Rho 算法)

现在只需要计算 Z(p^k)

由 Möbius 函数性质,发现只有 e=1 或 e=p 时有意义

枚举 d=p^q(0\leq q\leq k)

(1) e=1

答案为 \sum_{q=0}^kp^{qx}\times p^{(k-q)i}

(2) e=p

答案为 -\sum_{q=0}^{k-1}p^{qx}\times p^y\times p^{(k-q-1)i}

得到了 Z(p^k) 的值之后,由积性函数性质可以得到 Z(n)

整个问题即可解决

刚才还遗留一个问题,自然数 k 次幂和的多项式怎么求?

这是一个经典问题,可以通过斯特林数,伯努利数,差分,拉格朗日插值等方法求得(我用的是伯努利数)

代码:

#include <iostream>
#include <algorithm>
#include <unordered_map>

const int N = 3005;
const int mod = 1E9 + 7;
using LL = long long;
using LD = long double;
int tc, factor[N], ifactor[N], B[N], t[N], x, y;
LL n;
std::unordered_map<LL, int> divisor;
inline int pow(int x, int y, int ans = 1) {
    for (; y; y >>= 1, x = static_cast<LL> (x) * x % mod)
        if (y & 1) ans = static_cast<LL> (ans) * x % mod;
    return ans;
}
inline void reduce(int &x) { x += x >> 31 & mod; }
inline void get_Factor(int R) {
    factor[0] = 1;
    for (int i = 1; i <= R; ++i)
        factor[i] = static_cast<LL> (factor[i - 1]) * i % mod;
    ifactor[R] = pow(factor[R], mod - 2);
    for (int i = R - 1; ~i; --i)
        ifactor[i] = static_cast<LL> (ifactor[i + 1]) * (i + 1) % mod;
}
inline int C(int x, int y) {
    return static_cast<LL> (factor[x]) * ifactor[y] % mod * ifactor[x - y] % mod;
}
inline void get_Bernoulli(int R) {
    B[0] = 1;
    for (int i = 1; i <= R; ++i) {
        int sum = 0;
        for (int j = 0; j < i; ++j)
            reduce(sum -= static_cast<LL> (C(i + 1, j)) * B[j] % mod);
        B[i] = pow(i + 1, mod - 2, sum);
    }
}
inline LL mul(LL x, LL y, LL mod) {
#ifdef ONLINE_JUDGE
    return static_cast<__int128> (x) * y % mod;
#else
    LL ret = (x * y - static_cast<LL> (static_cast<LD> (x) * y / mod + 0.5) * mod) % mod;
    return ret + (ret >> 63 & mod);
#endif
}
inline LL powmod(LL x, LL y, LL mod, LL ans = 1) {
    for (; y; y >>= 1, x = mul(x, x, mod))
        if (y & 1) ans = mul(ans, x, mod);
    return ans;
}
const int primes[] = { 2, 3, 5, 7, 11, 13, 23, 31 };
inline bool prime_test(LL n) {
    LL e = n - 1 >> __builtin_ctz(n - 1); 
    for (int p : primes) if (p < n) {
        for (LL d = e, lst = powmod(p, d, n), now; d != n - 1; d <<= 1, lst = now) {
            now = mul(lst, lst, n);
            if (lst != 1 && lst != n - 1 && now == 1) return false;
        }
        if (powmod(p, n - 1, n) != 1) return false;
    }
    return true;
}
inline void get_Poly(int R) {
    t[0] = 0;
    for (int i = 0; i <= R; ++i)
        t[R + 1 - i] = static_cast<LL> (C(R + 1, i)) * B[i] % mod;
    for (int i = 0; i <= R + 1; ++i)
        t[i] = pow(R + 1, mod - 2, t[i]);
    if (R) ++t[R];
}
inline void pollard_rho(LL n) {
    if (n == 1) return;
    if (prime_test(n)) { ++divisor[n]; return; }
    for (; ; ) {
        int c = std::rand(), k = 1, i = 0;
        LL x = std::rand(), y = x;
        for (; ; ) {
            x = mul(x, x, n) + c;
            LL d = std::__gcd(n, std::abs(x - y));
            if (d > 1 && d < n) { pollard_rho(d), pollard_rho(n / d); return; }
            if (x == y) break;
            if (++i == k) k <<= 1, y = x;
        }
    }
}
inline int G(int i, int p, int k) {
    int ans = 0;
    for (int q = 0; q <= k; ++q)
        reduce(ans += pow(p, (static_cast<LL> (q) * x + static_cast<LL> (k - q) * i) % (mod - 1)) - mod);
    for (int q = 0; q < k; ++q)
        reduce(ans -= pow(p, (static_cast<LL> (q) * x + y + static_cast<LL> (k - q - 1) * i) % (mod - 1)));
    return ans;
}
inline int solve(LL n, int x, int y) {
    divisor.clear(), pollard_rho(n), get_Poly(y);
    int ans = 0;
    for (int i = 0; i <= y + 1; ++i) {
        int p = t[i];
        for (auto pk : divisor)
            p = static_cast<LL> (p) * G(i, pk.first % mod, pk.second) % mod;
        reduce(ans += static_cast<LL> (p) % mod - mod);
    }
    ans = static_cast<LL> (ans) * pow(n % mod, y) % mod;
    return ans;
}
int main() {
    std::ios::sync_with_stdio(0), std::cin.tie(0);
    get_Factor(3001);
    get_Bernoulli(3001);
    std::cin >> tc;
    while (tc--)
        std::cin >> n >> x >> y, std::cout << solve(n, x, y) << '\n';
    return 0;
}