# bzoj3202：[Sdoi2013]项链

#include<iostream>
#include<cstdio>
using namespace std;
#define maxn 10001005

int cases,a,tot,p=1000000007;
int prime[maxn],mu[maxn],phi[maxn];
bool bo[maxn];
long long n,mod;

long long mul(long long x,long long y){
long double t2=(long double)x*y/mod;
long long t1=x*y,t3=(long long)(t1-(long long)t2*mod)%mod;
return (t3+mod)%mod;
}

long long power(long long a,long long k){
if (k==0) return 1;
if (k==1) return a%mod;
long long x=power(a,k/2),ans=mul(x,x);
if (k&1) ans=mul(ans,a);
return ans;
}

long long fphi(long long x){
if (x<maxn) return phi[x];
long long ans=x;
for (int i=2;1ll*i*i<=x;i++)
if (x%i==0){
ans=ans-ans/i;
while (x%i==0) x/=i;
}
if (x!=1) ans=ans-ans/x;
return ans;
}

long long solve(long long m,long long n){
return ((power(m-1,n)+(n&1?1ll-m:m-1ll))%mod+mod)%mod;
}

void ex_gcd(long long a,long long b,long long &x,long long &y){
if (!b){x=1,y=0;return;}else ex_gcd(b,a%b,x,y);
long long x1=x,y1=y;
x=y1,y=x1-a/b*y1;
}

long long inv(long long a){
long long x=0,y=0;ex_gcd(a,p,x,y);return (x%p+p)%p;
}

int main(){
scanf("%d",&cases);mu[1]=1,phi[1]=1;
for (int i=2;i<maxn;i++){
if (!bo[i]) prime[++tot]=i,mu[i]=-1,phi[i]=i-1;
for (int j=1;j<=tot && i*prime[j]<maxn;j++){
bo[i*prime[j]]=1;
if (i%prime[j]==0){
mu[i*prime[j]]=0;
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i],phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for (int i=2;i<maxn;i++) mu[i]+=mu[i-1];
while (cases--){
scanf("%lld%d",&n,&a);long long ans2=0,ans3=0;
if (n%p==0) mod=1ll*p*p;else mod=p;
for (int i=1,j;i<=a;i=j+1){
j=a/(a/i);
ans2=(ans2+mul((mu[j]-mu[i-1]),(1ll*(a/i)*(a/i)%mod)))%mod;
ans3=(ans3+mul((mu[j]-mu[i-1]),mul(1ll*(a/i)*(a/i)%mod,(a/i)%mod)))%mod;
}
long long m=mul((ans3+3*ans2%mod+2)%mod,power(6,1ll*p*(p-1)-1)),ans=0;
for (int i=1;1ll*i*i<=n;i++)
if (n%i==0){
ans=(ans+mul(solve(m,i),fphi(n/i)))%mod;
if (1ll*i*i!=n) ans=(ans+mul(solve(m,n/i),fphi(i)))%mod;
}
if (n%p!=0) ans=mul(ans,power(n,mod-2))%p;
else ans=ans/p,n=n/p,ans=mul(ans,inv(n))%p;
printf("%lld\n",ans);
}
return 0;
}


UPD:一年以后来复习的时候发现后面这个也会证了。。。。。

posted @ 2016-10-13 20:10  DUXT  阅读(383)  评论(0编辑  收藏