Olympiad in Informatics

Codeforces Round #548 (Div. 2) Problem-Solving Report (CF1139)

A - Even Substrings

Very simple: while scanning, if the current digit is even, just add the current position to the answer.

// A.cpp
#include <bits/stdc++.h>
using namespace std;
char str[66000];
int main()
{
    int ans = 0, len;
    scanf("%d", &len), scanf("%s", str + 1);
    for (int i = 1; i <= len; i++)
        if (((str[i] - '0') & 1) == 0)
            ans += i;
    printf("%d", ans);
    return 0;
}

B - Chocolates

Scan from back to front and just take the minimum of the previous value minus one and the limit (remember to special-case \(0\)).

// B.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = (int)2e5 + 2000;
int n, arr[MAX_N], trial[MAX_N];
ll check()
{
    memset(trial, 0x3f, sizeof(trial));
    ll acc = 0;
    for (int i = n; i >= 1; i--)
    {
        if (trial[i + 1] == 0)
        {
            trial[i] = 0;
            continue;
        }
        trial[i] = min(trial[i + 1] - 1, arr[i]);
        acc += trial[i];
    }
    return acc;
}
int main()
{
    ll l = 0, r = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &arr[i]), r += arr[i];
    printf("%lld", check());
    return 0;
}

C - Edgy Trees

Looks like a very hard problem at first glance, but it's actually really easy.

Consider just subtracting it out with inclusion–exclusion: the initial answer is \(n^k\); ignore the black edges when building the tree, then for each connected component compute the contribution \(subtreeSiz^k\), and remember that this contribution is negative:

\[ans = n^k - \sum_{t \in subtree} t.size()^k\]

// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e5 + 2000, mod = 1e9 + 7;
int head[MAX_N], current, n, tmpx, tmpy, tmpz;
int subtreeSiz[MAX_N], totTree, k;
bool vis[MAX_N];
struct edge
{
    int to, nxt, color;
} edges[MAX_N];
void addpath(int src, int dst, int color)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    edges[current].color = color, head[src] = current++;
}
ll quick_pow(ll bas, ll tim)
{
    if (bas == 1)
        return 1;
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}
void dfs(int u, int org, int fa)
{
    vis[u] = true;
    subtreeSiz[org]++;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
            dfs(edges[i].to, org, u);
}
int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &k);
    bool flag = false;
    for (int i = 1; i <= n - 1; i++)
    {
        scanf("%d%d%d", &tmpx, &tmpy, &tmpz);
        if (tmpz != 1)
            addpath(tmpx, tmpy, tmpz), addpath(tmpy, tmpx, tmpz);
        else
            flag = true;
    }
    if (!flag)
        printf("0"), exit(0);
    for (int i = 1; i <= n; i++)
        if (!vis[i])
            dfs(i, ++totTree, 0);
    ll ans = quick_pow(n, k);
    for (int i = 1; i <= totTree; i++)
        ans = (ans - quick_pow(subtreeSiz[i], k) + mod) % mod;
    printf("%lld", ans);
    return 0;
}

D - Steps to One

I referred to this big shot's article: cf1139D. Steps to One(dp)

First, consider designing the state: let \(dp[i]\) be, when the \(gcd\) is \(i\), the expected length to turn the \(gcd\) of the whole sequence into \(1\). From this state we can work out:

\[ dp[i] = \frac{ \sum_{j = 1}^i dp[gcd(i,j)]+1 }{m} \]

Then we can enumerate \(k=gcd(i,j)\):

\[ dp[i] = 1 + \frac{ \sum_{k = 1}^m dp[k]*f(i,k) }{m} \]

Here \(f(i,k)\) denotes, within the interval \(j \in [1,m]\), the count of those satisfying \(gcd(i,j)=k\) among the \(j\), which we can consider precomputing. Note that the enumeration may hit \(i\) itself, so pull it out:

\[ (1-\frac{\lfloor \frac{m}{i} \rfloor}{m})dp[i] = 1 + \frac{\sum_{k=1}^{i-1} dp[k]*f(i,k)}{m} \]

Now let's think about how to precompute the function \(f\). First, we can set all the initial values to \(f(i,k) = \lfloor \frac{m}{k} \rfloor\), then for pairs with \(u>v,v|u\), just do \(f(i,v)-=f(i,u)\) for inclusion–exclusion.

// D.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e5 + 200, mod = 1e9 + 7;
vector<int> facts[MAX_N], cnt[MAX_N];
ll m, dp[MAX_N], invm;
ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}
int main()
{
    scanf("%lld", &m), invm = quick_pow(m, mod - 2);
    for (int i = 1; i <= m; i++)
        for (int j = i; j <= m; j += i)
            facts[j].push_back(i);
    for (int i = 1; i <= m; i++)
    {
        cnt[i].resize(facts[i].size() + 1);
        for (int j = facts[i].size() - 1; j != -1; j--)
        {
            cnt[i][j] = m / facts[i][j];
            int siz = facts[i].size();
            for (int k = j + 1; k < siz; k++)
                if (facts[i][k] % facts[i][j] == 0)
                    cnt[i][j] -= cnt[i][k];
        }
    }
    ll ans = 0;
    for (int i = 2; i <= m; i++)
    {
        int dom = m, tmp = 0;
        for (int j = 0; j < facts[i].size(); j++)
            if (facts[i][j] == i)
                dom -= cnt[i][j];
            else
                tmp = (tmp + cnt[i][j] * dp[facts[i][j]] + mod) % mod;
        dp[i] = (tmp + m + mod) % mod * quick_pow(dom, mod - 2) % mod;
    }
    for (int i = 1; i <= m; i++)
        ans = (ans + dp[i] + 1 + mod) % mod;
    printf("%lld", ans * invm % mod);
    return 0;
}