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

A. Find Minimum Operations

Problem Statement

You are given two integers n n n and k k k.

In one operation, you can subtract any power of k k k from n n n. Formally, in one operation, you can replace n n n by ( n − k x ) (n-k^x) (n−kx) for any non-negative integer x x x.

Find the minimum number of operations required to make n n n equal to 0 0 0.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104). 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 , k ≤ 1 0 9 1 \le n, k \le 10^9 1≤n,k≤109).

Output

For each test case, output the minimum number of operations on a new line.

Example

input
6
5 2
3 5
16 4
100 3
6492 10
10 1
output
2
3
1
4
21
10

Note

In the first test case, you can choose a = 1 a = 1 a=1, b = 2 b = 2 b=2, c = 3 c = 3 c=3 in the only operation, since gcd ⁡ ( 1 , 2 ) = gcd ⁡ ( 2 , 3 ) = gcd ⁡ ( 1 , 3 ) = 1 \gcd(1, 2) = \gcd(2, 3) = \gcd(1, 3) = 1 gcd(1,2)=gcd(2,3)=gcd(1,3)=1, and then there are no more integers in the set, so no more operations can be performed.

In the second test case, you can choose a = 3 a = 3 a=3, b = 5 b = 5 b=5, c = 7 c = 7 c=7 in the only operation.

In the third test case, you can choose a = 11 a = 11 a=11, b = 19 b = 19 b=19, c = 20 c = 20 c=20 in the first operation, a = 13 a = 13 a=13, b = 14 b = 14 b=14, c = 15 c = 15 c=15 in the second operation, and a = 10 a = 10 a=10, b = 17 b = 17 b=17, c = 21 c = 21 c=21 in the third operation. After the three operations, the set s s s contains the following integers: 12 12 12, 16 16 16, 18 18 18. It can be proven that it's impossible to perform more than 3 3 3 operations.

Solution

具体见文后视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

void solve() {
	int n, k;
	cin >> n >> k;
	
	if (k == 1) {
		cout << n << endl;
		return;
	}
	int cnt = 0, s = n, mul = 1, res = 0;
	while (s) s /= k, cnt ++, mul *= k;
	for (int i = cnt; i >= 0; i --)
		res += n / mul, n %= mul, mul /= k;
	
	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. Brightness Begins

Problem Statement

Imagine you have n n n light bulbs numbered 1 , 2 , ... , n 1, 2, \ldots, n 1,2,...,n. Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.

Next, you do the following:

  • for each i = 1 , 2 , ... , n i = 1, 2, \ldots, n i=1,2,...,n, flip the state of all bulbs j j j such that j j j is divisible by i † i^\dagger i†.

After performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly k k k.

Find the smallest suitable n n n such that after performing the operations there will be exactly k k k bulbs on. We can show that an answer always exists.

† ^\dagger † An integer x x x is divisible by y y y if there exists an integer z z z such that x = y ⋅ z x = y\cdot z x=y⋅z.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104). The description of the test cases follows.

The only line of each test case contains a single integer k k k ( 1 ≤ k ≤ 1 0 18 1 \le k \le 10^{18} 1≤k≤1018).

Output

For each test case, output n n n --- the minimum number of bulbs.

Example

input
3
1
3
8
output
2
5
11

Note

In the first test case, the minimum number of bulbs is 2 2 2. Let's denote the state of all bulbs with an array, where 1 1 1 corresponds to a turned on bulb, and 0 0 0 corresponds to a turned off bulb. Initially, the array is [ 1 , 1 ] [1, 1] [1,1].

  • After performing the operation with i = 1 i = 1 i=1, the array becomes [ 0 ‾ , 0 ‾ ] [\underline{0}, \underline{0}] [0,0].
  • After performing the operation with i = 2 i = 2 i=2, the array becomes [ 0 , 1 ‾ ] [0, \underline{1}] [0,1].

In the end, there are k = 1 k = 1 k=1 bulbs on. We can also show that the answer cannot be less than 2 2 2.

