原题链接:http://codeforces.com/contest/984/problem/C

Finite or not?

You are given several queries. Each query consists of three integers p,qp,qp, q and bbb. You need to answer whether the result of p/q" role="presentation" style="position: relative;">p/qp/qp/q in notation with base bbb is a finite fraction.

A fraction in notation with base b" role="presentation" style="position: relative;">bbb 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 n(1≤n≤105)n(1≤n≤105)n (1≤n≤10^5) — the number of queries.

Next nnn lines contain queries, one per line. Each line contains three integers p,q" role="presentation" style="position: relative;">p,qp,qp, q, and b(0≤p≤1018,1≤q≤1018,2≤b≤1018)b(0≤p≤1018,1≤q≤1018,2≤b≤1018)b (0≤p≤10^{18},1≤q≤10^{18}, 2≤b≤10^{18}). All numbers are given in notation with base 101010.

Output

For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.

Examples
input

2
6 12 10
4 3 10

output

Finite
Infinite

input

4
1 1 2
9 36 2
4 12 3
3 5 4

output

Finite
Finite
Finite
Infinite

Note

612=12=0,510612=12=0,510\frac{6}{12}=\frac{1}{2}=0,5_{10}

43=1,(3)1043=1,(3)10\frac{4}{3}=1,(3)_{10}

936=14=0,012936=14=0,012\frac{9}{36}=\frac{1}{4}=0,01_2

412=13=0,13412=13=0,13\frac{4}{12}=\frac{1}{3}=0,1_3

题解

先约一波分,如果qqq的质因数都是b" role="presentation" style="position: relative;">bbb的因数,那么pqpq\frac{p}{q}在bbb进制下就是有限的。

但是值域是1018" role="presentation" style="position: relative;">1018101810^{18},直接分解质因数肯定血TTT,所以我们每次直接除以gcd(q,b)" role="presentation" style="position: relative;">gcd(q,b)gcd(q,b)gcd(q,b)就可以将qqq和b" role="presentation" style="position: relative;">bbb共有的质因子除掉,我们这样一直除下去直到gcd(b,q)=1gcd(b,q)=1gcd(b,q)=1或q=1q=1q=1时就可以判定了,如果q=1q=1q=1输出FiniteFiniteFinite,否则输出InfiniteInfiniteInfinite。

另外还有一个优化,bbb如果有q" role="presentation" style="position: relative;">qqq没有的质因数对结果是不影响的,可以直接将bbb设为gcd(q,b)" role="presentation" style="position: relative;">gcd(q,b)gcd(q,b)gcd(q,b)。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void in(){scanf("%d",&n);}
void ac()
{ll p,q,b,g;while(n--){scanf("%lld%lld%lld",&p,&q,&b);for(q/=gcd(p,q),g=gcd(q,b);g!=1&&q!=1;b=g,q/=g,g=gcd(q,b));q==1?puts("Finite"):puts("Infinite");}
}
int main(){in();ac();}

CF984C Finite or not?相关推荐

  1. ADPRL - 近似动态规划和强化学习 - Note 2 - Stochastic Finite Horizon Problem

    2. Stochastic Finite Horizon Problem 在这一节中主要介绍了随机DP算法来解决不确定性下的有限地范围问题,如Denition 1.4所述,它被表述为一个组合优化问题. ...

  2. 确定有限状态机和非确定有限状态机详解 包含Java实现源码(Nondeterministic finite automata)

    本文将讲解确定有限自动状态机和非确定有限自动状态机的特点和区别.将结合图片例子重点讲解什么是非确定有限自动状态机.最后讲解如何将非确定状态机转换为确定的状态机.多图预警!! 有限自动状态机可以分为确定 ...

  3. UA Stat PhD Qualify Problems for Finite Sample Space Probability

    UA Stat PhD Qualify Problems for Finite Sample Space Probability May 2014 #1 解答与反思 Jan 2015 #2 第一问 第 ...

  4. matlab命令fvtool,FVTool: a finite volume toolbox for Matlab

    摘要: FVTool is an implementation of cell-centered finite volume technique in Matlab, that can be used ...

  5. 二阶偏微分方程组 龙格库塔法_有限单元法(Finite Element Method)实现声波方程模拟(Part 2)...

    2.1 前言 承接上一篇文章,前面我们已经介绍了一维声波方程有限元求解: 蓝不是蓝:有限单元法(Finite Element Method)实现声波方程模拟(Part 1)​zhuanlan.zhih ...

  6. 计算机态,(计算机)有限态自动机,FSM(finite state machine),音标,读音,翻译,英文例句,英语词典...

    补充资料:ω-有限自动机 ω-有限自动机 ω-finite state automata 1094·.一youx一anz}dongJ-..有限自动机(.一rinite state automata)一 ...

  7. TensorFlow | ReluGrad input is not finite. Tensor had NaN values

    问题的出现 Question 这个问题是我基于TensorFlow使用CNN训练MNIST数据集的时候遇到的.关键的相关代码是以下这部分: cross_entropy = -tf.reduce_sum ...

  8. Robust Quasistatic Finite Elements and Flesh Simulation

    文章来源: Robust Quasistatic Finite Elements and Flesh Simulation 3.Quasistatic Formulation { 公式xt→=v⃗ \ ...

  9. reason: 'Multiplier is not finite! That's illegal. multiplier:nan' *** First throw call stack:

    reason: 'Multiplier is not finite!  That's illegal.  multiplier:nan' *** First throw call stack: 0作为 ...

  10. RL(Chapter 3): Finite Markov Decision Processes (有限马尔可夫决策过程)

    本文为强化学习笔记,主要参考以下内容: Reinforcement Learning: An Introduction 代码全部来自 GitHub 习题答案参考 Github 目录 The Agent ...

最新文章

  1. 30分钟看懂XGBoost的基本原理
  2. Xamarin图表开发基础教程(4)OxyPlot框架
  3. SqlServer的分割函数
  4. Spring 使用注解注入 学习(四)
  5. 【Redis监控工具之treeNMS 】
  6. win8.1 安装kb2999226 一直提示 搜索更新
  7. 1.STC15W408AS单片机硬件资源
  8. Linux的web视频服务器的构建 (chinaitlab)
  9. win10系统服务器不能创建对象,教你win10系统activex部件不能创建对象的解决教程...
  10. Mipmap与纹理过滤
  11. 登录模块 用户认证 SpringSecurity +Oauth2+Jwt
  12. 为什么别人进步你退步,你应该如何提升自己
  13. 基于linux的oracle_rac实时应用集群研究,基于Linux的OracleRAC实时应用集群研究毕业设计论文...
  14. k8s学习-持久化存储(Volumes、hostPath、emptyDir、PV、PVC)详解与实战
  15. 使用telnet发送附件邮件
  16. 月薪五万,996真的就像呼吸一样自然吗?
  17. 看服务器时间修改日志,怎样看服务器远程更改时间记录
  18. 永信至诚发起亿元创投基金 助力网络安全创业者成长
  19. python数据分析5个案例-Python数据分析-案例分析
  20. Three.js光照贴图添加阴影(·lightMap)

热门文章

  1. wordpress.org 删除
  2. 文件删除后未释放磁盘
  3. 2016-05-25 margin-right jsp获取页面流变量 文字颜色
  4. html标记详解博客,HTML表格标记详解8:表格嵌套
  5. Mysql的server_id_MySQL如何生成唯一的server-id
  6. pythonwhileelse,关于Python while语句的Else子句
  7. Linux用户环境变量及操作
  8. [git]git 分支
  9. 第二阶段 站立会议 10
  10. Android中网络流量控制(防火墙)——Iptables