题目
- 题意
给你两个数 p , q ~p,q~ p,q ,让你找到最大的一个数 x ~ x~ x ,使得 x ~x~ x 整除 p ~p~ p ,但是不满足 q ~q~ q 整除 x ~x~ x
- 思路
显然当 p % q ! = 0 ~p\%q!=0~ p%q!=0 时,答案就是 p ~p~ p ;但是当 p % = = 0 时 ~p\%==0时~ p%==0时 ,如何处理,这里套路使然嘛,肯定要往 算术基本定理 上想:很显然 p ~p~ p 包含的质因子, p ~p~ p 都包含,所以我们可以处理出 q ~q~ q 的质因子,然后不断用 p ~p~ p 除以 q ~q~ q 的质因子,直到 p % q ! = 0 ~p\%q!=0~ p%q!=0 ,同时记录最大值。
时间复杂度 O ( 1 e 9 ) ~O(\sqrt{1e9})~ O(1e9)
注:这里为什么要处理q的质因子,因为 1 e 9 ~1e9~ 1e9 的开根号 1 e 4.5 ~1e4.5~ 1e4.5 .。
处理 q ~q~ q 的质因子也不能把素数筛出来,哎,还是对这样的题不熟吧,应该用下面的方法,自己看吧。
- 代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e7+10,mod=1e9+7;
int a[N];
#define PII pair<int,long double>
#define x first
#define y second
#define push_back push_back
void solve()
{
ll a,b;cin>>a>>b;
if(a%b!=0){
cout<<a<<endl;
}
else
{
ll ans=0;
vector<int> v; v.clear();
ll tmp=b;
for(ll i=2;i*i<=b;i++){
if(tmp%i) continue;
while(tmp%i==0) tmp/=i;
v.push_back(i);
}
if(tmp>1) v.push_back(tmp);
for(int i=0;i<v.size();i++){
tmp=a;
while(tmp%v[i]==0 && tmp%b==0) tmp/=v[i];
ans=max(ans,tmp);
}
cout<<ans<<endl;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
solve();
}