結果
| 問題 |
No.14 最小公倍数ソート
|
| ユーザー |
anta
|
| 提出日時 | 2014-10-31 02:34:41 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 5,000 ms |
| コード長 | 4,405 bytes |
| コンパイル時間 | 1,235 ms |
| コンパイル使用メモリ | 96,672 KB |
| 実行使用メモリ | 6,784 KB |
| 最終ジャッジ日時 | 2024-12-30 14:55:16 |
| 合計ジャッジ時間 | 2,032 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:113:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
113 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
main.cpp:115:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
115 | rep(i, N) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
ソースコード
/*
O(X log log X + Σ_i σ(a[i])) where X = 10000 (a[i]の上限), σ(x) = xの約数の数
(なはず)!
シンプルに O(N + X^{1+\epsilon}) ということもできるはず。
*/
#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <bitset>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
vector<int> smallestPrimeFactor;
void getSmallestPrimeFactors(int n) {
n = max(n, 1);
if((int)smallestPrimeFactor.size() >= n+1) return;
smallestPrimeFactor.assign(n+1, 0);
smallestPrimeFactor[1] = 1;
rer(i, 2, n) if(!smallestPrimeFactor[i]) {
smallestPrimeFactor[i] = i;
if((ll)i * i <= n)
for(int j = i * i; j <= n; j += i)
if(!smallestPrimeFactor[j]) smallestPrimeFactor[j] = i;
}
}
typedef int FactorsInt;
typedef vector<pair<FactorsInt,int> > Factors;
void primeFactors(FactorsInt x, Factors &out_v) {
getSmallestPrimeFactors(x);
out_v.clear();
while(x != 1) {
int p = smallestPrimeFactor[x], k = 0;
x /= p, k ++;
while(x % p == 0) x /= p, k ++;
out_v.push_back(make_pair(p, k));
}
}
void getDivisors(FactorsInt x, vector<FactorsInt> &out_v) {
Factors fs;
primeFactors(x, fs);
out_v.assign(1, 1);
rep(i, fs.size()) {
for(int j = (int)out_v.size()-1; j >= 0; j --) {
FactorsInt x = out_v[j];
rep(k, fs[i].second) {
x *= fs[i].first;
out_v.push_back(x);
}
}
}
// sort(all(out_v));
}
struct Node {
Node *next, *prev;
Node(): next(0), prev(0) { }
};
void insertToList(Node *&head, Node *t) {
if(t->next = head)
head->prev = t;
head = t;
}
void removeFromList(Node *&head, Node *t) {
if(t->prev)
t->prev->next = t->next;
if(t->next)
t->next->prev = t->prev;
if(head == t)
head = t->next;
t->prev = t->next = 0;
}
int main() {
int N;
scanf("%d", &N);
vector<int> a(N);
rep(i, N) scanf("%d", &a[i]);
const int X = 10000;
getSmallestPrimeFactors(X);
vector<vi> divisors(N);
rep(i, N)
getDivisors(a[i], divisors[i]);
vector<vi> indices(X+1);
rep(i, N)
indices[a[i]].push_back(i);
vector<int> offsets(N);
int numTotalDivisors = 0;
rep(i, N) {
offsets[i] = numTotalDivisors;
numTotalDivisors += divisors[i].size();
}
vector<int> invoffsets(numTotalDivisors);
rep(i, N) rep(j, divisors[i].size())
invoffsets[offsets[i] + j] = i;
vector<Node> nodes(numTotalDivisors);
vector<Node*> heads(X+1, nullptr);
for(int x = X; x >= 1; -- x) each(it, indices[x]) {
int i = *it;
rep(j, divisors[i].size()) {
int d = divisors[i][j];
insertToList(heads[d], &nodes[offsets[i] + j]);
}
}
int curi = 0;
printf("%d", a[curi]);
rep(k, N-1) {
rep(j, divisors[curi].size()) {
int d = divisors[curi][j];
removeFromList(heads[d], &nodes[offsets[curi] + j]);
}
pair<pii,int> p(mp(INF, INF), -1);
rep(j, divisors[curi].size()) {
int d = divisors[curi][j];
Node *t = heads[d];
if(t != nullptr) {
int i = invoffsets[t - &nodes[0]];
amin(p, mp(mp(a[i] / d, a[i]), i));
}
}
curi = p.second;
printf(" %d", a[curi]);
}
puts("");
return 0;
}
anta