Olympiad in Informatics

[Fortuna OJ] Aug 18th - Group A Problem-Solving Report

A - Unbounded Knapsack

This problem was truly suffocating, and it also taught me that going on QQ during a contest is a very bad habit.

The 60-point solution to this problem is extremely obvious: group items of the same volume into one item, whose value is the largest value among that volume. This squeezes the number of items down to under 100.

But this can't get the full score, and the capacity for the full score is enormous, so the usual DP optimizations won't work. After doing the compression above, we can consider some greedy optimization. Next I'll introduce this greedy strategy in detail:

Lemma 1 Given any \(n\) positive integers, there must exist a subset whose sum is a multiple of \(n\).

Proof Consider setting up prefix sums:\[ S_i = \sum_{j = 1}^i a_j \]Then you'll find that these prefix sums are pairwise distinct. Consider taking \(S_i\) modulo, and then there are two cases:

  • If two \(S\) are equal after the modulo, then their difference is \(0\) after the modulo, which is a multiple of \(n\).
  • If no two \(S\) are equal after the modulo, then there must be a prefix sum that is \(0\) after the modulo, which is also a multiple of \(n\).

Q.E.D.

Strategy For the most cost-effective (\(rate_i = \frac{b_i}{a_i}\)) item \(i\), and the remaining \(x\) items, if \(a_i \leq x\), then using \(a_i\) to replace \(x\) is definitely better. This is because when \(a_i \leq x\), the combinations of the \(x\) items are bound to produce a multiple of \(a_i\), and then we can absolutely replace these combinations with the more cost-effective item \(i\) to get a better answer. After we use this strategy to replace these combinations (which can swap out a lot of space), we just carve out the remaining space and run a brute-force unbounded knapsack on it.

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

using namespace std;

const int MAX_N = 1e6 + 200;

ll n, m;
int volumes[MAX_N], weights[MAX_N], wgt[110];
int upper_tot, max_rate = 0, dp[2][MAX_N];
long double rate[110];

int main()
{
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d%d", &volumes[i], &weights[i]);
        wgt[volumes[i]] = max(wgt[volumes[i]], weights[i]);
        upper_tot = max(upper_tot, volumes[i]);
    }
    for (int i = 1; i <= upper_tot; i++)
    {
        rate[i] = (long double)wgt[i] / i;
        if (rate[i] > rate[max_rate])
            max_rate = i;
    }
    ll minus_part = m / max_rate - 100, answer = 0;
    if (minus_part > 0)
        m -= minus_part * max_rate, answer += wgt[max_rate] * minus_part;
    for (int i = 1; i <= upper_tot; i++)
        for (int j = 0; j <= m; j++)
        {
            dp[i & 1][j] = dp[(i - 1) & 1][j];
            if (j >= i)
                dp[i & 1][j] = max(dp[i & 1][j], dp[i & 1][j - i] + wgt[i]);
        }
    printf("%lld", dp[upper_tot & 1][m] + answer);
    return 0;
}

B - Middle Value

This problem really is a divide-and-conquer template problem (but I'm truly weak at divide and conquer and can't write it at all; I still need to work on it).

The problem guarantees that both sequences are increasing, so we can consider doing a divide and conquer to search. Initially, we piece \(a[l_1 \dots r_1], b[l_2 \dots r_2]\) together and look for the \(k = \lfloor \frac{length}{2} \rfloor + 1\)-th number. We can halve \(k\) and put one half into each of the two intervals, say \(s, t\). If \(a_s > b_t\), we know that the latter half of \(\{b_i\}\) is the useless part, and vice versa. Roughly, just write it like that.

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

using namespace std;

const int MAX_N = 5e5 + 200;

int ai[MAX_N], bi[MAX_N], n, m;

namespace IO
{
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc()                                                                 \
    (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
         ? EOF                                                               \
         : *p1++)
inline int rd()
{
    int x = 0, f = 1;
    char c = gc();
    while (!isdigit(c))
    {
        if (c == '-')
            f = -1;
        c = gc();
    }
    while (isdigit(c))
        x = x * 10 + (c ^ 48), c = gc();
    return x * f;
}
char pbuf[1 << 20], *pp = pbuf;
inline void push(const char &c)
{
    if (pp - pbuf == 1 << 20)
        fwrite(pbuf, 1, 1 << 20, stdout), pp = pbuf;
    *pp++ = c;
}
inline void write(int x)
{
    static int sta[35];
    int top = 0;
    do
    {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top)
        push(sta[--top] + '0');
}
} // namespace IO

