## A. Rectangle Cutting

### Problem Statement

Bob has a rectangle of size $a×b$. He tries to cut this rectangle into two rectangles with integer sides by making a cut parallel to one of the sides of the original rectangle. Then Bob tries to form some **other** rectangle from the two resulting rectangles, and he can rotate and move these two rectangles as he wishes.

Note that if two rectangles differ only by a $90_{∘}$ rotation, they are considered **the same**. For example, the rectangles $6×4$ and $4×6$ are considered the same.

Thus, from the $2×6$ rectangle, another rectangle can be formed, because it can be cut into two $2×3$ rectangles, and then these two rectangles can be used to form the $4×3$ rectangle, which is different from the $2×6$ rectangle.

However, from the $2×1$ rectangle, another rectangle cannot be formed, because it can only be cut into two rectangles of $1×1$, and from these, only the $1×2$ and $2×1$ rectangles can be formed, which are considered the same.

Help Bob determine if he can obtain some other rectangle, or if he is just wasting his time.

### Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1≤t≤10_{4}$) — the number of test cases. This is followed by the description of the test cases.

The single line of each test case contains two integers $a$ and $b$ ($1≤a,b≤10_{9}$) — the size of Bob’s rectangle.

### Output

For each test case, output “Yes” if Bob can obtain another rectangle from the $a×b$ rectangle. Otherwise, output “No”.

You can output the answer in any case (upper or lower). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as positive answers.

### Example

Example

input |
---|

7 |

1 1 |

2 1 |

2 6 |

3 2 |

2 2 |

2 4 |

6 3 |

output |
---|

No |

No |

Yes |

Yes |

Yes |

Yes |

No |

**Note**

In the first test case, the $1×1$ rectangle cannot be cut into two rectangles, so another rectangle cannot be obtained from it.

In the fourth test case, the $3×2$ rectangle can be cut into two $3×1$ rectangles, and from these, the $1×6$ rectangle can be formed.

In the fifth test case, the $2×2$ rectangle can be cut into two $1×2$ rectangles, and from these, the $1×4$ rectangle can be formed.

### Solution

具体见文后视频。

### Code

```
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
bool Check(int A1, int B1, int A2, int B2) { return (A1 == A2 && B1 == B2) || (A1 == B2 && B1 == A2); }
void solve()
{
int A, B;
cin >> A >> B;
if ((A & 1) && (B & 1)) cout << "No" << endl;
else
{
if ((A % 2 == 0) && !Check(A, B, A / 2, B * 2)) cout << "Yes" << endl;
else if ((B % 2 == 0) && !Check(A, B, A * 2, B / 2)) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int Data;
cin >> Data;
while (Data --)
solve();
return 0;
}
```

## B. Equalize

### Problem Statement

Vasya has two hobbies — adding permutations$_{†}$ to arrays and finding the most frequently occurring element. Recently, he found an array $a$ and decided to find out the maximum number of elements equal to the same number in the array $a$ that he can obtain after adding some permutation to the array $a$.

More formally, Vasya must choose exactly one permutation $p_{1},p_{2},p_{3},…,p_{n}$ of length $n$, and then change the elements of the array $a$ according to the rule $a_{i}:=a_{i}+p_{i}$. After that, Vasya counts how many times each number occurs in the array $a$ and takes the maximum of these values. You need to determine the maximum value he can obtain.

$_{†}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).

### Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1≤t≤2⋅10_{4}$) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer $n$ ($1≤n≤2⋅10_{5}$) — the length of the array $a$.

The second line of each test case contains $n$ integers $a_{1},a_{2},…,a_{n}$ ($1≤a_{i}≤10_{9}$) — the elements of the array $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2⋅10_{5}$.

### Output

For each test case, output a single number — the maximum number of elements equal to the same number after the operation of adding a permutation.

### Example

Example

input |
---|

7 |

2 |

1 2 |

4 |

7 1 4 1 |

3 |

103 102 104 |

5 |

1 101 1 100 1 |

5 |

1 10 100 1000 1 |

