Codeforces Round 943 (Div. 3 ABCDEFG1G2题) 视频讲解

news2024/12/30 4:11:28

A. Maximize?

Problem Statement

You are given an integer x x x. Your task is to find any integer y y y ( 1 ≤ y < x ) (1\le y<x) (1y<x) such that gcd ⁡ ( x , y ) + y \gcd(x,y)+y gcd(x,y)+y is maximum possible.

Note that if there is more than one y y y which satisfies the statement, you are allowed to find any.

gcd ⁡ ( a , b ) \gcd(a,b) gcd(a,b) is the Greatest Common Divisor of a a a and b b b. For example, gcd ⁡ ( 6 , 4 ) = 2 \gcd(6,4)=2 gcd(6,4)=2.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases.

Each of the following t t t lines contains a single integer x x x ( 2 ≤ x ≤ 1000 2 \le x \le 1000 2x1000).

Output

For each test case, output any y y y ( 1 ≤ y < x 1 \le y < x 1y<x), which satisfies the statement.

Example

Example

input
7
10
7
21
100
2
1000
6
output
5
6
18
98
1
750
3

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve() {
	int x;
	cin >> x;

	int mx = 0, p;
	for (int y = 1; y < x; y ++) {
		if (__gcd(x, y) + y > mx) {
			mx = __gcd(x, y) + y, p = y;
		}
	}
	cout << p << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

B. Prefiquence

Problem Statement

You are given two binary strings a a a and b b b. A binary string is a string consisting of the characters ‘0’ and ‘1’.

Your task is to determine the maximum possible number k k k such that a prefix of string a a a of length k k k is a subsequence of string b b b.

A sequence a a a is a subsequence of a sequence b b b if a a a can be obtained from b b b by the deletion of several (possibly, zero or all) elements.

Input

The first line consists of a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n , m ≤ 2 ⋅ 1 0 5 1\le n,m \le 2 \cdot 10^5 1n,m2105) — the length of string a a a and the length of string b b b, respectively.

The second line of each test case contains a binary string a a a of length n n n.

The third line of each test case contains a binary string b b b of length m m m.

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

Output

For each test case, output a single number — the maximum k k k, such that the first k k k characters of a a a form a subsequence of b b b.

Example

Example

input
6
5 4
10011
1110
3 3
100
110
1 3
1
111
4 4
1011
1111
3 5
100
11010
3 1
100
0
output
2
2
1
1
3
0

Note

In the first example, the string ‘ 10 10 10’ is a subsequence of ‘ 1 11 0 1\color{red}11\color{red}0 1110’ but the string ‘ 100 100 100’ is not. So the answer is 2 2 2.

In the fifth example, a a a=‘ 100 100 100’, b b b=‘ 1 101 0 1\color{red}{10}1\color{red}0 11010’, whole string a a a is a subsequence of string b b b. So the answer is 3 3 3.

In the sixth example, string b b b does not contain ‘ 1 1 1’ so the answer is 0 0 0.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

int n, m;
string a, b;

bool check(int x) {
	for (int i = 1, j = 0; i <= m; i ++) {
		if (a[j + 1] == b[i])
			j ++;
		if (j == x)
			return 1;
	}
	return 0;
}

void solve() {
	cin >> n >> m >> a >> b;
	a = ' ' + a, b = ' ' + b;

	int l = 0, r = min(n, m);
	while (l < r) {
		int mid = l + r + 1 >> 1;
		if (check(mid)) l = mid;
		else r = mid - 1;
	}
	cout << l << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

C. Assembly via Remainders

Problem Statement

You are given an array x 2 , x 3 , … , x n x_2,x_3,\dots,x_n x2,x3,,xn. Your task is to find any array a 1 , … , a n a_1,\dots,a_n a1,,an, where:

  • 1 ≤ a i ≤ 1 0 9 1\le a_i\le 10^9 1ai109 for all 1 ≤ i ≤ n 1\le i\le n 1in.
  • x i = a i   m o d   a i − 1 x_i=a_i \bmod a_{i-1} xi=aimodai1 for all 2 ≤ i ≤ n 2\le i\le n 2in.

Here c   m o d   d c\bmod d cmodd denotes the remainder of the division of the integer c c c by the integer d d d. For example 5   m o d   2 = 1 5 \bmod 2 = 1 5mod2=1, 72   m o d   3 = 0 72 \bmod 3 = 0 72mod3=0, 143   m o d   14 = 3 143 \bmod 14 = 3 143mod14=3.

Note that if there is more than one a a a which satisfies the statement, you are allowed to find any.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 ) (1\le t\le 10^4) (1t104) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 500 ) (2\le n\le 500) (2n500) — the number of elements in a a a.

