前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #483 (Div. 2) C. Finite or not?

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

作者头像
用户2965768
发布2018-08-30 17:11:26
4710
发布2018-08-30 17:11:26
举报
文章被收录于专栏:wymwymwym

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 pp, qq 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 (1≤n≤1051≤n≤105) — the number of queries.

Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0≤p≤10180≤p≤1018, 1≤q≤10181≤q≤1018, 2≤b≤10182≤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; } 

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档