Bit1055the Euler function

Source: Internet
Author: User

Question:

For a and B

F [a] + F [A + 1] +... f [B]

F is the corresponding Euler's function value.

Solution:

Linear screening method, because Euler's function is a product function

Use the screening method to calculate the Euler's function value

If P has I, then (p * I) = (p-1) * Then (I)
If p | I, then (p * I) = p * Then (I)

Calculate the Euler's function value of all numbers in 1 to 10 ^ 6 based on the two formulas above by screening.

Then use the ANS array to represent

Ans [I] = f [1] + F [2] +... f [ANS]

When A and B are input, the result is

Ans [B]-ans [a] + F [A]

# Include <iostream> # include <algorithm> # include <cstdio> using namespace STD; # define Max 1000100 long f [Max]; // The Void Init () function value () // sieve out the Euler's function values of all numbers within max {memset (F, 0, sizeof (f); F [1] = 1; for (long I = 2; I <Max; I ++) {If (F [I] = 0) // I is a prime number {f [I] = I-1; For (long j = 1; J * I <Max; j ++) // If P has I, then (p * I) = (p-1) * Then (I) // if p | I, percentile (p * I) = p * percentile (I) // P is a prime number, And I is a multiple {If (J % I = 0) {f [I * j] = I * f [J];} else {f [I * j] = (I-1) * f [J] ;}}} long long ans [Max]; int Ma In () {Init (); long a, B; ans [1] = f [1]; for (long I = 2; I <Max; I ++) {ans [I] = ans [I-1] + F [I];} while (~ Scanf ("% d", & A, & B) {printf ("% LLD \ n ", ans [B]-ans [a] + F [a]);} 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.