Codeforces Round 953 (Div. 2 ABCDEF题) 视频讲解

news2024/10/7 12:26:54

A. Alice and Books

Problem Statement

Alice has n n n books. The 1 1 1-st book contains a 1 a_1 a1 pages, the 2 2 2-nd book contains a 2 a_2 a2 pages, … \ldots , the n n n-th book contains a n a_n an pages. Alice does the following:

  • She divides all the books into two non-empty piles. Thus, each book ends up in exactly one of the two piles.
  • Alice reads one book with the highest number in each pile.

Alice loves reading very much. Help her find the maximum total number of pages she can read by dividing the books into two piles.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100) — the number of books Alice has.

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 1ai109) — the number of pages in each book.

Output

For each test case, output a single integer — the maximum number of pages Alice can read.

Example

Example

input
5
2
1 1
4
2 3 3 1
5
2 2 3 2 2
2
10 3
3
1 2 3
output
2
4
5
13
5

Note

In the first test case, Alice can put book number 1 1 1 in the first pile, and book number 2 2 2 in the second pile. Then she will read a 1 + a 2 = 1 + 1 = 2 a_1 + a_2 = 1 + 1 = 2 a1+a2=1+1=2 pages.

In the second test case, Alice can put books with numbers 2 2 2 and 3 3 3 in the first pile, and books with numbers 1 1 1 and 4 4 4 in the second pile. Then she will read the book with the highest number 3 3 3 from the first pile, and the book with the highest number 4 4 4 from the second pile. Then she will read a 3 + a 4 = 3 + 1 = 4 a_3 + a_4 = 3 + 1 = 4 a3+a4=3+1=4 pages.

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 = 1e2 + 10;

int n;
int a[N];

void solve() {
	cin >> n;
	int mx = 0;
	for (int i = 1; i <= n; i ++)
		cin >> a[i];
	for (int i = 1; i < n; i ++)
		mx = max(mx, a[i]);

	cout << mx + a[n] << endl;
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

B. New Bakery

Problem Statement

Bob decided to open a bakery. On the opening day, he baked n n n buns that he can sell. The usual price of a bun is a a a coins, but to attract customers, Bob organized the following promotion:

  • Bob chooses some integer k k k ( 0 ≤ k ≤ min ⁡ ( n , b ) 0 \le k \le \min(n, b) 0kmin(n,b)).
  • Bob sells the first k k k buns at a modified price. In this case, the price of the i i i-th ( 1 ≤ i ≤ k 1 \le i \le k 1ik) sold bun is ( b − i + 1 ) (b - i + 1) (bi+1) coins.
  • The remaining ( n − k ) (n - k) (nk) buns are sold at a a a coins each.

Note that k k k can be equal to 0 0 0. In this case, Bob will sell all the buns at a a a coins each.

Help Bob determine the maximum profit he can obtain by selling all n n n buns.

Input

Each test consists of multiple test cases. 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 description of the test cases follows.

The only line of each test case contains three integers n n n, a a a, and b b b ( 1 ≤ n , a , b ≤ 1 0 9 1 \le n, a, b \le 10^9 1n,a,b109) — the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price.

Output

For each test case, output a single integer — the maximum profit that Bob can obtain.

Example

Example

input
7
4 4 5
5 5 9
10 10 5
5 5 11
1000000000 1000000000 1000000000
1000000000 1000000000 1
1000 1 1000
output
17
35
100
45
1000000000000000000
1000000000000000000
500500

Note

In the first test case, it is optimal for Bob to choose k = 1 k = 1 k=1. Then he will sell one bun for 5 5 5 coins, and three buns at the usual price for 4 4 4 coins each. Then the profit will be 5 + 4 + 4 + 4 = 17 5 + 4 + 4 + 4 = 17 5+4+4+4=17 coins.

In the second test case, it is optimal for Bob to choose k = 5 k = 5 k=5. Then he will sell all the buns at the modified price and obtain a profit of 9 + 8 + 7 + 6 + 5 = 35 9 + 8 + 7 + 6 + 5 = 35 9+8+7+6+5=35 coins.

In the third test case, it is optimal for Bob to choose k = 0 k = 0 k=0. Then he will sell all the buns at the usual price and obtain a profit of 10 ⋅ 10 = 100 10 \cdot 10 = 100 1010=100 coins.

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, a, b;
	cin >> n >> a >> b;
	int k1 = max(min(b - a, n), 0ll), k2 = max(min(b - a + 1, n), 0ll);
	cout << max((2 * b - k1 + 1) * k1 / 2 + (n - k1) * a, (2 * b - k2 + 1) * k2 / 2 + (n - k2) * a) << endl;
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

C. Manhattan Permutations

Problem Statement

Let’s call the Manhattan value of a permutation † ^{\dagger} p p p the value of the expression ∣ p 1 − 1 ∣ + ∣ p 2 − 2 ∣ + … + ∣ p n − n ∣ |p_1 - 1| + |p_2 - 2| + \ldots + |p_n - n| p11∣+p22∣++pnn.

For example, for the permutation [ 1 , 2 , 3 ] [1, 2, 3] [1,2,3], the Manhattan value is ∣ 1 − 1 ∣ + ∣ 2 − 2 ∣ + ∣ 3 − 3 ∣ = 0 |1 - 1| + |2 - 2| + |3 - 3| = 0 ∣11∣+∣22∣+∣33∣=0, and for the permutation [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2], the Manhattan value is ∣ 3 − 1 ∣ + ∣ 1 − 2 ∣ + ∣ 2 − 3 ∣ = 2 + 1 + 1 = 4 |3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4 ∣31∣+∣12∣+∣23∣=2+1+1=4.

You are given integers n n n and k k k. Find a permutation p p p of length n n n such that its Manhattan value is equal to k k k, or determine that no such permutation exists.

† ^{\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 ≤ 1 0 4 1 \leq t \leq 10^{4} 1t104) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 , 0 ≤ k ≤ 1 0 12 1 \le n \le 2 \cdot 10^{5}, 0 \le k \le 10^{12} 1n2105,0k1012) — the length of the permutation and the required Manhattan value.

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, if there is no suitable permutation, output “No”. Otherwise, in the first line, output “Yes”, and in the second line, output n n n distinct integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn ( 1 ≤ p i ≤ n 1 \le p_i \le n 1pin) — a suitable permutation.

