Codeforces Round 988 (Div. 3 ABCDEFG题) 视频讲解

A. Twice

Problem Statement

Kinich wakes up to the start of a new day. He turns on his phone, checks his mailbox, and finds a mysterious present. He decides to unbox the present.

Kinich unboxes an array a a a with n n n integers. Initially, Kinich’s score is 0 0 0. He will perform the following operation any number of times:

  • Select two indices i i i and j j j ( 1 ≤ i < j ≤ n ) (1 \leq i < j \leq n) (1i<jn) such that neither i i i nor j j j has been chosen in any previous operation and a i = a j a_i = a_j ai=aj. Then, add 1 1 1 to his score.

Output the maximum score Kinich can achieve after performing the aforementioned operation any number of times.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 500 1 \leq t \leq 500 1t500) — the number of test cases.

The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 20 1 \leq n \leq 20 1n20) — the length of a a a.

The following line of each test case contains n n n space-separated integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1ain).

Output

For each test case, output the maximum score achievable on a new line.

Example

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

Note

In the first and third testcases, Kinich cannot perform any operations.

In the second testcase, Kinich can perform one operation with i = 1 i=1 i=1 and j = 2 j=2 j=2.

In the fourth testcase, Kinich can perform one operation with i = 1 i=1 i=1 and j = 4 j=4 j=4.

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);
	for (int i = 0; i < n; i ++)
		cin >> a[i];
	sort(a.begin(), a.end());

	int res = 0;
	for (int i = 1; i < n; i ++)
		if (a[i - 1] == a[i])
			res ++, i ++;

	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;
}

B. Intercepted Inputs

Problem Statement

To help you prepare for your upcoming Codeforces contest, Citlali set a grid problem and is trying to give you a n n n by m m m grid through your input stream. Specifically, your input stream should contain the following:

  • The first line contains two integers n n n and m m m — the dimensions of the grid.
  • The following n n n lines contain m m m integers each — the values of the grid.

However, someone has intercepted your input stream, shuffled all given integers, and put them all on one line! Now, there are k k k integers all on one line, and you don’t know where each integer originally belongs. Instead of asking Citlali to resend the input, you decide to determine the values of n n n and m m m yourself.

Output any possible value of n n n and m m m that Citlali could have provided.

Input

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

The first line of each test case contains an integer k k k ( 3 ≤ k ≤ 2 ⋅ 1 0 5 3 \leq k \leq 2 \cdot 10^5 3k2105) — the total number of inputs in your input stream.

The following line of each test case contains k k k integers a 1 , a 2 , … , a k a_1, a_2, \ldots, a_k a1,a2,,ak ( 1 ≤ a i ≤ k 1 \leq a_i \leq k 1aik) — the shuffled inputs of your input stream. It is guaranteed that n n n and m m m are contained within the k k k integers.

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

Output

For each test case, output two integers, one possible value of n n n and m m m. If multiple possible answers exist, output any.

Example

input
5
3
1 1 2
11
3 3 4 5 6 7 8 9 9 10 11
8
8 4 8 3 8 2 8 1
6
2 1 4 5 3 3
8
1 2 6 3 8 5 5 3
output
1 1
3 3
2 3
4 1
1 6

Note

In the first test case, the initial input could have been the following:

1 1

2

In the second test case, the initial input could have been the following:

3 3

4 5 6

7 8 9

9 10 11

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), b(n + 1);
	for (int i = 0; i < n; i ++)
		cin >> a[i], b[a[i]] = i;
	for (int i = 0; i < n; i ++)
		if ((n - 2) % a[i] == 0 && b[(n - 2) / a[i]] && b[(n - 2) / a[i]] != i) {
			cout << a[i] << " " << (n - 2) / a[i] << endl;
			return;
		}
}

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

	int dt;
	cin >> dt;

	while (dt --)
		solve();

	return 0;
}

C. Superultra’s Favorite Permutation

Problem Statement

Superultra, a little red panda, desperately wants primogems. In his dreams, a voice tells him that he must solve the following task to obtain a lifetime supply of primogems. Help Superultra!

Construct a permutation ∗ ^{\text{∗}} p p p of length n n n such that p i + p i + 1 p_i + p_{i+1} pi+pi+1 is composite † ^{\text{†}} over all 1 ≤ i ≤ n − 1 1 \leq i \leq n - 1 1in1. If it’s not possible, output − 1 -1 1.

