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 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
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,13412=13=0,13

题意:给出一个分式q/p,求在b进制下q/p得到的小数是否有限。

思路:首先对于我们熟悉的十进制下,如果一个数除的尽另一个数,当且仅当分母的所有质因子集合为分子的质因子集合的子集,因为分子除以分母得到的是一个小数,而有尽的小数可以看成是一个整数(乘以对应小数点后数字位数的10的对应次方)(假设小数点后有n位,那么就乘以10的n次方),这样就把问题简化为求十进制下一个数是否可以整除另一个数的问题。而分子可以有限的在需要的情况下可以乘10再乘10^(-1)(对应的增加10的质因子2,5来抵消分母的质因子2或5)。

例:3/16==3/(2*2*2*2)==(3*10*10*10*10)/(2*2*2*2)*10^(-4)==(3*5*2*5*2*5*2*5*2)/(2*2*2*2)*10^(-4)==(3*5*5*5*5)*10^(-4)==1875*10(-4)==0.1875

4/80-->先化简(分子分母同时除以他们的最大公因数)-->1/20==1/(2*2*5)==(1*10*10)/(2*2*5)*10^(-2)==(1*2*5*2*5)/(2*2*5)*10^(-2)==5*10^(-2)==0.05

1/12==1/(2*2*3)==(1*10*10)/(2*2*3)*10^(-2)==(1*2*5*2*5)/(2*2*3)*10^(-2)==(1*5*5)/3*10^(-2),此时我们可以发现我们不能以分子乘10的方法来消去分母的3,因为3不是10的质因子,所以1/12除不尽。

综上,推广到n进制的情况下,先化简分子与分母后,分母化成最小质因子乘积的状态下,如果分母的所有的质因子都可以在n的质因子集合里找到(相当于是分母质因子集合是n的质因子集合的子集)它就可以除的尽(因为可以对应的乘n来抵消分母与n共同的质因子)。

代码:

#include<stdio.h>
#define ll __int64
ll gcd(ll a,ll b)//求最大公约数
{
  ll r;
  while(b)
  {
    r=a%b;
    a=b;
    b=r;
  }
  return a;
}
int main()
{
  int n;
  scanf("%d",&n);
  ll p,q,b;
  while(n--)
  {
    scanf("%d%d%d",&p,&q,&b);
    if(p==0)//任何数除以0后等于0
    printf("Finite\n");
    else
    {
      ll c=gcd(p,q);
      p=p/c;
      q=q/c;
      ll g;
      while(b%q)//剔除分母q与b的共同质因子
      {
        g=gcd(q,b);
        if(g==1)
        break;
        q=q/g;
        b=g; //因为q与b的最大公因数是g,当q/g后,剩余的数与b的公因数只会在g中,以此来缩小范围
      }
      if(b%q==0)//如果b%q==0,说明在剔除后,q所有剩余的质因子都可以在剩余的b的质因子中找到
      printf("Finite\n");
      else
      printf("Infinite\n");
    }
  }
return 0;
}

codeforces983A(数学题)的更多相关文章

  1. ytu 2558: 游起来吧!超妹!(水题,趣味数学题)

    2558: 游起来吧!超妹! Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 7  Solved: 3[Submit][Status][Web Board ...

  2. sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)

    Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game name ...

  3. python解无忧公主数学题107.py

    python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...

  4. python解无忧公主数学题108

    """ python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...

  5. HDU 圆桌会议 - 数学题

    圆桌   题意就是每分钟可以将相邻的两个人的位置互换一下 , 问你 ,几分钟可以将所有人的位置互换成    原先的  B 在A的右边 C在A的左边 , 换成现在的 C 在A 的右边 , B 在 A 的 ...

  6. HDU 2529 Shot (物理数学题)

    题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...

  7. HDU 2671 Can't be easier(数学题,点关于直线对称)

    题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...

  8. ACM之数学题

    数学题,始终记得,第一次被带飞师大校赛以及省赛,毫无例外的在数学题上卡死....因此,现在开始,有意识的保留遇见的数学题...(下列知识点按遇见先后顺序排列: 1欧拉公式 欧拉公式的用处是,找出小于N ...

  9. hdu 5587 Array 数学题

    Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5587 De ...

随机推荐

  1. Axios的默认配置(碎片知识)API

    axios API axios(config) axios({ method: 'Post', url: '/user/123', data: { //略 } }) axios(url[, confi ...

  2. MyEclipse6.5的SVN插件的安装

    在线安装 1. 打开Myeclipse,在菜单栏中选择Help→Software Updates→Find and Install; 2. 选择Search for new features to i ...

  3. linux基础知识(1)

    1.date man date :查看帮助 1. date [OPTION]... [+FORMAT]:显示时间 ,format表示格式符号 例如: date :Sun Dec 23 21:45:34 ...

  4. vue 点击一个div,使input获得焦点

    <div class="inputMessage" @click="inputMessage">输入留言</div> <input ...

  5. 重写TreeMap的compare方法处理配置表

    需要处理的配置表如下: 接上一篇的优化,接着优化,优化代码如下:  这段代码的关键在于重写TreeMap的compare方法. 关于如何重写TreeMap的compare方法,以及返回值代表的意义,可 ...

  6. vue3.0 配置公共请求地址

    正常请求接口: return request({ url: 'http://192.168.1.0/User/cancelUpgrade', method: 'get', params: data } ...

  7. js动态检测加载 JQ

    var jqcdnurl = 'https://cdn.bootcss.com/jquery/3.2.1/jquery.js'; //控制台输出 function log() { for (var i ...

  8. 牛客网第一场E题 Removal

    链接:https://www.nowcoder.com/acm/contest/139/E 来源:牛客网 Bobo has a sequence of integers s1, s2, ..., sn ...

  9. 解决libvlc_media_player_stop时死锁的方法

    转自:http://www.jiazi.cn/blog/?id=56 扩散该解决方法 最近需要使用VLC控件来做一个简单的流媒体播放器,在实施过程中,发现在调用libvlc_media_player_ ...

  10. linux jdk install and tomcat install

      1● linux安装jdk1.8 Download jdk1.8   export JAVA_HOME=/usr/java/jdk1.8.0_181 export JRE_HOME=${JAVA_ ...