結果

問題 No.2449 square_permutation
ユーザー stoqstoq
提出日時 2023-03-04 19:35:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,085 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 208,324 KB
実行使用メモリ 20,468 KB
最終ジャッジ日時 2023-08-31 20:30:14
合計ジャッジ時間 6,754 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,436 KB
testcase_01 AC 6 ms
5,376 KB
testcase_02 AC 6 ms
5,368 KB
testcase_03 AC 6 ms
5,320 KB
testcase_04 AC 6 ms
5,328 KB
testcase_05 AC 6 ms
5,368 KB
testcase_06 AC 48 ms
10,208 KB
testcase_07 AC 53 ms
10,792 KB
testcase_08 AC 58 ms
11,320 KB
testcase_09 AC 65 ms
12,300 KB
testcase_10 AC 90 ms
15,332 KB
testcase_11 AC 104 ms
15,920 KB
testcase_12 AC 107 ms
16,904 KB
testcase_13 AC 140 ms
20,320 KB
testcase_14 AC 142 ms
20,468 KB
testcase_15 AC 142 ms
20,240 KB
testcase_16 AC 139 ms
20,212 KB
testcase_17 AC 142 ms
20,252 KB
testcase_18 AC 129 ms
19,036 KB
testcase_19 AC 140 ms
20,056 KB
testcase_20 AC 134 ms
20,308 KB
testcase_21 AC 134 ms
20,328 KB
testcase_22 AC 6 ms
5,320 KB
testcase_23 AC 140 ms
20,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MAX_N = 5e5;
int can_div[MAX_N] = {};

struct init_prime {
  init_prime() {
    can_div[1] = -1;
    for (int i = 2; i < MAX_N; i++) {
      if (can_div[i] != 0) continue;
      for (int j = i + i; j < MAX_N; j += i) can_div[j] = i;
    }
  }
} init_prime;

inline bool is_prime(int n) {
  if (n <= 1) return false;
  return !can_div[n];
}

void factorization(int n, unordered_map<int, int> &res) {
  if (n <= 1) return;
  if (!can_div[n]) {
    ++res[n];
    return;
  }
  ++res[can_div[n]];
  factorization(n / can_div[n], res);
}

int main() {
  int n;
  cin >> n;
  vector<int> odd(n + 1);
  vector<vector<int>> v(n + 1);
  for (int i = 1; i <= n; i++) {
    unordered_map<int, int> F;
    factorization(i, F);
    int q = 1;
    for (auto [p, e] : F) {
      if (e & 1) q *= p;
    }
    odd[i] = q;
    v[q].push_back(i);
  }
  vector<int> ans(n);
  for (int i = 1; i <= n; i++) {
    ans[i - 1] = v[odd[i]].back();
    v[odd[i]].pop_back();
  }
  for (int i = 0; i < n; i++) cout << ans[i] << (i + 1 == n ? "\n" : " ");
}
0