If there are multiple solutions, output any of them.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes”, and “YES” will be recognized as a positive answer).

Example

input
8
3 4
4 5
7 0
1 1000000000000
8 14
112 777
5 12
5 2
output
Yes
3 1 2
No
Yes
1 2 3 4 5 6 7
No
Yes
8 2 3 4 5 6 1 7
No
Yes
5 4 3 1 2
Yes
2 1 3 4 5

Note

In the first test case, the permutation [ 3 , 1 , 2 ] [3, 1, 2] [3,1,2] is suitable, its Manhattan value is ∣ 3 − 1 ∣ + ∣ 1 − 2 ∣ + ∣ 2 − 3 ∣ = 2 + 1 + 1 = 4 |3 - 1| + |1 - 2| + |2 - 3| = 2 + 1 + 1 = 4 ∣31∣+∣12∣+∣23∣=2+1+1=4.

In the second test case, it can be proven that there is no permutation of length 4 4 4 with a Manhattan value of 5 5 5.

In the third test case, the permutation [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] [1,2,3,4,5,6,7] [1,2,3,4,5,6,7] is suitable, its Manhattan value is ∣ 1 − 1 ∣ + ∣ 2 − 2 ∣ + ∣ 3 − 3 ∣ + ∣ 4 − 4 ∣ + ∣ 5 − 5 ∣ + ∣ 6 − 6 ∣ + ∣ 7 − 7 ∣ = 0 |1-1|+|2-2|+|3-3|+|4-4|+|5-5|+|6-6|+|7-7|=0 ∣11∣+∣22∣+∣33∣+∣44∣+∣55∣+∣66∣+∣77∣=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;

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

	if (n * n / 2 < k || k & 1) {
		cout << "No" << endl;
		return;
	}
	cout << "Yes" << endl;
	std::vector<int> p(n + 1);
	int cnt = (n * n / 2 - k) / ((n + 1 >> 1) * 2), pos = ((n * n / 2 - k) % ((n + 1 >> 1) * 2)) / 2;
	for (int i = 1; i <= cnt; i ++)
		p[i] = i;
	for (int i = cnt + 1, j = n / 2 + 1; i <= cnt + (n + 1 >> 1); i ++, j ++)
		p[i] = j;
	for (int i = cnt + (n + 1 >> 1) + 1, j = cnt + 1; i <= n; i ++, j ++)
		p[i] = j;
	for (int i = cnt + (n + 1 >> 1) + 1, j = pos; j; j --, i --)
		swap(p[i], p[i - 1]);
	for (int i = 1; i <= n; i ++)
		cout << p[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. Elections

Problem Statement

Elections are taking place in Berland. There are n n n candidates participating in the elections, numbered from 1 1 1 to n n n. The i i i-th candidate has a i a_i ai fans who will vote for him. Additionally, there are c c c people who are undecided about their favorite candidate, let’s call them undecided. Undecided people will vote for the candidate with the lowest number.

The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins.

You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number i i i to participate in the elections, all a i a_i ai of his fans will become undecided, and will vote for the candidate with the lowest number.

You are curious to find, for each i i i from 1 1 1 to n n n, the minimum number of candidates that need to be excluded from the elections for candidate number i i i to win the elections.

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 1t2104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and c c c ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105, 0 ≤ c ≤ 1 0 9 0 \le c \le 10^9 0c109) — the number of candidates in the elections and the number of undecided people.

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 ( 0 ≤ a i ≤ 1 0 9 0 \le a_i \le 10^9 0ai109) — the number of fans for each candidate.

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 n n n integers, the i i i-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number i i i to win.

