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 这题是与数相关
首先你先要静下心来弄明白这几件事情:
第一:
gcd函数,这个是求最大公约数的函数。
第二:
在小数点后面将十进制转化为二进制,是对十进制*2取一个数,一直*2直到出现1;
例如:0.125(10)转化成二进制是0.001;
这题求1/q转化成其他进制,判断是否为一个有限小数。即1/q *b*b*b...是否==1或者大于一的整数。
注意:不能用cin输入会超时!
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll; ll gcd(ll a,ll b)//求a和b的最大公约数的函数
{
if(!b) return a;
return gcd(b,a%b);
} int main()
{
int n;
scanf("%d",&n);
while(n--)
{
ll p,q,b;
scanf("%I64d %I64d %I64d",&p,&q,&b);
ll g=gcd(p,q);
p=p%q;
p/=g;
q/=g;
if(q==1||p==0)
{
printf("Finite\n");
continue;
}
while(b!=1&&q!=1)
{
b=gcd(q,b);
q/=b;
}
if(q==1) printf("Finite\n");
else printf("Infinite\n");
}
return 0;
}

  


cf C. Finite or not? 数论的更多相关文章

  1. CF 984C Finite or not? (数论)

    CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...

  2. CF 980D Perfect Groups(数论)

    CF 980D Perfect Groups(数论) 一个数组a的子序列划分仅当这样是合法的:每个划分中的任意两个数乘积是完全平方数.定义a的权值为a的最小子序列划分个数.现在给出一个数组b,问权值为 ...

  3. 【cf 483 div2 -C】Finite or not?(数论)

    链接:http://codeforces.com/contest/984/problem/C 题意 三个数p, q, b, 求p/q在b进制下小数点后是否是有限位. 思路 题意转化为是否q|p*b^x ...

  4. CF 371B Fox Dividing Cheese[数论]

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. CF984 C. Finite or not?【数论/GCD】

    [链接]:CF [题意]:n组样例,对于每组样例,给你三个数p q b,问你p/q在b进制下是不是一个有限小数,是的话输出Finite,否则输出Infinite. [分析]:b的过程是对q约分,那么只 ...

  6. CodeForces - 984C——Finite or not?分数整除问题(数论,gcd)

    题目传送门 题目描述:给你一个p/q,让你求在b进制下,这个小数是不是有限小数. 思路: 先来膜拜一个大神的博客,如何求小数的二进制表达,(感谢博主肘子zhouzi).然后小数的其他进制表达也一样. ...

  7. 【数论】Codeforces Round #483 (Div. 2) [Thanks, Botan Investments and Victor Shaburov!] C. Finite or not?

    题意:给你一个分数,问你在b进制下能否化成有限小数. 条件:p/q假如已是既约分数,那么如果q的质因数分解集合是b的子集,就可以化成有限小数,否则不能. 参见代码:反复从q中除去b和q的公因子部分,并 ...

  8. cf 450b 矩阵快速幂(数论取模 一大坑点啊)

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  9. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. easyui datagrid 禁止选中行

    没有找到可以直接禁止的属性,但是找到两个间接禁止的方式. 方式一: //onClickRow: function (rowIndex, rowData) {     // $(this).datagr ...

  2. forfiles

    关键命令就这一条了:forfiles.exe /p "D:\Kugou" /m *.tar /d -14 /c "cmd /c del @path" 这条命令的 ...

  3. hive 表类型

    Hive表有受控表(内部表).外部表.分区表.桶表四种.   内部表,就是一般的表,前面讲到的表都是内布标,当表定义被删除的时候,表中的数据随之一并被删除.   外部表,数据存在与否和表的定义互不约束 ...

  4. [PHP] 算法-两个n位的二进制整数相加问题PHP实现

    两个n位二进制数分别存储在两个n元数组A和B中,这两个整数的和存在一个n+1元的数组C中答:此问题主要是考察相加进位的问题,元素1+1 =0 并且往前进一位ADD-BINARY(A,B) C=new ...

  5. [android] 手机卫士保存安全号码

    调用ListView对象的setOnItemClickListener()方法,设置条目的点击事件,参数:OnItemClickListener对象 使用匿名内部类实现,重写onClick()方法,传 ...

  6. java中import static和import的区别【转】

    转自:http://blog.csdn.net/ygc87/article/details/7371254

  7. 【Java并发编程】11、volatile的使用及其原理

    一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...

  8. vsftpd-基于ftp协议的文件传输服务器软件

    第一部分:在Linux上部署vsftpd服务 1. vsftpd简介 1.1 vsftpd是什么? ftp(File Transfer Protocol)文件传输协议.(实现不同操作系统之间文件的传输 ...

  9. DOM之城市二级联动

    1.HTML内容 <select id="province"> <option>请选择</option> <option>山东省&l ...

  10. opencv学习系列:连通域参考处理

    OpenCV里提取目标轮廓的函数是findContours,它的输入图像是一幅二值图像,输出的是每一个连通区域的轮廓点的集合:vector<vector<Point>>. 外层 ...