
Codeforces 1445C. Division(质因数)
发布日期:2021-05-08 22:04:06
浏览次数:21
分类:精选文章
本文共 1980 字,大约阅读时间需要 6 分钟。
C. Division
time limit per test1 second
memory limit per test512 megabytes input standard input output standard output Oleg’s favorite subjects are History and Math, and his favorite branch of mathematics is division.To improve his division skills, Oleg came up with t pairs of integers
pi and qi and for each pair decided to find the greatest integer xi, such that:pi is divisible by xi; xi is not divisible by qi. Oleg is really good
at division and managed to find all the answers quickly, how about you? Input The first line contains an integer t (1≤t≤50) — the number of pairs.Each of the following t lines contains two integers pi and qi
(1≤pi≤1018; 2≤qi≤109) — the i-th pair of integers.Output Print t integers: the i-th integer is the largest xi such that
pi is divisible by xi, but xi is not divisible by qi.One can show that there is always at least one value of xi satisfying
the divisibility conditions for the given constraints.
题意:给两个数p,q,求出最大的数x使得(p%x=0)且(q%x!=0)
大致解法:从2开始到sqrt(q)寻找n可以让 ((p/(n^x))%q!=0) 或 (p/((q/n)^x))%q!=0) 成立的n值,找出最大的(p/(n^x))或 (p/((q/n)^x 代码如下~#pragma GCC optimize("Ofast")#pragma GCC target("avx,avx2,fma")#pragma GCC optimization ("unroll-loops")#include#define int long longusing namespace std;signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int _; cin >> _; while(_--) { int p, q; int ans; ans=0; cin >> p >> q; if(p % q != 0)cout << p << endl; else { int tmp; for(int i=1;i*i<=q;i++){ if(q%i==0){ if(i!=1){ tmp=p; while(tmp%q==0){ tmp/=i; } ans=max(ans,tmp); } tmp=p; while(tmp%q==0){ tmp/=q/i; } ans=max(ans,tmp); } } cout< <
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月09日 05时00分36秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java ThreadPoolExecutor初探
2021-05-09
快速指数算法
2021-05-09
python去除字符串中的特殊字符(爬虫存储数据时会遇到不能作为文件名的字符串)
2021-05-09
SpringCloud微服务(03):Hystrix组件,实现服务熔断
2021-05-09
Spring 框架基础(01):核心组件总结,基础环境搭建
2021-05-09
Cassandra数据建模
2021-05-09
Internet Explorer 10 专题上线
2021-05-09
云计算之路-阿里云上:0:25~0:40网络存储故障造成网站不能正常访问
2021-05-09
网站故障公告1:使用阿里云RDS之后一个让人欲哭无泪的下午
2021-05-09
上周热点回顾(6.3-6.9)
2021-05-09
上周热点回顾(8.12-8.18)
2021-05-09
【故障公告】升级阿里云 RDS SQL Server 实例故障经过
2021-05-09
蹒跚来迟:新版博客后台上线公测
2021-05-09
[网站公告]11月26日00:00-04:00阿里云RDS升级
2021-05-09
[网站公告]又拍云API故障造成图片无法上传(已恢复)
2021-05-09
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
2021-05-09
上周热点回顾(6.9-6.15)
2021-05-09
上周热点回顾(10.20-10.26)
2021-05-09
上周热点回顾(2.16-2.22)
2021-05-09
上周热点回顾(3.2-3.8)
2021-05-09