QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Submission ID | Problem | Hacker | Owner | Result | Submit time | Judge time |
---|---|---|---|---|---|---|---|
#1140 | #716773 | #9545. Magical Set | liuenci | wym1111 | Failed. | 2024-11-06 16:59:12 | 2024-11-06 16:59:12 |
Details
Extra Test:
Accepted
time: 2ms
memory: 6944kb
input:
1 998244353
output:
1
result:
ok single line: '1'
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#716773 | #9545. Magical Set | wym1111 | AC ✓ | 19ms | 8584kb | C++17 | 3.3kb | 2024-11-06 16:00:53 | 2024-11-06 16:00:54 |
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
class Dinic_ssp {
private:
struct Edge {
int from, to, cap, flow, val;
Edge (int u, int v, int c, int f, int vl) :
from(u), to(v), cap(c), flow(f), val(vl) {}
};
vector<Edge> edges;
vector<int> g[N];
int dis[N], cur[N], mincost;
bool vis[N];
public:
void init (int n) {
for (int i = 0; i <= n; i ++) {
g[i].clear();
}
edges.clear();
mincost = 0;
}
void addEdge (int u, int v, int c, int w) {
edges.push_back({u, v, c, 0, w});
edges.push_back({v, u, 0, 0, -w});
int m = edges.size();
g[u].push_back(m - 2);
g[v].push_back(m - 1);
}
bool spfa (int s, int t) {
memset(dis, 0x3f, sizeof dis);
memset(cur, 0, sizeof cur);
queue<int> q;
dis[s] = 0;
q.push(s);
vis[s] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
// cout << x << ' ' << dis[x] << endl;
for (auto &i: g[x]) {
Edge &e = edges[i];
if (e.cap > e.flow && dis[e.to] > dis[x] + e.val) {
dis[e.to] = dis[x] + e.val;
if (!vis[e.to]) {
q.push(e.to);
vis[e.to] = 1;
}
}
}
}
return dis[t] != INF;
}
int dfs (int x, int t, int flow) {
if (x == t || flow == 0) return flow;
int res = 0;
vis[x] = 1;
for (int i = cur[x]; i < (int)g[x].size(); i ++) {
cur[x] = i;
Edge &e = edges[g[x][i]];
if (dis[e.to] == dis[x] + e.val && !vis[e.to]) {
int d = dfs(e.to, t, min(flow - res, e.cap - e.flow));
res += d;
mincost += d * e.val;
edges[g[x][i]].flow += d;
edges[g[x][i] ^ 1].flow -= d;
if (res == flow) {
vis[x] = 0;
return flow;
}
}
}
vis[x] = 0;
return res;
}
pair<int, int> mcmf (int s, int t) {
int flow = 0;
while (spfa(s, t)) {
while (int d = dfs(s, t, INF)) {
flow += d;
}
}
return {flow, mincost};
}
};
Dinic_ssp dinic;
int n;
int a[N];
set<int> st, st2;
bool not_prime[N];
vector<int> prime;
void pre () {
for (int i = 2; i < N; i ++) {
if (not_prime[i]) continue;
prime.push_back(i);
for (int j = i + i; j < N; j += i) {
not_prime[j] = 1;
}
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
st.insert(a[i]);
}
while (!st.empty()) {
auto x = *st.rbegin();
st.erase(x);
st2.insert(x);
for (auto p: prime) {
if (p * p > x) break;
if (x % p) continue;
st.insert(x / p);
st.insert(p);
}
}
st2.insert(1);
map<int, int> id;
int idx = 0;
for (auto x: st2) {
id[x] = ++ idx;
// cout << idx << ' ' << x << endl;
}
int s = 0, t = idx + 1;
for (int i = 1; i <= idx; i ++) {
if (i <= n) dinic.addEdge(s, id[a[i]], 1, 0);
dinic.addEdge(i, t, 1, 0);
}
for (auto x: st2) {
if (x == 1) continue;
int cur = id[x];
for (auto p: prime) {
if (p * p > x) break;
if (x % p) continue;
dinic.addEdge(cur, id[x / p], INF, -1);
dinic.addEdge(cur, id[p], INF, -1);
}
dinic.addEdge(cur, 1, INF, -1);
}
// cout << "debug\n";
auto res = dinic.mcmf(s, t);
cout << -res.second << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
pre();
int _ = 1;
// cin >> _;
while (_--){
solve();
}
return 0;
}