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(1≤t≤104,1≤k,x≤109),计算 t t t 组 n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n(1\le n\le 2\cdot10^5) n(1≤n≤2⋅105)的函数值:
∑ 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=1∑n⋯ax=1∑nj=1∏xajk⋅f(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=1∑na1=1∑na2=1∑n⋯ax=1∑n(j=1∏xajk)⋅f(d)⋅[gcd(a1,⋯,ax)=d]⋅d=d=1∑nf(d)⋅dkx+1a1=1∑⌊dn⌋a2=1∑⌊dn⌋⋯ax=1∑⌊dn⌋(j=1∏xajk)⋅[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=1∑na2=1∑n⋯ax=1∑n(j=1∏xajk)⋅[gcd(a1,⋯,ax)=1]=a1=1∑na2=1∑n⋯ax=1∑n(j=1∏xajk)⋅p∣gcd(a1,⋯,ax)∑μ(p)=p=1∑na1=1∑⌊pn⌋a2=1∑⌊pn⌋⋯ax=1∑⌊pn⌋(j=1∏x(aj⋅p)k)⋅μ(p)=p=1∑npkx⋅μ(p)⋅⎝⎜⎛i=1∑⌊pn⌋ik⎠⎟⎞x
令:
S ( n ) = ( ∑ i = 1 n i k ) x S(n)=\left(\sum_{i=1}^{n}i^k\right)^x S(n)=(i=1∑nik)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=1∑npkx⋅μ(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=1∑nf(d)⋅dkx+1⋅h(⌊dn⌋)=d=1∑nf(d)⋅dkx+1⋅p=1∑⌊dn⌋pkx⋅μ(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=1∑nS(⌊Tn⌋)⋅p∣T∑pkx⋅μ(p)⋅f(pT)⋅(pT)kx+1=T=1∑nS(⌊Tn⌋)⋅⎝⎛p∣T∑μ(p)⋅(μ(pT))2⋅pT⎠⎞⋅Tkx
令:
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)=⎝⎛p∣T∑μ(p)⋅(μ(pT))2⋅pT⎠⎞⋅Tkx
则原式:
= ∑ 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=1∑nS(⌊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)。
#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;
}