codeforces 983A Finite or not?

本文探讨了一个数论问题:如何判断一个分数在特定进制下是否能表示为有限小数。通过分析分数的基本性质和质因数分解的方法,文章提供了一种有效的算法实现思路。

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


A. 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 basebb 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,13

这是一个数论题目,问q/p能不能在b进制下表示出来。首先对小数位的b进制表示要理解清楚。我们以1/3位例子。1/3的三进制是0.1。因为三进制的小数点右第一位表示为三分位,也就是把1分成了三份。而10进制的小数点右第一位就是10分位。这点理解清楚了后,我们再来看题目。首先先把q,p给同分一下。然后q/p要想在b进制下表示出来的必要条件就是q的质因数被b的质因数包括。然后我们通过gcd再进行while循环,知道gcd值为1,也就是无法约的情况下。如果p==1那么可以否则不可以。原因大家可以想做1/p要想在b进制下表示出来。则b内必须含有p分位这个点。所以他们的质因数是包含关系。

不过我的算法不够快。500ms+。快的看人300ms+。

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 1e9
#define IOS ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 5005;
ll gcd(ll a,ll b){
	return b==0?a:gcd(b,a%b);
}
int main(){
	IOS;
	ll n,q,p,b;
	cin>>n;
	while(n--){
		cin>>q>>p>>b;
		ll k = gcd(q,p);
		q/=k;
		p/=k;
		q%=p;
	//	cout<<q<<' '<<p<<endl;
		if(q<=1&&p==1){
			cout<<"Finite"<<endl;
			continue ;
		}
		k = gcd(p,b);
		while(k!=1){
			while(p%k == 0) p/=k;
			k = gcd(p,b);
		}
		if(p==1){
			cout<<"Finite"<<endl;
			continue ;
		}
		cout<<"Infinite"<<endl;
		
		
	}
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值