You are given several queries. Each query consists of three integers pp, qq and bb. You need to answer whether the result of p/qp/q in notation with basebb is a finite fraction.
A fraction in notation with base bb 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.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of queries.
Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0≤p≤10180≤p≤1018, 1≤q≤10181≤q≤1018, 2≤b≤10182≤b≤1018). All numbers are given in notation with base 1010.
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
2 6 12 10 4 3 10
Finite Infinite
4 1 1 2 9 36 2 4 12 3 3 5 4
Finite Finite Finite Infinite
612=12=0,510612=12=0,510
43=1,(3)1043=1,(3)10
936=14=0,012936=14=0,012
412=13=0,13
这是一个数论题目,问q/p能不能在b进制下表示出来。首先对小数位的b进制表示要理解清楚。我们以1/3位例子。1/3的三进制是0.1。因为三进制的小数点右第一位表示为三分位,也就是把1分成了三份。而10进制的小数点右第一位就是10分位。这点理解清楚了后,我们再来看题目。首先先把q,p给同分一下。然后q/p要想在b进制下表示出来的必要条件就是q的质因数被b的质因数包括。然后我们通过gcd再进行while循环,知道gcd值为1,也就是无法约的情况下。如果p==1那么可以否则不可以。原因大家可以想做1/p要想在b进制下表示出来。则b内必须含有p分位这个点。所以他们的质因数是包含关系。
不过我的算法不够快。500ms+。快的看人300ms+。
#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 1e9
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 5005;
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
int main(){
IOS;
ll n,q,p,b;
cin>>n;
while(n--){
cin>>q>>p>>b;
ll k = gcd(q,p);
q/=k;
p/=k;
q%=p;
// cout<<q<<' '<<p<<endl;
if(q<=1&&p==1){
cout<<"Finite"<<endl;
continue ;
}
k = gcd(p,b);
while(k!=1){
while(p%k == 0) p/=k;
k = gcd(p,b);
}
if(p==1){
cout<<"Finite"<<endl;
continue ;
}
cout<<"Infinite"<<endl;
}
}