codeforce#483div2C-Finite or not?数论,GCD

传送门:http://codeforces.com/contest/984/problem/C

这道题
题意:求q/p是否能用k进制有限表示小数点后的数;
 
思路:数学推理:
    1、首先把q/p化为最简形式。
    2、如果有限,相当于 q | p *k的n次 ,就是说p*k。。。*k后可以整除q
        (“|”—>整除 如3|12表示12能被3整除
    3、因为p,q现在互质,所以就是k*k*k*k…*k后可以整除q,那么就是可以表示。
   4、上面3中的整除关系,还可以转为q的质因子全都是k的质因子。
    5、也就是说,每次我们把q除以gcd(q,k).直到gcd(q,k)==1;
    6、 如果q==1,说明4句成立,否则不成立;
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iterator>
#include <cmath>
using namespace std;

typedef long long ll;

int n;
ll p,q,b;

ll gcd(ll a,ll b)  
{  
        return b==0?a:gcd(b,a%b);
}  
int main(){
    scanf("%d", &n);
    while(n--)
    {
        scanf("%lld%lld%lld", &p, &q, &b);
        ll tmp = gcd(p,q);
        if(p==0)
        {
            puts("Finite");
            continue;
        }
        p/=tmp;q/=tmp;
        tmp = gcd(q,b);
        while(tmp!=1)
        {
            while(q%tmp == 0)q/=tmp;    //这不要加上while才不会tle
            tmp = gcd(q,b);
        }
        if(q==1)puts("Finite");
        else puts("Infinite");
        
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9045998.html