Olympiad in Informatics

[2018 Quanzhou National Day Training Camp #1] - Problem-Solving Report

A - Gift

Stupid brute force works miracles, toying with people's feelings.

So it turns out your damn \(1e9\) complexity passes too? I'm honestly floored.

// A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1010;

int prefix[MAX_N][MAX_N], suffix[MAX_N][MAX_N];
int n, ai[MAX_N], bi[MAX_N], ci[MAX_N], m;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d%d%d", &ai[i], &bi[i], &ci[i]);
    for (int i = 1; i <= n; i++)
        for (int j = 0; j < MAX_N; j++)
        {
            prefix[i][j] = prefix[i - 1][j];
            if (j >= ai[i])
                prefix[i][j] = max(prefix[i][j], prefix[i - 1][j - ai[i]] + bi[i]);
        }
    for (int i = n; i >= 1; i--)
        for (int j = 0; j < MAX_N; j++)
        {
            suffix[i][j] = suffix[i + 1][j];
            if (j >= ai[i])
                suffix[i][j] = max(suffix[i][j], suffix[i + 1][j - ai[i]] + bi[i]);
        }
    scanf("%d", &m);
    while (m--)
    {
        int del, money;
        scanf("%d%d", &del, &money), del++;
        int ans = 0;
        for (int i = 0; i <= money; i++)
            ans = max(ans, prefix[del - 1][i] + suffix[del + 1][money - i]);
        printf("%d\n", ans);
    }
    return 0;
}

B - XOR

This problem is way too damn stupid.

Consider that a bit can contribute to the answer if and only if all the bits before it are equal. On a trie, any node guarantees that the prefixes are equal, so the contribution of each node is \(2^{len} \times son_0 \times son_1\).

// B.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 1e5 + 200;
const ll mod = 199907210507;

struct node
{
    int ch[2];
    ll val;
} nodes[MAX_N * 70];

ll ptot = 1, n, arr[MAX_N], ans, pow2[70];

void insert(ll x)
{
    int p = 1;
    for (int i = 0; i <= 62; i++)
    {
        int bit = (x >> i) & 1;
        if (nodes[p].ch[bit] == 0)
            nodes[p].ch[bit] = ++ptot;
        nodes[p].val++, p = nodes[p].ch[bit];
    }
}

void dfs(int u, int cnt)
{
    if (u == 0)
        return;
    (ans += 1LL * nodes[nodes[u].ch[0]].val * nodes[nodes[u].ch[1]].val % mod * pow2[cnt]) %= mod;
    dfs(nodes[u].ch[0], cnt + 1), dfs(nodes[u].ch[1], cnt + 1);
}

int main()
{
    scanf("%lld", &n);
    for (int i = pow2[0] = 1; i < 70; i++)
        pow2[i] = 1LL * pow2[i - 1] * 2 % mod;
    for (int i = 1; i <= n; i++)
        scanf("%lld", &arr[i]), insert(arr[i]);
    dfs(1, 0), printf("%lld", 1LL * ans * 2 % mod);
    return 0;
}

C - Subsets

A really intriguing problem.

The answer is \(2^{n \times k}\). I thought for a long time about a reason to explain this answer, and thanks as well to XG_Zepto and CrazyDave for their thinking. We can see that choosing a monotone (non-partially-ordered) set of points, and filling numbers into such sets, is enough to determine the whole configuration.

By “non-partially-ordered points” we mean points related the way the two B points in the diagram are:

A
^
|
A <- A
^    ^
|    |
A <- A <- B
^    ^    ^
|    |    |
A <- B <- A <- A

Define such a point as the earliest point where some element exists; putting an element into such a point makes the whole upstream region covered by that element. Consider that there are \(2^k\) such point sets (that is, the number of subsets of the set made up of “non-partially-ordered points”), and then each element is put into one of the sets, so the number of elements is \(2^{nk}\).

Roughly like this; I still need to think it over some more.

A proof that the number of subsets of the set made up of “non-partially-ordered points” is \(2^k\), from XG_Zepto:

Let \(f(k)\) be the number of non-partially-ordered point sets from row \(k\) upward. Considering that row \(k\) includes all the earlier cases, \(f(k) = 2 + \sum_{i = 1}^{k - 1} f(i)\), and this \(2\) stands for the exclusive cases.

That's about it.

// C.cpp
#include <bits/stdc++.h>
#define ll unsigned long long

using namespace std;

const ll mod = 7528443412579576937;

ll quick_mul(ll x, ll y)
{
    ll res = 0;
    if (x < y)
        swap(x, y);
    while (y)
    {
        if (y & 1LL)
            res = (res + x < mod) ? (res + x) : (res + x - mod);
        x = (x + x < mod) ? (x + x) : (x + x - mod);
        y >>= 1LL;
    }
    return res;
}

ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1LL)
            ans = quick_mul(ans, bas);
        bas = quick_mul(bas, bas);
        tim >>= 1LL;
    }
    return ans;
}

int main()
{
    ll n, k;
    scanf("%lld%lld", &n, &k), printf("%lld", quick_pow(2, 1LL * n * k));
    return 0;
}