CodeForces - 984C——Finite or not?分数整除问题(数论,gcd)
题目描述:给你一个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");
}
}
}
}
1 second
256 megabytes
standard input
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.
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.
For each question, in a separate line, print Finite if the fraction is finite and Infinite otherwise.
2
6 12 10
4 3 10
Finite
Infinite
4
1 1 2
9 36 2
4 12 3
3 5 4
Finite
Finite
Finite
Infinite
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)的更多相关文章
- CodeForces 984C Finite or not?
http://codeforces.com/problemset/problem/984/C Time limit 1000 msMemory limit 262144 kB 题目 You ...
- codeforces 983A Finite or not?
题意: 判断一个分数在某一进制下是否为无限小数. 思路: 首先把这个分数约分,然后便是判断. 首先,一个分数是否为无限小数,与分子是无关的,只与分母有关. 然后,再来看看10进制的分数,可化为有限小数 ...
- CF 984C Finite or not? (数论)
CF 984C Finite or not? (数论) 给定T(T<=1e5)组数据,每组数据给出十进制表示下的整数p,q,b,求问p/q在b进制意义下是否是有限小数. 首先我们先把p/q约分一 ...
- CF984 C. Finite or not?【数论/GCD】
[链接]:CF [题意]:n组样例,对于每组样例,给你三个数p q b,问你p/q在b进制下是不是一个有限小数,是的话输出Finite,否则输出Infinite. [分析]:b的过程是对q约分,那么只 ...
- Codeforces Round #276 (Div. 2)A. Factory(数论)
这道题可以暴力的一直按要求的方法去做,做1000000次还不能整除m就认为永远不能整除m了(m不超过100000,循环1000000次比较安全了已经).这种方法可以AC. 下面深入的分析一下到底循环多 ...
- Codeforces - 1114C - Trailing Loves (or L'oeufs?) - 简单数论
https://codeforces.com/contest/1114/problem/C 很有趣的一道数论,很明显是要求能组成多少个基数. 可以分解质因数,然后统计各个质因数的个数. 比如8以内,有 ...
- 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 ...
- CodeForces 689C Mike and Chocolate Thieves (二分+数论)
Mike and Chocolate Thieves 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/G Description ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
随机推荐
- Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
Bitmap bmp =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); Paint paint = new ...
- 激活 jave platform se
1.有的网页上面会显示这个,但是点击后没反应 解决方案: firefox- >工具-附加组件管理器-“插件” 找到“Java(TM) Platform SE 7”,把“询问是否激活”改为“总是激 ...
- 为SSRS配置SMTP服务器身份验证
此处设置外邮地址却无法填写邮箱密码 一.安装SMTP服务 1.在服务管理器中单击“功能” 2.单击“添加功能”打开“添加功能向导”对话框 3.在“选择功能”页上选择“SMTP服务器”并选择“添加必须的 ...
- android tween动画和Frame动画总结
tween 动画有四种 //透明度动画 AlphaAnimation aa = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity. ...
- java字符编码转换研究(转)
1. 概述 本文主要包括以下几个方面:编码基本知识,java,系统软件,url,工具软件等. 在下面的描述中,将以"中文"两个字为例,经查表可以知道其GB2312编码是" ...
- Ros学习——C++发布器publisher和订阅器subscriber
1.编写发布器 初始化 ROS 系统 在 ROS 网络内广播我们将要在 chatter 话题上发布 std_msgs/String 类型的消息 以每秒 10 次的频率在 chatter 上发布消息 在 ...
- Java进程与多线程+线程中的join、yield、wait等方法+synchronized同步锁使用
首先了解什么是多线程与进程 进程:是一个执行过程,动态的概念 --->会分配内存线程:是进程的一个单元,线程是系统最小的执行单元 详解: http://blog.csdn.net/luoweif ...
- 动态tab页
1.前台代码 <%-- builed by manage.aspx.cmt [ver:2015.25.26] at 2015-06-26 15:25:42 --%> <%@ Pag ...
- 《the art of software testing》 (1-2)章
软件测试的心理学,重点是要认清: 测试时为发现错误而执行程序的过程 成功的测试:如果在测试某段程序时发现了错误,而且这些错误是可以修复的,就将这次合理设计并得到有效执行的测试称作是"成功的& ...
- const char *p; char const *p; char * const p的区别
请看下面三种定义: const char *p; char const *p; char * const p; 首先看第一种,我们先看p,本着”从里往外”的原则,p是一个char *类型的变量,但ch ...