2020HDU多校第六场:1007. A Very Easy Math Problem

题目链接

hdu6833

题目大意

给定 t , k , x ( 1 ≤ t ≤ 1 0 4 , 1 ≤ k , x ≤ 1 0 9 ) t, k, x (1\le t\le 10^4,1\le k,x\le 10^9) t,k,x(1t104,1k,x109),计算 t t t n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n(1\le n\le 2\cdot10^5) n(1n2105)的函数值:

∑ a 1 = 1 n ⋯ ∑ a x = 1 n ∏ j = 1 x a j k ⋅ f ( gcd ⁡ ( a 1 , ⋯   , a x ) ) gcd ⁡ ( a 1 , ⋯   , a x ) \sum_{a_1=1}^{n}\cdots \sum_{a_x=1}^{n} \prod_{j=1}^x a_j^k \cdot f(\gcd(a_1,\cdots ,a_x))\gcd(a_1,\cdots ,a_x) a1=1nax=1nj=1xajkf(gcd(a1,,ax))gcd(a1,,ax)

其中 f ( x ) = ∣ μ ( x ) ∣ f(x)=|\mu(x)| f(x)=μ(x)

解题思路

枚举 g c d ( a 1 , ⋯   , a x ) = d gcd(a_1,\cdots ,a_x)=d gcd(a1,,ax)=d

