Author: fcspartakm
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for _ in range(t):
a, b = map(int, input().split())
if b >= a:
print(-1)
else:
print(min(b, a - b))
Author: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for _ in range(t):
s = input()
ans = 1
if "010" in s or "101" in s:
ans += 1
if "000" in s or "111" in s:
ans += 1
print(ans)
Author: fcspartakm
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, input().split()))
p = [0 for i in range(n+1)]
for i in range(n):
p[i+1] = (p[i]+a[i]) % k
ans = 0
cnt = [0 for i in range(k)]
for i in range(k+1, n+1):
cnt[p[i-k-1]] += 1
ans += cnt[p[i]]
print(ans)
Author: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n, m;
cin >> n >> m;
vector<int> d(n);
for(int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
x--;
y--;
d[x]++;
d[y]++;
}
sort(d.begin(), d.end());
if(d[0] == 0)
cout << "Brain Melt\n";
else if(d[n - 1] == n - 1 && d[n - 2] == 1)
cout << 2 << "\n";
else
cout << "Many\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
Author: Roms
Tutorial
Tutorial is loading...
Solution (Roms)
#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
const string s25 = "2025", s26 = "2026";
cin >> t;
for (int tc = 0; tc < t; ++tc) {
int n;
string s;
cin >> n >> s;
vector <vector <int>> nxt(n, vector <int> (10));
vector <int> cur(10, n);
for (int i = n - 1; i >= 0; --i) {
cur[s[i] - '0'] = i;
for (int j = 0; j < 10; ++j)
nxt[i][j] = cur[j];
}
vector <int> d(n + 1);
vector <int> dd(n + 1);
for (int l = 0; l < n; ++l) {
int r25 = l, r26 = l;
int i25 = 0, i26 = 0;
while(i25 < s25.size() && r25 < n) {
int x = s25[i25++] - '0';
r25 = nxt[r25][x];
}
while(i26 < s26.size() && r26 < n) {
int x = s26[i26++] - '0';
r26 = nxt[r26][x];
}
int cnt = n - l;
d[l] += cnt;
--dd[l + 1];
if (r25 < r26) {
// bad == [r25..r26-1]
cnt = r26 - r25;
d[l] -= cnt;
++dd[r25 + 1];
//--dd[r26];
if(r26 < n)
--dd[r26 + 1];
}
}
vector <int> ans(n);
long long sum = 0, add = 0;
for (int i = 0; i < n; ++i) {
sum += d[i];
add += dd[i];
sum += add;
cout << sum << ' ';
}
cout << endl;
}
return 0;
}
2276F - Permutation Coloring Game
Author: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
void upd(int& x, int y)
{
if(x > y) x = y;
}
void solve()
{
int n, k;
cin >> n >> k;
vector<int> p(n);
for(int i = 0; i < n; i++)
{
cin >> p[i];
p[i]--;
}
vector<bool> used(n);
vector<int> len;
for(int i = 0; i < n; i++)
{
if(!used[i])
{
used[i] = true;
int cur_len = 1;
int j = p[i];
while(!used[j])
{
used[j] = true;
cur_len++;
j = p[j];
}
len.push_back(cur_len);
}
}
sort(len.begin(), len.end());
reverse(len.begin(), len.end());
if(k == 1)
{
if(len[0] >= 3)
cout << "Bob\n";
else if(len[0] == 1 || n % 2 == 1)
cout << "Alice\n";
else
cout << "Bob\n";
}
else if(len[0] > k + 1)
{
cout << "Bob\n";
}
else if(len[0] == k + 1)
{
if(len.size() > 1 && len[1] == k + 1)
{
cout << "Bob\n";
}
else
{
int must_remove = 0;
int cnt1 = 0;
for(int i = 1; i < len.size(); i++)
{
if(len[i] > 1)
must_remove += len[i];
else cnt1++;
}
int alice_move = must_remove + cnt1 % (k + 1);
if(alice_move > k || alice_move == 0)
cout << "Bob\n";
else
cout << "Alice\n";
}
}
else
{
int m = len.size();
m = min(m, 2 * k);
vector<vector<vector<int>>> dp(m+1, vector<vector<int>>(k+1, vector<int>(k + 1, INF)));
dp[0][0][0] = 0;
for(int i = 0; i < m; i++)
for(int j = 0; j <= k; j++)
for(int c = 0; c <= k; c++)
{
if(dp[i][j][c] == INF) continue;
bool alice_can = (j + len[i] <= k);
bool bob_can = (i - c < k);
if(alice_can)
upd(dp[i+1][j + len[i]][c + 1], dp[i][j][c]);
upd(dp[i+1][j][c], dp[i][j][c] + (bob_can ? len[i] - 1 : 0));
}
bool alice_win = false;
for(int j = 0; j <= k; j++)
for(int c = 0; c <= k; c++)
if(dp[m][j][c] <= k)
alice_win = true;
if(alice_win)
cout << "Alice\n";
else
cout << "Bob\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}
2276G - Elimination of Players
Author: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 5005;
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;
}
int sub(int x, int y)
{
return add(x, -y);
}
int binpow(int x, int y)
{
int z = 1;
while(y)
{
if(y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
int fact[N];
int inv[N];
void precalc()
{
fact[0] = 1;
for(int i = 1; i < N; i++)
fact[i] = mul(i, fact[i - 1]);
for(int i = 0; i < N; i++)
inv[i] = binpow(i, MOD - 2);
}
struct Blocks
{
int n;
vector<int> cutoff_pos;
vector<int> pref;
vector<int> pref_G;
vector<pair<int, int>> a;
void rebuild()
{
pref = { 0 };
pref_G = { 0 };
vector<bool> used(n);
vector<pair<int, int>> b;
cutoff_pos = vector<int>(n, -1);
int sign = 1;
int cnt = n - 1;
for(int i = 0; i < a.size(); i++)
{
int x = a[i].first;
int len = a[i].second;
if(used[x])
break;
used[x] = true;
b.push_back(make_pair(x, len));
cutoff_pos[x] = i;
int c = mul(sign, mul(len, fact[cnt]));
int c_G = mul(sign, mul(len, fact[cnt - 1]));
--cnt;
sign = sub(0, sign);
pref.push_back(add(pref.back(), c));
pref_G.push_back(add(pref_G.back(), c_G));
}
a = b;
}
int getF()
{
return pref.back();
}
int getG(int x)
{
if(cutoff_pos[x] == -1) return pref_G.back();
else return pref_G[cutoff_pos[x]];
}
void append(int x)
{
if(a.empty() || a[0].first != x)
a.insert(a.begin(), make_pair(x, 0));
a[0].second++;
rebuild();
}
Blocks(int n = 0) : n(n) {};
};
void solve()
{
int n;
cin >> n;
vector<int> a(n - 1);
for(int i = 0; i < n - 1; i++)
{
cin >> a[i];
a[i]--;
}
Blocks b(n);
vector<vector<int>> ans(n - 1, vector<int>(n));
vector<vector<int>> G(n - 1, vector<int>(n));
vector<int> F(n - 1);
for(int i = 0; i < n - 1; i++)
{
int x = a[i];
b.append(x);
F[i] = b.getF();
for(int j = 0; j < n; j++)
G[i][j] = b.getG(j);
for(int j = 0; j < n; j++)
{
if(a[i] == j) continue;
int s = add(fact[n - 1], G[i][j]);
if(i > 0)
{
s = sub(s, G[i - 1][j]);
if(a[i - 1] == j)
s = add(s, F[i - 1]);
}
ans[i][j] = s;
}
}
long long res = 0;
for(int i = 0; i < n - 1; i++)
for(int j = 0; j < n; j++)
res ^= ans[i][j] * 1ll * (i + 1) * 1ll * (j + 1);
cout << res << endl;
}
int main()
{
precalc();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
}



