CF984C Finite or not?

原题链接: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 p/q in notation with base b 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) n ( 1 ≤ n ≤ 10 5 ) — the number of queries.

Next n n lines contain queries, one per line. Each line contains three integers p,q, and b(0p1018,1q1018,2b1018) 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 的质因数都是b的因数,那么 pq p q b b 进制下就是有限的。

但是值域是1018,直接分解质因数肯定血 T T ,所以我们每次直接除以gcd(q,b)就可以将 q q b共有的质因子除掉,我们这样一直除下去直到 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 如果有q没有的质因数对结果是不影响的,可以直接将 b b 设为gcd(q,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();}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值