Hdu 1695 synthesis number theory Euler's Function Decomposition principle of matter-factor exclusion print prime number table handsome question explanation

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): 3112 Accepted Submission (s): 1095


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
2
1 3 1 5 1
1 11014 1 14409 9
 

Sample Output
Case 1: 9
Case 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 ).
 
 

Source
2008 "Sunline Cup" National Invitational Contest
 

Recommend
Wangye
[Cpp] view plaincopy
/*
Question:
 
Give you a, B, c, d, k; find such a Team x, y, so that gcd (x, y) = k, and x in [1, B], yε [1, d]: ask how many pairs meet the requirements (x, y ).
 
------------------------------------------------------------------------------
 
 
Idea: gcd (x, y) = k indicates that x and y can be divisible by k, but what can be divisible by k may not be gcd = k, and must satisfy the mutual quality. the problem is converted to 1 ~ A/k and 1 ~ For the mass Logarithm Problem between B/k, you can set a as a small number,
Use y> x to maintain uniqueness (subject requirements, such as [1, 3] = [3, 1]).
 
The following two cases:
 
1. y <= a, then the logarithm is 1 ~ Sum of Euler's functions of a (easy to think)
 
2. y> = a. In this case, the Euler function cannot be used. How can this problem be solved? You can use the refresh principle to convert the values of y and 1 ~ The prime number factor for converting a to y is 1 ~ Number of divisible items in the range (the relationship between prime number decomposition and content rejection ),
 
*/
 
# Include <iostream>
# Include <stdio. h>
# Include <memory. h>
# Include <math. h>
# Include <vector>
Using namespace std;
 
Const int n= 100005;
Typedef long LL;
# Define maxn100005
LL phi [N];
Vector <LL> link [N];
Int vis [1000000 + 5], c;
LL prime [79000];
Void get_prime () // print the prime number table template
{
Int I, j, n, m;
C = 0;
N = 1000000;
M = (int) sqrt (n + 0.5 );
Memset (vis, 0, sizeof (vis ));
For (I = 2; I <= m; I ++)
If (! Vis [I])
{
For (j = I * I; j <= n; j + = I)
Vis [j] = 1;
}
For (I = 2; I <= n; I ++) if (! Vis [I])
Prime [c ++] = I;
}
 
 
Void get_PHI () // The template obtains the number of mutual qualities between n and 1-> n in phi [n].
{
Int I, j;
For (I = 1; I <= maxn; I ++) phi [I] = I;
For (I = 2; I <= maxn; I + = 2) phi [I]/= 2;
For (I = 3; I <= maxn; I + = 2) if (phi [I] = I)
{
For (j = I; j <= maxn; j + = I)
Phi [j] = phi [j]/I * (I-1 );
}
}
 
Void init () // calculate the prime factor of each number, and store the vector
{
LL I, j, k;
For (I = 1; I <N; I ++) // evaluate the prime factor of n is also a template
{
K = I;
For (j = 0; prime [j] * prime [j] <= k; j ++)
{
If (k % prime [j] = 0)
{
Link [I]. push_back (prime [j]);
While (k % prime [j] = 0)
K/= prime [j];
}
If (k = 1) break;
}
If (k> 1) link [I]. push_back (k );
}
}
 
LL make_ans (LL num, LL n) // The number of all numbers in the 1 to num and m quality factors in the n are not mutually qualitative. Note that they are not mutually qualitative.
{
LL ans = 0, tmp, I, j, flag;
For (I = 1; I <(LL) (1 <link [n]. size (); I ++)
{// Use a binary value of to indicate whether the first prime factor is used, for example, m = 3. If the three prime factors are 2, 3, and 5, then when I = 3, the binary value is 011, indicates that 2nd and 3 factors are used.
Tmp = 1, flag = 0;
For (j = 0; j <link [n]. size (); j ++)
If (I & (LL) (1 <j) // determines the number of factors currently used
Flag ++, tmp * = link [n] [j]; // link [n] [j]
If (flag & 1) // refresh principle, odd addition and even reduction
Ans + = num/tmp;
Else
Ans-= num/tmp;
}
Return ans;
}
 
 
Int main ()
{
LL I, a, B, c, d, k, sum, t, zz = 1; // you can use % I64d to input and output longlong data.
Get_prime ();
Get_PHI ();
Init ();
Scanf ("% I64d", & t );
While (t --)
{
Scanf ("% I64d % I64d % I64d % I64d % I64d", & a, & B, & c, & d, & k );
If (k = 0 | k> B | k> d)
{
Printf ("Case % I64d: 0 \ n", zz ++ );
Continue;
}
If (B> d) swap (B, d); // keep d large
B/= k;
D/= k;
Sum = 0;
For (I = 1; I <= B; I ++)
{
Sum + = phi [I];
}
For (I = B + 1; I <= d; I ++)
{
Sum + = B-make_ans (B, I );
}
Printf ("Case % I64d: % I64d \ n", zz ++, sum );
}
 
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.