Olympiad in Informatics

[Fortuna OJ] May 6 Provincial Team Selection Group A - Problem-Solving Report

A - Cow Gymnasts

A pattern-spotting problem. As long as you can spot the property that the range of a valid sequence is at most \(1\), you can just compute it. Directly:

\[ ans = 2 - n + \sum_{i = 1}^{n - 1} 2^{\gcd(i, n)} \]

Then this can obviously be optimized with number-theoretic functions, the way some Polya problems do it, and that's it.

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

using namespace std;

const int MAX_N = 1e5 + 200, mod = 1e9 + 7;

typedef long long ll;

ll n, acc, ans;
vector<pair<ll, int>> nset;

void fileIO(string src)
{
    freopen((src + ".in").c_str(), "r", stdin);
    freopen((src + ".out").c_str(), "w", stdout);
}

ll fpow(ll bas, ll tim)
{
    ll ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

void dfs(int pos, ll gcd, ll phiup, ll phidown)
{
    if (pos == int(nset.size()))
    {
        if (gcd == 1)
            return;
        ans += fpow(2, (n / gcd) % (mod - 1)) * ((1LL * gcd / phidown % mod * phiup % mod) % mod) % mod;
        ans %= mod;
        return;
    }
    dfs(pos + 1, gcd, phiup, phidown);
    phiup = 1LL * phiup * (nset[pos].first - 1) % mod;
    phidown = 1LL * phidown * nset[pos].first;
    for (int i = 1; i <= nset[pos].second; i++)
    {
        gcd *= nset[pos].first;
        dfs(pos + 1, gcd, phiup, phidown);
    }
}

int main()
{
    // fileIO("gymnasts");
    scanf("%lld", &n), acc = n;
    for (ll i = 2; 1LL * i * i <= acc; i++)
        if (acc % i == 0)
        {
            int cnt = 0;
            while (acc % i == 0)
                cnt++, acc /= i;
            nset.push_back(make_pair(i, cnt));
        }
    if (acc > 1)
        nset.push_back(make_pair(acc, 1));
    dfs(0, 1, 1, 1);
    printf("%lld\n", (2LL + ans - (n % mod) + mod) % mod);
    return 0;
}

B - Cow Date

This problem is fairly interesting. Let's try simplifying the expression:

\[ f_{l, r} = \prod_{i = l}^r (1 - p_i) \sum_{i = l}^r \frac{p_i}{1 - p_i} \]

Suppose we transition from \([l, r - 1]\); then:

\[ \begin{aligned} f_{l, r} &= (1 - p_r) \prod_{i = l}^{r - 1} (1 - p_i) (\frac{p_r}{1 - p_r} + \sum_{i = l}^{r - 1} \frac{p_i}{1 - p_i}) \\ &= (1 - p_r) \prod_{i = l}^{r - 1} (1 - p_i) \sum_{i = l}^{r - 1} \frac{p_i}{1 - p_i} + p_r \prod_{i = l}^{r - 1} (1 - p_i) \\ &= (1 - p_r) f_{l, r - 1} + p_r \prod_{i = l}^{r - 1} (1 - p_i) \end{aligned} \]

Clearly we can consider Two-Pointers, and then just check whether that extra lump makes the answer better.

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

using namespace std;

const int MAX_N = 1000000 + 200;

typedef long double ld;

int n, pi[MAX_N], ans;

void fileIO(string src)
{
    freopen((src + ".in").c_str(), "r", stdin);
    freopen((src + ".out").c_str(), "w", stdout);
}

int main()
{
    // fileIO("cowdate");
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &pi[i]), ans = max(ans, pi[i]);
    if (ans >= 5e5)
        printf("%d\n", ans), exit(0);
    int rptr = 1;
    ld acc = pi[1], prod = 1000000 - pi[1];
    while (rptr < n && prod > acc)
    {
        rptr++, acc = acc * (1000000 - pi[rptr]) / 1000000 + pi[rptr] * prod / 1000000;
        prod = prod * (1000000 - pi[rptr]) / 1000000;
    }
    ans = max(ans, int(acc));
    for (int lptr = 2; lptr <= n; lptr++)
    {
        prod = prod * 1000000 / (1000000 - pi[lptr - 1]);
        acc = (acc * 1000000 - pi[lptr - 1] * prod) / (1000000 - pi[lptr - 1]);
        while (rptr < n && prod > acc)
        {
            rptr++, acc = acc * (1000000 - pi[rptr]) / 1000000 + pi[rptr] * prod / 1000000;
            prod = prod * (1000000 - pi[rptr]) / 1000000;
        }
        ans = max(ans, int(acc));
    }
    printf("%d\n", ans);
    return 0;
}

