Codeforces Round #680 (Div. 2)C. Division

Codeforces Round #680 (Div. 2, based on Moscow Team Olympiad)C. Division

题意

让你找到最大的\(x\)满足

\[p_i \% x == 0\\ x \% q_i !=0 \]

思路

分为三类情况:

如果 \(p < q\),那么直接输出\(p\)

如果\(p\%q!=0\),也是直接输出\(p\)

\(p\%q==0\)时,我们需要将\(p\)化为\(p\%q!=0\)的最大数,即为所求。

看到数据范围,我们考虑枚举q的因子。(分解质因数)

然后在想到约数定理,每个数必然是一系列素数的乘积。(分解质因数)

我们把\(p,q\)都分解为素数的乘积。

要使\(p\%q!=0,\)那么我们一定是要让\(p\)除以\(q\)的素因子

\(p\)\(q\)中相同素数的次幂中,\(p\)的小于\(q\)的即可符合\(p\%q!=0\)

\[P=p_1^{a1}×p_2^{a2}×p_3^{a3}*…*p_k^{ak}\\ Q=q_1^{a1}×q_2^{a2}×q_3^{a3}*…*q_k^{ak} \]

让P的素数次幂小于Q的素数次幂,则\(p\%q!=0\)

#include<bits/stdc++.h>

using namespace std;
#define int long long

void solve() {
    int p, q; cin >> p >> q;
    vector<int>fac;
    if(p < q) {
        cout << p << endl;
    }
    else {
        if(p % q != 0) cout << p << endl;
        else {
            int n = sqrt(q);
            int t = q;
            for (int i = 2; i <= n; ++i) {
                if(t % i ==0) {
                    fac.push_back(i);
                    while (t % i == 0) t /= i;
                }
            }
            if(t > 1) fac.push_back(t);

            int ans = 1;

            for (int i = 0; i < fac.size(); ++i) {
                t = p;
                while (t % q == 0) t /= fac[i];
                ans = max(ans, t);
            }
            cout << ans << endl;
        }
    }

}


signed main() {
    int t; cin >> t;
    while (t--) {
        solve();
    }

}
posted @ 2020-11-07 11:55  waryan  阅读(79)  评论(0编辑  收藏  举报