Example

Example

input
5
3 1
2 0 3
2 3
0 10
5 3
5 4 3 2 1
4 5
3 10 7 1
6 0
2 2 2 3 3 3
output
0 1 2
1 0
0 1 2 3 4
1 0 2 3
1 1 2 0 4 5

Note

In the first test case:

  • If all candidates are allowed, candidate number 1 1 1 will receive 3 3 3 votes ( 1 1 1 undecided person will vote for him), candidate number 2 2 2 will receive 0 0 0 votes, and candidate number 3 3 3 will receive 3 3 3 votes. Therefore, candidate number 1 1 1 wins (he received the same number of votes as candidate 3 3 3, but his number is lower), so the answer for him is 0 0 0.
  • If candidate number 1 1 1 is not allowed, his 2 2 2 fans will become undecided. Then candidate number 2 2 2 will receive 3 3 3 votes ( 3 3 3 undecided people will vote for him) and candidate number 3 3 3 will receive 3 3 3 votes. Therefore, candidate number 2 2 2 wins (he received the same number of votes as candidate 3 3 3, but his number is lower), so the answer for him is 1 1 1.
  • If candidates with numbers 1 1 1 and 2 2 2 are not allowed, candidate number 3 3 3 wins, so the answer for him is 2 2 2.

In the second test case, candidate number 1 1 1 will win if candidate number 2 2 2 is not allowed to participate.

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, c;
	cin >> n >> c;
	std::vector<int> a(n);
	int mx = 0, p;
	for (int i = 0; i < n; i ++) {
		cin >> a[i];
		if (a[i] > mx) mx = a[i], p = i;
	}

	int sum = 0;
	for (int i = 0; i < n; i ++) {
		if (p == 0 && i == 0 || i == p && a[0] + c < a[i]) cout << 0 << " ";
		else if (sum + c + a[i] >= mx) cout << i << " ";
		else cout << i + 1 << " ";
		sum += 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;
}

E. Computing Machine

Problem Statement

Sasha has two binary strings s s s and t t t of the same length n n n, consisting of the characters 0 and 1.

There is also a computing machine that can perform two types of operations on binary strings a a a and b b b of the same length k k k:

  1. If a i = a i + 2 = a_{i} = a_{i + 2} = ai=ai+2= 0, then you can assign b i + 1 : = b_{i + 1} := bi+1:= 1 ( 1 ≤ i ≤ k − 2 1 \le i \le k - 2 1ik2).
  2. If b i = b i + 2 = b_{i} = b_{i + 2} = bi=bi+2= 1, then you can assign a i + 1 : = a_{i + 1} := ai+1:= 1 ( 1 ≤ i ≤ k − 2 1 \le i \le k - 2 1ik2).

Sasha became interested in the following: if we consider the string a = s l s l + 1 … s r a=s_ls_{l+1}\ldots s_r a=slsl+1sr and the string b = t l t l + 1 … t r b=t_lt_{l+1}\ldots t_r b=tltl+1tr, what is the maximum number of 1 characters in the string a a a that can be obtained using the computing machine. Since Sasha is very curious but lazy, it is up to you to answer this question for several pairs ( l i , r i ) (l_i, r_i) (li,ri) that interest him.

