hdu 1695(莫比乌斯反演)

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12004    Accepted Submission(s): 4531


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

 

Source
题意:在[a,b]里面选一个数x,[c,d]里面选一个数y 使得gcd(x,y)==k,这样的方案有多少个
定义:
f(d)表示gcd(x,y)==k的个数  演化就是gcd(x/k,y/k)==1的个数
F(n)表示gcd(x/k,y/k)==n(t=1,2,3,4,5,...)

可以通过莫比乌斯反演变化为    

F(k)=(x/k)*(y/k)

 很明显  根据公式来看  F(d)是已知的,
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cctype>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<vector>
11 #include<algorithm>
12 #include<string>
13 #define ll long long
14 #define eps 1e-10
15 #define LL unsigned long long
16 using namespace std;
17 const int INF=0x3f3f3f3f;
18 const int N=1000000+100;
19 const int mod=998244353;
20 ll mu[N];
21 void getmu(){
22     ll flag=0;
23     for(int i=1;i<N;i++){
24         if(i==1)flag=1;
25         else{
26             flag=0;
27         }
28         ll t=flag-mu[i];
29         mu[i]=t;
30         for(int j=2*i;j<N;j=j+i){
31             mu[j]=mu[j]+t;
32         }
33     }
34 }
35 int main(){
36     int t;
37     getmu();
38     //for(int i=1;i<=10;i++)cout<<mu[i]<<" ";
39     //cout<<endl;
40     scanf("%d",&t);
41     int a,b,c,d,k;
42     int Case=1;
43     while(t--){
44         int flag=0;
45         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
46         if(k==0){
47             printf("Case %d: ",Case++);
48             cout<<0<<endl;
49             continue;
50         }
51         b=b/k;
52         d=d/k;
53         ll ans=0;
54         int minn=min(b,d);
55         for(int i=1;i<=minn;i++){
56             ans=ans+mu[i]*(b/i)*(d/i);
57         }
58         ll ans1=0;
59         for(int i=1;i<=minn;i++){
60             ans1=ans1+(mu[i]*(minn/i)*(minn/i));
61         }
62         printf("Case %d: ",Case++);
63         cout<<ans-ans1/2<<endl;
64     }
65 
66 }

 

posted on 2017-08-04 23:29  见字如面  阅读(178)  评论(0编辑  收藏  举报

导航