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)

数学-线性欧拉函数+前缀和

前缀和用long long

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e6+10;
int phi[N],p[N/5];
ll s[N];
bool vis[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int a,b;
    phi[1]=1;s[1]=1;
    int cnt=0;
    for(int i=2;i<N;++i)
    {
    	if(!vis[i]) p[++cnt]=i,phi[i]=i-1;
    	for(int j=1;j<=cnt&&i*p[j]<N;++j)
    	{
    		vis[i*p[j]]=1;
    		if(i%p[j]==0) {phi[i*p[j]]=p[j]*phi[i];break;}
    		phi[i*p[j]]=(p[j]-1)*phi[i];
    	}
    	s[i]=s[i-1]+phi[i];
    }
    while(cin>>a>>b)
    {
    	cout<<s[b]-s[a-1]<<'\n';
    }
    return 0;
}

 posted on 2023-05-19 12:04  ruoye123456  阅读(38)  评论(0)    收藏  举报