---
id: 2083
lang: en
translation_of: index.md
source_sha256: 99ba35a063e7652918e77b260822a260daa5d7073552b14cdeb976b85ba90a21
title: "Codeforces Round #511 (Div. 1) - Problem-Solving Report"
description: "Short solutions to Codeforces Round #511 Div. 1 A to D: a GCD sieve, a pattern found by printing a table, a sieve-style DP, and ODT with two pointers."
tags: ["OI","DP","Codeforces","ODT"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "考虑每一个 $d = \\gcd$ 出现了 $d$ 次 is terse in the source; rendered literally as “Consider each d = gcd appearing d times”."
  - "至少 $k$ 组个为 $0$ 的差 (garbled in the source) rendered literally as “at least k groups of differences equal to 0”."
  - "啊这题好难 read as 啊，这题好难 (“Ah, this problem is so hard”), not the 啊这 meme."
---

## A - Enlarge GCD

You can probably figure out that we first divide all the numbers by the GCD and then count the prime factor that occurs most often. We use the sieve of Eratosthenes to find each number's smallest prime factor, then just divide by brute force and mark them.

<!--more-->

```cpp
// CF1034A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 200, MAX_P = 1.5e7 + 200;

int n, ai[MAX_N], primes[MAX_P], ptot, gloabl_gcd, cnt[MAX_P], sfactor[MAX_P];
bool vis[MAX_P];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }

int main()
{
    // sieve process;
    for (int i = 2; i < MAX_P; i++)
    {
        if (!vis[i])
            primes[++ptot] = i;
        for (int j = 1; j <= ptot && 1LL * primes[j] * i < MAX_P; j++)
        {
            vis[i * primes[j]] = true;
            if (i % primes[j] == 0)
                break;
        }
    }
    for (int i = 2; i < MAX_P; i++)
        sfactor[i] = i;
    for (int i = 2; i < MAX_P; i++)
        if (!vis[i])
            for (int j = (i << 1); j < MAX_P; j += i)
                if (sfactor[j] == j)
                    sfactor[j] = i;
    // read;
    scanf("%d%d", &n, &gloabl_gcd), ai[1] = gloabl_gcd;
    for (int i = 2; i <= n; i++)
        scanf("%d", &ai[i]), gloabl_gcd = gcd(gloabl_gcd, ai[i]);
    for (int i = 1; i <= n; i++)
        ai[i] /= gloabl_gcd;
    for (int i = 1; i <= n; i++)
    {
        int curt = ai[i];
        while (curt != 1)
        {
            int x = sfactor[curt];
            cnt[x]++;
            while (sfactor[curt] == x)
                curt /= sfactor[curt];
        }
    }
    int ans = n;
    for (int i = 1; i < MAX_P; i++)
        ans = min(ans, n - cnt[i]);
    if (ans == n)
        puts("-1");
    else
        printf("%d\n", ans);
    return 0;
}
```

## B - Little C Loves 3 II

<span class="slang" data-zh="打表题" data-gloss="dǎbiǎo tí, “make-a-table problem”. A problem solved by brute-forcing answers for small inputs and spotting the pattern in them.">A table-printing problem</span>, I guess: first use network flow to [<span class="slang" data-zh="打个表" data-gloss="dǎ ge biǎo, “make a table”. Here: brute-forcing answers for small inputs to look for a pattern in them.">print a table</span>](https://pastebin.ubuntu.com/p/9pFX3VyKv4/), then find the pattern and you're done.

```cpp
// CF1034B.cpp
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll n, m;

int main()
{
    scanf("%lld%lld", &n, &m);
    if (n > m)
        swap(n, m);
    long long ans = 0;
    if (n == 1)
        ans = 6LL * ll(m / 6) + 2LL * max(m % 6 - 3, 0LL);
    else if (n == 2)
    {
        if (m == 2)
            ans = 0;
        else if (m == 3)
            ans = 4;
        else if (m == 7)
            ans = 12;
        else
            ans = m * n;
    }
    else
        ans = 1LL * n * m;
    printf("%lld\t", ans ^ (ans & 1LL));
    return 0;
}
```

## C - Region Seperation

Ah, this problem is so hard.

First consider the case of splitting into two levels, and list a few things we know:

- The level-two sum $L_2$ must be a divisor of $S_1$.
- Suppose we want to peel off blocks of size $\frac{S_1}{k}$; then we can do it bottom-up and record the gcd with $S_1$. Consider each $d = \gcd$ appearing $d$ times; then, under the modulus, we can directly subtract $k$ times to get at least $k$ groups of differences equal to $0$. This is very similar to the greedy conclusion of a certain knapsack problem.
- Finally, use a sieve-style DP to count all the answers.

```cpp
// CF1034C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6 + 200, mod = 1e9 + 7;
typedef long long ll;

int n, fa[MAX_N], dp[MAX_N];
ll sum[MAX_N], cnt[MAX_N];

ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%lld", &sum[i]);
    for (int i = 2; i <= n; i++)
        scanf("%d", &fa[i]);
    for (int i = n; i >= 1; i--)
        if (fa[i])
            sum[fa[i]] += sum[i];
    for (int i = n; i >= 1; i--)
    {
        sum[i] = sum[1] / gcd(sum[1], sum[i]);
        if (sum[i] <= n)
            cnt[sum[i]]++;
    }
    for (int i = n; i >= 1; i--)
        for (int j = (i << 1); j <= n; j += i)
            cnt[j] += cnt[i];
    dp[1] = 1;
    int ans = 0;
    for (int i = 1; i <= n; i++)
        if (cnt[i] == i)
        {
            ans = (0LL + ans + dp[i]) % mod;
            for (int j = i << 1; j <= n; j += i)
                dp[j] = (0LL + dp[j] + dp[i]) % mod;
        }
    printf("%d\n", ans);
    return 0;
}
```

## D - Intervals of Intervals

First, a binary search on the answer to find the size of the $k$-th largest set. We can handle the segment sums the ODT way.

After the binary search, we can use this answer as the condition for two-pointers, and then compute the count and the sum of lengths. The final answer comes out a bit too large; we just take the number of segments counted minus the number of segments required, multiply by the size of the $k$-th largest set, and that finishes it.

```cpp
// CF1034D.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 200, INF = 0x3f3f3f3f;

typedef pair<int, int> pii;
typedef long long ll;

int n, limit, li[MAX_N], ri[MAX_N], len[MAX_N];

struct node
{
    int l, r, timestamp;
    bool operator<(const node &rhs) const { return l < rhs.l || (l == rhs.l && r < rhs.r); }
};

multiset<node> ms;
vector<pii> info[MAX_N];

bool check(int mid)
{
    int acc = 0;
    ll cnt = 0;
    for (int i = 1, ptr = 1; i <= n; i++)
    {
        len[i] = ri[i] - li[i] + 1, acc += len[i];
        for (auto &x : info[i])
        {
            len[x.second] -= x.first;
            // coming later;
            if (x.second >= ptr)
                acc -= x.first;
        }
        while (acc >= mid)
            acc -= len[ptr], ptr++;
        cnt += ptr - 1;
    }
    return cnt >= limit;
}

void calc(int max_len)
{
    int acc = 0;
    ll ans = 0, len_acc = 0, len_cnt = 0;
    for (int i = 1, ptr = 1; i <= n; i++)
    {
        len[i] = ri[i] - li[i] + 1, acc += len[i];
        for (auto &x : info[i])
        {
            len[x.second] -= x.first;
            // coming later;
            if (x.second >= ptr)
                acc -= x.first;
            if (x.second > 0 && x.second < ptr)
                len_acc -= 1LL * x.second * x.first;
        }
        while (acc >= max_len)
            acc -= len[ptr], len_acc += 1LL * ptr * len[ptr], ptr++;
        ans += len_acc + 1LL * acc * (ptr - 1), len_cnt += (ptr - 1);
    }
    printf("%lld\n", ans - 1LL * (len_cnt - limit) * max_len);
}

int main()
{
    scanf("%d%d", &n, &limit);
    for (int i = 1; i <= n; i++)
        scanf("%d%d", &li[i], &ri[i]), ri[i]--;
    // ODT-like initialize;
    ms.insert(node{0, INF, 0});
    for (int i = 1; i <= n; i++)
    {
        auto it = --ms.lower_bound(node{li[i], INF, 0});
        if (it->r >= ri[i])
        {
            // covered;
            info[i].push_back(make_pair(ri[i] - li[i] + 1, it->timestamp));
            if (it->l < li[i])
                ms.insert(node{it->l, li[i] - 1, it->timestamp});
            if (it->r > ri[i])
                ms.insert(node{ri[i] + 1, it->r, it->timestamp});
            ms.erase(it), ms.insert(node{li[i], ri[i], i});
        }
        else
        {
            // intersect;
            info[i].push_back(make_pair(it->r - li[i] + 1, it->timestamp));
            if (it->l < li[i])
                ms.insert(node{it->l, li[i] - 1, it->timestamp});
            ms.erase(it++);
            if (it->r < li[i])
                it++;
            while (it->r < ri[i])
                info[i].push_back(make_pair(it->r - it->l + 1, it->timestamp)), ms.erase(it++);
            info[i].push_back(make_pair(ri[i] - it->l + 1, it->timestamp));
            if (it->r > ri[i])
                ms.insert(node{ri[i] + 1, it->r, it->timestamp});
            ms.erase(it), ms.insert(node{li[i], ri[i], i});
        }
    }
    int l = 0, r = 1e9, res;
    while (l <= r)
    {
        int mid = (l + r) >> 1;
        if (check(mid))
            l = mid + 1, res = mid;
        else
            r = mid - 1;
    }
    calc(res);
    return 0;
}
```
