HDU 6706

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=1nj=1igcd(iaja,ibjb)[gcd(i,j)=1]%(mod),mod=1e9+7,abab
引理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}) xnyn=(xy)(xn1+xn2y++yn1)
引理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(iaja,ibjb)=igcd(a,b)jgcd(a,b)
证明:记 a = k ∗ b + r , a=k*b+r, a=kb+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} iaja=ikb+rikbjr+ikbjrjkb+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}) =ikb(irjr)+jr(ikbjkb)
= 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(ibjb)(ib(k1)+ib(k2)j++jb(k1))+ikb(irjr)
g c d ( i a − j a , i b − j b ) gcd(i^a-j^a,i^b-j^b) gcd(iaja,ibjb)(引理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(ikb(irjr),ibjb)
因为 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)}) ikbjkb=(ibjb)(ib(k1)+ib(k2)j++jb(k1))
所以记 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,ibjb)=gcd(jkb,ibjb)
d ∣ i k b , d ∣ j k b , d ∣ i b − j b d|i^{kb},d|j^{kb},d|i^b-j^b dikb,djkb,dibjb,因为 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(iaja,ibjb)=gcd(ikb(irjr),ibjb)
= 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(irjr,ibjb)=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=1nj=1ij[gcd(i,j)=1]=i=1n(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(xa,x),ax
a a a x x x互素,则 x − a x-a xa 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=1nj=1ij[gcd(i,j)=1]=2i=1nj=1ii[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=2n(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=1nj=1igcd(iaja,ibjb)[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=1nj=1i(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=1nj=1i(ij)[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=1nj=1ii[gcd(i,j)=1]i=1nj=1ij[gcd(i,j)=1](引理3)
= ∑ i = 2 n ( i φ ( i ) 2 ) =\sum\limits_{i=2}^{n}(\frac{i\varphi(i)}{2}) =i=2n(2iφ(i))
= ∑ i = 1 n i φ ( i ) − 1 2 =\frac{\sum\limits_{i=1}^ni\varphi(i)-1}{2} =2i=1niφ(i)1
现在问题转换为了如何求解 ∑ i = 1 n i φ ( i ) \sum\limits_{i=1}^ni\varphi(i) i=1niφ(i), n ≤ 1 e 9 n\leq 1e9 n1e9
构造杜教筛:
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=fg=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=1nh(i)=i=1ndif(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=1ndidφ(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=1nidiφ(d)=i=1ni2
利用杜教筛公式: 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=1nh(i)d=2ng(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;
}

你可能感兴趣的:(ACM,数论)