欧拉函数线性筛(模板)

☆樱花仙子☆ 提交于 2019-12-05 08:37:53

 

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr strcat
 13 #include <string>
 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 #include <cassert>
 21 #include <iomanip>
 22 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 23 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 24 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 25 //******************
 26 clock_t __START,__END;
 27 double __TOTALTIME;
 28 void _MS(){__START=clock();}
 29 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
 30 //***********************
 31 #define rint register int
 32 #define fo(a,b,c) for(rint a=b;a<=c;++a)
 33 #define fr(a,b,c) for(rint a=b;a>=c;--a)
 34 #define mem(a,b) memset(a,b,sizeof(a))
 35 #define pr printf
 36 #define sc scanf
 37 #define ls rt<<1
 38 #define rs rt<<1|1
 39 typedef pair<int,int> PII;
 40 typedef vector<int> VI;
 41 typedef long long ll;
 42 const double E=2.718281828;
 43 const double PI=acos(-1.0);
 44 const ll INF=(1LL<<60);
 45 const int inf=(1<<30);
 46 const double ESP=1e-9;
 47 const int mod=(int)1e9+7;
 48 const int N=(int)3e6+10;
 49 
 50 int prime[N];
 51 ll phi[N];
 52 bool unprime[N];
 53 
 54 ///O(NloglogN),推荐
 55 void phi_table()
 56 {
 57     int i, j;
 58     for (i = 2; i < N; ++i)
 59         if (!phi[i])
 60             for (j = i; j < N; j += i)
 61             {
 62                 if (!phi[j]) phi[j] = j;
 63                 phi[j] -= phi[j] / i; ///简化后的代码
 64             }
 65 }
 66 
 67 ///O(N)
 68 void linear_phi_table2()
 69 {
 70     int i, j, k = 0;
 71     for (i = 2; i < N; i++)
 72     {
 73         if (!unprime[i]) ///若i为素数,phi(i)=i-1
 74         {
 75             prime[k++] = i;
 76             phi[i] = i - 1;
 77         }
 78         for (j = 0; j < k && prime[j] * i < N; j++)
 79         {
 80             unprime[prime[j] * i] = true;
 81             if (i % prime[j]) ///若i和p互素,则phi(i*p) = phi(i) * phi(p) = phi(i) * (p-1)
 82                 phi[prime[j] * i] = phi[i] * (prime[j] - 1);
 83             else
 84             {
 85                 ///此时有i=kp,则
 86                 ///phi(p*kp) = phi(k*p^2) = p*phi(kp)
 87                 phi[prime[j] * i] = phi[i] * prime[j];
 88                 break;
 89             }
 90         }
 91     }
 92 }
 93 
 94 int main()
 95 {
 96     int a,b;
 97     linear_phi_table2();
 98     for(int i=2;i<=3000010;++i)
 99         phi[i]+=phi[i-1];
100     while(~sc("%d%d",&a,&b))
101     {
102         pr("%lld\n",phi[b]-phi[a-1]);
103     }
104     return 0;
105 }
106 
107 /**************************************************************************************/

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!