原题链接:http://codeforces.com/contest/984/problem/C
Finite or not?
You are given several queries. Each query consists of three integers p,q p , q and b b . You need to answer whether the result of in notation with base b b is a finite fraction.
A fraction in notation with base 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(1≤n≤105) n ( 1 ≤ n ≤ 10 5 ) — the number of queries.
Next n n lines contain queries, one per line. Each line contains three integers , and b(0≤p≤1018,1≤q≤1018,2≤b≤1018) b ( 0 ≤ p ≤ 10 18 , 1 ≤ q ≤ 10 18 , 2 ≤ b ≤ 10 18 ) . All numbers are given in notation with base 10 10 .
Output
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
Examples
input
2
6 12 10
4 3 10
output
Finite
Infinite
input
4
1 1 2
9 36 2
4 12 3
3 5 4
output
Finite
Finite
Finite
Infinite
Note
612=12=0,510 6 12 = 1 2 = 0 , 5 10
43=1,(3)10 4 3 = 1 , ( 3 ) 10
936=14=0,012 9 36 = 1 4 = 0 , 01 2
412=13=0,13 4 12 = 1 3 = 0 , 1 3
题解
先约一波分,如果 q q 的质因数都是的因数,那么 pq p q 在 b b 进制下就是有限的。
但是值域是,直接分解质因数肯定血 T T ,所以我们每次直接除以就可以将 q q 和共有的质因子除掉,我们这样一直除下去直到 gcd(b,q)=1 g c d ( b , q ) = 1 或 q=1 q = 1 时就可以判定了,如果 q=1 q = 1 输出 Finite F i n i t e ,否则输出 Infinite I n f i n i t e 。
另外还有一个优化, b b 如果有没有的质因数对结果是不影响的,可以直接将 b b 设为。
代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void in(){scanf("%d",&n);}
void ac()
{
ll p,q,b,g;
while(n--)
{
scanf("%lld%lld%lld",&p,&q,&b);
for(q/=gcd(p,q),g=gcd(q,b);g!=1&&q!=1;b=g,q/=g,g=gcd(q,b));
q==1?puts("Finite"):puts("Infinite");
}
}
int main(){in();ac();}