A Very Hard Easy Math Problem
仅作笔记和理解,可能有误。
题意
给你
n
,
x
,
k
,
n,x,k,
n,x,k,让你求
∑
a
1
=
1
n
∑
a
2
=
1
n
⋅
⋅
⋅
∑
a
x
=
1
n
(
∏
j
=
1
x
a
j
k
)
f
(
g
c
d
(
a
1
,
a
2
,
⋅
⋅
⋅
,
a
x
)
)
⋅
g
c
d
(
a
1
,
a
2
,
⋅
⋅
⋅
,
a
x
)
\sum\limits_{a_{1}=1}^{n}\sum\limits_{a_{2}=1}^{n}···\sum\limits_{a_{x}=1}^{n}(\prod\limits_{j=1}^{x}a_{j}^{k})f(gcd(a_{1},a_{2},···,a_{x}))·gcd(a_{1},a_{2},···,a_{x})
a1=1∑na2=1∑n⋅⋅⋅ax=1∑n(j=1∏xajk)f(gcd(a1,a2,⋅⋅⋅,ax))⋅gcd(a1,a2,⋅⋅⋅,ax)
f ( x ) = { 0 , x 有两个及以上相同的质因子 1 , e l s e f(x)= \begin{cases} 0,x有两个及以上相同的质因子 \\ 1,else \end{cases} f(x)={0,x有两个及以上相同的质因子1,else
函数 f ( x ) f(x) f(x)其实就是莫比乌斯函数的平方
t t t组样例,每组样例一个 n n n,求出上面式子的值
1
≤
t
≤
1
0
4
1\leq t \leq10^{4}
1≤t≤104
1
≤
k
≤
1
0
9
1\leq k\leq10^{9}
1≤k≤109
1
≤
x
≤
1
0
9
1\leq x\leq10^{9}
1≤x≤109
1
≤
n
≤
2
⋅
1
0
5
1\leq n\leq2·10^{5}
1≤n≤2⋅105
解
开始化式子= = 除法默认向下取整
首先我们可以枚举gcd的值,那么式子就变成了
∑ g = 1 n ∑ a 1 = 1 n ∑ a 2 = 1 n ⋅ ⋅ ⋅ ∑ a x = 1 n ( ∏ j = 1 x a j k ) f ( g ) ⋅ g [ g c d ( a 1 , a 2 , ⋅ ⋅ ⋅ , a x ) = g ] \sum\limits_{g=1}^{n}\sum\limits_{a_{1}=1}^{n}\sum\limits_{a_{2}=1}^{n}···\sum\limits_{a_{x}=1}^{n}(\prod\limits_{j=1}^{x}a_{j}^{k})f(g)·g[gcd(a_{1},a_{2},···,a_{x})=g] g=1∑na1=1∑na2=1∑n⋅⋅⋅ax=1∑n(j=1∏xajk)f(g)⋅g[gcd(a1,a2,⋅⋅⋅,ax)=g]
上面式子只有在gcd==g的时候才有值的贡献,所以我们把 a 1 , a 2 , ⋅ ⋅ ⋅ , a x a_{1},a_{2},···,a_{x} a1,a2,⋅⋅⋅,ax的值都除上一个 g g g,这时候只有 ( ∏ j = 1 x a j k ) (\prod\limits_{j=1}^{x}a_{j}^{k}) (j=1∏xajk)这一部分的值被影响了,所以只需要把这一部分乘上一个 g g g(下面枚举d的时候也是同样的操作),那么式子就变成了
∑ g = 1 n ∑ a 1 = 1 n g ∑ a 2 = 1 n g ⋅ ⋅ ⋅ ∑ a x = 1 n g ( ∏ j = 1 x ( a j ⋅ g ) k ) f ( g ) ⋅ g [ g c d ( a 1 , a 2 , ⋅ ⋅ ⋅ , a x ) = 1 ] \sum\limits_{g=1}^{n}\sum\limits_{a_{1}=1}^{\frac{n}{g}}\sum\limits_{a_{2}=1}^{\frac{n}{g}}···\sum\limits_{a_{x}=1}^{\frac{n}{g}}(\prod\limits_{j=1}^{x}(a_{j}·g)^{k})f(g)·g[gcd(a_{1},a_{2},···,a_{x})=1] g=1∑na1=1∑gna2=1∑gn⋅⋅⋅ax=1∑gn(j=1∏x(aj⋅g)k)f(g)⋅g[gcd(a1,a2,⋅⋅⋅,ax)=1]
然后如果学过莫比乌斯函数的话就可以马上发现后面 [ g c d ( a 1 , a 2 , ⋅ ⋅ ⋅ , a x ) = 1 ] [gcd(a_{1},a_{2},···,a_{x})=1] [gcd(a1,a2,⋅⋅⋅,ax)=1]这个条件式子可以等价替换成 ∑ d ∣ g c d ( a 1 , a 2 , ⋅ ⋅ ⋅ , a x ) μ ( d ) \sum\limits_{d|gcd(a_{1},a_{2},···,a_{x})}μ(d) d∣gcd(a1,a2,⋅⋅⋅,ax)∑μ(d)
然后式子就可以变成这样
∑ g = 1 n ∑ a 1 = 1 n g ∑ a 2 = 1 n g ⋅ ⋅ ⋅ ∑ a x = 1 n g ( ∏ j = 1 x ( a j ⋅ g ) k ) f ( g ) ⋅ g ∑ d ∣ g c d ( a 1 , a 2 , ⋅ ⋅ ⋅ , a x ) μ ( d ) \sum\limits_{g=1}^{n}\sum\limits_{a_{1}=1}^{\frac{n}{g}}\sum\limits_{a_{2}=1}^{\frac{n}{g}}···\sum\limits_{a_{x}=1}^{\frac{n}{g}}(\prod\limits_{j=1}^{x}(a_{j}·g)^{k})f(g)·g\sum\limits_{d|gcd(a_{1},a_{2},···,a_{x})}μ(d) g=1∑na1=1∑gna2=1∑gn⋅⋅⋅ax=1∑gn(j=1∏x(aj⋅g)k)f(g)⋅gd∣gcd(a1,a2,⋅⋅⋅,ax)∑μ(d)
接下来考虑枚举 d d d的值, d d d是 a 1 , a 2 , ⋅ ⋅ ⋅ , a x a_{1},a_{2},···,a_{x} a1,a2,⋅⋅⋅,ax的 g c d gcd gcd,所以 d d d的值的范围是 [ 1 , n g ] [1,\frac{n}{g}] [1,gn]由于 d d d要是 a 1 , a 2 , ⋅ ⋅ ⋅ , a x a_{1},a_{2},···,a_{x} a1,a2,⋅⋅⋅,ax的 g c d gcd gcd才会有 μ ( d ) μ(d) μ(d)的值,所以当枚举 d d d的时候 a 1 , a 2 , ⋅ ⋅ ⋅ , a x a_{1},a_{2},···,a_{x} a1,a2,⋅⋅⋅,ax的取值范围就会变成 [ 1 , n g d ] [1,\frac{n}{gd}] [1,gdn],所以式子就变成了这样
∑ g = 1 n ∑ d = 1 n g ∑ a 1 = 1 n g d ∑ a 2 = 1 n g d ⋅ ⋅ ⋅ ∑ a x = 1 n g d ( ∏ j = 1 x ( a j ⋅ g ⋅ d ) k ) f ( g ) ⋅ g ⋅ μ ( d ) \sum\limits_{g=1}^{n}\sum\limits_{d=1}^{\frac{n}{g}}\sum\limits_{a_{1}=1}^{\frac{n}{gd}}\sum\limits_{a_{2}=1}^{\frac{n}{gd}}···\sum\limits_{a_{x}=1}^{\frac{n}{gd}}(\prod\limits_{j=1}^{x}(a_{j}·g·d)^{k})f(g)·g·μ(d) g=1∑nd=1∑gna1=1∑gdna2=1∑gdn⋅⋅⋅ax=1∑gdn(j=1∏x(aj⋅g⋅d)k)f(g)⋅g⋅μ(d)
接下来整理一下式子,发现式子后面的很多表达式都和 a 1 , a 2 , ⋅ ⋅ ⋅ , a x a_{1},a_{2},···,a_{x} a1,a2,⋅⋅⋅,ax的值无关,所以可以把他们提到前面,然后式子就变成了
∑ g = 1 n ∑ d = 1 n g f ( g ) ⋅ g ⋅ μ ( d ) ⋅ ( g d ) k x ∑ a 1 = 1 n g d ∑ a 2 = 1 n g d ⋅ ⋅ ⋅ ∑ a x = 1 n g d ( ∏ j = 1 x ( a j ) k ) \sum\limits_{g=1}^{n}\sum\limits_{d=1}^{\frac{n}{g}}f(g)·g·μ(d)·(gd)^{kx}\sum\limits_{a_{1}=1}^{\frac{n}{gd}}\sum\limits_{a_{2}=1}^{\frac{n}{gd}}···\sum\limits_{a_{x}=1}^{\frac{n}{gd}}(\prod\limits_{j=1}^{x}(a_{j})^{k}) g=1∑nd=1∑gnf(g)⋅g⋅μ(d)⋅(gd)kxa1=1∑gdna2=1∑gdn⋅⋅⋅ax=1∑gdn(j=1∏x(aj)k)
再考虑一下 d d d的取值, d d d从1取到了 n g \frac{n}{g} gn,所以我们可以把 d d d乘上 g g g倍,同时修改后面式子里的 d d d,那么式子变成了
∑ g = 1 n ∑ g ∣ d n f ( g ) ⋅ g ⋅ μ ( d g ) ⋅ ( d ) k x ∑ a 1 = 1 n d ∑ a 2 = 1 n d ⋅ ⋅ ⋅ ∑ a x = 1 n d ( ∏ j = 1 x ( a j ) k ) \sum\limits_{g=1}^{n}\sum\limits_{g|d}^{n}f(g)·g·μ(\frac{d}{g})·(d)^{kx}\sum\limits_{a_{1}=1}^{\frac{n}{d}}\sum\limits_{a_{2}=1}^{\frac{n}{d}}···\sum\limits_{a_{x}=1}^{\frac{n}{d}}(\prod\limits_{j=1}^{x}(a_{j})^{k}) g=1∑ng∣d∑nf(g)⋅g⋅μ(gd)⋅(d)kxa1=1∑dna2=1∑dn⋅⋅⋅ax=1∑dn(j=1∏x(aj)k)
这一题,我们考虑前面 g g g和 d d d的取值,我们假设 n n n=5,那么我们有以下几种情况可以取 ( 1 , 1 ) ( 1 , 2 ) ( 1 , 3 ) ( 1 , 4 ) ( 1 , 5 ) ( 2 , 2 ) ( 2 , 4 ) ( 3 , 3 ) ( 4 , 4 ) ( 5 , 5 ) (1,1)(1,2)(1,3)(1,4)(1,5)(2,2)(2,4)(3,3)(4,4)(5,5) (1,1)(1,2)(1,3)(1,4)(1,5)(2,2)(2,4)(3,3)(4,4)(5,5)现在是先枚举 g g g,再求符合条件的 d d d,我们也可以先枚举 d d d,再求符合条件的 g g g,也就是 ( 1 , 1 ) ( 1 , 2 ) ( 2 , 2 ) ( 1 , 3 ) ( 3 , 3 ) ( 1 , 4 ) ( 2 , 4 ) ( 4 , 4 ) ( 1 , 5 ) ( 5 , 5 ) (1,1)(1,2)(2,2)(1,3)(3,3)(1,4)(2,4)(4,4)(1,5)(5,5) (1,1)(1,2)(2,2)(1,3)(3,3)(1,4)(2,4)(4,4)(1,5)(5,5),那么式子就变成了
∑ d = 1 n ∑ g ∣ d f ( g ) ⋅ g ⋅ μ ( d g ) ⋅ ( d ) k x ∑ a 1 = 1 n d ∑ a 2 = 1 n d ⋅ ⋅ ⋅ ∑ a x = 1 n d ( ∏ j = 1 x ( a j ) k ) \sum\limits_{d=1}^{n}\sum\limits_{g|d}f(g)·g·μ(\frac{d}{g})·(d)^{kx}\sum\limits_{a_{1}=1}^{\frac{n}{d}}\sum\limits_{a_{2}=1}^{\frac{n}{d}}···\sum\limits_{a_{x}=1}^{\frac{n}{d}}(\prod\limits_{j=1}^{x}(a_{j})^{k}) d=1∑ng∣d∑f(g)⋅g⋅μ(gd)⋅(d)kxa1=1∑dna2=1∑dn⋅⋅⋅ax=1∑dn(j=1∏x(aj)k)
我们🐍 F ( n ) = ∑ g ∣ n f ( g ) ⋅ g ⋅ μ ( n g ) ⋅ ( n ) k x F(n)=\sum\limits_{g|n}f(g)·g·μ(\frac{n}{g})·(n)^{kx} F(n)=g∣n∑f(g)⋅g⋅μ(gn)⋅(n)kx那么式子就变成了
∑ d = 1 n F ( d ) ∑ a 1 = 1 n d ∑ a 2 = 1 n d ⋅ ⋅ ⋅ ∑ a x = 1 n d ( ∏ j = 1 x ( a j ) k ) \sum\limits_{d=1}^{n}F(d)\sum\limits_{a_{1}=1}^{\frac{n}{d}}\sum\limits_{a_{2}=1}^{\frac{n}{d}}···\sum\limits_{a_{x}=1}^{\frac{n}{d}}(\prod\limits_{j=1}^{x}(a_{j})^{k}) d=1∑nF(d)a1=1∑dna2=1∑dn⋅⋅⋅ax=1∑dn(j=1∏x(aj)k)
然后再考虑后半部分,由结合律可以变成
(
∑
a
1
=
1
n
d
a
1
k
)
(
∑
a
2
=
1
n
d
a
2
k
)
⋅
⋅
⋅
(
∑
a
x
=
1
n
d
a
x
k
)
(\sum\limits_{a_{1}=1}^{\frac{n}{d}}a_{1}^{k})(\sum\limits_{a_{2}=1}^{\frac{n}{d}}a_{2}^{k})···(\sum\limits_{a_{x}=1}^{\frac{n}{d}}a_{x}^{k})
(a1=1∑dna1k)(a2=1∑dna2k)⋅⋅⋅(ax=1∑dnaxk)
我们🐍
t
=
n
d
,
S
k
x
(
t
)
=
(
1
k
+
2
k
+
3
k
+
4
k
+
⋅
⋅
⋅
+
t
k
)
x
t=\frac{n}{d},S_{k}^{x}(t)=(1^{k}+2^{k}+3^{k}+4^{k}+···+t^{k})^{x}
t=dn,Skx(t)=(1k+2k+3k+4k+⋅⋅⋅+tk)x,
S
S
S函数就是上面的后半部分,所以最后柿子变成了这样
∑ d = 1 n F ( d ) S k x ( n d ) \sum\limits_{d=1}^{n}F(d)S_{k}^{x}(\frac{n}{d}) d=1∑nF(d)Skx(dn),接下来只要预处理一下 F F F函数和 S S S函数,然后对 F F F函数的值求一个前缀和,利用 S S S函数的数论分块,就可以 O ( t ⋅ n ) O(t·\sqrt{n}) O(t⋅n)解出这题了
Code(577MS)
/****************************
* Author : 水娃 *
* Date : 2020-08-06-17.07.23*
****************************/
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
#define mst(a,b) memset(a,b,sizeof(a))
#define ll long long
#define debug(a) cout<<#a<<" is "<<a<<"\n"
#define ull unsigned long long
#define fi first
#define se second
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<int,ll>pil;
typedef pair<ll,ll>pll;
const ll mo=(ll)1e9+7;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll n,x,k;
const int MAXN=2e5+100;
int mu[MAXN];
int pri[MAXN>>1];int now;
bool vis[MAXN];
ll F[MAXN],D[MAXN],S[MAXN];
ll fpow(ll b,ll e)
{
ll ans=1;
while(e)
{
if(e&1)
{
ans=ans*b%mo;
}
b=b*b%mo;
e>>=1;
}
return ans;
}
void pre()
{
now=0;
mu[1]=1;
for(int i=2;i<MAXN;i++){
if(!vis[i]){
pri[++now]=i;
mu[i]=-1;
}
for(int j=1;j<=now&&i*pri[j]<MAXN;j++){
vis[i*pri[j]]=1;
if(i%pri[j])mu[i*pri[j]]=-mu[i];
else{
mu[i*pri[j]]=0;
break;
}
}
}
D[0]=1;
ll num;
for(ll g=1;g<=200000;g++)
{
D[g]=fpow(g,k*x);
for(ll t=1;(num=t*g)<=200000;t++)
{
F[num]=(F[num]+mu[g]*mu[g]*g*mu[t]+mo)%mo;
}
}
num=0;
for(ll t=1;t<=200000;t++)
{
num=(num+fpow(t,k))%mo;
S[t]=fpow(num,x);
F[t]=(F[t]*D[t]%mo+F[t-1])%mo;
}
}
void work()
{
cin>>n;
int l=1,r;
ll ans=0;
while(l<=n)
{
r=n/(n/l);
ans=(ans+(F[r]-F[l-1]+mo)*S[n/l]%mo)%mo;
l=r+1;
}
cout<<ans<<"\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;cin>>T>>k>>x;
pre();
while(T--)
work();
return 0;
}