Codeforces 1445C. Division(质因数)

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<bits/stdc++.h>
#define int long long
using 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<<ans<<endl;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值