Codeforces Round #483 (Div. 2) C. Finite or not?

本文介绍了一种判断分数在特定进制下是否能表示为有限小数的方法,并提供了一个具体的算法实现。通过输入三个整数 p、q 和 b,算法能够判断 p/q 在 b 进制下的表示是否为有限小数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



题意:p/q能否表示为b进制下的有限小数形式,即1/q能否表示为b进制下的有限小数形式,类似十进制小数转二进制,

如0.125,小数乘以进制取整数0,小数0.25乘以进制取整数0,小数0.5乘以进制取整数1,且乘到1停止

0.125对应  0.001  即0*(1/2)+0*(1/4)+1*(1/8)=0.125,回看过程,1/q*b*b....每次取整数,最后等于1,相当于乘以

b的过程是对q约分,那么只要b包含q全部的因子即可。

#include <iostream>

#define ll long long
using namespace std;
ll gcd(ll a,ll b)
{
if(!b)return a;
return gcd(b,a%b); 
}
int main()
{
   cin.sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
int n; cin>>n;
while(n--)

 ll p,q,b;   cin>>p>>q>>b;
 ll g=gcd(p,q); p/=g; q/=g;//约分,将分母化成最简。 
if(p==0||q==1)//p==0 该数值为0显然任何形式都可以表示; q==1分母为1表示整数 ,都可以表示 
{
cout<<"Finite"<<endl; continue;
}
//若1/q可以被表示,则p/q也能被表示 
while(q!=1&&b!=1)
{
b=gcd(b,q);
q/=b; //约分 ,b必须含有q的全部因子才是有限小数 

if(q==1)printf("Finite\n");
            else printf("Infinite\n");



   return 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wym_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值