∗ ^{\text{∗}} 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).

† ^{\text{†}} An integer x x x is composite if it has at least one other divisor besides 1 1 1 and x x x. For example, 4 4 4 is composite because 2 2 2 is a divisor.

Input

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

Each test case contains an integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \leq n \leq 2 \cdot 10^5 2n2105) — the length of the permutation.

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 it’s not possible to construct p p p, output − 1 -1 1 on a new line. Otherwise, output n n n integers p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn on a new line.

Example

input
2
3
8
output
-1
1 8 7 3 6 2 4 5

Note

In the first example, it can be shown that all permutation of size 3 3 3 contain two adjacent elements whose sum is prime. For example, in the permutation [ 2 , 3 , 1 ] [2,3,1] [2,3,1] the sum 2 + 3 = 5 2+3=5 2+3=5 is prime.

In the second example, we can verify that the sample output is correct because 1 + 8 1+8 1+8, 8 + 7 8+7 8+7, 7 + 3 7+3 7+3, 3 + 6 3+6 3+6, 6 + 2 6+2 6+2, 2 + 4 2+4 2+4, and 4 + 5 4+5 4+5 are all composite. There may be other constructions that are correct.

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;

	if (n <= 4) {
		cout << -1 << endl;
		return;
	}
	for (int i = 1; i <= n; i += 2)
		if (i != 5) cout << i << " ";
	cout << 5 << " " << 4 << " ";
	for (int i = 2; i <= n; i += 2)
		if (i != 4) cout << 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. Sharky Surfing

Problem Statement

Mualani loves surfing on her sharky surfboard!

Mualani’s surf path can be modeled by a number line. She starts at position 1 1 1, and the path ends at position L L L. When she is at position x x x with a jump power of k k k, she can jump to any integer position in the interval [ x , x + k ] [x, x+k] [x,x+k]. Initially, her jump power is 1 1 1.

However, her surf path isn’t completely smooth. There are n n n hurdles on her path. Each hurdle is represented by an interval [ l , r ] [l, r] [l,r], meaning she cannot jump to any position in the interval [ l , r ] [l, r] [l,r].

There are also m m m power-ups at certain positions on the path. Power-up i i i is located at position x i x_i xi and has a value of v i v_i vi. When Mualani is at position x i x_i xi, she has the option to collect the power-up to increase her jump power by v i v_i vi. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle.

What is the minimum number of power-ups she must collect to reach position L L L to finish the path? If it is not possible to finish the surf path, output − 1 -1 1.

Input

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

The first line of each test case contains three integers n n n, m m m, and L L L ( 1 ≤ n , m ≤ 2 ⋅ 1 0 5 , 3 ≤ L ≤ 1 0 9 1 \leq n, m \leq 2 \cdot 10^5, 3 \leq L \leq 10^9 1n,m2105,3L109) — the number of hurdles, the number of power-ups, and the position of the end.

The following n n n lines contain two integers l i l_i li and r i r_i ri ( 2 ≤ l i ≤ r i ≤ L − 1 2 \leq l_i \leq r_i \leq L-1 2liriL1) — the bounds of the interval for the i i i’th hurdle. It is guaranteed that r i + 1 < l i + 1 r_i + 1 < l_{i+1} ri+1<li+1 for all 1 ≤ i < n 1 \leq i < n 1i<n (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle).

