QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#83678#4101. 数字配对little_sun100 ✓50ms4132kbC++144.0kb2023-03-03 02:27:322023-03-03 02:27:33

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-03 02:27:33]
  • Judged
  • Verdict: 100
  • Time: 50ms
  • Memory: 4132kb
  • [2023-03-03 02:27:32]
  • Submitted

answer

#include <bits/stdc++.h>

#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
#define meow(cat...) fprintf(stderr, cat)

const ll MaxN = 5e5 + 10;
const ll MaxM = 5e5 + 10;
const ll inf = 0x3f3f3f3f3f3f3f3f;

struct edge {
    ll next, to, flow, cost;
} e[MaxM << 1];

std::vector<ll> L, R;
ll n, m, s, t, S, T, cnt = 1, mincost, maxflow, a[MaxN], b[MaxN];
ll head[MaxN], c[MaxN], cur[MaxN], vis[MaxN], dis[MaxN], num[MaxN];

ll calc(ll x) {
    ll s = x, res = 0;

    for (ll i = 2; i * i <= x; i++)
        while (s % i == 0)
            s /= i, res++;

    if (s > 1)
        res++;

    return res;
}

void add(ll u, ll v, ll f, ll c) {
    ++cnt;
    e[cnt].to = v;
    e[cnt].flow = f;
    e[cnt].cost = c;
    e[cnt].next = head[u];
    head[u] = cnt;
}

void add_edge(ll u, ll v, ll f, ll c) {
    add(u, v, f, c), add(v, u, 0, -c);
}

inline ll read() {
    ll x = 0, f = 1;
    char ch = getchar();

    while (ch > '9' || ch < '0') {
        if (ch == '-')
            f = 0;

        ch = getchar();
    }

    while (ch <= '9' && ch >= '0')
        x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();

    return f ? x : (-x);
}

void init() {
    cnt = 1, mincost = maxflow = 0;
    memset(head, 0, 8 * (n + 10));
}

ll bfs(ll s) {
    std::queue<ll> q;
    memset(dis, 0x3f, 8 * (n + 10));
    memcpy(cur, head, 8 * (n + 10));
    dis[s] = 0, q.push(s), vis[s] = 1;

    while (!q.empty()) {
        ll u = q.front();
        q.pop(), vis[u] = 0;

        for (ll i = head[u]; i; i = e[i].next) {
            ll v = e[i].to, c = e[i].flow;

            if (dis[v] > dis[u] + e[i].cost && c) {
                dis[v] = dis[u] + e[i].cost;

                if (!vis[v])
                    vis[v] = 1, q.push(v);
            }
        }
    }

    return dis[t] != inf;
}

ll dinic(ll u, ll flow) {
    if (u == t) {
        maxflow += flow;
        return flow;
    }

    ll rest = flow;
    vis[u] = 1;

    for (ll i = cur[u]; i && rest; i = e[i].next) {
        ll v = e[i].to, c = e[i].flow, k;

        if (!vis[v] && (dis[v] == dis[u] + e[i].cost) && c) {
            cur[u] = i, k = dinic(v, std::min(c, rest));
            rest -= k, e[i].flow -= k, e[i ^ 1].flow += k;
            mincost += k * e[i].cost;
        }
    }

    vis[u] = 0;
    return flow - rest;
}

void solve() {
    while (bfs(s)) {
        memset(vis, 0, 8 * (n + 10));
        dinic(s, inf);
        memset(vis, 0, 8 * (n + 10));
    }
}

ll check(ll g) {
    init(), add_edge(s, S, g, 0), add_edge(T, t, g, 0);

    for (auto x : L)
        add_edge(S, x, b[x], 0);

    for (auto x : R)
        add_edge(x, T, b[x], 0);

    for (ll i = 0; i < L.size(); i++)
        for (ll j = 0; j < R.size(); j++)
            if ((a[L[i]] % a[R[j]] == 0 && num[L[i]] == num[R[j]] + 1) ||
                    (a[R[j]] % a[L[i]] == 0 && num[L[i]] == num[R[j]] - 1))
                add_edge(L[i], R[j], inf, -(c[L[i]] * c[R[j]]));

    solve(); // meow("%lld %lld\n", g, mincost);
    return (mincost <= 0 && maxflow == g);
}

ll getr() {
    init(), add_edge(s, S, inf, 0), add_edge(T, t, inf, 0);

    for (auto x : L)
        add_edge(S, x, b[x], 0);

    for (auto x : R)
        add_edge(x, T, b[x], 0);

    for (ll i = 0; i < L.size(); i++)
        for (ll j = 0; j < R.size(); j++)
            add_edge(L[i], R[j], inf, 0);

    solve();
    return maxflow;
}

