A. Shohag Loves Mod
Problem Statement
Shohag has an integer n n n. Please help him find an increasing integer sequence 1 ≤ a 1 < a 2 < … < a n ≤ 100 1 \le a_1 \lt a_2 \lt \ldots \lt a_n \le 100 1≤a1<a2<…<an≤100 such that a i m o d i ≠ a j m o d j a_i \bmod i \neq a_j \bmod j aimodi=ajmodj ∗ ^{\text{∗}} ∗ is satisfied over all pairs 1 ≤ i < j ≤ n 1 \le i \lt j \le n 1≤i<j≤n.
It can be shown that such a sequence always exists under the given constraints.
∗ ^{\text{∗}} ∗ a m o d b a \bmod b amodb denotes the remainder of a a a after division by b b b. For example, 7 m o d 3 = 1 , 8 m o d 4 = 0 7 \bmod 3 = 1, 8 \bmod 4 = 0 7mod3=1,8mod4=0 and 69 m o d 10 = 9 69 \bmod 10 = 9 69mod10=9.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 50 1 \le t \le 50 1≤t≤50) — the number of test cases.
The first and only line of each test case contains an integer n n n ( 2 ≤ n ≤ 50 2 \le n \le 50 2≤n≤50).
Output
For each test case, print n n n integers — the integer sequence that satisfies the conditions mentioned in the statement. If there are multiple such sequences, output any.
Example
input |
---|
2 |
3 |
6 |
output |
---|
2 7 8 |
2 3 32 35 69 95 |
Note
In the first test case, the sequence is increasing, values are from 1 1 1 to 100 100 100 and each pair of indices satisfies the condition mentioned in the statement:
- For pair ( 1 , 2 ) (1, 2) (1,2), a 1 m o d 1 = 2 m o d 1 = 0 a_1 \bmod 1 = 2 \bmod 1 = 0 a1mod1=2mod1=0, and a 2 m o d 2 = 7 m o d 2 = 1 a_2 \bmod 2 = 7 \bmod 2 = 1 a2mod2=7mod2=1. So they are different.
- For pair ( 1 , 3 ) (1, 3) (1,3), a 1 m o d 1 = 2 m o d 1 = 0 a_1 \bmod 1 = 2 \bmod 1 = 0 a1mod1=2mod1=0, and a 3 m o d 3 = 8 m o d 3 = 2 a_3 \bmod 3 = 8 \bmod 3 = 2 a3mod3=8mod3=2. So they are different.
- For pair ( 2 , 3 ) (2, 3) (2,3), a 2 m o d 2 = 7 m o d 2 = 1 a_2 \bmod 2 = 7 \bmod 2 = 1 a2mod2=7mod2=1, and a 3 m o d 3 = 8 m o d 3 = 2 a_3 \bmod 3 = 8 \bmod 3 = 2 a3mod3=8mod3=2. So they are different.
Note that you do not necessarily have to print the exact same sequence, you can print any other sequence as long as it satisfies the necessary conditions.
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;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. Shohag Loves Strings
Problem Statement
For a string p p p, let f ( p ) f(p) f(p) be the number of distinct non-empty substrings ∗ ^{\text{∗}} ∗ of p p p.
Shohag has a string s s s. Help him find a non-empty string p p p such that p p p is a substring of s s s and f ( p ) f(p) f(p) is even or state that no such string exists.
∗ ^{\text{∗}} ∗A string a a a is a substring of a string b b b if a a a can be obtained from b b b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first and only line of each test case contains a string s s s ( 1 ≤ ∣ s ∣ ≤ 1 0 5 1 \le |s| \le 10^5 1≤∣s∣≤105) consisting of lowercase English letters.
It is guaranteed that the sum of the length of s s s over all test cases doesn’t exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105.
Output
For each test case, print a non-empty string that satisfies the conditions mentioned in the statement, or − 1 -1 −1 if no such string exists. If there are multiple solutions, output any.
Example
input |
---|
5 |
dcabaac |
a |
youknowwho |
codeforces |
bangladesh |
output |
---|
abaa |
-1 |
youknowwho |
eforce |
bang |
Note
In the first test case, we can set $p = $ abaa because it is a substring of s s s and the distinct non-empty substrings of p p p are a, b, aa, ab, ba, aba, baa and abaa, so it has a total of 8 8 8 distinct substrings which is even.
In the second test case, we can only set $p = $ a but it has one distinct non-empty substring but this number is odd, so not valid.
In the third test case, the whole string contains 52 52 52 distinct non-empty substrings, so the string itself is a valid solution.
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() {string s;cin >> s;for (int i = 1; i < s.size(); i ++)if (s[i] == s[i - 1]) {cout << s[i - 1] << s[i] << endl;return;}if (s.size() >= 3) {for (int i = 2; i < s.size(); i ++)if (s[i - 2] != s[i]) {cout << s[i - 2] << s[i - 1] << s[i] << endl;return;}cout << -1 << endl;}else cout << -1 << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}
C1. Shohag Loves XOR (Easy Version)
Problem Statement
This is the easy version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers x x x and m m m. Help him count the number of integers 1 ≤ y ≤ m 1 \le y \le m 1≤y≤m such that x ≠ y \mathbf{x \neq y} x=y and x ⊕ y x \oplus y x⊕y is a divisor ∗ ^{\text{∗}} ∗ of either x x x, y y y, or both. Here ⊕ \oplus ⊕ is the bitwise XOR operator.
∗ ^{\text{∗}} ∗The number b b b is a divisor of the number a a a if there exists an integer c c c such that a = b ⋅ c a = b \cdot c a=b⋅c.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first and only line of each test case contains two space-separated integers x x x and m m m ( 1 ≤ x ≤ 1 0 6 1 \le x \le 10^6 1≤x≤106, 1 ≤ m ≤ 1 0 18 1 \le m \le 10^{18} 1≤m≤1018).
It is guaranteed that the sum of x x x over all test cases does not exceed 1 0 7 10^7 107.
Output
For each test case, print a single integer — the number of suitable y y y.
Example
input |
---|
5 |
6 9 |
5 7 |
2 3 |
6 4 |
4 1 |
output |
---|
3 |
2 |
1 |
1 |
0 |
Note
In the first test case, for x = 6 x = 6 x=6, there are 3 3 3 valid values for y y y among the integers from 1 1 1 to m = 9 m = 9 m=9, and they are 4 4 4, 5 5 5, and 7 7 7.
- y = 4 y = 4 y=4 is valid because x ⊕ y = 6 ⊕ 4 = 2 x \oplus y = 6 \oplus 4 = 2 x⊕y=6⊕4=2 and 2 2 2 is a divisor of both x = 6 x = 6 x=6 and y = 4 y = 4 y=4.
- y = 5 y = 5 y=5 is valid because x ⊕ y = 6 ⊕ 5 = 3 x \oplus y = 6 \oplus 5 = 3 x⊕y=6⊕5=3 and 3 3 3 is a divisor of x = 6 x = 6 x=6.
- y = 7 y = 7 y=7 is valid because x ⊕ y = 6 ⊕ 7 = 1 x \oplus y = 6 \oplus 7 = 1 x⊕y=6⊕7=1 and 1 1 1 is a divisor of both x = 6 x = 6 x=6 and y = 7 y = 7 y=7.
In the second test case, for x = 5 x = 5 x=5, there are 2 2 2 valid values for y y y among the integers from 1 1 1 to m = 7 m = 7 m=7, and they are 4 4 4 and 6 6 6.
- y = 4 y = 4 y=4 is valid because x ⊕ y = 5 ⊕ 4 = 1 x \oplus y = 5 \oplus 4 = 1 x⊕y=5⊕4=1 and 1 1 1 is a divisor of both x = 5 x = 5 x=5 and y = 4 y = 4 y=4.
- y = 6 y = 6 y=6 is valid because x ⊕ y = 5 ⊕ 6 = 3 x \oplus y = 5 \oplus 6 = 3 x⊕y=5⊕6=3 and 3 3 3 is a divisor of y = 6 y = 6 y=6.
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 x, m;cin >> x >> m;int cnt = 0;for (int y = 1; y <= x * 2; y ++)if (x != y && y <= m && (x % (x ^ y) == 0 || y % (x ^ y) == 0))cnt ++;cout << cnt << endl;
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int dt;cin >> dt;while (dt --)solve();return 0;
}
C2. Shohag Loves XOR (Hard Version)
Problem Statement
This is the hard version of the problem. The differences between the two versions are highlighted in bold. You can only make hacks if both versions of the problem are solved.
Shohag has two integers x x x and m m m. Help him count the number of integers 1 ≤ y ≤ m 1 \le y \le m 1≤y≤m such that x ⊕ y x \oplus y x⊕y is divisible ∗ ^{\text{∗}} ∗ by either x x x, y y y, or both. Here ⊕ \oplus ⊕ is the bitwise XOR operator.
∗ ^{\text{∗}} ∗The number a a a is divisible by the number b b b if there exists an integer c c c such that a = b ⋅ c a = b \cdot c a=b⋅c.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first and only line of each test case contains two space-separated integers x x x and m m m ( 1 ≤ x ≤ 1 0 6 1 \le x \le 10^6 1≤x≤106, 1 ≤ m ≤ 1 0 18 1 \le m \le 10^{18} 1≤m≤1018).
It is guaranteed that the sum of x x x over all test cases does not exceed 1 0 7 10^7 107.
Output
For each test case, print a single integer — the number of suitable y y y.
Example
input |
---|
5 |
7 10 |
2 3 |
6 4 |
1 6 |
4 1 |
output |
---|
3 |
2 |
2 |
6 |
1 |
Note
In the first test case, for x = 7 x = 7 x=7, there are 3 3 3 valid values for y y y among the integers from 1 1 1 to m = 10 m = 10 m=10, and they are 1 1 1, 7 7 7, and 9 9 9.
- y = 1 y = 1 y=1 is valid because x ⊕ y = 7 ⊕ 1 = 6 x \oplus y = 7 \oplus 1 = 6 x⊕y=7⊕1=6 and 6 6 6 is divisible by y = 1 y = 1 y=1.
- y = 7 y = 7 y=7 is valid because x ⊕ y = 7 ⊕ 7 = 0 x \oplus y = 7 \oplus 7 = 0 x⊕y=7⊕7=0 and 0 0 0 is divisible by both x = 7 x = 7 x=7 and y = 7 y = 7 y=7.
- y = 9 y = 9 y=9 is valid because x ⊕ y = 7 ⊕ 9 = 14 x \oplus y = 7 \oplus 9 = 14 x⊕y=7⊕9=14 and 14 14 14 is divisible by x = 7 x = 7 x=7.
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 x, m;cin >> x >> m;int lo = 0, ro = (m + x - 1) / x, all = 1, ok;while (all <= x) all *= 2;all --;int lim = m <= all ? m : x * 2, res = 0;for (int i = 1; i <= min(m, lim); i ++)if ((i ^ x) % i == 0 && (i ^ x) % x != 0)res ++;if (m <= all) {for (int i = 1; i <= m; i ++)if ((i ^ x) % x == 0) res ++;cout << res << endl;return;}while (lo <= ro) {int mid = lo + ro >> 1;if (mid * x <= m && (m ^ (mid * x)) > all) lo = mid + 1, ok = mid;else ro = mid - 1;}res += ok + (ok == 0);// cout << ok << " " << res << endl;if ((((ok + 1) * x) ^ x) <= m) res ++;if ((((ok + 2) * x) ^ x) <= m) 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;
}
D. Shohag Loves GCD
Problem Statement
Shohag has an integer n n n and a set S S S of m m m unique integers. Help him find the lexicographically largest ∗ ^{\text{∗}} ∗ integer array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an such that a i ∈ S a_i \in S ai∈S for each 1 ≤ i ≤ n 1 \le i \le n 1≤i≤n and a gcd ( i , j ) ≠ gcd ( a i , a j ) a_{\operatorname{gcd}(i, j)} \neq \operatorname{gcd}(a_i, a_j) agcd(i,j)=gcd(ai,aj) † ^{\text{†}} † is satisfied over all pairs 1 ≤ i < j ≤ n 1 \le i \lt j \le n 1≤i<j≤n, or state that no such array exists.
∗ ^{\text{∗}} ∗An array a a a is lexicographically larger than an array b b b of the same length if a ≠ b a \ne b a=b, and in the first position where a a a and b b b differ, the array a a a has a larger element than the corresponding element in b b b.
† ^{\text{†}} † gcd ( x , y ) \gcd(x, y) gcd(x,y) denotes the greatest common divisor (GCD) of integers x x x and y y y.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first line of each test case contains two integers n n n and m m m ( 1 ≤ m ≤ n ≤ 1 0 5 1 \le m \le n \le 10^5 1≤m≤n≤105).
The second line contains m m m unique integers in increasing order, representing the elements of the set S S S ( 1 ≤ x ≤ n 1 \le x \le n 1≤x≤n for each x ∈ S x \in S x∈S).
It is guaranteed that the sum of n n n over all test cases does not exceed 3 ⋅ 1 0 5 3 \cdot 10^5 3⋅105.
Output
For each test case, if there is no solution print − 1 -1 −1, otherwise print n n n integers — the lexicographically largest integer array that satisfies the conditions.
Example
input |
---|
3 |
6 3 |
3 4 6 |
1 1 |
1 |
2 1 |
2 |
output |
---|
6 4 4 3 4 3 |
1 |
-1 |
Note
In the first test case, every element in the array belongs to the given set S = { 3 , 4 , 6 } S = \{3, 4, 6\} S={3,4,6}, and all pairs of indices of the array satisfy the necessary conditions. In particular, for pair ( 2 , 3 ) (2, 3) (2,3), a gcd ( 2 , 3 ) = a 1 = 6 a_{\operatorname{gcd}(2, 3)} = a_1 = 6 agcd(2,3)=a1=6 and gcd ( a 2 , a 3 ) = gcd ( 4 , 4 ) = 4 \operatorname{gcd}(a_2, a_3) = \operatorname{gcd}(4, 4) = 4 gcd(a2,a3)=gcd(4,4)=4, so they are not equal. There are other arrays that satisfy the conditions as well but this one is the lexicographically largest among them.
In the third test case, there is no solution possible because we are only allowed to use a = [ 2 , 2 ] a = [2, 2] a=[2,2] but for this array, for pair ( 1 , 2 ) (1, 2) (1,2), a gcd ( 1 , 2 ) = a 1 = 2 a_{\operatorname{gcd}(1, 2)} = a_1 = 2 agcd(1,2)=a1=2 and gcd ( a 1 , a 2 ) = gcd ( 2 , 2 ) = 2 \operatorname{gcd}(a_1, a_2) = \operatorname{gcd}(2, 2) = 2 gcd(a1,a2)=gcd(2,2)=2, so they are equal which is not allowed!
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, m;cin >> n >> m;std::vector<int> S(m);for (auto &v : S) cin >> v;std::vector<int> ans(n + 1, m - 1);for (int i = 1; i <= n; i ++)for (int j = i + i; j <= n; j += i)ans[j] = min(ans[j], ans[i] - 1);for (int i = 1; i <= n; i ++)if (ans[i] < 0) {cout << -1 << endl;return;}for (int i = 1; i <= n; i ++)cout << S[ans[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. Shohag Loves Inversions
Problem Statement
Shohag has an array a a a of integers. Initially a = [ 0 , 1 ] a = [0, 1] a=[0,1]. He can repeatedly perform the following operation any number of times:
- Let k k k be the number of inversions ∗ ^{\text{∗}} ∗ in the current array a a a.
- Insert k k k at any position in a a a, including the beginning or the end.
For example, if a = [ 4 , 6 , 2 , 4 ] a = [4, 6, 2, 4] a=[4,6,2,4], then the number of inversions is k = 3 k = 3 k=3. So Shohag can obtain the following arrays after the operation: [ 3 , 4 , 6 , 2 , 4 ] [\textbf{3}, 4, 6, 2, 4] [3,4,6,2,4], [ 4 , 3 , 6 , 2 , 4 ] [4, \textbf{3}, 6, 2, 4] [4,3,6,2,4], [ 4 , 6 , 3 , 2 , 4 ] [4, 6, \textbf{3}, 2, 4] [4,6,3,2,4], [ 4 , 6 , 2 , 3 , 4 ] [4, 6, 2, \textbf{3}, 4] [4,6,2,3,4], and [ 4 , 6 , 2 , 4 , 3 ] [4, 6, 2, 4, \textbf{3}] [4,6,2,4,3].
Given an integer n n n, help Shohag count, modulo 998 244 353 998\,244\,353 998244353, the number of distinct arrays of length n n n that can be obtained after performing the operations.
∗ ^{\text{∗}} ∗The number of inversions in an array a a a is the number of pairs of indices ( i i i, j j j) such that i < j i < j i<j and a i > a j a_i > a_j ai>aj.
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases.
The first and only line of each test case contains an integer n n n ( 2 ≤ n ≤ 1 0 6 2 \le n \le 10^6 2≤n≤106).
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 an integer — the number of possible arrays modulo 998 244 353 998\,244\,353 998244353.
Example
input |
---|
4 |
4 |
2 |
7 |
69 |
output |
---|
5 |
1 |
682 |
325188814 |
Note
In the first test case, the following 5 5 5 arrays can be obtained (the inserted inversion count is shown in bold):
- [ 0 , 1 ] → [ 0 , 0 , 1 ] → [ 0 , 0 , 1 , 0 ] [0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, 1, \textbf{0}] [0,1]→[0,0,1]→[0,0,1,0],
- [ 0 , 1 ] → [ 0 , 0 , 1 ] → [ 0 , 0 , 0 , 1 ] [0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, \textbf{0}, 1] [0,1]→[0,0,1]→[0,0,0,1],
- [ 0 , 1 ] → [ 0 , 1 , 0 ] → [ 0 , 1 , 0 , 1 ] [0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, 0, \textbf{1}] [0,1]→[0,1,0]→[0,1,0,1],
- [ 0 , 1 ] → [ 0 , 1 , 0 ] → [ 0 , 1 , 1 , 0 ] [0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, \textbf{1}, 0] [0,1]→[0,1,0]→[0,1,1,0],
- [ 0 , 1 ] → [ 0 , 1 , 0 ] → [ 1 , 0 , 1 , 0 ] [0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [\textbf{1}, 0, 1, 0] [0,1]→[0,1,0]→[1,0,1,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;cin >> n;std::vector<int> f(n + 1, 0);int mod = 998244353, sum = 0;for (int i = 3; i <= n; i ++) f[i] = 1;for (int i = 1; i <= n; i ++) {(f[i] += sum) %= mod;(sum += max(f[i] * i % mod - 1, 0ll)) %= mod;}int res = 1;for (int i = 1; i <= n; i ++) (res += f[i]) %= 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;
}
视频讲解
CodeTON Round 9 (Div. 1 + Div. 2, Rated, Prizes!)(A ~ E 题讲解)
最后祝大家早日