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

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 longusing 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 longusing 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 longusing 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 longusing 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 longusing 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 longusing 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.mzph.cn/pingmian/29868.shtml

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

相关文章

【HTML01】HTML基础-基本元素-附带案例-作业

文章目录 HTML 概述学HTML到底学什么HTML的基本结构HTML的注释的作用html的语法HTML的常用标签&#xff1a;相关单词参考资料 HTML 概述 英文全称&#xff1a;Hyper Text Markup Language 中文&#xff1a;超文本标记语言&#xff0c;就将常用的50多个标记嵌入在纯文本中&…

RabbitMQ 入门

目录 一&#xff1a;什么是MQ 二&#xff1a;安装RabbitMQ 三&#xff1a;在Java中如何实现MQ的使用 RabbitMQ的五种消息模型 1.基本消息队列&#xff08;BasicQueue&#xff09; 2.工作消息队列&#xff08;WorkQueue&#xff09; 3. 发布订阅&#xff08;Publish、S…

【论文阅读】Multi-Camera Unified Pre-Training via 3D Scene Reconstruction

论文链接 代码链接 多摄像头三维感知已成为自动驾驶领域的一个重要研究领域&#xff0c;为基于激光雷达的解决方案提供了一种可行且具有成本效益的替代方案。具有成本效益的解决方案。现有的多摄像头算法主要依赖于单目 2D 预训练。然而&#xff0c;单目 2D 预训练忽略了多摄像…

【深度学习】GPT-3,Language Models are Few-Shot Learners(一)

论文&#xff1a; https://arxiv.org/abs/2005.14165 摘要 最近的研究表明&#xff0c;通过在大规模文本语料库上进行预训练&#xff0c;然后在特定任务上进行微调&#xff0c;可以在许多NLP任务和基准上取得显著的进展。虽然这种方法在结构上通常是任务无关的&#xff0c;但…

走进Web3时代的物联网领域:科技的无限可能

随着Web3技术的迅速发展&#xff0c;物联网&#xff08;IoT&#xff09;领域正迎来一场深刻的变革。本文将深入探讨Web3时代如何重新定义物联网的边界和未来发展的无限可能性&#xff0c;从技术原理到应用案例&#xff0c;为读者呈现一个充满挑战和机遇的全新科技景观。 1. Web…

mediasoup源码分析(三)channel创建及信令交互

mediasoup源码分析--channel创建及信令交互 概述跨职能图业务流程图代码剖析 概述 在golang实现mediasoup的tcp服务及channel通道一文中&#xff0c;已经介绍过信令服务中tcp和channel的创建&#xff0c;本文主要讲解c中mediasoup的channel创建&#xff0c;以及信令服务和medi…

如何避免接口重复请求(axios推荐使用AbortController)

前言&#xff1a; 我们日常开发中&#xff0c;经常会遇到点击一个按钮或者进行搜索时&#xff0c;请求接口的需求。 如果我们不做优化&#xff0c;连续点击按钮或者进行搜索&#xff0c;接口会重复请求。 以axios为例&#xff0c;我们一般以以下几种方法为主&#xff1a; 1…

【Pmac】PMAC QT联合开发中各种可能遇到的坑

目录 1. 错误 C2027 使用了未定义类型“PCOMMSERVERLib::DEVUPLOAD”2. 输入了正确的pmac的ip地址&#xff0c;没有显示可选的pmac设备3. Pmac DTC-28B无读数 使用QT编写PMAC上位机程序时&#xff0c;利用QT中的dump工具可以将pcommserver.exe转化为pcommserverlib.h和pcommser…

调度算法-内存页面置换算法

缺⻚异常&#xff08;缺⻚中断&#xff09; 与⼀般中断的主要区别在于&#xff1a; 缺⻚中断在指令执⾏「期间」产⽣和处理中断信号&#xff0c;⽽⼀般中断在⼀条指令执⾏「完成」后检查和处理中断信号。缺⻚中断返回到该指令的开始重新执⾏「该指令」&#xff0c;⽽⼀般中断返…

【HarmonyOS】鸿蒙应用模块化实现

