hdu 2824The Euler function
生活随笔
收集整理的這篇文章主要介紹了
hdu 2824The Euler function
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接
快速求出a到b之間所有數的歐拉函數。
一個一個求肯定是不行的, 我們知道歐拉函數的公式為phi(n) = n*(1-1/i1)*(1-1/i2).......i1, i2為素因子。 那么我們就可以先將每一個數的歐拉函數值預處理出來。
具體看代碼
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(a, 0, sizeof(a)) 8 #define rson m+1, r, rt<<1|1 9 #define mem1(a) memset(a, -1, sizeof(a)) 10 #define mem2(a) memset(a, 0x3f, sizeof(a)) 11 #define rep(i, a, n) for(int i = a; i<n; i++) 12 #define ull unsigned long long 13 typedef pair<int, int> pll; 14 const double PI = acos(-1.0); 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int inf = 1061109567; 18 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 19 const int maxn = 3e6+6;; 20 int f[maxn]; 21 int main() 22 { 23 int t, n, m, cnt; 24 for(int i = 2; i<=maxn; i++) { 25 f[i] = i; 26 } 27 for(int i = 2; i<maxn; i++) { 28 if(f[i] == i) { //如果f[i] == i, 說明這個數是素數 29 for(int j = i; j<maxn; j+=i) { 30 f[j] = f[j]/i*(i-1); //f[j]*(1-1/i) 31 } 32 } 33 } 34 while(~scanf("%d%d", &n, &m)) { 35 ll sum = 0; 36 for(int i = n; i<=m; i++) { 37 sum += f[i]; 38 } 39 cout<<sum<<endl; 40 } 41 }?
轉載于:https://www.cnblogs.com/yohaha/p/5045131.html
總結
以上是生活随笔為你收集整理的hdu 2824The Euler function的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C语言笔记进阶篇】第一章:指针进阶
- 下一篇: 操作系统之进程管理:15、哲学家进餐问题