cf C. Finite or not? 数论

You are given several queries. Each query consists of three integers pp, qq and bb. You need to answer whether the result of p/qp/q in notation with base bb is a finite fraction.

A fraction in notation with base bb 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 nn (1n1051≤n≤105) — the number of queries.

Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0p10180≤p≤1018, 1q10181≤q≤1018, 2b10182≤b≤1018). All numbers are given in notation with base 1010.

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


这题是与数相关
首先你先要静下心来弄明白这几件事情:
第一:
gcd函数,这个是求最大公约数的函数。
第二:
在小数点后面将十进制转化为二进制,是对十进制*2取一个数,一直*2直到出现1;
例如:0.125(10)转化成二进制是0.001;
这题求1/q转化成其他进制,判断是否为一个有限小数。即1/q *b*b*b...是否==1或者大于一的整数。
注意:不能用cin输入会超时!


#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;

ll gcd(ll a,ll b)//求a和b的最大公约数的函数
{
    if(!b) return a;
    return gcd(b,a%b);
}

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

  



猜你喜欢

转载自www.cnblogs.com/EchoZQN/p/10185728.html