結果

問題 No.1063 ルートの計算 / Sqrt Calculation
ユーザー yomo3yomo3
提出日時 2020-06-02 13:29:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,161 bytes
コンパイル時間 1,567 ms
コンパイル使用メモリ 169,224 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 11:38:54
合計ジャッジ時間 2,439 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

inline int ctz(int n) { return __builtin_ctzl(n); }
inline int ctz(long long n) { return __builtin_ctzll(n); }

template<class T> vector<pair<T,int>> factorize(T n)
{
    vector<pair<T,int>> factors;
    int c;

    if ((c = ctz(n)) > 0) {
        factors.emplace_back(2, c);
        n >>= c;
    }

    T lim = sqrt(n) + 0.1;
    for (T p = 3; p <= lim; p += 2) {
        c = 0;
        while (n % p == 0) {
            n /= p;
            c++;
        }
        if (c != 0)
            factors.emplace_back(p, c);
    }
    if (n != 1)
        factors.emplace_back(n, 1);
    
    return factors;
}

template <class INT_T> INT_T powi(INT_T b, INT_T x) {
    if (x == 0) return 1;
    if (x == 1) return b;
    if (x & 1) return powi(b*b, x/2) * b;
    return powi(b*b, x/2);
}

int main()
{
    int n; cin >> n;
    vector<pair<int, int>> factors = factorize(n);

    int a = 1, b = 1;
    for (const auto& f : factors) {
        if (f.second / 2) {
            a *= powi(f.first, f.second/2);
        }
        if (f.second & 1) {
            b *= f.first;
        }
    }

    cout << a << " " << b << endl;
}
0