---
id: 1743
lang: en
translation_of: index.md
source_sha256: e5e6f6aaa8d776eeb059b12cdccdce8dcfac1311024441e738e696d1dfc38b2c
title: "NOIp 2017 Problem-Solving Report"
description: "Brief notes and code for all six NOIp 2017 problems, from Xiaokai’s Puzzle to Lining Up."
tags: ["OI","NOIP","2017","NOIp 2017"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "策策主任逛机房 rendered as “Director Cece strolling the computer lab”, keeping the parody of the problem name 逛公园; 策策 is a nickname whose referent is not stated in the post, so it was romanized and left unexplained."
  - "Problem names were translated literally (小凯的疑惑 → Xiaokai’s Puzzle, 列队 → Lining Up, etc.) rather than taken from any official English statement."
  - "Lowercase sb was chipped with the glossary gloss of SB, since it is the same word."
---

## A - Xiaokai’s Puzzle

A <span class="slang" data-zh="送命" data-gloss="sòngmìng, “to give away one's life”. Deadly; said of a problem that can easily cost you dearly.">killer</span> pattern-spotting problem. Of course you can also use the properties of exgcd; what follows is <span class="slang" data-zh="感性理解" data-gloss="gǎnxìng lǐjiě, “perceptual understanding”. OI slang for a hand-wavy, intuitive argument instead of a rigorous proof.">hand-wavy intuition</span>:

Let this number be $x \equiv ya {\pmod b}$; expanding it gives:

$$
x = ya + kb, k \in [1, b - 1]
$$

We find that it holds whenever $k \geq 0$, so going the other way and taking $-1$ makes it both the largest and not satisfying the condition.

```cpp
// P3951.cpp
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    unsigned long long a, b, i;
    cin >> a >> b;
    cout << a * b - a - b;
    return 0;
}
```

<!--more-->

## B - Time Complexity

At first I found this problem really scary, but in the end I still wrote it out on my own (because the format the problem gives is actually pretty nice, and it wasn’t as hard as I imagined). Just use counters to record the count and whether a loop runs zero times.

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

using namespace std;

char content[1010], opt[20];
int T, L, complexity, vis[300];

struct instruction
{
    char typ, var_name[3];
    bool tag, tag2;
    int x, y;
    instruction()
    {
        typ = 0, memset(var_name, 0, sizeof(var_name));
        tag = tag2 = false;
        x = y = 0;
    }
};
stack<instruction> st;

int read()
{
    int pos = 1, ret = 0;
    while (opt[pos] != 0)
        ret = ret * 10 + opt[pos] - '0', pos++;
    return ret;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        memset(vis, 0, sizeof(vis));
        // complexity reader;
        complexity = 0;
        memset(opt, 0, sizeof(opt));
        scanf("%d%s", &L, opt + 1);
        if (strlen(opt + 1) == 4)
            complexity = (opt[3] == '1') ? 0 : 1;
        else
            for (int i = 5, siz = strlen(opt + 1); i <= siz - 1; i++)
                complexity = complexity * 10 + (opt[i] - '0');
        // read the whole content;
        int mx_idx = 0;
        bool err_flag = false;
        while (!st.empty())
            st.pop();
        for (int i = 1, curt_complexity = 0, choke = 0; i <= L; i++)
        {
            instruction ist;
            ist.tag = false, ist.tag2 = false;
            scanf("%s", content + 1);
            ist.typ = content[1];
            if (ist.typ == 'F')
            {
                scanf("%s", ist.var_name + 1);
                // repeated name;
                err_flag |= (vis[ist.var_name[1]] > 0);
                vis[ist.var_name[1]]++;
                // read the condition;
                scanf("%s", opt + 1);
                if (opt[1] == 'n')
                    ist.x = -1;
                else
                    ist.x = read();

                scanf("%s", opt + 1);
                if (opt[1] == 'n')
                    ist.y = -1;
                else
                    ist.y = read();

                if (ist.x != -1 && ist.y == -1)
                {
                    // O(n);
                    if (choke == 0)
                        curt_complexity++, ist.tag2 = true;
                }
                else if (ist.x == -1 && ist.y != -1)
                    choke++, ist.tag = true;
                else if (ist.x != -1 && ist.y != -1 && ist.x > ist.y)
                    choke++, ist.tag = true;

                st.push(ist);
            }
            else if (ist.typ == 'E')
            {
                if (st.empty())
                    // nowhere to go;
                    err_flag = true;
                else
                {
                    instruction current_ist = st.top();
                    if (current_ist.tag)
                        choke--;
                    if (current_ist.tag2)
                        curt_complexity--;
                    vis[current_ist.var_name[1]]--;
                    st.pop();
                }
            }
            mx_idx = max(mx_idx, curt_complexity);
        }
        err_flag |= (!st.empty());
        if (err_flag)
        {
            puts("ERR");
            continue;
        }
        puts(mx_idx == complexity ? "Yes" : "No");
    }
    return 0;
}
```

## C - Strolling in the Park

~~Director Cece strolling the computer lab.~~

This problem looks super hard, but in the end brute-forcing it with memoized search surprised me at first, though after a few seconds I really found it clever. Since $k < 50$, clearly we can just run memoized search over the states and transition (who the hell cares about the transition order).

Set up the state $dp[u][addition]$, where $addition$ is the remaining $bonus$ part, and then you can start counting by brute force. This method is really good.

```cpp
// P3953.cpp
#include <bits/stdc++.h>
#define ll long long
#define pr pair<ll, int>

