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. pycharm中文乱码问题 总结

    前言: 这几天刚刚开始学习python,然后就安装了pycharm,但是那个中文乱码的问题真是让人心烦,在网上找了好久,都写得好乱,今天终于让我解决了,在这里总结一下经验,希望可以帮到你们 问题:如下 ...

  2. [Codeforces976E]Well played!(贪心)

    [不稳定的传送门] Solution 首先可以证明,hp翻倍的操作一定是在同一个生物上最优 Code #include <cstdio> #include <algorithm> ...

  3. PHP.TP框架下商品项目的优化2-图片优化

    图片存储.上传.显示优化 1.图片路径写进配置文件,当路径有变动时[因业务扩大,服务器存储图片空间不足等],只需修改配置文件,而不用修改代码 2.封装显示.上传.删除函数,实现代码重用 [可类比其他类 ...

  4. [WC2002][洛谷P1578]奶牛浴场

    洛谷题解里那个人可真是话多呢. 题目描述 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建造一个大型浴场.但是John的奶牛有一个奇怪的习惯,每 ...

  5. AD9 设置网络标号作用域

    http://blog.sina.com.cn/s/blog_99c8ec600102uxul.html 1.版本:Altium Designer 10 2.原因:在进行多原理图设计时, 不同原理图之 ...

  6. 1096: [ZJOI2007]仓库建设

    1096: [ZJOI2007]仓库建设 思路 斜率优化. 代码 #include<cstdio> #include<iostream> using namespace std ...

  7. 亲手搭建一个基于Asp.Net WebApi的项目基础框架2

    本篇目的:封装一些抽象类 1::封装日志相关类 2:封装一个Service操作类 3:封装缓存操作类 4:封装其他一些常用Helper 1.1在Framework项目里面建立好相关操作类文件夹,以便于 ...

  8. Eclipse 修改字符集---Eclipse教程第02课

    默认情况下 Eclipse 字符集为 GBK,但现在很多项目采用的是 UTF-8,这是我们就需要设置我们的 Eclipse 开发环境字符集为 UTF-8, 设置步骤如下: 在菜单栏选择 Window ...

  9. Active Directory 域服务 (AD DS) 虚拟化

    TechNet 库 Windows Server Windows Server 2012 R2 和 Windows Server 2012 服务器角色和技术 Active Directory Acti ...

  10. Windows下python 3 pip程序升级异常问题及pip常用命令

    最近在学习,Selenium+Python自动化,在安装selenium包的时候,出现无法安装的情况,并提示Pip有新的版本,我的版本太低了.然后安装系统提示操作,pip升级也出现异常,报错timeo ...