備戰NOIP——模板複習20
阿新 • • 發佈:2018-12-19
這裡只有模板,並不作講解,僅為路過的各位做一個參考以及用做自己複習的資料,轉載註明出處。
線性篩求尤拉函式
/*Copyright: Copyright (c) 2018 *Created on 2018-11-05 *Author: 十甫 *Version 1.0 *Title: 線性篩求尤拉函式 *Time: inf mins */ #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; const int size = 50050; int phi[size], prime[size]; bool vis[size]; inline void make(int n) { phi[1] = 1; for(int i = 2;i <= n;i++) { if(!vis[i]) { prime[++prime[0]] = i; phi[i] = i - 1; } for(int j = 1;j <= prime[0] && prime[j] * i <= n;j++) { vis[prime[j] * i] = true; if(!(i % prime[j])) { phi[prime[j] * i] = phi[i] * prime[j]; break; } else { phi[prime[j] * i] = phi[i] * phi[prime[j]]; } } } } int main() { int n; scanf("%d", &n); make(n); int m; scanf("%d", &m); while(m--) { int k; scanf("%d", &k); printf("%d\n", phi[k]); } return 0; }