C - Moorio Kart

Damn it, I blew the \(\Theta(n^3)\) on this problem in the exam (I wrote it way too complicated, I'm really stupid).

First, as everyone knows, this problem asks you to string a bunch of trees together and then compute all the cycle lengths greater than \(y\). Normally a \(\Theta(n^3)\) knapsack can do this, and then you just tack on the number of circular permutations.

The complexity is still too high. However, if you add a small pruning when doing the knapsack, the complexity drops to the order of \(\Theta(n^2 \sqrt n)\). Consider the merging: the number of paths within each block is \(\Theta(k^2)\), and the knapsack size is \(Y - Xk\). Multiplied together, these two are at worst on the order of \(\Theta(n^2 \sqrt n)\).

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

using namespace std;

const int MAX_N = 1.5e3 + 200, mod = 1e9 + 7;

typedef long long ll;

int n, m, X, Y, head[MAX_N], current, dist[MAX_N][MAX_N], idx[MAX_N], mem[MAX_N];
int coltot, ans, prod[MAX_N][2600], tot[MAX_N][2600], F[2600][2], tmp[2600][2];

void fileIO(string src)
{
    freopen((src + ".in").c_str(), "r", stdin);
    freopen((src + ".out").c_str(), "w", stdout);
}

struct edge
{
    int to, nxt, weight;
} edges[MAX_N << 1];

void addpath(int src, int dst, int weight)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].weight = weight, head[src] = current++;
}

int find(int x) { return x == mem[x] ? x : mem[x] = find(mem[x]); }

void dfs(int u, int fa, int org, int dep)
{
    if (fa)
    {
        prod[idx[u]][min(dep, Y)] = (0LL + prod[idx[u]][min(dep, Y)] + dep) % mod;
        tot[idx[u]][min(dep, Y)]++;
    }
    dist[org][u] = dep;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            dfs(edges[i].to, u, org, dep + edges[i].weight);
}

int main()
{
    // fileIO("mooriokart");
    memset(head, -1, sizeof(head)), memset(dist, -1, sizeof(dist));
    scanf("%d%d%d%d", &n, &m, &X, &Y);
    for (int i = 1; i <= n; i++)
        mem[i] = i;
    for (int i = 1, u, v, w; i <= m; i++)
    {
        scanf("%d%d%d", &u, &v, &w), addpath(u, v, w), addpath(v, u, w);
        if (find(u) != find(v))
            mem[find(u)] = find(v);
    }
    for (int i = 1; i <= n; i++)
    {
        if (idx[find(i)] == 0)
            idx[find(i)] = ++coltot;
        idx[i] = idx[find(i)];
    }
    for (int i = 1; i <= n; i++)
        dfs(i, 0, i, 0);
    int base = min(Y, coltot * X);
    F[base][0] = 1, F[base][1] = coltot * X;
    for (int i = 1; i <= coltot; i++)
    {
        for (int j = base; j <= Y; j++)
            tmp[j][0] = tmp[j][1] = 0;
        for (int j = 0; j <= Y; j++)
            if (tot[i][j])
                for (int k = base; k <= Y; k++)
                    if (F[k][0])
                    {
                        tmp[min(j + k, Y)][0] = (0LL + tmp[min(j + k, Y)][0] + 1LL * tot[i][j] * F[k][0] % mod) % mod;
                        tmp[min(j + k, Y)][1] = (0LL + tmp[min(j + k, Y)][1] + 1LL * prod[i][j] * F[k][0] % mod + 1LL * F[k][1] * tot[i][j] % mod) % mod;
                    }
        memcpy(F, tmp, sizeof(tmp));
    }
    ans = F[Y][1];
    for (int i = 1; i < coltot; i++)
        ans = 1LL * ans * i % mod;
    printf("%lld\n", 1LL * ans * ((mod + 1) >> 1) % mod);
    return 0;
}