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

题目:

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,因为1357均和8互质。

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

   2)n是质数pk次幂,φ(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)

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

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

  若( N%==0&&(N/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;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值