码迷,mamicode.com
首页 > 其他好文 > 详细

欧拉函数模板

时间:2017-04-06 21:51:10      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:class   style   ret   i++   euler   log   ==   color   ini   

 1 int phi[5*K];
 2 void init(int k)
 3 {
 4     phi[1]=1;
 5     for(int i=2;i<k;i++) if(!phi[i])
 6     for(int j=i;j<k;j+=i)
 7     {
 8         if(!phi[j]) phi[j]=j;
 9         phi[j]=phi[j]/i*(i-1);
10     }
11 }
12 int euler(int x)
13 {
14     int ans=x;
15     for(int i=2;i*i<=x;i++)
16     if(x%i==0)
17     {
18         ans=ans/i*(i-1);
19         while(x%i==0) x/=i;
20     }
21     if(x>1)
22         ans=ans*x/(x-1);
23     return ans;
24 }

 

欧拉函数模板

标签:class   style   ret   i++   euler   log   ==   color   ini   

原文地址:http://www.cnblogs.com/weeping/p/6675581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!