inline void solve(int l1, int r1, int l2, int r2, int k)
{
    if (k == 1)
    {
        if (l1 > r1)
            printf("%d\n", bi[l2]);
        else if (l2 > r2)
            printf("%d\n", ai[l1]);
        else
            printf("%d\n", min(ai[l1], bi[l2]));
        return;
    }
    int s = l1 + (k >> 1) - 1, t = l2 + (k >> 1) - 1;
    if (s > r1)
        s = n + 1;
    if (t > r2)
        t = n + 1;
    if (ai[s] > bi[t])
        solve(l1, r1, l2 + (k >> 1), r2, k - (k >> 1));
    else
        solve(l1 + (k >> 1), r1, l2, r2, k - (k >> 1));
}

using namespace IO;

int main()
{
	freopen("median.in", "r", stdin);
	freopen("median.out", "w", stdout);
    n = rd(), m = rd();
    // scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; i++)
        // scanf("%d", &ai[i]);
        ai[i] = rd();
    for (int i = 1; i <= n; i++)
        // scanf("%d", &bi[i]);
        bi[i] = rd();
    ai[n + 1] = bi[n + 1] = 0x3f3f3f3f;
    while (m--)
    {
        int opt, x, y, z, l1, l2, r1, r2;
        // scanf("%d", &opt);
        opt = rd();
        if (opt == 1)
            x = rd(), y = rd(), z = rd(), ((x == 0) ? ai[y] : bi[y]) = z;
        else
            l1 = rd(), r1 = rd(), l2 = rd(), r2 = rd(), solve(l1, r1, l2, r2, (((r1 - l1 + 1) + (r2 - l2 + 1)) >> 1) + 1);
    }
    return 0;
}

C - Sequence

Really fucking nasty.

First, \(f(x)\) is obviously a multiplicative function, so we only need to know how to compute the prime-power case (of the form \(p^k\)). (This should count as a standard trick)

\(f(x)\) contains consecutive \(gcd\) operations; after factoring, you find it is really just taking the minimum of each prime's exponent, and the problem gives \(B\), so in the end the upper bound of every exponent must be the exponent of the corresponding prime factor in \(B\).

So that is to say, let \(p\) be one of the primes, and let \(d\) be the largest exponent that \(B\) has for \(p\), i.e. the largest \(p^d | B\); then:

\[ f(p_c) = \begin{cases} p_d(c - d + 1)^n + \sum_{i = 0}^{d - 1} [(c - i + 1)^n - (c - i)^1], c > d \\ \sum_{i = 0}^c p^i[(c - i + 1)^n - (c - i)^n], c \leq d \end{cases} \]

Then just record some information during the linear sieve.

// C.cpp
#include <bits/stdc++.h>
#define pr pair<int, int>
typedef long long ll;

using namespace std;

const int MAX_N = 2e7 + 200, mod = 998244353;

ll n, m, B;
int fi[MAX_N], prime[MAX_N], tot, pows[105], min_prime[MAX_N];
pr g[MAX_N];

int quick_pow(int bas, int tim)
{
    int ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = 1LL * ans * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

void sieve()
{
    for (int i = 2; i <= m; i++)
    {
        if (!min_prime[i])
            prime[++tot] = min_prime[i] = g[i].second = i, g[i].first = 1;
        for (int j = 1; j <= tot && 1LL * i * prime[j] <= m; j++)
        {
            int tmp = prime[j] * i;
            g[tmp].first = 1, g[tmp].second = min_prime[tmp] = prime[j];
            if (i % prime[j] == 0)
            {
                g[tmp].first += g[i].first, g[tmp].second *= g[i].second;
                break;
            }
        }
    }
}

int main()
{
	freopen("sequence.in", "r", stdin);
	freopen("sequence.out", "w", stdout);
    scanf("%lld%lld%lld", &n, &m, &B);
    sieve();
    for (int i = 0; i <= 100; i++)
        pows[i] = quick_pow(i, n % (mod - 1));
    fi[1] = 1;
    for (int i = 2; i <= m; i++)
        if (g[i].second == i)
        {
            for (int j = 0, tmp = 1; j <= g[i].first; j++)
            {
                fi[i] = (1LL * fi[i] + 1LL * tmp * (1LL * pows[g[i].first - j + 1] - 1LL * pows[g[i].first - j] + mod) % mod) % mod;
                if ((B / tmp) % min_prime[i] == 0)
                    tmp *= min_prime[i];
            }
        }
        else
            fi[i] = 1LL * fi[i / g[i].second] * fi[g[i].second] % mod;
    for (int i = 1; i <= m; i++)
        fi[i] = (1LL * fi[i] + 1LL * fi[i - 1]) % mod;
    printf("%d\n", fi[m]);
    return 0;
}