結果

問題 No.14 最小公倍数ソート
ユーザー tashikanitashikani
提出日時 2015-06-18 22:20:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,207 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 98,492 KB
実行使用メモリ 4,904 KB
最終ジャッジ日時 2023-09-21 09:34:27
合計ジャッジ時間 60,548 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 TLE -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:104:18: warning: ‘index’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   cout << a[index] << " ";
                  ^

ソースコード

diff #

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(auto i: c)
#define SORT(c) sort((c).begin(),(c).end())

const double EPS = 1e-10;
const double PI  = acos(-1.0);

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

	int N;
	cin >> N;
	VI a(N);
	REP(i, N) cin >> a[i];
	sort(a.begin() + 1, a.end());

	VI prime;
	vector<bool> isPrime(101, true);
	isPrime[0] = isPrime[1] = false;
	REP(i, 100){
		if(isPrime[i]){
			prime.push_back(i);
			for(int j = i; j < 100; j += i) isPrime[j] = false;
		}
	}

	VVI div(N, VI(prime.size(), 0));
	VI b = a;
	REP(i, N){
		REP(j, prime.size()){
			while(b[i] % prime[j] == 0){
				div[i][j]++;
				b[i] /= prime[j];
			}
			if(b[i] == 1) break;
		}
	}

	vector<bool> done(N, false);
	int pre = 0, index;
	cout << a[0] << " ";
	done[0] = true;
	REP(i, N - 1){
		int m = 100000;
		REP(j, N){
			if(done[j]) continue;
			int tmp = 1;
			REP(k, prime.size()){
				int cnt = max(div[pre][k], div[j][k]);
				while(cnt > 0){
					tmp *= prime[k];
					cnt--;
				}
			}
			//cout << a[pre] << " " << a[j] << " " << tmp << endl;
			if(tmp < m){
				m = tmp;
				index = j;
			}
		}
		cout << a[index] << " ";
		pre = index;
		done[pre] = true;
	}
	cout << endl;

	return 0;
}
0