HDU 1695 (number theory, screening + prime factor decomposition + rejection)

Source: Internet
Author: User
Tags greatest common divisor
GCD

Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1323 accepted submission (s): 449

Problem descriptiongiven 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. 

 

Inputthe 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.

 

Outputfor 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: 9 Case 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 ). ideas:

It is not difficult to know the meaning of the question. Given K, X, Y, 1 is equal to <= A <= x 1 <= B <= Y, where gcd (a, B) = K (, b) logarithm. (Note that the number pair is unordered ). 1 <= X, Y <= 10 W, 0 <= k <= 10 W

 

The question is quite disgusting. The data is k = 0. Obviously, the answer is 0, and The gcd with no 2 numbers is 0.

First, gcd is useless. This is because the last two digits of GCD are mutually dependent. So we can make X/= k y/= K and assume that x <= y

Then the question is changed to the number of pairs of the two numbers in the range [1. X] and [1. Y.

General idea:

For enumeration [1. Y], I determines the number of each number in [1. Min (X, I. (Note that the enumerated values are relatively large intervals [1 .. y]).

Obviously, if I is a prime number, then the number of I in [1 .. Min (X, I)] is the total number or I-1. (Depends on the size of x and I ).

When I is not a prime number, the number of times of prime factor after I is decomposed does not affect the result. We can see how many of the other intervals do not interact with I (just reduce it), so we just need to see how many of the other intervals are multiples of the I prime factor.

In the range [1. W], there are obviously W/P multiples of P.

Let's enumerate the principle of I's use of the prime factor:

Check the number of instances in the other interval.

The principle of rejection is as follows:

Number of non-interconnectivity with I in the interval = (the number of multiples of each prime factor of I in the interval)-(a multiple of the product of each two prime factors of I in the interval) + (the number of multiples of each 3 prime factor of I in the interval)-(the product of each 4 prime factors of I in the interval) +...

The problem becomes to count the number of different quality factors for each number and ignore the number of times. This can be screened. The procedure is as follows:

Store a list of real factors for each number. The initial length of each list is 0. Check the list length of each number from 2. If the list length is not 0, the number is a combination and skipped. If the list length is 0, then we find a prime number and add it to the list of multiples of this number (not including itself.

In this way, we have saved the real prime factor list of each number and solved the problem. We also need to use _ int64 for the result.

# Include <iostream> using namespace STD; # define n 100005 # define ll _ int64ll eule [N]; int P [N] [15]; int num [N]; void Init () {int I, j; eule [1] = 1; for (I = 2; I <n; I ++) // obtain the prime factor of the number and the Euler's function value of each number by using the distinct method {If (! Eule [I]) {for (j = I; j <n; j + = I) {If (! Eule [J]) eule [J] = J; eule [J] = eule [J] * (I-1)/I; P [J] [num [J] ++] = I;} eule [I] + = eule [I-1];} ll DFS (INT X, int Max, int q) // calculate the number of non-interconnectivity between Q and max {int I; ll res = 0; for (I = x; I <num [Q]; I ++) {res + = max/P [Q] [I]-DFS (I + 1, max/P [Q] [I], q);} return res ;} int main () {int t, a, B, Max, Min, K, flag, I ;__ int64 res; Init (); scanf ("% d ", & T); flag = 0; while (t --) {flag ++; scanf ("% d", & A, &, & B, & B, & K); If (k = 0) {printf ("case % d: 0/N", flag); continue;} max = A> B? A: B; min = A> B? B: A; max/= K; min/= K; Res = eule [Min]; for (I = min + 1; I <= max; I ++) {res + = MIN-DFS (0, Min, I);} printf ("case % d: % i64d/N", flag, Res);} 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.