1. 程式人生 > >兩數相除,判斷小數位是否有限位

兩數相除,判斷小數位是否有限位

vertica each res while www. nsis CI 1.2 etc

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
Note

612=12=0,510

43=1,(3)10

936=14=0,012

412=13=0,13

題意 : 給兩個數,以及一個所要求的進制,詢問是否在此進制下,相除後是否為有限位小數

思路分析 : 比賽的時候想了一個模擬,不過沒有去寫,旁邊的大佬一直再和說,會超時,會超時,然後賽後才知道是用這樣的,比如 10進制下,判斷兩數是否整除,只需要讓除數已知除以 2 或者 5,當可以除到 1 的時候,代表是可以整除的,如果是換成任意進制下的一個數,則需要去除當前的數和 b進制的約數。

代碼示例 :

#define ll long long

ll gcd(ll a, ll b){
    return b == 0?a:gcd(b, a%b);
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    ll t;
    ll p, q, b;
    
    cin >> t;
    while(t--){
        scanf("%lld%lld%lld", &p, &q, &b);
        ll g = gcd(p, q);
        ll x = q / g;
        ll f = gcd(b, x);
        
        while(1){
            if (f == 1) break;
            while(x%f == 0) x /= f;
            f = gcd(b, x);
        }   
        if (x == 1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}

兩數相除,判斷小數位是否有限位