The following m m m lines contain two integers x i x_i xi and v i v_i vi ( 1 ≤ x i , v i ≤ L 1 \leq x_i, v_i \leq L 1xi,viL) — the position and the value for the i i i’th power-up. There may be multiple power-ups with the same x x x. It is guaranteed that x i ≤ x i + 1 x_i \leq x_{i+1} xixi+1 for all 1 ≤ i < m 1 \leq i < m 1i<m (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle.

It is guaranteed the sum of n n n and the sum of 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 the minimum number of power-ups she must collect to reach position L L L. If it is not possible, output − 1 -1 1.

Example

input
4
2 5 50
7 14
30 40
2 2
3 1
3 5
18 2
22 32
4 3 50
4 6
15 18
20 26
34 38
1 2
8 2
10 2
1 4 17
10 14
1 6
1 2
1 2
16 9
1 2 10
5 9
2 3
2 2
output
4
-1
1
2

Note

In the first test case, she can collect power-ups 1 1 1, 2 2 2, 3 3 3, and 5 5 5 to clear all hurdles.

In the second test case, she cannot jump over the first hurdle.

In the fourth test case, by collecting both power-ups, she can jump over the hurdle.

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, m, L;
	cin >> n >> m >> L;

	std::vector<int> l(n), r(n), x(m), v(m);
	for (int i = 0; i < n; i ++) cin >> l[i] >> r[i];
	for (int i = 0; i < m; i ++) cin >> x[i] >> v[i];

	multiset<int> up;
	int E = 1, res = 0;
	for (int i = 0, j = -1; i < n; i ++) {
		while (j + 1 < m && x[j + 1] < l[i]) j ++, up.insert(v[j]);
		while (up.size() && E <= r[i] - l[i] + 1) {
			E += *up.rbegin(), res ++;
			up.erase( -- up.end());
		}
		if (E <= r[i] - l[i] + 1) {
			cout << -1 << endl;
			return;
		}
	}

	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;
}

E. Kachina’s Favorite Binary String

Problem Statement

This is an interactive problem.

Kachina challenges you to guess her favorite binary string ∗ ^{\text{∗}} s s s of length n n n. She defines f ( l , r ) f(l, r) f(l,r) as the number of subsequences † ^{\text{†}} of 01 \texttt{01} 01 in s l s l + 1 … s r s_l s_{l+1} \ldots s_r slsl+1sr. Two subsequences are considered different if they are formed by deleting characters from different positions in the original string, even if the resulting subsequences consist of the same characters.

To determine s s s, you can ask her some questions. In each question, you can choose two indices l l l and r r r ( 1 ≤ l < r ≤ n 1 \leq l < r \leq n 1l<rn) and ask her for the value of f ( l , r ) f(l, r) f(l,r).

Determine and output s s s after asking Kachina no more than n n n questions. However, it may be the case that s s s is impossible to be determined. In this case, you would need to report IMPOSSIBLE \texttt{IMPOSSIBLE} IMPOSSIBLE instead.

Formally, s s s is impossible to be determined if after asking n n n questions, there are always multiple possible strings for s s s, regardless of what questions are asked. Note that if you report IMPOSSIBLE \texttt{IMPOSSIBLE} IMPOSSIBLE when there exists a sequence of at most n n n queries that will uniquely determine the binary string, you will get the Wrong Answer verdict.

∗ ^{\text{∗}} A binary string only contains characters 0 \texttt{0} 0 and 1 \texttt{1} 1.

† ^{\text{†}} 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. For example, subsequences of 1011101 \mathtt{1011101} 1011101 are 0 \mathtt{0} 0, 1 \mathtt{1} 1, 11111 \mathtt{11111} 11111, 0111 \mathtt{0111} 0111, but not 000 \mathtt{000} 000 nor 11100 \mathtt{11100} 11100.

Input

The first line of input contains a single integer t t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1t103) — the number of test cases.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 1 0 4 2 \leq n \leq 10^4 2n104) — the length of s s s.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 4 10^4 104.

The jury will return an integer . Interaction

To ask a question, output a line in the following format (do not include quotes)

  • ?   l   r \texttt{? l r} ? l r” ( 1 ≤ l < r ≤ n 1 \leq l < r \leq n 1l<rn)

The jury will return an integer f ( l , r ) f(l, r) f(l,r).

When you are ready to print the answer, output a single line in the following format

  • If s s s is impossible to be determined, output “ !   IMPOSSIBLE \texttt{! IMPOSSIBLE} ! IMPOSSIBLE
  • Otherwise, output “ !   s \texttt{! s} ! s

After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query.

The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn’t depend on the queries asked by the participant.

If your program makes more than n n n queries for one test case, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see the documentation for other languages.

Hacks

To make a hack, use the following format.

The first line should contain a single integer t t t ( 1 ≤ t ≤ 1 0 3 1 \leq t \leq 10^3 1t103) – the number of test cases.

The first line of each test case should contain an integer n n n ( 2 ≤ n ≤ 1 0 4 2 \leq n \leq 10^4 2n104) — the length of s s s.

