Codeforeces 984 C. Finite or not? (数论)

C. Finite or not?
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given several queries. Each query consists of three integers ppqq 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 ppqq, and bb (0p10180≤p≤10181q10181≤q≤10182b10182≤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
Note

612=12=0,510612=12=0,510

43=1,(3)1043=1,(3)10

936=14=0,012936=14=0,012

412=13=0,13412=13=0,13



题意:

给定p,q,b,问p/q能否表示成b进制下的有限小数形式(p,q,b <= 1e18)
思路:
对于约分后的p/q,只需关心1/q是否能在b进制下有限表现即可。
把1/q换成小数形式,例如1/8 = 0.125,像整数转换进制一样,小数转换进制也是不断地除以进制的基,而小数的基是b^-1。则0.125转换为二进制过程如下,0.125(除2^-1)-->余0商0.25(除2^-1)-->余0商0.5(除2^-1)-->余1商0-->停止。所以0.125的二进制形式就是0.001。所以1/q能否能在b进制下有限表现即 是否存在x使 1/q * b^x == 整数(没有小数位)即 是否存在x使q能被b^x整除。

其实最终能转化成 q中的所有质因子是否都是b中的质因子。


在十进制下一个分数是有限小数的结论是:分母的因子只有2和5
2和5正好是10的两个因子(除了1和它本身)

于是大胆猜了一发结论XD
先将p/q约分为最简形式

  1. 如果此时gcd(q, b) == 1,即q,b互质,则一定为无限小数
  2. 否则,若gcd(q, b) != 1,就让q一直除gcd(q, b),直到gcd(q, b) == 1
    如果此时q==1,说明q只含有b的因子,可以转化为有限小数,反之不行。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
ll p, q, b;
int main()
{
    int t;
    while(~scanf("%d", &t))
    {
        while(t--)
        {
            scanf("%lld%lld%lld", &p, &q, &b);
            ll gcd = __gcd(p, q);
            q /= gcd;
            while(q != 1)
            {
                gcd = __gcd(q, b);
                if(gcd == 1) break;
                while(q%gcd == 0) q /= gcd;
            }
            if(q == 1) printf("Finite\n");
            else puts("Infinite");
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值