The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4518    Accepted Submission(s): 1879

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
 
Source
 
Recommend
gaojie   |   We have carefully selected several similar problems for you:  2818 2825 2817 2822 2821

解析:(转)

定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质的数的数目。

    例如:φ(8)=4,因为1,3,5,7均和8互质。

性质:1.若p是质数,φ(p)=
p-1.

   2.若n是质数p的k次幂,φ(n)=(p-1)*p^(k-1)。因为除了p的倍数都与n互质

   3.欧拉函数是积性函数,若m,n互质,φ(mn)=
φ(m)φ(n).

  根据这3条性质我们就可以推出一个整数的欧拉函数的公式。因为一个数总可以写成一些质数的乘积的形式。

  E(k)=(p1-1)(p2-1)...(pi-1)*(p1^(a1-1))(p2^(a2-1))...(pi^(ai-1))

    = k*(p1-1)(p2-1)...(pi-1)/(p1*p2*...*pi)

    = k*(1-1/p1)*(1-1/p2)...(1-1/pk)

在程序中利用欧拉函数如下性质,可以快速求出欧拉函数的值(a为N的质因素)

  若( N%a
==0&&(N/a)%a
==0)则有:E(N)=
E(N/a)*a;

  若( N%a
==0&&(N/a)%a
!=0)则有:E(N)=
E(N/a)*(a-1);

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=3000010;
int prime[N],isprime[N];
int phi[N];
void get_phi()
{
int i,j,cnt=0;
for(i=2;i<N;i++)
{
if(isprime[i]==0)
{
prime[cnt++]=i;
phi[i]=i-1;
}
for(j=0;j<cnt && i*prime[j]<N;j++)
{ //注意这里,i*prime[j]<N 可换成 prime[j]<=N/i(带等号)
isprime[i*prime[j]]=1;
if(i%prime[j]==0)
phi[i*prime[j]]=phi[i]*prime[j];
else
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
int main()
{
long long sum;
int a,b;
get_phi();
while(~scanf("%d%d",&a,&b))
{
sum=0;
for(int i=a;i<=b;i++)
sum+=phi[i];
cout<<sum<<endl;
}
return 0;
}

The Euler function(hdoj --2824-欧拉函数)的更多相关文章

  1. hdu 2824(欧拉函数)

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

  2. HDU5597/BestCoder Round #66 (div.2) GTW likes function 打表欧拉函数

    GTW likes function      Memory Limit: 131072/131072 K (Java/Others) 问题描述 现在给出下列两个定义: f(x)=f_{0}(x)=\ ...

  3. hdu 2824 欧拉函数 O(nlogn) 和O(n)

    裸题 O(nlogn): #include <cstdio> #include <iostream> #include <algorithm> using name ...

  4. *HDU 1286,2824欧拉函数

    #include<iostream> #include<string> #include<cstdio> #include<cmath> #includ ...

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

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

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

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

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

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

  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 欧拉函数筛 模板

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

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

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

随机推荐

  1. Python yield解析

    Pyhton generators and the yield keyword At a glance,the yield statement is used to define generators ...

  2. 非内置浏览器WebView 调起H5支付,提示商家参数格式有误

    微信H5 支付开发官方文档参考资料: https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4 0. 场景描述:在APP 中使用webVie ...

  3. Eclipse之调试代码和返回

    编写代码时,经常会遇到各种莫名其妙的问题,为了检测程序是哪里出现问题,我们通过断点调试来判断哪一步出错 一.断点 在需要断点的地方,在左侧双击鼠标设置断点,可设置多个 去掉断点:在断点上双击一下,没有 ...

  4. AI:AI是什么?

    古老的哲学对科学有永远的借鉴意义,科学上的咬文嚼字往往会让其丧失完备性. 一.AI是什么 你看起来它有多好,它就有多好.本质只能通过表象来描述,在色即是空的逻辑里,图灵测试也许是最精准的AI测试方式. ...

  5. 【sqli-labs】 less40 GET -Blind based -String -Stacked(GET型基于盲注的堆叠查询字符型注入)

    提交,页面正常,说明是')闭合的 http://192.168.136.128/sqli-labs-master/Less-40/?id=1')%23 http://192.168.136.128/s ...

  6. Git及Github环境搭建(Windows系统)

    一.github账号注册 1.打开网址https://github.com  注册账号: 二.本地安装Git 1.安装包下载地址:链接:https://pan.baidu.com/s/1smpnJL7 ...

  7. Java通过接口实现匿名类的实例

    package com.chase.test; /** * 通过接口实现匿名类的实例 * * @author Chase * * @date 2013-10-18 下午04:28:17 * * @ve ...

  8. python tips:类的绑定方法(bound)和非绑定方法(unbound)

    类属性只有类及其实例能够访问,可以理解为一个独立的命名空间. Python中类属性的引用方式有两种: 1. 通过类的实例进行属性引用,称为绑定方法(bound method),可以理解为方法与实例绑定 ...

  9. day008 字符编码之 字符编码 、Python2和Python3字符编码的区别

    计算机基础(掌握) 启动应用程序的流程 双击qq 操作系统接受指令然后把该操作转化为0和1发送给CPU CPU接受指令然后把指令发送给内存 内存接受指令把指令发送给硬盘获取数据 qq在内存中运行 文本 ...

  10. Codeforces Round #406 (Div. 2) 787-D. Legacy

    Rick and his co-workers have made a new radioactive formula and a lot of bad guys are after them. So ...