zoukankan      html  css  js  c++  java
  • cf C. Finite or not? 数论

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

    Next nn lines contain queries, one per line. Each line contains three integers pp, qq, and bb (0p10180≤p≤1018, 1q10181≤q≤1018, 2b10182≤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
    ");
                continue;
            }
            while(b!=1&&q!=1)
            {
                b=gcd(q,b);
                q/=b;
            }
            if(q==1) printf("Finite
    ");
            else printf("Infinite
    ");
        }
        return 0;
    }
    

      



  • 相关阅读:
    【OpenGL】Shader实例分析(七)- 雪花飘落效果
    BZOJ 1091([SCOI2003]分割多边形-分割直线)
    Protocol buffer序列化及其在微信蓝牙协议中的应用
    运行计划中cost计算方法
    jquery全局变量---同步请求设置
    Java split字符串中包含.的情况
    jQuery获取、设置title的值
    jQuery获取URL中所带参数的办法
    在Eclipse中提交SVN项目的时候注意提交项目信息
    马丁 福勒 Martin Fowler 关于依赖注入和反转控制的区别
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10185728.html
Copyright © 2011-2022 走看看