传送门
考虑对于一个 k k k进制循环小数 x y \frac x y yx
如果循环节为 l l l
那么这个数乘上 k l k^l kl后小数部分不变
那么就是 x y − ⌊ x y ⌋ = x k l y − ⌊ x k l y ⌋ \frac x y-\lfloor\frac x y\rfloor=\frac {xk^l} y-\lfloor\frac {xk^l} y\rfloor yx−⌊yx⌋=yxkl−⌊yxkl⌋
x − ⌊ x y ⌋ y = x k l − ⌊ x k l y ⌋ y x-\lfloor\frac x y\rfloor y=xk^l-\lfloor\frac {xk^l} y\rfloor y x−⌊yx⌋y=xkl−⌊yxkl⌋y
x ≡ x k l m o d y x\equiv xk^l\ \mod y x≡xkl mody
k l ≡ 1 m o d y k^l\equiv 1\mod y kl≡1mody
考虑这个中 l l l有解的条件即 g c d ( k , y ) = 1 gcd(k,y)=1 gcd(k,y)=1
又 g c d ( x , y ) = 1 gcd(x,y)=1 gcd(x,y)=1
所以就可以的到
a n s = S ( n , m , k ) = ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = 1 ] [ g c d ( j , k ) = 1 ] ans=S(n,m,k)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=1][gcd(j,k)=1] ans=S(n,m,k)=i=1∑nj=1∑m[gcd(i,j)=1][gcd(j,k)=1]
= ∑ i = 1 n ∑ j = 1 m [ g c d ( i , j ) = 1 ] ∑ d ∣ j , d ∣ k μ ( d ) =\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=1]\sum_{d|j,d|k}\mu(d) =i=1∑nj=1∑m[gcd(i,j)=1]d∣j,d∣k∑μ(d)
枚举 d d d后可以得到
= ∑ d ∣ k μ ( d ) ∑ i = 1 n ∑ j = 1 m d [ g c d ( i , j d ) = 1 ] =\sum_{d|k}\mu(d)\sum_{i=1}^{n}\sum_{j=1}^{\frac md}[gcd(i,jd)=1] =d∣k∑μ(d)i=1∑nj=1∑dm[gcd(i,jd)=1]
= ∑ d ∣ k μ ( d ) ∑ i = 1 n ∑ j = 1 m d [ g c d ( i , j ) = 1 ] [ g c d ( i , d ) = 1 ] =\sum_{d|k}\mu(d)\sum_{i=1}^{n}\sum_{j=1}^{\frac md}[gcd(i,j)=1][gcd(i,d)=1] =d∣k∑μ(d)i=1∑nj=1∑dm[gcd(i,j)=1][gcd(i,d)=1]
= ∑ d ∣ k μ ( d ) S ( m d , n , d ) =\sum_{d|k}\mu(d)S(\frac md ,n,d) =d∣k∑μ(d)S(dm,n,d)
然后就可以愉快的递归了
k = 1 k=1 k=1时可以简单莫反后用杜教筛算 μ \mu μ前缀和整除分块
考虑 n , m n,m n,m最多只有 n \sqrt n n个取值
所以复杂度为 O ( n σ 0 ( k ) + n 2 3 ) O(\sqrt n\sigma_0(k)+n^{\frac 2 3}) O(nσ0(k)+n32)
#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
//cs int mod=1e9+7;
//inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
//inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
//inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
//inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
//inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
//inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
//inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
//inline int Inv(int x){return ksm(x,mod-2);}
cs int N=1000005;
int mu[N],pr[N],tot;
bitset<N>vis;
inline void init(cs int len=N-5){
mu[1]=1;
for(int i=2;i<=len;i++){
if(!vis[i])pr[++tot]=i,mu[i]=-1;
for(int j=1,p;j<=tot&&i*pr[j]<=len;j++){
p=i*pr[j],vis[p]=1;
if(i%pr[j]==0)break;
mu[p]=-mu[i];
}
}
for(int i=1;i<=len;i++)mu[i]+=mu[i-1];
}
map<int,int> S;
inline int summu(int n){
if(n<=N-5)return mu[n];
if(S.count(n))return S[n];
int ret=1;
for(int i=2,j;i<=n;i=j+1){
j=n/(n/i);
ret-=(j-i+1)*summu(n/i);
}
return S[n]=ret;
}
struct node{
int n,m,k;
node(int a=0,int b=0,int c=0):n(a),m(b),k(c){}
friend inline bool operator <(cs node &a,cs node &b){
return a.n==b.n?(a.m==b.m?(a.k<b.k):a.m<b.m):a.n<b.n;
}
};
map<node,ll> s;
int n,m,k,fc[2005],cnt;
inline ll calc(int n,int m){
ll ret=0;if(n>m)swap(n,m);
for(int i=1,j;i<=n;i=j+1){
j=min(n/(n/i),m/(m/i));
ret+=1ll*(summu(j)-summu(i-1))*(n/i)*(m/i);
}
return ret;
}
inline ll calc(int n,int m,int k){
if(!n)return 0;
if(!m)return 0;
node p=node(n,m,k);
if(s.count(p))return s[p];
if(k==1)return s[p]=calc(n,m);
ll ret=0;
for(int i=1,d;i<=cnt&&fc[i]<=k;i++)
if(k%(d=fc[i])==0)ret+=(mu[d]-mu[d-1])*calc(m/d,n,d);
return s[p]=ret;
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
init();
n=read(),m=read(),k=read();
for(int i=1;i<=k;i++)if(mu[i]!=mu[i-1]&&k%i==0)fc[++cnt]=i;
cout<<calc(n,m,k)<<'\n';
}