zoukankan      html  css  js  c++  java
  • Codeforces Round #483 (Div. 2) C. Finite or not?

    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 (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
    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

     这题,看这个分数在b进制下是否为无限循环小数。

    一个分数是否为无限循环小数取决于分母。

    于是想到这题一定是考虑 分母q 和 进制b 的关系,

    于是就是想办法  把分母化为1就不是无限循环小数

    temp1 = gcd(q, b);
    if (temp1 == 1) break;
    while(q % temp1== 0) {
          q /= temp1;
    }
    while一定要加 不然会超时。

     

     1 #include<bits/stdc++.h>
     2 
     3 using namespace std;
     4 const int maxn = 3e5 + 10;
     5 typedef long long LL;
     6 LL gcd(LL a, LL b ) {
     7     if (b == 0) return a;
     8     return gcd(b, a % b);
     9 }
    10 int main() {
    11     int n;
    12     LL p, q, b;
    13     scanf("%d", &n);
    14     while(n--) {
    15         scanf("%lld%lld%lld", &p, &q, &b);
    16         LL g = gcd(p, q);
    17         p = p / g, q = q / g;
    18         LL temp = p / q;
    19         p = p - temp * q;
    20         if (!p) {
    21             printf("Finite
    ");
    22             continue;
    23         }
    24         LL temp1;
    25         while(1) {
    26             temp1 = gcd(q, b);
    27             if (temp1 == 1) break;
    28             while(q % temp1== 0) {
    29                 q /= temp1;
    30             }
    31         }
    32         if (q == 1 ) printf("Finite
    ");
    33         else printf("Infinite
    ");
    34     }
    35     return 0;
    36 }
  • 相关阅读:
    1.Python基础语法
    Python学习4:商城购物
    Python学习3:猜年龄游戏进阶版
    Python学习2:猜年龄游戏
    python学习1:判断学生成绩等级
    K8S集群平滑回退或升级
    Xtrabackup工作原理
    Android App 侧边栏菜单的简单实现
    NoActionBar主题下如何添加OptionsMenu
    TabLayout+ViewPager制作简单导航栏
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9049818.html
Copyright © 2011-2022 走看看