using namespace std;

const int MAX_N = 100100, MAX_E = 200010;

int head[MAX_N], n, m, k, p, current, T;
ll dist[MAX_N], dp[MAX_N][60];
bool vis[MAX_N], inst[MAX_N][60];

struct edge
{
	int to, nxt;
	ll weight;
	bool tag;
} edges[MAX_E << 1];

void addpath(int src, int dst, ll weight, bool toggle)
{
	edges[current].to = dst, edges[current].nxt = head[src];
	edges[current].tag = toggle, edges[current].weight = weight;
	head[src] = current++;
}

void shortest_path()
{
	memset(dist, 0x3f, sizeof(dist));
	memset(vis, false, sizeof(vis));
	priority_queue<pr> pq;
	pq.push(make_pair(0, n)), dist[n] = 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 (edges[i].tag == true && 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));
	}
}

int dfs(int u, int addition)
{
	if (inst[u][addition])
		return -1;
	if (dp[u][addition])
		return dp[u][addition];
	inst[u][addition] = true;
	if (u == n)
		dp[u][addition] = 1;
	for (int i = head[u]; i != -1; i = edges[i].nxt)
		if (dist[edges[i].to] - dist[u] + edges[i].weight <= addition && edges[i].tag == true)
		{
			ll diff = dist[edges[i].to] - dist[u] + edges[i].weight;
			if (dfs(edges[i].to, addition - diff) == -1)
				return (dp[u][addition] = -1);
			dp[u][addition] = (1LL * dp[u][addition] + 1LL * dp[edges[i].to][addition - diff]) % p;
		}
	inst[u][addition] = false;
	return dp[u][addition];
}

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		memset(head, -1, sizeof(head)), current = 0;
		memset(inst, 0, sizeof(inst)), memset(dp, 0, sizeof(dp));
		scanf("%d%d%d%d", &n, &m, &k, &p);
		for (int i = 1, u, v, w; i <= m; i++)
			scanf("%d%d%d", &u, &v, &w), addpath(u, v, w, false), addpath(v, u, w, true);
		shortest_path();
		for (int i = 0; i < current; i++)
			edges[i].tag ^= 1;
		printf("%d\n", dfs(1, k));
	}
	return 0;
}
```

## D - Cheese

A <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> DSU takes care of it.

```cpp
// P3958.cpp
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
#define ll long long

int n, h, r;

struct Point
{
    ll x, y, z;
    Point() {}
    Point(int x, int y, int z)
    {
        this->x = x;
        this->y = y;
        this->z = z;
    }
};

struct Sphere
{
    Point center;
    ll radius;
    bool upperColliding;
    bool lowerColliding;
    int index;

