Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

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
 

Source

2009 Multi-University Training Contest 1 - Host by TJU

 

算出300w个范围内的欧拉函数,然后求个前缀和。

然而这题内存限制好小,直接套惯用的模板会MLE。

已经没有什么优化的空间了,只好重构代码。

 

解1:MLE

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #define LL long long
 8 using namespace std;
 9 const int mxn=3000002;
10 LL phi[mxn],pr[mxn>>1];
11 int cnt=0;
12 int mark[mxn];
13 int n;
14 void euler(){
15     phi[1]=1;
16     for(int i=2;i<=mxn;i++){
17         if(!mark[i])
18             pr[++cnt]=mark[i]=i,phi[i]=i-1;
19         for(int j=1;j<=cnt && pr[j]*i<mxn;j++){
20             mark[i*pr[j]]=pr[j];
21             if(mark[i]==pr[j]){
22                 phi[pr[j]*i]=phi[i]*pr[j];
23                 break;
24             }
25             else phi[i*pr[j]]=phi[i]*(pr[j]-1);
26         }
27     }
28     return;
29 }
30 int main(){
31     int s,t;
32     euler();
33     int i;
34     for(i=1;i<=mxn;i++)phi[i]+=phi[i-1];
35     while(scanf("%d%d",&s,&t)!=EOF)printf("%I64d\n",phi[t]-phi[s-1]);
36     return 0;
37 }
View Code

相关文章:

  • 2022-01-10
  • 2021-09-16
  • 2022-12-23
  • 2022-01-14
  • 2021-12-03
  • 2022-03-01
  • 2021-12-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-19
  • 2021-06-23
  • 2022-01-19
  • 2021-10-31
  • 2022-01-25
相关资源
相似解决方案