标签:function eula int MAX 欧拉 isPrime 预处理 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 characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
Output
Output the result of (a)+ (a+1)+....+ (b)
Sample Input
3 100
Sample Output
3042
思路:
先用欧拉预处理和素数筛得到3000000以内每个数的欧拉函数,然后用前缀和算法对eula进行求前缀和的操作。
欧拉函数定义:给定一个数,满足gcd(i,n)==1(1<=i<=n)的个数。
AC代码如下:
#include<iostream>
using namespace std;
#define MAX 3000005
#define ll long long
bool isPrime[MAX];
ll eula[MAX];
void eulaPrime() {
int i, j;
isPrime[1] = 1;
for (i = 1; i < MAX; i++) {
eula[i] = i;
}
for (i = 2; i < MAX; i++) {
if (!isPrime[i]) {
eula[i] = i - 1;
for (j = i + i; j < MAX; j += i) {
isPrime[j] = 1;
eula[j] /= i;
eula[j] *= (i - 1);
}
}
}
}
int main() {
eulaPrime();
int a, b;
for (int i = 1; i < MAX; i++) {
eula[i] += eula[i - 1];
}
while (cin >> a >> b) {
cout << eula[b] - eula[a - 1] << endl;
}
return 0;
}
标签:function,eula,int,MAX,欧拉,isPrime,预处理,Euler
来源: https://blog.csdn.net/weixin_54664477/article/details/121401516
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。