Spoj COMDIV - Number of common divisors Solution

  




Problem Link:https://www.spoj.com/problems/COMDIV/

Solution in C++: 

  1. ///La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. ///Abul Hasnat Tonmoy
  4. ///Department of CSE,23rd batch
  5. ///Islamic University,Bangladesh
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. int gcd(long long a, long long b)
  9. {
  10. if (a == 0)
  11. return b;
  12. return gcd(b % a, a);
  13. }
  14.  
  15. int main()
  16. {
  17. ios::sync_with_stdio(false);
  18. cin.tie(0);
  19. long long t,n,i,divisor,a,b;
  20. scanf("%d",&t);
  21. while(t--)
  22. {
  23. scanf("%lld %lld",&a,&b);
  24. n=gcd(a,b);
  25. divisor=0;
  26. for(i=1; i<=sqrt(n); i++)
  27. {
  28. if(i*i==n)
  29. divisor++;
  30. else if(n%i==0)
  31. divisor=divisor+2;
  32. }
  33. printf("%d\n",divisor);
  34. }
  35. }
  36.  
  37.  

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.