Codeforces Round #483 (Div. 1) A. Finite or not?

时间:2022-08-25 05:15:39

最简分数p/q在b进制下的小数为无限小数的充要条件是

  q不完全由b的某些因子乘积构成

#include<bits/stdc++.h>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define op operator
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
template<typename Q>
void inin(Q &x)
{
    x=0;int f=0;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    x=f?-x:x;
}
LL gcd(LL a,LL b)
{
    LL c;
    while(a%b)
    {
        c=a%b;
        a=b,b=c;
    }
    return b;
}
int main()
{
//    freopen("a.in","r",stdin);
//    freopen("a.out","w",stdout);
    LL p,q,b,t;
    inin(t);
    while(t--)
    {
        inin(p),inin(q),inin(b);
        if(!p){cout<<"Finite\n";continue;}
        LL d=gcd(q,p);
        q/=d;
        while(q%b==0)q/=b;
        d=gcd(q,b);
        while(d>1)
        {
            while(!(q%d))
                q/=d;
            d=gcd(b,q);
        }
        if(q==1)cout<<"Finite\n";
        else cout<<"Infinite\n";
    }
    return 0;
}