The second line of each test case contains n − 1 n-1 n1 integers x 2 , … , x n x_2,\dots,x_n x2,,xn ( 1 ≤ x i ≤ 500 ) (1\le x_i\le 500) (1xi500) — the elements of x x x.

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

Output

For each test case output any a 1 , … , a n a_1,\dots,a_n a1,,an ( 1 ≤ a i ≤ 1 0 9 1 \le a_i \le 10^9 1ai109) which satisfies the statement.

Example

input
5
4
2 4 1
3
1 1
6
4 2 5 1 2
2
500
3
1 5
output
3 5 4 9
2 5 11
5 14 16 5 11 24
501 500
2 7 5

Note

In the first test case a = [ 3 , 5 , 4 , 9 ] a=[3,5,4,9] a=[3,5,4,9] satisfies the conditions, because:

  • a 2   m o d   a 1 = 5   m o d   3 = 2 = x 2 a_2\bmod a_1=5\bmod 3=2=x_2 a2moda1=5mod3=2=x2;
  • a 3   m o d   a 2 = 4   m o d   5 = 4 = x 3 a_3\bmod a_2=4\bmod 5=4=x_3 a3moda2=4mod5=4=x3;
  • a 4   m o d   a 3 = 9   m o d   4 = 1 = x 4 a_4\bmod a_3=9\bmod 4=1=x_4 a4moda3=9mod4=1=x4;

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

void solve() {
	int n;
	cin >> n;

	std::vector<int> a(n + 1);
	for (int i = 2; i <= n; i ++)
		cin >> a[i];
	cout << 501 << " ";
	int lst = 501;
	for (int i = 2; i <= n; i ++)
		cout << lst + a[i] << " ", lst += a[i];
	cout << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

D. Permutation Game

Problem Statement

Bodya and Sasha found a permutation p 1 , … , p n p_1,\dots,p_n p1,,pn and an array a 1 , … , a n a_1,\dots,a_n a1,,an. They decided to play a well-known “Permutation game”.

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).

Both of them chose a starting position in the permutation.

The game lasts k k k turns. The players make moves simultaneously. On each turn, two things happen to each player:

  • If the current position of the player is x x x, his score increases by a x a_x ax.
  • Then the player either stays at his current position x x x or moves from x x x to p x p_x px.

The winner of the game is the player with the higher score after exactly k k k turns.

Knowing Bodya’s starting position P B P_B PB and Sasha’s starting position P S P_S PS, determine who wins the game if both players are trying to win.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of testcases.

The first line of each testcase contains integers n n n, k k k, P B P_B PB, P S P_S PS ( 1 ≤ P B , P S ≤ n ≤ 2 ⋅ 1 0 5 1\le P_B,P_S\le n\le 2\cdot 10^5 1PB,PSn2105, 1 ≤ k ≤ 1 0 9 1\le k\le 10^9 1k109) — length of the permutation, duration of the game, starting positions respectively.

The next line contains n n n integers p 1 , … , p n p_1,\dots,p_n p1,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — elements of the permutation p p p.

The next line contains n n n integers a 1 , … , a n a_1,\dots,a_n a1,,an ( 1 ≤ a i ≤ 1 0 9 1\le a_i\le 10^9 1ai109) — elements of array a a a.

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

Output

For each testcase output:

  • “Bodya” if Bodya wins the game.
  • “Sasha” if Sasha wins the game.
  • “Draw” if the players have the same score.

Example

Example

input
10
4 2 3 2
4 1 2 3
7 2 5 6
10 8 2 10
3 1 4 5 2 7 8 10 6 9
5 10 5 1 3 7 10 15 4 3
2 1000000000 1 2
1 2
4 4
8 10 4 1
5 1 4 3 2 8 6 7
1 1 2 1 2 100 101 102
5 1 2 5
1 2 4 5 3
4 6 9 4 2
4 2 3 1
4 1 3 2
6 8 5 3
6 9 5 4
6 1 3 5 2 4
6 9 8 9 5 10
4 8 4 2
2 3 4 1
5 2 8 7
4 2 3 1
4 1 3 2
6 8 5 3
2 1000000000 1 2
1 2
1000000000 2
output
Bodya
Sasha
Draw
Draw
Bodya
Sasha
Sasha
Sasha
Sasha
Bodya

Note

Below you can find the explanation for the first testcase, where the game consists of k = 2 k=2 k=2 turns.

