HDU2824 The Euler function
2016-07-11 20:57
316 查看
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 100Sample Output
3042Source
2009 Multi-University Training Contest 1 - Host by TJU
算出300w个范围内的欧拉函数,然后求个前缀和。
然而这题内存限制好小,直接套惯用的模板会MLE。
已经没有什么优化的空间了,只好重构代码。
解1:MLE
#include <cstdio> long long n=3000000,phi[3000001],p[1500001],top=0; bool ma[3000001]; void init() { phi[1]=1; for (int i=2;i<=n;++i) { if (!ma[i]) ma[i]=true,p[++top]=i,phi[i]=i-1; for (int j=1;j<=top&&i*p[j]<=n;++j) { ma[i*p[j]]=true; if (i%p[j]==0){phi[i*p[j]]=phi[i]*p[j];break;} else phi[i*p[j]]=(p[j]-1)*phi[i]; } } } int main() { int l,r; init(); for (int i=1;i<=n;++i) phi[i]+=phi[i-1]; while (~scanf("%d%d",&l,&r)) printf("%lld\n",phi[r]-phi[l-1]); }View Code
相关文章推荐
- android进度条
- scala学习笔记(9): 语法续
- (4.2.32.3)android热修复之Andfix方式:Andfix的初步使用
- HDU 1203 I NEED A OFFER!
- 十进制转换为其他进制数
- 【Unity Shaders】法线纹理(Normal Mapping)的实现细节
- 工作中任务管理的四个原则和四个技能
- 第9讲 进程与服务
- android关于版本更新问题
- 密码学_RSA算法原理详解
- FPGA笔记(九)-实现ROM步骤
- PHP Mail() 函数最终解决办法
- Tomcat异常页面处理
- POJ 1087 A Plug for UNIX(网络流)
- Node.js 适应场景
- 优先级队列
- 自定义ViewGroup
- Adobe Photoshop cc 2015.5 V17.0安装与破解完全教程
- ie内核相关网址记录
- 正向代理与反向代理