1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include <cstdio> #include <queue> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int N = 210, M = 1e5 + 10, inf = 200; namespace PrimeSeive { const int N = 2e4 + 10; bool vis[N]; int cnt, p[N]; void init() { for (int i = 2; i < N; i++) { if (!vis[i]) p[++cnt] = i; for (int j = 1; j <= cnt && i * p[j] < N; j++) { vis[i * p[j]] = true; if (i % p[j] == 0) break; } } } } bool vis[N]; int n, st, ed, w[N], d[N], cur[N]; int idx = -1, hd[N], nxt[M], edg[M], wt[M]; void add (int a, int b, int c) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = c; } bool bfs() { for (int i = st; i <= ed; i++) d[i] = -1; d[st] = 0; cur[st] = hd[st]; queue <int> q; q.push(st); while (!q.empty()) { int t = q.front(); q.pop(); for (int i = hd[t]; ~i; i = nxt[i]) if (d[edg[i]] == -1 && wt[i]) { cur[edg[i]] = hd[edg[i]]; d[edg[i]] = d[t] + 1; if (edg[i] == ed) return true; q.push(edg[i]); } } return false; } int exploit(int x, int limit) { if (x == ed) return limit; int res = 0; for (int i = cur[x]; ~i && res < limit; i = nxt[i]) { cur[x] = i; if (d[edg[i]] == d[x] + 1 && wt[i]) { int t = exploit(edg[i], min(wt[i], limit - res)); if (!t) d[edg[i]] = -1; wt[i] -= t; wt[i ^ 1] += t; res += t; } } return res; } int dinic() { int res = 0, flow; while (bfs()) while (flow = exploit(st, inf)) res += flow; return res; } void dfs (int x, vector <int> &p) { vis[x] = true; p.push_back(x); for (int i = hd[x]; ~i; i = nxt[i]) if (!vis[edg[i]] && wt[i ^ (w[x] & 1)]) return dfs(edg[i], p); } int main () { PrimeSeive::init(); read(n); st = 0, ed = n + 1; for (int i = st; i <= ed; ++i) hd[i] = -1; vector <int> odd, even; for (int i = 1; i <= n; i++) { read(w[i]); if (w[i] & 1) { add(st, i, 2), add(i, st, 0); odd.push_back(i); } else { add(i, ed, 2), add(ed, i, 0); even.push_back(i); } } for (int i : odd) for (int j : even) if (!PrimeSeive::vis[w[i] + w[j]]) add(i, j, 1), add(j, i, 0); if (dinic() ^ n) return puts("Impossible"), 0; vector <vector <int>> ans; for (int i : odd) if (!vis[i]) { vector <int> p; dfs(i, p); ans.push_back(p); } write(ans.size()), puts(""); for (auto i : ans) { write(i.size()), putchar(' '); for (int j : i) write(j), putchar(' '); puts(""); } return 0; }
|