Codeforces Round 924 (Div. 2 ABCDE题 视频讲解)

A. Rectangle Cutting

Problem Statement

Bob has a rectangle of size a × b a \times b 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 9 0 ∘ 90^{\circ} 90∘ rotation, they are considered the same . For example, the rectangles 6 × 4 6 \times 4 6×4 and 4 × 6 4 \times 6 4×6 are considered the same.

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

However, from the 2 × 1 2 \times 1 2×1 rectangle, another rectangle cannot be formed, because it can only be cut into two rectangles of 1 × 1 1 \times 1 1×1, and from these, only the 1 × 2 1 \times 2 1×2 and 2 × 1 2 \times 1 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 t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) --- 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 a a and b b b ( 1 ≤ a , b ≤ 1 0 9 1 \le a, b \le 10^9 1≤a,b≤109) --- the size of Bob's rectangle.

Output

For each test case, output "Yes" if Bob can obtain another rectangle from the a × b a \times b 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 1 \times 1 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 3 \times 2 3×2 rectangle can be cut into two 3 × 1 3 \times 1 3×1 rectangles, and from these, the 1 × 6 1 \times 6 1×6 rectangle can be formed.

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

Solution

具体见文后视频。


Code

cpp 复制代码
#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 † ^{\dagger} † to arrays and finding the most frequently occurring element. Recently, he found an array a a a and decided to find out the maximum number of elements equal to the same number in the array a a a that he can obtain after adding some permutation to the array a a a.

More formally, Vasya must choose exactly one permutation p 1 , p 2 , p 3 , ... , p n p_1, p_2, p_3, \ldots, p_n p1,p2,p3,...,pn of length n n n, and then change the elements of the array a a a according to the rule a i : = a i + p i a_i := a_i + p_i ai:=ai+pi. After that, Vasya counts how many times each number occurs in the array a a a and takes the maximum of these values. You need to determine the maximum value he can obtain.

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

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \leq t \leq 2 \cdot 10^4 1≤t≤2⋅104) --- 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 n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105) --- the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , ... , a n a_1, a_2, \ldots, a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1≤ai≤109) --- the elements of the array a a a.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.

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 ] p = [2, 1] p=[2,1]. Then after applying the operation, the array a a a will be [ 3 , 3 ] [3, 3] [3,3], in which the number 3 3 3 occurs twice, so the answer is 2 2 2.

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

Solution

具体见文后视频。


Code

cpp 复制代码
#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 k k-th" position.

As is known, settling in "the first-- k k k-th" position occurs as follows: the first k k k people have numbers 1 , 2 , 3 , ... , k 1, 2, 3, \ldots, k 1,2,3,...,k, the next k − 2 k - 2 k−2 people have numbers k − 1 , k − 2 , ... , 2 k - 1, k - 2, \ldots, 2 k−1,k−2,...,2, the next k k k people have numbers 1 , 2 , 3 , ... , k 1, 2, 3, \ldots, k 1,2,3,...,k, and so on. Thus, the settling repeats every 2 k − 2 2k - 2 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 k 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 k k fit under the given constraints.

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

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \leq t \leq 100 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 n n and x x x ( 1 ≤ x < n ≤ 1 0 9 1 \le x < n \le 10^9 1≤x<n≤109) --- 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 k 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 k k equals 2 , 3 , 5 , 6 2, 3, 5, 6 2,3,5,6 are suitable.

An example of settling for these k k k:

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

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

Solution

具体见文后视频。


Code

cpp 复制代码
#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 b 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 k k squads is reduced by ( k − 1 ) ⋅ x (k - 1) \cdot x (k−1)⋅x units. Note that the army always consists of at least one squad.

It is known that there are n n n races in Middle-earth, and the number of creatures of the i i i-th race is equal to c i c_i ci. 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 t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \le t \le 2 \cdot 10^4 1≤t≤2⋅104) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers n n n, b b b, and x x x ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105, 1 ≤ b ≤ 1 0 6 , 0 ≤ x ≤ 1 0 9 1 \le b \le 10^6, 0 \le x \le 10^9 1≤b≤106,0≤x≤109) --- the number of races and the constants b b b and x x x described above.

