AtCoder284 D - Happy New Year 2023

AtCoder284 D - Happy New Year 2023

[Editorial](Editorial - AtCoder Beginner Contest 284)

You are given a positive integer \(N\). It is known that \(N\) can be represented as \(N=p^2q\) using two different prime numbers \(p\) and \(q\).

Find \(p\) and \(q\).

You have \(T\) test cases to solve.

首先,我们易知 \(min(p,q)\le \sqrt[3]{n}\),于是时间复杂度 \(O(\sqrt[3]n)\),最大时为 \(10^6\) 左右。

因为 \(N=p^2q\)\(p,q\) 都是质数,我们知道这样的分解是唯一的,故找到一组就是答案。

枚举只能是 \(p,q,p^2,pq\) 四种数。且 \(p,q<p^2,pq\) 故先找到的数只能是 \(p\)\(q\)

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;
        
        ll p = 0, q = 0;
        for (ll i = 2; i * i * i <= n; i++) {
            if (n % i != 0) continue;
            if ((n / i) % i == 0) {
                p = i;
                q = n / i / i;
            } else {
                q = i;
                p = (ll) round(sqrt(n / i));
            }
        }
        cout << p << ' ' << q << endl;
    }
}
posted @ 2023-01-09 16:30  Vegdie  阅读(50)  评论(0编辑  收藏  举报