Hdu1695 -- GCD (Euler's Function + refresh principle)

Source: Internet
Author: User
Tags greatest common divisor

Hdu1695 -- GCD (Euler's Function + refresh principle)

GCD Time Limit:3000 MS Memory Limit:32768KB 64bit IO Format:% I64d & % I64uSubmit StatusAppoint description: System Crawler)

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: 736427 

Hint

For 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).         


Calculate the logarithm of x, y, gcd (x, y) = k in 1 to B. The binary groups are unordered and do not need to be repeated.

The maximum common divisor of x and y is k, that is to say, x and y are multiples of k. B/= k, d/= k to get a new interval, we need to find the reciprocal logarithm of each other in the new area, which requires no repetition. Therefore, we need to make the numbers larger than B, the numbers smaller than d, and traverse from 1 to B --> I, when I is less than or equal to d, ans Add the Euler's function value of I. When I is greater than d, calculate the number of interclasses between 1 and d and accumulate the final result.

To prevent time-out, the Euler's function value must be processed in advance, and a number decomposition must also be preprocessed.



# Include
 
  
# Include
  
   
# Include using namespace std; # define LL _ int64 # define maxn 111_ll euler [maxn]; // record euler's functions from 1 to I and LL p [maxn] [30], p_num [maxn]; // number of quality factors void sieve () {LL I, j; memset (p_num, 0, sizeof (p_num); memset (p, 0, sizeof (p); memset (euler, 0, sizeof (euler); for (I = 1; I <maxn; I ++) euler [I] = I; for (I = 2; I <maxn; I ++) {if (euler [I] = I) {euler [I] = I-1; // Yes = prime number p [I] [p_num [I] ++] = I; for (j = 2 * I; j <maxn; j + = I) {p [j] [p_num [j] + +] = I; euler [j] = euler [j] * (I-1)/I ;}} euler [I] + = euler [I-1];} int cop (int n, int m) {int I, j, num, temp, x = 1 <
   
    
B? A: B;} int min (int a, int B) {return a> B? B: a;} LL f (int a, int B) {int n = max (a, B), m = min (a, B ); LL num = euler [m]; for (int I = m + 1; I <= n; I ++) num + = cop (m, I); return num ;} int main () {int a, B, c, d, k; int t, tt; sieve (); scanf ("% d", & t ); for (tt = 1; tt <= t; tt ++) {scanf ("% d", & a, & B, & c, & d, & k); if (k = 0 | k> B | k> d) {printf ("Case % d: 0 \ n", tt ); continue;} printf ("Case % d: % I64d \ n", tt, f (B/k, d/k);} return 0 ;}
   
  
 


Related Article

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.