C. Finite or not?

You are given several queries. Each query consists of three integers p, q and b. You need to answer whether the result of p/q in notation with base b

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

(1n105

) — the number of queries.

Next n

lines contain queries, one per line. Each line contains three integers p, q, and b (0p1018, 1q1018, 2b1018). 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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;


ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}
/*
不得不承认自己还是菜啊 ,其实这道题我也想到了就是q能不能用b,来表示
但是我更想直接根据b和q的关系,直接求出来
课堂上都学过
其实就是一个二进制转为10进制的思想,1/q *b*b*b*....
就是看一看,这个数字有没有可能变成一个整数
也就是说b是不是有q 的所有素因子
然后就是一个判断,我觉得这个判断很好,自己领悟吧
*/

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
	   ll p,q,b;
	   scanf("%lld %lld %lld",&p,&q,&b);
	   p=p%q;
	   ll g=gcd(p,q);
	
	   p/=g,q/=g;
	   if(p==0||q==1){
	   	 printf("Finite\n");
	   	 continue;
	   }
	 
	   while(q!=1&&b!=1){
          b=gcd(q,b);
          q/=b;
	   }
	   if(q==1)printf("Finite\n");
	   else printf("Infinite\n");
	}
	return 0;
}

阅读更多
版权声明:小白一个,欢迎各位指错。 https://blog.csdn.net/qq_36424540/article/details/80337894
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