HANOI DEPARTMENT OF EDUCATION AND TRAINING

OLYMPIC EXAM FOR HIGH SCHOOL STUDENTS

SCHOOL YEAR 2025 – 2026

Subject: INFORMATICS 11

Date: March 14, 2026 | Time: 120 Minutes

EXAM OVERVIEW

No. Problem Name Program File Input File Output File Points
1Chess (Cờ vua)COVUA.*COVUA.INPCOVUA.OUT5.0
2Sorting (Sắp xếp)SAPXEP.*SAPXEP.INPSAPXEP.OUT5.0
3Counting Pairs (Đếm cặp)DEMCAP.*DEMCAP.INPDEMCAP.OUT4.0
4Spring Festival (Hội xuân)HOIXUAN.*HOIXUAN.INPHOIXUAN.OUT4.0
5Shortest Segment (Đoạn con ngắn nhất)DOANCON.*DOANCON.INPDOANCON.OUT2.0

Problem I. Chess (5.0 pts)

An and Binh played $n$ consecutive chess games. There are no draws. Count who won more games.

Input: 6, ABAAAA | Output: >

Problem II. Sorting (5.0 pts)

Rearrange digits of a 4-digit number $N$ to form the largest number divisible by 5. Output -1 if impossible.

Input: 2026 | Output: 6220

Problem III. Counting Pairs (4.0 pts)

Find pairs $(x, y)$ where both are prime, $1 < x < y \le N$, and $y - x = K$.

Input: 20 6 | Output: 4

Problem IV. Spring Festival (4.0 pts)

Find triplets $(i, j, k)$ such that $\max(a_i, a_j, a_k) - \min(a_i, a_j, a_k) \le d$.

Input: 5 3 / 6 1 7 2 4 | Output: 2

Problem V. Shortest Segment (2.0 pts)

Find the shortest subsegment containing at least $k$ elements that have the maximum possible number of divisors in the array.

Input: 8 3 / 6 2 3 8 4 10 9 10 | Output: 5

REFERENCE SOLUTIONS (C++)

Solution 1: Chess (COVUA.cpp)
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
    ifstream fin("COVUA.INP");
    ofstream fout("COVUA.OUT");
    int n;
    string s;
    if (!(fin >> n >> s)) return 0;

    int a = 0, b = 0;
    for (char c : s) {
        if (c == 'A') a++;
        else if (c == 'B') b++;
    }

    if (a > b) fout << ">";
    else if (a < b) fout << "<";
    else fout << "=";

    return 0;
}
Solution 2: Sorting (SAPXEP.cpp)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

int main() {
    ifstream fin("SAPXEP.INP");
    ofstream fout("SAPXEP.OUT");
    string n;
    fin >> n;

    vector<int> digits;
    bool hasFiveOrZero = false;
    for (char c : n) {
        int d = c - '0';
        digits.push_back(d);
        if (d == 0 || d == 5) hasFiveOrZero = true;
    }

    if (!hasFiveOrZero) {
        fout << -1;
        return 0;
    }

    sort(digits.rbegin(), digits.rend());

    int lastDigitIdx = -1;
    for (int i = digits.size() - 1; i >= 0; i--) {
        if (digits[i] == 0 || digits[i] == 5) {
            lastDigitIdx = i;
            break;
        }
    }

    int lastVal = digits[lastDigitIdx];
    digits.erase(digits.begin() + lastDigitIdx);

    for (int d : digits) fout << d;
    fout << lastVal;

    return 0;
}
Solution 3: Counting Pairs (DEMCAP.cpp)
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

const int MAXN = 1000005;
bool is_prime[MAXN];

void sieve() {
    fill(is_prime + 2, is_prime + MAXN, true);
    for (int p = 2; p * p < MAXN; p++) {
        if (is_prime[p]) {
            for (int i = p * p; i < MAXN; i += p)
                is_prime[i] = false;
        }
    }
}

int main() {
    ifstream fin("DEMCAP.INP");
    ofstream fout("DEMCAP.OUT");
    int n, k;
    fin >> n >> k;

    sieve();
    int count = 0;
    for (int x = 2; x <= n - k; x++) {
        if (is_prime[x] && is_prime[x + k]) {
            count++;
        }
    }
    fout << count;
    return 0;
}
Solution 4: Spring Festival (HOIXUAN.cpp)
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

using namespace std;

int main() {
    ifstream fin("HOIXUAN.INP");
    ofstream fout("HOIXUAN.OUT");
    int n;
    long long d;
    if (!(fin >> n >> d)) return 0;

    vector<long long> a(n);
    for (int i = 0; i < n; i++) fin >> a[i];

    sort(a.begin(), a.end());

    long long count = 0;
    int left = 0;
    for (int right = 0; right < n; right++) {
        while (a[right] - a[left] > d) {
            left++;
        }
        long long num_elements = right - left;
        if (num_elements >= 2) {
            count += (num_elements * (num_elements - 1)) / 2;
        }
    }
    fout << count;
    return 0;
}
Solution 5: Shortest Segment (DOANCON.cpp)
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

const int MAX_VAL = 10000001;
int divisor_count[MAX_VAL];

void precompute() {
    for (int i = 1; i < MAX_VAL; i++) {
        for (int j = i; j < MAX_VAL; j += i) {
            divisor_count[j]++;
        }
    }
}

int main() {
    ifstream fin("DOANCON.INP");
    ofstream fout("DOANCON.OUT");

    int n, k;
    if (!(fin >> n >> k)) return 0;

    precompute();

    vector<int> a(n);
    int max_divs = 0;
    for (int i = 0; i < n; i++) {
        fin >> a[i];
        if (divisor_count[a[i]] > max_divs) max_divs = divisor_count[a[i]];
    }

    vector<int> indices;
    for (int i = 0; i < n; i++) {
        if (divisor_count[a[i]] == max_divs) indices.push_back(i);
    }

    if (indices.size() < k) fout << -1;
    else {
        int min_len = 2e9;
        for (int i = 0; i <= (int)indices.size() - k; i++) {
            min_len = min(min_len, indices[i + k - 1] - indices[i] + 1);
        }
        fout << min_len;
    }
    return 0;
}