题目传送门

题目描述:给你一个p/q,让你求在b进制下,这个小数是不是有限小数。

思路:

先来膜拜一个大神的博客,如何求小数的二进制表达,(感谢博主肘子zhouzi)。然后小数的其他进制表达也一样。

而分数的转化,比如1/6转化成3进制,看图 ↓ 。

其实就是将1/6不断乘以3,然后丢掉整数部分,如果我们不看丢掉整数部分这个环节,就是把1/6不断乘以3看看最后能不能整除就好了,如果有限的话,肯定会得到((b)^n))%q=0,b的某一次幂可以整除q,就代表是有限。(感谢薛佬帮我理解!!)

那么一个朴素的想法,就是,n从1一直加上去,找到一个可以整除的,但问题是 证有不证无,我们无法保证n到几退出循环,所以要改进思路。

其实b^n整除q的过程,其实就是b^n的因子和q的因子不断约分的过程,如果约分到最后,q还剩下一个b中没有的因数,则说明无法整除。  那就是每一次都用q除去gcd(q,b),这样消耗q消耗到最后,判断得到的数是不是1,是1则代表可以整除,不是1则代表  用b没法约分q了,不能整除。思路就是这样

但代码中有不少细节要注意。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
if(a%b==0)return b;
else return gcd(b,a%b);
} //辗转相除法求两个数的最大公因数
int main()
{
int n;
cin>>n;
while(n--)
{
ll p,q,b;
//cin>>p>>q>>b; 超时
scanf("%I64d%I64d%I64d",&p,&q,&b); //cf读入longlong类型只能用 I64%
if(p==0)
{
printf("Finite\n");
}else
{
q/=gcd(p,q);//约分
ll g;
while(g=gcd(q,b),g!=1)
{
while(q%g==0)//由于可能出现q=10000000000 g=2的情况 这样子多次调用gcd会浪费时间 所以在这里优化一下
q=q/g;
}
if(q==1){ // q最后如果为 1 则用若干个b把q消耗掉了 即b的若干次方 可以整除 q
printf("Finite\n");
}else{
printf("Infinite\n");
}
}
}
}
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 (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,13

CodeForces - 984C——Finite or not?分数整除问题(数论,gcd)的更多相关文章

  1. CodeForces 984C Finite or not?

    http://codeforces.com/problemset/problem/984/C Time limit    1000 msMemory limit    262144 kB 题目 You ...

  2. codeforces 983A Finite or not?

    题意: 判断一个分数在某一进制下是否为无限小数. 思路: 首先把这个分数约分,然后便是判断. 首先,一个分数是否为无限小数,与分子是无关的,只与分母有关. 然后,再来看看10进制的分数,可化为有限小数 ...

  3. CF 984C Finite or not? (数论)

    CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...

  4. CF984 C. Finite or not?【数论/GCD】

    [链接]:CF [题意]:n组样例,对于每组样例,给你三个数p q b,问你p/q在b进制下是不是一个有限小数,是的话输出Finite,否则输出Infinite. [分析]:b的过程是对q约分,那么只 ...

  5. Codeforces Round #276 (Div. 2)A. Factory(数论)

    这道题可以暴力的一直按要求的方法去做,做1000000次还不能整除m就认为永远不能整除m了(m不超过100000,循环1000000次比较安全了已经).这种方法可以AC. 下面深入的分析一下到底循环多 ...

  6. Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论

    https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...

  7. Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

    题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0&l ...

  8. CodeForces 689C Mike and Chocolate Thieves (二分+数论)

    Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...

  9. Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

    题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

随机推荐

  1. hbase性能调优(转载)

    一.服务端调优 1.参数配置 1).hbase.regionserver.handler.count:该设置决定了处理RPC的线程数量,默认值是10,通常可以调大,比如:150,当请求内容很大(上MB ...

  2. HTML->CSS->JS->PHP的顺序及相关网址(转)

    如果你有耐心坚持一年以上的话, 我会推荐HTML->CSS->JS->PHP的顺序来学习. 1. HTML学习:首先学习HTML,HTML作为标记语言是非常容易学的,把w3schoo ...

  3. 02 mybatis环境搭建 【spring + mybatis】

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  4. Condition实现一个生产者一个消费者

    Condition实现一个生产者一个消费者,实现一对一交替打印: import java.util.concurrent.locks.Condition; import java.util.concu ...

  5. C语言访问网页

    一.理论 http://www.zixue7.com/thread-3860-1-1.html

  6. EZOJ #87

    传送门 分析 由于我不知道壶里到底有多少水,那么显然我第一次 分别向两个杯子分别到 L/2 +1 和 L/2 才是最优的.(这样既维护了两个人的差值不超1,又正好倒了L的水).那么接下来如果壶里还有水 ...

  7. unix 下 shell 遍历指定范围内的日期

    UNIX下遍历日期,date 没有 -d 的参数,所以需要自己处理. 下面使用时差的方法进行计算,遍历的日期是降序的 #!/usr/bin/ksh . $HOME/.profile timelag= ...

  8. Entity Framework Tutorial Basics(28):Concurrency

    Concurrency in Entity Framework: Entity Framework supports Optimistic Concurrency by default. In the ...

  9. JavaWeb_泛型(Generic)

    JDK5以前,对象保存到集合中就会失去其特性,取出时通常要程序员手工进行类型的强制转换,这样不可避免的就会引发程序的一些安全性问题.例如: ArrayList list = new ArrayList ...

  10. tcpdump/HTTP协议实践

    tcpdump/HTTP协议实践 客户端: CLOSED->SYN_SENT->ESTABLISHED->FIN_WAIT_1->FIN_WAIT_2->TIME_WAI ...