(Hdu step 7.2.1) The Euler function (Euler function template question -- calculate The sum of phi [a] to phi [B]), hdu7.2.1

Source: Internet
Author: User

(Hdu step 7.2.1) The Euler function (Euler function template question -- calculate The sum of phi [a] to phi [B]), hdu7.2.1

Question:

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


Question Analysis:

Euler's function, simple question. Attackers can solve this problem directly .... The following describes some knowledge about Euler's functions.

1. Definition: for a positive integer n, Phi (n) is a positive integer smaller than or equal to n, and the number of mutual quality with n.

For example, Phi (8) = 4, because 1, 3, 5, 7 and 8 are mutually qualitative.

2. properties: 1) If p is a prime number, Phi (p) = p-1.

2) if n is the k power of prime p, Phi (n) = (p-1) * p ^ (k-1 ). Because all the multiples of p are mutually dependent on n.

3) the Euler's function is a product function. If m, n are mutually qualitative, Phi (mn) = PHI (m) PHI (n ).

Based on these three properties, we can introduce an integer Euler's function formula. Because a number can always be written as the product of some prime numbers.

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)

Using the following functions in the program, you can quickly find the value of the Euler function (a is a qualitative factor of N)

If (N % a = 0 & (N/a) % a = 0), E (N) = E (N/a) *;

If (N % a = 0 & (N/a) %! = 0) then there are: E (N) = E (N/a) * (A-1 );



The Code is as follows:

/** A1.cpp ** Created on: March 19, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 3000001; int phi [maxn];/*** initializes the Euler's array. * phi [8]: indicates from 1 ~ Number of 8-plus elements **/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", & a, & B )! = EOF) {long ans = 0; int I; for (I = a; I <= B; ++ I) {// obtain the sum between phi [a] and phi [B] and ans + = phi [I];} printf ("% lld \ n", ans );} return 0 ;}



The following is a version of TLE:

The only reason for the TLE is that every time you calculate phi [I], it drops phi (). The workload is too large.


/** POJ_2407.cpp ** Created on: July 15, November 19, 2013 * Author: Administrator */# include <iostream> # include <cstdio> # include <cstring> using namespace std; typedef 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 () {// generate the prime number table ll I, j; memset (u, true, true, sizeof (u); for (I = 2; I <= 1000010; ++ I) {if (u [I]) {su [++ nu M] = 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) {// Euler's function, used to calculate the number of integers in the [1, x) that are mutually correlated with 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] % 10000%7ll;} ans = ans * (su [I]-1) % 10000%7ll; if (x = 1) {break ;}} I F (x> 1) {ans = ans * (x-1) % lower limit 7ll;} return ans;} int main () {prepare (); long; long B; while (scanf ("% lld", & a, & B )! = EOF) {long ans = 0; long I; for (I = a; I <= B; ++ I) {ans + = phi (I );} printf ("% lld \ n", ans);} return 0 ;}




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.