In the second test case, the minimum number of bulbs is 5 5 5. Initially, the array is [ 1 , 1 , 1 , 1 , 1 ] [1, 1, 1, 1, 1] [1,1,1,1,1].

  • After performing the operation with i = 1 i = 1 i=1, the array becomes [ 0 ‾ , 0 ‾ , 0 ‾ , 0 ‾ , 0 ‾ ] [\underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{0}] [0,0,0,0,0].
  • After performing the operation with i = 2 i = 2 i=2, the array becomes [ 0 , 1 ‾ , 0 , 1 ‾ , 0 ] [0, \underline{1}, 0, \underline{1}, 0] [0,1,0,1,0].
  • After performing the operation with i = 3 i = 3 i=3, the array becomes [ 0 , 1 , 1 ‾ , 1 , 0 ] [0, 1, \underline{1}, 1, 0] [0,1,1,1,0].
  • After performing the operation with i = 4 i = 4 i=4, the array becomes [ 0 , 1 , 1 , 0 ‾ , 0 ] [0, 1, 1, \underline{0}, 0] [0,1,1,0,0].
  • After performing the operation with i = 5 i = 5 i=5, the array becomes [ 0 , 1 , 1 , 0 , 1 ‾ ] [0, 1, 1, 0, \underline{1}] [0,1,1,0,1].

In the end, there are k = 3 k = 3 k=3 bulbs on. We can also show that the answer cannot be smaller than 5 5 5.

Solution

具体见文后视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

void solve() {
	int n;
	cin >> n;
	
	int lo = 1, ro = 9e18, res;
	while (lo <= ro) {
		int mid = lo + ro >> 1;
		auto sq = [](int x) -> int {
			int lo = 1, ro = 3e9, res;
			while (lo <= ro) {
				int mid = lo + ro >> 1;
				if (mid * mid <= x) lo = mid + 1, res = mid;
				else ro = mid - 1;
			}
			return res;
		};
		if (mid - (int)sq(mid) >= n) 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;
}

C. Bitwise Balancing

Problem Statement

You are given three non-negative integers b b b, c c c, and d d d.

Please find a non-negative integer a ∈ [ 0 , 2 61 ] a \in [0, 2^{61}] a∈[0,261] such that ( a   ∣   b ) − ( a   &   c ) = d (a\, |\, b)-(a\, \&\, c)=d (a∣b)−(a&c)=d, where ∣ | ∣ and & \& & denote the bitwise OR operation and the bitwise AND operation, respectively.

If such an a a a exists, print its value. If there is no solution, print a single integer − 1 -1 −1. If there are multiple solutions, print any of them.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 5 1 \le t \le 10^5 1≤t≤105). The description of the test cases follows.

The only line of each test case contains three positive integers b b b, c c c, and d d d ( 0 ≤ b , c , d ≤ 1 0 18 0 \le b, c, d \le 10^{18} 0≤b,c,d≤1018).

Output

For each test case, output the value of a a a, or − 1 -1 −1 if there is no solution. Please note that a a a must be non-negative and cannot exceed 2 61 2^{61} 261.

Example

input
3
2 2 2
4 2 6
10 2 14
output
0
-1
12

Note

In the first test case, we can increase c 1 = 1 c_1 = 1 c1=1 by a = 5 a = 5 a=5. The array c c c will become [ 6 , 3 , 4 , 4 ] [6, 3, 4, 4] [6,3,4,4], and the range is 3 3 3. Note that there is more than one way to reach the answer.

In the second test case, we can increase c 1 = 1 c_1 = 1 c1=1 by a = 2 a = 2 a=2 and then increase c 1 = 3 c_1 = 3 c1=3 by b = 3 b = 3 b=3. Also, we can increase c 2 = 3 c_2 = 3 c2=3 by b = 3 b = 3 b=3 and increase c 3 = 4 c_3 = 4 c3=4 by a = 2 a = 2 a=2. The array c c c will become [ 6 , 6 , 6 , 6 ] [6, 6, 6, 6] [6,6,6,6], and the range is 0 0 0.

Solution

具体见文后视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

void solve() {
	int a = 0, b, c, d;
	cin >> b >> c >> d;
	
	for (int i = 0; i <= 61; i ++)
		if ((b >> i & 1) == (d >> i & 1)) ;
		else if (1 - (c >> i & 1) == (d >> i & 1)) a |= (1ll << i);
		else {
			cout << -1 << endl;
			return;
		}
	cout << a << endl;
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    int dt;
    cin >> dt;
    while (dt -- ) solve();

	return 0;
}

D. Connect the Dots

Problem Statement

