P2522 [HAOI2011]Problem b
∑ 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⌋
-   \ \ \ \ \ \, O ( n ) O(n) O(n)预处理 μ \mu μ,每次询问分块,复杂度 O ( n ) O(\sqrt n) O(n),总复杂度为 O ( n + T 4 n ) \color{#f00}O(n+T4\sqrt n) O(n+T4n)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
P2257 YY的GCD
∑ 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)
-   \ \ \ \ \ \, O ( n ) O(n) O(n)预处理 μ \mu μ, O ( n log n ) O(n\log{n}) O(nlogn)预处理 F F F,每次询问分块,复杂度 O ( n ) O(\sqrt n) O(n),总复杂度为 O ( n log n + T n ) \color{#f00}O(n\log n+T\sqrt n) O(nlogn+Tn)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
P3312 [SDOI2014]数表
∑ 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)
-   \ \ \ \ \ \, O ( n log n ) O(n\log n) O(nlogn)预处理 F F F和 μ \mu μ,把每次询问的 a a a值离线排序,用树状数组维护 f f f,花费时间 O ( n log 2 n ) O(n\log ^2n) O(nlog2n),询问分块,复杂度 O ( n log n ) O(\sqrt n\log n) O(nlogn),总时间复杂度 O ( n log 2 n + T n log n ) \color{#f00}O(n\log ^2n+T\sqrt n\log n) O(nlog2n+Tnlogn)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
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;
}
P3704 [SDOI2017]数字表格
∏ 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⌋
-   \ \ \ \ \ \, O ( n ) O(n) O(n)预处理 f f f和 μ \mu μ, O ( n log n ) O(n\log{n}) O(nlogn)预处理 C C C,每次询问分块,复杂度 O ( n log n ) O(\sqrt n \log n) O(nlogn),总复杂度为 O ( n log n + T n log n ) \color{#f00}O(n\log n+T\sqrt n \log n) O(nlogn+Tnlogn)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
P3327 [SDOI2015]约数个数和
∑ 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)
-   \ \ \ \ \ \, O ( n log n ) O(n\log n) O(nlogn)预处理 F F F, f f f和 μ \mu μ,询问分块,复杂度 O ( n log n ) O(\sqrt n\log n) O(nlogn),总时间复杂度 O ( n log n + T n log n ) \color{#f00}O(n\log n+T\sqrt n\log n) O(nlogn+Tnlogn),和P3312 [SDOI2014]数表这道题差不多
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
P3455 [POI2007]ZAP-Queries
∑ 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⌋
-   \ \ \ \ \ \, 裸题啊!模板啊! O ( n ) O(n) O(n)预处理 μ \mu μ,询问分块,复杂度 O ( n ) O(\sqrt n) O(n),总时间复杂度 O ( n + T n ) \color{#f00}O(n+T\sqrt n) O(n+Tn)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
P1829 [国家集训队]Crash的数字表格 / JZPTAB
∑ 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⌋)
-   \ \ \ \ \ \, 感觉很神奇,需要连续提取 2 2 2 次公约数, O ( n ) O(n) O(n)预处理 μ \mu μ,分块询问 F F F, O ( n ) O(\sqrt{\sqrt n}) O(n),答案询问分块,复杂度 O ( n ) O(\sqrt n) O(n),嵌套起来总时间复杂度 O ( n + n 3 4 ) \color{#f00}O(n+n^{\frac{3}{4}}) O(n+n43),代码比较丑
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
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;
}
- 扩展:这个题其实是两道题的融合,上面的方法是过不了JZPTAB的,在bzoj上面是权限题,所以先挖坑,我们下面继续来化简:
∑ 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−p2s 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<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
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;
}
P3768 简单的数学题
∑ 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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<map>
#include<stack>
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;
}
P4240 毒瘤之神的考验
∑ i = 1 n ∑ j = 1 m φ ( i j ) \sum_{i=1}^n\sum_{j=1}^m\varphi(ij) i=1∑nj=1∑mφ(ij)
-   \ \ \ \ \ \, 反演过程:
∑ i = 1 n ∑ j = 1 m φ ( i ) φ ( j ) g c d ( i , j ) φ ( g c d ( i , j ) ) \sum_{i=1}^n\sum_{j=1}^m\frac{\varphi(i)\varphi(j){\rm gcd}(i,j)}{\varphi({\rm gcd}(i,j))} i=1∑nj=1∑mφ(gcd(i,j))φ(i)φ(j)gcd(i,j)
∑ d = 1 m i n ( n , m ) ∑ i = 1 n ∑ j = 1 m φ ( i ) φ ( j ) d φ ( d ) [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}\sum_{i=1}^n\sum_{j=1}^m\frac{\varphi(i)\varphi(j)d}{\varphi(d)}[{\rm gcd}(i,j)=d] d=1∑min(n,m)i=1∑nj=1∑mφ(d)φ(i)φ(j)d[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ i = 1 n ∑ j = 1 m φ ( i ) φ ( j ) [ g c d ( i , j ) = d ] \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{i=1}^n\sum_{j=1}^m\varphi(i)\varphi(j)[{\rm gcd}(i,j)=d] d=1∑min(n,m)φ(d)di=1∑nj=1∑mφ(i)φ(j)[gcd(i,j)=d]
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ φ ( d i ) φ ( d j ) [ g c d ( i , j ) = 1 ] \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\varphi(di)\varphi(dj)[{\rm gcd}(i,j)=1] d=1∑min(n,m)φ(d)di=1∑⌊dn⌋j=1∑⌊dm⌋φ(di)φ(dj)[gcd(i,j)=1]
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ φ ( d i ) φ ( d j ) ϵ ( g c d ( i , j ) ) \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\varphi(di)\varphi(dj)ϵ({\rm gcd}(i,j)) d=1∑min(n,m)φ(d)di=1∑⌊dn⌋j=1∑⌊dm⌋φ(di)φ(dj)ϵ(gcd(i,j))
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ φ ( d i ) φ ( d j ) ∑ k ∣ g c d ( i , j ) μ ( k ) \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\varphi(di)\varphi(dj)\sum_{k|{\rm gcd}(i,j)}\mu(k) d=1∑min(n,m)φ(d)di=1∑⌊dn⌋j=1∑⌊dm⌋φ(di)φ(dj)k∣gcd(i,j)∑μ(k)
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ k = 1 ⌊ m i n ( n , m ) d k ⌋ μ ( k ) ∑ i = 1 ⌊ n d ⌋ ∑ j = 1 ⌊ m d ⌋ φ ( d i ) φ ( d j ) [ k ∣ g c d ( i , j ) ] \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{dk}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\varphi(di)\varphi(dj)[k|{\rm gcd}(i,j)] d=1∑min(n,m)φ(d)dk=1∑⌊dkmin(n,m)⌋μ(k)i=1∑⌊dn⌋j=1∑⌊dm⌋φ(di)φ(dj)[k∣gcd(i,j)]
∑ d = 1 m i n ( n , m ) d φ ( d ) ∑ k = 1 ⌊ m i n ( n , m ) d k ⌋ μ ( k ) ∑ i = 1 ⌊ n d k ⌋ ∑ j = 1 ⌊ m d k ⌋ φ ( d i k ) φ ( d j k ) \sum_{d=1}^{{\rm min}(n,m)}\frac{d}{\varphi(d)}\sum_{k=1}^{\left\lfloor\frac{{\rm min}(n,m)}{dk}\right\rfloor}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{dk}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dk}\right\rfloor}\varphi(dik)\varphi(djk) d=1∑min(n,m)φ(d)dk=1∑⌊dkmin(n,m)⌋μ(k)i=1∑⌊dkn⌋j=1∑⌊dkm⌋φ(dik)φ(djk)
∑ T = 1 m i n ( n , m ) ∑ i = 1 ⌊ n T ⌋ ∑ j = 1 ⌊ m T ⌋ φ ( i T ) φ ( j T ) ∑ d ∣ T d φ ( d ) μ ( T d ) \color{#f00}\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}\varphi(iT)\varphi(jT)\sum_{d|T}\frac{d}{\varphi(d)}\mu(\frac{T}{d}) T=1∑min(n,m)i=1∑⌊Tn⌋j=1∑⌊Tm⌋φ(iT)φ(jT)d∣T∑φ(d)dμ(dT)
-
 
\ \ \ \ \ \,
O
(
n
)
O(n)
O(n)预处理出
μ
\mu
μ,
φ
\varphi
φ,
O
(
n
log
n
)
O(n\log n)
O(nlogn)预处理出后面的
∑
d
∣
T
d
φ
(
d
)
μ
(
T
d
)
\sum_{d|T}\frac{d}{\varphi(d)}\mu(\frac{T}{d})
∑d∣Tφ(d)dμ(dT),然后对前面的进行分块操作,会发现要存很多东西,于是我们一边分块,一边操作,总复杂度很玄学,大约是
O
(
n
log
n
+
n
T
2
3
+
T
(
n
+
⌊
n
T
1
3
⌋
)
)
\color{#f00}O(n\log n+nT^{\frac{2}{3}}+T(\sqrt n+\left\lfloor\frac{n}{T^{\frac{1}{3}}}\right\rfloor))
O(nlogn+nT32+T(n+⌊T31n⌋)),能过就行了XD,
(代码加了信仰fread)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<cctype>
#include<vector>
using namespace std;
const int inf=0x7fffffff;
const double eps=1e-10;
const double pi=acos(-1.0);
char buf[1<<15],*S=buf,*TT=buf;
char getch(){return S==TT&&(TT=(S=buf)+fread(buf,1,1<<15,stdin),S==TT)?0:*S++;}
inline int read(){
int x=0,f=1;char ch;ch=getch();
while(ch<'0'||ch>'9'){if(ch=='-') f=0;ch=getch();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch&15);ch=getch();}
if(f)return x;else return -x;
}
const int N=100010;
const int mod=998244353;
const int B=35;
bool vis[N];
int prim[N],phi[N],mu[N],inv[N];
int F[N];
vector<int> G[N],T[B+1][B+1];
void get_phi_mu_inv(int n){
phi[1]=mu[1]=inv[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i])phi[i]=i-1,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){phi[i*prim[j]]=phi[i]*prim[j];break;}
else phi[i*prim[j]]=phi[i]*(prim[j]-1),mu[i*prim[j]]=-mu[i];
}
inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
}
}
void init(int n){
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j+=i)
F[j]=(F[j]+(1ll*mu[j/i]*i*inv[phi[i]]%mod+mod)%mod)%mod;
for(int i=1;i<=n;i++){
G[i].push_back(0);
for(int j=1;j<=n/i;j++)
G[i].push_back((G[i][j-1]+phi[i*j])%mod);
}
for(int j=1;j<=B;j++)
for(int k=j;k<=B;k++){
int len=n/max(j,k);
T[j][k].push_back(0);
for(int i=1;i<=len;i++)
T[j][k].push_back((T[j][k][i-1]+1ll*F[i]*G[i][j]%mod*G[i][k]%mod)%mod);
}
}
long long Solve(int n,int m){
long long res=0;
int ed=m/B;
for(int i=1;i<=ed;i++)
res=(res+1ll*F[i]*G[i][n/i]%mod*G[i][m/i]%mod)%mod;
for(int l=m/B+1,r,ln,lm;l<=n;l=r+1){
ln=n/l,lm=m/l;
r=min(n/ln,m/lm);
res=(res+(T[ln][lm][r]-T[ln][lm][l-1]+mod)%mod)%mod;
}
return res;
}
int main()
{
get_phi_mu_inv(100000);init(100000);
int t=read();
while(t--){
int n=read(),m=read();if(n>m)swap(n,m);
printf("%lld\n",Solve(n,m));
}
return 0;
}
P1587 [NOI2016]循环之美
 