2 |

3 1 |

3 |

1000000000 999999997 999999999 |

output |
---|

2 |

2 |

3 |

2 |

1 |

1 |

2 |

**Note**

In the first test case, it is optimal to choose $p=[2,1]$. Then after applying the operation, the array $a$ will be $[3,3]$, in which the number $3$ occurs twice, so the answer is $2$.

In the second test case, one of the optimal options is $p=[2,3,1,4]$. After applying the operation, the array $a$ will be $[9,4,5,5]$. Since the number $5$ occurs twice, the answer is $2$.

### Solution

具体见文后视频。

### Code

```
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int SIZE = 2e5 + 10;
int N;
int A[SIZE], B[SIZE];
void solve()
{
cin >> N;
for (int i = 1; i <= N; i ++)
cin >> A[i];
sort(A + 1, A + 1 + N);
int M = 0;
for (int i = 2; i <= N; i ++)
if (A[i] - A[i - 1] != 0)
B[ ++ M] = A[i] - A[i - 1], B[M] += B[M - 1];
int Result = 1;
for (int i = 1; i <= M; i ++)
{
int j = lower_bound(B, B + M + 1, B[i] - N + 1) - B;
Result = max(Result, i - j + 1);
}
cout << Result << endl;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int Data;
cin >> Data;
while (Data --)
solve();
return 0;
}
```

## C. Physical Education Lesson

### Problem Statement

In a well-known school, a physical education lesson took place. As usual, everyone was lined up and asked to settle in “the first–$k$-th” position.

As is known, settling in “the first–$k$-th” position occurs as follows: the first $k$ people have numbers $1,2,3,…,k$, the next $k−2$ people have numbers $k−1,k−2,…,2$, the next $k$ people have numbers $1,2,3,…,k$, and so on. Thus, the settling repeats every $2k−2$ positions. Examples of settling are given in the “Note” section.

The boy Vasya constantly forgets everything. For example, he forgot the number $k$ described above. But he remembers the position he occupied in the line, as well as the number he received during the settling. Help Vasya understand how many natural numbers $k$ fit under the given constraints.

Note that the settling exists if and only if $k>1$. In particular, this means that the settling **does not exist** for $k=1$.

### Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1≤t≤100$) — the number of test cases. This is followed by the description of the test cases.

The only line of each test case contains two integers $n$ and $x$ ($1≤x<n≤10_{9}$) — Vasya’s position in the line and the number Vasya received during the settling.

### Output

For each test case, output a single integer — the number of different $k$ that fit under the given constraints.

It can be proven that under the given constraints, the answer is finite.

### Example

Example

input |
---|

5 |

10 2 |

3 1 |

76 4 |

100 99 |

1000000000 500000000 |

output |
---|

4 |

1 |

9 |

0 |

1 |

**Note**

In the first test case, $k$ equals $2,3,5,6$ are suitable.

An example of settling for these $k$:

$k$ / № | $1$ | $2$ | $3$ | $4$ | $5$ | $6$ | $7$ | $8$ | $9$ | $10$ |
---|---|---|---|---|---|---|---|---|---|---|

$2$ | $1$ | $2$ | $1$ | $2$ | $1$ | $2$ | $1$ | $2$ | $1$ | $2$ |

$3$ | $1$ | $2$ | $3$ | $2$ | $1$ | $2$ | $3$ | $2$ | $1$ | $2$ |

$5$ | $1$ | $2$ | $3$ | $4$ | $5$ | $4$ | $3$ | $2$ | $1$ | $2$ |

$6$ | $1$ | $2$ | $3$ | $4$ | $5$ | $6$ | $5$ | $4$ | $3$ | $2$ |

In the second test case, $k=2$ is suitable.

### Solution

具体见文后视频。

### Code

