結果

問題 No.2449 square_permutation
ユーザー merom686merom686
提出日時 2023-08-31 21:45:02
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,191 bytes
コンパイル時間 4,615 ms
コンパイル使用メモリ 245,772 KB
実行使用メモリ 5,736 KB
最終ジャッジ日時 2023-08-31 21:45:12
合計ジャッジ時間 8,279 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 17 ms
4,376 KB
testcase_07 AC 19 ms
4,376 KB
testcase_08 AC 21 ms
4,380 KB
testcase_09 AC 23 ms
4,428 KB
testcase_10 AC 33 ms
4,912 KB
testcase_11 AC 36 ms
4,976 KB
testcase_12 AC 38 ms
5,228 KB
testcase_13 AC 48 ms
5,684 KB
testcase_14 AC 48 ms
5,640 KB
testcase_15 AC 50 ms
5,724 KB
testcase_16 AC 48 ms
5,632 KB
testcase_17 AC 49 ms
5,736 KB
testcase_18 AC 44 ms
5,404 KB
testcase_19 AC 48 ms
5,656 KB
testcase_20 AC 48 ms
5,636 KB
testcase_21 AC 48 ms
5,648 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 49 ms
5,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Sieve {
    Sieve(int n) : l(n + 1, 0) {
        for (int i = 2; i <= n; i++) {
            if (!l[i]) p.push_back(l[i] = i);
            for (int q : p) {
                if (q > l[i]) break;
                if (int j = i * q; j <= n) l[j] = q; else break;
            }
        }
    }
    bool is_prime(int i) {
        return l[i] == i;
    }
    vector<int> l, p;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
        a[i] = i;
    }

    Sieve si(n);
    for (int i = 1; i <= n; i++) {
        int t = i, s = 1;
        while (t > 1) {
            int l = si.l[t];
            int k = 0;
            while (t % l == 0) t /= l, k++;
            if (k & 1) s *= l;
        }
        for (int j = (int)sqrt(n / s); j > 0; j--) {
            int r = j * j * s;
            if (r <= i) break;
            if (a[r] == r) {
                swap(a[i], a[r]);
                break;
            }
        }
    }

    for (int i = 1; i <= n; i++) {
        cout << a[i] << " \n"[i == n];
    }

    return 0;
}
0