結果

問題 No.14 最小公倍数ソート
ユーザー antaanta
提出日時 2014-10-31 02:34:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 4,405 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 92,332 KB
実行使用メモリ 6,512 KB
最終ジャッジ日時 2023-08-28 23:36:00
合計ジャッジ時間 2,012 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 11 ms
6,512 KB
testcase_05 AC 7 ms
5,256 KB
testcase_06 AC 8 ms
5,168 KB
testcase_07 AC 8 ms
5,368 KB
testcase_08 AC 10 ms
6,216 KB
testcase_09 AC 11 ms
6,228 KB
testcase_10 AC 11 ms
6,208 KB
testcase_11 AC 11 ms
6,196 KB
testcase_12 AC 11 ms
6,196 KB
testcase_13 AC 12 ms
6,324 KB
testcase_14 AC 12 ms
6,204 KB
testcase_15 AC 12 ms
6,324 KB
testcase_16 AC 7 ms
5,176 KB
testcase_17 AC 7 ms
4,892 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 9 ms
5,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	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;
}
0