Input

Each test consists of multiple test cases. 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 description of the test cases follows.

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 1n2105) — the length of the strings s s s and t t t.

The second line of each test case contains a binary string s s s of length n n n, consisting of the characters 0 and 1.

The third line of each test case contains a binary string t t t of length n n n, consisting of the characters 0 and 1.

The fourth line of each test case contains a single integer q q q ( 1 ≤ q ≤ 2 ⋅ 1 0 5 1 \le q \le 2 \cdot 10^5 1q2105) — the number of queries.

The i i i-th of the following lines contains two integers l i l_{i} li and r i r_{i} ri ( 1 ≤ l i ≤ r i ≤ n 1 \le l_{i} \le r_{i} \le n 1lirin) — the boundaries of the i i i-th pair of substrings that interest Sasha.

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 and the sum of q q q over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each test case, output q q q integers — the answers to all queries.

Example

input
3
4
1111
0000
2
1 2
2 4
4
1010
1101
2
1 3
1 4
6
010101
011010
5
2 3
1 6
2 5
4 4
3 6
output
2
3
2
3
1
4
3
1
2

Note

In the first test case:

  • In the first query, a = a = a= 11, so the maximum number of 1 characters is 2 2 2.
  • In the second query, a = a = a= 111, so the maximum number of 1 characters is 3 3 3.

In the second test case:

  • In the first query, a = a = a= 101 and b = b = b= 110. No operations can be performed, so the maximum number of 1 characters is 2 2 2.
  • In the second query, a = a = a= 1010 and b = b = b= 1101. Since a 2 = a 4 = a_2 = a_4 = a2=a4= 0, we can assign b 3 : = b_3 := b3:= 1. Now b 1 = b 3 = b_1 = b_3 = b1=b3= 1, so we can assign a 2 : = a_2 := a2:= 1. The string a a a becomes 1110, so the maximum number of 1 characters is 3 3 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;

const int N = 2e5 + 10;

int n, q, cnt;
string a, b, c, d;
int tot[N];

int work(int l, int r) {
	string s = b, t = a;
	for (int i = l; i <= r - 2; i ++)
		if (a[i] == a[i + 2] && a[i] == '0') s[i + 1] = '1';
	for (int i = l; i <= r - 2; i ++)
		if (s[i] == s[i + 2] && s[i] == '1') t[i + 1] = '1';
	int res = 0;
	for (int i = l; i <= r; i ++)
		res += t[i] == '1';
	return res;
}

void solve() {
	cin >> n >> a >> b >> q;
	a = ' ' + a, b = ' ' + b, c = a, d = b;

	for (int i = 1; i <= n - 2; i ++)
		if (c[i] == c[i + 2] && c[i] == '0') d[i + 1] = '1';
	for (int i = 1; i <= n - 2; i ++)
		if (d[i] == d[i + 2] && d[i] == '1') c[i + 1] = '1';
	for (int i = 1; i <= n; i ++)
		tot[i] = tot[i - 1] + (c[i] == '1');

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

		if (r - l + 1 <= 4) {
			cout << work(l, r) << endl;
			continue;
		}
		int res = tot[r] - tot[l - 1];
		if (d[l - 1] == d[l + 1] && d[l - 1] == '1' && a[l] == '0') res --;
		if (l + 1 <= n && a[l - 1] == '0' && a[l + 1] == '0' && b[l] == '0') {
			if (l + 2 <= n && d[l + 2] == '1') res --;
		}
		if (r + 1 <= n && d[r - 1] == d[r + 1] && d[r + 1] == '1' && a[r] == '0') res --;
		if (r + 1 <= n && a[r - 1] == '0' && a[r + 1] == '0' && b[r] == '0') {
			if (r - 2 >= 1 && d[r - 2] == '1') res --;
		}
		cout << res << endl;
	}
}

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

	int dt;
	
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

F. Large Graph

Problem Statement

Given an array a a a of length n n n. Let’s construct a square matrix b b b of size n × n n \times n n×n, in which the i i i-th row contains the array a a a cyclically shifted to the right by ( i − 1 ) (i - 1) (i1). For example, for the array a = [ 3 , 4 , 5 ] a = [3, 4, 5] a=[3,4,5], the obtained matrix is

