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. SQLServer2008 在where条件中使用CASE WHEN

    create table #temp(    id int identity(1,1),    name varchar(20),    startYear int,    startMonth in ...

  2. CSS元素水平垂直居中的方法

    1.  元素水平居中 1.1  设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...

  3. page事件

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  4. jQuery顺序加载图片(初版)

    浏览器加载图片区别: IE:同时加载与渲染 其他:加载完之后再渲染 根据这个差异用jQuery做个实例:按顺序加载一组图片,加载完成后提示. <!DOCTYPE html> <htm ...

  5. 【sicily】 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  6. (转)C#开发微信门户及应用(2)--微信消息的处理和应答

    http://www.cnblogs.com/wuhuacong/p/3614175.html 微信应用如火如荼,很多公司都希望搭上信息快车,这个是一个商机,也是一个技术的方向,因此,有空研究下.学习 ...

  7. freemarker使用map替换字符串中的值

    package demo01; import java.io.IOException;import java.io.OutputStreamWriter;import java.io.StringRe ...

  8. eas之树

    如何设置树的深度(即树总共有几级) // 设置树的深度为3,即树包括0.1.2三级结点 table.getTreeColumn().setDepth(3); 如何设置树的方向 树的方向包括两种:自上向 ...

  9. 【剑指Offer】36、两个链表的第一个公共结点

      题目描述:   输入两个链表,找出它们的第一个公共结点.   解题思路:   本题首先可以很直观的想到蛮力法,即对链表1(假设长度为m)的每一个结点,遍历链表2(假设长度为n),找有没有与其相同的 ...

  10. Codeforces Round #548 (Div. 2) B. Chocolates

    You went to the store, selling