結果

問題 No.2449 square_permutation
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-09-01 10:07:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,088 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 211,140 KB
実行使用メモリ 20,624 KB
最終ジャッジ日時 2023-09-01 10:07:08
合計ジャッジ時間 7,808 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 55 ms
8,888 KB
testcase_07 AC 63 ms
9,388 KB
testcase_08 AC 69 ms
10,044 KB
testcase_09 AC 84 ms
11,420 KB
testcase_10 AC 120 ms
14,268 KB
testcase_11 AC 132 ms
15,240 KB
testcase_12 AC 137 ms
15,800 KB
testcase_13 AC 187 ms
20,600 KB
testcase_14 AC 185 ms
20,624 KB
testcase_15 AC 185 ms
20,412 KB
testcase_16 AC 185 ms
20,476 KB
testcase_17 AC 183 ms
20,336 KB
testcase_18 AC 166 ms
18,932 KB
testcase_19 AC 184 ms
20,196 KB
testcase_20 AC 189 ms
20,452 KB
testcase_21 AC 187 ms
20,448 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 188 ms
20,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

vector<ll> spf;
map<ll, ll> prime;

void osa_k(ll n){
    spf.resize(n+1);
    for (ll i=0; i<=n; i++) spf[i] = i;
    for (ll i=2; i*i<=n; i++){
        if (spf[i] == i){
            for (ll j=2; i*j <= n; j++){
                spf[i*j] = min(spf[i*j], i);
            }
        }
    }
}

void prime_factor(ll n){
    prime.clear();
    while(n != 1){
        prime[spf[n]]++;
        n /= spf[n];
    }
}

int main(){

    int N, x, y;
    cin >> N;
    osa_k(N+1);
    vector<vector<int>> v(N+1);
    for (int i=1; i<=N; i++){
        prime_factor(i);
        x = 1;
        for (auto [p, e] : prime){
            if (e % 2 == 1) x *= p;
        }
        v[x].push_back(i);
    }

    vector<int> ans;

    for (int i=1; i<=N; i++){
        prime_factor(i);
        x = 1;
        for (auto [p, e] : prime){
            if (e % 2 == 1) x *= p;
        }
        y = v[x].back();
        v[x].pop_back();
        ans.push_back(y);
    }

    for (auto x : ans) cout << x << " ";
    cout << endl;

    return 0;
}
0