2019CCPC网络赛1005
问题求解:
∑ i = 1 n ∑ j = 1 i g c d ( i a − j a , i b − j b ) [ g c d ( i , j ) = 1 ] % ( m o d ) , m o d = 1 e 9 + 7 , a 和 b 互 素 , 且 a ≥ b \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}gcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1]\%(mod),mod=1e9+7,a和b互素,且a\geq b i=1∑nj=1∑igcd(ia−ja,ib−jb)[gcd(i,j)=1]%(mod),mod=1e9+7,a和b互素,且a≥b
引理1: x n − y n = ( x − y ) ( x n − 1 + x n − 2 y + … + y n − 1 ) x^n-y^n=(x-y)(x^{n-1}+x^{n-2}y+…+y^{n-1}) xn−yn=(x−y)(xn−1+xn−2y+…+yn−1)
引理2:当 i , j i,j i,j互素时, g c d ( i a − j a , i b − j b ) = i g c d ( a , b ) − j g c d ( a , b ) gcd(i^a-j^a,i^b-j^b)=i^{gcd(a,b)}-j^{gcd(a,b)} gcd(ia−ja,ib−jb)=igcd(a,b)−jgcd(a,b)
证明:记 a = k ∗ b + r , a=k*b+r, a=k∗b+r,
i a − j a = i k ∗ b + r − i k ∗ b ∗ j r + i k ∗ b ∗ j r − j k ∗ b + r i^a-j^a=i^{k*b+r}-i^{k*b}*j^r+i^{k*b}*j^r-j^{k*b+r} ia−ja=ik∗b+r−ik∗b∗jr+ik∗b∗jr−jk∗b+r
= i k ∗ b ∗ ( i r − j r ) + j r ∗ ( i k ∗ b − j k ∗ b ) =i^{k*b}*(i^r-j^r)+j^r*(i^{k*b}-j^{k*b}) =ik∗b∗(ir−jr)+jr∗(ik∗b−jk∗b)
= j r ( i b − j b ) ( i b ∗ ( k − 1 ) + i b ∗ ( k − 2 ) j + … + j b ∗ ( k − 1 ) ) + i k ∗ b ( i r − j r ) =j^r(i^b-j^b)(i^{b*(k-1)}+i^{b*(k-2)}j+…+j^{b*(k-1)})+i^{k*b}(i^r-j^r) =jr(ib−jb)(ib∗(k−1)+ib∗(k−2)j+…+jb∗(k−1))+ik∗b(ir−jr)
g c d ( i a − j a , i b − j b ) gcd(i^a-j^a,i^b-j^b) gcd(ia−ja,ib−jb)(引理1)
= g c d ( i k ∗ b ( i r − j r ) , i b − j b ) =gcd(i^{k*b}(i^r-j^r),i^b-j^b) =gcd(ik∗b(ir−jr),ib−jb)
因为 i k b − j k b = ( i b − j b ) ( i b ∗ ( k − 1 ) + i b ∗ ( k − 2 ) j + … + j b ∗ ( k − 1 ) ) i^{kb}-j^{kb}=(i^b-j^b)(i^{b*(k-1)}+i^{b*(k-2)}j+…+j^{b*(k-1)}) ikb−jkb=(ib−jb)(ib∗(k−1)+ib∗(k−2)j+…+jb∗(k−1))
所以记 d = g c d ( i k b , i b − j b ) = g c d ( j k b , i b − j b ) d=gcd(i^{kb},i^b-j^b)=gcd(j^{kb},i^b-j^b) d=gcd(ikb,ib−jb)=gcd(jkb,ib−jb)
则 d ∣ i k b , d ∣ j k b , d ∣ i b − j b d|i^{kb},d|j^{kb},d|i^b-j^b d∣ikb,d∣jkb,d∣ib−jb,因为 i , j i,j i,j互素,所以 d = 1 d=1 d=1
g c d ( i a − j a , i b − j b ) = g c d ( i k ∗ b ( i r − j r ) , i b − j b ) gcd(i^a-j^a,i^b-j^b)=gcd(i^{k*b}(i^r-j^r),i^b-j^b) gcd(ia−ja,ib−jb)=gcd(ik∗b(ir−jr),ib−jb)
= g c d ( i r − j r , i b − j b ) = i g c d ( a , b ) − j g c d ( a , b ) =gcd(i^r-j^r,i^b-j^b)=i^{gcd(a,b)}-j^{gcd(a,b)} =gcd(ir−jr,ib−jb)=igcd(a,b)−jgcd(a,b)(类似辗转相除法)
证毕。
引理3: ∑ i = 1 n ∑ j = 1 i j [ g c d ( i , j ) = 1 ] = ∑ i = 1 n ( i φ ( i ) 2 + [ i = 1 ] ) \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}j[gcd(i,j)=1]=\sum\limits_{i=1}^n(\frac{i\varphi(i)}{2}+[i=1]) i=1∑nj=1∑ij[gcd(i,j)=1]=i=1∑n(2iφ(i)+[i=1])
证明: g c d ( a , x ) = g c d ( x − a , x ) , a ≤ x gcd(a,x)=gcd(x-a,x),a\leq x gcd(a,x)=gcd(x−a,x),a≤x
若 a a a和 x x x互素,则 x − a x-a x−a和 x x x也互素,所以
∑ i = 1 n ∑ j = 1 i j [ g c d ( i , j ) = 1 ] = ∑ i = 1 n ∑ j = 1 i i [ g c d ( i , j ) = 1 ] 2 + [ i = 1 ] \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}j[gcd(i,j)=1]=\frac{\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}i[gcd(i,j)=1]}{2}+[i=1] i=1∑nj=1∑ij[gcd(i,j)=1]=2i=1∑nj=1∑ii[gcd(i,j)=1]+[i=1]
= ∑ i = 2 n ( i φ ( i ) 2 ) + 1 =\sum\limits_{i=2}^{n}(\frac{i\varphi(i)}{2})+1 =i=2∑n(2iφ(i))+1
证毕。
∑ i = 1 n ∑ j = 1 i g c d ( i a − j a , i b − j b ) [ g c d ( i , j ) = 1 ] \sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}gcd(i^a-j^a,i^b-j^b)[gcd(i,j)=1] i=1∑nj=1∑igcd(ia−ja,ib−jb)[gcd(i,j)=1]
= ∑ i = 1 n ∑ j = 1 i ( i g c d ( a , b ) − j g c d ( a , b ) ) [ g c d ( i , j ) = 1 ] =\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}(i^{gcd(a,b)}-j^{gcd(a,b)})[gcd(i,j)=1] =i=1∑nj=1∑i(igcd(a,b)−jgcd(a,b))[gcd(i,j)=1](引理2)
= ∑ i = 1 n ∑ j = 1 i ( i − j ) [ g c d ( i , j ) = 1 ] =\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}(i-j)[gcd(i,j)=1] =i=1∑nj=1∑i(i−j)[gcd(i,j)=1],( a , b a,b a,b互素)
= ∑ i = 1 n ∑ j = 1 i i [ g c d ( i , j ) = 1 ] − ∑ i = 1 n ∑ j = 1 i j [ g c d ( i , j ) = 1 ] =\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}i[gcd(i,j)=1]-\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{i}j[gcd(i,j)=1] =i=1∑nj=1∑ii[gcd(i,j)=1]−i=1∑nj=1∑ij[gcd(i,j)=1](引理3)
= ∑ i = 2 n ( i φ ( i ) 2 ) =\sum\limits_{i=2}^{n}(\frac{i\varphi(i)}{2}) =i=2∑n(2iφ(i))
= ∑ i = 1 n i φ ( i ) − 1 2 =\frac{\sum\limits_{i=1}^ni\varphi(i)-1}{2} =2i=1∑niφ(i)−1
现在问题转换为了如何求解 ∑ i = 1 n i φ ( i ) \sum\limits_{i=1}^ni\varphi(i) i=1∑niφ(i), n ≤ 1 e 9 n\leq 1e9 n≤1e9
构造杜教筛:
f = i d ⋅ φ , g = i d , h = f ∗ g = i 2 f=id\cdot\varphi,g=id,h=f*g=i^2 f=id⋅φ,g=id,h=f∗g=i2
∑ i = 1 n h ( i ) = ∑ i = 1 n ∑ d ∣ i f ( d ) g ( i d ) \sum\limits_{i=1}^{n}h(i)=\sum\limits_{i=1}^{n}\sum\limits_{d|i}f(d)g(\frac{i}{d}) i=1∑nh(i)=i=1∑nd∣i∑f(d)g(di)
= ∑ i = 1 n ∑ d ∣ i d φ ( d ) ⋅ i d =\sum\limits_{i=1}^{n}\sum\limits_{d|i}d\varphi(d)\cdot\frac{i}{d} =i=1∑nd∣i∑dφ(d)⋅di
= ∑ i = 1 n i ∑ d ∣ i φ ( d ) = ∑ i = 1 n i 2 =\sum\limits_{i=1}^{n}i\sum\limits_{d|i}\varphi(d)=\sum\limits_{i=1}^{n}i^2 =i=1∑nid∣i∑φ(d)=i=1∑ni2
利用杜教筛公式: g ( 1 ) S ( n ) = ∑ i = 1 n h ( i ) − ∑ d = 2 n g ( d ) S ( ⌊ n d ⌋ ) g(1)S(n)=\sum\limits_{i=1}^{n}h(i)-\sum\limits_{d=2}^{n}g(d)S(\lfloor\frac{n}{d}\rfloor) g(1)S(n)=i=1∑nh(i)−d=2∑ng(d)S(⌊dn⌋)
后面就是杜教筛了。
AC代码:
#include
#define ll long long
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e6+10;
ll p[maxn];
ll sum[maxn];
ll inv2,inv6;
map<int,ll>mp;
ll qpow(ll a,ll b){
ll res=1;
while(b!=0){
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
}
ll f_g_sum(int n){
return (ll)n%mod*(n+1)%mod*(2*n+1)%mod*inv6%mod;
}
ll g_sum(int n){
return (ll)n%mod*(n+1)%mod*inv2%mod;
}
ll GetSum(int n){
if(n<=1000000)return sum[n];
if(mp[n]!=0)return mp[n];
ll ans = f_g_sum(n)%mod;
for(ll l = 2, r; l <= n; l = r + 1) {
r = (n / (n / l));
ans = (ans - (g_sum(r) - g_sum(l - 1)) * GetSum(n / l) + mod)%mod;
}
mp[n]=ans;
return ans;
}
void phi_table(int n, ll* phi) {
for (int i = 2; i <= n; i++) phi[i] = 0;
phi[1] = 1;
for (int i = 2; i <= n; i++)
if (!phi[i])
for (int j = i; j <= n; j += i) {
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
int main(){
int T,n,a,b;
scanf("%d",&T);
inv2=qpow(2,mod-2);
inv6=qpow(6,mod-2);
phi_table(1000000,p);
sum[0]=0;
for(int i=1;i<=1000000;i++){
sum[i]=(sum[i-1]+i*p[i])%mod;
}
while(T--){
scanf("%d%d%d",&n,&a,&b);
ll ans;
ans=(GetSum(n)-1+mod)%mod*inv2%mod;
printf("%lld\n",ans);
}
return 0;
}