---
id: 1178
lang: en
translation_of: index.md
source_sha256: 5c018d9e5681a836dbf4087d81ca9e3ecf0a9e3efb9dc2ce469d96d64548fe29
title: "Codeforces Round #539 (Div. 2) Problem-Solving Report (CF1113)"
description: "XOR is its own inverse, so the condition rearranges directly; then keep odd and even counts at the ends of the Trie."
tags: ["OI","Strings","Trie","Permutations and Combinations","Counting","Combinatorics"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "The Prufer-sequence sums keep 为 and 序列 inside the TeX subscripts as written, since only \\text{…} contents may be translated."
  - "思路清奇 rendered as “refreshingly unconventional”; treated as an ordinary idiom rather than slang, so no chip (as in post 401)."
  - "大水题 rendered as “a total freebie”, with the chip on the 水题 part, as in post 1950."
---

## C - Sasha and a Bit of Relax

Actually, this problem is a total <span class="slang" data-zh="水题" data-gloss="shuǐtí, “water problem”. An easy problem.">freebie</span>. We know the condition is $x_l \text{ xor } x_{l+1} \dots x_{mid} = x_{mid+1} \text{ xor } x_{mid+2} \dots x_{r}$, but we can move terms straight across in this operation: because $xor$ is its own inverse. Then, to make sure $r-l+1$ is even, we just need to maintain odd and even counts at the ends of the Trie.

<!--more-->

```cpp
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5 * 20 + 20000;
int trie[MAX_N][2], tot = 1, sum[MAX_N], n, even[MAX_N], odd[MAX_N];
void insert(int num, int id)
{
    int p = 1;
    for (int i = 30; i >= 0; i--)
    {
        int bit = (num >> i) & 1;
        if (trie[p][bit] == 0)
            trie[p][bit] = ++tot;
        p = trie[p][bit];
    }
    if (id & 1)
        odd[p]++;
    else
        even[p]++;
}
int find(int num)
{
    int p = 1;
    for (int i = 30; i >= 0; i--)
    {
        int bit = (num >> i) & 1;
        if (trie[p][bit] == 0)
            return -1;
        p = trie[p][bit];
    }
    return p;
}
int main()
{
    scanf("%d", &n);
    long long ans = 0;
    insert(0, 0);
    for (int r = 1; r <= n; r++)
    {
        scanf("%d", &sum[r]), sum[r] ^= sum[r - 1];
        int id = find(sum[r]);
        if (id != -1)
            if (r & 1)
                ans += odd[id];
            else
                ans += even[id];
        insert(sum[r], r);
    }
    printf("%lld", ans);
    return 0;
}
```

## D - Sasha and One More Name

The approach to this problem is refreshingly unconventional.

First, through <span class="slang" data-zh="人类智慧" data-gloss="rénlèi zhìhuì, “human wisdom”. OI slang for a solution that hinges on a clever, hard-to-motivate insight rather than a standard technique.">human wisdom</span> we learn that the answer can only be one of three things, $\text{no solution}, 1, 2$, and then we discuss each case:

1. If some letter appears a staggering $n-1$ or $n$ times, we immediately rule that there is no solution.
2. After that, consider enumerating the split point in $O(n)$, cutting into a left part and a right part and splicing them back together: the left end of the left part is joined to the right end of the right part. This is easy to check. The total complexity is $O(n^2)$.
3. Whatever is left, the answer is $2$.

```cpp
// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5050;
char str[MAX_N];
long long ans, n, cnt[26];

void goFuck() { printf("Impossible"), exit(0); }

int main()
{
    scanf("%s", str + 1), n = strlen(str + 1);
    char opt = str[1];
    string st = str + 1;
    for (int i = 1; i <= n; i++)
        cnt[str[i] - 'a']++;
    for (int i = 0; i < 26; i++)
        if (cnt[i] == n || cnt[i] == n - 1)
            goFuck();
    for (int i = 1; i < n; i++)
    {
        bool flag = true;
        string sub = st.substr(i) + st.substr(0, i);
        for (int pt = 0; pt < sub.length(); pt++)
            if (sub[pt] != sub[sub.length() - pt - 1])
            {
                flag = false;
                break;
            }
        if (flag && sub != st)
            printf("1"), exit(0);
    }
    printf("2");
    return 0;
}
```

## F - Sasha and Interesting Fact from Graph Theory

This problem is really interesting.

First consider A and B lying on one chain; let's deal with the case of this chain first:

![chain](media/chain.png)

Consider that the number of points on this chain (excluding A and B) ranges over $[0, min(n - 2, m - 1)]$; when counting the answer, we enumerate $i$ as the number of points inside the chain (excluding A and B). Ignoring edge weights and considering only the number of points and their arrangement, there are in total:

$$
\sum_{i = 0}^{min(n - 2, m - 1)} P(n - 2, i)
$$

Considering that the sum of the edge weights on the chain is always $m$, once we maintain this property the answer becomes:

$$
\sum_{i = 0}^{min(n - 2, m - 1)} P(n - 2, i) {m - 1 \choose i}
$$

Because this chain lies on the spanning tree, and the other edge weights can be chosen freely from $[1, m]$, the answer is:

$$
ans = \sum_{i = 0}^{m - 1} P(n - 2, i) * {m - 1 \choose i} * m^{n-2-i} * n^{n - 3 - i}(i+2)
$$

Here we need to prove that the number of spanning trees is $n^{n - 3 - i}(i+2)$:

**Proof**  For this tree, we can partition the points into $n - i - 2 + 1$ point sets: the points on the chain form one point set of size $i + 2$, and every remaining point is a point set on its own. The number of ways to connect two point sets is $|S_1|*|S_2|$. So in total it is:

$$
\sum_{p 为 Prufer 序列} \prod_i size[i]^{di} = \sum_{p 为 Prufer 序列} \prod_i size[i]^{di - 1} \prod size[i]
$$

Because:

$$
n^{n - 2} = \sum_i d_i-1
$$

So:

$$
n^{n - 3 - i}(i+2)
$$

```cpp
// F.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 1e6 + 2000, mod = 1e9 + 7;
ll level[MAX_N], level_rev[MAX_N], n, m, tmpx, tmpy;

ll quick_pow(ll bas, ll tim)
{
    if (tim < 0)
        return quick_pow(bas, tim + mod - 1);
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}

ll P(ll a, ll b)
{
    return level[a] * level_rev[a - b] % mod;
}

ll comb(ll a, ll b)
{
    return level[a] * level_rev[b] % mod * level_rev[a - b] % mod;
}

int main()
{
    scanf("%lld%lld%lld%lld", &n, &m, &tmpx, &tmpy);
    ll ans = 0;
    // preprocessing;
    level[1] = level_rev[1] = level[0] = level_rev[0] = 1;
    for (int i = 2; i < MAX_N; i++)
        level[i] = level[i - 1] * i % mod;
    level_rev[MAX_N - 1] = quick_pow(level[MAX_N - 1], mod - 2);
    for (int i = MAX_N - 1; i >= 3; i--)
        level_rev[i - 1] = level_rev[i] * i % mod;
    // start to calc!
    for (int i = 0; i <= min(m - 1, n - 2); i++)
        ans = (ans + P(n - 2, i) * comb(m - 1, i) % mod * quick_pow(m, n - 2 - i) % mod * quick_pow(n, n - 3 - i) % mod * (i + 2) % mod) % mod;
    printf("%lld", ans);
    return 0;
}
```