Iris has a tree rooted at vertex 1 1 1. Each vertex has a value of 0 \mathtt 0 0 or 1 \mathtt 1 1.

Let's consider a leaf of the tree (the vertex 1 1 1 is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of 10 \mathtt{10} 10 and 01 \mathtt{01} 01 substrings in it.

Take the following tree as an example. Green vertices have a value of 1 \mathtt 1 1 while white vertices have a value of 0 \mathtt 0 0.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 5 1 \le t \le 10^5 1≤t≤105). The description of the test cases follows.

The first line of each test case contains two integers n n n and m m m ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1≤n≤2⋅105, 1 ≤ m ≤ 2 ⋅ 1 0 5 1 \le m \le 2 \cdot 10^5 1≤m≤2⋅105).

The i i i-th of the following m m m lines contains three integers a i a_i ai, d i d_i di, and k i k_i ki ( 1 ≤ a i ≤ a i + k i ⋅ d i ≤ n 1 \le a_i \le a_i + k_i\cdot d_i \le n 1≤ai≤ai+ki⋅di≤n, 1 ≤ d i ≤ 10 1 \le d_i \le 10 1≤di≤10, 0 ≤ k i ≤ n 0 \le k_i \le n 0≤ki≤n).

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

Output

For each test case, output the number of connected components.

Example

input
3
10 2
1 2 4
2 2 4
100 1
19 2 4
100 3
1 2 5
7 2 6
17 2 31
output
2
96
61

Note

In the first test case, there are n = 10 n = 10 n=10 points. The first operation joins the points 1 1 1, 3 3 3, 5 5 5, 7 7 7, and 9 9 9. The second operation joins the points 2 2 2, 4 4 4, 6 6 6, 8 8 8, and 10 10 10. There are thus two connected components: { 1 , 3 , 5 , 7 , 9 } \{1, 3, 5, 7, 9\} {1,3,5,7,9} and { 2 , 4 , 6 , 8 , 10 } \{2, 4, 6, 8, 10\} {2,4,6,8,10}.

In the second test case, there are n = 100 n = 100 n=100 points. The only operation joins the points 19 19 19, 21 21 21, 23 23 23, 25 25 25, and 27 27 27. Now all of them form a single connected component of size 5 5 5. The other 95 95 95 points form single-point connected components. Thus, the answer is 1 + 95 = 96 1 + 95 = 96 1+95=96.

In the third test case, there are n = 100 n = 100 n=100 points. After the operations, all odd points from 1 1 1 to 79 79 79 will be in one connected component of size 40 40 40. The other 60 60 60 points form single-point connected components. Thus, the answer is 1 + 60 = 61 1 + 60 = 61 1+60=61.

Solution

具体见文后视频。

Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;
typedef pair<int, int> PII;

void solve() {
	int n, m;
	cin >> n >> m;
	
	int con = n;
	vector<int> p(n + 1);
	vector<vector<int>> to(11, vector<int>(n + 1));
	for (int i = 1; i <= n; i ++) {
		p[i] = i;
		for (int j = 1; j <= 10; j ++) to[j][i] = i;
	}
	
	auto merge = [&con](vector<int> &p, int u, int v, int op) -> void {
		auto find = [&](auto self, int x) -> int {
			if (p[x] != x) p[x] = self(self, p[x]);
			return p[x];
		};
		int pu = find(find, u), pv = find(find, v);
		if (pu != pv) p[pu] = pv, con -= op;
	};
	while (m -- ) {
		int a, d, k;
		cin >> a >> d >> k;
		
		int x = to[d][a];
		while (x + d <= a + k * d) {
			merge(p, x, x + d, 1), merge(to[d], x, x + d, 0);
			x = to[d][x];
		}
	}
	
	cout << con << endl;
}

signed main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    int dt;
    cin >> dt;
    while (dt -- ) solve();

	return 0;
}

E. Expected Power

Problem Statement

You are given an array of n n n integers a 1 , a 2 , ... , a n a_1,a_2,\ldots,a_n a1,a2,...,an. You are also given an array p 1 , p 2 , ... , p n p_1, p_2, \ldots, p_n p1,p2,...,pn.

Let S S S denote the random multiset (i. e., it may contain equal elements) constructed as follows:

  • Initially, S S S is empty.
  • For each i i i from 1 1 1 to n n n, insert a i a_i ai into S S S with probability p i 1 0 4 \frac{p_i}{10^4} 104pi. Note that each element is inserted independently.

