其他分享
首页 > 其他分享> > The Euler function(欧拉函数预处理+素数筛+一维数组前缀和)

The Euler function(欧拉函数预处理+素数筛+一维数组前缀和)

作者:互联网

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