HDU 2824 the Euler function (Euler functions)

Source: Internet
Author: User

Title Link: hdu 2824 the Euler function

Test instructions

Let you ask for a range of Euler function values.

Exercises

Go directly to the board.

Derivation process:

Definition: For a positive integer n, φ (n) is a positive integer less than or equal to N and the number of n coprime.

For example: φ (8)=4, because 1,3,5,7 are and 8 coprime.

Property:1. If p is prime, φ (p)= P-1.

2.n is prime p kn = (p- 1p^ (k- 1). Because in addition to pn coprime

   3. Euler functions are integrable functions, if m,n coprime, φ (mn)=φ (m) φ (n).

According to these 3 properties We can introduce the formula of an integer Euler function. Because a number can always be written in the form of the product of some prime numbers.

e (k= (p1-1 p2-1pi-1) * (p1 ^ (a1-1p2^ (a2-1 pi^ (ai-1 /span>

= k* (p1-< Span class= "lit" >1p2-1 pi-1)/(p1 *p2*...*pi)

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

In the program, the Euler function can be quickly obtained by using the following properties (the quality factor of A is N ) .

( n%a  ==0&& (n/a)%a == 0) There are: e ( n) = e ( n/a) *a

If ( n%a = =0&&(n/a)% A! =0) there are: e(n)= e(n/a) * (a-1);

1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;++i)3 using namespacestd;4 5 Const intn=3e6+7;6 intPrime[n],phi[n];7 BOOLVis[n];8 voidPHI (intNintCnt=0)//O (N) preprocessing of 1 to n Euler functions9 {  TenF (I,2, N) One     {   A         if(!vis[i]) prime[++cnt]=i,phi[i]=i-1;  -          for(intj=1,k;j<=cnt&& (K=i*prime[j]) <=n;j++)   -         if(vis[k]=1, i%prime[j]==0) {Phi[k]=phi[i]*prime[j]; Break;} the         Elsephi[k]=phi[i]* (prime[j]-1); -     }   - } -  + intMain () - { +     intb; APHI (n7); at      while(~SCANF ("%d%d",&a,&b)) -     { -         Long Longans=0; -F (i,a,b) ans+=Phi[i]; -printf"%lld\n", ans); -     } in     return 0; -}
View Code

HDU 2824 the Euler function (Euler functions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.