TurnBodya’s positionBodya’s scoreBodya’s moveSasha’s positionSasha’s scoreSasha’s move
first 3 3 3 0 + a 3 = 0 + 5 = 5 0 + a_3 = 0 + 5 = 5 0+a3=0+5=5stays on the same position 2 2 2 0 + a 2 = 0 + 2 = 2 0 + a_2 = 0 + 2 = 2 0+a2=0+2=2moves to p 2 = 1 p_2=1 p2=1
second 3 3 3 5 + a 3 = 5 + 5 = 10 5 + a_3 = 5 + 5 = 10 5+a3=5+5=10stays on the same position 1 1 1 2 + a 1 = 2 + 7 = 9 2 + a_1 = 2 + 7 = 9 2+a1=2+7=9stays on the same position
final results 3 3 3 10 10 10 1 1 1 9 9 9

As we may see, Bodya’s score is greater, so he wins the game. It can be shown that Bodya always can win this game.

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2e5 + 10;

int n, k, pb, ps;
int a[N], p[N];
int vis[N];

void solve() {
	cin >> n >> k >> pb >> ps;

	for (int i = 1; i <= n; i ++)
		cin >> p[i];
	for (int i = 1; i <= n; i ++)
		cin >> a[i];

	int r1 = 0, tot = 0, r2 = 0, left = k;
	for (int i = 1; i <= n; i ++) vis[i] = 0;
	while (!vis[pb] && left) {
		vis[pb] = 1, tot += a[pb], left --;
		r1 = max(tot + left * a[pb], r1), pb = p[pb];
	}
	for (int i = 1; i <= n; i ++) vis[i] = 0;
	left = k, tot = 0;
	while (!vis[ps] && left) {
		vis[ps] = 1, tot += a[ps], left --;
		r2 = max(tot + left * a[ps], r2), ps = p[ps];
	}

	// cout << r1 << " " << r2 << endl;
	if (r1 == r2) cout << "Draw" << endl;
	else if (r1 > r2) cout << "Bodya" << endl;
	else cout << "Sasha" << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

E. Cells Arrangement

Problem Statement

You are given an integer n n n. You choose n n n cells ( x 1 , y 1 ) , ( x 2 , y 2 ) , … , ( x n , y n ) (x_1,y_1), (x_2,y_2),\dots,(x_n,y_n) (x1,y1),(x2,y2),,(xn,yn) in the grid n × n n\times n n×n where 1 ≤ x i ≤ n 1\le x_i\le n 1xin and 1 ≤ y i ≤ n 1\le y_i\le n 1yin.

Let H \mathcal{H} H be the set of distinct Manhattan distances between any pair of cells. Your task is to maximize the size of H \mathcal{H} H. Examples of sets and their construction are given in the notes.

If there exists more than one solution, you are allowed to output any.

Manhattan distance between cells ( x 1 , y 1 ) (x_1,y_1) (x1,y1) and ( x 2 , y 2 ) (x_2,y_2) (x2,y2) equals ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ |x_1-x_2|+|y_1-y_2| x1x2+y1y2.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 50 1\le t\le 50 1t50) — the number of test cases.

Each of the following t t t lines contains a single integer n n n ( 2 ≤ n ≤ 1 0 3 2\le n\le 10^3 2n103).

Output

For each test case, output n n n points which maximize the size of H \mathcal{H} H. It is not necessary to output an empty line at the end of the answer for each test case.

Example

Example

input
5
2
3
4
5
6
output
1 1
1 2

2 1
2 3
3 1

1 1
1 3
4 3
4 4

1 1
1 3
1 4
2 1
5 5

1 4
1 5
1 6
5 2
5 5
6 1

Note

In the first testcase we have n = 2 n=2 n=2. One of the possible arrangements is:

The arrangement with cells located in ( 1 , 1 ) (1,1) (1,1) and ( 1 , 2 ) (1,2) (1,2).

In this case H = { ∣ 1 − 1 ∣ + ∣ 1 − 1 ∣ , ∣ 1 − 1 ∣ + ∣ 2 − 2 ∣ , ∣ 1 − 1 ∣ + ∣ 1 − 2 ∣ } = { 0 , 0 , 1 } = { 0 , 1 } \mathcal{H}=\{|1-1|+|1-1|,|1-1|+|2-2|,|1-1|+|1-2|\}=\{0,0,1\}=\{0,1\} H={∣11∣+∣11∣,∣11∣+∣22∣,∣11∣+∣12∣}={0,0,1}={0,1}. Hence, the size of H \mathcal{H} H is 2 2 2. It can be shown that it is the greatest possible answer.

In the second testcase we have n = 3 n=3 n=3. The optimal arrangement is:

The arrangement with cells located in ( 2 , 1 ) (2,1) (2,1), ( 2 , 3 ) (2,3) (2,3) and ( 3 , 1 ) (3,1) (3,1).