The following line should contain s s s, a binary string of length n n n.

The sum of n n n over all test cases should not exceed 1 0 4 10^4 104.

Example

input
2
5

4

0

1

2

2

0

output
? 1 5

? 2 4

? 4 5

? 3 5

! 01001

? 1 2

! IMPOSSIBLE

Note

In the first test case:

In the first query, you ask Kachina for the value of f ( 1 , 5 ) f(1, 5) f(1,5), and she responds with 4 4 4 in the input stream.

In the second query, you ask Kachina for the value of f ( 2 , 4 ) f(2, 4) f(2,4). Because there are no subsequences of 01 \texttt{01} 01 in the string 100 \texttt{100} 100, she responds with 0 0 0 in the input stream.

After asking 4 4 4 questions, you report 01001 \texttt{01001} 01001 as s s s, and it is correct.

In the second test case:

In the first query, you ask Kachina for the value of f ( 1 , 2 ) f(1, 2) f(1,2), and she responds with 0 0 0 in the input stream. Notice that this is the only distinct question you can ask.

However, notice that the strings 00 00 00 and 11 11 11 both have an answer of 0 0 0, and it is impossible to differentiate between the two. Therefore, we report IMPOSSIBLE.

Please note that this example only serves to demonstrate the interaction format. It is not guaranteed the queries provided are optimal or uniquely determine the answer. However, it can be shown there exists a sequence of at most 5 5 5 queries that does uniquely determine sample test case 1 1 1.

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 ask(int l, int r) {
	int cnt;
	cout << "? " << l << " " << r << endl;
	cin >> cnt;
	return cnt;
}

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

	int pos = 2, v;
	while (pos <= n && (v = ask(1, pos)) == 0) pos ++;
	if (pos > n) {
		cout << "! IMPOSSIBLE" << endl;
		return;
	}
	string res;
	for (int i = 1; i <= pos - v - 1; i ++) res += '1';
	for (int i = 1; i <= v; i ++) res += '0';
	res += '1';
	for (int i = pos + 1; i <= n; i ++) {
		int tmp = ask(1, i);
		if (tmp > v) v = tmp, res += '1';
		else res += '0';
	}
	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. Ardent Flames

Problem Statement

You have obtained the new limited event character Xilonen. You decide to use her in combat.

There are n n n enemies in a line. The i i i’th enemy from the left has health h i h_i hi and is currently at position x i x_i xi. Xilonen has an attack damage of m m m, and you are ready to defeat the enemies with her.

Xilonen has a powerful “ground stomp” attack. Before you perform any attacks, you select an integer p p p and position Xilonen there ( p p p can be any integer position, including a position with an enemy currently). Afterwards, for each attack, she deals m m m damage to an enemy at position p p p (if there are any), m − 1 m-1 m1 damage to enemies at positions p − 1 p-1 p1 and p + 1 p+1 p+1, m − 2 m-2 m2 damage to enemies at positions p − 2 p-2 p2 and p + 2 p+2 p+2, and so on. Enemies that are at least a distance of m m m away from Xilonen take no damage from attacks.

Formally, if there is an enemy at position x x x, she will deal max ⁡ ( 0 , m − ∣ p − x ∣ ) \max(0,m - |p - x|) max(0,mpx) damage to that enemy each hit. Note that you may not choose a different p p p for different attacks.

Over all possible p p p, output the minimum number of attacks Xilonen must perform to defeat at least k k k enemies. If it is impossible to find a p p p such that eventually at least k k k enemies will be defeated, output − 1 -1 1 instead. Note that an enemy is considered to be defeated if its health reaches 0 0 0 or below.

Input

The first line contains an integer t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1t104) – the number of test cases.

The first line of each test case contains three integers n n n, m m m, and k k k ( 1 ≤ k ≤ n ≤ 1 0 5 1 \leq k \leq n \leq 10^5 1kn105, 1 ≤ m ≤ 1 0 9 1 \leq m \leq 10^9 1m109).

The following line contains n n n integers h 1 , h 2 , . . . , h n h_1, h_2, ..., h_n h1,h2,...,hn ( 1 ≤ h i ≤ 1 0 9 1 \leq h_i \leq 10^9 1hi109).