b = [ 3 4 5 5 3 4 4 5 3 ] b = \begin{bmatrix} 3 & 4 & 5 \\ 5 & 3 & 4 \\ 4 & 5 & 3 \end{bmatrix} b= 354435543

Let’s construct the following graph:

  • The graph contains n 2 n^2 n2 vertices, each of which corresponds to one of the elements of the matrix. Let’s denote the vertex corresponding to the element b i , j b_{i, j} bi,j as ( i , j ) (i, j) (i,j).
  • We will draw an edge between vertices ( i 1 , j 1 ) (i_1, j_1) (i1,j1) and ( i 2 , j 2 ) (i_2, j_2) (i2,j2) if ∣ i 1 − i 2 ∣ + ∣ j 1 − j 2 ∣ ≤ k |i_1 - i_2| + |j_1 - j_2| \le k i1i2+j1j2k and gcd ⁡ ( b i 1 , j 1 , b i 2 , j 2 ) > 1 \gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1 gcd(bi1,j1,bi2,j2)>1, where gcd ⁡ ( x , y ) \gcd(x, y) gcd(x,y) denotes the greatest common divisor of integers x x x and y y y.

Your task is to calculate the number of connected components † ^{\dagger} in the obtained graph.

† ^{\dagger} A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 5 1 \leq t \leq 10^5 1t105) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2n106, 2 ≤ k ≤ 2 ⋅ 1 0 6 2 \le k \le 2 \cdot 10^6 2k2106) — the length of the array and the parameter k k k.

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 6 1 \le a_i \le 10^6 1ai106) — 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 1 0 6 10^6 106.

Output

For each test case, output a single integer — the number of connected components in the obtained graph.

Example

input
6
3 3
3 4 5
3 3
3 4 9
3 2
3 4 9
2 2
2 8
5 3
8 27 5 4 3
4 10
2 2 2 2
output
3
2
3
1
4
1

Note

In the first test case, the matrix b b b is given in the statement. The first connected component contains the vertices ( 1 , 1 ) (1, 1) (1,1), ( 2 , 2 ) (2, 2) (2,2), and ( 3 , 3 ) (3, 3) (3,3). The second connected component contains the vertices ( 1 , 2 ) (1, 2) (1,2), ( 2 , 3 ) (2, 3) (2,3), and ( 3 , 1 ) (3, 1) (3,1). The third connected component contains the vertices ( 1 , 3 ) (1, 3) (1,3), ( 2 , 1 ) (2, 1) (2,1), and ( 3 , 2 ) (3, 2) (3,2). Thus, the graph has 3 3 3 connected components.

In the second test case, the following matrix is obtained:

b = [ 3 4 9 9 3 4 4 9 3 ] b = \begin{bmatrix} 3 & 4 & 9 \\ 9 & 3 & 4 \\ 4 & 9 & 3 \end{bmatrix} b= 394439943

The first connected component contains all vertices corresponding to elements with values 3 3 3 and 9 9 9. The second connected component contains all vertices corresponding to elements with the value 4 4 4.

In the fourth test case, all vertices are in one connected component.

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 = 2e6 + 10;

int n, m, k;
int a[N], b[N], p[N];
int st[N], prime[N], idx;
std::vector<int> fact[N], pos[N];

int find(int x) {
	if (p[x] != x) p[x] = find(p[x]);
	return p[x];
}
void Euler(int lim) {
	st[1] = 1;
	for (int i = 2; i <= lim; i ++) {
		if (!st[i]) prime[ ++ idx] = i;
		for (int j = 1; prime[j] * i <= lim; j ++) {
			st[prime[j] * i] = 1;
			if (i % prime[j] == 0) break;
		}
	}
}
void solve() {
	cin >> n >> k;
	for (int i = 1; i <= n; i ++)
		cin >> a[i];
	m = 0;
	for (int i = 2; i <= n; i ++) b[ ++ m] = a[i];
	b[ ++ m] = a[1];
	for (int i = 2; i <= n; i ++) b[ ++ m] = a[i];

	int res = m;
	set<int> avl;
	for (int i = 1; i <= m; i ++) {
		p[i] = i, res += (b[i] == 1) * ((m + 1 >> 1) - abs(i - (m + 1 >> 1)) - 1);
		for (auto v : fact[b[i]])
			if (!st[v])
				pos[v].push_back(i), avl.insert(v);
	}

	for (auto i : avl)
		for (int j = 1; j < pos[i].size(); j ++) {
			if (pos[i][j] - pos[i][j - 1] <= k) {
				int pa = find(pos[i][j]), pb = find(pos[i][j - 1]);
				if (pa != pb) {
					p[pa] = pb;
					res --;
				}
			}
		}

	cout << res << endl;
	for (int i = 1; i <= m; i ++)
		for (auto v : fact[b[i]])
			pos[v].clear();
}

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

	for (int i = 1; i < (N >> 1); i ++)
		for (int j = i; j < (N >> 1); j += i)
			fact[j].push_back(i);
	Euler(N >> 1);

	int dt;
	cin >> dt;
	while (dt --) 
		solve();

	return 0;
}