\ \ \ \ \ \,
对于一个
K
K
K进制下的无限循环小数,在最简形态下,分母一定和
K
K
K互质,所以这道题,就是求:
∑
i
=
1
n
∑
j
=
1
m
[
g
c
d
(
i
,
j
)
=
1
]
[
g
c
d
(
j
,
K
)
=
1
]
\sum_{i=1}^n\sum_{j=1}^m[{\rm gcd}(i,j)=1][{\rm gcd}(j,K)=1]
i=1∑nj=1∑m[gcd(i,j)=1][gcd(j,K)=1]
-   \ \ \ \ \ \, 反演过程:
∑ j = 1 m [ g c d ( j , K ) = 1 ] ∑ i = 1 n [ g c d ( i , j ) = 1 ] \sum_{j=1}^m[{\rm gcd}(j,K)=1]\sum_{i=1}^n[{\rm gcd}(i,j)=1] j=1∑m[gcd(j,K)=1]i=1∑n[gcd(i,j)=1]
∑ j = 1 m [ g c d ( j , K ) = 1 ] ∑ i = 1 n ϵ ( g c d ( i , j ) ) \sum_{j=1}^m[{\rm gcd}(j,K)=1]\sum_{i=1}^nϵ({\rm gcd}(i,j)) j=1∑m[gcd(j,K)=1]i=1∑nϵ(gcd(i,j))
∑ j = 1 m [ g c d ( j , K ) = 1 ] ∑ i = 1 n ∑ k ∣ g c d ( i , j ) μ ( k ) \sum_{j=1}^m[{\rm gcd}(j,K)=1]\sum_{i=1}^n\sum_{k|{\rm gcd}(i,j)}\mu(k) j=1∑m[gcd(j,K)=1]i=1∑nk∣gcd(i,j)∑μ(k)
∑ k = 1 m i n ( n , m ) μ ( k ) ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ g c d ( j k , K ) = 1 ] \sum_{k=1}^{{\rm min}(n,m)}\mu(k)\sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[{\rm gcd}(jk,K)=1] k=1∑min(n,m)μ(k)i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(jk,K)=1]
∑ k = 1 m i n ( n , m ) μ ( k ) [ g c d ( k , K ) = 1 ] ∑ i = 1 ⌊ n k ⌋ ∑ j = 1 ⌊ m k ⌋ [ g c d ( j , K ) = 1 ] \sum_{k=1}^{{\rm min}(n,m)}\mu(k)[{\rm gcd}(k,K)=1]\sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[{\rm gcd}(j,K)=1] k=1∑min(n,m)μ(k)[gcd(k,K)=1]i=1∑⌊kn⌋j=1∑⌊km⌋[gcd(j,K)=1]
∑ k = 1 m i n ( n , m ) μ ( k ) [ g c d ( k , K ) = 1 ] ∑ j = 1 ⌊ m k ⌋ [ g c d ( j , K ) = 1 ] ∑ i = 1 ⌊ n k ⌋ \sum_{k=1}^{{\rm min}(n,m)}\mu(k)[{\rm gcd}(k,K)=1]\sum_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[{\rm gcd}(j,K)=1]\sum_{i=1}^{\left\lfloor\frac{n}{k}\right\rfloor} k=1∑min(n,m)μ(k)[gcd(k,K)=1]j=1∑⌊km⌋[gcd(j,K)=1]i=1∑⌊kn⌋
∑ k = 1 m i n ( n , m ) μ ( k ) [ g c d ( k , K ) = 1 ] ∑ j = 1 ⌊ m k ⌋ [ g c d ( j , K ) = 1 ] ⌊ n k ⌋ \sum_{k=1}^{{\rm min}(n,m)}\mu(k)[{\rm gcd}(k,K)=1]\sum_{j=1}^{\left\lfloor\frac{m}{k}\right\rfloor}[{\rm gcd}(j,K)=1]{\left\lfloor\frac{n}{k}\right\rfloor} k=1∑min(n,m)μ(k)[gcd(k,K)=1]j=1∑⌊km⌋[gcd(j,K)=1]⌊kn⌋
f ( x , K ) = ∑ i = 1 x μ ( i ) [ g c d ( i , K ) = 1 ] \color{#f00}f(x,K)=\sum_{i=1}^{x}\mu(i)[{\rm gcd}(i,K)=1] f(x,K)=i=1∑xμ(i)[gcd(i,K)=1]
s ( x ) = ∑ i = 1 x [ g c d ( i , K ) = 1 ] \color{#f00}s(x)=\sum_{i=1}^{x}[{\rm gcd}(i,K)=1] s(x)=i=1∑x[gcd(i,K)=1]
∑ k = 1 m i n ( n , m ) μ ( k ) [ g c d ( k , K ) = 1 ] ⌊ n k ⌋ s ( ⌊ m k ⌋ ) \color{#f00}\sum_{k=1}^{{\rm min}(n,m)}\mu(k)[{\rm gcd}(k,K)=1]{\left\lfloor\frac{n}{k}\right\rfloor}s\left(\left\lfloor\frac{m}{k}\right\rfloor\right) k=1∑min(n,m)μ(k)[gcd(k,K)=1]⌊kn⌋s(⌊km⌋)
  \ \ \ \ \ \, 然而还没有完,我们求出 f f f和 s s s,也就是方便分块操作,现在我们得化简它们:
-
s ( x ) \color{#f00}s(x) s(x)
s ( x ) = ∑ i = 1 x [ g c d ( i , K ) = 1 ] s(x)=\sum_{i=1}^{x}[{\rm gcd}(i,K)=1] s(x)=i=1∑x[gcd(i,K)=1]
因为若是 g c d ( a , K ) = 1 {\rm gcd}(a,K)=1 gcd(a,K)=1,那么就有 g c d ( a + b k , K ) = 1 {\rm gcd}(a+bk,K)=1 gcd(a+bk,K)=1。
所以有:
s ( x ) = ⌊ x k ⌋ s ( k ) + s ( x % k ) \color{#f00}s(x)=\left\lfloor\frac{x}{k}\right\rfloor s(k)+s(x\%k) s(x)=⌊kx⌋s(k)+s(x%k)
所以只需要暴力求出 k k k以内的 s s s就可以了。 -
f ( x , K ) \color{#f00}f(x,K) f(x,K)
∑ i = 1 x μ ( i ) [ g c d ( i , K ) = 1 ] \sum_{i=1}^{x}\mu(i)[{\rm gcd}(i,K)=1] i=1∑xμ(i)[gcd(i,K)=1]
∑ i = 1 x μ ( i ) ϵ ( g c d ( i , K ) ) \sum_{i=1}^{x}\mu(i)ϵ({\rm gcd}(i,K)) i=1∑xμ(i)ϵ(gcd(i,K))
∑ i = 1 x μ ( i ) ∑ d ∣ g c d ( i , K ) μ ( d ) \sum_{i=1}^{x}\mu(i)\sum_{d|{\rm gcd}(i,K)}\mu(d) i=1∑xμ(i)d∣gcd(i,K)∑μ(d)
∑ d = 1 m i n ( x , K ) μ ( d ) ∑ i = 1 x μ ( i ) [ d ∣ g c d ( i , K ) ] \sum_{d=1}^{{\rm min}(x,K)}\mu(d)\sum_{i=1}^x\mu(i)[d|{\rm gcd}(i,K)] d=1∑min(x,K)μ(d)i=1∑xμ(i)[d∣gcd(i,K)]
∑ d = 1 m i n ( x , K ) μ ( d ) ∑ i = 1 ⌊ x d ⌋ μ ( i d ) [ d ∣ g c d ( i d , K ) ] \sum_{d=1}^{{\rm min}(x,K)}\mu(d)\sum_{i=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(id)[d|{\rm gcd}(id,K)] d=1∑min(x,K)μ(d)i=1∑⌊dx⌋μ(id)[d∣gcd(id,K)]
∑ d = 1 m i n ( x , K ) μ ( d ) ∑ i = 1 ⌊ x d ⌋ μ ( i d ) [ d ∣ K ] \sum_{d=1}^{{\rm min}(x,K)}\mu(d)\sum_{i=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(id)[d|K] d=1∑min(x,K)μ(d)i=1∑⌊dx⌋μ(id)[d∣K]
∑ d ∣ K μ ( d ) ∑ i = 1 ⌊ x d ⌋ μ ( i d ) \sum_{d|K}\mu(d)\sum_{i=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(id) d∣K∑μ(d)i=1∑⌊dx⌋μ(id)
∑ d ∣ K μ ( d ) ∑ i = 1 , g c d ( i , d ) = 1 ⌊ x d ⌋ μ ( d ) μ ( i ) \sum_{d|K}\mu(d)\sum_{i=1,{\rm gcd}(i,d)=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(d)\mu(i) d∣K∑μ(d)i=1,gcd(i,d)=1∑⌊dx⌋μ(d)μ(i)
∑ d ∣ K μ ( d ) 2 ∑ i = 1 , g c d ( i , d ) = 1 ⌊ x d ⌋ μ ( i ) \sum_{d|K}\mu(d)^2\sum_{i=1,{\rm gcd}(i,d)=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(i) d∣K∑μ(d)2i=1,gcd(i,d)=1∑⌊dx⌋μ(i)
∑ d ∣ K μ ( d ) 2 ∑ i = 1 ⌊ x d ⌋ μ ( i ) [ g c d ( i , d ) = 1 ] \sum_{d|K}\mu(d)^2\sum_{i=1}^{\left\lfloor\frac{x}{d}\right\rfloor}\mu(i)[{\rm gcd}(i,d)=1] d∣K∑μ(d)2i=1∑⌊dx⌋μ(i)[gcd(i,d)=1]
∑ d ∣ K μ ( d ) 2 f ( ⌊ x d ⌋ , d ) \color{#f00}\sum_{d|K}\mu(d)^2f\left({\left\lfloor\frac{x}{d}\right\rfloor},d\right) d∣K∑μ(d)2f(⌊dx⌋,d)
这个怎么办啊?每次递归求解,但是当 K = 1 K=1 K=1的时候,就没有办法了,我们试一试化一下 K = 1 K=1 K=1的情况:
∑ i = 1 x μ ( i ) [ g c d ( i , 1 ) = 1 ] \sum_{i=1}^{x}\mu(i)[{\rm gcd}(i,1)=1] i=1∑xμ(i)[gcd(i,1)=1]
∑ i = 1 x μ ( i ) \color{#f00}\sum_{i=1}^{x}\mu(i) i=1∑xμ(i)
一个杜教筛就完了。
复杂度大概是 O ( n 2 3 ) O(n^{\frac{2}{3}}) O(n32)
-
感觉题目还是比较难的,反正我一开始是没有想出来,翻了题解,主要是对于 s s s的处理,有多加一维描述,还有递归处理,程序的最终复杂度呢,大概就在 O ( n 2 3 ) O(n^{\frac{2}{3}}) O(n32)的样子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<cctype>
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=10000010;
int n,K,m;
int pr[2010];
bool vis[N];
int prim[N],mu[N],Sum_mu[N];
map<pair<int,int>,int> used;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
void init(){
for(int i=1;i<=K;i++)pr[i]=pr[i-1]+(gcd(i,K)==1);
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];j++){
if(i*prim[j]>=N) break;
vis[i*prim[j]]=1;
if(i%prim[j]==0){mu[i*prim[j]]=0;break;}
mu[i*prim[j]]=-mu[i];
}
}
for(int i=1;i<N;i++)Sum_mu[i]=Sum_mu[i-1]+mu[i];
}
int s(int x){return pr[x%K]+1ll*(x/K)*pr[K];}
int f(int x,int k){
if(!k||!x)return 0;
if(k==1&&x<N)return Sum_mu[x];
if(used[make_pair(x,k)])return used[make_pair(x,k)];
if(k==1){
int ret=1;
for(int l=2,r;l<=x;l=r+1)
r=x/(x/l),ret-=(r-l+1)*f(x/l,1);
return used[make_pair(x,k)]=ret;
}
int ret=0;
for(int i=1;i*i<=k;i++)if(k%i==0){
if(mu[i])ret+=f(x/i,i);
if(i*i!=k&&mu[k/i])
ret+=f(x/(k/i),k/i);
}
return used[make_pair(x,k)]=ret;
}
int main()
{
n=read();m=read();K=read();
init();
long long ans=0;
for(int l=1,r;l<=min(n,m);l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=1ll*(f(r,K)-f(l-1,K))*(n/l)*s(m/l);
}
printf("%lld\n",ans);
return 0;
}