The second line of each test case contains n n n integers c 1 , c 2 , ... , c n c_1, c_2, \ldots, c_n c1,c2,...,cn ( 1 ≤ c i ≤ 2 ⋅ 1 0 5 1 \le c_i \le 2 \cdot 10^5 1≤ci≤2⋅105) --- the number of creatures of each of the n n n races.

It is guaranteed that the sum of the values c 1 + c 2 + ... + c n c_1 + c_2 + \ldots + c_n c1+c2+...+cn over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.

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 3 3 squads. Since x = 0 x = 0 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 b = 1 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 3 \cdot b = 3 3⋅b=3, as they form three pairs in different squads.

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

In the second test case, the inhabitants of Middle-earth can form 3 3 3 squads. Since x = 10 x = 10 x=10, the army's strength will decrease by 20 20 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 b = 5 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 8 \cdot b = 40 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 3 \cdot b = 15 3⋅b=15, as they form three pairs in different squads.

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

Solution

具体见文后视频。

Code

cpp 复制代码
#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 x x and y y y. A sequence a a a of length n n n is called modular if a 1 = x a_1=x a1=x, and for all 1 < i ≤ n 1 < i \le n 1<i≤n the value of a i a_{i} ai is either a i − 1 + y a_{i-1} + y ai−1+y or a i − 1   m o d   y a_{i-1} \bmod y ai−1mody. Here x   m o d   y x \bmod y xmody denotes the remainder from dividing x x x by y y y.

Determine if there exists a modular sequence of length n n n with the sum of its elements equal to S S 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 t t ( 1 ≤ t ≤ 2 ⋅ 1 0 4 1 \le t \le 2 \cdot 10^4 1≤t≤2⋅104). The description of the test cases follows.

The first and only line of each test case contains four integers n n n, x x x, y y y, and s s s ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105, 0 ≤ x ≤ 2 ⋅ 1 0 5 0 \le x \le 2 \cdot 10^5 0≤x≤2⋅105, 1 ≤ y ≤ 2 ⋅ 1 0 5 1 \le y \le 2 \cdot 10^5 1≤y≤2⋅105, 0 ≤ s ≤ 2 ⋅ 1 0 5 0 \le s \le 2 \cdot 10^5 0≤s≤2⋅105) --- the length of the sequence, the parameters x x x and y y y, and the required sum of the sequence elements.

The sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105, and also the sum of s s s over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.

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 n n integers a 1 , a 2 , ... , a n a_1, a_2, \ldots, a_n a1,a2,...,an separated by a space --- the elements of the sequence a a 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 ] [8, 11, 2, 5, 2] [8,11,2,5,2] satisfies the conditions. Thus, a 1 = 8 = x a_1 = 8 = x a1=8=x, a 2 = 11 = a 1 + 3 a_2 = 11 = a_1 + 3 a2=11=a1+3, a 3 = 2 = a 2   m o d   3 a_3 = 2 = a_2 \bmod 3 a3=2=a2mod3, a 4 = 5 = a 3 + 3 a_4 = 5 = a_3 + 3 a4=5=a3+3, a 5 = 2 = a 4   m o d   3 a_5 = 2 = a_4 \bmod 3 a5=2=a4mod3.

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

Solution

具体见文后视频。


Code

cpp 复制代码
#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)


最后祝大家早日

相关推荐
penguin_bark19 分钟前
69. x 的平方根
算法
FL162386312922 分钟前
[C++]使用纯opencv部署yolov11-pose姿态估计onnx模型
c++·opencv·yolo
sukalot26 分钟前
windows C++-使用任务和 XML HTTP 请求进行连接(一)
c++·windows
这可就有点麻烦了29 分钟前
强化学习笔记之【TD3算法】
linux·笔记·算法·机器学习
苏宸啊35 分钟前
顺序表及其代码实现
数据结构·算法
lin zaixi()38 分钟前
贪心思想之——最大子段和问题
数据结构·算法
FindYou.38 分钟前
C - Separated Lunch
算法·深度优先
ぃ扶摇ぅ43 分钟前
Windows系统编程(三)进程与线程二
c++·windows
夜雨翦春韭44 分钟前
【代码随想录Day30】贪心算法Part04
java·数据结构·算法·leetcode·贪心算法
Kent_J_Truman1 小时前
【平方差 / C】
算法