A. Division

time limit per test1 second

memory limit per test512 megabytes

inputstandard input

outputstandard 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 \(p_i\) and \(q_i\) and for each pair decided to find the greatest integer \(x_i\), such that:

\(p_i\) is divisible by \(x_i\);

\(x_i\) is not divisible by \(q_i\).

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\leq t\leq 50)\) — the number of pairs.

Each of the following \(t\) lines contains two integers \(p_i\) and \(q_i\) (\(1≤p_i≤10^18\); \(2≤q_i≤10^9\)) — the \(i-th\) pair of integers.

Output

Print \(t\) integers: the \(i-th\) integer is the largest \(x_i\) such that \(p_i\) is divisible by \(x_i\), but xi is not divisible by \(q_i\).

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

SOLUTION

这还算一道良心数学题

首先我们发现,我们先把p, q质因数分解,有如下结果:

\(p = a_1^p_1 \times a_2^p_2\times ... \times a_m^p_m \times ... a_n^p_n\)

\(q = a_1^q_1 \times a_2^q_2\times ... \times a_m^q_m\)

其中, \(m<n\)

注意,当\(p mod q!=0\), 答案就是\(p\)

当\(p mod q=0\)

就是任意小于\(m\)的数\(i\),\(q_i <= p_i\)

于是,我们就只要考虑前\(m\)个质因数

我们只要对于\(p\)的任意一个质因数,\(p_i\)变成\(q_i-1\),就是一个符合条件的答案

求出最大的,我们只需要考虑变化代价最小的计算出来就好了

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define _(d) while(d(isdigit(ch=getchar())))
template <class T> void g(T&t){T x,f=1;char ch;_(!)ch=='-'?f=-1:f;x=ch-48;_()x=x*10+ch-48;t=f*x;}
typedef unsigned long long ll;
const int N = 1e5+4;
int pri[N], tot, vis[N];
ll p,q,ans;
void pre(){
int End = sqrt(1e9)+1;
for(int i=2; i <= End; i++){
if(!vis[i]) pri[++tot]=i;
for(int j=1; j <= tot; j++){
if( i*pri[j] > End ) break;
vis[i*pri[j]] = 1;
if( i%pri[j] == 0 ) break;
}
}
} int st[N], tp, num1[N], num2[N];
ll s1[N], s2[N]; int main(){
int T; g(T);
pre();
// rep(i,1,10) cout<<pri[i]<<endl;
while(T--){
g(p), g(q);
if( p%q ) ans = p;
else{
tp = 0; ll nowq = q; ans = 0;
for( int i=1; pri[i]*pri[i] <= nowq && i<=tot; i++ ){
if( nowq % pri[i] == 0 ){
st[++tp] = pri[i]; s1[tp] = 1;
while( nowq % pri[i] == 0 ){
// puts("orz");
nowq /= pri[i];
s1[tp] *= pri[i];
}
}
}
if( nowq >1 ) st[++tp] = nowq, s1[tp] = nowq;
ll nowp = p;
for( int i=1; i <= tp; i++ ){
ll tmp = nowp / s1[i]; s2[i] = s1[i];
while( tmp % st[i] == 0 ){
// puts("orz");
s2[i] *= st[i];
tmp /= st[i];
}
nowp = tmp;
}
ll mn = 1e18;
for( int i=1; i <= tp; i++ ){
// cerr<<s1[i]<<endl;
s2[i] = s2[i]/s1[i]*st[i];
mn = min( mn, s2[i] );
}
ans = p/mn;
}
printf("%llu\n",ans);
}
return 0;
}

