feat(codeforces/970): a-f
This commit is contained in:
parent
ae73b1baf1
commit
4094b4cd7b
28 changed files with 1061 additions and 1 deletions
9
codeforces/970/.clang-format
Normal file
9
codeforces/970/.clang-format
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
BasedOnStyle: Google
|
||||
AllowShortBlocksOnASingleLine: false
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortCompoundRequirementOnASingleLine: false
|
||||
AllowShortEnumsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: false
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLambdasOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
34
codeforces/970/.clangd
Normal file
34
codeforces/970/.clangd
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
CompileFlags:
|
||||
Add:
|
||||
-O2
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wformat=2
|
||||
-Wfloat-equal
|
||||
-Wlogical-op
|
||||
-Wshift-overflow=2
|
||||
-Wnon-virtual-dtor
|
||||
-Wold-style-cast
|
||||
-Wcast-qual
|
||||
-Wuseless-cast
|
||||
-Wno-sign-promotion
|
||||
-Wcast-align
|
||||
-Wunused
|
||||
-Woverloaded-virtual
|
||||
-Wconversion
|
||||
-Wsign-conversion
|
||||
-Wmisleading-indentation
|
||||
-Wduplicated-cond
|
||||
-Wduplicated-branches
|
||||
-Wlogical-op
|
||||
-Wnull-dereference
|
||||
-Wformat=2
|
||||
-Wformat-overflow
|
||||
-Wformat-truncation
|
||||
-Wdouble-promotion
|
||||
-Wundef
|
||||
-DLOCAL
|
||||
-std=c++20
|
||||
-Wno-unknown-pragmas
|
||||
87
codeforces/970/a.cc
Normal file
87
codeforces/970/a.cc
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using d64 = double;
|
||||
using d128 = long double;
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
template <typename T, size_t N>
|
||||
using arr = std::array<T, N>;
|
||||
template <typename T1, typename T2>
|
||||
using pai = std::pair<T1, T2>;
|
||||
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 ones, twos;
|
||||
cin >> ones >> twos;
|
||||
|
||||
for (u32 c1 = 0; c1 <= ones; ++c1) {
|
||||
for (u32 c2 = 0; c2 <= twos; ++c2) {
|
||||
if (c1 + c2 * 2 == (ones - c1) + (twos - c2) * 2) {
|
||||
YES();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
NO();
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
int tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (int t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
130
codeforces/970/b.cc
Normal file
130
codeforces/970/b.cc
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using d64 = double;
|
||||
using d128 = long double;
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
template <typename T, size_t N>
|
||||
using arr = std::array<T, N>;
|
||||
template <typename T1, typename T2>
|
||||
using pai = std::pair<T1, T2>;
|
||||
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
|
||||
static void NO() {
|
||||
std::cout << "No\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "Yes\n";
|
||||
}
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
string s;
|
||||
cin >> n >> s;
|
||||
|
||||
i64 cols = -1;
|
||||
for (u32 i = 0; i < n; ++i) {
|
||||
if (s[i] == '0') {
|
||||
cols = i - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (cols == -1) {
|
||||
if (n == 4)
|
||||
YES();
|
||||
else
|
||||
NO();
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE: didn't check it was square, holy shit throw
|
||||
if (cols == 0 || n % cols != 0 || cols * cols != n) {
|
||||
NO();
|
||||
return;
|
||||
}
|
||||
|
||||
vec<vec<char>> board(n / cols, vec<char>(cols));
|
||||
for (u32 i = 0; i < board.size(); ++i) {
|
||||
for (u32 j = 0; j < board[i].size(); ++j) {
|
||||
board[i][j] = s[i * cols + j];
|
||||
}
|
||||
}
|
||||
|
||||
u32 zeroes = 0;
|
||||
for (auto c : s)
|
||||
if (c == '0')
|
||||
++zeroes;
|
||||
for (u32 i = 0; i < cols; ++i) {
|
||||
if (s[i] == '0' || s[n - cols + i] == '0') {
|
||||
NO();
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (u32 i = 0; i < n; i += cols) {
|
||||
if (s[i] == '0' || s[i + cols - 1] == '0') {
|
||||
NO();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (n - 2 * cols - 2 * (n / cols) + 4 != zeroes) {
|
||||
NO();
|
||||
} else {
|
||||
YES();
|
||||
}
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
int tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (int t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
91
codeforces/970/c.cc
Normal file
91
codeforces/970/c.cc
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using d64 = double;
|
||||
using d128 = long double;
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
template <typename T, size_t N>
|
||||
using arr = std::array<T, N>;
|
||||
template <typename T1, typename T2>
|
||||
using pai = std::pair<T1, T2>;
|
||||
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
void solve() {
|
||||
i64 l, r;
|
||||
cin >> l >> r;
|
||||
|
||||
i64 lo = 0, hi = r - l + 1;
|
||||
|
||||
while (lo <= hi) {
|
||||
i64 mid = lo + (hi - lo) / 2;
|
||||
if (l + mid * (mid + 1) / 2 <= r) {
|
||||
lo = mid + 1;
|
||||
} else {
|
||||
hi = mid - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: hi is # elems ADDING; so also include l implciitly
|
||||
cout << hi + 1 << '\n';
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
int tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (int t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
31
codeforces/970/compile_flags.txt
Normal file
31
codeforces/970/compile_flags.txt
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
-O2
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wpedantic
|
||||
-Wshadow
|
||||
-Wformat=2
|
||||
-Wfloat-equal
|
||||
-Wlogical-op
|
||||
-Wshift-overflow=2
|
||||
-Wnon-virtual-dtor
|
||||
-Wold-style-cast
|
||||
-Wcast-qual
|
||||
-Wuseless-cast
|
||||
-Wno-sign-promotion
|
||||
-Wcast-align
|
||||
-Wunused
|
||||
-Woverloaded-virtual
|
||||
-Wconversion
|
||||
-Wsign-conversion
|
||||
-Wmisleading-indentation
|
||||
-Wduplicated-cond
|
||||
-Wduplicated-branches
|
||||
-Wlogical-op
|
||||
-Wnull-dereference
|
||||
-Wformat=2
|
||||
-Wformat-overflow
|
||||
-Wformat-truncation
|
||||
-Wdouble-promotion
|
||||
-Wundef
|
||||
-DLOCAL
|
||||
-std=c++23
|
||||
163
codeforces/970/d.cc
Normal file
163
codeforces/970/d.cc
Normal file
|
|
@ -0,0 +1,163 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using d64 = double;
|
||||
using d128 = long double;
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
template <typename T, size_t N>
|
||||
using arr = std::array<T, N>;
|
||||
template <typename T1, typename T2>
|
||||
using pai = std::pair<T1, T2>;
|
||||
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
bitset<2 * 100000 + 1> black;
|
||||
|
||||
void solve() {
|
||||
black.reset();
|
||||
|
||||
u32 n;
|
||||
cin >> n;
|
||||
vec<u32> a(n);
|
||||
for (auto& e : a) {
|
||||
cin >> e;
|
||||
--e;
|
||||
}
|
||||
|
||||
char c;
|
||||
for (u32 i = 0; i < n; ++i) {
|
||||
cin >> c;
|
||||
if (c == '1')
|
||||
black[i] = true;
|
||||
}
|
||||
|
||||
vec<int> cycle_id(n, -1);
|
||||
vec<int> cycle_pos(n, -1);
|
||||
vec<bool> visited(n, false);
|
||||
vec<int> black_count(n, 0);
|
||||
|
||||
for (u32 start = 0; start < n; ++start) {
|
||||
if (visited[start])
|
||||
continue;
|
||||
|
||||
u32 slow = start;
|
||||
u32 fast = start;
|
||||
|
||||
do {
|
||||
slow = a[slow];
|
||||
fast = a[a[fast]];
|
||||
} while (slow != fast);
|
||||
|
||||
u32 cycle_start = slow;
|
||||
int cycle_length = 1;
|
||||
slow = a[slow];
|
||||
|
||||
while (slow != cycle_start) {
|
||||
cycle_length++;
|
||||
slow = a[slow];
|
||||
}
|
||||
|
||||
slow = cycle_start;
|
||||
int cycle_black = 0;
|
||||
for (int i = 0; i < cycle_length; ++i) {
|
||||
cycle_id[slow] = start;
|
||||
cycle_pos[slow] = i;
|
||||
visited[slow] = true;
|
||||
|
||||
if (black[slow])
|
||||
cycle_black++;
|
||||
|
||||
slow = a[slow];
|
||||
}
|
||||
|
||||
black_count[start] = cycle_black;
|
||||
}
|
||||
|
||||
vec<int> answer(n, 0);
|
||||
|
||||
for (u32 i = 0; i < n; ++i) {
|
||||
if (cycle_id[i] != -1) {
|
||||
answer[i] = black_count[cycle_id[i]];
|
||||
} else {
|
||||
vec<u32> path;
|
||||
u32 curr = i;
|
||||
|
||||
while (cycle_id[curr] == -1) {
|
||||
path.push_back(curr);
|
||||
curr = a[curr];
|
||||
}
|
||||
|
||||
for (u32 node : path) {
|
||||
if (black[node])
|
||||
answer[i]++;
|
||||
}
|
||||
|
||||
answer[i] += black_count[cycle_id[curr]];
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 i = 0; i < n; ++i) {
|
||||
cout << answer[i] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
int tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (int t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
12
codeforces/970/debug_flags.txt
Normal file
12
codeforces/970/debug_flags.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-g3
|
||||
-fsanitize=address,undefined
|
||||
-fsanitize=float-divide-by-zero
|
||||
-fsanitize=float-cast-overflow
|
||||
-fno-sanitize-recover=all
|
||||
-fstack-protector-all
|
||||
-fstack-usage
|
||||
-fno-omit-frame-pointer
|
||||
-fno-inline
|
||||
-ffunction-sections
|
||||
-D_GLIBCXX_DEBUG
|
||||
-D_GLIBCXX_DEBUG_PEDANTIC
|
||||
122
codeforces/970/e.cc
Normal file
122
codeforces/970/e.cc
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
#include <bits/stdc++.h> // {{{
|
||||
|
||||
// https://codeforces.com/blog/entry/96344
|
||||
|
||||
#pragma GCC optimize("O2,unroll-loops")
|
||||
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
|
||||
|
||||
using namespace std;
|
||||
|
||||
using i32 = int32_t;
|
||||
using u32 = uint32_t;
|
||||
using i64 = int64_t;
|
||||
using u64 = uint64_t;
|
||||
using d64 = double;
|
||||
using d128 = long double;
|
||||
template <typename T>
|
||||
using vec = std::vector<T>;
|
||||
template <typename T, size_t N>
|
||||
using arr = std::array<T, N>;
|
||||
template <typename T1, typename T2>
|
||||
using pai = std::pair<T1, T2>;
|
||||
|
||||
template <typename T>
|
||||
constexpr T MIN = std::numeric_limits<T>::min();
|
||||
|
||||
template <typename T>
|
||||
constexpr T MAX = std::numeric_limits<T>::max();
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sc(auto&& x) {
|
||||
return static_cast<T>(x);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
[[nodiscard]] static T sz(auto&& x) {
|
||||
return static_cast<T>(x.size());
|
||||
}
|
||||
|
||||
static void NO() {
|
||||
std::cout << "NO\n";
|
||||
}
|
||||
|
||||
static void YES() {
|
||||
std::cout << "YES\n";
|
||||
}
|
||||
|
||||
#define all(x) (x).begin(), (x).end()
|
||||
#define rall(x) (x).rbegin(), (x).rend()
|
||||
|
||||
#ifdef LOCAL
|
||||
#define db(...) std::print(__VA_ARGS__)
|
||||
#define dbln(...) std::println(__VA_ARGS__)
|
||||
#else
|
||||
#define db(...)
|
||||
#define dbln(...)
|
||||
#endif
|
||||
// }}}
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
void solve() {
|
||||
u32 n;
|
||||
cin >> n;
|
||||
string s;
|
||||
cin >> s;
|
||||
int ans = s.size();
|
||||
|
||||
if (n & 1) {
|
||||
vector<int> pref[2] = {vector<int>(26), vector<int>(26)};
|
||||
vector<int> suf[2] = {vector<int>(26), vector<int>(26)};
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
suf[i % 2][s[i] - 'a']++;
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
suf[i % 2][s[i] - 'a']--;
|
||||
int odd = n;
|
||||
|
||||
for (int k = 0; k < 2; k++) {
|
||||
int best = 0;
|
||||
for (int j = 0; j < 26; j++) {
|
||||
best = max(best, suf[1 - k][j] + pref[k][j]);
|
||||
}
|
||||
odd -= best;
|
||||
}
|
||||
|
||||
ans = min(ans, odd);
|
||||
pref[i % 2][s[i] - 'a']++;
|
||||
}
|
||||
} else {
|
||||
vector<int> v[2] = {vector<int>(26), vector<int>(26)};
|
||||
for (int i = 0; i < n; i++) {
|
||||
v[i % 2][s[i] - 'a']++;
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
int best = 0;
|
||||
for (int j = 0; j < 26; j++) {
|
||||
best = max(best, v[i][j]);
|
||||
}
|
||||
ans -= best;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main() { // {{{
|
||||
cin.tie(nullptr)->sync_with_stdio(false);
|
||||
cin.exceptions(cin.failbit);
|
||||
|
||||
int tc = 1;
|
||||
cin >> tc;
|
||||
|
||||
for (int t = 0; t < tc; ++t) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
// }}}
|
||||
58
codeforces/970/f.cc
Normal file
58
codeforces/970/f.cc
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
using i64 = long long;
|
||||
constexpr i64 MOD = 1e9 + 7;
|
||||
|
||||
i64 modinv(i64 a, i64 m) {
|
||||
i64 m0 = m, y = 0, x = 1;
|
||||
if (m == 1)
|
||||
return 0;
|
||||
while (a > 1) {
|
||||
i64 q = a / m;
|
||||
i64 t = m;
|
||||
m = a % m, a = t;
|
||||
t = y;
|
||||
y = x - q * y;
|
||||
x = t;
|
||||
}
|
||||
if (x < 0)
|
||||
x += m0;
|
||||
return x;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
int n;
|
||||
cin >> n;
|
||||
vector<i64> a(n);
|
||||
for (auto &x : a)
|
||||
cin >> x;
|
||||
|
||||
i64 total_sum = 0, sum_sq = 0;
|
||||
for (auto x : a) {
|
||||
total_sum = (total_sum + x) % MOD;
|
||||
sum_sq = (sum_sq + x * x) % MOD;
|
||||
}
|
||||
|
||||
i64 sum_pairs = (total_sum * total_sum % MOD - sum_sq + MOD) % MOD;
|
||||
sum_pairs = sum_pairs * modinv(2, MOD) % MOD;
|
||||
|
||||
i64 total_pairs = (i64)n * (n - 1) / 2 % MOD;
|
||||
i64 inv_total_pairs = modinv(total_pairs, MOD);
|
||||
i64 ans = sum_pairs * inv_total_pairs % MOD;
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
6
codeforces/970/io/a.in
Normal file
6
codeforces/970/io/a.in
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
5
|
||||
0 1
|
||||
0 3
|
||||
2 0
|
||||
2 3
|
||||
3 1
|
||||
8
codeforces/970/io/a.out
Normal file
8
codeforces/970/io/a.out
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
NO
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
|
||||
[code]: 0
|
||||
[time]: 10.9363 ms
|
||||
11
codeforces/970/io/b.in
Normal file
11
codeforces/970/io/b.in
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
5
|
||||
2
|
||||
11
|
||||
4
|
||||
1111
|
||||
9
|
||||
111101111
|
||||
9
|
||||
111111111
|
||||
12
|
||||
111110011111
|
||||
8
codeforces/970/io/b.out
Normal file
8
codeforces/970/io/b.out
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
No
|
||||
Yes
|
||||
Yes
|
||||
No
|
||||
No
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.70805 ms
|
||||
6
codeforces/970/io/c.in
Normal file
6
codeforces/970/io/c.in
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
5
|
||||
1 2
|
||||
1 5
|
||||
2 2
|
||||
10 20
|
||||
1 1000000000
|
||||
8
codeforces/970/io/c.out
Normal file
8
codeforces/970/io/c.out
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
2
|
||||
3
|
||||
1
|
||||
5
|
||||
44721
|
||||
|
||||
[code]: 0
|
||||
[time]: 3.74532 ms
|
||||
16
codeforces/970/io/d.in
Normal file
16
codeforces/970/io/d.in
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
5
|
||||
1
|
||||
1
|
||||
0
|
||||
5
|
||||
1 2 4 5 3
|
||||
10101
|
||||
5
|
||||
5 4 1 3 2
|
||||
10011
|
||||
6
|
||||
3 5 6 1 2 4
|
||||
010000
|
||||
6
|
||||
1 2 3 4 5 6
|
||||
100110
|
||||
8
codeforces/970/io/d.out
Normal file
8
codeforces/970/io/d.out
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
0
|
||||
1 0 2 2 2
|
||||
3 3 3 3 3
|
||||
0 1 0 0 1 0
|
||||
1 0 0 1 1 0
|
||||
|
||||
[code]: 0
|
||||
[time]: 3.73864 ms
|
||||
21
codeforces/970/io/e.in
Normal file
21
codeforces/970/io/e.in
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
10
|
||||
1
|
||||
a
|
||||
2
|
||||
ca
|
||||
3
|
||||
aab
|
||||
5
|
||||
ababa
|
||||
6
|
||||
acdada
|
||||
9
|
||||
ejibmyyju
|
||||
6
|
||||
bbccbc
|
||||
6
|
||||
abacba
|
||||
5
|
||||
bcbca
|
||||
5
|
||||
dcbdb
|
||||
13
codeforces/970/io/e.out
Normal file
13
codeforces/970/io/e.out
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
1
|
||||
0
|
||||
1
|
||||
1
|
||||
2
|
||||
6
|
||||
2
|
||||
3
|
||||
1
|
||||
1
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.22215 ms
|
||||
7
codeforces/970/io/f.in
Normal file
7
codeforces/970/io/f.in
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
3
|
||||
3
|
||||
3 2 3
|
||||
4
|
||||
2 2 2 4
|
||||
5
|
||||
1 2 3 4 5
|
||||
6
codeforces/970/io/f.out
Normal file
6
codeforces/970/io/f.out
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
7
|
||||
6
|
||||
500000012
|
||||
|
||||
[code]: 0
|
||||
[time]: 4.27008 ms
|
||||
28
codeforces/970/makefile
Normal file
28
codeforces/970/makefile
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
.PHONY: run debug clean setup init
|
||||
|
||||
SRC = $(word 2,$(MAKECMDGOALS))
|
||||
|
||||
.SILENT:
|
||||
|
||||
run:
|
||||
sh scripts/run.sh $(SRC)
|
||||
|
||||
debug:
|
||||
sh scripts/debug.sh $(SRC)
|
||||
|
||||
clean:
|
||||
rm -rf build/*
|
||||
|
||||
setup:
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
test -d scripts || mkdir -p scripts
|
||||
test -f compile_flags.txt || cp $(HOME)/.config/cp-template/compile_flags.txt .
|
||||
test -f .clangd || cp $(HOME)/.config/cp-template/.clangd .
|
||||
test -f .clang-format || cp $(HOME)/.config/cp-template/.clang-format .
|
||||
|
||||
init:
|
||||
make setup
|
||||
|
||||
%:
|
||||
@:
|
||||
66
codeforces/970/new
Normal file
66
codeforces/970/new
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
void solve() {
|
||||
u32 n;
|
||||
string s;
|
||||
cin >> n >> s;
|
||||
|
||||
// Function to calculate minimum ops for alternating even-length string
|
||||
auto calc_min_ops = [](const vec<u32>& even_count, const vec<u32>& odd_count,
|
||||
u32 even_positions, u32 odd_positions) -> u32 {
|
||||
// Find most frequent characters in each position type
|
||||
pai<u32, u32> max_even = {0, 0}; // (char, count)
|
||||
pai<u32, u32> second_max_even = {0, 0};
|
||||
pai<u32, u32> max_odd = {0, 0};
|
||||
pai<u32, u32> second_max_odd = {0, 0};
|
||||
|
||||
for (u32 c = 0; c < 26; ++c) {
|
||||
if (even_count[c] > max_even.second) {
|
||||
second_max_even = max_even;
|
||||
max_even = {c, even_count[c]};
|
||||
} else if (even_count[c] > second_max_even.second) {
|
||||
second_max_even = {c, even_count[c]};
|
||||
}
|
||||
|
||||
if (odd_count[c] > max_odd.second) {
|
||||
second_max_odd = max_odd;
|
||||
max_odd = {c, odd_count[c]};
|
||||
} else if (odd_count[c] > second_max_odd.second) {
|
||||
second_max_odd = {c, odd_count[c]};
|
||||
}
|
||||
}
|
||||
|
||||
u32 option1 =
|
||||
(even_positions - max_even.second) + (odd_positions - max_odd.second);
|
||||
u32 option2 = MAX<u32>;
|
||||
if (max_even.first != max_odd.first) {
|
||||
option2 = (even_positions - even_count[max_odd.first]) +
|
||||
(odd_positions - odd_count[max_even.first]);
|
||||
} else {
|
||||
// Try second-best for one position type
|
||||
u32 sub_option1 = (even_positions - second_max_even.second) +
|
||||
(odd_positions - max_odd.second);
|
||||
u32 sub_option2 = (even_positions - max_even.second) +
|
||||
(odd_positions - second_max_odd.second);
|
||||
option2 = min(sub_option1, sub_option2);
|
||||
}
|
||||
|
||||
return min(option1, option2);
|
||||
};
|
||||
|
||||
if (n % 2 == 0) {
|
||||
// Even length case
|
||||
vec<u32> even_count(26, 0);
|
||||
vec<u32> odd_count(26, 0);
|
||||
|
||||
for (u32 i = 0; i < n; ++i) {
|
||||
if (i % 2 == 0) {
|
||||
even_count[s[i] - 'a']++;
|
||||
} else {
|
||||
odd_count[s[i] - 'a']++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << calc_min_ops(even_count, odd_count, n / 2, n / 2) << '\n';
|
||||
} else {
|
||||
// O(n) - use suffix?
|
||||
}
|
||||
}
|
||||
29
codeforces/970/scripts/debug.sh
Normal file
29
codeforces/970/scripts/debug.sh
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./scripts/utils.sh
|
||||
|
||||
SRC="$1"
|
||||
BASE=$(basename "$SRC" .cc)
|
||||
INPUT="${BASE}.in"
|
||||
OUTPUT="${BASE}.out"
|
||||
DBG_BIN="${BASE}.debug"
|
||||
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
|
||||
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
|
||||
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
|
||||
|
||||
test -f "io/$INPUT" || touch "io/$INPUT"
|
||||
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
|
||||
|
||||
INPUT="io/$INPUT"
|
||||
OUTPUT="io/$OUTPUT"
|
||||
DBG_BIN="build/$DBG_BIN"
|
||||
|
||||
compile_source "$SRC" "$DBG_BIN" "$OUTPUT" @debug_flags.txt
|
||||
CODE=$?
|
||||
test $CODE -gt 0 && exit $CODE
|
||||
|
||||
execute_binary "$DBG_BIN" "$INPUT" "$OUTPUT"
|
||||
exit $?
|
||||
29
codeforces/970/scripts/run.sh
Normal file
29
codeforces/970/scripts/run.sh
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/bin/sh
|
||||
|
||||
. ./scripts/utils.sh
|
||||
|
||||
SRC="$1"
|
||||
BASE=$(basename "$SRC" .cc)
|
||||
INPUT="${BASE}.in"
|
||||
OUTPUT="${BASE}.out"
|
||||
RUN_BIN="${BASE}.run"
|
||||
|
||||
test -d build || mkdir -p build
|
||||
test -d io || mkdir -p io
|
||||
|
||||
test -f "$INPUT" && test ! -f "io/$INPUT" && mv "$INPUT" "io/"
|
||||
test -f "$OUTPUT" && test ! -f "io/$OUTPUT" && mv "$OUTPUT" "io/"
|
||||
|
||||
test -f "io/$INPUT" || touch "io/$INPUT"
|
||||
test -f "io/$OUTPUT" || touch "io/$OUTPUT"
|
||||
|
||||
INPUT="io/$INPUT"
|
||||
OUTPUT="io/$OUTPUT"
|
||||
RUN_BIN="build/$RUN_BIN"
|
||||
|
||||
compile_source "$SRC" "$RUN_BIN" "$OUTPUT" ""
|
||||
CODE=$?
|
||||
test $CODE -gt 0 && exit $CODE
|
||||
|
||||
execute_binary "$RUN_BIN" "$INPUT" "$OUTPUT"
|
||||
exit $?
|
||||
53
codeforces/970/scripts/utils.sh
Normal file
53
codeforces/970/scripts/utils.sh
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#!/bin/sh
|
||||
|
||||
execute_binary() {
|
||||
binary="$1"
|
||||
input="$2"
|
||||
output="$3"
|
||||
|
||||
start=$(date '+%s.%N')
|
||||
timeout 2s ./"$binary" <"$input" >"$output" 2>&1
|
||||
CODE=$?
|
||||
end=$(date '+%s.%N')
|
||||
truncate -s "$(head -n 1000 "$output" | wc -c)" "$output"
|
||||
|
||||
if [ $CODE -ge 124 ]; then
|
||||
MSG=''
|
||||
case $CODE in
|
||||
124) MSG='TIMEOUT' ;;
|
||||
128) MSG='SIGILL' ;;
|
||||
130) MSG='SIGABRT' ;;
|
||||
131) MSG='SIGBUS' ;;
|
||||
136) MSG='SIGFPE' ;;
|
||||
135) MSG='SIGSEGV' ;;
|
||||
137) MSG='SIGPIPE' ;;
|
||||
139) MSG='SIGTERM' ;;
|
||||
esac
|
||||
[ $CODE -ne 124 ] && sed -i '$d' "$output"
|
||||
test -n "$MSG" && printf '\n[code]: %s (%s)' "$CODE" "$MSG" >>"$output"
|
||||
else
|
||||
printf '\n[code]: %s' "$CODE" >>"$output"
|
||||
fi
|
||||
|
||||
printf '\n[time]: %s ms' "$(awk "BEGIN {print ($end - $start) * 1000}")" >>$output
|
||||
return $CODE
|
||||
}
|
||||
|
||||
compile_source() {
|
||||
src="$1"
|
||||
bin="$2"
|
||||
output="$3"
|
||||
flags="$4"
|
||||
|
||||
test -f "$bin" && rm "$bin" || true
|
||||
g++ @compile_flags.txt $flags "$src" -o "$bin" 2>"$output"
|
||||
CODE=$?
|
||||
|
||||
if [ $CODE -gt 0 ]; then
|
||||
printf '\n[code]: %s' "$CODE" >>"$output"
|
||||
return $CODE
|
||||
else
|
||||
echo '' >"$output"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue