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

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int mxn=;
LL phi[mxn],pr[mxn>>];
int cnt=;
int mark[mxn];
int n;
void euler(){
phi[]=;
for(int i=;i<=mxn;i++){
if(!mark[i])
pr[++cnt]=mark[i]=i,phi[i]=i-;
for(int j=;j<=cnt && pr[j]*i<mxn;j++){
mark[i*pr[j]]=pr[j];
if(mark[i]==pr[j]){
phi[pr[j]*i]=phi[i]*pr[j];
break;
}
else phi[i*pr[j]]=phi[i]*(pr[j]-);
}
}
return;
}
int main(){
int s,t;
euler();
int i;
for(i=;i<=mxn;i++)phi[i]+=phi[i-];
while(scanf("%d%d",&s,&t)!=EOF)printf("%I64d\n",phi[t]-phi[s-]);
return ;
}

解2:AC

 #include <cstdio>
long long n=,phi[],p[],top=;
bool ma[];
void init()
{
phi[]=;
for (int i=;i<=n;++i)
{
if (!ma[i]) ma[i]=true,p[++top]=i,phi[i]=i-;
for (int j=;j<=top&&i*p[j]<=n;++j)
{
ma[i*p[j]]=true;
if (i%p[j]==){phi[i*p[j]]=phi[i]*p[j];break;}
else phi[i*p[j]]=(p[j]-)*phi[i];
}
}
}
int main()
{
int l,r; init();
for (int i=;i<=n;++i) phi[i]+=phi[i-];
while (~scanf("%d%d",&l,&r)) printf("%lld\n",phi[r]-phi[l-]);
}

HDU2824 The Euler function的更多相关文章

  1. hdu2824 The Euler function(欧拉函数个数)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/36426357 题目链接:h ...

  2. HDU2824 The Euler function(欧拉函数)

    题目求φ(a)+φ(a+1)+...+φ(b-1)+φ(b). 用欧拉筛选法O(n)计算出n以内的φ值,存个前缀和即可. φ(p)=p-1(p是质数),小于这个质数且与其互质的个数就是p-1: φ(p ...

  3. The Euler function[HDU2824]

    The Euler functionTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...

  4. The Euler function(欧拉函数)

    The Euler function Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. hdu 2824 The Euler function

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

  7. The Euler function(线性筛欧拉函数)

    /* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体 ...

  8. hdu 2824 The Euler function 欧拉函数打表

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 2824 The Euler function --------欧拉模板

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. 简单的for循环实现九九乘法表

    PHP for 循环 语法 for (init counter; test counter; increment counter) { code to be executed; } 参数: init ...

  2. 寻找物体的凸包 opencv

    凸包的含义: 在二维平面上给定点集,凸包就是将最外层的点连接起来构成的凸多边形.并且这个凸多边形能包含点集中所有的点.OPENCV中: convexHull函数用于寻找图像点集中的凸包.它有六个输入参 ...

  3. [BZOJ2243][SDOI2011]染色(树链剖分)

    [传送门] 树链剖分就行了,注意线段树上颜色的合并 Code #include <cstdio> #include <algorithm> #define N 100010 # ...

  4. [Bzoj2286]消耗战(虚树+DP)

    Description 题目链接 Solution 在虚树上跑DP即可 Code #include <cstdio> #include <algorithm> #include ...

  5. TI C6000 数据存储处理与性能优化

    存储器之于CPU好比仓库之于车间.车间加工过程中的原材料.半成品.成品等均需入出仓库,生产效率再快,如果仓库周转不善,也必然造成生产阻塞.如同仓库需要合理地规划管理一般,数据存储也需要恰当的处理技巧来 ...

  6. MySQL忘记密码怎么重置

    1打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 输入命令  mysqld --skip-grant-tables  回车,此时就跳过了mysql的用户验证.注意输入此命令之后 ...

  7. java线程安全总结 - 1 (转载)

    原文地址:http://www.jameswxx.com/java/java%E7%BA%BF%E7%A8%8B%E5%AE%89%E5%85%A8%E6%80%BB%E7%BB%93/ 最近想将ja ...

  8. git 设置别名 git alias

    git config --global alias.st status git config --global alias.ck checkout git config --global alias. ...

  9. 获取IMSI

    转:http://letsunlockiphone.guru/find-imsi-number/ HOW TO FIND IMSI NUMBER (UPDATED) You probably alre ...

  10. 【Divided Two】cpp

    题目: Divide two integers without using multiplication, division and mod operator. If it is overflow, ...