Skip to content

Commit b79ce22

Browse files
Add files via upload
1 parent fba0441 commit b79ce22

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://codeforces.com/contest/2082/problem/A
2+
// Max of the number of odd sum rows and odd sum columns
3+
4+
#include <iostream>
5+
#include <algorithm>
6+
#include <vector>
7+
#include <ranges>
8+
#include <print>
9+
10+
11+
int main() {
12+
int32_t tc;
13+
std::cin >> tc;
14+
while (tc--) {
15+
size_t h, w;
16+
std::cin >> h >> w;
17+
std::vector<int8_t> v(h * w, 0);
18+
for (auto &i : v) {
19+
char c;
20+
std::cin >> c;
21+
i = c == '1' ? 1 : 0;
22+
}
23+
24+
auto const rows = v | std::views::chunk(w);
25+
auto const num_odd_rows = std::ranges::count_if(rows, [](auto const &row) {
26+
return std::ranges::count(row, 1) % 2 == 1;
27+
});
28+
29+
auto const cols = std::views::iota(0u, w) | std::views::transform([&, w = w](auto const i) { return v | std::views::drop(i) | std::views::stride(w); });
30+
auto const num_odd_cols = std::ranges::count_if(cols, [](auto const &col) {
31+
return std::ranges::count(col, 1) % 2 == 1;
32+
});
33+
34+
std::println("{}", std::max(num_odd_rows, num_odd_cols));
35+
}
36+
return 0;
37+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <iostream>
4+
#include <print>
5+
6+
#define fast_cin() \
7+
std::ios_base::sync_with_stdio(false); \
8+
std::cin.tie(NULL); \
9+
std::cout.tie(NULL);
10+
11+
static int64_t apply_ceilings(int64_t x, int64_t ceils) {
12+
while (ceils-- && x > 1) {
13+
if (x & 1) {
14+
x = (x >> 1) + 1;
15+
} else {
16+
x >>= 1;
17+
}
18+
}
19+
return x;
20+
}
21+
22+
int main() {
23+
fast_cin();
24+
int32_t tc;
25+
std::cin >> tc;
26+
while (tc--) {
27+
int64_t x, n, m;
28+
std::cin >> x >> n >> m;
29+
n = std::clamp(n, static_cast<int64_t>(0), static_cast<int64_t>(32));
30+
m = std::clamp(m, static_cast<int64_t>(0), static_cast<int64_t>(32));
31+
32+
int64_t mini = apply_ceilings(x, m) >> n;
33+
int64_t maxi = apply_ceilings((x >> n), m);
34+
std::println("{} {}", mini, maxi);
35+
}
36+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/** CF2082 C - Math Division
2+
* we let dp(i) be the expected number of operations assuming we only need to deal with the prefix from the i-th least significant bit onwards,
3+
* assuming that the i-th least significant bit is now 1.
4+
*
5+
* When considering dp(i), either we do the ceil or the floor.
6+
* - If we do the floor, we need to skip forward to the next 1. For all the zeros between, it doesn't matter which operation we do.
7+
* - If we do the ceil, we need to skip forward to the next 0, and treat if as if it is a 1. All the 1s in between will turn into 0s so the operations don't matter.
8+
*
9+
* The base cases is when i=n-1 and i=n, which is 0 since the number will already be <= 1
10+
*
11+
* Time: O(n), Space: O(n)
12+
*/
13+
#pragma GCC optimize("Ofast")
14+
#pragma GCC optimize("unroll-loops")
15+
#include <algorithm>
16+
#include <array>
17+
#include <cassert>
18+
#include <iostream>
19+
#include <print>
20+
21+
using namespace std;
22+
23+
typedef long long ll;
24+
#define fast_cin() \
25+
ios_base::sync_with_stdio(false); \
26+
cin.tie(NULL); \
27+
cout.tie(NULL);
28+
29+
constexpr ll MOD = 1e9 + 7;
30+
struct mint {
31+
ll x;
32+
constexpr mint(ll xx) : x(xx) {}
33+
constexpr mint() : x(0) {}
34+
constexpr mint operator+(mint b) const { return mint((x + b.x) % MOD); }
35+
constexpr mint operator-(mint b) const { return mint((x - b.x + MOD) % MOD); }
36+
constexpr mint operator*(mint b) const { return mint((x * b.x) % MOD); }
37+
constexpr mint operator/(mint b) const { return *this * invert(b); }
38+
39+
constexpr static ll euclid(ll a, ll b, ll &x, ll &y) {
40+
if (!b) return x = 1, y = 0, a;
41+
ll d = euclid(b, a % b, y, x);
42+
return y -= a / b * x, d;
43+
}
44+
45+
constexpr static mint invert(mint a) {
46+
ll x, y, g = euclid(a.x, MOD, x, y);
47+
assert(g == 1);
48+
return mint((x + MOD) % MOD);
49+
}
50+
51+
constexpr mint operator^(ll e) const {
52+
if (!e) return mint(1);
53+
mint r = *this ^ (e / 2);
54+
r = r * r;
55+
return e & 1 ? *this * r : r;
56+
}
57+
};
58+
59+
int32_t main() {
60+
constexpr mint two_inv = mint::invert(2);
61+
constexpr int32_t MAX_N = 1e6 + 1;
62+
array<mint, MAX_N> memo;
63+
string s;
64+
65+
fast_cin();
66+
67+
int32_t tc;
68+
cin >> tc;
69+
while (tc--) {
70+
ssize_t n;
71+
cin >> n >> s;
72+
ranges::reverse(s);
73+
74+
ssize_t idx = ranges::find(s, '1') - s.begin();
75+
memo[n] = memo[n - 1] = 0;
76+
77+
int32_t current_zero = n;
78+
int32_t current_one = n - 1;
79+
for (ssize_t next_idx = n - 2; next_idx >= idx; next_idx--) {
80+
mint if_floor = memo[current_one] + current_one - next_idx;
81+
mint if_ceil = memo[current_zero] + current_zero - next_idx;
82+
memo[next_idx] = (if_floor + if_ceil) * two_inv;
83+
84+
if (s[next_idx] == '0')
85+
current_zero = next_idx;
86+
else
87+
current_one = next_idx;
88+
}
89+
println("{}", (memo[idx] + idx).x);
90+
}
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)