视频讲解

Codeforces Round 953 (Div. 2)(A ~ F 题讲解)


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

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

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

相关文章

可以聊天的ai软件有实用的吗?分享3个智能的软件!

在数字化浪潮席卷而来的今天&#xff0c;人工智能&#xff08;AI&#xff09;技术已经深入我们生活的方方面面&#xff0c;其中AI聊天软件以其独特的交互方式和智能化的对话体验&#xff0c;吸引了众多用户的关注。本文将为您盘点当前市场上热门的AI聊天软件&#xff0c;带您领…

怎么监控公司的电脑屏幕使用记录?倾情推荐这六款电脑屏幕监控软件

监控公司电脑屏幕使用记录主要是为了提高工作效率、保障信息安全以及确保员工合规使用公司资源。以下是几个推荐的软件。看完您心中就会有自己的选择。 1.安企神软件 功能特点&#xff1a;安企神提供了实时屏幕监控、屏幕录制、文件操作记录、网页浏览监控等多种功能。它可以实…

安装docker与docker-compose

1. 项目目标 &#xff08;1&#xff09;安装docker &#xff08;2&#xff09;安装docker-compose &#xff08;3&#xff09;配置镜像源 2. 项目准备 centos7.9系统 3. 项目实施 3.1. 安装docker 安装基本命令&#xff1a; yum -y install wget yum -y install vim y…

共享充电新风潮来了,能效电气与高德地图强强联手

在新能源汽车市场中,无论是新势力车企还是BBA等传统车企,都在积极布局,内卷现象愈发明显。为何这些车企纷纷选择入局新能源市场?答案显而易见,新能源汽车具有环保、节能、低维护成本等诸多优势,相较于传统燃油车,它们更能满足现代消费者对绿色、可持续出行的需求。 这场内卷不…

.Net Core WebApi 程序在Swagger API说明文档中不显示注释的解决办法

本次所用版本为 .Net6.0 现象&#xff1a;在Swagger启动后&#xff0c;API接口等不显示注释描述&#xff0c;如下&#xff1a; 在代码中找到调用AddSwaggerGen的地方&#xff0c;如下&#xff1a; builder.Services.AddSwaggerGen(); 修改为&#xff1a; builder.Services.…

IDEA中SpringMVC的运行环境问题

文章目录 一、IEAD 清理缓存二、用阿里云和spring创建 SpringMVC 项目中 pom.xml 文件的区别 一、IEAD 清理缓存 springMVC 运行时存在一些之前运行过的缓存导致项目不能运行&#xff0c;可以试试清理缓存 二、用阿里云和spring创建 SpringMVC 项目中 pom.xml 文件的区别 以下…

分数限制下,@Nelson认为优先选择体现自己优势的专业,欢迎围观~

目录 一、写在前面 二、自己的观点 三、最热门行业统计及专业分析 1.计算机、人工智能 2.材料化工、新能源方向 3.生物方向-基因/脑科学 4.自动化、机器人 5.芯片设计与制造 四、总结 一、写在前面 24年高考帷幕落下&#xff0c;对于每一位高考考生&#xff0c;学校和…

原生APP开发的技术难点

原生APP开发是一项复杂的技术工作&#xff0c;需要掌握多种编程语言和技术。原生APP开发的技术难点主要体现在以下几个方面&#xff0c;原生APP开发是一项技术难度较高的工作&#xff0c;需要开发者具备扎实的编程基础和丰富的开发经验。北京木奇移动技术有限公司&#xff0c;专…

友思特分享 | 百皮秒+一体化:基于Q开关与增益开关技术的短脉冲激光器

