题目传送门

题目描述:给你一个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. nginx配置域名

    其他都一样,就特别说下server块的配置. server { listen 80; server_name www.icweshop.com; # 注意:这里你填写的域名必须在/etc/hosts中 ...

  2. ssh整合(dao使用hibernateTemplate)

  3. git 本地代码到github(转)

    git 本地代码到github   一·什么是gitHub? 官网解释:gitHub是一个让无论处于何地的代码工作者能工作于同一个项目,同一个版本的平台.(GitHub is a code hosti ...

  4. 760. Find Anagram Mappings乱序字符串的坐标位置

    [抄题]: Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by rand ...

  5. 面试题: Struts2

    1. Struts2与Struts1的联系与区别是什么?为什么要用Struts2? 答案: struts1与struts2都是mvc框架的经典实现模式. Struts2不是从Struts1升级而来,而 ...

  6. 机器人自主移动的秘密:SLAM与路径规划有什么关系?(三)

    博客转载自:https://www.leiphone.com/news/201612/lvDXqY82OGNqEiyl.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...

  7. 浅谈android代码保护技术_ 加固

    浅谈android代码保护技术_加固 导语 我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk,结果被人反编译了,那心情真心不舒服.虽然我们混淆,做到native层,但 ...

  8. input 输入框两种改变事件的方式

    一.在输入框内容变化的时候不会触发,当鼠标在其他地方点一下才会触发 $('input[name=myInput]').change(function() { ... }); 二.在输入框内容变化的时候 ...

  9. MyBatis基本查询、条件查询、查询排序

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  10. 《Maven实战》笔记-8-构建部署Web项目

      一.Web项目结构 1.显式指定Web项目打包方式为war:   2.默认目录 根据“约定大于配置”的规则,Web项目的类及资源文件默认位置为src/main/java和src/main/reso ...