C. Finite or not?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given several queries. Each query consists of three integers p
is a finite fraction.
A fraction in notation with base b
is finite if it contains finite number of numerals after the decimal point. It is also possible that a fraction has zero numerals after the decimal point.
Input
The first line contains a single integer n
) — the number of queries.
Next n
lines contain queries, one per line. Each line contains three integers p, q, and b ( 0≤p≤1018, 1≤q≤1018, 2≤b≤1018). All numbers are given in notation with base 10.
Output
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
Examples
Input
Copy
2 6 12 10 4 3 10
Output
Copy
Finite Infinite
Input
Copy
4 1 1 2 9 36 2 4 12 3 3 5 4
Output
Copy
Finite Finite Finite Infinite
Note
612=12=0,510
43=1,(3)10
936=14=0,012
412=13=0,13
#define happy
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define all(a) (a).begin(),(a).end()
#define pll pair<ll,ll>
#define vi vector<int>
#define pb push_back
const int inf=0x3f3f3f3f;
ll rd(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll gcd(ll a,ll b){
if(!b)return a;
return gcd(b,a%b);
}
bool finite(ll q,ll base){
while(q!=1){
ll x=gcd(q,base);
if(x==1)return false;
//cout<<x<<endl;
while(x<numeric_limits<int32_t>::max()/2)x*=x;
x=gcd(q,x);
q/=x;
}
return true;
}
int main(){
#ifdef happy
freopen("in.txt","r",stdin);
#endif
ll n=rd();
while(n--){
ll q=rd(),p=rd(),b=rd();
ll x=gcd(p,q);
printf("%s\n",finite(p/x,b)?"Finite" : "Infinite");
}
}