    Sphere() {}
    Sphere(Point c, int radius);
};

double getDist(Point a, Point b);
bool checkCollide(Sphere s1, Sphere s2);
bool checkLowerCollide(Sphere s);
bool checkUpperCollide(Sphere s);

Sphere::Sphere(Point c, int radius)
{
    center = c;
    this->radius = radius;
}

double getDist(Point a, Point b)
{
    return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2) + pow(a.z - b.z, 2));
}

bool checkUpperCollide(Sphere s)
{
    if (s.center.z + s.radius >= h)
        return true;
    return false;
}

bool checkLowerCollide(Sphere s)
{
    if (s.center.z - s.radius <= 0)
        return true;
    return false;
}

bool checkCollide(Sphere s1, Sphere s2)
{
    if (getDist(s1.center, s2.center) <= s1.radius + s2.radius)
        return true;
    return false;
}

struct Batch
{
    vector<Sphere> spheres;
    vector<Sphere> upper;
    vector<Sphere> lower;
    int mem[100100];

    void MakeHeap()
    {
        for (int i = 0; i < n + 2; i++)
            mem[i] = i;
    }

    void UnionElement(int p, int q)
    {
        int p_parent = FindElement(p);
        int q_element = FindElement(q);
        if (p_parent != q_element)
            mem[p_parent] = q_element;
    }

    int FindElement(int p)
    {
        if (mem[p] != p)
            mem[p] = FindElement(mem[p]);
        return mem[p];
    }

    bool allFlag = true;
    bool goFlag = false;

    void initialize()
    {
        cin >> n >> h >> r;
        for (int i = 0; i < n; i++)
        {
            ll x, y, z;
            cin >> x >> y >> z;
            Sphere sss = Sphere(Point(x, y, z), r);
            sss.index = i;
            sss.upperColliding = checkUpperCollide(sss);
            sss.lowerColliding = checkLowerCollide(sss);
            if (sss.upperColliding && sss.lowerColliding)
                goFlag = true;
            if (sss.upperColliding)
                allFlag = false, upper.push_back(sss);
            if (sss.lowerColliding)
                lower.push_back(sss);
            spheres.push_back(sss);
        }
        return;
    }

    void preprocess()
    {
        MakeHeap();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
            {
                if (i == j)
                    continue;
                if (checkCollide(spheres[i], spheres[j]))
                    UnionElement(i, j);
            }
    }

    void Solve()
    {
        if (goFlag)
        {
            cout << "Yes" << endl;
            return;
        }
        if (allFlag)
        {
            cout << "No" << endl;
            return;
        }
        preprocess();
        for (int i = 0; i < upper.size(); i++)
            for (int j = 0; j < lower.size(); j++)
            {
                if (upper[i].index == lower[i].index)
                    continue;
                int resi = FindElement(upper[i].index), resj = FindElement(lower[j].index);
                if (resi != resj)
                    continue;
                cout << "Yes" << endl;
                return;
            }
        cout << "No" << endl;
    }
};

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        Batch b;
        b.initialize();
        b.Solve();
    }
    return 0;
}
```

## E - Treasure

Once you think of the state, it counts as a <span class="slang" data-zh="sb" data-gloss="Initials of 傻逼 (shǎbī). A vulgar insult, roughly “idiot” or “stupid”.">stupid</span> problem. Set up the state $dp[stat][i]$, where stat is the set of vertices already included and $i$ is the maximum tree height. Once you know this, enumerating subsets gets it done (because the maximum tree height is the worst case, and this case is handled automatically by the bitmask state; think it through yourself).

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

using namespace std;

const int MAX_N = 20, INF = 0x3f3f3f3f;

int dist[MAX_N][MAX_N], n, m, dp[1 << MAX_N][MAX_N], dist_mask[1 << MAX_N];

int main()
{
    memset(dist, 0x3f, sizeof(dist));
    scanf("%d%d", &n, &m);
    for (int i = 1, u, v, w; i <= m; i++)
        scanf("%d%d%d", &u, &v, &w), u--, v--, dist[u][v] = dist[v][u] = min(dist[u][v], w);
    for (int i = 1; i < (1 << n); i++)
        for (int j = 0; j < n; j++)
            if (i & (1 << j))
            {
                dist[j][j] = 0;
                for (int k = 0; k < n; k++)
                    if (dist[j][k] != INF)
                        dist_mask[i] |= (1 << k);
            }
    memset(dp, 0x3f, sizeof(dp));
    for (int i = 0; i < n; i++)
        dp[1 << i][0] = 0;
    for (int stat = 2; stat < (1 << n); stat++)
        for (int subset = stat - 1; subset; subset = (subset - 1) & stat)
            if ((dist_mask[subset] | stat) == dist_mask[subset])
            {
                int sum = 0, from = subset ^ stat;
                for (int k = 0; k < n; k++)
                    if (from & (1 << k))
                    {
                        int tmp = INF;
                        for (int idx = 0; idx < n; idx++)
                            if (subset & (1 << idx))
                                tmp = min(tmp, dist[idx][k]);
                        sum += tmp;
                    }
                for (int i = 1; i < n; i++)
                    if (dp[subset][i - 1] != INF)
                        dp[stat][i] = min(dp[stat][i], sum * i + dp[subset][i - 1]);
            }
    int ans = INF;
    for (int i = 0; i < n; i++)
        ans = min(ans, dp[(1 << n) - 1][i]);
    printf("%d", ans);
    return 0;
}
```

