hduoj-2824【欧拉函数】

    xiaoxiao2021-03-25  124

    题目链接:点击打开链接

    The Euler function

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6405    Accepted Submission(s): 2691 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

    思路:欧拉函数裸题,注意优化内存,开一个数组就行。

    #include<cstdio> #include<algorithm> #include<cstring> #define LL long long using namespace std; const int MAXN=3e6; int a,b; LL eu[MAXN+10]={0}; void euler() { eu[1]=1; for(int i=2;i<=MAXN;i++) { if(!eu[i]) { for(int j=i;j<=MAXN;j+=i) { if(!eu[j]) eu[j]=j; eu[j]=eu[j]/i*(i-1); } } } for(int i=1;i<=MAXN;i++) eu[i]=eu[i-1]+eu[i]; } int main() { euler(); while(~scanf("%d%d",&a,&b)) { printf("%lld\n",eu[b]-eu[a-1]); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-7650.html

    最新回复(0)