CodeForces 984C Finite or not?

http://codeforces.com/problemset/problem/984/C

Time limit    1000 ms
Memory limit    262144 kB

题目

You are given several queries. Each query consists of three integers $p$, $q$ and $b$. You need to answer whether the result of $p/q$ in notation with base $b$ is a finite fraction.

A fraction in notation with base $b$ 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\leqslant n \leqslant 10^5$) — the number of queries.

Next ? lines contain queries, one per line. Each line contains three integers $p$, $q$, and $b$ ($0\leqslant p\leqslant 10^{18}, $0\leqslant q\leqslant 10^{18}, $2\leqslant p\leqslant 10^{18}). All numbers are given in notation with base 10.

Output

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

Hint

$\frac{6}{12}=\frac{1}{2}=0.5_{10}$
$\frac{4}{3}=1.333\dots_{10}$
$\frac{9}{36}=\frac{1}{4}=0.01_{2}$
$\frac{4}{12}=\frac{1}{3}=0.1_{3}$

题解

刚一开始看这题被吓傻了!模拟显然超时啊!

显然是有限小数的条件是存在$t$,使$q\mid{p\times b^t}$,首先约分,得到$q'\mid p'\times b^t$,因为$q', p'$互质,所以$q'\mid b^t$(LL也不能乘)

然后唯一分解定理,分解因数!只要b含有所有q的因数那么就是有限的,否则就是无限的!

但是分解因数最快都是期望$\mathcal{\Theta}(\sqrt{n})$显然超时

所以两边不停除以GCD,一次GCD大概是$\mathcal{O}(\log{x})$,即使是多次也比根号快

但为了不浪费时间,将一次GCD用多次(因为被卡了……十倍差距)

AC代码

#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<set>
#define REP(r,x,y) for(register int r=(x); r<(y); r++)
#define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
#define PERE(r,x,y) for(register int r=(x); r>=(y); r--)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__),fflush(stdout)
#else
#define DBG(...) (void)0
#endif
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
char ch; int si;

#define gc() getchar()

template<class T>
inline void read(T &x) {
    x=0; si=1; for(ch=gc();!isdigit(ch) && ch!='-';ch=gc());
    if(ch=='-'){si=-1,ch=gc();} for(;isdigit(ch);ch=gc())x=x*10+ch-'0';
    x*=si;
}
template<class T, class...A> inline void read(T &x, A&...a){read(x); read(a...);}

inline LL GCD(LL a, LL b) {
    return b==0?a:GCD(b,a%b);
}

int main() {
#ifdef sahdsg
    freopen("in.txt","r",stdin);
#endif
    int n;
    read(n);
    while(0<n--) {
        LL p,q,b; read(p,q,b);
        LL g=GCD(p,q); p/=g, q/=g;
        g=GCD(q,b);
        while(g!=1) {
            while(q%g==0) q/=g;
            g=GCD(q,b);
        }
        if(q==1) puts("Finite");
        else puts("Infinite");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/sahdsg/p/10816117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值