P2158 [SDOI2008]仪仗队 线性筛(欧拉函数和素数表)

摘要:
vis[i]){44prime[tot++]=i;45phi[i]=i-1;46}47for(j=0;jN)break;49vis[i*prime[j]=true;50if(i%prime[j]==0){51phi[i*primi[j]=phi[i]*prime[j];52break;53}54else{55phi[i*preme[j]=phi[i]*(prime[j-1);56}57}58}59}6061最小(){62intn;63LLans;64ans=0;65scanf(“%d”,&N);66p_pri();67表示(inti=1;i˂N;i++){68ans+=phi[i];69}70cout˂˂ans*2+1˂endl;71返回0;72}

上三角行恰好是[1,n-1]的欧拉函数

http://www.luogu.org/problem/show?pid=2158#sub

 1 //#pragma comment(linker, "/STACK:167772160")
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <iostream>
 6 #include <queue>
 7 #include <stack>
 8 #include <cmath>
 9 #include <set>
10 #include <utility>
11 #include <algorithm>
12 #include <vector>
13 #include <map>
14 // #include<malloc.h>
15 using namespace std;
16 #define clc(a,b) memset(a,b,sizeof(a))
17 #define LL long long
18 void fre() {
19     freopen("in.txt","r",stdin);
20 }
21 const int inf = 0x3f3f3f3f;
22 #define eps 1e-8
23 // const double pi = acos(-1);
24 const LL mod = 1e9+7;
25 inline int r(){
26     int x=0,f=1;char ch=getchar();
27     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
28     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
29     return x*f;
30 }
31 const int maxn=1e7;
32 const int N=40000;
33 bool vis[maxn];
34 int phi[maxn];
35 int prime[maxn];
36 int tot;
37 void p_pri(){
38    // clc(vis,0);
39    phi[1]=1;
40    tot=0;
41    int i,j;
42    for(i=2;i<=N;i++){
43       if(!vis[i]){
44           prime[tot++]=i;
45           phi[i]=i-1;
46       }
47       for(j=0;j<tot;j++){
48          if(i*prime[j]>N) break;
49          vis[i*prime[j]]=true;
50          if(i%prime[j]==0){
51          phi[i*prime[j]]=phi[i]*prime[j];
52          break;
53          }
54          else{
55             phi[i*prime[j]]=phi[i]*(prime[j]-1);
56          }   
57       }
58    }
59 }
60 
61 int main(){
62     int n;
63     LL ans;
64     ans=0;
65     scanf("%d",&n);
66     p_pri();
67     for(int i=1;i<n;i++){
68         ans+=phi[i];
69     }
70     cout<<ans*2+1<<endl;
71     return 0;
72 }

免责声明:文章转载自《P2158 [SDOI2008]仪仗队 线性筛(欧拉函数和素数表)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python(元组)PowerShell中一个分号引发的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

51nod 1136 欧拉函数【数论】

1136 欧拉函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。此函数以其首名研究者欧拉命名,它又称为Euler's totient function、φ函数、欧拉商数等。例如:φ(8) = 4(Phi(8) = 4),因为1...

洛谷网课数论

老久以前的了,以前忘放上面了(差点丢了/jk) 目录 欧拉筛素数 同余 辗转相除法证明 exgcd 中国剩余定理 扩展CRT 乘法逆元 欧拉函数 康托展开 卢卡斯定理/Lucas 定理 欧拉筛素数 (O(n))筛法 for(int i = 2; i <= n; i++) { if(vis[i] == 0) pre[tot++] =...