結果
| 問題 |
No.14 最小公倍数ソート
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-08-22 16:28:11 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,028 bytes |
| コンパイル時間 | 773 ms |
| コンパイル使用メモリ | 92,240 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-18 12:47:15 |
| 合計ジャッジ時間 | 1,646 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 WA * 5 |
ソースコード
#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;
/////////
const int Amax = 10001;
int A[Amax];
bool pdp[Amax];
vector<vector<int>> dp(Amax);
vector<int> ans(Amax);
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;
int Ymax=0;
int ansCount = 1;
cin>>ans[0];
int cintemp;
for(int i=0;i<N-1;++i){
cin>>cintemp;
Ymax = max(Ymax,cintemp+1);
++A[cintemp];
}
makePrime(Ymax-1);
int count = 1;
int min = 1;
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= min/ter + 1; !ansFlag && yRItr != yREnd && i <= min ;++i){
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;
}
IL_msta