結果
問題 |
No.2449 square_permutation
|
ユーザー |
![]() |
提出日時 | 2023-09-08 14:43:50 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 131 ms / 2,000 ms |
コード長 | 1,628 bytes |
コンパイル時間 | 591 ms |
コンパイル使用メモリ | 61,240 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-26 07:48:59 |
合計ジャッジ時間 | 4,461 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
/* -*- coding: utf-8 -*- * * 2449.cc: No.2449 square_permutation - yukicoder */ #include<cstdio> #include<cmath> #include<vector> #include<algorithm> #include<utility> using namespace std; /* constant */ const int MAX_N = 300000; /* typedef */ typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pii> vpii; /* global variables */ bool used[MAX_N + 1]; bool primes[MAX_N + 1]; /* subroutines */ int gen_primes(int maxp, vi &pnums) { fill(primes, primes + maxp + 1, true); primes[0] = primes[1] = false; int p; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums.push_back(p); for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums.push_back(p); return (int)pnums.size(); } bool prime_decomp(int n, vi &pnums, vpii& pds) { pds.clear(); int pn = pnums.size(); for (int i = 0; i < pn; i++) { int pi = pnums[i]; if (pi * pi > n) { if (n > 1) pds.push_back(pii(n, 1)); return true; } if (n % pi == 0) { int fi = 0; while (n % pi == 0) n /= pi, fi++; pds.push_back(pii(pi, fi)); } } return false; } /* main */ int main() { vi pnums; gen_primes(MAX_N, pnums); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { vpii pds; prime_decomp(i, pnums, pds); int j = 1; for (auto &pd: pds) if (pd.second & 1) j *= pd.first; int x = sqrt(n / j + 0.5), m = x * x * j; while (m > 0 && used[m]) { x--; m = x * x * j; } used[m] = true; printf("%d%c", m, (i < n) ? ' ' : '\n'); } return 0; }