【HarmonyOS】鸿蒙应用模块化实现 一、Module的概念 Module是HarmonyOS应用的基本功能单元&#xff0c;包含了源代码、资源文件、第三方库及应用清单文件&#xff0c;每一个Module都可以独立进行编译和运行。一个HarmonyOS应用通常会包含一个或多个Module&#xff0c;因此&am…

我主编的电子技术实验手册(08)——串联电阻分压

本专栏是笔者主编教材&#xff08;图0所示&#xff09;的电子版&#xff0c;依托简易的元器件和仪表安排了30多个实验&#xff0c;主要面向经费不太充足的中高职院校。每个实验都安排了必不可少的【预习知识】&#xff0c;精心设计的【实验步骤】&#xff0c;全面丰富的【思考习…

Golang——gRPC认证和拦截器

一. OpenSSL 1.1 介绍 OpenSSL是一个开放源代码的软件库包&#xff0c;用于支持网络通讯过程中的加密。这个库提供的功能包含了SSL和TLS协议的实现&#xff0c;并可用于生成密钥、证书、进行密码运算等。 其组成主要包括一下三个组件&#xff1a; openssl&#xff1a;多用途的命…

有效招聘营销策略的六个组成部分

任何想吸引更多人购买其产品的公司都必须投资于市场营销。然而&#xff0c;当涉及到让更多的人了解公司的工作时&#xff0c;许多有效的营销活动可能不是招聘团队的首要考虑因素。为了超越招聘委员会上的“发布祈祷”策略&#xff0c;有必要包括有效招聘营销策略的所有组成部分…

车联网车载设备

智能网联主要通过OBU&#xff08;On Board Unit,车载单元&#xff09;实现。OBU是一种安装在车辆上用于实现V2X通信的硬件设备&#xff0c;可实现和其他车辆OBU&#xff08;PC5&#xff09;、路侧RSU&#xff08;PC5&#xff09;、行人&#xff08;PC5&#xff09;和V2X平台&am…

基于Redis实现共享session登录

搭配食用&#xff1a;Redis&#xff08;基础篇&#xff09;-CSDN博客 项目实现前的 Mysql中的表&#xff1a; 表说明tb_user用户表tb_user_info用户详情表tb_shop商户信息表tb_shop_type商户类型表tb_blog用户日记表&#xff08;达人探店日记)tb_follow用户关注表tb_voucher优…

if/case条件测试语句

一 条件测试 1.1返回码 $? $? 返回码 用来哦按段命令或者脚本是否执行成功 0 true为真就是成功成立 非0 false 失败或者异常 1.2 test 命令 可以进行条件测试 然后根据返回值来判断条件是否成立 -e &#xff1a;exist 测试目录或者目录是否存在 -d : director…

【产品经理】订单处理6-审单方案

电商系统中订单管理员会对特殊类型的订单进行审核&#xff0c;普通订单则自动审核&#xff0c;本节讲述自动审单方案、手动审单以及加急审单。 一、自动审单 自动审单方案可按照方案形式制定&#xff0c;可一次性制定多套审单方案。 1. 审单通过条件有 执行店铺&#xff…

同三维T80006EHL-4K30CN 单路4K30 HDMI编码器(全国产化)

同三维T80006EHL-4K30CN 单路4K30 HDMI编码器 带1路HDMI环出和1路3.5音频输入&#xff0c;支持4K30&#xff0c;所有元器件全国产 一、 产品简介&#xff1a; T80006EHL-4K30CN 4K编码器&#xff08;采集盒&#xff09;是一款全国产化的专业4K HDMI音视频编码产品&#xff0c;…

2024年先进机械电子、电气工程与自动化国际学术会议(ICAMEEA 2024)

2024年先进机械电子、电气工程与自动化国际学术会议(ICAMEEA 2024) 2024 International Conference on Advanced Mechatronic, Electrical Engineering and Automation 会议地点&#xff1a;杭州&#xff0c;中国 网址&#xff1a;www.icameea.com 邮箱: icameeasub-conf.c…

5.How Fast Should You Be When Learning?(你应该用多快的速度学习?)

Normally when I talk about learing quickly, I’m using speed as a synonym for efficiency.Use more effective methods and you’ll learn more in less time.All else being equal, that means you’re learing faster. 通常我在谈到快速学习时&#xff0c;是把“速度&qu…