H \mathcal{H} H= { ∣ 2 − 2 ∣ + ∣ 1 − 1 ∣ , ∣ 2 − 2 ∣ + ∣ 3 − 3 ∣ , ∣ 3 − 3 ∣ + ∣ 1 − 1 ∣ , ∣ 2 − 2 ∣ + ∣ 1 − 3 ∣ , ∣ 2 − 3 ∣ + ∣ 1 − 1 ∣ , ∣ 2 − 3 ∣ + ∣ 3 − 1 ∣ } \{|2-2|+|1-1|,|2-2|+|3-3|,|3-3|+|1-1|,|2-2|+|1-3|,|2-3|+|1-1|,|2-3|+|3-1|\} {∣22∣+∣11∣,∣22∣+∣33∣,∣33∣+∣11∣,∣22∣+∣13∣,∣23∣+∣11∣,∣23∣+∣31∣}= { 0 , 0 , 0 , 2 , 1 , 3 } \{0,0,0,2,1,3\} {0,0,0,2,1,3}= { 0 , 1 , 2 , 3 } \{0,1,2,3\} {0,1,2,3}.

For n = 4 n=4 n=4 a possible arrangement is:

For n = 5 n=5 n=5 a possible arrangement is:

For n = 6 n=6 n=6 a possible arrangement is:

Solution

具体见文后视频。

Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 7;

int n;
bool vis[N][N], res[N][N];
int mx;

void dfs(int u) {
	if (!u) {
		set<int> s;
		std::vector<PII> point;
		for (int i = 1; i <= n; i ++)
			for (int j = 1; j <= n; j ++)
				if (vis[i][j])
					point.emplace_back(i, j);
			for (auto i : point)
				for (auto j : point)
					s.insert(abs(i.fi - j.fi) + abs(i.se - j.se));
		if (s.size() > mx) {
			mx = s.size();
			memcpy(res, vis, sizeof vis);
		}
		return;
	}
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= n; j ++)
			if (!vis[i][j]) {
				vis[i][j] = 1;
				dfs(u - 1);
				vis[i][j] = 0;
			}
}

void solve() {
	cin >> n;

	if (n == 2) {
		cout << "1 2\n2 2\n";
	} else {
		for (int i = 1; i <= n - 2; i ++)
			cout << 1 << " " << i << endl;
		cout << 2 << " " << n << endl << n << " " << n << endl;
	}
	cout << endl;

	// set<int> s;
	// std::vector<PII> point;
	// int x, y;
	// for (int i = 1; i <= n; i ++)
	// 	cin >> x >> y, point.emplace_back(x, y);
	// for (auto i : point)
	// 	for (auto j : point)
	// 		s.insert(abs(i.fi - j.fi) + abs(i.se - j.se));
	// cout << s.size() << endl;

	// mx = 0;
	// dfs(n);
	// for (int i = 1; i <= n; i ++)
	// 	for (int j = 1; j <= n; j ++)
	// 		if (res[i][j])
	// 			cout << i << " " << j << endl;
	// cout << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

F. Equal XOR Segments

Problem Statement

Let us call an array x 1 , … , x m x_1,\dots,x_m x1,,xm interesting if it is possible to divide the array into k > 1 k>1 k>1 parts so that bitwise XOR of values from each part are equal.

More formally, you must split array x x x into k k k consecutive segments, each element of x x x must belong to exactly 1 1 1 segment. Let y 1 , … , y k y_1,\dots,y_k y1,,yk be the XOR of elements from each part respectively. Then y 1 = y 2 = ⋯ = y k y_1=y_2=\dots=y_k y1=y2==yk must be fulfilled.

For example, if x = [ 1 , 1 , 2 , 3 , 0 ] x = [1, 1, 2, 3, 0] x=[1,1,2,3,0], you can split it as follows: [ 1 ] , [ 1 ] , [ 2 , 3 , 0 ] [\color{blue}1], [\color{green}1], [\color{red}2, \color{red}3, \color{red}0] [1],[1],[2,3,0]. Indeed 1 = 1 = 2 ⊕ 3 ⊕ 0 \color{blue}1=\color{green}1=\color{red}2 \oplus \color{red}3\oplus \color{red}0 1=1=230.

You are given an array a 1 , … , a n a_1,\dots,a_n a1,,an. Your task is to answer q q q queries:

  • For fixed l l l, r r r, determine whether the subarray a l , a l + 1 , … , a r a_l,a_{l+1},\dots,a_r al,al+1,,ar is interesting.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1\le t\le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n and q q q ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105, 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2 \cdot 10^5 1q2105) — the number of elements in the array and the number of queries respectively.

