codeforces983A(数学题)
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,13412=13=0,13
题意:给出一个分式q/p,求在b进制下q/p得到的小数是否有限。
思路:首先对于我们熟悉的十进制下,如果一个数除的尽另一个数,当且仅当分母的所有质因子集合为分子的质因子集合的子集,因为分子除以分母得到的是一个小数,而有尽的小数可以看成是一个整数(乘以对应小数点后数字位数的10的对应次方)(假设小数点后有n位,那么就乘以10的n次方),这样就把问题简化为求十进制下一个数是否可以整除另一个数的问题。而分子可以有限的在需要的情况下可以乘10再乘10^(-1)(对应的增加10的质因子2,5来抵消分母的质因子2或5)。
例:3/16==3/(2*2*2*2)==(3*10*10*10*10)/(2*2*2*2)*10^(-4)==(3*5*2*5*2*5*2*5*2)/(2*2*2*2)*10^(-4)==(3*5*5*5*5)*10^(-4)==1875*10(-4)==0.1875
4/80-->先化简(分子分母同时除以他们的最大公因数)-->1/20==1/(2*2*5)==(1*10*10)/(2*2*5)*10^(-2)==(1*2*5*2*5)/(2*2*5)*10^(-2)==5*10^(-2)==0.05
1/12==1/(2*2*3)==(1*10*10)/(2*2*3)*10^(-2)==(1*2*5*2*5)/(2*2*3)*10^(-2)==(1*5*5)/3*10^(-2),此时我们可以发现我们不能以分子乘10的方法来消去分母的3,因为3不是10的质因子,所以1/12除不尽。
综上,推广到n进制的情况下,先化简分子与分母后,分母化成最小质因子乘积的状态下,如果分母的所有的质因子都可以在n的质因子集合里找到(相当于是分母质因子集合是n的质因子集合的子集)它就可以除的尽(因为可以对应的乘n来抵消分母与n共同的质因子)。
代码:
#include<stdio.h>
#define ll __int64
ll gcd(ll a,ll b)//求最大公约数
{
ll r;
while(b)
{
r=a%b;
a=b;
b=r;
}
return a;
}
int main()
{
int n;
scanf("%d",&n);
ll p,q,b;
while(n--)
{
scanf("%d%d%d",&p,&q,&b);
if(p==0)//任何数除以0后等于0
printf("Finite\n");
else
{
ll c=gcd(p,q);
p=p/c;
q=q/c;
ll g;
while(b%q)//剔除分母q与b的共同质因子
{
g=gcd(q,b);
if(g==1)
break;
q=q/g;
b=g; //因为q与b的最大公因数是g,当q/g后,剩余的数与b的公因数只会在g中,以此来缩小范围
}
if(b%q==0)//如果b%q==0,说明在剔除后,q所有剩余的质因子都可以在剩余的b的质因子中找到
printf("Finite\n");
else
printf("Infinite\n");
}
}
return 0;
}
codeforces983A(数学题)的更多相关文章
- ytu 2558: 游起来吧!超妹!(水题,趣味数学题)
2558: 游起来吧!超妹! Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3[Submit][Status][Web Board ...
- sdut 2416:Fruit Ninja II(第三届山东省省赛原题,数学题)
Fruit Ninja II Time Limit: 5000MS Memory limit: 65536K 题目描述 Have you ever played a popular game name ...
- python解无忧公主数学题107.py
python解无忧公主数学题107.py """ python解无忧公主数学题107.py http://mp.weixin.qq.com/s?__biz=MzI5ODE ...
- python解无忧公主数学题108
""" python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...
- HDU 圆桌会议 - 数学题
圆桌 题意就是每分钟可以将相邻的两个人的位置互换一下 , 问你 ,几分钟可以将所有人的位置互换成 原先的 B 在A的右边 C在A的左边 , 换成现在的 C 在A 的右边 , B 在 A 的 ...
- HDU 2529 Shot (物理数学题)
题目 解题过程: //物理数学题 #include<stdio.h> #include<string.h> #include<algorithm> using na ...
- HDU 2671 Can't be easier(数学题,点关于直线对称)
题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...
- ACM之数学题
数学题,始终记得,第一次被带飞师大校赛以及省赛,毫无例外的在数学题上卡死....因此,现在开始,有意识的保留遇见的数学题...(下列知识点按遇见先后顺序排列: 1欧拉公式 欧拉公式的用处是,找出小于N ...
- hdu 5587 Array 数学题
Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5587 De ...
随机推荐
- textarea点击蓝色背景,黄色条,input点击黄色条,如何去掉?
textarea:focus{ background: #ffff outline:none; } input:focus{ oulilne:none; }
- Linux中安装Mysql授权远程访问
一.直接授权 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OP ...
- P3784 [SDOI2017]遗忘的集合
非常神仙的一道题! 题意:给出某n个数字跑完全背包m容量的dp数组,求满足要求的字典序最小的n个元素,不知道n是多少. 首先考虑付公主的背包这个题. 对dp数组求一个ln,设它为F. 已知 e^(G1 ...
- python-day76--django-Form组件
django中Form组件 1. 用户请求数据验证 2. 自动生成错误信息 3. 打包用户提交正确信息 4. 错误:保留上次输入内容 5. 定制页面上显示的HTML标签 引入: from django ...
- PAT 1019 General Palindromic Number
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- Visual Studio编译时报错“函数名:重定义;不同的基类型”
错误原因: 方法在还未声明的地方就使用了.由于使用的地方与定义的地方都是在同一个.c文件中,所以没有报未声明的错误. 解决方法: 把实现放到使用的前面,或者在include语句和宏定义后面加上函数声明 ...
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'list' in 'c
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'list' in ' ...
- C++三大特性 封装 继承 多态
C++ 三大特性 封装,继承,多态 封装 定义:封装就是将抽象得到的数据和行为相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成类,其中数据和函数都是类的成员,目的在于将对 ...
- Python购物车
product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...
- 【转】Entity Framework Extended Library (EF扩展类库,支持批量更新、删除、合并多个查询等)
E文好的可以直接看https://github.com/loresoft/EntityFramework.Extended 也可以在nuget上直接安装这个包,它的说明有点过时了,最新版本已经改用对I ...