結果

問題 No.14 最小公倍数ソート
ユーザー IL_mstaIL_msta
提出日時 2015-08-22 15:51:17
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 3,231 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 89,352 KB
実行使用メモリ 4,612 KB
最終ジャッジ日時 2023-09-25 16:11:41
合計ジャッジ時間 2,843 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 10 ms
4,380 KB
testcase_04 AC 21 ms
4,612 KB
testcase_05 AC 10 ms
4,384 KB
testcase_06 AC 12 ms
4,380 KB
testcase_07 AC 15 ms
4,376 KB
testcase_08 AC 18 ms
4,380 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 21 ms
4,392 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
 
#include <iostream>
#include <iomanip>
#include <sstream>
 
#include <algorithm>
#include <cmath>
 
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
 
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
 
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////

LL gcd(LL a,LL b){
	return b ? gcd(b,a%b) : a;
}
const int Amax = 10001;
int Ymax=0;
int A[Amax];

bool pdp[Amax];
vector<int> dp[Amax];
//2からNまでの素数を作る
void makePrime(int N){
	for(int i=2; i<= N; ++i){
		if(pdp[i] == false){
			for(int j = i+i; j<= N; j += i){
				pdp[j] = true;
				dp[j].push_back(i);
			}
		}else{
			for(int j = i+i; j<= N; j += i){
				dp[j].push_back(i);
			}
		}
		dp[i].push_back(i);
	}
	return ;
}

int main(void){
    std::cin.tie(0); 
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed;//
    //cout << setprecision(16);//
	
	int N;
	cin>>N;
	
	vector<int> ans(N);
	int ansCount = 1;
	
	cin>>ans[0];
	int temp;
	for(int i=0;i<N-1;++i){
		cin>>temp;
		Ymax = max(Ymax,temp+1);
		++A[temp];
	}
	makePrime(Ymax);
	int count = 1;
	int min = 1;
	int it = 1;
	bool flag = true;
	while( A[1] ){//1を全部使う
		ans[ansCount] = 1;
		++ansCount;
		--A[1];
	}
	for(count = min; count<Ymax; ++count){
		if( A[count] ){
			min = count;
			break;
		}
	}
	if( ans[ansCount-1] == 1){
		while( A[min] ){
			ans[ansCount] = min;
			--A[min];
			++ansCount;
		}
	}

	while( ansCount < N ){
		for(count = min; count<Ymax; ++count){
			if( A[count] ){
				min = count;
				break;
			}
		}

		int ter = ans[ansCount-1];
		bool ansFlag = false;

		if( pdp[ter] == false ){//素数
			int ansA,ansB;
			for(int i=1;i*ter<Ymax;++i){
				if( A[i*ter] ){
					ansA = i*ter;
					ansFlag = true;
					break;
				}
			}
			
			ansB = min*ter;
			if( ansFlag ){
				if( ansB < ansA ){
					ans[ansCount] = min;
					--A[min];
					++ansCount;
				}else{//ansA
					ans[ansCount] = ansA;
					--A[ansA];
					++ansCount;
				}
			}else{
				ans[ansCount] = min;
				--A[min];
				++ansCount;
			}
			continue;
		}
		
		vector<int>::reverse_iterator yRItr		= dp[ ter ].rbegin();
		vector<int>::reverse_iterator yRBegin	= yRItr;
		vector<int>::reverse_iterator yREnd		= dp[ ter ].rend();
		int temp;
		//for(int i= 1; !ansFlag && yRItr != yREnd && i*(*(yREnd-1)) < Ymax ;++i){//i*terが最小公倍数
		for(int i= 1; !ansFlag && yRItr != yREnd && i <= min ;++i){//i*terが最小公倍数
			while( yRItr != yREnd){
				temp = i *ter/(*yRItr);
				if( temp < Ymax && A[temp] ){
					ans[ansCount] = temp;
					--A[temp];
					++ansCount;
					ansFlag = true;
					break;
				}
				++yRItr;
			}
			if(!ansFlag){
				temp = i *ter;//約数1
				if( temp < Ymax && A[temp] ){
					ans[ansCount] = temp;
					--A[temp];
					++ansCount;
					ansFlag = true;
					break;
				}
			}
			yRItr = yRBegin;
		}

		if( !ansFlag ){
			ans[ansCount] = min;
			--A[min];
			++ansCount;
		}
	}
	////////////
	rep(i,N){
		cout << ans[i];
		if( i!=N-1){
			cout << ' ';
		}
	}cout << '\n';
	return 0;
}
0