题目:

The Euler function

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 166 Accepted Submission(s): 96
 
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
2009 Multi-University Training Contest 1 - Host by TJU
 
Recommend
gaojie
 

题目分析:

欧拉函数,简单题。

直接暴力这道题就能过。。。。下面简介一下欧拉函数的一些知识。

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

    比如:φ(8)=4。由于1。3,5,7均和8互质。

2、性质: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);

代码例如以下:

/*
* a1.cpp
*
* Created on: 2015年3月19日
* Author: Administrator
*/ #include <iostream>
#include <cstdio> using namespace std; const int maxn = 3000001; int phi[maxn]; /**
* 初始化欧拉数组.
* phi[8]: 表示从1~8与8互质的元素的个数
*
*/
void prepare(){
int i;
for(i = 1 ; i < maxn ; ++i){
phi[i] = i;
} int j;
for(i = 2 ; i < maxn ; ++i){
if(phi[i] == i){
for(j = i ; j < maxn ; j += i){
phi[j] = phi[j]/i*(i-1);
}
}
}
} int main(){
prepare();
int a,b;
while(scanf("%d%d",&a,&b)!=EOF){
long long ans = 0;
int i;
for(i = a ; i <= b ; ++i){//暴力求phi[a]到phi[b]之间的和
ans += phi[i];
} printf("%lld\n",ans);
} return 0;
}

下面贴一个TLE了的版本号:

TLE的原因非常仅仅管,由于每次算phi[i],它都掉了一次phi()。运算量太大。

/*
* POJ_2407.cpp
*
* Created on: 2013年11月19日
* Author: Administrator
*/ #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; typedef long long ll; const int maxn = 1000015; bool u[maxn];
ll su[maxn];
ll num; ll gcd(ll a, ll b) {
if (b == 0) {
return a;
} return gcd(b, a % b);
} void prepare() {//欧拉筛法产生素数表
ll i, j;
memset(u, true, sizeof(u)); for (i = 2; i <= 1000010; ++i) {
if (u[i]) {
su[++num] = i;
} for (j = 1; j <= num; ++j) {
if (i * su[j] > 1000010) {
break;
} u[i * su[j]] = false; if (i % su[j] == 0) {
break;
}
}
}
} ll phi(ll x) {//欧拉函数,用于求[1,x)中与x互质的整数的个数
ll ans = 1;
int i, j, k;
for (i = 1; i <= num; ++i) {
if (x % su[i] == 0) {
j = 0;
while (x % su[i] == 0) {
++j;
x /= su[i];
} for (k = 1; k < j; ++k) {
ans = ans * su[i] % 1000000007ll;
}
ans = ans * (su[i] - 1) % 1000000007ll;
if (x == 1) {
break;
}
}
} if (x > 1) {
ans = ans * (x - 1) % 1000000007ll;
} return ans;
} int main(){ prepare(); long long a;
long long b;
while(scanf("%lld%lld",&a,&b)!=EOF){
long long ans = 0;
long long i;
for(i = a ; i <= b ; ++i){
ans += phi(i);
} printf("%lld\n",ans);
} return 0;
}

(hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)的更多相关文章

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

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

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

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

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

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

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

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

  5. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  6. HDU 5597 GTW likes function 欧拉函数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5597 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  7. hdu-5597 GTW likes function(欧拉函数+找规律)

    题目链接: GTW likes function Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 131072/131072 K (J ...

  8. hdu 1286:找新朋友(数论,欧拉函数)

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. Euler:欧拉函数&素数筛

    一.欧拉函数 欧拉函数是小于x的整数中与x互质的数的个数,一般用φ(x)表示. 通式:   其中p1, p2……pn为x的所有质因数,x是不为0的整数. 比如x=12,拆成质因数为12=2*2*3, ...

随机推荐

  1. schema文件及XML文件的DOM和Sax解析

    schema文件 <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="htt ...

  2. mysql 将时间转换成时间戳

    select UNIX_TIMESTAMP(addtime/*date_column*/) from tablename 输出:1548658912 数据库原格式:2019-01-28 15:01:2 ...

  3. VBA 中Dim含义

    楼主是个初学者,在应用vba时遇到了dim方面的问题,查了很多资料后想把关于dim的这点儿知识简单整理出来 首先,从我遇到的问题作为切入点吧, (不得不承认我遇到的错误是很低级的) 具体的情境就不还原 ...

  4. set集合关于set与set进行比较

    containsAll方法用来判断Set集合是否包含另一个集合中的全部内容. 语法  boolean containsAll(Collection<?> c) 返回值:如果Set集合包含参 ...

  5. C实现JAVA类似ArrayList的静态链接库

    文件结构: ArrayList.h:头文件 ArrayList.c:实现头文件中的功能 ArrayListTest.c:测试 ArrayList.h 头文件的基本框架: #ifndef _ARRAYL ...

  6. 改造vue-quill-editor: 结合element-ui上传图片到服务器

    前排提示:现在可以直接使用封装好的插件vue-quill-editor-upload 需求概述 vue-quill-editor是我们再使用vue框架的时候常用的一个富文本编辑器,在进行富文本编辑的时 ...

  7. 【codeforces 367C】Sereja and the Arrangement of Numbers

    [题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...

  8. hdu3468 Treasure Hunting 二分匹配

    //给一个n*m的图 //.表示空白地 //*表示有黄金 //#表示墙 //一个人须要依照A...Z..a..z的顺序以最短路径走到下一个 //每次仅仅能在他的路线上经过的地方取一块黄金 //问最多能 ...

  9. Could not load the FreeMarker template named &#39;select&#39;

    眼下项目使用struts2, 所以页面中就使用到了struts2的标签,可是今天在做新的功能的时候突然出现 "Could not load the FreeMarker template n ...

  10. nginx 代理https后,应用redirect https变成http --转

    原文地址:http://blog.sina.com.cn/s/blog_56d8ea900101hlhv.html 情况说明nginx配置https,tomcat正常http接受nginx转发.ngi ...