Submission #1096410
Source Code Expand
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <vector> #include <string> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; #define MOD @ #define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD) typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec; int gcd(int a, int b) { while (b) { int t = a % b; a = b; b = t; } return a; } int N, A[2020]; vector<int> dv[2020]; vector<int> rdiv[50505]; int RC; bool isok[2020], used[2020]; bool rdivvis[50505]; bool vis[2020]; int chk() { for (int i = 0; i < N; ++i) vis[i] = false; for (int i = 0; i < RC; ++i) rdivvis[i] = false; queue<int> Qa; int top = 0; int ret = -1; for (;;) { while (top < N) { if (!used[top] && isok[top] && !vis[top]) { Qa.push(top); vis[top] = true; ret = top; break; } ++top; } if (top == N) break; while (!Qa.empty()) { int v = Qa.front(); Qa.pop(); for (int p : dv[v]) if (!rdivvis[p]) { rdivvis[p] = true; for (int i : rdiv[p]) if (!used[i] && !vis[i]) { vis[i] = true; Qa.push(i); } } } } return ret; } int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d", A + i); } sort(A, A + N); for (int i = 0; i < N; ++i) { int v = A[i]; for (int j = 2; j * j <= v; ++j) { if (v % j == 0) { dv[i].push_back(j); while (v % j == 0) v /= j; } } if (v > 1) dv[i].push_back(v); } vector<int> alldv; for (int i = 0; i < N; ++i) { for (int p : dv[i]) alldv.push_back(p); } sort(alldv.begin(), alldv.end()); alldv.erase(unique(alldv.begin(), alldv.end()), alldv.end()); for (int i = 0; i < N; ++i) { for (int &p : dv[i]) { p = lower_bound(alldv.begin(), alldv.end(), p) - alldv.begin(); rdiv[p].push_back(i); } } RC = alldv.size(); for (int i = 0; i < N; ++i) { isok[i] = true; used[i] = false; } vector<int> ans; for (int i = 0; i < N; ++i) { int id = chk(); int v = A[id]; ans.push_back(v); // printf("%d\n", v); used[id] = true; for (int j = 0; j < N; ++j) { if (A[j] > v) { isok[j] = false; } if (gcd(A[j], v) != 1) { isok[j] = true; } } } for (int i = 0; i < N; ++i) printf("%d%c", ans[i], i == N - 1 ? '\n' : ' '); return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Rearranging |
User | semiexp |
Language | C++14 (GCC 5.4.1) |
Score | 1600 |
Code Size | 2425 Byte |
Status | AC |
Exec Time | 593 ms |
Memory | 1792 KB |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:72:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &N); ^ ./Main.cpp:74:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", A + i); ^
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1600 / 1600 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample1.txt, sample2.txt |
All | sample1.txt, sample2.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
in1.txt | AC | 590 ms | 1792 KB |
in10.txt | AC | 591 ms | 1664 KB |
in11.txt | AC | 497 ms | 1664 KB |
in12.txt | AC | 501 ms | 1664 KB |
in13.txt | AC | 497 ms | 1664 KB |
in14.txt | AC | 499 ms | 1664 KB |
in15.txt | AC | 499 ms | 1664 KB |
in16.txt | AC | 512 ms | 1664 KB |
in17.txt | AC | 515 ms | 1664 KB |
in18.txt | AC | 510 ms | 1664 KB |
in19.txt | AC | 513 ms | 1792 KB |
in2.txt | AC | 591 ms | 1792 KB |
in20.txt | AC | 511 ms | 1664 KB |
in21.txt | AC | 358 ms | 1664 KB |
in22.txt | AC | 358 ms | 1664 KB |
in23.txt | AC | 361 ms | 1664 KB |
in24.txt | AC | 359 ms | 1664 KB |
in25.txt | AC | 358 ms | 1664 KB |
in26.txt | AC | 499 ms | 1792 KB |
in27.txt | AC | 498 ms | 1664 KB |
in28.txt | AC | 495 ms | 1664 KB |
in29.txt | AC | 500 ms | 1792 KB |
in3.txt | AC | 592 ms | 1664 KB |
in30.txt | AC | 498 ms | 1664 KB |
in31.txt | AC | 4 ms | 1536 KB |
in32.txt | AC | 4 ms | 1664 KB |
in33.txt | AC | 4 ms | 1536 KB |
in34.txt | AC | 415 ms | 1664 KB |
in35.txt | AC | 413 ms | 1664 KB |
in36.txt | AC | 520 ms | 1664 KB |
in37.txt | AC | 519 ms | 1664 KB |
in38.txt | AC | 520 ms | 1664 KB |
in39.txt | AC | 522 ms | 1664 KB |
in4.txt | AC | 593 ms | 1664 KB |
in40.txt | AC | 519 ms | 1664 KB |
in41.txt | AC | 519 ms | 1664 KB |
in42.txt | AC | 521 ms | 1664 KB |
in43.txt | AC | 519 ms | 1664 KB |
in44.txt | AC | 520 ms | 1664 KB |
in45.txt | AC | 527 ms | 1792 KB |
in5.txt | AC | 591 ms | 1664 KB |
in6.txt | AC | 589 ms | 1664 KB |
in7.txt | AC | 589 ms | 1664 KB |
in8.txt | AC | 591 ms | 1664 KB |
in9.txt | AC | 593 ms | 1664 KB |
sample1.txt | AC | 4 ms | 1536 KB |
sample2.txt | AC | 4 ms | 1536 KB |