(hdu step 7.2.1)The Euler function(歐拉函數模板題——求phi[a]到phi[b]的和),hdu7.2.1

來源:互聯網
上載者:User

(hdu step 7.2.1)The Euler function(歐拉函數模板題——求phi[a]到phi[b]的和),hdu7.2.1

題目:

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 DescriptionThe 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) 
InputThere 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
 
 
Source2009 Multi-University Training Contest 1 - Host by TJU 
Recommendgaojie 


題目分析:

               歐拉函數,簡單題。直接暴力這道題就能過。。。。以下簡單介紹一下歐拉函數的一些知識。

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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.