JZYZOJ 1375 双亲数 莫比乌斯反演

http://172.20.6.3/Problem_Show.asp?id=1375

网上搜推理图。

有一段没有写莫比乌斯反演都快忘了。。数学能力--,定理完全不会推,但是这道题整体来说应该是比较好写的(虽然我没写出来)

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm>  
 5 #include<cmath>
 6 using namespace std;
 7 const int maxn=1000010;
 8 long long a,b,d;
 9 long long f[maxn]={},su[maxn]={};
10 long long w[maxn][2]={};
11 long long tot=0,cnt1=0,cnt2=0;
12 bool vis[maxn]={};
13 int main(){
14     scanf("%I64d%I64d%I64d",&a,&b,&d);
15     a/=d,b/=d;
16     if(b<a)swap(a,b);
17     f[1]=1;   int z;
18     for(int i=2;i<=a;i++){
19         if(!vis[i])su[++tot]=i,f[i]=-1;
20         for(int j=1;j<=tot;j++){
21             z=i*su[j];
22             if(z>a)break;
23             vis[z]=1;
24             if(i%su[j]) f[z]=-f[i];
25             else break;
26         }
27     }
28     long long ans=0;
29     for(int i=1;i<=a;i++){
30         ans+=f[i]*(a/i)*(b/i);
31     }
32     printf("%I64d
",ans);
33     return 0;
34 }
View Code
原文地址:https://www.cnblogs.com/137shoebills/p/7788108.html