∑ i = a b ∑ j = c d [ g c d ( i , j ) = k ] \sum_{i=a}^b\sum_{j=c}^d[{\rm gcd}(i,j)=k] i=a∑bj=c∑d[gcd(i,j)=k]
F ( n , m ) = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = k ] \color{#f00}{F(n,m)=\sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)=k]} F(n,m)=i=1∑nj=1∑m[gcd(i,j)=k]
a n s = F ( a , b ) − F ( a − 1 , d ) − F ( b , c − 1 ) + F ( a − 1 , c − 1 ) \color{#f00}ans=F(a,b)-F(a-1,d)-F(b,c-1)+F(a-1,c-1) ans=F(a,b)−F(a−1,d)−F(b,c−1)+F(a−1,c−1)
F ( n , m ) = F(n,m)= F(n,m)=
∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ n k ⌋ [ g c d ( i , j ) = 1 ] \sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{k}\right\rfloor}[{\rm gcd}(i,j)=1] i=1∑⌊kn⌋j=1∑⌊kn⌋[gcd(i,j)=1]
∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ n k ⌋ ϵ ( g c d ( i , j ) = 1 ) \sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{k}\right\rfloor}ϵ({\rm gcd}(i,j)=1) i=1∑⌊kn⌋j=1∑⌊kn⌋ϵ(gcd(i,j)=1)
∑ d = 1 ⌊ m i n ( n , m ) k ⌋ ∑ d ∣ T μ ( d ) ⌊ n T ⌋ ⌊ m T ⌋ \sum_{d=1}^{\left\lfloor\frac{{\rm min}(n,m)}{k}\right\rfloor}\sum_{d|T}\mu(d){\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor} d=1∑⌊kmin(n,m)⌋d∣T∑μ(d)⌊Tn⌋⌊Tm⌋
∑ d = 1 ⌊ m i n ( n , m ) k ⌋ μ ( d ) ⌊ n k d ⌋ ⌊ m k d ⌋ \color{#f00}\sum_{d=1}^{\left\lfloor\frac{{\rm min}(n,m)}{k}\right\rfloor}\mu(d){\left\lfloor\frac{n}{kd}\right\rfloor}{\left\lfloor\frac{m}{kd}\right\rfloor} d=1∑⌊kmin(n,m)⌋μ(d)⌊kdn⌋⌊kdm⌋
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
const int N=50010;
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool vis[N];
int prim[N],mu[N],sum[N],k;
void get_mu(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+mu[i];
}
long long Ans(int a,int b){
a/=k;b/=k;
int max_rep=min(a,b);
long long ans=0;
for(int l=1,r;l<=max_rep;l=r+1){
r=min(a/(a/l),b/(b/l));
ans+=(long long)(a/l)*(long long)(b/l)*(long long)(sum[r]-sum[l-1]);
}
return ans;
}
int main()
{
int T=read();
get_mu(N-10);
while(T--){
int a=read(),b=read(),c=read(),d=read();k=read();
printf("%lld\n",Ans(b,d)-Ans(b,c-1)-Ans(a-1,d)+Ans(a-1,c-1));
}
return 0;
}
∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) i s p r i m e ] \sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)\rm\ \ is\ \ prime] i=1∑nj=1∑m[gcd(i,j) is prime]
∑ p = 1 , [ p i s p r i m e ] ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = p ] \sum_{p=1,[p\rm\ \ is\ \ prime]}\sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)=p] p=1,[p is prime]∑i=1∑nj=1∑m[gcd(i,j)=p]
∑ p = 1 , [ p i s p r i m e ] ∑ i = 1 ⌊ n p ⌋ ∑ j = 1 ⌊ m p ⌋ [ g c d ( i , j ) = 1 ] \sum_{p=1,[p\rm\ \ is\ \ prime]}\sum_{i=1}^{\left\lfloor\frac{n}{p}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{p}\right\rfloor}[{\rm gcd}(i,j)=1] p=1,[p is prime]∑i=1∑⌊pn⌋j=1∑⌊pm⌋[gcd(i,j)=1]
∑ p = 1 , [ p i s p r i m e ] ∑ d = 1 ⌊ m i n ( n , m ) p ⌋ μ ( d ) ⌊ n p d ⌋ ⌊ m p d ⌋ \sum_{p=1,[p\rm\ \ is\ \ prime]}\sum_{d=1}^{\left\lfloor\frac{{\rm min}(n,m)}{p}\right\rfloor}\mu(d){\left\lfloor\frac{n}{pd}\right\rfloor}{\left\lfloor\frac{m}{pd}\right\rfloor} p=1,[p is prime]∑d=1∑⌊pmin(n,m)⌋μ(d)⌊pdn⌋⌊pdm⌋
∑ T = 1 m i n ( n , m ) ∑ d ∣ T μ ( d ) ⌊ n T ⌋ ⌊ m T ⌋ [ T d i s p r i m e ] \sum_{T=1}^{{\rm min}(n,m)}\sum_{d|T}\mu(d){\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}\left[\frac{T}{d}\rm\ \ is\ \ prime\right] T=1∑min(n,m)d∣T∑μ(d)⌊Tn⌋⌊Tm⌋[dT is prime]
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ ∑ d ∣ T , [ d i s p r i m e ] μ ( T d ) \sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}\sum_{d|T,\left[d\rm\ \ is\ \ prime\right]}\mu\left(\frac{T}{d}\right) T=1∑min(n,m)⌊Tn⌋⌊Tm⌋d∣T,[d is prime]∑μ(dT)
F ( x ) = ∑ d ∣ x , [ d i s p r i m e ] μ ( x d ) \color{#f00}{F(x)=\sum_{d|x,\left[d\rm\ \ is\ \ prime\right]}\mu\left(\frac{x}{d}\right)} F(x)=d∣x,[d is prime]∑μ(dx)
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ F ( T ) \color{#f00}{\sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}F(T)} T=1∑min(n,m)⌊Tn⌋⌊Tm⌋F(T)
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
const int N=1e7+10;
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool vis[N];
int prim[N],mu[N];
long long F[N];
void get_mu(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=prim[0];i++)
for(int j=1;j*prim[i]<=n;j++)F[j*prim[i]]+=(long long)mu[j];
for(int i=1;i<=n;i++)F[i]+=F[i-1];
}
long long Ans(int n,int m){
int lim=min(n,m);
long long ans=0;
for(int l=1,r;l<=lim;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(long long)(n/l)*(long long)(m/l)*(F[r]-F[l-1]);
}
return ans;
}
int main()
{
get_mu(N-10);
int T=read();
while(T--){
int n=read(),m=read();
printf("%lld\n",Ans(n,m));
}
return 0;
}
∑ i = 1 n ∑ j = 1 m ∑ k ∣ g c d ( i , j ) k [ ∑ k ∣ g c d ( i , j ) k ≤ a ] \sum_{i=1}^n\sum_{j=1}^m\sum_{k|{\rm gcd}(i,j)}k\left[\sum_{k|{\rm gcd}(i,j)}k≤a\right] i=1∑nj=1∑mk∣gcd(i,j)∑k⎣⎡k∣gcd(i,j)∑k≤a⎦⎤
F ( x ) = ∑ i ∣ x i \color{#f00}F(x)=\sum_{i|x}i F(x)=i∣x∑i
∑ i = 1 n ∑ j = 1 m F ( g c d ( i , j ) ) [ F ( g c d ( i , j ) ) ≤ a ] \sum_{i=1}^n\sum_{j=1}^mF({\rm gcd}(i,j))[F({\rm gcd}(i,j))≤a] i=1∑nj=1∑mF(gcd(i,j))[F(gcd(i,j))≤a]
∑ d = 1 m i n ( n , m ) ∑ i = 1 n ∑ j = 1 m F ( d ) [ g c d ( i , j ) = d ] [ F ( d ) ≤ a ] \sum_{d=1}^{{\rm min}(n,m)}\sum_{i=1}^n\sum_{j=1}^mF(d)[{\rm gcd}(i,j)=d][F(d)≤a] d=1∑min(n,m)i=1∑nj=1∑mF(d)[gcd(i,j)=d][F(d)≤a]
∑ d = 1 m i n ( n , m ) F ( d ) [ F ( d ) ≤ a ] ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}F(d)[F(d)≤a]\sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)=d] d=1∑min(n,m)F(d)[F(d)≤a]i=1∑nj=1∑m[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) F ( d ) [ F ( d ) ≤ a ] ∑ d ′ = 1 ⌊ m i n ( n , m ) d ⌋ μ ( d ′ ) ⌊ n d ′ d ⌋ ⌊ m d ′ d ⌋ \sum_{d=1}^{{\rm min}(n,m)}F(d)[F(d)≤a]\sum_{d'=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(d')\left\lfloor\frac{n}{d'd}\right\rfloor\left\lfloor\frac{m}{d'd}\right\rfloor d=1∑min(n,m)F(d)[F(d)≤a]d′=1∑⌊dmin(n,m)⌋μ(d′)⌊d′dn⌋⌊d′dm⌋
∑ T = 1 m i n ( n , m ) ∑ d ∣ T F ( d ) μ ( T d ) ⌊ n T ⌋ ⌊ m T ⌋ [ F ( d ) ≤ a ] \sum_{T=1}^{{\rm min}(n,m)}\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right){\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}[F(d)≤a] T=1∑min(n,m)d∣T∑F(d)μ(dT)⌊Tn⌋⌊Tm⌋[F(d)≤a]
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ ∑ d ∣ T F ( d ) μ ( T d ) [ F ( d ) ≤ a ] \sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right)[F(d)≤a] T=1∑min(n,m)⌊Tn⌋⌊Tm⌋d∣T∑F(d)μ(dT)[F(d)≤a]
f ( x ) = ∑ d ∣ T F ( d ) μ ( T d ) [ F ( d ) ≤ a ] \color{#f00}f(x)=\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right)[F(d)≤a] f(x)=d∣T∑F(d)μ(dT)[F(d)≤a]
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ f ( T ) \color{#f00}\sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}f(T) T=1∑min(n,m)⌊Tn⌋⌊Tm⌋f(T)
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
//char buf[1<<15],*S=buf,*T=buf;
//char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
if(f)return x;else return -x;
}
const int N=1e5+10;
const int Q=2e4+10;
int sum[N];
bool vis[N];
int prim[N],mu[N];
int S=1;
struct Sum{int data,id;}F[N];
bool operator <(Sum a,Sum b){return a.data<b.data;}
int ans[Q];
#define lowbit(x) ((x)&(-x))
void add(int i,int c)
{for(;i<=N-5;i+=lowbit(i))sum[i]+=c;}
int query(int i){
int ans=0;
for(;i;i-=lowbit(i))ans+=sum[i];
return ans;
}
void Add(int i,int data){
int ed=((N-5)/i);
for(register int j=1;j<=ed;++j)
add(i*j,data*mu[j]);
}
void init(){
mu[1]=1;
for(register int i=2;i<=N-5;++i){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(register int j=1;j<=prim[0]&&i*prim[j]<=N-5;++j){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(register int i=1;i<=N-5;++i){
F[i].id=i;
int ed=((N-5)/i);
for(register int j=1;j<=ed;++j)
F[i*j].data+=i;
}
sort(F+1,F+1+N-5);
}
struct Query{int n,m,a,id;}q[Q];
inline bool operator <(const Query &a,const Query &b){return a.a<b.a;}
int main()
{
int T=read();
init();
for(register int i=1;i<=T;i++){
q[i].n=read();q[i].m=read();q[i].a=read();q[i].id=i;
if(q[i].n>q[i].m)swap(q[i].n,q[i].m);
}
sort(q+1,q+T+1);
for(register int i=1;i<=T;i++){
int n=q[i].n,m=q[i].m,a=q[i].a,id=q[i].id;
while(F[S].data<=a){Add(F[S].id,F[S].data);S++;}
for(register int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),(m/(m/l)));
ans[id]+=(n/l)*(m/l)*(query(r)-query(l-1));
}
}
for(register int i=1;i<=T;i++)printf("%d\n",ans[i]&0x7fffffff);
return 0;
}
∏ i = 1 n ∏ j = 1 m f ( g c d ( i , j ) ) \prod_{i=1}^{n}\prod_{j=1}^{m}f\left({\rm gcd}(i,j)\right) i=1∏nj=1∏mf(gcd(i,j))
  \ \ \ \ \ \, f f f为斐波拉契序列。
∏ i = 1 m i n ( n , m ) ∏ i = 1 n ∏ j = 1 m f ( d ) [ g c d ( i , j ) = d ] \prod_{i=1}^{{\rm min}(n,m)}\prod_{i=1}^{n}\prod_{j=1}^{m}f(d)\left[{\rm gcd}(i,j)=d\right] i=1∏min(n,m)i=1∏nj=1∏mf(d)[gcd(i,j)=d]
∏ d = 1 m i n ( n , m ) f ( d ) ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] \prod_{d=1}^{{\rm min}(n,m)}f(d)^{\sum_{i=1}^n\sum_{j=1}^{m}\left[{\rm gcd}(i,j)=d\right]} d=1∏min(n,m)f(d)∑i=1n∑j=1m[gcd(i,j)=d]
∏ d = 1 m i n ( n , m ) f ( d ) ∑ d ′ = 1 ⌊ m i n ( n , m ) d ⌋ μ ( d ′ ) ⌊ n d ′ d ⌋ ⌊ m d ′ d ⌋ \prod_{d=1}^{{\rm min}(n,m)}f(d)^{\sum_{d'=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(d')\left\lfloor\frac{n}{d'd}\right\rfloor\left\lfloor\frac{m}{d'd}\right\rfloor} d=1∏min(n,m)f(d)∑d′=1⌊dmin(n,m)⌋μ(d′)⌊d′dn⌋⌊d′dm⌋
∏ d = 1 m i n ( n , m ) ∏ d ′ = 1 ⌊ m i n ( n , m ) d ⌋ f ( d ) μ ( d ′ ) ⌊ n d ′ d ⌋ ⌊ m d ′ d ⌋ \prod_{d=1}^{{\rm min}(n,m)}\prod_{d'=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}f(d)^{\mu(d')\left\lfloor\frac{n}{d'd}\right\rfloor\left\lfloor\frac{m}{d'd}\right\rfloor} d=1∏min(n,m)d′=1∏⌊dmin(n,m)⌋f(d)μ(d′)⌊d′dn⌋⌊d′dm⌋
∏ T = 1 m i n ( n , m ) ∏ d ∣ T f ( d ) μ ( T d ) ⌊ n T ⌋ ⌊ m T ⌋ \prod_{T=1}^{{\rm min}(n,m)}\prod_{d|T}f(d)^{\mu\left(\frac{T}{d}\right)\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor} T=1∏min(n,m)d∣T∏f(d)μ(dT)⌊Tn⌋⌊Tm⌋
C ( x ) = ∏ d ∣ T f ( d ) μ ( T d ) \color{#f00}{C(x)=\prod_{d|T}f(d)^{\mu\left(\frac{T}{d}\right)}} C(x)=d∣T∏f(d)μ(dT)
∏ T = 1 m i n ( n , m ) C ( T ) ⌊ n T ⌋ ⌊ m T ⌋ \color{#f00}\prod_{T=1}^{{\rm min}(n,m)}C(T)^{\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor} T=1∏min(n,m)C(T)⌊Tn⌋⌊Tm⌋
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
const int N=1e6+10;
const long long mod=1e9+7;
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
long long power(long long a,long long b){
long long ans=1ll;
a=a%mod;
while(b!=0){
if(b&1) ans=(ans*a)%mod;
b>>=1;a=(a*a)%mod;
}
return ans;
}
long long f[N];
bool vis[N];
int prim[N],mu[N],sum[N];
long long C[N],g[N];
void get_mu(int n){
mu[1]=1;
g[1]=C[0]=C[1]=1ll;
for(int i=2;i<=n;i++){
g[i]=power(f[i],mod-2);C[i]=1;
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=n;++i){
if(!mu[i])continue;
for(int j=i;j<=n;j+=i)
C[j]=1ll*C[j]*(mu[i]==1?f[j/i]:g[j/i])%mod;
}
for(int i=2;i<=n;++i)C[i]=1ll*C[i]*C[i-1]%mod;
}
int n,m;
int main()
{
f[1]=f[2]=1ll;
for(int i=3;i<=N-10;i++)f[i]=f[i-1]+f[i-2],f[i]%=mod;
get_mu(N-10);
int T=read();
while(T--){
n=read();m=read();
if(n>m)swap(n,m);
long long ans=1ll;
for(int i=1,lim=0;i<=n;i=lim+1){
lim=min(n/(n/i),m/(m/i));
ans=1ll*ans*power(C[lim]*power(C[i-1],mod-2),1ll*(n/i)*(m/i)%(mod-1));
ans%=mod;
}
printf("%lld\n",ans);
}
return 0;
}
∑ i = 1 n ∑ j = 1 m ∑ k ∣ g c d ( i , j ) 1 \sum_{i=1}^n\sum_{j=1}^m\sum_{k|{\rm gcd}(i,j)}1 i=1∑nj=1∑mk∣gcd(i,j)∑1
F ( x ) = ∑ i ∣ x 1 \color{#f00}F(x)=\sum_{i|x}1 F(x)=i∣x∑1
∑ i = 1 n ∑ j = 1 m F ( g c d ( i , j ) ) \sum_{i=1}^n\sum_{j=1}^mF({\rm gcd}(i,j)) i=1∑nj=1∑mF(gcd(i,j))
∑ d = 1 m i n ( n , m ) ∑ i = 1 n ∑ j = 1 m F ( d ) [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}\sum_{i=1}^n\sum_{j=1}^mF(d)[{\rm gcd}(i,j)=d] d=1∑min(n,m)i=1∑nj=1∑mF(d)[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) F ( d ) ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}F(d)\sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)=d] d=1∑min(n,m)F(d)i=1∑nj=1∑m[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) F ( d ) ∑ d ′ = 1 ⌊ m i n ( n , m ) d ⌋ μ ( d ′ ) ⌊ n d ′ d ⌋ ⌊ m d ′ d ⌋ \sum_{d=1}^{{\rm min}(n,m)}F(d)\sum_{d'=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(d')\left\lfloor\frac{n}{d'd}\right\rfloor\left\lfloor\frac{m}{d'd}\right\rfloor d=1∑min(n,m)F(d)d′=1∑⌊dmin(n,m)⌋μ(d′)⌊d′dn⌋⌊d′dm⌋
∑ T = 1 m i n ( n , m ) ∑ d ∣ T F ( d ) μ ( T d ) ⌊ n T ⌋ ⌊ m T ⌋ \sum_{T=1}^{{\rm min}(n,m)}\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right){\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor} T=1∑min(n,m)d∣T∑F(d)μ(dT)⌊Tn⌋⌊Tm⌋
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ ∑ d ∣ T F ( d ) μ ( T d ) \sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right) T=1∑min(n,m)⌊Tn⌋⌊Tm⌋d∣T∑F(d)μ(dT)
f ( x ) = ∑ d ∣ T F ( d ) μ ( T d ) \color{#f00}f(x)=\sum_{d|T}F(d)\mu\left(\frac{T}{d}\right) f(x)=d∣T∑F(d)μ(dT)
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⌊ m T ⌋ f ( T ) \color{#f00}\sum_{T=1}^{{\rm min}(n,m)}{\left\lfloor\frac{n}{T}\right\rfloor}{\left\lfloor\frac{m}{T}\right\rfloor}f(T) T=1∑min(n,m)⌊Tn⌋⌊Tm⌋f(T)
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
const int N=50010;
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool vis[N];
int prim[N],mu[N],sum[N];
long long g[N];
void get_mu(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
for(int i=1;i<=n;i++){
long long ans=0;
for(int l=1,r;l<=i;l=r+1){
r=(i/(i/l));
ans+=1ll*(r-l+1)*1ll*(i/l);
}
g[i]=ans;
}
}
long long Ans(int n,int m){
int max_rep=min(n,m);
long long ans=0;
for(int l=1,r;l<=max_rep;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(sum[r]-sum[l-1])*1ll*g[n/l]*1ll*g[m/l];
}
return ans;
}
int main()
{
int T=read();
get_mu(N-10);
while(T--){
int a=read(),b=read();
printf("%lld\n",Ans(a,b));
}
return 0;
}
∑ i = 1 a ∑ j = 1 b [ g c d ( i , j ) = c ] \sum_{i=1}^a\sum_{j=1}^b[{\rm gcd}(i,j)=c] i=1∑aj=1∑b[gcd(i,j)=c]
∑ i = 1 a ∑ j = 1 b [ g c d ( i , j ) = 1 ] \sum_{i=1}^a\sum_{j=1}^b[{\rm gcd}(i,j)=1] i=1∑aj=1∑b[gcd(i,j)=1]
∑ i = 1 ⌊ a c ⌋ ∑ j = 1 ⌊ b c ⌋ ϵ ( g c d ( i , j ) ) \sum_{i=1}^{\left\lfloor\frac{a}{c}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{b}{c}\right\rfloor}ϵ({\rm gcd}(i,j)) i=1∑⌊ca⌋j=1∑⌊cb⌋ϵ(gcd(i,j))
∑ i = 1 ⌊ a c ⌋ ∑ j = 1 ⌊ b c ⌋ ( μ ∗ 1 ) ( g c d ( i , j ) ) \sum_{i=1}^{\left\lfloor\frac{a}{c}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{b}{c}\right\rfloor}(\mu*1)({\rm gcd}(i,j)) i=1∑⌊ca⌋j=1∑⌊cb⌋(μ∗1)(gcd(i,j))
∑ i = 1 ⌊ a c ⌋ ∑ j = 1 ⌊ b c ⌋ ∑ d ∣ g c d ( i , j ) μ ( d ) × 1 \sum_{i=1}^{\left\lfloor\frac{a}{c}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{b}{c}\right\rfloor}\sum_{d|{\rm gcd}(i,j)}\mu(d)\times 1 i=1∑⌊ca⌋j=1∑⌊cb⌋d∣gcd(i,j)∑μ(d)×1
∑ x = 1 ⌊ m i n ( a , b ) c ⌋ ∑ d ∣ x μ ( d ) ⌊ a x c ⌋ ⌊ b x c ⌋ \sum_{x=1}^{\left\lfloor\frac{{\rm min}(a,b)}{c}\right\rfloor}\sum_{d|x}\mu(d){\left\lfloor\frac{a}{xc}\right\rfloor}{\left\lfloor\frac{b}{xc}\right\rfloor} x=1∑⌊cmin(a,b)⌋d∣x∑μ(d)⌊xca⌋⌊xcb⌋
∑ d = 1 ⌊ m i n ( a , b ) c ⌋ μ ( d ) ⌊ a d c ⌋ ⌊ b d c ⌋ \color{#f00}\sum_{d=1}^{\left\lfloor\frac{{\rm min}(a,b)}{c}\right\rfloor}\mu(d){\left\lfloor\frac{a}{dc}\right\rfloor}{\left\lfloor\frac{b}{dc}\right\rfloor} d=1∑⌊cmin(a,b)⌋μ(d)⌊dca⌋⌊dcb⌋
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
const int N=50010;
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
bool vis[N];
int prim[N],mu[N],sum[N],k;
void get_mu(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+mu[i];
}
long long Ans(int a,int b){
int max_rep=min(a,b);
long long ans=0;
for(int l=1,r;l<=max_rep;l=r+1){
r=min(a/(a/l),b/(b/l));
ans+=(long long)(a/(l*k))*(long long)(b/(l*k))*(long long)(sum[r]-sum[l-1]);
}
return ans;
}
int T;
int main()
{
T=read();
get_mu(N-10);
while(T--){
int a=read(),b=read();k=read();
printf("%lld\n",Ans(a,b));
}
return 0;
}
∑ i = 1 n ∑ j = 1 m l c m ( i , j ) \sum_{i=1}^n\sum_{j=1}^m{\rm lcm}(i,j) i=1∑nj=1∑mlcm(i,j)
∑ i = 1 n ∑ j = 1 m i j g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^m\frac{ij}{{\rm gcd}(i,j)} i=1∑nj=1∑mgcd(i,j)ij
∑ d = 1 m i n ( n , m ) ∑ i = 1 n ∑ j = 1 m i j d [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}\sum_{i=1}^n\sum_{j=1}^m\frac{ij}{d}[{\rm gcd}(i,j)=d] d=1∑min(n,m)i=1∑nj=1∑mdij[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) 1 d ∑ i = 1 n ∑ j = 1 m i j [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}\frac{1}{d}\sum_{i=1}^n\sum_{j=1}^mij[{\rm gcd}(i,j)=d] d=1∑min(n,m)d1i=1∑nj=1∑mij[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) 1 d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j ⋅ d 2 [ g c d ( i , j ) = 1 ] \sum_{d=1}^{{\rm min}(n,m)}\frac{1}{d}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}ij\cdot d^2[{\rm gcd}(i,j)=1] d=1∑min(n,m)d1i=1∑⌊dn⌋j=1∑⌊dm⌋ij⋅d2[gcd(i,j)=1]
∑ d = 1 m i n ( n , m ) d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j [ g c d ( i , j ) = 1 ] \sum_{d=1}^{{\rm min}(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}ij[{\rm gcd}(i,j)=1] d=1∑min(n,m)di=1∑⌊dn⌋j=1∑⌊dm⌋ij[gcd(i,j)=1]
∑ d = 1 m i n ( n , m ) d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j ϵ ( g c d ( i , j ) ) \sum_{d=1}^{{\rm min}(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}ijϵ({\rm gcd}(i,j)) d=1∑min(n,m)di=1∑⌊dn⌋j=1∑⌊dm⌋ijϵ(gcd(i,j))
∑ d = 1 m i n ( n , m ) d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j ∑ k ∣ g c d ( i , j ) μ ( k ) \sum_{d=1}^{{\rm min}(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}ij\sum_{k|{\rm gcd}(i,j)}\mu(k) d=1∑min(n,m)di=1∑⌊dn⌋j=1∑⌊dm⌋ijk∣gcd(i,j)∑μ(k)
∑ d = 1 m i n ( n , m ) d ∑ k = 1 ⌊ m i n ( n , m ) d ⌋ μ ( k ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ i j [ k ∣ g c d ( i , j ) ] \sum_{d=1}^{{\rm min}(n,m)}d\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}ij[k|{\rm gcd}(i,j)] d=1∑min(n,m)dk=1∑⌊dmin(n,m)⌋μ(k)i=1∑⌊dn⌋j=1∑⌊dm⌋ij[k∣gcd(i,j)]
∑ d = 1 m i n ( n , m ) d ∑ k = 1 ⌊ m i n ( n , m ) d ⌋ μ ( k ) ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j ⋅ k 2 \sum_{d=1}^{{\rm min}(n,m)}d\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij\cdot k^2 d=1∑min(n,m)dk=1∑⌊dmin(n,m)⌋μ(k)i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij⋅k2
∑ d = 1 m i n ( n , m ) d ∑ k = 1 ⌊ m i n ( n , m ) d ⌋ μ ( k ) ⋅ k 2 ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j \sum_{d=1}^{{\rm min}(n,m)}d\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij d=1∑min(n,m)dk=1∑⌊dmin(n,m)⌋μ(k)⋅k2i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij
F ( n , m ) = ∑ k = 1 m i n ( n , m ) d μ ( k ) ⋅ k 2 ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ i j = ∑ k = 1 m i n ( n , m ) d μ ( k ) ⋅ k 2 ⋅ ⌊ n k ⌋ ⋅ ( ⌊ n k ⌋ + 1 ) 2 × ⌊ m k ⌋ ⋅ ( ⌊ m k ⌋ + 1 ) 2 \color{#f00}\begin{aligned}F(n,m)=&\sum_{k=1}^{\frac{{\rm min}(n,m)}{d}}\mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}ij\\=&\sum_{k=1}^{\frac{{\rm min}(n,m)}{d}}\mu(k)\cdot k^2\cdot\frac{\left\lfloor\frac{n}{k}\right\rfloor\cdot(\left\lfloor\frac{n}{k}\right\rfloor+1)}{2}\times\frac{\left\lfloor\frac{m}{k}\right\rfloor\cdot(\left\lfloor\frac{m}{k}\right\rfloor+1)}{2} \end{aligned} F(n,m)==k=1∑dmin(n,m)μ(k)⋅k2i=1∑⌊kn⌋j=1∑⌊km⌋ijk=1∑dmin(n,m)μ(k)⋅k2⋅2⌊kn⌋⋅(⌊kn⌋+1)×2⌊km⌋⋅(⌊km⌋+1)
∑ d = 1 m i n ( n , m ) d × F ( ⌊ n k ⌋ , ⌊ m k ⌋ ) \color{#f00}\sum_{d=1}^{{\rm min}(n,m)}d\times F\left(\left\lfloor\frac{n}{k}\right\rfloor,\left\lfloor\frac{m}{k}\right\rfloor\right) d=1∑min(n,m)d×F(⌊kn⌋,⌊km⌋)
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int N=10010000;
const long long mod=20101009;
long long sum[N];
bool vis[N];
int prim[N],mu[N];
void get_mu(int n){
mu[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){mu[i]=-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<=n;i++)
sum[i]=(sum[i-1]+1ll*mu[i]*1ll*i%mod*1ll*i%mod)%mod;
}
int main()
{
int n=read(),m=read();
if(n>m)swap(n,m);
get_mu(n);
long long ans=0;
long long inv2=(mod+1ll)/2ll;
for(int d=1;d<=n;d++){
int x=n/d,y=m/d,minn=min(x,y);
long long Sum=0ll;
for(int l=1,r;l<=minn;l=r+1ll){
r=min(x/(x/l),y/(y/l));
Sum=(Sum+(sum[r]-sum[l-1])%mod*(((1ll+x/l)%mod*1ll*(x/l)%mod*inv2%mod)%mod)%mod*(((1ll+y/l)%mod*1ll*(y/l)%mod*inv2%mod)%mod)%mod)%mod;
}
ans=(ans+Sum*1ll*d)%mod;
}
printf("%lld\n",(ans%mod+mod)%mod);
return 0;
}
∑ d = 1 m i n ( n , m ) d ∑ k = 1 ⌊ m i n ( n , m ) d ⌋ μ ( k ) ⋅ k 2 ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j \sum_{d=1}^{{\rm min}(n,m)}d\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}\mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij d=1∑min(n,m)dk=1∑⌊dmin(n,m)⌋μ(k)⋅k2i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij
∑ d = 1 m i n ( n , m ) ∑ k = 1 ⌊ m i n ( n , m ) d ⌋ d ⋅ μ ( k ) ⋅ k 2 ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j \sum_{d=1}^{{\rm min}(n,m)}\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{d}\right\rfloor}d\cdot \mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij d=1∑min(n,m)k=1∑⌊dmin(n,m)⌋d⋅μ(k)⋅k2i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij
∑ T = 1 m i n ( n , m ) ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ m T ⌋ i j ∑ d ∣ T T d ⋅ μ ( d ) ⋅ d 2 \sum_{T=1}^{{\rm min}(n,m)}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{T}\right\rfloor}ij\sum_{d|T}\frac{T}{d}\cdot \mu(d)\cdot d^2 T=1∑min(n,m)i=1∑⌊Tn⌋j=1∑⌊Tm⌋ijd∣T∑dT⋅μ(d)⋅d2
∑ T = 1 m i n ( n , m ) ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ m T ⌋ i j ∑ d ∣ T T μ ( d ) d \sum_{T=1}^{{\rm min}(n,m)}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{T}\right\rfloor}ij\sum_{d|T}T \mu(d) d T=1∑min(n,m)i=1∑⌊Tn⌋j=1∑⌊Tm⌋ijd∣T∑Tμ(d)d
F ( T ) = ∑ d ∣ T T μ ( d ) d \color{#f00}F(T)=\sum_{d|T}T \mu(d) d F(T)=d∣T∑Tμ(d)d
∑ T = 1 m i n ( n , m ) ⌊ n T ⌋ ⋅ ( ⌊ n T ⌋ + 1 ) 2 × ⌊ m T ⌋ ⋅ ( ⌊ m T ⌋ + 1 ) 2 × F ( T ) \color{#f00}\sum_{T=1}^{{\rm min}(n,m)}\frac{\left\lfloor\frac{n}{T}\right\rfloor\cdot(\left\lfloor\frac{n}{T}\right\rfloor+1)}{2}\times\frac{\left\lfloor\frac{m}{T}\right\rfloor\cdot(\left\lfloor\frac{m}{T}\right\rfloor+1)}{2}\times F(T) T=1∑min(n,m)2⌊Tn⌋⋅(⌊Tn⌋+1)×2⌊Tm⌋⋅(⌊Tm⌋+1)×F(T)
  \ \ \ \ \ \, 显然, F F F是积性函数,我们最快可以 O ( n ) O(n) O(n)地把他筛出来,具体操作可以看【积性函数的线性筛】,欧拉筛三步走,询问分块,复杂度 O ( n ) O(\sqrt n) O(n),总时间复杂度 O ( n + T n ) \color{#f00}O(n+T\sqrt n) O(n+Tn)
s t e p 1 \tt step1 step1. 如果 p p p是素数:
f ( p ) = p − p 2 f(p)=p-p^2 f(p)=p−p2
s t e p 2 \tt step2 step2. 如果 p p p是素数, i % p ≠ 0 i\%p\neq0 i%p̸=0: f ( p i ) = f ( i ) × ( p − p 2 ) f(pi)=f(i)\times (p-p^2) f(pi)=f(i)×(p−p2)
s t e p 3 \tt step3 step3. 如果 p p p是素数, i % p = 0 i\%p=0 i%p=0:我们把 p i pi pi 分解成 p c x p^cx pcx :
f ( p i ) = f ( p c x ) = f ( x ) × ( p c − p c + 1 ) f(pi)=f(p^cx)=f(x)\times(p^c-p^{c+1}) f(pi)=f(pcx)=f(x)×(pc−pc+1)
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
//char buf[1<<15],*S=buf,*T=buf;
//char getch(){return S==T&&(T=(S=buf)+fread(buf,1,1<<15,stdin),S==T)?0:*S++;}
inline int read(){
int x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
if(f)return x;else return -x;
}
const int N=1e7+10;
const long long mod=20101009;
long long inv2=(mod+1ll)/2ll;
bool vis[N];
long long pri[N],cnt[N],power[N];
long long prime[N],f[N];
void Get_Shai(long long n){
f[1]=1;
for(long long i=1;i<=n;i++)power[i]=1;
for(long long i=2;i<=n;i++){
if(!vis[i]){
cnt[i]=1;pri[i]=i;power[i]=i;
prime[++prime[0]]=i;
f[i]=(1ll*i-1ll*i*i%mod+mod)%mod;
}
for(long long j=1,v,pc;j<=prime[0]&&i*prime[j]<=n;j++){
v=i*prime[j];
vis[v]=1;
if(i%prime[j]==0){
cnt[v]=cnt[i]+1;pri[v]=pri[i];power[v]=(1ll*power[i]*pri[i])%mod;
f[v]=f[v/power[v]]*(1ll*power[v]-1ll*power[v]*pri[i]%mod+mod)%mod;
break;
}
cnt[v]=1;pri[v]=prime[j];power[v]=prime[j];
f[v]=(f[i]*f[prime[j]])%mod;
}
}
for(int i=1;i<=n;i++)f[i]=(1ll*f[i]+f[i-1])%mod;
}
long long Get_ans(int x,int y){
long long Sum=0;
for(int l=1,r;l<=x;l=r+1){
r=min(x/(x/l),y/(y/l));
Sum=(Sum+(1ll*f[r]-f[l-1]+mod)%mod*(((1ll+x/l)%mod*1ll*(x/l)%mod*inv2%mod)%mod)%mod*(((1ll+y/l)%mod*1ll*(y/l)%mod*inv2%mod)%mod)%mod)%mod;
}
return Sum;
}
int main()
{
int n=read(),m=read();
if(n>m)swap(n,m);
Get_Shai(1ll*n);
printf("%lld\n",(Get_ans(n,m)%mod+mod)%mod);
return 0;
}
∑ i = 1 n ∑ j = 1 n i j g c d ( i , j ) \sum_{i=1}^n\sum_{j=1}^nij{\rm gcd}(i,j) i=1∑nj=1∑nijgcd(i,j)
∑ d = 1 n ∑ i = 1 n ∑ j = 1 n i j d [ g c d ( i , j ) = d ] \sum_{d=1}^{n}\sum_{i=1}^n\sum_{j=1}^nijd[{\rm gcd}(i,j)=d] d=1∑ni=1∑nj=1∑nijd[gcd(i,j)=d]
∑ d = 1 n d ∑ i = 1 n ∑ j = 1 n i j [ g c d ( i , j ) = d ] \sum_{d=1}^{n}d\sum_{i=1}^n\sum_{j=1}^nij[{\rm gcd}(i,j)=d] d=1∑ndi=1∑nj=1∑nij[gcd(i,j)=d]
∑ d = 1 n d ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i j ⋅ d 2 [ g c d ( i , j ) = 1 ] \sum_{d=1}^{n}{d}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor}ij\cdot d^2[{\rm gcd}(i,j)=1] d=1∑ndi=1∑⌊dn⌋j=1∑⌊dn⌋ij⋅d2[gcd(i,j)=1]
∑ d = 1 n d 3 ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i j [ g c d ( i , j ) = 1 ] \sum_{d=1}^{n}{d^3}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor}ij[{\rm gcd}(i,j)=1] d=1∑nd3i=1∑⌊dn⌋j=1∑⌊dn⌋ij[gcd(i,j)=1]
∑ d = 1 n d 3 ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i j ϵ ( g c d ( i , j ) ) \sum_{d=1}^{n}d^3\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor}ijϵ({\rm gcd}(i,j)) d=1∑nd3i=1∑⌊dn⌋j=1∑⌊dn⌋ijϵ(gcd(i,j))
∑ d = 1 n d 3 ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i j ∑ k ∣ g c d ( i , j ) μ ( k ) \sum_{d=1}^{n}d^3\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor}ij\sum_{k|{\rm gcd}(i,j)}\mu(k) d=1∑nd3i=1∑⌊dn⌋j=1∑⌊dn⌋ijk∣gcd(i,j)∑μ(k)
∑ d = 1 n d 3 ∑ k = 1 ⌊ n d ⌋ μ ( k ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ n d ⌋ i j [ k ∣ g c d ( i , j ) ] \sum_{d=1}^{n}d^3\sum_{k=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{d}\right\rfloor}ij[k|{\rm gcd}(i,j)] d=1∑nd3k=1∑⌊dn⌋μ(k)i=1∑⌊dn⌋j=1∑⌊dn⌋ij[k∣gcd(i,j)]
∑ d = 1 n d 3 ∑ k = 1 ⌊ n d ⌋ μ ( k ) ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j ⋅ k 2 \sum_{d=1}^{n}d^3\sum_{k=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij\cdot k^2 d=1∑nd3k=1∑⌊dn⌋μ(k)i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij⋅k2
∑ d = 1 n d 3 ∑ k = 1 ⌊ n d ⌋ μ ( k ) ⋅ k 2 ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ i j \sum_{d=1}^{n}d^3\sum_{k=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\mu(k)\cdot k^2\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}ij d=1∑nd3k=1∑⌊dn⌋μ(k)⋅k2i=1∑⌊dkn⌋j=1∑⌊dkm⌋ij
∑ T = 1 n ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ n T ⌋ i j ∑ d ∣ T d 3 ⋅ μ ( T d ) ⋅ ( T d ) 2 \sum_{T=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{T}\right\rfloor}ij\sum_{d|T}d^3\cdot \mu\left(\frac{T}{d}\right)\cdot \left(\frac{T}{d}\right)^2 T=1∑ni=1∑⌊Tn⌋j=1∑⌊Tn⌋ijd∣T∑d3⋅μ(dT)⋅(dT)2
∑ T = 1 n ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ n T ⌋ i j ∑ d ∣ T T 2 ⋅ d ⋅ μ ( T d ) \sum_{T=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{T}\right\rfloor}ij\sum_{d|T}T^2\cdot d\cdot \mu\left(\frac{T}{d}\right) T=1∑ni=1∑⌊Tn⌋j=1∑⌊Tn⌋ijd∣T∑T2⋅d⋅μ(dT)
∑ T = 1 n ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ n T ⌋ i j T 2 ∑ d ∣ T d ⋅ μ ( T d ) \sum_{T=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{T}\right\rfloor}ijT^2\sum_{d|T}d\cdot \mu\left(\frac{T}{d}\right) T=1∑ni=1∑⌊Tn⌋j=1∑⌊Tn⌋ijT2d∣T∑d⋅μ(dT)
∑ T = 1 n ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ n T ⌋ i j T 2 ( i d ∗ μ ) ( T ) \sum_{T=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{T}\right\rfloor}ijT^2\color{#f00}(id*\mu)(T) T=1∑ni=1∑⌊Tn⌋j=1∑⌊Tn⌋ijT2(id∗μ)(T)
∑ T = 1 n ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ n T ⌋ i j T 2 φ ( T ) \sum_{T=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{T}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{n}{T}\right\rfloor}ijT^2\color{#f00}\varphi(T) T=1∑ni=1∑⌊Tn⌋j=1∑⌊Tn⌋ijT2φ(T)
∑ T = 1 n ⌊ n T ⌋ ⋅ ( ⌊ n T ⌋ + 1 ) 2 × ⌊ m T ⌋ ⋅ ( ⌊ m T ⌋ + 1 ) 2 × T 2 φ ( T ) \color{#f00}\sum_{T=1}^{n}\frac{\left\lfloor\frac{n}{T}\right\rfloor\cdot(\left\lfloor\frac{n}{T}\right\rfloor+1)}{2}\times\frac{\left\lfloor\frac{m}{T}\right\rfloor\cdot(\left\lfloor\frac{m}{T}\right\rfloor+1)}{2}\times T^2\varphi(T) T=1∑n2⌊Tn⌋⋅(⌊Tn⌋+1)×2⌊Tm⌋⋅(⌊Tm⌋+1)×T2φ(T)
  \ \ \ \ \ \, 需要连续提取 2 2 2 次公约数,和P1829有异曲同工之妙,杜教筛筛出 T 2 φ ( T ) T^2\varphi(T) T2φ(T) 的前缀和,分块询问答案询问,总时间复杂度 O ( n 2 3 ) \color{#f00}O(n^{\frac{2}{3}}) O(n32),杜教筛详见【莫比乌斯反演和杜教筛】
f ( x ) = x 2 φ ( x ) f(x)=x^2\varphi(x) f(x)=x2φ(x)
g ( x ) = x 2 , ∑ i = 1 x g ( i ) = x ( x + 1 ) ( 2 x + 1 ) 6 g(x)=x^2\ \ \ ,\sum_{i=1}^{x}g(i)=\frac{x(x+1)(2x+1)}{6} g(x)=x2 ,∑i=1xg(i)=6x(x+1)(2x+1)
( f ∗ g ) ( x ) = x 3 , ∑ i = 1 x ( f ∗ g ) ( i ) = x 2 ( x + 1 ) 2 4 (f*g)(x)=x^3\ \ \ ,\sum_{i=1}^{x}(f*g)(i)=\frac{x^2(x+1)^2}{4} (f∗g)(x)=x3 ,∑i=1x(f∗g)(i)=4x2(x+1)2
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
inline long long read(){
long long x=0,f=1;char ch;ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getchar();}
if(f)return x;else return -x;
}
const int N=8000010;
long long mod,n,inv6,inv4;
int prim[N];
long long f[N];
bool vis[N];
void get_f(int n){
f[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){f[i]=i-1;prim[++prim[0]]=i;}
for(int j=1;j<=prim[0]&&i*prim[j]<=n;j++){
vis[i*prim[j]]=1;
if(i%prim[j]==0){f[i*prim[j]]=f[i]*prim[j];break;}
else f[i*prim[j]]=f[i]*(prim[j]-1);
}
}
for(int i=1;i<=n;++i)f[i]=(f[i-1]+1ll*f[i]*i%mod*i%mod)%mod;
}
map<long long,long long> mp;
long long power(long long a,long long b){
long long ans=1;
while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b>>=1;}
return ans;
}
long long Sumfg(long long x){x%=mod;return x*(x+1)%mod*x%mod*(x+1)%mod*inv4%mod;}
long long Sumg(long long x){x%=mod;return x*(x+1)%mod*(x+x+1)%mod*inv6%mod;}
long long Sumf(long long x){
if(x<=min(N-10,(int)n))return f[x];
if(mp[x])return mp[x];
long long ret=Sumfg(x);
for(long long i=2,j;i<=x;i=j+1){
j=x/(x/i);
ret=(ret-(Sumg(j)-Sumg(i-1)+mod)%mod*Sumf(x/i)%mod+mod)%mod;
}
return mp[x]=(ret+mod)%mod;
}
int main()
{
mod=read();n=read();
inv4=power(4,mod-2);
inv6=power(6,mod-2);
get_f(min(N-10,(int)n));
long long ans=0;
for(long long i=1,j;i<=n;i=j+1){
j=n/(n/i);
ans=(ans+(Sumf(j)-Sumf(i-1))%mod*Sumfg(n/i)%mod)%mod;
}
printf("%lld\n",(ans+mod)%mod);
return 0;
}
∑ i = 1 n ∑ j = 1 m φ ( i j ) \sum_{i=1}^n\sum_{j=1}^m\varphi(ij) i=1∑nj=1∑mφ(ij)