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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值