HDU -- 2588 -- Euler's function | refresh Principle

Source: Internet
Author: User

In fact, I think this question is not so easy to think of the Euler's function, but it is easy for you to think of it. It is a bit suitable because of the conditions of the question.

There are contradictions in this sentence...

This is very important because the background is that gcd makes it necessary for you to use the GCD function =-=.

At first, I went astray. Fortunately, I saw that the data was too big... 10E.

We all know that Phi (X) is used to obtain the number of elements that are mutually dependent on X in.

Assuming n> = x and N % x = 0, then gcd (n, x) = x

So if x> = m, can we get ans + = PHI (N/x?

Because the element that interacts with N/X and then X, then it and the GCD of N are X.

This must be of mutual quality. Otherwise, gcd (n, y * X) is> X.

I don't know why you don't think it's just ans + = N/X? What is the Euler's function ..

After calculation, many elements are repeatedly computed.

However, we can use the above method to skillfully make each element that meets the condition be calculated only once, because the condition of a certain element being ++ is gcd (n, k * X) = x this is where the equation is true, not> =

Even if we do this, we may still need to note that I <= N/I is used in for traversal, which can reduce the number of traversal tasks.

In fact, in short, it is to find all the n-grams greater than m and calculate ans + = PHI (N/m ).

I have written two here-one is open array, and the other is not open.

The array size can be roughly estimated.

I calculated 10 ^ 9 = (2*5) ^ 9

It can be expressed as X = A1 ^ P1 * A2 ^ P2 ............ An ^ Pn and A1 A2... An are all prime numbers

Then the approximate number is (P1 + 1) * (P2 + 1 )*...... * (Pn + 1) because for each AI index PI, We Have pi + 1 acquisition method 0, 1, 2 ..... pi, we can combine it to get the formula just now.

So here we can get the number of approx. 10 ^ 9 is 100, so I expanded the Array Storage by 100 times. It turns out that there is enough re or data problems?

 1 #include <iostream> 2 using namespace std; 3  4 const int size = 10010; 5 int fact[size]; 6 void init( int n , int& cnt ) 7 { 8     for( int i = 1 ; i<=n/i ; i++ ) 9     {10         if( n%i == 0 )11         {12             if(i*i==n)13                 fact[cnt++] = i;14             else15             {16                 fact[cnt++] = i;17                 fact[cnt++] = n/i;18             }19         }20     }21 }22 23 int euler( int n )24 {25     int ans = n;26     for( int i = 2 ; i<=n/i ; i++ )27     {28         if( n%i==0 )29         {30             ans = ans / i * (i-1);31             while( n%i==0 )32                 n /= i;33         }34     }35     if(n>1)36         ans = ans / n * (n-1);37     return ans;38 }39 40 int main()41 {42     cin.sync_with_stdio(false);43     int t , n , m , cnt , ans;44     cin >> t;45     while( t-- )46     {47         cin >> n >> m;48         cnt = ans = 0;49         init( n , cnt );50         for( int i = 0 ; i<cnt ; i++ )51         {52             if( fact[i]>=m )53             {54                 ans += euler( n/fact[i] );55             }56         }57         cout << ans << endl;58     }59     return 0;60 }
View code

At the same time, pay attention to the judgment of the special example I * I = n. If you do not pay attention to it, the computation will be repeated. Of course, you can choose to-go to Phi (I) again.

 1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4  5 int euler( int n ) 6 { 7     int ans = n; 8     for( int i = 2 ; i<=n/i ; i++ ) 9     {10         if( n%i==0 )11         {12             ans = ans / i * (i-1);13             while( n%i==0 )14                 n /= i;15         }16     }17     if(n>1)18         ans = ans / n * (n-1);19     return ans;20 }21 22 int main()23 {24     cin.sync_with_stdio(false);25     int t , n , m , ans , num;26     double temp , val;27     cin >> t;28     while( t-- )29     {30         cin >> n >> m;31         ans = 0;32         for( int i = 1 ; i<=n/i ; i++ )33         {34             if( n%i ==0 )35             {36                 if(i>=m)37                     ans += euler(n/i);38                 if(n/i>=m)39                     ans += euler(i);40             }41         }42         temp = sqrt(n*1.0);43         val = temp - floor(temp);44         if( val==0 )45         {46             num = (int)temp;47             if( num>=m && num*m<=n )48                 ans -= euler(num);49         }50         cout << ans << endl;51     }52     return 0;53 }
View code

It is certainly possible to solve the rejection. I will first think about it -.-

 

 

 

Today:

So annoying ....

Too many thoughts...

Cannot escape ....

May understand why loneliness/loneliness is actually a manifestation of helpless panic

 

HDU -- 2588 -- Euler's function | refresh Principle

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.