```
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
void solve()
{
int N, X;
cin >> N >> X;
std::vector<int> F1, F2;
for (int i = 1; i <= (N - X) / i; i ++)
if ((N - X) % i == 0)
{
F1.push_back(i);
if ((N - X) / i != i) F1.push_back((N - X) / i);
}
for (int i = 1; i <= (N + X - 2) / i; i ++)
if ((N + X - 2) % i == 0)
{
F2.push_back(i);
if ((N + X - 2) / i != i) F2.push_back((N + X - 2) / i);
}
set<int> Result;
for (auto c : F1)
{
if (c & 1) continue;
int K = c + 2 >> 1;
if (K >= X) Result.insert(K);
}
for (auto c : F2)
{
if (c & 1) continue;
int K = c + 2 >> 1;
if (X >= 2 && X < K) Result.insert(K);
}
cout << Result.size() << endl;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int Data;
cin >> Data;
while (Data --)
solve();
return 0;
}
```

## D. Lonely Mountain Dungeons

### Problem Statement

Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.

The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of **the same race**, which are in different squads, adds $b$ units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of $k$ squads is reduced by $(k−1)⋅x$ units. Note that the army always consists **of at least one squad**.

It is known that there are $n$ races in Middle-earth, and the number of creatures of the $i$-th race is equal to $c_{i}$. Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble.

### Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1≤t≤2⋅10_{4}$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers $n$, $b$, and $x$ ($1≤n≤2⋅10_{5}$, $1≤b≤10_{6},0≤x≤10_{9}$) — the number of races and the constants $b$ and $x$ described above.

The second line of each test case contains $n$ integers $c_{1},c_{2},…,c_{n}$ ($1≤c_{i}≤2⋅10_{5}$) — the number of creatures of each of the $n$ races.

It is guaranteed that the sum of the values $c_{1}+c_{2}+…+c_{n}$ over all test cases does not exceed $2⋅10_{5}$.

### Output

For each test case, output a single integer — the maximum strength of the army that the inhabitants of Middle-earth can assemble.

### Example

Example

input |
---|

5 |

3 1 0 |

1 2 3 |

3 5 10 |

2 5 3 |

4 3 3 |

3 2 1 2 |

4 1 0 |

4 1 4 2 |

4 1 10 |

4 1 4 2 |

output |
---|

4 |

40 |

9 |

13 |

0 |

**Note**

In the first test case, the inhabitants of Middle-earth can form $3$ squads. Since $x=0$, the army’s strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows:

- The single representative of the first species can be sent to the first squad.
- The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by $b=1$.
- The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3⋅b=3$, as they form three pairs in different squads.

Thus, the total strength of the army is $4$.

In the second test case, the inhabitants of Middle-earth can form $3$ squads. Since $x=10$, the army’s strength will decrease by $20$. The inhabitants can be distributed among the squads as follows:

- The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by $b=5$.
- The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $8⋅b=40$.
- The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3⋅b=15$, as they form three pairs in different squads.

Thus, the total strength of the army is $5+40+15−20=40$.

### Solution

具体见文后视频。

### Code

```
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int SIZE = 2e5 + 10;
int N, B, X;
int C[SIZE], D[SIZE];
int F(int K)
{
int Result = 0;
for (int i = 1; i <= N; i ++)
{
if (C[i] <= K) Result += C[i] * (C[i] - 1) / 2 * B;
else
{
int Sum = 0;
for (int j = 1; j <= K; j ++)
D[j] = (C[i] - Sum + K - j) / (K - j + 1), Sum += D[j];
Sum = 0;
for (int j = 1; j <= K; j ++)
Result += D[j] * (C[i] - Sum - D[j]) * B, Sum += D[j];
}
}
Result -= (K - 1) * X;
return Result;
}
void solve()
{
cin >> N >> B >> X;
int Sum = 0;
for (int i = 1; i <= N; i ++)
cin >> C[i], Sum += C[i];
int l = 1, r = Sum;
while(l < r) {
int lmid = l + (r - l) / 3;
int rmid = r - (r - l) / 3;
if(F(lmid) <= F(rmid)) l = lmid + 1;
else r = rmid - 1;
}
printf("%lld\n", max(F(l), F(r)));
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int Data;
cin >> Data;
while (Data --)
solve();
return 0;
}
```

