ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 2025. Line Fighting

Problem Editorial (written by a beginner)
Posted by Adham Essam 18 Oct 2025 15:00
First of all, we need to understand which number of teams x (from 2 to k) we need to pick to distribute our fighters into.

Through intuition we might get the idea that the bigger the K **the more the fights** (which is what is required).
It can be proven that our intuition is correct; the more you divide n the better the result, therefore we will need to divide n by k.
Proof: assuming we have n = 3, now let us assume we will distribute them to 1 team where the first team is of 3 fighters, there will be 0 fights, let us now assume we will distribute them to 2 teams where the first is of 2 and second is of 1 fighters, there will be 2 fights, lastly let us assume that we will distribute them to 3 teams where each team has a fighter, there will be 3 fights, we can then deduce that for each fighter you divide into a new group more fights are formed.
Conclusion: It is always the best choice to divide by the biggest possible number you can divide by, thus we will always divide by K.


To solve the problem we can then deduce that there will always be two cases:
Case 1 (k divides n): here we simply divide fighters into k groups, each group (composed of (n/k) fighters) will fight some other group (composed of (n/k) fighters) thus the equation for the first case is simply:
(k * (k - 1) / 2) * (n / k)^2

Case 2 (k doesn't divide n): here we notice that n will be composed of x count of floor(n / k) and y count of ceil(n / k) meaning floor(n/k)*y + ceil(n/k)*z = n, and that obviously, x + y = k, through these equations we can deduce algebraically the values of x and y, and from it form our equation.
Our equation will have x groups of floor (n / k) fighters which within them will form a specific number of fights, we will also have y groups of ceil (n / k) fighters which within them will form a specific number of fights, then we will also need an to find an equation that calculates the number of fights that happens between the x groups and the y groups, through observation and deduction you will find that this will be the equation of the second case:
((x * (x - 1) / 2) * (floor(n/k))^2) +
((y * (y - 1) / 2) * (ceil(n/k))^2) +
(floor(n/k) * ceil(n/k)) * x * y



Code:
```
#include <bits/stdc++.h>

using i64 = long long;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int t;    std::cin >> t;
    while (t--) {
        int n, k;    std::cin >> n >> k;
        if (n % k == 0) {
            int count = k * (k - 1) / 2;
            std::cout << count * (n / k) * (n / k) << '\n';
        } else {
            int x = n - ((n + k - 1) / k) * k;
            x = x / ((n / k) - ((n + k - 1) / k));
            int y = k - x;

            int part1 = (x * (x - 1) / 2) * (n / k) * (n / k);
            int part2 = (y * (y - 1) / 2) * ((n + k - 1) / k) * ((n + k - 1) / k);
            int part3 = (n / k) * ((n + k - 1) / k) * x * y;
            std::cout << part1 + part2 + part3 << '\n';
        }
    }

    return 0;
}
```

Edited by author 18.10.2025 15:04

Edited by author 18.10.2025 15:06
Re: Problem Editorial (written by a beginner)
Posted by Adham Essam 18 Oct 2025 15:03
Thank you for reading my editorial.

Edited by author 18.10.2025 15:05