HDU 2588 GCD------オーラ関数変形
8232 ワード
GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 812 Accepted Submission(s): 363
Problem Description
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.
Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
Output
For each test case,output the answer on a single line.
Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 812 Accepted Submission(s): 363
Problem Description
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.
Input
The first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.
Output
For each test case,output the answer on a single line.
Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
1 /*
2 : 1<=X<=N GCD(X,N)>=M.
3
4 :if(n%p==0 && p>=m) gcd(n,p)=p,
5 n p ,
6 n/p 。
7 n/p x1,x2,x3....
8 gcd(n,x1*p)=gcd(n,x2*p)=gcd(n,x3*p)....
9
10 。
11 */
12
13 #include<stdio.h>
14 #include<stdlib.h>
15 #include<string.h>
16
17
18 int Euler(int n)
19 {
20 int i,temp=n;
21 for(i=2;i*i<=n;i++)
22 {
23 if(n%i==0)
24 {
25 while(n%i==0)
26 n=n/i;
27 temp=temp/i*(i-1);
28 }
29 }
30 if(n!=1)
31 temp=temp/n*(n-1);
32 return temp;
33 }
34
35 void make_ini(int n,int m)
36 {
37 int i,sum=0;
38 for(i=1;i*i<=n;i++)//!!
39 {
40 if(n%i==0)
41 {
42 if(i>=m)
43 sum=sum+Euler(n/i);
44 if((n/i)!=i && (n/i)>=m)//!!
45 sum=sum+Euler(i);
46 }
47 }
48 printf("%d
",sum);
49 }
50
51 int main()
52 {
53 int T,n,m;
54 while(scanf("%d",&T)>0)
55 {
56 while(T--)
57 {
58 scanf("%d%d",&n,&m);
59 make_ini(n,m);
60 }
61 }
62 return 0;
63 }