导读 基于主动调Q、被动调Q和增益开关技术的激光器能够产生高能量的瞬时短激光脉冲。友思特提供基于多种调制技术的百皮秒级脉冲激光器&#xff0c;脉宽覆盖独特的50ps~1ns范围&#xff0c;可在科研、医疗与工业领域广泛应用。 产生激光脉冲最直接的方法是在连续激光器外部加一…

如何快速搭建满足用户需求的运营体系?Xinstall来支招!

随着互联网的飞速发展&#xff0c;App的推广和运营面临着越来越多的挑战。传统的营销手段逐渐失效&#xff0c;如何在这个多变的互联网环境下&#xff0c;迅速搭建起能满足用户需求的运营体系&#xff0c;成为了众多企业关注的焦点。而Xinstall&#xff0c;作为一款专业的App推…

轻松获取指定日期所在周的周一和周日

哈喽&#xff0c;大家好呀&#xff0c;好久不见&#xff01;今天是一篇浅记。根据传入日期自动获取所在周一和周日… 正常基操方法&#xff0c;根据传入日期自动获取所在周一和周日。注意传入日期是周日的情况哈&#xff0c;需要往前推7天才是周一。 楼主方法中已处理&#xf…

重庆悠琦企业管理有限公司是骗子吗?

答案 是的&#xff0c;重庆悠琦企业管理有限公司是诈骗公司。该公司位于&#xff1a;重庆市江北区建新北路八支路35号1幢29-7 为什么说他是诈骗公司&#xff1f; 该公司顶着帮别人办理职称的名义进行收费&#xff0c;承诺会在某个时间段将职称办理好。别信&#xff0c;别信&…

基于文本挖掘的卡塔尔世界杯赛事网络舆情演变与趋势预测

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

Web渗透信息收集进阶

网站敏感目录与文件 网站敏感目录表示网站目录中容易被恶意人员利用的一些目录。通常恶意人员都是通过工具扫描&#xff0c;来扫出网站的敏感目录&#xff0c;敏感目录是能够得到其他网页的信息&#xff0c;从而找到后台管理页面&#xff0c;尝试进入后台等&#xff0c;扫描网…

基于格网拓扑关系的边缘点检测

1、背景介绍 前文已介绍对点云进行格网处理,可以计算平面点云面积、格网拓扑关系构建,相关博客如下: (1)点云格网过程可视化(C++ PCL)-CSDN博客 (2)

科普:什么是-动态加解密技术?

动态加解密技术是一种在数据使用过程中自动对数据进行加密或解密操作的技术&#xff0c;无需用户的干预。这种技术对于保护敏感信息、防止数据泄露具有重要意义。 www.drhchina.com 以下是关于动态加解密技术的详细补充&#xff1a; 定义与特点 定义&#xff1a; 动态加解密…

怎么做到源代码防泄密?9种方法教会你

源代码加密是一种安全措施&#xff0c;其目的是为了保护软件的源代码不被未授权的个人或实体访问或泄露。源代码是软件应用程序的基础&#xff0c;它包含了程序的逻辑结构、核心算法以及设计理念。由于源代码承载了软件的核心知识和创新&#xff0c;因此它具有极高的商业价值和…

Docker部署私有仓库Harbor

Harbor构建Docker私有仓库 文章目录 Harbor构建Docker私有仓库资源列表一、部署Docker-Compose服务1.1、下载最新Docker-Compose1.2、查看Docker-Compose版本 二、部署Harbor服务2.1、下载Harbor安装程序2.2、配置Harbor参数文件2.3、所需参数和可选参数2.3.1、所需参数2.3.2、…

RERCS系统开发实战案例-Part08 FPM 应用程序的表单组件(From UIBB)与列表组件(List UIBB)组合的创建

1、新建From UIBB的FPM Application的快速启动面板 备注&#xff1a;该步骤可第一步操作&#xff0c;也可最后一步操作&#xff0c;本人习惯第一步操作。 1&#xff09;使用事务码 LPD_CUST&#xff0c;选择对应的角色与实例进入快速启动板定制页面&#xff1b; 2&#xff09…

从热潮到理性,大模型迎来产业「拐点」

前言 无人不谈大模型&#xff0c;是今年上半年科技界的真实写照。 从市场热闹程度来看&#xff0c;大模型已经成为各家科技厂商争先涌入的赛道&#xff0c;无论是互联网巨头&#xff0c;还是科技公司&#xff0c;甚至是研究机构&#xff0c;均已加入这场大模型混战&#xff0…