Skip to content

Commit a1306b2

Browse files
Add files via upload
1 parent 3e7b088 commit a1306b2

7 files changed

+306
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
for _ in range(int(input())):
2+
print("YES" if int(input()) % 2 == 1 else "NO")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
for _ in range(int(input())):
2+
s = list(input())
3+
n = len(s)
4+
x = 0
5+
# number of significant zeros
6+
for i in range(n - 1, -1, -1):
7+
if s[i] != "0":
8+
break
9+
else:
10+
x += 1
11+
12+
print(len(s) - s.count("0") + x - 1)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def is_prime(n):
2+
"""Check if a number is a prime number."""
3+
if n <= 1:
4+
return False
5+
for i in range(2, int(n**0.5) + 1):
6+
if n % i == 0:
7+
return False
8+
return True
9+
10+
11+
for _ in range(int(input())):
12+
n, k = map(int, input().split())
13+
14+
# edge cases
15+
if n == 1:
16+
if k == 2:
17+
print("YES")
18+
continue
19+
20+
if k > 1:
21+
print("NO")
22+
continue
23+
elif is_prime(n):
24+
print("YES")
25+
continue
26+
else:
27+
print("NO")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
def coord_to_num(x, y, n):
2+
# print(f"coord_to_num({x}, {y}, {n})")
3+
if n == 0:
4+
assert x == y == 1
5+
return 1
6+
7+
is_top = x <= (1 << (n - 1))
8+
is_left = y <= (1 << (n - 1))
9+
10+
if is_top and is_left:
11+
return coord_to_num(x, y, n - 1)
12+
elif not is_top and not is_left:
13+
return coord_to_num(x - (1 << (n - 1)), y - (1 << (n - 1)), n - 1) + (1 << (2*n - 2))
14+
elif not is_top and is_left:
15+
return coord_to_num(x - (1 << (n - 1)), y, n - 1) + 2 * (1 << (2*n - 2))
16+
else:
17+
return coord_to_num(x, y - (1 << (n - 1)), n - 1) + 3 * (1 << (2*n - 2))
18+
19+
def num_to_coord(num, n) -> tuple[int, int]:
20+
if n == 0:
21+
assert num == 1
22+
return (1, 1)
23+
24+
if num <= (1<<(2*n - 2)):
25+
return num_to_coord(num, n - 1)
26+
elif num <= 2 * (1 << (2*n - 2)):
27+
x, y = num_to_coord(num - (1 << (2*n - 2)), n - 1)
28+
return (x + (1 << (n - 1)), y + (1 << (n - 1)))
29+
elif num <= 3 * (1 << (2*n - 2)):
30+
x, y = num_to_coord(num - 2 * (1 << (2*n - 2)), n - 1)
31+
return (x + (1 << (n - 1)), y)
32+
else:
33+
x, y = num_to_coord(num - 3 * (1 << (2*n - 2)), n - 1)
34+
return (x, y + (1 << (n - 1)))
35+
36+
for _ in range(int(input())):
37+
n = int(input())
38+
q = int(input())
39+
40+
for _ in range(q):
41+
op, *x = input().split()
42+
43+
if op == "->":
44+
x, y = map(int, x)
45+
print(coord_to_num(x, y, n))
46+
elif op == "<-":
47+
x = int(x[0])
48+
print(num_to_coord(x, n)[0], num_to_coord(x, n)[1])
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
int n, k;
14+
vector<int> a;
15+
16+
bool cando(int x) {
17+
// can we achieve k segments with all mex >= x?
18+
vector<int> achieved_by_segment(n + 2, -1);
19+
int idx = 0;
20+
for (int i = 0; i < k; i++) {
21+
int mex = 0;
22+
while (idx < n && mex < x) {
23+
if (achieved_by_segment[a[idx]] < i) {
24+
achieved_by_segment[a[idx]] = i;
25+
26+
while (mex < x && achieved_by_segment[mex] == i) {
27+
mex++;
28+
}
29+
}
30+
idx++;
31+
}
32+
33+
if (mex < x) {
34+
return false;
35+
}
36+
}
37+
// if we reach here, we can achieve k segments with all mex >= x
38+
return true;
39+
}
40+
int main() {
41+
int tc;
42+
cin >> tc;
43+
while (tc--) {
44+
cin >> n >> k;
45+
a.resize(n);
46+
for (auto& x : a) {
47+
cin >> x;
48+
if (x > n + 1) x = n + 1;
49+
}
50+
51+
ll hi = 1e9;
52+
ll lo = 0;
53+
while (lo < hi) {
54+
int mid = (hi + lo + 1) / 2;
55+
if (cando(mid)) {
56+
lo = mid;
57+
} else {
58+
hi = mid - 1;
59+
}
60+
}
61+
62+
cout << lo << "\n";
63+
}
64+
return 0;
65+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
typedef long long ll;
7+
typedef vector<int> vi;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
int n, m;
14+
vector<string> a;
15+
vector<vector<string>> b;
16+
int main() {
17+
int tc;
18+
cin >> tc;
19+
while (tc--) {
20+
cin >> n >> m;
21+
a.resize(n);
22+
b.assign(m, vector<string>(n));
23+
for (int i = 0; i < n; i++) {
24+
cin >> a[i];
25+
}
26+
for (int j = 0; j < m; j++) {
27+
for (int i = 0; i < n; i++) {
28+
cin >> b[j][i];
29+
}
30+
}
31+
32+
// check that for each i, there is a j such that a[i] = b[j][i]
33+
// if not, return -1
34+
bool all_there = true;
35+
for (int i = 0; i < n; i++) {
36+
bool found = false;
37+
for (int j = 0; j < m; j++) {
38+
if (a[i] == b[j][i]) {
39+
found = true;
40+
break;
41+
}
42+
}
43+
if (!found) {
44+
all_there = false;
45+
break;
46+
}
47+
}
48+
if (!all_there) {
49+
cout << -1 << endl;
50+
continue;
51+
}
52+
53+
// find the j with the most common elements with a[i]
54+
int max_count = 0;
55+
int max_j = -1;
56+
for (int j = 0; j < m; j++) {
57+
int count = 0;
58+
for (int i = 0; i < n; i++) {
59+
if (a[i] == b[j][i]) {
60+
count++;
61+
}
62+
}
63+
if (count > max_count) {
64+
max_count = count;
65+
max_j = j;
66+
}
67+
}
68+
69+
int manually_fill = n - max_count;
70+
cout << n + 2 * manually_fill << endl;
71+
}
72+
73+
return 0;
74+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// CF2093G
2+
// Line sweep, maintaining 32 sets of last seen hash_maps based on masked versions of the array elements.
3+
// Time: O(n log n), Space: O(n)
4+
#pragma GCC optimize("Ofast")
5+
#pragma GCC optimize("unroll-loops")
6+
#include <bits/stdc++.h>
7+
using namespace std;
8+
9+
int n, k;
10+
vector<int> a;
11+
int main() {
12+
int tc;
13+
cin >> tc;
14+
while (tc--) {
15+
cin >> n >> k;
16+
a.resize(n);
17+
for (auto& x : a) {
18+
cin >> x;
19+
}
20+
21+
// check single
22+
if (k == 0) {
23+
cout << 1 << endl;
24+
continue;
25+
}
26+
27+
// check adjacent
28+
bool found_shortcut = false;
29+
for (int i = 0; i < n - 1; i++) {
30+
if ((a[i] ^ a[i + 1]) >= k) {
31+
found_shortcut = true;
32+
break;
33+
}
34+
}
35+
if (found_shortcut) {
36+
cout << 2 << endl;
37+
continue;
38+
}
39+
40+
int best = 1e9;
41+
unordered_map<int, int> last_seen[32]; // last_seen[i][j] is the last seen index of j (masking out the least significant i bits)
42+
for (int i = 0; i < n; i++) {
43+
int looking_for = 0;
44+
for (int j = 31; j >= 0; j--) {
45+
if ((1 << j) & k) { // must follow along differently
46+
looking_for = looking_for | ((1 << j) ^ ((1 << j) & a[i]));
47+
} else { // we can try to find someone who has a different version at this point
48+
looking_for = looking_for | ((1 << j) ^ ((1 << j) & a[i]));
49+
if (last_seen[j].contains(looking_for)) {
50+
int idx = last_seen[j][looking_for];
51+
best = min(best, i - idx + 1);
52+
}
53+
// otherwise assume this bit will be the same
54+
looking_for = looking_for ^ (1 << j);
55+
}
56+
}
57+
58+
// also check non-masked
59+
if (last_seen[0].contains(looking_for)) {
60+
int idx = last_seen[0][looking_for];
61+
best = min(best, i - idx + 1);
62+
}
63+
64+
int mask = ~0;
65+
for (int j = 0; j < 32; j++) {
66+
last_seen[j][a[i] & mask] = i;
67+
mask = mask ^ (1 << j);
68+
}
69+
}
70+
71+
if (best == 1e9) {
72+
cout << -1 << endl;
73+
} else {
74+
cout << best << endl;
75+
}
76+
}
77+
return 0;
78+
}

0 commit comments

Comments
 (0)