## F - Lining Up

A score-50-and-<span class="slang" data-zh="退役" data-gloss="tuìyì, “retire from service”. Quitting competitive programming, usually after one's last contest.">retire</span> problem.

First, there is a property: each time someone leaves the formation, it only affects the current column, the current row, and the bottom-right cell. So, ~~as is not hard to think of~~, we maintain a bunch of segment trees recording, for each column, how many people are before a given position, so that we can use the usual value-segment-tree routine to binary search for that position (each time a point is added, adding one makes it shift automatically, very clever). Anything beyond the range just goes into a vector.

Of course, even so, I still can’t do it.

```cpp
// P3960.cpp
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAX_N = 3e5 + 200, SEG = 1e7 + 200;

ll siz[SEG], n, m, q, ptot, upper;
int lson[SEG], rson[SEG], roots[SEG];
vector<ll> vecs[MAX_N];

ll atRank(ll k, int l, int r, int p)
{
    if (l == r)
        return l;
    ll mid = (l + r) >> 1, lsiz = mid - l + 1 - siz[lson[p]];
    if (k <= lsiz)
        return atRank(k, l, mid, lson[p]);
    else
        return atRank(k - lsiz, mid + 1, r, rson[p]);
}

int update(int qx, int l, int r, int p)
{
    if (p == 0)
        p = ++ptot;
    ++siz[p];
    if (l == r)
        return p;
    int mid = (l + r) >> 1;
    if (qx <= mid)
        lson[p] = update(qx, l, mid, lson[p]);
    else
        rson[p] = update(qx, mid + 1, r, rson[p]);
    return p;
}

ll query_col(int x, int y)
{
    ll tmp = atRank(y, 1, upper, roots[x]);
    roots[x] = update(tmp, 1, upper, roots[x]);
    return tmp < m ? 1LL * (x - 1) * m + tmp : vecs[x][tmp - m];
}

ll query_row(int x)
{
    ll tmp = atRank(x, 1, upper, roots[n + 1]);
    roots[n + 1] = update(tmp, 1, upper, roots[n + 1]);
    return tmp <= n ? 1LL * tmp * m : vecs[n + 1][tmp - n - 1];
}

int main()
{
    scanf("%lld%lld%lld", &n, &m, &q);
    upper = max(n, m) + q;
    while (q--)
    {
        int x, y;
        ll tmp;
        scanf("%d%d", &x, &y);
        if (y == m)
            tmp = query_row(x), vecs[n + 1].push_back(tmp), printf("%lld\n", tmp);
        else
        {
            tmp = query_col(x, y), vecs[n + 1].push_back(tmp);
            printf("%lld\n", tmp);
            tmp = query_row(x), vecs[x].push_back(tmp);
        }
    }
    return 0;
}
```