## E. Modular Sequence

### Problem Statement

You are given two integers $x$ and $y$. A sequence $a$ of length $n$ is called modular if $a_{1}=x$, and for all $1<i≤n$ the value of $a_{i}$ is either $a_{i−1}+y$ or $a_{i−1}mody$. Here $xmody$ denotes the remainder from dividing $x$ by $y$.

Determine if there exists a modular sequence of length $n$ with the sum of its elements equal to $S$, and if it exists, find any such sequence.

### Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1≤t≤2⋅10_{4}$). The description of the test cases follows.

The first and only line of each test case contains four integers $n$, $x$, $y$, and $s$ ($1≤n≤2⋅10_{5}$, $0≤x≤2⋅10_{5}$, $1≤y≤2⋅10_{5}$, $0≤s≤2⋅10_{5}$) — the length of the sequence, the parameters $x$ and $y$, and the required sum of the sequence elements.

The sum of $n$ over all test cases does not exceed $2⋅10_{5}$, and also the sum of $s$ over all test cases does not exceed $2⋅10_{5}$.

### Output

For each test case, if the desired sequence exists, output “Yes” on the first line (without quotes). Then, on the second line, output $n$ integers $a_{1},a_{2},…,a_{n}$ separated by a space — the elements of the sequence $a$. If there are multiple suitable sequences, output any of them.

If the sequence does not exist, output “No” on a single line.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be accepted as a positive answer.

### Example

Example

input |
---|

3 |

5 8 3 28 |

3 5 3 6 |

9 1 5 79 |

output |
---|

YES |

8 11 2 2 5 |

NO |

NO |

**Note**

In the first example, the sequence $[8,11,2,5,2]$ satisfies the conditions. Thus, $a_{1}=8=x$, $a_{2}=11=a_{1}+3$, $a_{3}=2=a_{2}mod3$, $a_{4}=5=a_{3}+3$, $a_{5}=2=a_{4}mod3$.

In the second example, the first element of the sequence should be equal to $5$, so the sequence $[2,2,2]$ is not suitable.

### Solution

具体见文后视频。

### Code

```
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int SIZE = 4e5 + 10;
int N, X, Y, S;
int F[SIZE], A[SIZE];
void solve()
{
cin >> N >> X >> Y >> S;
if (S < X + (N - 1) * (X % Y) || (S - N * (X % Y)) % Y != 0)
{
cout << "NO" << endl;
return;
}
S -= N * (X % Y), S /= Y;
for (int i = 0; i <= S; i ++)
F[i] = 1e18;
F[0] = 0;
for (int i = 1; ; i ++)
{
int V = i * (i + 1) / 2;
if (V > S) break;
for (int j = 0; j <= S; j ++)
F[j + V] = min(F[j + V], F[j] + i + 1);
}
int Base = X / Y, Pos = -1;
for (int i = 1; i <= N; i ++)
if (S >= Base && F[S - Base] <= N - i)
{
Pos = i;
break;
}
else
S -= Base, Base ++;
if (Pos == -1)
{
cout << "NO" << endl;
return;
}
A[1] = X / Y, S -= Base;
for (int i = 2; i <= Pos; i ++)
A[i] = A[i - 1] + 1;
for (int i = Pos + 1; i <= N; i ++)
A[i] = 0;
while (S)
for (int i = 1; ; i ++)
{
int V = i * (i + 1) / 2;
if (V > S) break;
if (F[S - V] + i + 1 == F[S])
{
for (int j = 0; j <= i; j ++)
A[ ++ Pos] = j;
S -= V;
break;
}
}
cout << "YES" << endl;
for (int i = 1; i <= N; i ++)
cout << A[i] * Y + (X % Y) << " ";
cout << endl;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int Data;
cin >> Data;
while (Data --)
solve();
return 0;
}
```

## 视频题解

Codeforces Round 924 (Div. 2)（CF924 之 A ~ E）

**最后祝大家早日**