# sol

$\sum_{i=1}^{n}\sum_{j=1}^{m}[j\bot k][i\bot j]=\sum_{j=1}^{m}[j\bot k]\sum_{i=1}^{n}[i\bot j]\\=\sum_{j=1}^{m}[j\bot k]\sum_{i=1}^{n}\sum_{d|\gcd(i,j)}\mu(d)\\=\sum_{d=1}^{min(n,m)}\mu(d)\lfloor\frac nd\rfloor\sum_{j=1}^{m}[jd\bot k]\\=\sum_{d=1}^{min(n,m)}\mu(d)[d\bot k]\lfloor\frac nd\rfloor\sum_{j=1}^{m/d}[j\bot k]$

$S(n,k)=\sum_{i=1}^{n}\mu(i)[i\bot k]\\=\sum_{i=1}^{n}\mu(i)\sum_{d|i,d|k}\mu(d)\\=\sum_{d|k}\mu(d)\sum_{d|i}\mu(i)\\=\sum_{d|k}\mu(d)\sum_{i=1}^{n/d}\mu(id)\\=\sum_{d|k}\mu(d)\sum_{i=1}^{n/d}\mu(i)\mu(d)[i\bot d]\\=\sum_{d|k}\mu(d)^2\sum_{i=1}^{n/d}\mu(i)[i\bot d]\\=\sum_{d|k}\mu(d)^2S(\lfloor\frac nd\rfloor,d)$

# code

#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define RG register
const int N = 10000000;
inline int gi()
{
RG int x=0,w=1;RG char ch=getchar();
while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if (ch=='-') w=0,ch=getchar();
while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return w?x:-x;
}
bool zhi[N+5];
int pri[N],tot,mu[N+5],s[N+5],f[2005];
map<pair<int,int>,int>M;
inline void Sieve()
{
zhi[1]=true;mu[1]=1;
for (RG int i=2;i<=N;++i)
{
if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
for (RG int j=1;j<=tot&&i*pri[j]<=N;++j)
{
zhi[i*pri[j]]=true;
if (i%pri[j]) mu[i*pri[j]]=-mu[i];
else break;
}
}
for (RG int i=1;i<=N;++i) s[i]=s[i-1]+mu[i];
}
int S(RG int n,RG int k)
{
if (!n) return 0;
if (k==1&&n<=N) return s[n];
if (M[make_pair(n,k)]) return M[make_pair(n,k)];
RG int res=0;
if (k==1)
{
res=1;RG int i=2,j;
while (i<=n)
{
j=n/(n/i);
res-=(j-i+1)*S(n/i,1);
i=j+1;
}
}
else
{
for (RG int i=1;i*i<=k;++i)
if (k%i==0)
{
if (mu[i]) res+=S(n/i,i);
if (i*i!=k&&mu[k/i]) res+=S(n/(k/i),k/i);
}
}
return M[make_pair(n,k)]=res;
}
int gcd(RG int a,RG int b){return b?gcd(b,a%b):a;}
int main()
{
Sieve();
RG int n=gi(),m=gi(),k=gi();
for (RG int i=1;i<=k;++i) f[i]=f[i-1]+(gcd(i,k)==1);
RG int i=1,j,gg,pre=0,cur;RG long long ans=0;
while (i<=n&&i<=m)
{
j=min(n/(n/i),m/(m/i));
gg=(m/i)/k*f[k]+f[(m/i)%k];
cur=S(j,k);
ans+=1ll*(n/i)*gg*(cur-pre);
i=j+1;pre=cur;
}
printf("%lld\n",ans);
return 0;
}

posted @ 2018-01-21 21:47  租酥雨  阅读(272)  评论(0编辑  收藏  举报