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. 2017 NAIPC A:Pieces of Parentheses

    my team solve the problem in the contest with similar ideathis is a more deep analysis The main idea ...

  2. leetcode 120. 三角形最小路径和 JAVA

    题目: 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径和 ...

  3. 1002. Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  4. jzoj2941

    我們可以暴力枚舉每一個人分幾個糖果,再暴力統計答案即可 每次遞歸下去可以從1-n號人,決定選多少個糖果再遞歸 #include<bits/stdc++.h> using namespace ...

  5. Python(IO model)

    day34 IO model 举例:https://blog.csdn.net/ZWE7616175/article/details/80591587 参考:http://www.cnblogs.co ...

  6. 二,windows下安装memcached服务

    window下安装memcached服务的流程如下: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c ...

  7. 北大POJ题库使用指南

    原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...

  8. Kafka运行环境优化实践

    Kafka高性能的特点及条件 Kafka是一个高吞吐量分布式消息中间件,并且提供了消息的持久化功能.其高可行有两个重要的特点: 利用了磁盘连续读写性能显著高于随机读写性能的特点 并发,将一个topic ...

  9. 目录打散-hash算法

    前几篇说了文件上传,都是上传到了WebRoot下的up目录,这样是不行的,文件多了性能就不行了.文件一般都是分目录存放的,这里讲建目录的一种算法.先看结果,经过本算法建的目录,结构是这样的,还以up目 ...

  10. javascript闭包使用 分类: JavaScript 2015-05-01 11:34 652人阅读 评论(3) 收藏

    之前看到一段代码,很是不能理解,然后就查找资料并且找网络上得大牛请教,最后弄懂了这段代码,然后就拿出来总结一下. 1.挖坑 先来看一段代码: var arrTest = []; for (var i ...