The Euler function

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

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
 
第一种打表的方法是,素数和欧拉,分开来打表。250ms
第二种打表只有一个,但是时间上更多。500ms
 
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; int prime[],len;
int opl[];
bool s[]; void Getprime() //打素数表
{
int i,j;
len=;
for(i=;i<=;i++)
{
if(s[i]==false)
{
prime[++len]=i;
for(j=i*;j<=;j=j+i)
s[j]=true;
}
}
} void Euler() //欧拉打表。
{
int i,j;
Getprime();
for(i=;i<=;i++)
opl[i]=i;
opl[]=;
for(i=;i<=len;i++)
{
for(j=prime[i];j<=;j=j+prime[i])
opl[j]=opl[j]/prime[i]*(prime[i]-); //利用的定理 }
} int main()
{
int n,m,i;
__int64 num;
Euler();
while(scanf("%d%d",&n,&m)>)
{
num=;
for(i=n;i<=m;i++)
num=num+opl[i];
printf("%I64d\n",num);
}
return ;
}

第二种方法。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; int opl[];
bool s[]; void Euler() //欧拉打表。
{
int i,j;
for(i=;i<=;i++)
opl[i]=i;
opl[]=; for(i=;i<=;i++)
if(s[i]==false)
{
for(j=i;j<=;j=j+i)
{
opl[j]=opl[j]/i*(i-);
s[j]=true;
}
}
} int main()
{
int n,m,i;
__int64 num;
Euler();
while(scanf("%d%d",&n,&m)>)
{
num=;
for(i=n;i<=m;i++)
num=num+opl[i];
printf("%I64d\n",num);
}
return ;
}
 

HDU 2824 The Euler function --------欧拉模板的更多相关文章

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

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

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

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

  3. HDU - 2824 The Euler function 欧拉函数筛 模板

    HDU - 2824 题意: 求[a,b]间的欧拉函数和.这道题卡内存,只能开一个数组. 思路: ϕ(n) = n * (p-1)/p * ... 可利用线性筛法求出所有ϕ(n) . #include ...

  4. hdu 2824 The Euler function

    The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (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(欧拉函数)

    如果打表的话会超内存,我想到了一种方法解决这个问题.题目给出的数据时3000000,我将三百万分成300个数据,将整万的数据存储下来,计算的时候,先计算x和y之间整万的数据,然后再计算零散数据. 想法 ...

  7. 欧拉函数 & 【POJ】2478 Farey Sequence & 【HDU】2824 The Euler function

    http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函 ...

  8. HDU——T 2824 The Euler function

    http://acm.hdu.edu.cn/showproblem.php?pid=2824 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  9. HDU 6322.Problem D. Euler Function -欧拉函数水题(假的数论题 ̄▽ ̄) (2018 Multi-University Training Contest 3 1004)

    6322.Problem D. Euler Function 题意就是找欧拉函数为合数的第n个数是什么. 欧拉函数从1到50打个表,发现规律,然后勇敢的水一下就过了. 官方题解: 代码: //1004 ...

随机推荐

  1. 《Python黑帽子:黑客与渗透测试编程之道》 Web攻击

    Web的套接字函数库:urllib2 一开始以urllib2.py命名脚本,在Sublime Text中运行会出错,纠错后发现是重名了,改过来就好: #!/usr/bin/python #coding ...

  2. django项目 设置session 实现用户登入登出

    一.配置文件 settngs.py中 # 使用django认知系统的登录状态装饰器时,没有登录的话,跳往登录页面后路径是/acount/login 需要重新指定 LOGIN_URL = '/user/ ...

  3. iOS根据图片url获取尺寸

    可以在UIImage的分类中加入下面的代码,并且引入系统的ImageIO.framework /** 根据图片的url获取尺寸 @param URL url @return CGSize */ + ( ...

  4. commons-logging.jar 和 log4j.jar 的关系

    在用springmvc开发项目的时候,在日志管理这一块,我们一般用的都是log4j进行日志管理,但是我们在导入spring相关的jar的时候,都会看到commons-logging.jar包,为什么我 ...

  5. H02-启动hdfs时可能遇到的几个问题

    启动hdfs时,有时候可能会遇到这样几种情况: 1)在主节点上执行start-dfs.sh命令启动hdfs,无法启动datanode. 2)在主节点上行启动hdfs,启动成功了,在各个节点上使用jps ...

  6. 前端必备:FastStoneCapture 和 Licecap &&& mingw c++ 编译执行

    端必备:FastStoneCapture 和 Licecap FastStoneCapture这个软件非常小,只有2M多,并且其功能很强大,包括截图,录制视频,量尺,取色等等,对于前端工程师绝对是必备 ...

  7. 【数组】Search for a Range

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  8. asp.net六种方法刷新页面

    第一: private void Button1_Click( object sender, System.EventArgs e )   {     Response.Redirect( Reque ...

  9. 解决Nginx 504 Gateway Time-out问题

    解决方案:在http里设置FastCGI相关参数,如: worker_processes 1; events { worker_connections 1024; } http { include m ...

  10. 02-python基本数据类型

    python的变量不需要声明, 但变量使用前必须复制, 因为python中所有的内容全部是对象 变量是没有类型的, 有类型的是指向内存对象的类型 a = ' a = 是合法的 此外, python还可 ...