[ACM] HDU 1695 GCD (Exclusion Principle)

Source: Internet
Author: User

[ACM] HDU 1695 GCD (Exclusion Principle)
GCD


Problem Description Given 5 integers: a, B, c, d, k, you're trying to find x in... b, y in c... d that GCD (x, y) = k. GCD (x, y) means the greatest common divisor of x and y. since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x = 5, y = 7) and (x = 7, y = 5) are considered to be the same.

Yoiu can assume that a = c = 1 in all test cases.

Input The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, B, c, d, k, 0 <a <= B <= 100,000, 0 <c <= d <= 100,000, 0 <= k <= 100,000, as described abve.

Output For each test case, print the number of choices. Use the format in the example.

Sample Input

21 3 1 5 11 11014 1 14409 9

Sample Output
Case 1: 9Case 2: 736427HintFor the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5). 

Source 2008 "Sunline Cup" National Invitational Contest

 

Given a, B, c, d, k, where a = 1, c = 1, ask

How many pairs of gcd (x, y) = k, a <= x <= B c <= y <= d
And () such as a pair, as long as the x

First, narrow the range, and B = B/k, d = d/k
How many pairs of gcd (x, y) = 1 are converted.
Assume that B Then convert d to [1, B], [B + 1, d ].
First obtain the part [1, B], gcd (x, y) = <= x <= B, 1 <= y <= B
You only need to obtain the Euler's function for each number and then accumulate it.
Calculate the part [B + 1, d], gcd (x, y) = 1, 1 <= x <= B, B + 1 <= y <= d
Requires the interconnectivity between x and y. For every y, if x can be divisible by a prime factor of y, then gcd (x, y) is definitely not equal to 1.


Therefore, we first calculate the number of [1, B] and the number of y which are not mutually exclusive, that is, they can be divisible by the prime factor of y, and then subtract this number with B, which is ours.
Number of required gcd (x, y) = 1

When the results are obtained, the concept of rejection is used, plus the elements that can be divisible by one prime factor, minus two, plus three, minus four .....

 

 

# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Include
       
        
# Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                # Define rd (x) scanf ("% d", & x) # define rd2 (x, y) scanf ("% d", & x, & y) # define rd3 (x, y, z) scanf ("% d", & x, & y, & z) using namespace std; typedef long ll; const int maxn = 1002; int prime [maxn + 1]; void getPrime () {memset (prime, 0, sizeof (prime); for (int I = 2; I <= maxn; I ++) {if (! Prime [I]) prime [++ prime [0] = I; for (int j = 1; j <= prime [0] & prime [j] <= maxn/I; j ++) {prime [prime [j] * I] = 1; if (I % prime [j] = 0) break ;}} int factor [100] [2]; int fatcnt; int getFactors (int x) {fatcnt = 0; int tmp = x; for (int I = 1; prime [I] <= tmp/prime [I]; I ++) {factor [fatcnt] [1] = 0; if (tmp % prime [I] = 0) {factor [fatcnt] [0] = prime [I]; while (tmp % prime [I] = 0) {factor [fatcnt] [1] ++; tmp/= prime [I] ;}fatcnt ++ ;}} if (tmp! = 1) {factor [fatcnt] [0] = tmp; factor [fatcnt ++] [1] = 1;} return fatcnt;} const int MAXN = 100010; int euler [MAXN + 1]; void getEuler () {memset (euler, 0, sizeof (euler); euler [1] = 1; for (int I = 2; I <= MAXN; I ++) {if (! Euler [I]) for (int j = I; j <= MAXN; j + = I) {if (! Euler [j]) euler [j] = j; euler [j] = euler [j]/I * (I-1) ;}} int t, cas = 1; int a, B, c, d, k; long ans; int cal (int n, int m) // calculate 1 ~ The number of numbers in n and the m mutual quality {getFactors (m); int ans = 0; for (int s = 1; s <(1 <
               
                 B | k> d) {printf ("Case % d: % d \ n", cas ++, 0); continue;} if (B> d) swap (B, d); B/= k, d/= k; ans = 0; for (int I = 1; I <= B; I ++) ans + = euler [I]; for (int I = B + 1; I <= d; I ++) ans + = cal (B, I ); printf ("Case % d: % I64d \ n", cas ++, 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.