1. 程式人生 > >三行寫出莫比烏斯函數(HDU1695)

三行寫出莫比烏斯函數(HDU1695)

namespace void end pac style def bit long min

莫比烏斯函數是可以在三行內寫出來的

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn=1000000;
 5 int mu[maxn+10],T;
 6 void Mobius(){
 7     for(int d=1,k;d<=maxn;++d)
 8         for(mu[1]=1,k=d<<1;k<=maxn;mu[k]=mu[k]-mu[d],k+=d);
 9     for(int i=1;i<=maxn;++i)mu[i]+=mu[i-1
]; 10 } 11 int main(){ 12 Mobius(); 13 ios::sync_with_stdio(false); 14 cin>>T; 15 for(int kase=1,a,b,c,d,k;kase<=T;++kase){ 16 cin>>a>>b>>c>>d>>k; 17 if(!k){ 18 cout<<"Case "<<kase<<": 0"<<endl;
19 continue; 20 } 21 b/=k;d/=k; 22 if(b>d)b^=d^=b^=d; 23 ll ans1=0,ans2=0; 24 for(int i=1,t;i<=b;i=t+1){ 25 t=min(b/(b/i),d/(d/i)); 26 ans1+=1ll*(mu[t]-mu[i-1])*(b/i)*(d/i); 27 } 28 for(int i=1,t;i<=b;i=t+1){ 29
t=b/(b/i); 30 ans2+=1ll*(mu[t]-mu[i-1])*(b/i)*(b/i); 31 } 32 ans1-=ans2/2; 33 cout<<"Case "<<kase<<": "<<ans1<<endl; 34 } 35 }

三行寫出莫比烏斯函數(HDU1695)