码迷,mamicode.com
首页 > 其他好文 > 详细

hdu2824(欧拉函数)

时间:2014-08-13 18:41:16      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3674    Accepted Submission(s): 1509


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
 
总结:最初,我用sum[i]数组存储1到i的欧拉函数值得和,但是因为开了两个数组,超了空间,本想用此法优化时间的,结果还是要从a循环到b取每个数的欧拉函数的值相加
bubuko.com,布布扣
 1 #include<stdio.h>
 2 #include<string.h>
 3 __int64 euler[3000000];
 4 int main()
 5 {
 6     __int64 ans;
 7     memset(euler,0,sizeof(euler));
 8     euler[1] = 1;
 9     int a,b,i,j;
10     for(i = 2; i <3000000; i++)
11     {
12         if(!euler[i])
13             for(j = i; j <3000000; j += i)
14             {
15                 if(!euler[j])
16                     euler[j] = j;
17                 euler[j] = euler[j]/i*(i-1);
18             }
19     }
20     while(scanf("%d%d",&a,&b)!=EOF)
21     {
22         ans=0;
23         for(i=a; i<=b; i++)
24             ans+=euler[i];
25         printf("%I64d\n",ans);
26     }
27     return 0;
28 }
View Code

 

hdu2824(欧拉函数),布布扣,bubuko.com

hdu2824(欧拉函数)

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/lxm940130740/p/3910589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!