标签:des style blog http color os io java strong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2181 Accepted Submission(s): 920
1 //欧拉函数 2 #include <stdio.h> 3 #include <math.h> 4 5 typedef __int64 LL; 6 const int Mod=1000000007; 7 8 LL euler_phi(LL n) 9 { 10 LL m=(LL)sqrt(n*1.0); 11 LL ans=n; 12 for(int i=2;i<=m;i++) if(n%i==0) 13 { 14 ans=ans/i*(i-1); 15 while(n%i==0) n/=i; 16 } 17 if(n>1) ans=ans/n*(n-1); 18 return ans; 19 } 20 21 int main() 22 { 23 LL n; 24 while(~scanf("%I64d",&n),n) 25 { 26 LL phi=euler_phi(n); 27 LL t1=(n*phi/2)%Mod; 28 LL t2=(n*(n+1)/2-n)%Mod; 29 LL ans=(t2-t1+Mod)%Mod; 30 printf("%I64d\n",ans); 31 } 32 return 0; 33 }
1 //容斥原理 2 #include <stdio.h> 3 #include <string.h> 4 #include <math.h> 5 6 typedef __int64 LL; 7 const int Mod=1000000007; 8 const int maxn=100000; 9 bool flag[maxn]; 10 int prime[maxn],num,n; 11 int factor[100],cnt; 12 bool vis[100]; 13 LL ans,temp; 14 15 void get_prime() 16 { 17 num=0;memset(flag,true,sizeof(flag)); 18 for(int i=2;i<maxn;i++) 19 { 20 if(flag[i]) prime[num++]=i; 21 for(int j=0;j<num&&prime[j]*i<maxn;j++) 22 { 23 flag[i*prime[j]]=false; 24 if(i%prime[j]==0) break; 25 } 26 } 27 } 28 29 void get_factor() 30 { 31 cnt=0; 32 int i,top=(int)sqrt(n*1.0),t=n; 33 for(i=0;i<num && prime[i]<=top;i++) 34 { 35 if(t%prime[i]==0) 36 { 37 factor[cnt++]=prime[i]; 38 while(t%prime[i]==0) 39 t/=prime[i]; 40 } 41 } 42 if(t>1) factor[cnt++]=t; 43 } 44 45 void dfs(int now,int top,int start,LL s) 46 { 47 if(now==top) 48 { 49 LL t=(n-1)/s; 50 LL a=((t+1)*t/2)%Mod; 51 LL b=(a*s)%Mod; 52 temp=(temp+b)%Mod; 53 return ; 54 } 55 for(int j=start;j<cnt;j++) 56 { 57 if(!vis[j]) 58 { 59 vis[j]=true; 60 dfs(now+1,top,j+1,s*factor[j]); 61 vis[j]=false; 62 } 63 } 64 return ; 65 } 66 67 void solve() 68 { 69 ans=0; 70 int c=1; 71 for(int i=1;i<=cnt;i++) 72 { 73 memset(vis,false,sizeof(vis)); 74 temp=0;dfs(0,i,0,1); 75 ans=(((ans+c*temp)%Mod)+Mod)%Mod; 76 c=-c; 77 } 78 } 79 80 int main() 81 { 82 get_prime(); 83 while(scanf("%d",&n),n) 84 { 85 get_factor(); 86 solve(); 87 printf("%I64d\n",ans); 88 } 89 return 0; 90 }
标签:des style blog http color os io java strong
原文地址:http://www.cnblogs.com/xiong-/p/3949617.html