backup
This commit is contained in:
parent
1e245f68c3
commit
0d37fb416d
38
src/bin/a.cc
38
src/bin/a.cc
|
@ -182,6 +182,22 @@ ll inverse(ll a, ll b) {
|
|||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
|
@ -241,21 +257,21 @@ void dump() {}
|
|||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n, m, k);
|
||||
readvec(int, a, n);
|
||||
readvec(int, b, m);
|
||||
sort(a.begin(), a.end());
|
||||
sort(b.begin(), b.end());
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int j = -1;
|
||||
while (j + 1 < m && b[j + 1] + a[i] <= k) ++j;
|
||||
res += j + 1;
|
||||
read(int, n);
|
||||
cout << "0 ";
|
||||
for (int i = 1; i <= (n - 1) / 2; ++i) {
|
||||
cout << i << ' ' << -i << ' ';
|
||||
}
|
||||
cout << res << endl;
|
||||
if (n % 2 == 0) {
|
||||
cout << (n - 1) / 2 + 1;
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
|
|
BIN
src/bin/a.out
BIN
src/bin/a.out
Binary file not shown.
59
src/bin/b.cc
59
src/bin/b.cc
|
@ -1,4 +1,3 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
|
@ -182,6 +181,22 @@ ll inverse(ll a, ll b) {
|
|||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
|
@ -242,23 +257,43 @@ void prep() {}
|
|||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(ll, a, n);
|
||||
for (int i = 1; i < n - 1; ++i) {
|
||||
if (a[i - 1] < 0) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
readvec(ll, a, n + 1);
|
||||
vector<ll> ps(n + 2);
|
||||
for (int i = 1; i <= n + 1; ++i) {
|
||||
ps[i] = ps[i - 1] ^ a[i - 1];
|
||||
}
|
||||
vector<int> res(n + 2);
|
||||
for (int i = 0; i <= n; ++i) {
|
||||
if (ps[i] == a[i]) {
|
||||
res[max(0, n - i - 1)] += 1;
|
||||
}
|
||||
a[i] -= 2 * a[i - 1];
|
||||
a[i + 1] -= a[i - 1];
|
||||
}
|
||||
if (a[n - 1] != 0 || a[n - 2] != 0) {
|
||||
cout << "NO\n";
|
||||
} else {
|
||||
cout << "YES\n";
|
||||
int ptr = 0;
|
||||
while (ptr <= n + 1 && res[ptr] == 2) {
|
||||
res[ptr] = 0;
|
||||
res[++ptr] += 1;
|
||||
}
|
||||
int lo = 0;
|
||||
for (int i = n + 1; ~i; --i) {
|
||||
if (res[i] == 0) {
|
||||
if (lo == 0) {
|
||||
;;
|
||||
} else {
|
||||
cout << 0;
|
||||
}
|
||||
} else {
|
||||
lo = 1;
|
||||
cout << 1;
|
||||
}
|
||||
}
|
||||
if (lo == 0) cout << 0;
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
|
|
76
src/bin/c.cc
76
src/bin/c.cc
|
@ -1,4 +1,3 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
|
@ -182,6 +181,22 @@ ll inverse(ll a, ll b) {
|
|||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
|
@ -233,25 +248,66 @@ int period(string s) { // find the length of shortest recurring period
|
|||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(string, a);
|
||||
int res = 0;
|
||||
for (int i = 2; i < n; ++i) {
|
||||
if (a[i - 2] == 'p' && a[i - 1] == 'i' && a[i] == 'e') res += 1;
|
||||
if (a[i - 2] == 'm' && a[i - 1] == 'a' && a[i] == 'p') res += 1, a[i] = 'x';
|
||||
template<typename T>
|
||||
struct BIT {
|
||||
int n;
|
||||
vector<T> c;
|
||||
BIT(size_t n) : n(n), c(n + 1) {}
|
||||
void add(size_t i, const T& k) {
|
||||
while (i <= n) {
|
||||
c[i] += k;
|
||||
i += lowbit(i);
|
||||
}
|
||||
}
|
||||
T getsum(size_t i) {
|
||||
T res = {};
|
||||
while (i) {
|
||||
res += c[i];
|
||||
i -= lowbit(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
void solve() {
|
||||
read(int, n, q);
|
||||
BIT<int> tr(n);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
read(int, x);
|
||||
tr.add(i, x);
|
||||
}
|
||||
while (q--) {
|
||||
read(int, x, p);
|
||||
tr.add(x, ll(p) - tr.getsum(x) + tr.getsum(x - 1));
|
||||
int l = 0, r = lg2(n);
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (tr.getsum(n - (1 << mid)) == 0) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
// debug(l);
|
||||
if (tr.getsum(n - (1 << l)) != 0) {
|
||||
cout << (1 << l + 1) << '\n';
|
||||
} else {
|
||||
cout << (1 << l) << '\n';
|
||||
}
|
||||
}
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
|
|
|
@ -0,0 +1,292 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(int, a, n);
|
||||
sort(a.begin(), a.end());
|
||||
ll res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
res += abs(a[i] - i);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,314 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(ll, a, n);
|
||||
vector<bool> mark(n);
|
||||
ll x = 0;
|
||||
ll prev = LLONG_MIN;
|
||||
int cb = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (a[i] < prev) {
|
||||
if (cb == 1) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
mark[i] = 1;
|
||||
cb = 1;
|
||||
x = max(x, prev - a[i]);
|
||||
} else {
|
||||
prev = a[i];
|
||||
cb = 0;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (mark[i]) a[i] += x;
|
||||
}
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (a[i] < a[i - 1]) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
cout << "Yes\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,298 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(string, a);
|
||||
int cnt_00 = 0, cnt_11 = 0, cnt_01 = 0, cnt_10 = 0;
|
||||
for (int i = 0; i < n; i += 2) {
|
||||
string curr = a.substr(i, 2);
|
||||
if (curr == "00") cnt_00 += 1;
|
||||
else if (curr == "01") cnt_01 += 1;
|
||||
else if (curr == "10") cnt_10 += 1;
|
||||
else cnt_11 += 1;
|
||||
}
|
||||
int res = 0;
|
||||
res = max(res, cnt_00 * 2 + (!!cnt_01) * 2 + cnt_11 * 2 + min(2, cnt_10));
|
||||
// res = max(res, (!!cnt_10) + cnt_00 * 2 + (!!cnt_01) * 2) + cnt_11 * 2;
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,286 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(ll, x);
|
||||
cout << (x << 32 | x) << ' ' << x << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,325 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
using ll = MLL<MDL>;
|
||||
read(int, n, m, k);
|
||||
vector<ll> fact;
|
||||
fact.push_back(1);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
fact.push_back(fact.back() * i);
|
||||
}
|
||||
// (M - K + 1) powers
|
||||
vector<ll> pw1;
|
||||
pw1.push_back(1);
|
||||
for (int i = 1; i < k; ++i) {
|
||||
pw1.push_back(pw1.back() * (m - k + 1));
|
||||
}
|
||||
// (K - 1) powers
|
||||
vector<ll> pw2;
|
||||
for (int i = 1; i < k; ++i)
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,320 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n, m);
|
||||
vector<vector<pii>> ch1(n + 1);
|
||||
while (m--) {
|
||||
read(int, u, v, w);
|
||||
ch1[u].emplace_back(v, w);
|
||||
ch1[v].emplace_back(u, w);
|
||||
}
|
||||
vector<vector<pii>> ch(n + 1);
|
||||
for (int v = 1; v <= n; ++v) {
|
||||
for (auto&& [i, w1] : ch1[v]) {
|
||||
for (auto&& [u, w2] : ch1[i]) {
|
||||
if (u == v) continue;
|
||||
ch[v].emplace_back(u, (w1 + w2) * (w1 + w2));
|
||||
ch[u].emplace_back(v, (w1 + w2) * (w1 + w2));
|
||||
}
|
||||
}
|
||||
}
|
||||
vector<bool> vis(n + 1);
|
||||
vector<ll> dis(n + 1, INFLL);
|
||||
priority_queue<pli, vector<pli>, greater<>> pq;
|
||||
dis[1] = 0;
|
||||
pq.emplace(0, 1);
|
||||
while (pq.size()) {
|
||||
poptop(pq, d, v);
|
||||
continue_or(vis[v], 1);
|
||||
// debug(v);
|
||||
for (auto&& [u, w] : ch[v]) {
|
||||
if (d + w < dis[u]) {
|
||||
dis[u] = d + w;
|
||||
pq.emplace(dis[u], u);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
cout << (dis[i] == INFLL ? -1 : dis[i]) << " \n"[i == n];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,338 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
constexpr int charset[4] = {'A', 'N', 'O', 'T'};
|
||||
|
||||
template<typename T>
|
||||
struct BIT {
|
||||
int n;
|
||||
vector<T> c;
|
||||
BIT(size_t n) : n(n), c(n + 1) {}
|
||||
void add(size_t i, const T& k) {
|
||||
while (i <= n) {
|
||||
c[i] += k;
|
||||
i += lowbit(i);
|
||||
}
|
||||
}
|
||||
T getsum(size_t i) {
|
||||
T res = {};
|
||||
while (i) {
|
||||
res += c[i];
|
||||
i -= lowbit(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
void solve() {
|
||||
read(string, a);
|
||||
int n = a.size();
|
||||
unordered_map<char, deque<int>, safe_hash> oc;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
oc[a[i]].push_back(i);;
|
||||
}
|
||||
string res;
|
||||
BIT<int> occ(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
char mx;
|
||||
int mx_v = -1;
|
||||
for (auto&& c : charset) {
|
||||
if (oc[c].size() && oc[c].front() - occ.getsum(oc[c].front()) > mx_v) {
|
||||
mx_v = oc[c].front() - occ.getsum(oc[c].front());
|
||||
mx = c;
|
||||
}
|
||||
}
|
||||
assert(mx_v != -1);
|
||||
debug(i), debug(mx), debug(mx_v);
|
||||
cerr << "oc = ";
|
||||
for (auto&& [u, v] : oc) {
|
||||
cerr << "{" << u << ", ";
|
||||
for (auto&& x : v) cerr << x << ' ';
|
||||
cerr << "}, ";
|
||||
}
|
||||
cerr << endl;
|
||||
res.push_back(mx);
|
||||
oc[mx].pop_front();
|
||||
occ.add(oc[mx].front(), 1);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,410 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(string, a);
|
||||
int n = a.size();
|
||||
array<int, 26> slot {};
|
||||
for (auto&& x : a) slot[x - 97] += 1;
|
||||
int uq = 26 - count(slot.begin(), slot.end(), 0);
|
||||
auto check = [&] (int k) -> optional<string> {
|
||||
// only if uq >= 3
|
||||
cerr << "Checking k = " << k << endl;
|
||||
for (int i = 0; i < 26; ++i) {
|
||||
if (!slot[i]) continue;
|
||||
debug(i);
|
||||
array<int, 26> slot1 = slot;
|
||||
string res;
|
||||
char mark = -1;
|
||||
while (slot1[i]) {
|
||||
int use = min(slot1[i], k + 1);
|
||||
slot1[i] -= use;
|
||||
res += string(use, char(i + 97));
|
||||
if (res.size() == n) break;
|
||||
int f = 0;
|
||||
for (int j = 0; j < 26; ++j) {
|
||||
if (slot1[j] && j != mark && j != i) {
|
||||
res.push_back(j + 97);
|
||||
--slot1[j];
|
||||
if (mark == -1) {
|
||||
mark = j;
|
||||
}
|
||||
f = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (f == 0) {
|
||||
goto fi;
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < 26; ++j) {
|
||||
if (slot1[j]) res += string(slot1[j], char(j + 97));
|
||||
}
|
||||
assert(res.size() == n);
|
||||
return res;
|
||||
fi:
|
||||
;;
|
||||
}
|
||||
return nullopt;
|
||||
};
|
||||
char x = -1, y = -1;
|
||||
auto check2 = [&] (int k, char x, char y) -> optional<string> {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
string res;
|
||||
int cb = 0;
|
||||
array<int, 26> slot1 = slot;
|
||||
if (slot[x] < k + 1) {
|
||||
res = string(slot[x], x + 97) + string(slot[y], y + 97);
|
||||
return res;
|
||||
} else {
|
||||
res = string(k + 1, x + 97);
|
||||
slot1[x] -= k + 1;
|
||||
cb = k;
|
||||
int flag = 1;
|
||||
for (int i = k + 1; i < n; ++i) {
|
||||
if (cb == k) {
|
||||
if (!slot1[y]) {
|
||||
flag = 0;
|
||||
break;
|
||||
} else {
|
||||
--slot1[y];
|
||||
res.push_back(y + 97);
|
||||
cb = 0;
|
||||
}
|
||||
} else {
|
||||
if (!slot1[x]) {
|
||||
assert(slot1[y]);
|
||||
--slot1[y];
|
||||
res.push_back(y + 97);
|
||||
cb = 0;
|
||||
} else {
|
||||
--slot1[x];
|
||||
res.push_back(x + 97);
|
||||
cb += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (flag) {
|
||||
assert(res.size() == n);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
swap(x, y);
|
||||
}
|
||||
return nullopt;
|
||||
};
|
||||
if (uq == 1) {
|
||||
cout << a << '\n';
|
||||
return;
|
||||
} else if (uq == 2) {
|
||||
for (int i = 0; i < 26; ++i) {
|
||||
if (!slot[i]) continue;
|
||||
if (x != -1) {
|
||||
y = i;
|
||||
break;
|
||||
} else {
|
||||
x = i;
|
||||
}
|
||||
}
|
||||
int l = 0, r = n;
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (check2(mid, x, y) != nullopt) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
cout << check2(l, x, y).value() << '\n';
|
||||
return;
|
||||
}
|
||||
int l = 0, r = n;
|
||||
while (l < r) {
|
||||
int mid = l + r >> 1;
|
||||
if (check(mid) != nullopt) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
cout << check(l).value() << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,300 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
priority_queue<ll> pq;
|
||||
while (n--) {
|
||||
read(int, x);
|
||||
pq.emplace(x);
|
||||
}
|
||||
ll res = 0;
|
||||
while (pq.size()) {
|
||||
ll x = pq.top(); pq.pop();
|
||||
if (x == 0) continue;
|
||||
int v = 63 - __builtin_clzll(x);
|
||||
res |= 1LL << v;
|
||||
x ^= 1LL << v;
|
||||
pq.push(x);
|
||||
}
|
||||
cout << res << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,291 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n, k);
|
||||
unordered_set<int, safe_hash> st;
|
||||
while (n--) {
|
||||
read(int, x);
|
||||
st.emplace(x);
|
||||
}
|
||||
cout << min(ull(k), st.size()) << '\n';
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,296 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
|
||||
constexpr ll MDL = 1e9 + 7;
|
||||
constexpr ll PRIME = 998'244'353;
|
||||
constexpr ll MDL1 = 8784491;
|
||||
constexpr ll MDL2 = PRIME;
|
||||
|
||||
/* random */
|
||||
|
||||
mt19937 rd(chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now().time_since_epoch()).count());
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) ((((x) % (y)) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | (ull(y) & ((1ULL << 32) - 1)))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(ll(p))))
|
||||
#define i2(p) (ll(u2(p) << 32) >> 32)
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* hash */
|
||||
struct safe_hash {
|
||||
// https://codeforces.com/blog/entry/62393
|
||||
static uint64_t splitmix64(uint64_t x) {
|
||||
// http://xorshift.di.unimi.it/splitmix64.c
|
||||
x += 0x9e3779b97f4a7c15;
|
||||
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
|
||||
return x ^ (x >> 31);
|
||||
}
|
||||
|
||||
size_t operator()(uint64_t x) const {
|
||||
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
|
||||
return splitmix64(x + FIXED_RANDOM);
|
||||
}
|
||||
};
|
||||
|
||||
struct pair_hash {
|
||||
template <typename T, typename U>
|
||||
size_t operator()(const pair<T, U>& a) const {
|
||||
auto hash1 = safe_hash()(a.first);
|
||||
auto hash2 = safe_hash()(a.second);
|
||||
if (hash1 != hash2) {
|
||||
return hash1 ^ hash2;
|
||||
}
|
||||
return hash1;
|
||||
}
|
||||
};
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t, safe_hash> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
template <typename T, typename Iterator> pair<size_t, map<T, size_t>> discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
map<T, size_t> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
template <typename T, typename Iterator> pair<size_t, unordered_map<T, size_t, safe_hash>> unordered_discretize(Iterator __first, Iterator __last) {
|
||||
set<T> st(__first, __last);
|
||||
size_t N = 0;
|
||||
unordered_map<T, size_t, safe_hash> mp;
|
||||
for (auto&& x : st) mp[x] = ++N;
|
||||
return {N, mp};
|
||||
}
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << (x) << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
template<typename T, typename U> ostream& operator<<(ostream& out, const pair<T, U>& p) {
|
||||
out << "{" << p.first << ", " << p.second << "}";
|
||||
return out;
|
||||
}
|
||||
template<typename Char, typename Traits, typename Tuple, std::size_t... Index>
|
||||
void print_tuple_impl(std::basic_ostream<Char, Traits>& os, const Tuple& t, std::index_sequence<Index...>) {
|
||||
using swallow = int[]; // guaranties left to right order
|
||||
(void)swallow { 0, (void(os << (Index == 0 ? "" : ", ") << std::get<Index>(t)), 0)... };
|
||||
}
|
||||
template<typename Char, typename Traits, typename... Args>
|
||||
decltype(auto) operator<<(std::basic_ostream<Char, Traits>& os, const std::tuple<Args...>& t) {
|
||||
os << "{";
|
||||
print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
|
||||
return os << "}";
|
||||
}
|
||||
template<typename T> ostream& operator<<(ostream& out, const vector<T>& vec) {
|
||||
for (auto&& i : vec) out << i << ' ';
|
||||
return out;
|
||||
}
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* math */
|
||||
constexpr inline int lg2(ll x) { return x == 0 ? -1 : sizeof(ll) * 8 - 1 - __builtin_clzll(x); }
|
||||
|
||||
void __exgcd(ll a, ll b, ll& x, ll& y) {
|
||||
if (b == 0) {
|
||||
x = 1, y = 0;
|
||||
return;
|
||||
}
|
||||
__exgcd(b, a % b, y, x);
|
||||
y -= a / b * x;
|
||||
}
|
||||
|
||||
ll inverse(ll a, ll b) {
|
||||
ll x, y;
|
||||
__exgcd(a, b, x, y);
|
||||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
int period(string s) { // find the length of shortest recurring period
|
||||
int n = s.length();
|
||||
auto z = calc_z(s);
|
||||
for (int i = 1; i <= n / 2; ++i) {
|
||||
if (n % i == 0 && z[i] == n - i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(ll, a, n);
|
||||
vector<ll> res;
|
||||
int next = 0;
|
||||
for (int i = 0; i < n - 1; i = next) {
|
||||
next = i + 1;
|
||||
while (next < n - 1 && a[next] > a[i]) ++next;
|
||||
res.push_back(a[i] * (next - i));
|
||||
for (int j = 0; j < next - i - 1; ++j) res.push_back(0);
|
||||
}
|
||||
res.push_back(0);
|
||||
putvec(res);
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
solve();
|
||||
#else
|
||||
read(int, t);
|
||||
for (int i = 0; i < t; ++i) {
|
||||
#ifdef DUMP_TEST_CASE
|
||||
if (i + 1 == (DUMP_TEST_CASE)) {
|
||||
dump();
|
||||
} else {
|
||||
solve();
|
||||
}
|
||||
#else
|
||||
solve();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
107
src/bin/d.cc
107
src/bin/d.cc
|
@ -1,4 +1,3 @@
|
|||
#pragma GCC optimize("Ofast")
|
||||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
|
@ -182,6 +181,22 @@ ll inverse(ll a, ll b) {
|
|||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
|
@ -233,38 +248,90 @@ int period(string s) { // find the length of shortest recurring period
|
|||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
// #define SINGLE_TEST_CASE
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
read(int, n, m, x);
|
||||
vector<vector<bool>> dp(m + 1, vector<bool>(n));
|
||||
dp[0][x - 1] = 1;
|
||||
for (int i = 1; i <= m; ++i) {
|
||||
read(int, r), read(char, op);
|
||||
if (op != '1') {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (dp[i - 1][j]) dp[i][mod(j + r, n)] = 1;
|
||||
}
|
||||
}
|
||||
if (op != '0') {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (dp[i - 1][j]) dp[i][mod(j - r, n)] = 1;
|
||||
}
|
||||
using ll = MLL<PRIME>;
|
||||
read(int, n);
|
||||
vector<vector<pii>> ch(n + 1);
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
read(int, u, v);
|
||||
ch[u].emplace_back(v, i);
|
||||
ch[v].emplace_back(u, i);
|
||||
}
|
||||
read(ll, e);
|
||||
int root = -1;
|
||||
int root_edge;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (ch[i].size() == 1) {
|
||||
root = i;
|
||||
root_edge = ch[i][0].second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << count(dp[m].begin(), dp[m].end(), 1) << "\n";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (dp[m][i]) cout << i + 1 << ' ';
|
||||
if (root == -1) {
|
||||
cout << -1 << '\n';
|
||||
return;
|
||||
}
|
||||
vector<int> dis(n + 1);
|
||||
auto dfs = [&] (auto dfs, int v, int pa, int d) -> void {
|
||||
dis[v] = d;
|
||||
for (auto&& [u, _] : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
dfs(dfs, u, v, d + 1);
|
||||
}
|
||||
};
|
||||
dfs(dfs, root, 0, 0);
|
||||
ll res = accumulate(dis.begin(), dis.end(), ll(0)) + *max_element(dis.begin(), dis.end());
|
||||
ll target = e * n;
|
||||
ll tar = (target - res) / n + 1;
|
||||
int64_t tar_val = tar.val;
|
||||
if (tar_val <= n + 2) {
|
||||
tar_val += PRIME;
|
||||
}
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
if (i == root_edge) {
|
||||
cout << tar_val << '\n';
|
||||
} else {
|
||||
cout << 1 << '\n';
|
||||
}
|
||||
}
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
|
|
67
src/bin/e.cc
67
src/bin/e.cc
|
@ -182,6 +182,22 @@ ll inverse(ll a, ll b) {
|
|||
return mod(x, b);
|
||||
}
|
||||
|
||||
vector<tuple<int, int, ll>> decompose(ll x) {
|
||||
vector<tuple<int, int, ll>> res;
|
||||
for (int i = 2; i * i <= x; i++) {
|
||||
if (x % i == 0) {
|
||||
int cnt = 0;
|
||||
ll pw = 1;
|
||||
while (x % i == 0) ++cnt, x /= i, pw *= i;
|
||||
res.emplace_back(i, cnt, pw);
|
||||
}
|
||||
}
|
||||
if (x != 1) {
|
||||
res.emplace_back(x, 1, x);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* string algorithms */
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
|
@ -233,24 +249,61 @@ int period(string s) { // find the length of shortest recurring period
|
|||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
||||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
read(string, a);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = i + 1; j < n; ++j) {
|
||||
template <ll mdl> struct MLL {
|
||||
ll val;
|
||||
MLL(ll v = 0) : val(mod(v, mdl)) {}
|
||||
friend MLL operator+(const MLL& lhs, const MLL& rhs) { return mod(lhs.val + rhs.val, mdl); }
|
||||
friend MLL operator-(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - rhs.val, mdl); }
|
||||
friend MLL operator*(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * rhs.val, mdl); }
|
||||
friend MLL operator/(const MLL& lhs, const MLL& rhs) { return mod(lhs.val * mod(inverse(rhs.val, mdl), mdl), mdl); }
|
||||
friend MLL operator%(const MLL& lhs, const MLL& rhs) { return mod(lhs.val - (lhs / rhs).val, mdl); }
|
||||
void operator+=(const MLL& rhs) { val = (*this + rhs).val; }
|
||||
void operator-=(const MLL& rhs) { val = (*this - rhs).val; }
|
||||
void operator*=(const MLL& rhs) { val = (*this * rhs).val; }
|
||||
void operator/=(const MLL& rhs) { val = (*this / rhs).val; }
|
||||
void operator%=(const MLL& rhs) { val = (*this % rhs).val; }
|
||||
};
|
||||
|
||||
}
|
||||
template <ll mdl>
|
||||
ostream& operator<<(ostream& out, const MLL<mdl>& num) {
|
||||
return out << num.val;
|
||||
}
|
||||
|
||||
template <ll mdl>
|
||||
istream& operator>>(istream& in, MLL<mdl>& num) {
|
||||
return in >> num.val;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
using ll = MLL<MDL>;
|
||||
read(int, n, m, k);
|
||||
vector<ll> fact;
|
||||
fact.push_back(1);
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
fact.push_back(fact.back() * i);
|
||||
}
|
||||
// (M - K + 1) powers
|
||||
vector<ll> pw1;
|
||||
pw1.push_back(1);
|
||||
for (int i = 1; i < k; ++i) {
|
||||
pw1.push_back(pw1.back() * (m - k + 1));
|
||||
}
|
||||
// (K - 1) powers
|
||||
vector<ll> pw2;
|
||||
for (int i = 1; i < k; ++i)
|
||||
}
|
||||
|
||||
int main() {
|
||||
#if __cplusplus < 201703L || defined(_MSC_VER) && !defined(__clang__)
|
||||
assert(false && "incompatible compiler variant detected.");
|
||||
#endif
|
||||
untie, cout.tie(NULL);
|
||||
prep();
|
||||
#ifdef SINGLE_TEST_CASE
|
||||
|
|
|
@ -1,13 +1,4 @@
|
|||
6 7
|
||||
2 1
|
||||
2 3
|
||||
3 5
|
||||
1 4
|
||||
6 1
|
||||
5 6
|
||||
4 5
|
||||
3
|
||||
1 4
|
||||
5 1
|
||||
4 5
|
||||
|
||||
1 2
|
||||
2 3
|
||||
665496238
|
||||
|
|
|
@ -249,7 +249,7 @@ int period(string s) { // find the length of shortest recurring period
|
|||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
#define SINGLE_TEST_CASE
|
||||
// #define SINGLE_TEST_CASE
|
||||
// #define DUMP_TEST_CASE 512
|
||||
|
||||
void dump() {}
|
||||
|
@ -257,6 +257,7 @@ void dump() {}
|
|||
void prep() {}
|
||||
|
||||
void solve() {
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
|
@ -1,4 +1,10 @@
|
|||
from random import randint
|
||||
print(500000)
|
||||
for i in range(500000):
|
||||
print(randint(7800000, 7900000))
|
||||
def diff(a, b):
|
||||
n = len(a)
|
||||
res = 0
|
||||
for i in range(n):
|
||||
if a[i] != b[i]:
|
||||
for j in range(i + 1, n):
|
||||
if b[j] == a[i]:
|
||||
res += j - i
|
||||
b = b[:i] + b[j] + b[i+1:j] + b[j+1:]
|
||||
return res
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
def diff(a, b):
|
||||
n = len(a)
|
||||
res = 0
|
||||
for i in range(n):
|
||||
if a[i] != b[i]:
|
||||
for j in range(i + 1, n):
|
||||
if b[j] == a[i]:
|
||||
res += j - i
|
||||
b = b[:i] + b[j] + b[i:j] + b[j+1:]
|
||||
break
|
||||
return res
|
Loading…
Reference in New Issue