Advertisement
dmkozyrev

solve.cpp

May 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #pragma GCC diagnostic ignored "-Wunused-result"
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. typedef long long Int;
  6.  
  7. Int gcd(Int a, Int b) {
  8.     return b == 0 ? a : gcd(b, a % b);
  9. }
  10.  
  11. bool is_finite(Int p, Int q, Int b) {
  12.     if (p == 0) {
  13.         return true;
  14.     }
  15.     Int d = ::gcd(p, q);
  16.    
  17.     p /= d;
  18.     q /= d;
  19.    
  20.     if (q == 1) {
  21.         return true;
  22.     }
  23.    
  24.     p %= q;
  25.     b %= q;
  26.    
  27.     return gcd(b, q) == 1 ? false : true;    
  28. }
  29.  
  30. int main() {
  31.     std::ios_base::sync_with_stdio(false);
  32.     std::cin.tie(0); std::cout.tie(0);
  33.    
  34.     int nQueries;
  35.     std::cin >> nQueries;
  36.    
  37.     while (nQueries--) {
  38.         Int p, q, b; std::cin >> p >> q >> b;
  39.         std::cout << (is_finite(p, q, b) ? "Finite" : "Infinite") << "\n";
  40.     }
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement