[HDU-1695] GCD inversion template to re-title +

Description

Given 5 integers: a, b, c, d, k, you're to find x in a...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 above.

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

 

Hot foot Chinese meaning of the questions

 Seeking to meet $ a \ leq x \ leq b, c \ leq y \ leq d $, and $ X $ may be equal to $ y $, satisfying $ gcd (i, j) = k $ how much of the $ x $, $ y $.

Thinking

The original meaning of the questions have been met $ a = = 1 $ and $ c = = 1 $.

Templates can be on hand to find $ \ sum_ {i = 1} ^ {n} \ sum_ {j = 1} ^ {m} [gcd (i, j) = k] $.

Might make $ b \ leq d $.

First seek $ \ sum_ {i = 1} ^ {b} \ sum_ {j = 1} ^ {d} [gcd (i, j) = k] $, minus $ \ sum_ {i = 1} ^ { b} \ sum_ {j = 1} ^ {b} [gcd (i, j) = k] $ deduplication.

 

AC Code

 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 const int SIZE=1e6+10;
 5 bool check[SIZE];
 6 int prime[SIZE];
 7 int mu[SIZE];
 8 void Moblus(){
 9     memset(check,false,sizeof(check));
10     mu[1]=1;
11     int tot=0;
12     for(int i=2;i<=SIZE;i++){
13         if(!check[i]){
14             prime[tot++]=i;
15             mu[i]=-1;
16         }
17         for(int j=0;j<tot;j++){
18             if(i*prime[j]>SIZE){
19                 break;
20             }
21             check[i*prime[j]]=true;
22             if(i%prime[j]==0){
23                 mu[i*prime[j]]=0;
24                 break;
25             }
26             else{
27                 mu[i*prime[j]]=-mu[i];
28             }
29         }
30     }
31 }
32 
33 ll sum(int x,int y,int k){
34     ll res=0;
35     int minn=min( floor(x/k) , floor(y/k) ); 
36     for(int d=1;d<=minn;d++){
37         res += (ll)mu[d]*(ll)floor( x/(d*k) )*(ll)floor( y/(d*k) );
38     }
39     return res;
40 }
41 
42 int main(){
43     Moblus();
44     int t;scanf("%d",&t);
45     int kase=1;
46     while(t--){
47         int a,b,c,d,e;scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
48         if(!e){// note that 0 is determined, several hair before Error ...... 
49              the printf ( " Case% D: 0 \ n- " , kase ++); Continue ;
 50          }
 51 is          IF (B> D) the swap (B, D); // not exchange will WA 
52 is          LL ANS1 = SUM (B, D, E);
 53 is          LL ANS2 = SUM (B, B, E);
 54 is          the printf ( " Case% D:% LLD \ n- " , kase ++, ANS1-ANS2 / 2 );
 55      }
 56 }

 

Story

shk001: number theory on today, and on my watch, you and persecute me.

tudouuuuu: Rea1 number of chain split not finished yet.

shk001: Line ⑧, I counted back theory, mathematics, computational geometry, dp, search the pot, how I feel these days is full of pot.

(* Tudouuuuu to Putuo Mountain play)

tudouuuuu: I lost a bet, I wrote 10 inversion, line ⑧!

Guess you like

Origin www.cnblogs.com/tudouuuuu/p/11600308.html