CF1444A Division(唯一分解定理)

题目传送门

题目大意

给定\(t(1\leq t\leq 50)\)组询问,每组给出两个数\(p(1\leq p\leq 10^{18})\)\(q(1\leq q\leq 10^{9})\),要求找出最大的整数\(x\),使得\(p\)可被\(x\)整除,且\(x\)不可被\(q\)整除。

Solution

根据唯一分解定理,只要枚举\(q\)的素因子,让\(p\)除这些素因子,直到\(p\)不能被\(q\)整除即可。例如\(q\)有个素因子\(P\)幂次为\(k(k \geq 1)\),只要让\(p\)中素因子\(P\)的幂次为\(k-1\)即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int T;
ll p,q,temp,ans;
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld",&p,&q);
        if(p<q||p%q!=0){
        printf("%lld\n",p);
        continue;
        }
        temp=1;
        ans=1;
        for(ll i=2;i*i<=q;i++){
            if(q%i==0){
                temp=1;
                while(q%i==0){
                    temp*=i;
                    q/=i;
                }
                temp/=i;
                ll pp=p;
                while(pp%i==0)pp/=i;
                ans=max(ans,pp*temp);
            }
        }
        if(q>1){
            while(p%q==0)p/=q;
            ans=max(ans,p);
        }
        printf("%lld\n",ans); 
    }
    return 0;
}
posted @ 2021-01-20 14:43  lzc2001  阅读(87)  评论(0)    收藏  举报