题意:

找一个最大的数X,使p%x==0且x%q!=0,题目保证至少有一个答案满足题意。

题目:

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.

Example

Input

3
10 4
12 6
179 822

Output

10
4
179

Note

For the first pair, where p1=10 and q1=4, the answer is x1=10, since it is the greatest divisor of 10 and 10 is not divisible by 4.

For the second pair, where p2=12 and q2=6, note that

12 is not a valid x2, since 12 is divisible by q2=6;
6 is not valid x2 as well: 6 is also divisible by q2=6.
The next available divisor of p2=12 is 4, which is the answer, since 4 is not divisible by 6.

分析:

1.先说当题目至少满足一个X,使之满足条件,分析可得,这个值为1,需要特判;
2.我在大犇博客借鉴的,嘻嘻:

显然,当 p与q不满足整除关系(p%q != 0) 时,最大的x就是p本身,这个简单。
但是当 p % q == 0 时怎么办呢?
不难发现,当 p % q == 0 时,
我们令 q = A * B ,
则 p = An * Bn * C , 其中C是一个与A,B均不满足整除关系的整数。
那么,从每一个 x = p / An 中找到的x(max)不就是结果了吗?
显然,A、B的集合就是q的所有因子,我们只需要O(sqrt(n))的时间来找出它们。
对于An,我们只需要让p不断的除以因子A直到p不再能被q整除就行了。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,t,ans;
ll solve(int x){if(x==1) return  x;ll num=n;while(num%m==0)num/=x;return num;
}
void dfs(ll x){for(int i=1;i*i<=x;i++){if(x%i==0){int a=i,b=x/i;ans=max(ans,solve(a));ans=max(ans,solve(b));}}
}
int main(){cin>>t;while(t--){ans=0;cin>>n>>m;dfs(m);if(ans!=0)cout<<ans<<endl;else cout<<"1"<<endl;}return 0;
}

Division CodeForces - 1445C(数论因子相关)相关推荐

  1. 信奥中的数学 数论篇 相关资料汇总(2022.07.07)

    数论入门书籍推荐 数论入门书籍推荐_dllglvzhenfeng的博客-CSDN博客_数论入门应该看什么书 数学女孩系列书籍 数学女孩系列书籍_dllglvzhenfeng的博客-CSDN博客 信息学 ...

  2. Codeforces数学1600day3[数学CodeForces - 1213D2, CodeForces - 1165E 数论,CodeForces - 1165D 因子分解]

    ps:day2太水了不写qwq A - Equalizing by Division (hard version) CodeForces - 1213D2 题目大意:给你n个数和一个k,然后你可以执行 ...

  3. Codeforces 396A 数论,组合数学

    题意:给一个a数组,求b 数组的方案数,但是要求两者乘积相同. 分析: 不可能将它们乘起来,对于每个数质因数分解,得到每个质因子个数,遍历这些质因子,将某个质因子放到 对应的盒子里面,可以不放,方案数 ...

  4. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  5. 数学--数论--因子和线性筛 (模板)

    ACM常用模板合集 #include <bits/stdc++.h> using namespace std; typedef long long ll; #define N 500022 ...

  6. 洛谷 数论入门相关题目--2022.01.22

    P2926 [USACO08DEC]Patting Heads S 拍头 https://www.luogu.com.cn/problem/P2926 P3383 [模板]线性筛素数 https:// ...

  7. Codeforces 1114C(数论+唯一分解)

    题目链接:传送门 解题思路:y1s1,拿到这题我脑袋中只有暴力,观摩了别人的博客,学到了点东西.对于本题,我们可以知道,在b进制后有几个0表示的是这个数是b的几次方的倍数,于是题目便转化为了求n的阶乘 ...

  8. 数论——余数相关定理

    1.加法律:(a+b) mod c = ((a mod c) + (b mod c)) mod c. 2.减法律:(a-b) mod c = ((a mod c) - (b mod c)) mod c ...

  9. 辣鸡(ljh) NOIP模拟赛 模拟 平面几何 数论 化学相关(雾)

    [题目描述] 辣鸡ljhNOI之后就退役了,然后就滚去学文化课了. 然而在上化学课的时候,数学和化学都不好的ljh却被一道简单题难住了,受到了大佬的嘲笑. 题目描述是这样的:在一个二维平面上有一层水分 ...

最新文章

  1. await使用中的阻塞和并发(一)
  2. python 第一安装 https网络请求问题
  3. 推荐 查公司信息的一个网站
  4. goroutine sync.RWMutex读写锁RLock的使用
  5. 手机版ziperello_Ziperello
  6. java 并发 变量_实例讲解Java并发编程之变量
  7. 芯片业巨震!英特尔拟90亿美元卖掉NAND闪存业务
  8. Java实验9 T6.将对象和数组存储在dat文件中
  9. java 分卷 zip
  10. 风渠全能永久免费进销存,无功能限制,网络+单机组合版本+云商平台管理系统,风渠ERP。(源代码)
  11. 苹果mac休眠快捷键_有人说 Mac 系统不适合搞科研,我笑了:还不是因为你不会用!...
  12. android 控制空调,控制精灵空调遥控器
  13. 图像篡改入门02 利用空间结构篡改定位
  14. 1. Arthas的命令
  15. 如何实现web系统检测浏览器类型的功能
  16. pfx格式是什么文件
  17. 不要再重复造轮子了,Hutool这款开源工具类库贼好使
  18. Liunx常用命令速查
  19. JVM垃圾回收算法与原理详解
  20. 宋晓丽20190919-5 代码规范,结对要求

热门文章

  1. linux之怎么使vim永久显示行号
  2. 【C语言简单说】十九:二维数组循环嵌套(2)
  3. 安卓效率微商_微商人脉通app下载-微商人脉通 安卓版v2.5.5-PC6安卓网
  4. 一个程序如何连接到外网_如何开发制作小程序?做一个电商带直播小程序
  5. 中国9大民居建筑,你都认识吗?
  6. 傅立叶变换是如何改变我们生活的? ——四个角度告诉你答案
  7. 这道题号称无人能解!300多年来无一人答对,却让这群人这么简单就解出来了?...
  8. hadoop安装hive及配置mysql_Hadoop系列之Hive(数据仓库)安装配置
  9. android 栏目编辑,android – 编辑文本导致内存泄漏
  10. micropython安装ros_ROS2与STM32入门教程-microROS的linux版本