zoukankan      html  css  js  c++  java
  • codeforces 984 C. Finite or not?

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

    题意:每次给次p,q,b三个数字,然后让你判断p/q在b进制下是不是无限小数

    分析:先把p/q化成最简形式,然后只需要q的所有质因子都在b中出现,就一定可以表示成x/(b^k)这样的形式,就一定是有限小数。

    AC代码:

     1 #include<bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 
     6 long long p,q,b;
     7 int main(){
     8     ios_base::sync_with_stdio(false);
     9     cin.tie(0);
    10     int n;
    11     cin>>n;
    12     for(int i=1;i<=n;i++){
    13         cin>>p>>q>>b;
    14         if(p>q){
    15             p%=q;
    16         }
    17         long long s=__gcd(p,q);
    18         p/=s;
    19         q/=s;
    20         if(p==0){
    21             cout<<"Finite"<<endl;
    22         }
    23         else {
    24             long long sp=__gcd(q,b);
    25             int sb=0;
    26             while(q>=sp){
    27                 q/=sp;
    28                 sp=__gcd(sp,q);
    29                 if(sp==1&&q==1){
    30                     break;
    31                 }
    32                 if(sp==1){
    33                     sb=1;
    34                     break;
    35                 }
    36             }
    37             if(sb==0){
    38                 cout<<"Finite"<<endl;
    39             }
    40             else {
    41                 cout<<"Infinite"<<endl;
    42             }
    43         }
    44     }
    45 return 0;
    46 }
    View Code
  • 相关阅读:
    SHELL[22]
    SHELL[15]
    SHELL[08]
    SHELL[12]
    SHELL[06]
    SHELL[24]
    SHELL[11]
    shell文件合并、去重
    SHELL[25]
    SHELL[14]
  • 原文地址:https://www.cnblogs.com/ls961006/p/9055941.html
Copyright © 2011-2022 走看看