The last line of each testcase contains n n n integers x 1 , x 2 , . . . , x n x_1, x_2, ..., x_n x1,x2,...,xn ( 1 ≤ x i ≤ 1 0 9 1\leq x_i \leq 10^9 1xi109, x i < x i + 1 x_i < x_{i+1} xi<xi+1 for all 1 ≤ i < n 1 \leq i < n 1i<n)

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

Output

For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least k k k enemies. If it is impossible to find a p p p such that eventually at least k k k enemies will be defeated, output − 1 -1 1 instead.

Example

input
6
5 5 3
7 7 7 7 7
1 2 3 4 5
9 5 9
2 4 6 8 10 8 6 4 2
1 2 3 4 5 6 7 8 9
2 10 2
1 1
1 20
2 10 1
69696969 420420420
1 20
2 10 2
10 15
1 19
2 2 2
1000000000 1
1 3
output
2
2
-1
6969697
15
1000000000

Note

In the first testcase, it is optimal to select p = 2 p=2 p=2. Each attack, the first enemy takes 5 − ∣ 2 − 1 ∣ = 4 5-|2-1|=4 5∣21∣=4 damage, the second enemy takes 5 5 5 damage, the third enemy takes 4 4 4 damage, the fourth enemy takes 3 3 3 damage, and the fifth enemy takes 2 2 2 damage. After 2 2 2 attacks, the first three enemies will be defeated. It can be shown that it is impossible to defeat 3 3 3 enemies in less than 2 2 2 attacks, no matter which p p p is selected.

In the second testcase, we must kill all 9 9 9 enemies. By selecting p = 5 p=5 p=5, all nine enemies will be defeated in 2 2 2 attacks.

In the third testcase, we must kill both enemies. However, it can be shown that no p p p selected will damage both enemies at the same time, so the answer is − 1 -1 1.

In the fourth testcase, selecting p = 1 p=1 p=1 will enable us to defeat the first enemy in 6969697 6969697 6969697 attacks.

In the fifth testcase, selecting p = 10 p=10 p=10 will make each enemy take 1 1 1 damage per attack. Both enemies will be defeated in 15 15 15 attacks.

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, m, k;
	cin >> n >> m >> k;

	std::vector<int> h(n), x(n), v(n);
	for (int i = 0; i < n; i ++)
		cin >> h[i];
	for (int i = 0; i < n; i ++)
		cin >> x[i];

	int lo = 1, ro = 1E9, res = -1;
	while (lo <= ro) {
		int mid = lo + ro >> 1;
		for (int i = 0; i < n; i ++) v[i] = (h[i] + mid - 1) / mid;
		std::vector<int> avl, dct1, dct2;
		for (int i = 0; i < n; i ++)
			avl.push_back(x[i] + v[i] - m), avl.push_back(m - v[i] + x[i]);
		for (int i = 0; i < n; i ++)
			dct2.push_back(x[i] + v[i]), dct1.push_back(v[i] - x[i]);
		sort(avl.begin(), avl.end());
		sort(dct1.begin(), dct1.end());
		dct1.erase(unique(dct1.begin(), dct1.end()), dct1.end());
		sort(dct2.begin(), dct2.end());
		dct2.erase(unique(dct2.begin(), dct2.end()), dct2.end());
		std::vector<int> pre(dct1.size() + 1), suf(dct2.size() + 1);

		for (int i = 0; i < n; i ++) {
			int idx = lower_bound(dct2.begin(), dct2.end(), x[i] + v[i]) - dct2.begin() + 1;
			for (int j = idx; j <= dct2.size(); j += (j & -j)) suf[j] ++;
		}
		int mx = 0;
		for (int i = 0, j = -1; i < avl.size(); i ++) {
			int p = avl[i];
			while (j + 1 < n && x[j + 1] <= p) {
				j ++;
				int idx = lower_bound(dct1.begin(), dct1.end(), v[j] - x[j]) - dct1.begin() + 1;
				for (int k = idx; k <= dct1.size(); k += (k & -k)) pre[k] ++;
				idx = lower_bound(dct2.begin(), dct2.end(), x[j] + v[j]) - dct2.begin() + 1;
				for (int k = idx; k <= dct2.size(); k += (k & -k)) suf[k] --;
			}
			int sum = 0, idx = upper_bound(dct1.begin(), dct1.end(), m - p) - dct1.begin();
			if (idx) for (int k = idx; k; k -= (k & -k)) sum += pre[k];
			idx = upper_bound(dct2.begin(), dct2.end(), m + p) - dct2.begin();
			if (idx) for (int k = idx; k; k -= (k & -k)) sum += suf[k];
			mx = max(mx, sum);
		}
		if (mx >= k) ro = mid - 1, res = mid;
		else lo = mid + 1;
	}

	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;
}