Denote f ( S ) f(S) f(S) as the bitwise XOR of all elements of S S S. Please calculate the expected value of ( f ( S ) ) 2 (f(S))^2 (f(S))2. Output the answer modulo 1 0 9 + 7 10^9 + 7 109+7.

Formally, let M = 1 0 9 + 7 M = 10^9 + 7 M=109+7. It can be shown that the answer can be expressed as an irreducible fraction p q \frac{p}{q} qp, where p p p and q q q are integers and q ≢ 0 ( m o d M ) q \not \equiv 0 \pmod{M} q≡0(modM). Output the integer equal to p ⋅ q − 1   m o d   M p \cdot q^{-1} \bmod M p⋅q−1modM. In other words, output such an integer x x x that 0 ≤ x < M 0 \le x < M 0≤x<M and x ⋅ q ≡ p ( m o d M ) x \cdot q \equiv p \pmod{M} x⋅q≡p(modM).

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104). 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 1≤n≤2⋅105).

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 ≤ 1023 1 \le a_i \le 1023 1≤ai≤1023).

The third line of each test case contains n n n integers p 1 , p 2 , ... , p n p_1,p_2,\ldots,p_n p1,p2,...,pn ( 1 ≤ p i ≤ 1 0 4 1 \le p_i \le 10^4 1≤pi≤104).

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

Output

For each test case, output the expected value of ( f ( S ) ) 2 (f(S))^2 (f(S))2, modulo 1 0 9 + 7 10^9 + 7 109+7.

Example

input
4
2
1 2
5000 5000
2
1 1
1000 2000
6
343 624 675 451 902 820
6536 5326 7648 2165 9430 5428
1
1
10000
output
500000007
820000006
280120536
1

Note

In the first test case, a = [ 1 , 2 ] a = [1, 2] a=[1,2] and each element is inserted into S S S with probability 1 2 \frac{1}{2} 21, since p 1 = p 2 = 5000 p_1 = p_2 = 5000 p1=p2=5000 and p i 1 0 4 = 1 2 \frac{p_i}{10^4} = \frac{1}{2} 104pi=21. Thus, there are 4 4 4 outcomes for S S S, each happening with the same probability of 1 4 \frac{1}{4} 41:

  • S = ∅ S = \varnothing S=∅. In this case, f ( S ) = 0 f(S) = 0 f(S)=0, ( f ( S ) ) 2 = 0 (f(S))^2 = 0 (f(S))2=0.
  • S = { 1 } S = \{1\} S={1}. In this case, f ( S ) = 1 f(S) = 1 f(S)=1, ( f ( S ) ) 2 = 1 (f(S))^2 = 1 (f(S))2=1.
  • S = { 2 } S = \{2\} S={2}. In this case, f ( S ) = 2 f(S) = 2 f(S)=2, ( f ( S ) ) 2 = 4 (f(S))^2 = 4 (f(S))2=4.
  • S = { 1 , 2 } S = \{1,2\} S={1,2}. In this case, f ( S ) = 1 ⊕ 2 = 3 f(S) = 1 \oplus 2 = 3 f(S)=1⊕2=3, ( f ( S ) ) 2 = 9 (f(S))^2 = 9 (f(S))2=9.

Hence, the answer is 0 ⋅ 1 4 + 1 ⋅ 1 4 + 4 ⋅ 1 4 + 9 ⋅ 1 4 = 14 4 = 7 2 ≡ 500   000   007 ( m o d 1 0 9 + 7 ) 0 \cdot \frac{1}{4} + 1 \cdot \frac{1}{4} + 4\cdot \frac{1}{4} + 9 \cdot \frac{1}{4} = \frac{14}{4} = \frac{7}{2} \equiv 500\,000\,007 \pmod{10^9 + 7} 0⋅41+1⋅41+4⋅41+9⋅41=414=27≡500000007(mod109+7).

