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 }
时间: 2024-12-08 10:12:37

hdu 2824The Euler function的相关文章

杭电2824--The Euler function(欧拉函数)

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4559    Accepted Submission(s): 1902 Problem Description The Euler function phi is an important kind of function in number theo

HDU 2824 The Euler function 题解

求区间的euler数值,自然使用筛子法了. Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful character

HDU——T 2824 The Euler function

http://acm.hdu.edu.cn/showproblem.php?pid=2824 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7752    Accepted Submission(s): 3217 Problem Description The Euler function phi is an important kin

HDU 2824 The Euler function

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3789    Accepted Submission(s): 1569 Problem Description The Euler function phi is an important kind of function in number theo

hdu 2824 The Euler function 欧拉函数打表

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are sm

(hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 166 Accepted Submission(s): 96   Problem Description The Euler function phi is an important kind of function in number theory

HDU——2824 The Euler function

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4507    Accepted Submission(s): 1872 Problem Description The Euler function phi is an important kind of function in number the

hdu2824 The Euler function(欧拉函数个数)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数性质: 1:(百科):http://baike.baidu.com/link?url=r-yneKCCyS9N6bhbQCqiZX0V2OCYq9r7iHSzHTSs03H7qRvu1OfUzlOxfVEs2PmR 2:http://www.cppblog.com/doer-xee/archive/2009

The Euler function(欧拉函数筛)

这题用欧拉函数会超时,要用函数筛. 解析:(转) 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目. 例如:φ(8)=4,因为1,3,5,7均和8互质. 性质:1.若p是质数,φ(p)= p-1. 2.若n是质数p的k次幂,φ(n)=(p-1)*p^(k-1).因为除了p的倍数都与n互质 3.欧拉函数是积性函数,若m,n互质,φ(mn)= φ(m)φ(n). 根据这3条性质我们就可以推出一个整数的欧拉函数的公式.因为一个数总可以写成一些质数的乘积的形式. E(k)=(p1