G. Natlan Exploring

Problem Statement

You are exploring the stunning region of Natlan! This region consists of n n n cities, and each city is rated with an attractiveness a i a_i ai. A directed edge exists from City i i i to City j j j if and only if i < j i < j i<j and gcd ⁡ ( a i , a j ) ≠ 1 \gcd(a_i,a_j)\neq 1 gcd(ai,aj)=1, where gcd ⁡ ( x , y ) \gcd(x, y) gcd(x,y) denotes the greatest common divisor (GCD) of integers x x x and y y y.

Starting from City 1 1 1, your task is to determine the total number of distinct paths you can take to reach City n n n, modulo 998   244   353 998\,244\,353 998244353. Two paths are different if and only if the set of cities visited is different.

Input

The first line contains an integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \leq n \leq 2 \cdot 10^5 2n2105) — the number of cities.

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 2 ≤ a i ≤ 1 0 6 2 \leq a_i \leq 10^6 2ai106) — the attractiveness of each city.

Output

Output the total number of distinct paths you can take to reach City n n n, modulo 998   244   353 998\,244\,353 998244353.

Example

input
5
2 6 3 4 6
output
5
input
5
4 196 2662 2197 121
output
2
input
7
3 6 8 9 11 12 20
output
7
input
2
2 3
output
0

Note

In the first example, the five paths are the following:

  • City 1 → 1\rightarrow 1 City 5 5 5
  • City 1 → 1\rightarrow 1 City 2 → 2\rightarrow 2 City 5 5 5
  • City 1 → 1\rightarrow 1 City 2 → 2\rightarrow 2 City 3 → 3\rightarrow 3 City 5 5 5
  • City 1 → 1\rightarrow 1 City 2 → 2\rightarrow 2 City 4 → 4\rightarrow 4 City 5 5 5
  • City 1 → 1\rightarrow 1 City 4 → 4\rightarrow 4 City 5 5 5

In the second example, the two paths are the following:

  • City 1 → 1\rightarrow 1 City 3 → 3\rightarrow 3 City 5 5 5
  • City 1 → 1\rightarrow 1 City 2 → 2\rightarrow 2 City 3 → 3\rightarrow 3 City 5 5 5

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 = 1E6 + 10, mod = 998244353;

int prm[N], st[N], mn[N], idx, sum[N];
std::vector<int> pfact[N];

void prework() {
	for (int i = 2; i < N; i ++) {
		if (!st[i]) prm[ ++ idx] = i, mn[i] = i;
		for (int j = 1; prm[j] * i < N; j ++) {
			st[prm[j] * i] = 1;
			mn[prm[j] * i] = prm[j];
			if (i % prm[j] == 0) break;
		}
	}
}

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

	prework();
	for (int i = 1; i < N; i ++) {
		int u = i;
		while (u != 1) {
			int v = mn[u];
			pfact[i].push_back(v);
			while (u % v == 0) u /= v;
		}
	}
	
	int n;
	cin >> n;

	std::vector<int> a(n), dp(n, 0);
	for (int i = 0; i < n; i ++)
		cin >> a[i];

	dp[0] = 1;
	for (int i = 0; i < n; i ++) {
		std::vector<int> avl;
		for (int j = 1; j < 1 << pfact[a[i]].size(); j ++) {
			int mul = 1, xr = -1;
			for (int k = 0; k < pfact[a[i]].size(); k ++)
				if (j >> k & 1) mul *= pfact[a[i]][k], xr *= (-1);
			(dp[i] += xr * sum[mul]) %= mod, avl.push_back(mul);
		}
		for (auto v : avl) (sum[v] += dp[i]) %= mod;
	}

	cout << (dp[n - 1] + mod) % mod << endl;

	return 0;
}

视频讲解

Codeforces Round 988 (Div. 3)(A ~ G 题讲解)


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值