---
id: 1958
lang: en
translation_of: index.md
source_sha256: 2f8b3541ef4cb28976a0a3869d3a3f1b3d3800283d663e07f17ceab74339a09e
title: "[National Training Team 2011 Yangtian] Problem-Solving Report"
description: "Three problems: hashing on a Fenwick tree for arithmetic subsequences, cycle shrinking for shortest paths, and a bitmask DP with fast matrix exponentiation."
tags: ["OI","Hashing","Shortest Path","Matrices","Tarjan","Cycles"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "扬天 in the title kept as the pinyin “Yangtian”, the name of the contest set."
  - "最段路 read as 最短路 (shortest paths); 段 is a typo for 短."
  - "排斥反应 (problem C) rendered as “Rejection Reaction”."
  - "两个一不相邻 rendered as “no two ones adjacent”, keeping the spelled-out 一 (one)."
---

## A - Arithmetic Subsequence

Since it's a permutation, while reading the input we can place the values seen so far on a number line, and we find that a single symmetric pair is enough. We can use the idea of hashing to <span class="slang" data-zh="魔改" data-gloss="mógǎi, “magic modification”. Internet slang for modifying something heavily and unconventionally.">heavily mod</span> a Fenwick tree, maintain the hashes of the string and of the reversed string, and then just compare them.

<!--more-->

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

using namespace std;

const int MAX_N = 10100, bitnum = 133;
typedef unsigned long long ull;

int T, n, ai[MAX_N];
ull nodes[2][MAX_N], pw[MAX_N];

inline char nc()
{
    static char buf[1000000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
}

int read()
{
    int x = 0, f = 1;
    char ch = nc();
    while (!isdigit(ch))
        ch = nc();
    while (isdigit(ch))
        x = (x << 3) + (x << 1) + ch - '0', ch = nc();
    return x * f;
}

inline int lowbit(int x) { return x & (-x); }

void update(ull *arr, int x)
{
    for (int i = x; i <= n; i += lowbit(i))
        arr[i] += pw[i - x];
}

ull query(ull *arr, int x, ull ret = 0)
{
    for (int i = x; i; i -= lowbit(i))
        ret += arr[i] * pw[x - i];
    return ret;
}

ull query(ull *arr, int l, int r) { return query(arr, r) - query(arr, l - 1) * pw[r - l + 1]; }

int main()
{
    T = read();
    for (int i = pw[0] = 1; i < MAX_N; i++)
        pw[i] = pw[i - 1] * bitnum;
    while (T--)
    {
        memset(nodes, 0, sizeof(nodes));
        n = read();
        bool flag = false;
        for (int i = 1; i <= n; i++)
        {
            ai[i] = read();
            int len = min(ai[i] - 1, n - ai[i]);
            if (len && query(nodes[0], ai[i] - len, ai[i]) != query(nodes[1], n - ai[i] - len + 1, n - ai[i] + 1))
                flag = true;
            update(nodes[0], ai[i]), update(nodes[1], n - ai[i] + 1);
        }
        puts(flag ? "Y" : "N");
    }
    return 0;
}
```

## B - Shortest Path

We consider a common <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span>: shrink the cycles so the graph becomes a tree, then handle it with the <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span> of differences on the tree. Of course, we can't just compute it head-on, because if the $LCA$ is a cycle vertex, then computing the length of an arc on the cycle is rather troublesome. We consider first finding the single-source shortest paths from vertex $1$, and then computing the cycle length for each cycle vertex; we know that the distance between two vertices $A, B$ on a cycle is: $\min \{ |pre[A] - pre[B]|, loop - |pre[A] - pre[B]| \}$. Here, $pre$ is the distance on the new tree, and the new tree is the graph obtained after brute-force breaking the cycles of the original graph (at random).

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

using namespace std;

const int MAX_N = 1e5 + 200;
typedef pair<int, int> pii;
typedef long long ll;

int head[MAX_N], current, n, m, q, dfn[MAX_N], low[MAX_N], stk[MAX_N], tail, ptot;
int ncnt, aff[MAX_N], up[15][MAX_N], dep[MAX_N];
ll dist[MAX_N], loops[MAX_N], prefix[MAX_N];
bool inst[MAX_N], vis[MAX_N];

vector<int> G[MAX_N];

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++;
}

void shortest_path(int src)
{
    memset(dist, 0x3f, sizeof(dist));
    priority_queue<pii> pq;
    pq.push(make_pair(0, src)), dist[src] = 0;
    while (!pq.empty())
    {
        int u = pq.top().second;
        pq.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] > dist[u] + edges[i].weight)
                dist[edges[i].to] = dist[u] + edges[i].weight, pq.push(make_pair(-dist[edges[i].to], edges[i].to));
    }
    memset(vis, false, sizeof(vis));
}

void tarjan(int u, int fa)
{
    dfn[u] = ++ptot;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            if (dfn[edges[i].to] == 0)
                up[0][edges[i].to] = u, prefix[edges[i].to] = prefix[u] + edges[i].weight, tarjan(edges[i].to, u);
            else if (dfn[edges[i].to] < dfn[u])
            {
                loops[++ncnt] = 0LL + prefix[u] - prefix[edges[i].to] + edges[i].weight;
                for (int pt = u, pre; pt != edges[i].to;)
                    G[edges[i].to].push_back(pt), aff[pt] = ncnt, pre = up[0][pt], up[0][pt] = edges[i].to, pt = pre;
            }
        }
    if (up[0][u] == fa)
        G[fa].push_back(u);
}

void dfs_collect(int u, int fa)
{
    dep[u] = dep[fa] + 1;
    for (int i = 0, siz = G[u].size(); i < siz; i++)
        if (G[u][i] != fa)
            dfs_collect(G[u][i], u);
}

ll query(int x, int y)
{
    if (dep[x] < dep[y])
        swap(x, y);
    int u = x, v = y;
    for (int i = 14; i >= 0; i--)
        if (dep[up[i][x]] >= dep[y])
            x = up[i][x];
    if (x == y)
        return dist[u] - dist[v];
    for (int i = 14; i >= 0; i--)
        if (up[i][x] != up[i][y])
            x = up[i][x], y = up[i][y];
    if (aff[x] && aff[x] == aff[y])
        return dist[u] - dist[x] + dist[v] - dist[y] + min(llabs(prefix[x] - prefix[y]), loops[aff[x]] - llabs(prefix[x] - prefix[y]));
    return dist[u] + dist[v] - (dist[up[0][x]] << 1);
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d%d", &n, &m, &q);
    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);
    shortest_path(1), tarjan(1, 0);
    dfs_collect(1, 0);
    for (int i = 1; i <= 14; i++)
        for (int j = 1; j <= ptot; j++)
            up[i][j] = up[i - 1][up[i - 1][j]];
    while (q--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        printf("%lld\n", query(x, y));
    }
    return 0;
}
```

## C - Rejection Reaction

First, this problem can be turned into doing <span class="slang" data-zh="互不侵犯" data-gloss="hù bù qīnfàn, “not invading each other”. The SCOI2005 problem (Luogu P1896) of placing kings on a grid so that no two attack each other, a classic bitmask DP.">Mutual Non-Aggression</span> on a grid. The concrete transformation: take the top-left corner as the starting point, a step to the left counts as $+p$ and a step to the right counts as $+q$; since $\gcd(p, q) = 1$, the numbers in the grid are all distinct. After turning it into this problem, we can design a bitmask DP equation. We find that a state must have no two ones adjacent, and the $1$s at the front end and the back end must not be adjacent either, so when $p = 10$, the number of states drops to $123$. We can consider fast matrix exponentiation.

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

using namespace std;

const int MAX_N = 150, mod = 19921107;

int p, q, stats[MAX_N], stat_tot;

struct matrix
{
    int mat[MAX_N][MAX_N];

    void clear() { memset(mat, 0, sizeof(mat)); }

    int *operator[](const int &rhs) { return mat[rhs]; }

    matrix operator*(const matrix &rhs)
    {
        matrix ret;
        ret.clear();
        for (int i = 1; i <= stat_tot; i++)
            for (int j = 1; j <= stat_tot; j++)
                for (int k = 1; k <= stat_tot; k++)
                    ret[i][j] = (1LL * ret[i][j] + 1LL * mat[i][k] * rhs.mat[k][j] % mod) % mod;
        return ret;
    }

    matrix operator^(const int &rhs)
    {
        int tim = rhs;
        matrix ret, bas = *this;
        ret.clear();
        for (int i = 1; i <= stat_tot; i++)
            ret[i][i] = 1;
        while (tim)
        {
            if (tim & 1)
                ret = ret * bas;
            bas = bas * bas;
            tim >>= 1;
        }
        return ret;
    }
} A, trans;

int main()
{
    scanf("%d%d", &p, &q);
    // process the stats;
    for (int i = 0; i < (1 << p); i++)
    {
        bool flag = true;
        for (int j = 1; j < p; j++)
            if ((i & (1 << (j - 1))) && (i & (1 << j)))
            {
                flag = false;
                break;
            }
        if (p != 1)
            flag &= (!((i & 1) && (i & (1 << (p - 1)))));
        if (flag)
            stats[++stat_tot] = i;
    }
    if (q == 1)
        printf("%d\n", stat_tot), exit(0);
    int ans = 0;
    trans.clear();
    for (int i = 1; i <= stat_tot; i++)
    {
        A[i][i] = 1;
        for (int j = 1; j <= stat_tot; j++)
            if (!(stats[i] & stats[j]))
                trans[i][j] = 1;
    }
    A = A * (trans ^ (q - 1));
    for (int i = 1; i <= stat_tot; i++)
        for (int j = 1; j <= stat_tot; j++)
            if (!(stats[i] & stats[j]))
                ans = (1LL * ans + A[i][j]) % mod;
    printf("%d\n", ans);
    return 0;
}
```