水题挑战6: CF1444A DIvision的更多相关文章

  1. 水题挑战4: luogu P1280 尼克的任务

    题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成. 尼克的一个工作日为 \(n\) 分钟,从 ...

  2. 【做题记录】CF1444A Division

    CF1444A Division 题意: 给定 \(t\) 组询问,每组给两个数 \(p_i\) 和 \(q_i\) ,找出最大的整数 \(x_i\) ,要求 \(p_i\) 可被 \(x_i\) 整 ...

  3. 水题挑战3: NOIP 2017 宝藏

    参与考古挖掘的小明得到了一份藏宝图,藏宝图上标出了 \(n\) 个深埋在地下的宝藏屋, 也给出了这 \(n\) 个宝藏屋之间可供开发的 \(m\) 条道路和它们的长度. 小明决心亲自前往挖掘所有宝藏屋 ...

  4. 水题挑战1:NOIP 2013 选择客栈

    丽江河边有\(n\) 家很有特色的客栈,客栈按照其位置顺序从 \(1\) 到 \(n\) 编号.每家客栈都按照某一种色调进行装饰(总共 \(k\) 种,用整数 \(0 \sim k-1\) 表示),且 ...

  5. 水题挑战2 :NOIP提高组 2011 聪明的质监员

    小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 \(n\) 个矿石,从\(1\) 到 \(n\) 逐一编号,每个矿石都有自己的重量 \(w_i\) 以及价值 \(v_i\) .检验矿 ...

  6. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem A: The 3n + 1 problem(水题)

    Problem A: The 3n + 1 problem Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 14  Solved: 6[Submit][St ...

  7. 烟大 Contest1025 - 《挑战编程》第二章:数据结构 Problem A: Jolly Jumpers(水题)

    Problem A: Jolly Jumpers Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 10  Solved: 4[Submit][Status] ...

  8. codeforcess水题100道

    之所以在codeforces上找这100道水题的原因是为了巩固我对最近学的编程语言的掌握程度. 找的方式在codeforces上的PROBLEMSET中过的题最多的那些题里面出现的最前面的10个题型, ...

  9. CF1444A Division 求质因数的方法

    2020.12.20 求质因数的方法 CF1444A Division #include<bits/stdc++.h> #define ll long long #define fp(i, ...

随机推荐

  1. NOI 2011 【阿狸的打字机】

    之前讲了[AC自动姬],今天我终于把这题给刚下来了...嗯,来给大家讲一讲. 题目描述: 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工 ...

  2. 如何在Windows7安装U盘中加入USB3.0驱动的支持

         安装前请务必备份好您硬盘中的重要数据. 一.在Windows7安装U盘中加入USB3.0驱动的支持 故障现象: 原生Win7系统不包含USB3.0的驱动,所以无法使用USB3.0的U盘在US ...

  3. 高性能HTTP加速器Varnish--基础知识

    一.Varnish 概述 Varnish 是一款高性能且开源的反向代理服务器和HTTP加速器,它的开发者 Poul-Henning Kamp 是 FreeBSD 核心的开发人员之一. 与传统的 Squ ...

  4. BeetleX之webapi自定义响应内容

    输出内容多样性在webapi服务中比较普遍的,有的情况使用json,xml,图片和二进制流下载等等:为了适应用不同情况的需要,组件支持自定义内容输出.接下来的主要描述组件在webapi如何定义各种内容 ...

  5. 多测师讲解接口测试 _windows中搭建环境cms_高级讲师肖sir

    eclipse集成开发环境 搭建开发环境需要安装的工具如下 jdk-8u60-windows-x64.exe        jdk eclipse.rar      集成开发框架 mysql-inst ...

  6. linux centos 02

    1.PS1变量,命令提示符的修改 PS1="[\u@\h \W]\$" \u  代表 用户 @   占位符 \h  主机名 \W   工作路径的最后一位 \t  \w   工作路径 ...

  7. 什么是C和C++标准——小白必看

    本文简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了 ...

  8. 无法访问GitHub

    我们开发者经常用的最大的同性交流平台--GitHub忽然访问不了了,很尴尬 可以打开控制台 ping一下 github.com 果不其然 不通 不过幸运的是里面有github的ip地址,好像是美国某个 ...

  9. BASH提示符颜色、显示返回值,终端标题显示当前目录与正在执行的命令

    BASH的PS1变量控制提示符相关的东西,善用它可以让BASH用起来舒服很多 提示符颜色 提示符显示上一个命令的返回值(exit code),并根据是否0调整颜色 提示符生成的时间(这样就知道上一条命 ...

  10. centos8平台使用blkid查看分区信息

    一,blkid的用途 blkid 命令是一个命令行工具,它可以显示关于可用块设备的信息 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/archite ...