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 base bb 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
题意:给你p,q让你判断在b进制下p/q是否为有限小数。
思路:
十进制小数转二进制,
如0.125,小数乘以进制取整数0,小数0.25乘以进制取整数0,小数0.5乘以进制取整数1,且乘到1停止
0.125对应 0.001 即0*(1/2)+0*(1/4)+1*(1/8)=0.125,回看过程,1/q*b*b....每次取整数,最后等于1。
这类似于b对q的约分,所以只要看q的质因子数是否包含在b中。
注意:在此之前要将p/q化为最简分数。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll gcd(ll x,ll y){
ll z;
while(x){
z=y%x;
y=x;
x=z;
}
return y;
}
int main(){
//freopen("data.in","r",stdin);
int n;
scanf("%d",&n);
while(n--){
ll p,q,d;
scanf("%lld%lld%lld",&p,&q,&d);
if(p==0||q==1){
printf("Finite\n");
continue;
}
ll g=gcd(p,q);
p=p/g;
q=q/g;
while(q!=1&&d!=1){
d=gcd(q,d);
q=q/d;
}
if(q==1){
printf("Finite\n");
}else
printf("Infinite\n");
}
return 0;
}