signed main() {
    ll l = 1, r = 0;
    n = read(), s = n + 1, t = n + 2, S = n + 3, T = n + 4;

    for (ll i = 1; i <= n; i++)
        a[i] = read(), num[i] = calc(a[i]);

    for (ll i = 1; i <= n; i++)
        b[i] = read();

    for (ll i = 1; i <= n; i++)
        c[i] = read();

    for (ll i = 1; i <= n; i++)
        (num[i] % 2 == 1) ? L.push_back(i) : R.push_back(i);

    r = getr();

    while (l < r) {
        ll mid = (l + r + 1) >> 1;

        if (check(mid))
            l = mid;
        else
            r = mid - 1;
    }

    printf("%lld\n", l);
    return 0;
}

詳細信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 3528kb

input:

3
12 24 48
1 1 1
1 1 -1

output:

1

result:

ok single line: '1'

Test #2:

score: 10
Accepted
time: 2ms
memory: 3520kb

input:

6
2 4 50 6 12 25
1 1 1 1 1 1
-1 1 -1 1 -1 -1

output:

2

result:

ok single line: '2'

Test #3:

score: 10
Accepted
time: 2ms
memory: 3588kb

input:

10
1 2 3 4 5 6 7 8 9 10
1 1 1 1 1 1 1 1 1 1
1 -1 3 3 -10 -1 -3 -3 -1 -1

output:

4

result:

ok single line: '4'

Test #4:

score: 10
Accepted
time: 5ms
memory: 3868kb

input:

120
141750 107163 13608 4286520 61236 47628 8037225 238140 25719120 244944 14175 1224720 5670 1701 425250 2857680 39690 11340 28350 637875 76545 5740875 8930250 10716300 642978000 1134 6804 40824 113400 122472 119070 212625 1587600 17010 612360 1148175 8505 952560 32148900 321489 680400 51030 321489...

output:

2461516

result:

ok single line: '2461516'

Test #5:

score: 10
Accepted
time: 11ms
memory: 4132kb

input:

180
3969000 5740875 306180 45360 22680 142884 26790750 5953500 571536 357210 23814 1428840 10206000 3827250 2835 3402 204120 63504 68040 15309000 283500 918540 11907000 4286520 1488375 79380 25719120 7938 226800 1984500 1148175 8573040 32148900 425250 2143260 4536 4762800 153090 9072 17010 85050 198...

output:

3950487

result:

ok single line: '3950487'

Test #6:

score: 10
Accepted
time: 44ms
memory: 3924kb

input:

141
1984500 1134000 99225 321489 4465125 80372250 15309000 1285956 141750 1190700 15309 992250 496125 238140 8573040 127575 27216 10206 7938000 3969 28350 7654500 5103 18370800 1913625 1071630 306180 571536 214326 1530900 39690 35721000 4592700 25719120 19845 1837080 10716300 2143260 170100 244944 4...

output:

3241104

result:

ok single line: '3241104'

Test #7:

score: 10
Accepted
time: 50ms
memory: 4096kb

input:

150
476280 3969 53581500 17010 1134 3674160 850500 9072 35721 367416 5143824 8573040 321489000 595350 857304 3402000 285768 8505 637875 47628 5670 214326000 39690 64297800 178605 45927000 2976750 535815 2857680 992250 4592700 1020600 14288400 127575 357210 428652 63504 5740875 6429780 128595600 7144...

output:

2845252

result:

ok single line: '2845252'

Test #8:

score: 10
Accepted
time: 42ms
memory: 3964kb

input:

156
18370800 7654500 13608 19845 4286520 136080 23814000 1134 7144200 26790750 15876 10206000 6429780 91854000 71442000 4465125 71442 340200 85050 567 27216 214326000 2679075 8037225 4762800 47628 11907000 1148175 2143260 535815 1714608 11907 1285956 1275750 20412 32148900 91854 8573040 857304 30618...

output:

2971020

result:

ok single line: '2971020'

Test #9:

score: 10
Accepted
time: 13ms
memory: 3792kb

input:

105
5103000 8505 63504 367416 119070 2268 496125 45927 1701000 1837080 459270 2296350 1913625 1134 2041200 85050 3572100 1071630 3827250 178605 7144200 396900 30618 3402000 283500 25719120 42865200 81648 7938 317520 11481750 612360 238140 535815 23814000 857304 1275750 1488375 893025 4592700 510300 ...

output:

1686391

result:

ok single line: '1686391'

Test #10:

score: 10
Accepted
time: 24ms
memory: 3856kb

input:

126
79380 1071630 153090 22963500 1587600 45927000 1607445 3061800 3402 15309 1148175 204120 1020600 5670 17860500 567 714420 91854000 6804 31752 23814 3674160 9185400 45927 81648 1984500 7938 476280 11481750 1701 4286520 25719120 321489000 71442000 5143824 9072 2296350 918540 1913625 283500 214326 ...

output:

2078711

result:

ok single line: '2078711'