The next line contains n n n integers a 1 , … , a n a_1,\dots,a_n a1,,an ( 0 ≤ a i < 2 30 0 \le a_i < 2^{30} 0ai<230) — elements of the array.

Each of the next q q q lines contains two integers l l l and r r r ( 1 ≤ l < r ≤ n 1 \le l < r \le n 1l<rn) describing the query.

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

It is guaranteed that the sum of q q q over all testcases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each query, output “YES” if the subarray is interesting and “NO” otherwise.

You can output “Yes” and “No” in any case (for example, the strings “yES”, “yes”, and “Yes” will be recognized as correct answers).

Example

input
4
5 5
1 1 2 3 0
1 5
2 4
3 5
1 3
3 4
5 5
1 2 3 4 5
1 5
2 4
3 5
1 3
2 3
7 4
12 9 10 9 10 11 9
1 5
1 7
2 6
2 7
11 4
0 0 1 0 0 1 0 1 1 0 1
1 2
2 5
6 9
7 11
output
YES
YES
NO
NO
NO

YES
NO
NO
YES
NO

NO
NO
NO
NO

YES
NO
YES
YES

Note

Explanation for the first test case:

The first query is described in the statement.

In the second query, we should divide [ 1 , 2 , 3 ] [1,2,3] [1,2,3]. A possible division is [ 1 , 2 ] , [ 3 ] [1,2],[3] [1,2],[3], since 1 ⊕ 2 = 3 1\oplus 2=3 12=3.

It can be shown that for queries 3 , 4 , 5 3,4,5 3,4,5, the subarrays are not interesting.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2e5 + 10;

int n, q;
int s[N];
map<int, vector<int>> p;

