Q.) FIND THE NUMBER OF COOMON DIVISOR OF TWO NUMBERS A AND B.
0<=A,B<=10^6
7718. Number of common divisors
Problem code: COMDIV
Input:
3
100000 100000
12 24
747794 238336
Output:
36
6
2
0<=A,B<=10^6
7718. Number of common divisors
Problem code: COMDIV
Input:
3
100000 100000
12 24
747794 238336
Output:
36
6
2
SOLUTION--
#include<stdio.h>
#include<math.h>
int gcd(int a,int b)
{
if(b==0)
return a;
else
gcd(b,a%b);
}
int main()
{
int t,a,b,num,sqt,i,ans;
scanf("%d",&t);
while(t--)
{
ans=0;
scanf("%d %d",&a,&b);
num=gcd(a,b);
sqt=sqrt(num);
for(i=1;i<=sqt;i++)
{
if(num%i==0){
ans+=2;
if(i==num/i)
ans--;
}
}
printf("%d\n",ans);
}
return 0;
}
No comments:
Post a Comment