結果

問題 No.3345 Reducible Sequence
コンテスト
ユーザー The Forsaking
提出日時 2025-11-21 16:47:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,966 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 201,836 KB
実行使用メモリ 18,260 KB
最終ジャッジ日時 2025-11-21 16:48:02
合計ジャッジ時間 3,837 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:73:42: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |     for (int i = 1; i < n + 1; i++) scanf("%d", a + i);
      |                                     ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
const int N = 2000010, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int n, m, w[N];

int e[N], ne[N], h[N], idx, a[N], S, T, d[N], cur[N];
void add(int a, int b, int c) {
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
    e[idx] = a, ne[idx] = h[b], w[idx] = 0, h[b] = idx++;
}

inline bool bfs() {
	queue<int> q;
    memset(d, -1, sizeof(int) * 12000);
    memcpy(cur, h, sizeof(int) * 12000);

    d[S] = 0;
    q.push(S);
    while (q.size()) {
        auto u = q.front();
        q.pop();
        for (int i = h[u]; ~i; i = ne[i]) {
            int j = e[i];
            if (w[i] && d[j] == -1) {
                d[j] = d[u] + 1;
                if (j == T) return true;
                q.push(j);
            }
        }
    }
    return false;
}

inline ll dfs(int r, int limit) {
    if (r == T) return limit;
    ll res = 0;
    for (int i = cur[r]; ~i && res < limit; i = ne[i]) {
        cur[r] = i;
        int j = e[i];
        if (d[j] == d[r] + 1 && w[i]) {
            ll t = dfs(j, min(limit - res, (ll)w[i]));
            if (!t) d[j] = -1;
            w[i] -= t, w[i ^ 1] += t, res += t;
        }
    }
    return res;
}

inline ll dinic() {
    ll res = 0, t;
    while (bfs()) while (t = dfs(S, INF)) res += t;
    return res;
}

bool check(int mid) {
    S = 0, T = mid + 5000 + 1;
    memset(h, -1, sizeof(int) * 12000);
    idx = 0;
    for (int i = 1; i <= mid; i++) add(S, 5000 + i, 1);
    for (int i = 1; i < n + 1; i++) add(a[i], T, 1);
    for (int i = 1; i <= mid; i++)
        for (int j = i; j <= 5000; j += i)
            add(5000 + i, j, 1);
    return dinic() == mid;
}

int main() {
    cin >> n;
    for (int i = 1; i < n + 1; i++) scanf("%d", a + i);
    int l = 1, r = n;
    while (l < r) {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    printf("%d\n", l);
    return 0;
}
0