In the second test case, a = [ 1 , 1 ] a = [1, 1] a=[1,1], a 1 a_1 a1 is inserted into S S S with probability 0.1 0.1 0.1, while a 2 a_2 a2 is inserted into S S S with probability 0.2 0.2 0.2. There are 3 3 3 outcomes for S S S:

  • S = ∅ S = \varnothing S=∅. In this case, f ( S ) = 0 f(S) = 0 f(S)=0, ( f ( S ) ) 2 = 0 (f(S))^2 = 0 (f(S))2=0. This happens with probability ( 1 − 0.1 ) ⋅ ( 1 − 0.2 ) = 0.72 (1-0.1) \cdot (1-0.2) = 0.72 (1−0.1)⋅(1−0.2)=0.72.
  • S = { 1 } S = \{1\} S={1}. In this case, f ( S ) = 1 f(S) = 1 f(S)=1, ( f ( S ) ) 2 = 1 (f(S))^2 = 1 (f(S))2=1. This happens with probability ( 1 − 0.1 ) ⋅ 0.2 + 0.1 ⋅ ( 1 − 0.2 ) = 0.26 (1-0.1) \cdot 0.2 + 0.1 \cdot (1-0.2) = 0.26 (1−0.1)⋅0.2+0.1⋅(1−0.2)=0.26.
  • S = { 1 , 1 } S = \{1, 1\} S={1,1}. In this case, f ( S ) = 0 f(S) = 0 f(S)=0, ( f ( S ) ) 2 = 0 (f(S))^2 = 0 (f(S))2=0. This happens with probability 0.1 ⋅ 0.2 = 0.02 0.1 \cdot 0.2 = 0.02 0.1⋅0.2=0.02.

Hence, the answer is 0 ⋅ 0.72 + 1 ⋅ 0.26 + 0 ⋅ 0.02 = 0.26 = 26 100 ≡ 820   000   006 ( m o d 1 0 9 + 7 ) 0 \cdot 0.72 + 1 \cdot 0.26 + 0 \cdot 0.02 = 0.26 = \frac{26}{100} \equiv 820\,000\,006 \pmod{10^9 + 7} 0⋅0.72+1⋅0.26+0⋅0.02=0.26=10026≡820000006(mod109+7).

Solution

具体见文后视频。


Code

cpp 复制代码
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

void solve() {
	int n;
	cin >> n;
	const int mod = 1e9 + 7;
	auto inv = [&](int x) -> int {
		int b = mod - 2, res = 1;
		while (b) {
			if (b & 1) res = res * x % mod;
			x = x * x % mod;
			b >>= 1;
		}
		return res;
	};
	vector<int> a(n + 1), p(n + 1);
	vector<vector<int>> dp(2, vector<int>(1024, 0));
	for (int i = 1; i <= n; i ++)
		cin >> a[i];
	for (int i = 1; i <= n; i ++)
		cin >> p[i], p[i] = p[i] * inv(10000) % mod;
	
	dp[0][0] = 1;
	for (int i = 1; i <= n; i ++) {
		for (int j = 0; j < 1024; j ++) dp[i & 1][j] = 0;
		for (int j = 0; j < 1024; j ++) {
			dp[i & 1][j ^ a[i]] += dp[(i - 1) & 1][j] * p[i] % mod, dp[i & 1][j ^ a[i]] %= mod;
			dp[i & 1][j] += dp[(i - 1) & 1][j] * (mod + 1 - p[i]) % mod, dp[i & 1][j] %= mod;
		}
	}
	
	int res = 0;
	for (int i = 0; i < 1024; i ++)
		res += dp[n & 1][i] * i % mod * i % mod, res %= mod;
	
	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;
}

视频讲解

Codeforces Round 976 (Div. 2)(A ~ E 题讲解)


最后祝大家早日

相关推荐
可均可可30 分钟前
C++之OpenCV入门到提高004:Mat 对象的使用
c++·opencv·mat·imread·imwrite
幸运超级加倍~1 小时前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
白子寰1 小时前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
yannan201903131 小时前
【算法】(Python)动态规划
python·算法·动态规划
埃菲尔铁塔_CV算法1 小时前
人工智能图像算法:开启视觉新时代的钥匙
人工智能·算法
EasyCVR1 小时前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
linsa_pursuer1 小时前
快乐数算法
算法·leetcode·职场和发展
小芒果_011 小时前
P11229 [CSP-J 2024] 小木棍
c++·算法·信息学奥赛
qq_434085901 小时前
Day 52 || 739. 每日温度 、 496.下一个更大元素 I 、503.下一个更大元素II
算法
Beau_Will1 小时前
ZISUOJ 2024算法基础公选课练习一(2)
算法