= ∑ d = 1 n ∑ a 1 = 1 n ∑ a 2 = 1 n ⋯ ∑ a x = 1 n ( ∏ j = 1 x a j k ) ⋅ f ( d ) ⋅ [ g c d ( a 1 , ⋯   , a x ) = d ] ⋅ d = ∑ d = 1 n f ( d ) ⋅ d k x + 1 ∑ a 1 = 1 ⌊ n d ⌋ ∑ a 2 = 1 ⌊ n d ⌋ ⋯ ∑ a x = 1 ⌊ n d ⌋ ( ∏ j = 1 x a j k ) ⋅ [ g c d ( a 1 , ⋯   , a x ) = 1 ] \begin{aligned} &=\sum_{d=1}^{n}\sum_{a_1=1}^{n}\sum_{a_2=1}^{n}\cdots\sum_{a_x=1}^{n}\left(\prod_{j=1}^{x}{a_j}^{k}\right)\cdot f(d)\cdot [gcd(a_1,\cdots ,a_x)=d]\cdot d\cr &=\sum_{d=1}^{n}f(d)\cdot d^{kx+1}\sum_{a_1=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{a_2=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\cdots \sum_{a_x=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\left(\prod_{j=1}^{x}{a_j}^{k}\right)\cdot [gcd(a_1,\cdots ,a_x)=1]\cr \end{aligned} =d=1na1=1na2=1nax=1n(j=1xajk)f(d)[gcd(a1,,ax)=d]d=d=1nf(d)dkx+1a1=1dna2=1dnax=1dn(j=1xajk)[gcd(a1,,ax)=1]

又有设:

h ( n ) = ∑ a 1 = 1 n ∑ a 2 = 1 n ⋯ ∑ a x = 1 n ( ∏ j = 1 x a j k ) ⋅ [ g c d ( a 1 , ⋯   , a x ) = 1 ] = ∑ a 1 = 1 n ∑ a 2 = 1 n ⋯ ∑ a x = 1 n ( ∏ j = 1 x a j k ) ⋅ ∑ p ∣ g c d ( a 1 , ⋯   , a x ) μ ( p ) = ∑ p = 1 n ∑ a 1 = 1 ⌊ n p ⌋ ∑ a 2 = 1 ⌊ n p ⌋ ⋯ ∑ a x = 1 ⌊ n p ⌋ ( ∏ j = 1 x ( a j ⋅ p ) k ) ⋅ μ ( p ) = ∑ p = 1 n p k x ⋅ μ ( p ) ⋅ ( ∑ i = 1 ⌊ n p ⌋ i k ) x \begin{aligned} h(n)&=\sum_{a_1=1}^{n}\sum_{a_2=1}^{n}\cdots\sum_{a_x=1}^{n}\left(\prod_{j=1}^{x}{a_j}^{k}\right)\cdot[gcd(a_1,\cdots ,a_x)=1]\cr &=\sum_{a_1=1}^{n}\sum_{a_2=1}^{n}\cdots\sum_{a_x=1}^{n}\left(\prod_{j=1}^{x}{a_j}^{k}\right)\cdot\sum_{p|gcd(a_1,\cdots,a_x)}\mu(p)\cr &=\sum_{p=1}^{n}\sum_{a_1=1}^{\left\lfloor\frac{n}{p}\right\rfloor}\sum_{a_2=1}^{\left\lfloor\frac{n}{p}\right\rfloor}\cdots \sum_{a_x=1}^{\left\lfloor\frac{n}{p}\right\rfloor}\left(\prod_{j=1}^{x}{\left(a_j\cdot p\right)}^{k}\right)\cdot \mu(p)\cr &=\sum_{p=1}^{n}p^{kx}\cdot\mu(p)\cdot\left(\sum_{i=1}^{\left\lfloor\frac{n}{p}\right\rfloor}i^k\right)^x \end{aligned} h(n)=a1=1na2=1nax=1n(j=1xajk)[gcd(a1,,ax)=1]=a1=1na2=1nax=1n(j=1xajk)pgcd(a1,,ax)μ(p)=p=1na1=1pna2=1pnax=1pn(j=1x(ajp)k)μ(p)=p=1npkxμ(p)i=1pnikx

令:

S ( n ) = ( ∑ i = 1 n i k ) x S(n)=\left(\sum_{i=1}^{n}i^k\right)^x S(n)=(i=1nik)x

则:

h ( n ) = ∑ p = 1 n p k x ⋅ μ ( p ) ⋅ S ( ⌊ n p ⌋ ) h(n)=\sum_{p=1}^{n}p^{kx}\cdot\mu(p)\cdot S\left(\left\lfloor\frac{n}{p}\right\rfloor\right) h(n)=p=1npkxμ(p)S(pn)

所以原式:

= ∑ d = 1 n f ( d ) ⋅ d k x + 1 ⋅ h ( ⌊ n d ⌋ ) = ∑ d = 1 n f ( d ) ⋅ d k x + 1 ⋅ ∑ p = 1 ⌊ n d ⌋ p k x ⋅ μ ( p ) ⋅ S ( ⌊ n p d ⌋ ) \begin{aligned} &=\sum_{d=1}^{n}f(d)\cdot d^{kx+1}\cdot h\left(\left\lfloor\frac{n}{d}\right\rfloor\right)\cr &=\sum_{d=1}^{n}f(d)\cdot d^{kx+1}\cdot \sum_{p=1}^{\left\lfloor\frac{n}{d}\right\rfloor}p^{kx}\cdot\mu(p)\cdot S\left(\left\lfloor\frac{n}{pd}\right\rfloor\right) \end{aligned} =d=1nf(d)dkx+1h(dn)=d=1nf(d)dkx+1p=1dnpkxμ(p)S(pdn)

枚举 T = p d T=pd T=pd

= ∑ T = 1 n S ( ⌊ n T ⌋ ) ⋅ ∑ p ∣ T p k x ⋅ μ ( p ) ⋅ f ( T p ) ⋅ ( T p ) k x + 1 = ∑ T = 1 n S ( ⌊ n T ⌋ ) ⋅ ( ∑ p ∣ T μ ( p ) ⋅ ( μ ( T p ) ) 2 ⋅ T p ) ⋅ T k x \begin{aligned} &=\sum_{T=1}^{n}S\left(\left\lfloor\frac{n}{T}\right\rfloor\right)\cdot\sum_{p|T}p^{kx}\cdot\mu(p)\cdot f\left(\frac{T}{p}\right)\cdot \left(\frac{T}{p}\right)^{kx+1}\cr &=\sum_{T=1}^{n}S\left(\left\lfloor\frac{n}{T}\right\rfloor\right)\cdot\left(\sum_{p|T}\mu(p)\cdot \left(\mu\left(\frac{T}{p}\right)\right)^2\cdot \frac{T}{p}\right)\cdot T^{kx} \end{aligned} =T=1nS(Tn)pTpkxμ(p)f(pT)(pT)kx+1=T=1nS(Tn)pTμ(p)(μ(pT))2pTTkx

令:

g ( T ) = ( ∑ p ∣ T μ ( p ) ⋅ ( μ ( T p ) ) 2 ⋅ T p ) ⋅ T k x g(T)=\left(\sum_{p|T}\mu(p)\cdot \left(\mu\left(\frac{T}{p}\right)\right)^2\cdot \frac{T}{p}\right)\cdot T^{kx} g(T)=pTμ(p)(μ(pT))2pTTkx

则原式:

= ∑ T = 1 n S ( ⌊ n T ⌋ ) ⋅ g ( T ) =\sum_{T=1}^{n}S\left(\left\lfloor\frac{n}{T}\right\rfloor\right)\cdot g(T) =T=1nS(Tn)g(T)

对于全部的 g ( T ) g(T) g(T),我们可以从 1 1 1 n n n 枚举因子 p p p,对于其全部的倍数进行处理,复杂度 O ( n l o g n ) O(nlogn) O(nlogn);我们可以线性筛出 μ ( i ) \mu(i) μ(i) S ( i ) S(i) S(i)。预处理总体复杂度 O ( n l o g n ) O(nlogn) O(nlogn)。对于每组询问,只需要整除分块即可,复杂度 O ( T n ) O(T\sqrt n) O(Tn )

AC代码

#include 
using namespace std;
typedef long long ll;
const int N = 2e5 + 50;
const int P = 1e9 + 7;
int T, n, k, x;

int ksm(int a, ll b, int c) {
    int ret = 1;
    for (; b; b >>= 1, a = 1LL * a * a % c) {
        if (b & 1) {
            ret = 1LL * ret * a % c;
        }
    }
    return ret;
}

int prime[N], num[N], mu[N], tot;
void getmu() {
    mu[1] = 1;
    for (int i = 2; i < N; i++) {
        if (!num[i]) {
            prime[++tot] = i;
            mu[i] = -1;
        }
        for (int j = 1; j <= tot && i * prime[j] < N; j++) {
            num[i * prime[j]] = 1;
            if (i % prime[j]) {
                mu[i * prime[j]] = -mu[i];
            }
            else {
                break;
            }
        }
    }
}
long long g[N], sumg[N];
void getg() {
    for (int i = 1; i < N; i++) {
        for (int j = 1; i * j < N; j++) {
            g[i * j] += mu[i] * mu[j] * mu[j] * j;
        }
    }
    for (int i = 1; i < N; i++) {
        int tmp = ksm(i, 1LL * k * x, P);
        g[i] = (g[i] + P) % P * tmp % P;
    }
    for (int i = 1; i < N; i++) {
        sumg[i] = (sumg[i - 1] + g[i]) % P;
    }
}
int s[N];
void gets() {
    for (int i = 1; i < N; i++) {
        s[i] = (s[i - 1] + ksm(i, k, P)) % P;
    }
    for (int i = 1; i < N; i++) {
        s[i] = ksm(s[i], x, P);
    }
}
void init() {
    getmu();
    getg();
    gets();
}
int work() {
    int ret = 0;
    for (int i = 1, j; i <= n; i = j + 1) {
        j = n / (n / i);
        (ret += 1LL * s[n / i] * (sumg[j] - sumg[i - 1] + P) % P) %= P;
    }
    return ret;
}

int main(int argc, const char * argv[]) {
    scanf("%d%d%d", &T, &k, &x);
    init();
    while (T--) {
        scanf("%d", &n);
        printf("%d\n", work());
    }
    return 0;
}

你可能感兴趣的:(数论与组合数学)