Olympiad in Informatics

[FZOI] Round #33 (Div. 0) - Problem-Solving Report

A - Function

In the exam I only managed to derive \(f(x) = \mu(x) * \sigma_0^2(x) = \prod_{i = 1}^m (2c_i + 1)\), and obviously min_25 could just insta-kill it directly (but I forgot the template code, boo-hoo-hoo-hoo-hoo)

Actually, now that we have reasoned this far, we see it is nothing more than multiplying \(c_i\) by \(2\). So in fact \(f(x) = \sigma_0(x^2)\). Actually, once you have derived the Dirichlet convolution above, this is already a lot clearer.

Now consider how to sieve out \(\sum_{x = 1}^n \sigma_0(x^2)\) quickly. Just sieve it the way SPOJ DIVCNT2 does.

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

using namespace std;

const int MAX_N = 1e6 + 500;

typedef long long ll;

int T, n, primes[MAX_N], tot;
ll sigma[MAX_N], mu[MAX_N], pmu[MAX_N];
bool vis[MAX_N];
unordered_map<ll, ll> ump;

ll getSigma(ll upper)
{
    if (upper < MAX_N)
        return sigma[upper];
    ll ret = 0;
    for (ll i = 1, gx; i <= upper; i = gx + 1)
    {
        gx = upper / (upper / i);
        ret += 1LL * (upper / i) * (gx - i + 1);
    }
    return ret;
}

ll getMu2(ll upper)
{
    if (upper < MAX_N)
        return pmu[upper];
    ll ret = 0;
    for (int i = 1; 1LL * i * i <= upper; i++)
        ret += 1LL * mu[i] * (upper / (1LL * i * i));
    return ret;
}

int main()
{
    sigma[1] = mu[1] = 1;
    for (int i = 2; i < MAX_N; i++)
    {
        if (!vis[i])
            primes[++tot] = i, mu[i] = -1, sigma[i] = 2;
        for (int j = 1; j <= tot && 1LL * i * primes[j] < MAX_N; j++)
        {
            vis[i * primes[j]] = true;
            if (i % primes[j] == 0)
            {
                mu[i * primes[j]] = 0;
                sigma[i * primes[j]] = 2LL * sigma[i] - sigma[i / primes[j]];
            }
            else
            {
                mu[i * primes[j]] = -mu[i];
                sigma[i * primes[j]] = sigma[i] * sigma[primes[j]];
            }
        }
    }
    for (int i = 1; i < MAX_N; i++)
        pmu[i] = pmu[i - 1] + mu[i] * mu[i], sigma[i] += sigma[i - 1];
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        ll ret = 0;
        for (int i = 1, gx; i <= n; i = gx + 1)
        {
            gx = n / (n / i);
            ret += getSigma(n / i) * (getMu2(gx) - getMu2(i - 1));
        }
        printf("%lld\n", ret);
    }
    return 0;
}

B - Permutation

Printing a table shows that if there are no fixed positions, the answer is \(1 \times 3 \times 5 \dots\). If they are fixed, then it is \(n!\).

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

using namespace std;

const int MAX_N = 1e5 + 200, mod = 998244353;

int n, ai[MAX_N], deg[MAX_N];
bool vis[MAX_N];

bool dfs(int u)
{
    if (vis[u])
        return false;
    vis[u] = true;
    return ai[u] ? !dfs(ai[u]) : 0;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &ai[i]);
        if (ai[i] != 0)
            deg[ai[i]] = 1;
    }
    if (n & 1)
        puts("0"), exit(0);
    int x = 0, y = 0;
    for (int i = 1, tmp; i <= n; i++)
        if (!deg[i])
            tmp = dfs(i), y += tmp, x += !tmp;
    for (int i = 1; i <= n; i++)
        if (!vis[i] && dfs(i))
            puts("0"), exit(0);
    int ans = 1;
    for (int i = 1; i <= x; i += 2)
        ans = 1LL * ans * i % mod;
    ans = 1LL * ans * ans % mod;
    for (int i = x + 1; i <= x + y; i++)
        ans = 1LL * ans * i % mod;
    printf("%d\n", ans);
    return 0;
}

C - Subsequence

Run a DP on the subsequence automaton. When designing the state, use the property that the answer should not be large: let \(dp[i][j]\) be the largest node of string B's automaton reached when the answer length is \(i\) and we are at node \(j\) of string A's automaton. If \(dp[i][a + 1] = b + 1\) (a mismatch), then the answer is \(i\).

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

using namespace std;

const int MAX_N = 4040;

int n, m, k, nxt[2][MAX_N][MAX_N], sA[MAX_N], sB[MAX_N], dp[MAX_N][MAX_N];

int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &sA[i]);
    for (int i = 1; i <= m; i++)
        scanf("%d", &sB[i]);
    for (int c = 1; c <= k; c++)
        nxt[0][n][c] = n + 1, nxt[1][m][c] = m + 1, nxt[0][n + 1][c] = n + 1, nxt[1][m + 1][c] = m + 1;
    for (int i = n - 1; i >= 0; i--)
    {
        for (int c = 1; c <= k; c++)
            nxt[0][i][c] = nxt[0][i + 1][c];
        nxt[0][i][sA[i + 1]] = i + 1;
    }
    for (int i = m - 1; i >= 0; i--)
    {
        for (int c = 1; c <= k; c++)
            nxt[1][i][c] = nxt[1][i + 1][c];
        nxt[1][i][sB[i + 1]] = i + 1;
    }
    memset(dp, -1, sizeof(dp)), dp[0][0] = 0;
    for (int i = 0; i <= min(n, m); i++)
    {
        for (int j = 0; j <= n; j++)
            if (dp[i][j] != -1)
            {
                for (int c = 1; c <= k; c++)
                    dp[i + 1][nxt[0][j][c]] = max(dp[i + 1][nxt[0][j][c]], nxt[1][dp[i][j]][c]);
            }
        if (dp[i + 1][n + 1] == m + 1)
            printf("%d\n", i + 1), exit(0);
    }
    return 0;
}