void solve() {
	cin >> n >> q;
	for (int i = 1; i <= n; i ++)
		cin >> s[i], s[i] ^= s[i - 1], p[s[i]].emplace_back(i);

	while (q -- ){
		int l, r;
		cin >> l >> r;

		if ((s[r] ^ s[l - 1]) == 0) {
			cout << "YES" << endl;
		} else {
			auto it = lower_bound(p[s[r]].begin(), p[s[r]].end(), l);
			if (it == p[s[r]].end()) {
				cout << "NO" << endl;
				continue;
			}
			int t1 = *it;
			auto it2 = upper_bound(p[s[l - 1]].begin(), p[s[l - 1]].end(), t1);
			if (it2 == p[s[l - 1]].end()) {
				cout << "NO" << endl;
				continue;
			}
			int t2 = *it2;
			if (t1 < r && t2 < r) cout << "YES" << endl;
			else cout << "NO" << endl;
		}
	}
	p.clear();
	cout << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

G1. Division + LCP (easy version)

Problem Statement

This is the easy version of the problem. In this version l = r l=r l=r.

You are given a string s s s. For a fixed k k k, consider a division of s s s into exactly k k k continuous substrings w 1 , … , w k w_1,\dots,w_k w1,,wk. Let f k f_k fk be the maximal possible L C P ( w 1 , … , w k ) LCP(w_1,\dots,w_k) LCP(w1,,wk) among all divisions.

L C P ( w 1 , … , w m ) LCP(w_1,\dots,w_m) LCP(w1,,wm) is the length of the Longest Common Prefix of the strings w 1 , … , w m w_1,\dots,w_m w1,,wm.

For example, if s = a b a b a b c a b s=abababcab s=abababcab and k = 4 k=4 k=4, a possible division is a b a b a b c a b \color{red}{ab}\color{blue}{ab}\color{orange}{abc}\color{green}{ab} abababcab. The L C P ( a b , a b , a b c , a b ) LCP(\color{red}{ab},\color{blue}{ab},\color{orange}{abc},\color{green}{ab}) LCP(ab,ab,abc,ab) is 2 2 2, since a b ab ab is the Longest Common Prefix of those four strings. Note that each substring consists of a continuous segment of characters and each character belongs to exactly one substring.

Your task is to find f l , f l + 1 , … , f r f_l,f_{l+1},\dots,f_r fl,fl+1,,fr. In this version l = r l=r l=r.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n, l l l, r r r ( 1 ≤ l = r ≤ n ≤ 2 ⋅ 1 0 5 1 \le l = r \le n \le 2 \cdot 10^5 1l=rn2105) — the length of the string and the given range.

The second line of each test case contains string s s s of length n n n, all characters are lowercase English letters.

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 2105.

Output

For each test case, output r − l + 1 r-l+1 rl+1 values: f l , … , f r f_l,\dots,f_r fl,,fr.

Example

input
7
3 3 3
aba
3 3 3
aaa
7 2 2
abacaba
9 4 4
abababcab
10 1 1
codeforces
9 3 3
abafababa
5 3 3
zpozp
output
0
1
3
2
10
2
0

Note

In the first sample n = k n=k n=k, so the only division of a b a aba aba is a b a \color{red}a\color{blue}b\color{orange}a aba. The answer is zero, because those strings do not have a common prefix.

In the second sample, the only division is a a a \color{red}a\color{blue}a\color{orange}a aaa. Their longest common prefix is one.

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2e5 + 10;

int n, k;
string s;
int ne[N];

int check(int x) {
	string p = " ";
	for (int i = 1; i <= x; i ++)
		p += s[i];
	for (int i = 1; i <= n; i ++)
		ne[i] = 0;
	for (int i = 2, j = 0; i <= x; i ++) {
		while (j && p[j + 1] != p[i]) j = ne[j];
		if (p[j + 1] == p[i]) j ++;
		ne[i] = j;
	}
	std::vector<PII> res;
	for (int i = 1, j = 0; i <= n; i ++) {
		while (j && s[i] != p[j + 1]) j = ne[j];
		if (s[i] == p[j + 1]) j ++;
		if (j == x) {
			res.emplace_back(i - x + 1, i);
			j = ne[j];
		}
	}
	sort(res.begin(), res.end(), [&](PII a, PII b) {
		return a.se < b.se;
	});
	int ans = 0, ed = 0;
	for (int i = 0; i < res.size(); i ++)
		if (res[i].fi > ed) {
			ans ++;
			ed = res[i].se;
		}
	return ans;
}

void solve() {
	cin >> n >> k >> k >> s;
	s = ' ' + s;

	int l = 0, r = n;
	while (l < r) {
		int mid = l + r + 1 >> 1;
		if (check(mid) >= k) l = mid;
		else r = mid - 1;
	}

	cout << l << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

G2. Division + LCP (hard version)

Problem Statement

This is the hard version of the problem. In this version l ≤ r l\le r lr.

You are given a string s s s. For a fixed k k k, consider a division of s s s into exactly k k k continuous substrings w 1 , … , w k w_1,\dots,w_k w1,,wk. Let f k f_k fk be the maximal possible L C P ( w 1 , … , w k ) LCP(w_1,\dots,w_k) LCP(w1,,wk) among all divisions.

L C P ( w 1 , … , w m ) LCP(w_1,\dots,w_m) LCP(w1,,wm) is the length of the Longest Common Prefix of the strings w 1 , … , w m w_1,\dots,w_m w1,,wm.

For example, if s = a b a b a b c a b s=abababcab s=abababcab and k = 4 k=4 k=4, a possible division is a b a b a b c a b \color{red}{ab}\color{blue}{ab}\color{orange}{abc}\color{green}{ab} abababcab. The L C P ( a b , a b , a b c , a b ) LCP(\color{red}{ab},\color{blue}{ab},\color{orange}{abc},\color{green}{ab}) LCP(ab,ab,abc,ab) is 2 2 2, since a b ab ab is the Longest Common Prefix of those four strings. Note that each substring consists of a continuous segment of characters and each character belongs to exactly one substring.

Your task is to find f l , f l + 1 , … , f r f_l,f_{l+1},\dots,f_r fl,fl+1,,fr.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of test cases.

The first line of each test case contains two integers n n n, l l l, r r r ( 1 ≤ l ≤ r ≤ n ≤ 2 ⋅ 1 0 5 1 \le l \le r \le n \le 2 \cdot 10^5 1lrn2105) — the length of the string and the given range.

The second line of each test case contains string s s s of length n n n, all characters are lowercase English letters.

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 2105.

Output

For each test case, output r − l + 1 r-l+1 rl+1 values: f l , … , f r f_l,\dots,f_r fl,,fr.

Example

input
7
3 1 3
aba
3 2 3
aaa
7 1 5
abacaba
9 1 6
abababcab
10 1 10
aaaaaaawac
9 1 9
abafababa
7 2 7
vvzvvvv
output
3 1 0
1 1
7 3 1 1 0
9 2 2 2 0 0
10 3 2 1 1 1 1 1 0 0
9 3 2 1 1 0 0 0 0
2 2 1 1 1 0

Solution

具体见文后视频。


Code

#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2e5 + 10;

int n, L, R;
string s;
int ne[N], f[N];

int check(int x) {
	if (f[x]) return f[x];
	string p = " ";
	for (int i = 1; i <= x; i ++)
		p += s[i];
	for (int i = 1; i <= n; i ++)
		ne[i] = 0;
	for (int i = 2, j = 0; i <= x; i ++) {
		while (j && p[j + 1] != p[i]) j = ne[j];
		if (p[j + 1] == p[i]) j ++;
		ne[i] = j;
	}
	int ans = 0, ed = 0;
	for (int i = 1, j = 0; i <= n; i ++) {
		while (j && s[i] != p[j + 1]) j = ne[j];
		if (s[i] == p[j + 1]) j ++;
		if (j == x) {
			if (i - x + 1 > ed) {
				ans ++;
				ed = i;
			}
			j = ne[j];
		}
	}
	// int ans = 0, ed = 0;
	// for (int i = 0; i < res.size(); i ++)
	// 	if (res[i].fi > ed) {
	// 		ans ++;
	// 		ed = res[i].se;
	// 	}
	return f[x] = ans;
}

void solve() {
	cin >> n >> L >> R >> s;
	for (int i = 1; i <= n; i ++) f[i] = 0;
	s = ' ' + s;

	int res = n;
	for (int i = L; i <= R; i ++) {
		int l = 0, r = res;
		while (l < r) {
			int mid = l + r + 1 >> 1;
			if (check(mid) >= i) l = mid;
			else r = mid - 1;
		}
		res = l;
		cout << res << " ";
	}
	cout << endl;
}

signed main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(0);

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

视频讲解

Codeforces Round 943 (Div. 3)(A ~ G2 讲解)


最后祝大家早日在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1642420.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

西门子V90参数移植方法

西门子V90参数移植方法 应用方面 由于设备老化损坏&#xff0c;需要更换V90驱动器&#xff0c;但是由于新驱动器与旧驱动器出现版本不一样时参数就会无法直接下载到新的驱动器里面&#xff0c;为了保证更换驱动器的稳定性最好能使用之前设备的参数&#xff0c;所以写了关于V9…

深度学习500问——Chapter08:目标检测(7)

文章目录 8.3.8 RFBNet 8.3.9 M2Det 8.3.8 RFBNet RFBNet有哪些创新点 1. 提出RF block&#xff08;RFB&#xff09;模块 RFBNet主要想利用一些技巧使得轻量级模型在速度和精度上达到很好的trade-off的检测器。灵感来自人类视觉的感受野结构Receptive Fields&#xff08;RFs…

5G Advanced and Release18简述

5G Advanced 5G-Advanced, formally defined in 3GPP Release 18, represents an upgrade to existing 5G networks. 先睹robot总结的5G Advanced的advancements: Enhanced Mobility and Reliability: 5G-Advanced will support advanced applications with improved mobility…

将VM虚拟机Ubuntu20.04系统扩容

一、拓展虚拟机硬盘空间 随着学习的深入&#xff0c;虚拟机里面的内容越来越多&#xff0c;我们可能会面临着硬盘空间不足的问题。今天我们就来沉浸式体验一把给虚拟机扩容。 二、拓展VM虚拟机硬盘前须知 在硬盘拓展时需要注意的一点是有快照的话拓展不了说是&#xff0c;先删除…

【SRC-Python】在数字与字母 / 中文与英文之间插入空格的自动化解决方案

文章目录 Part.I IntroductionPart.II 使用方法Chap.I 直接处理字符串Chap.II 处理文件 Part.III Source CodeReference Part.I Introduction 在编辑文本的过程中&#xff0c;尤其是在 COPY 的过程中&#xff0c;经常会遇到如下问题&#xff1a; 源文本数字与英文字母之间没有…

Vue单页面应用和多页面应用的区别

概念&#xff1a; SPA单页面应用&#xff08;SinglePage Web Application&#xff09;&#xff0c;指只有一个主页面的应用&#xff0c;一开始只需要加载一次js、css等相关资源。所有内容都包含在主页面&#xff0c;对每一个功能模块组件化。单页应用跳转&#xff0c;就是切换…

麦克纳姆轮 Mecanum 小车运动学模型和动力学分析

目录 一、简介 二、运动学模型分析 1. 逆运动学方程 2. 正运动学方程 三、动力学模型 四、广泛运动学模型 一、简介 参考文献https://www.geometrie.tugraz.at/gfrerrer/publications/MecanumWheel.pdf 移动机器人的运动学模型是为了解决小车的正向运动学和逆向运动学问…

Liunx磁盘管理(中)

Liunx磁盘管理(上)-CSDN博客 目录 查看块设备信息 lsblk&#xff08;list block devices&#xff09; fdisk gdisk parted blkid df&#xff08;disk free&#xff09; 虚拟机添加硬盘 步骤&#xff1a; 磁盘分区 MBR格式创建分区 使用方法 替代工具 GPT分区格式…

mysq重连次数过多错误和Public Key Retrieval错误解决记录

文章目录 问题记录和解决方法Public key问题解决many connection errors问题解决 问题记录和解决方法 本次出现错误是&#xff1a;在重启服务器&#xff0c;启动seata应用druid连接mysql作为存储源时出现。主要出现了两个错误&#xff1a;第一个问题&#xff0c;通常采用修改连…

STM32G474 CMAKE VSCODE FREERTOS 导入

一. 文件准备 1. 首先下载 freertos FreeRTOS - Free RTOS Source Code Downloads, the official FreeRTOS zip file release download 2. 移动 FreeRTOS-Kenel 到 moto_control 文件夹下。 3. 将 FreeRTOSConfig.h 放到 /Core/Inc 下面 4. 由于 FreeRTOSConfig.h 中使用了…

我们的手机是如何连接上网的?骨干网又是什么?

什么是骨干网&#xff08;Backbone Network&#xff09; 几台计算机连接起来&#xff0c;互相可以看到其他人的文件&#xff0c;这叫局域网。整个城市的计算机都连接起来&#xff0c;就是城域网。把城市之间连接起来的网就叫骨干网。 这些骨干网是国家批准的可以直接和国外连…

【netty系列-03】深入理解NIO的基本原理和底层实现(详解)

Netty系列整体栏目 内容链接地址【一】深入理解网络通信基本原理和tcp/ip协议https://zhenghuisheng.blog.csdn.net/article/details/136359640【二】深入理解Socket本质和BIOhttps://zhenghuisheng.blog.csdn.net/article/details/136549478【三】深入理解NIO的基本原理和底层…

如何构建用于从收据中提取信息的生成式人工智能工具

原文地址&#xff1a;how-to-build-a-generative-ai-tool-for-information-extraction-from-receipts 使用 LangChain 和 OpenAI 工具从 Google Drive 中存储的收据图像中提取结构化信息 2024 年 4 月 10 日 纸质收据有各种样式和格式&#xff0c;是自动信息提取的一个有趣目…

bpmn-js推荐几款常用的插件

bpmn-js整体框架库的风格是以组件的方式进行实现的,这样的结构也更加便于我们更好的对其进行功能扩展,以及客制化功能实现。其实bpmn.io已经为我们实现了较多场景的组件的实现,了解对应组件的功能更能便于我们区分是否需要自己实现,降低重复造轮子的行为,提高开发效率,本…

All In ai,Oracle 23C没了,等来了Oracle 23ai

今年一月份的Blog介绍Oracle命名规则的时候&#xff0c;说到Oracle的命名是紧紧跟随时代浪潮的前言科技的&#xff0c;在文章的最后还大胆预测也许Oracle的下一个版本就叫25A了&#xff0c;结果Oracle根本等不及&#xff0c;把原来已经海量宣传的Oracle 23C直接改名为23ai&…

《苍穹外卖》前端课程知识点记录

一、VUE基础知识 基于脚手架创建前端工程 1. 环境要求 安装node.js&#xff1a;Node.js安装与配置&#xff08;详细步骤&#xff09;_nodejs安装及环境配置-CSDN博客查看node和npm的版本号 安装Vue CLI&#xff1a;Vue.js安装与创建默认项目&#xff08;详细步骤&#xff09;…

分布式websocket IM即时通讯聊天开源项目如何启动

前言 自己之前分享了分布式websocket的视频有同学去fork项目了&#xff0c;自己启动一下更方便理解项目嘛。然后把项目启动需要的东西全部梳理出来。支持群聊单聊,表情包以及发送图片。 支持消息可靠&#xff0c;消息防重&#xff0c;消息有序。同时基础架构有分布式权限&…

【牛客网】值周

原题链接&#xff1a;登录—专业IT笔试面试备考平台_牛客网 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 差分。 因为l<100000000,所以数组开1e8。 唯一需要注意的点就是前面给b[0]单独赋值为1&#xff08;因为如果在循环中给b[0]赋值&…

【进程间通信】管道和命名管道

文章目录 进程间通信的目的管道匿名管道管道的读写规则 命名管道命名管道和匿名管道区别 进程间通信的目的 数据传输&#xff1a;一个进程需要将它的数据发送给另一个进程资源共享&#xff1a;多个进程之间共享同样的资源。通知事件&#xff1a;一个进程需要向另一个或一组进程…

构建智能化商旅服务:酒店中台云服务架构设计与实践

随着商旅行业的不断发展和智能化趋势的兴起&#xff0c;酒店中台云服务成为了提升服务质量和效率的关键。本文将探讨酒店商旅中台云服务的架构设计与实现&#xff0c;介绍其关键特点和最佳实践&#xff0c;助力商旅行业迈向智能化未来。 1. **需求分析与场景设计&#xff1a;*…