Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, d;
cin >> n >> d;
vector<int> a(n);
for (int& x : a) cin >> x;
sort(a.begin(), a.end());
cout << (a.back() <= d || a[0] + a[1] <= d ? "YES" : "NO") << endl;
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
auto mul = [](string s, int k) -> string {
string res = "";
while (k--) res += s;
return res;
};
int tc;
cin >> tc;
while (tc--) {
string s, t;
cin >> s >> t;
int n = s.length(), m = t.length();
int g = __gcd(n, m);
if (mul(s, m / g) == mul(t, n / g))
cout << mul(s, m / g) << endl;
else
cout << "-1" << endl;
}
}
Идея: adedalic
Разбор
Tutorial is loading...
Решение (adedalic)
fun main() {
repeat(readLine()!!.toInt()) {
val (n, k) = readLine()!!.split(' ').map { it.toInt() }
for (i in 1 until (k - (n - k)))
print("$i ")
for (i in k downTo (k - (n - k)))
print("$i ")
println("")
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int t;
cin >> t;
forn(_, t){
int n, m;
string s;
cin >> n >> m;
cin >> s;
vector<int> sul(1, 0), sur(1, 0);
for (int i = n - 1; i >= 0; --i){
int d = s[i] == '+' ? 1 : -1;
sul.push_back(min(0, sul.back() + d));
sur.push_back(max(0, sur.back() + d));
}
reverse(sul.begin(), sul.end());
reverse(sur.begin(), sur.end());
vector<int> prl(1, 0), prr(1, 0), pr(1, 0);
forn(i, n){
int d = s[i] == '+' ? 1 : -1;
pr.push_back(pr.back() + d);
prl.push_back(min(prl.back(), pr.back()));
prr.push_back(max(prr.back(), pr.back()));
}
forn(i, m){
int l, r;
cin >> l >> r;
--l;
int l1 = prl[l], r1 = prr[l];
int l2 = sul[r] + pr[l], r2 = sur[r] + pr[l];
printf("%d\n", max(r1, r2) - min(l1, l2) + 1);
}
}
return 0;
}
Идея: Neon
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 13;
int n, m;
vector<pair<int, int>> g[N];
long long d[N][2][2];
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int v, u, w;
scanf("%d%d%d", &v, &u, &w);
--v; --u;
g[v].emplace_back(u, w);
g[u].emplace_back(v, w);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
d[i][j][k] = (long long)1e18;
d[0][0][0] = 0;
set<pair<long long, array<int, 3>>> q;
q.insert({0, {0, 0, 0}});
while (!q.empty()) {
auto [v, mx, mn] = q.begin()->second;
q.erase(q.begin());
for (auto [u, w] : g[v]) {
for (int i = 0; i <= 1 - mx; i++) {
for (int j = 0; j <= 1 - mn; j++) {
if (d[u][mx | i][mn | j] > d[v][mx][mn] + (1 - i + j) * w) {
auto it = q.find({d[u][mx | i][mn | j], {u, mx | i, mn | j}});
if (it != q.end())
q.erase(it);
d[u][mx | i][mn | j] = d[v][mx][mn] + (1 - i + j) * w;
q.insert({d[u][mx | i][mn | j], {u, mx | i, mn | j}});
}
}
}
}
}
for (int i = 1; i < n; i++) {
printf("%lld ", d[i][1][1]);
}
puts("");
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
#include<bits/stdc++.h>
using namespace std;
template<typename T>
struct dinic {
struct edge {
int u, rev;
T cap, flow;
};
int n, s, t;
T flow;
vector<int> lst;
vector<int> d;
vector<vector<edge>> g;
T scaling_lim;
bool scaling;
dinic() {}
dinic(int n, int s, int t, bool scaling = false) : n(n), s(s), t(t), scaling(scaling) {
g.resize(n);
d.resize(n);
lst.resize(n);
flow = 0;
}
void add_edge(int v, int u, T cap, bool directed = true) {
g[v].push_back({u, g[u].size(), cap, 0});
g[u].push_back({v, int(g[v].size()) - 1, directed ? 0 : cap, 0});
}
T dfs(int v, T flow) {
if (v == t)
return flow;
if (flow == 0)
return 0;
T result = 0;
for (; lst[v] < g[v].size(); ++lst[v]) {
edge& e = g[v][lst[v]];
if (d[e.u] != d[v] + 1)
continue;
T add = dfs(e.u, min(flow, e.cap - e.flow));
if (add > 0) {
result += add;
flow -= add;
e.flow += add;
g[e.u][e.rev].flow -= add;
}
if (flow == 0)
break;
}
return result;
}
bool bfs() {
fill(d.begin(), d.end(), -1);
queue<int> q({s});
d[s] = 0;
while (!q.empty() && d[t] == -1) {
int v = q.front(); q.pop();
for (auto& e : g[v]) {
if (d[e.u] == -1 && e.cap - e.flow >= scaling_lim) {
q.push(e.u);
d[e.u] = d[v] + 1;
}
}
}
return d[t] != -1;
}
T calc() {
T max_lim = numeric_limits<T>::max() / 2 + 1;
for (scaling_lim = scaling ? max_lim : 1; scaling_lim > 0; scaling_lim >>= 1) {
while (bfs()) {
fill(lst.begin(), lst.end(), 0);
T add;
while((add = dfs(s, numeric_limits<T>::max())) > 0)
flow += add;
}
}
return flow;
}
vector<bool> min_cut() {
vector<bool> res(n);
for(int i = 0; i < n; i++)
res[i] = (d[i] != -1);
return res;
}
};
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cin >> b[i];
int s = n;
int t = n + 1;
dinic<int> d(n + 2, s, t, true);
vector<int> last(101, -1);
for(int i = 0; i < n; i++){
if(b[i] > 0)
d.add_edge(s, i, b[i]);
if(b[i] < 0)
d.add_edge(i, t, -b[i]);
for(int k = 1; k <= 100; k++)
if(last[k] != -1 && a[i] % k == 0)
d.add_edge(i, last[k], int(1e9));
last[a[i]] = i;
}
int sum = 0;
for(int i = 0; i < n; i++)
sum += max(0, b[i]);
cout << sum - d.calc() << endl;
}
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define sz(a) int((a).size())
template<const int &MOD>
struct _m_int {
int val;
_m_int(int64_t v = 0) {
if (v < 0) v = v % MOD + MOD;
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(uint64_t v) {
if (v >= MOD) v %= MOD;
val = int(v);
}
_m_int(int v) : _m_int(int64_t(v)) {}
_m_int(unsigned v) : _m_int(uint64_t(v)) {}
static int inv_mod(int a, int m = MOD) {
int g = m, r = a, x = 0, y = 1;
while (r != 0) {
int q = g / r;
g %= r; swap(g, r);
x -= q * y; swap(x, y);
}
return x < 0 ? x + m : x;
}
explicit operator int() const { return val; }
explicit operator unsigned() const { return val; }
explicit operator int64_t() const { return val; }
explicit operator uint64_t() const { return val; }
explicit operator double() const { return val; }
explicit operator long double() const { return val; }
_m_int& operator+=(const _m_int &other) {
val -= MOD - other.val;
if (val < 0) val += MOD;
return *this;
}
_m_int& operator-=(const _m_int &other) {
val -= other.val;
if (val < 0) val += MOD;
return *this;
}
static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
#if !defined(_WIN32) || defined(_WIN64)
return unsigned(x % m);
#endif
// Optimized mod for Codeforces 32-bit machines.
// x must be less than 2^32 * m for this to work, so that x / m fits in an unsigned 32-bit int.
unsigned x_high = unsigned(x >> 32), x_low = unsigned(x);
unsigned quot, rem;
asm("divl %4\n"
: "=a" (quot), "=d" (rem)
: "d" (x_high), "a" (x_low), "r" (m));
return rem;
}
_m_int& operator*=(const _m_int &other) {
val = fast_mod(uint64_t(val) * other.val);
return *this;
}
_m_int& operator/=(const _m_int &other) {
return *this *= other.inv();
}
friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; }
friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; }
friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; }
friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; }
_m_int& operator++() {
val = val == MOD - 1 ? 0 : val + 1;
return *this;
}
_m_int& operator--() {
val = val == 0 ? MOD - 1 : val - 1;
return *this;
}
_m_int operator++(int) { _m_int before = *this; ++*this; return before; }
_m_int operator--(int) { _m_int before = *this; --*this; return before; }
_m_int operator-() const {
return val == 0 ? 0 : MOD - val;
}
friend bool operator==(const _m_int &a, const _m_int &b) { return a.val == b.val; }
friend bool operator!=(const _m_int &a, const _m_int &b) { return a.val != b.val; }
friend bool operator<(const _m_int &a, const _m_int &b) { return a.val < b.val; }
friend bool operator>(const _m_int &a, const _m_int &b) { return a.val > b.val; }
friend bool operator<=(const _m_int &a, const _m_int &b) { return a.val <= b.val; }
friend bool operator>=(const _m_int &a, const _m_int &b) { return a.val >= b.val; }
_m_int inv() const {
return inv_mod(val);
}
_m_int pow(int64_t p) const {
if (p < 0)
return inv().pow(-p);
_m_int a = *this, result = 1;
while (p > 0) {
if (p & 1)
result *= a;
a *= a;
p >>= 1;
}
return result;
}
friend string to_string(_m_int<MOD> x) {
return to_string(x.val);
}
friend ostream& operator<<(ostream &os, const _m_int &m) {
return os << m.val;
}
};
extern const int MOD = 998244353;
using Mint = _m_int<MOD>;
const int g = 3;
const int LOGN = 15;
vector<Mint> w[LOGN];
vector<int> rv[LOGN];
void prepare() {
Mint wb = Mint(g).pow((MOD - 1) / (1 << LOGN));
forn(st, LOGN - 1) {
w[st].assign(1 << st, 1);
Mint bw = wb.pow(1 << (LOGN - st - 1));
Mint cw = 1;
forn(k, 1 << st) {
w[st][k] = cw;
cw *= bw;
}
}
forn(st, LOGN) {
rv[st].assign(1 << st, 0);
if (st == 0) {
rv[st][0] = 0;
continue;
}
int h = (1 << (st - 1));
forn(k, 1 << st)
rv[st][k] = (rv[st - 1][k & (h - 1)] << 1) | (k >= h);
}
}
void ntt(vector<Mint> &a, bool inv) {
int n = sz(a);
int ln = __builtin_ctz(n);
forn(i, n) {
int ni = rv[ln][i];
if (i < ni) swap(a[i], a[ni]);
}
forn(st, ln) {
int len = 1 << st;
for (int k = 0; k < n; k += (len << 1)) {
fore(pos, k, k + len){
Mint l = a[pos];
Mint r = a[pos + len] * w[st][pos - k];
a[pos] = l + r;
a[pos + len] = l - r;
}
}
}
if (inv) {
Mint rn = Mint(n).inv();
forn(i, n) a[i] *= rn;
reverse(a.begin() + 1, a.end());
}
}
vector<Mint> mul(vector<Mint> a, vector<Mint> b) {
int cnt = 1 << (32 - __builtin_clz(sz(a) + sz(b) - 1));
a.resize(cnt);
b.resize(cnt);
ntt(a, false);
ntt(b, false);
vector<Mint> c(cnt);
forn(i, cnt) c[i] = a[i] * b[i];
ntt(c, true);
return c;
}
int main() {
prepare();
vector<Mint> fact(1, 1), ifact(1, 1);
auto C = [&](int n, int k) -> Mint {
if (k < 0 || k > n) return 0;
while (sz(fact) <= n) {
fact.push_back(fact.back() * sz(fact));
ifact.push_back(fact.back().inv());
}
return fact[n] * ifact[k] * ifact[n - k];
};
int n;
cin >> n;
vector<int> a(n), b(n);
forn(i, n) cin >> a[i] >> b[i];
vector<Mint> ans(1, 1);
forn(i, n) {
vector<Mint> Cs;
for (int j = b[i] - sz(ans) + 1; j < sz(ans) + a[i]; ++j)
Cs.push_back(C(a[i] + b[i], j));
auto res = mul(ans, Cs);
int cnt = sz(ans);
ans.resize(cnt + a[i] - b[i]);
forn(j, sz(ans)) ans[j] = res[cnt + j - 1];
}
cout << accumulate(ans.begin(), ans.end(), Mint(0)) << endl;
}