Tutorial
Tutorial is loading...
Solution
t = int(input())
for i in range(t):
l, r = map(int, input().split())
if r < 0 or l > 0:
print(r - l)
else:
m = min(r, -l)
print(max(0, r - l - 2 * m - 1))
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++)
cin >> a[i];
int ans = 1;
for(int i = 0; i < n; i++)
{
int cur = 0;
for(int j = 0; j < n; j++)
if(a[j] % a[i] == 0)
cur++;
ans = max(ans, cur);
}
cout << n - ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
cin >> n;
int k = 0;
for(int i = 1; i <= n; i++) k |= i;
int m = 0;
vector<int> ans(n, 0);
for(int i = 1; i <= n; i++) m ^= i;
int d = 0;
cout << k << endl;
while((k >> d) & 1) d++;
cout << d << endl;
for(int i = d; i >= 1; i--)
if(((m >> i) & 1) == 0)
{
int x = (1 << (i - 1));
cout << x << endl;
ans[x - 1] = 1;
m ^= x;
m ^= (x << 1);
}
for(int i = 0; i < n; i++)
cout << ans[i] << " ";
cout << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
int tc;
cin >> tc;
while (tc--){
int n;
cin >> n;
vector<vector<int>> g(n);
forn(i, n - 1){
int v, u;
cin >> v >> u;
--v, --u;
g[v].push_back(u);
g[u].push_back(v);
}
if (n % 2 == 0){
cout << n + 1 << '\n';
continue;
}
int ans = n;
for (int k : {n + 1, n + 2}){
int u = k == n + 1 ? n / 2 : 0;
vector<int> clr(n, -1);
queue<int> q;
clr[u] = 0;
int c = 1;
forn(i, n) if (clr[i] == -1){
q.push(i);
clr[i] = c;
while (!q.empty()){
int v = q.front();
q.pop();
for (int u : g[v]) if (clr[u] == -1){
clr[u] = c;
q.push(u);
}
}
++c;
}
bool any = false;
for (int i = 1; i <= n; ++i) if (k - i <= n && k - i != i)
any |= clr[i - 1] != clr[k - i - 1];
if (any)
ans = k;
}
cout << ans << '\n';
}
}
2283E - Partition of a Binary String
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int N = 5043;
const int MOD = 998244353;
int bell[N];
int C[N][N];
int dp[N];
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
void precalc()
{
for(int i = 0; i < N; i++)
{
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; j++)
C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
}
bell[0] = 1;
for(int i = 1; i < N; i++)
for(int k = 0; k < i; k++)
bell[i] = add(bell[i], mul(C[i - 1][k], bell[i - k - 1]));
dp[0] = 1;
for(int i = 1; i < N; i++)
{
if(i >= 2) dp[i] = add(dp[i], dp[i - 2]);
if(i >= 3) dp[i] = add(dp[i], dp[i - 3]);
}
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
int zero = 0, one = 0, cur = 0;
int ans = 1;
int val = 0;
for(int i = 0; i < n; i++)
{
bool neq_adj = false;
if(i > 0 && s[i] != s[i - 1]) neq_adj = true;
if(i + 1 < n && s[i] != s[i + 1]) neq_adj = true;
if(i > 0 && s[i] == s[i - 1])
{
ans = mul(ans, dp[cur]);
cur = 0;
}
if(neq_adj)
{
cur++;
val += 2;
}
else
{
ans = mul(ans, dp[cur]);
cur = 0;
if(s[i] == '0') zero++;
else one++;
val++;
}
}
ans = mul(ans, dp[cur]);
ans = mul(ans, bell[zero]);
ans = mul(ans, bell[one]);
cout << val << " " << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
precalc();
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
struct FenwickSum
{
vector<int> f;
int n;
void add(int i, int x)
{
for(; i < n; i |= (i + 1))
f[i] += x;
}
int get(int i)
{
int ans = 0;
for(; i >= 0; i = (i & (i + 1)) - 1)
ans += f[i];
return ans;
}
int get(int l, int r)
{
return get(r) - get(l - 1);
}
FenwickSum(int n = 0) : n(n)
{
f.resize(n);
}
};
struct event
{
int moment;
int type;
int idx;
event(int moment = 0, int type = 0, int idx = 0) : moment(moment), type(type), idx(idx) {};
};
bool operator<(const event& a, const event& b)
{
if(a.moment != b.moment) return a.moment < b.moment;
if(a.type != b.type) return a.type < b.type;
return a.idx < b.idx;
}
void solve()
{
int n;
cin >> n;
vector<int> l(n), r(n);
for(int i = 1; i < n; i++)
{
cin >> l[i] >> r[i];
l[i]--;
}
vector<int> h(n, INF);
set<int> s;
for(int i = 0; i < n; i++) s.insert(i);
for(int i = 1; i < n; i++)
{
int lf = l[i];
int rg = r[i];
while(true)
{
auto it = s.lower_bound(lf);
if(it == s.end() || *it >= rg) break;
h[*it] = i;
s.erase(it);
}
}
vector<event> events;
for(int i = 0; i < n; i++)
events.push_back(event(h[i], 0, i));
for(int i = 1; i < n; i++)
events.push_back(event(r[i] - 1, 1, i));
long long ans = 0;
FenwickSum f(n + 1);
for(int i = 0; i < n; i++)
f.add(i, 1);
sort(events.begin(), events.end());
for(auto e : events)
if(e.type == 0)
f.add(e.idx, -1);
else
{
int lf = l[e.idx];
int rg = r[e.idx];
ans += f.get(lf, rg - 1);
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
2283G - Order Statistics on the Graph
Tutorial
Tutorial is loading...
Solution
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
const int V = 1 << N;
const int Q = int(1e5);
int adj_mask[N];
int idx[N];
int nxt[V];
vector<int> gt[V];
vector<pair<long long, int>> que[N];
int ans[N * Q];
bool used[V];
int cycle_pos[V];
vector<int> cycle;
vector<int> dfs_stack;
vector<int> get_unique(const vector<int>& a)
{
vector<int> b = a;
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
return b;
}
vector<int> get_masks(const vector<int>& a)
{
int n = a.size();
auto b = get_unique(a);
int m = b.size();
vector<int> masks(m - 1);
for(int i = 0; i < m - 1; i++)
{
int mask = 0;
for(int j = 0; j < n; j++)
if(a[j] > b[i])
mask |= (1 << j);
masks[i] = mask;
}
return masks;
}
vector<int> restore(const vector<int>& a, const vector<int>& masks)
{
int n = a.size();
auto b = get_unique(a);
int m = b.size();
vector<int> ans(n, b[0]);
for(int i = 0; i < m - 1; i++)
for(int j = 0; j < n; j++)
if((masks[i] >> j) & 1)
ans[j] = b[i + 1];
return ans;
}
void dfs(int x)
{
used[x] = true;
int depth = dfs_stack.size();
dfs_stack.push_back(x);
for(auto y : gt[x])
if(!used[y])
dfs(y);
for(auto [t, i] : que[x])
{
if(t <= depth)
ans[i] = dfs_stack[depth - t];
else
{
long long add = t - depth;
int pos = (cycle_pos[dfs_stack[0]] + add) % cycle.size();
ans[i] = cycle[pos];
}
}
dfs_stack.pop_back();
}
void solve()
{
int n, m, q;
cin >> n >> m >> q;
for(int i = 0; i < n; i++)
cin >> idx[i];
for(int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
--x;
--y;
adj_mask[y] |= (1 << x);
}
vector<long long> t(q);
vector<vector<int>> a(q, vector<int>(n));
for(int i = 0; i < q; i++)
{
cin >> t[i];
for(int j = 0; j < n; j++)
cin >> a[i][j];
}
int qid = 0;
for(int i = 0; i < q; i++)
{
auto masks = get_masks(a[i]);
for(auto mask : masks)
{
que[mask].push_back(make_pair(t[i], qid));
qid++;
}
}
for(int i = 0; i < (1 << n); i++)
{
int ni = 0;
for(int j = 0; j < n; j++)
{
int cur_mask = adj_mask[j] & i;
if(__builtin_popcount(cur_mask) >= idx[j])
ni |= (1 << j);
}
nxt[i] = ni;
gt[ni].push_back(i);
}
for(int i = 0; i < (1 << n); i++)
{
if(used[i]) continue;
int j = i;
while(!used[j])
{
used[j] = true;
j = nxt[j];
}
int k = j;
cycle = {};
do
{
cycle_pos[k] = cycle.size();
cycle.push_back(k);
k = nxt[k];
}
while(k != j);
for(auto c : cycle) dfs(c);
}
qid = 0;
for(int i = 0; i < q; i++)
{
auto masks = get_masks(a[i]);
vector<int> res;
for(int i = 0; i < masks.size(); i++)
{
res.push_back(ans[qid]);
qid++;
}
vector<int> res2 = restore(a[i], res);
for(auto x : res2) cout << x << " ";
cout << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 0; i < t; i++)
solve();
}









