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. 【Python 2 到 3 系列】 print 是函数

    v3.0 以前,print一直作为语法结构存在,他是python语法的一部分:这个理解起来可能有点蹩脚,但的确是这样. print 一直被定以为一个statement,也就是说,他跟return/tr ...

  2. 词向量1.md

    词向量 我们以句子分类为例,我们使用深度学习模型对句子进行分类,本质上这个模型的接受的舒服需要是数值型.因为文字是人们抽象出来的一个概念,这个 东西是不能被计算机直接理解的,我们需要人为的将这个文字转 ...

  3. manjaro安装teamviewer后无法打开

    点桌面快捷方式一闪而过 命令行运行提示 $ teamviewer /opt/teamviewer/tv_bin/script/tvw_exec:行7: /opt/teamviewer/logfiles ...

  4. DNS无法区域传送(axfr,ixfr)

    这两天博主在学习dns服务器的配 首先简单介绍一下axfr,ixfr axfr:完全区域传送 ixfr :增量区域传送 主要是在dns主从服务器上面进行备份更新的. ----------------- ...

  5. Clion 不能杀死进程

    描述 自己使用时发现点了结束按钮后,打开任务管理器,发现刚才运行的程序还在,并没有被杀死. 有时如果一个程序写了死循环,就会出现疯狂占用内存,最后不得不关机重启. 解决方案 这是他的社区有人也有这样的 ...

  6. java 值传递 和 引用传递

    参考:(http://www.cnblogs.com/woshimrf/p/5263018.html) 参考:(http://www.cnblogs.com/binyue/p/3862276.html ...

  7. django中间件CsrfViewMiddleware源码分析,探究csrf实现

    Django Documentation csrf保护基于以下: 1. 一个CSRF cookie 基于一个随机生成的值,其他网站无法得到.此cookie由CsrfViewMiddleware产生.它 ...

  8. 常见算法用Pascal实现

    基本算法    这些都是非常基本的的算法,希望所有学习的人都能理解!        1.数论算法      求两数的最大公约数      function gcd(a,b:integer):integ ...

  9. Python 实现随机打乱字符串

    from random import shuffle def shuffle_str(s): # 将字符串转换成列表 str_list = list(s) # 调用random模块的shuffle函数 ...

  10. 1、python 循环控制

     案例1: lucky_num = 19 input_num = int(input("Input the guess number:")) if input_num == luc ...