solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
string in;
cin >> in;
vector<int> left;
vector<int> rigth;
for (int i = 0; i < in.size(); i++) {
if (in[i] == 'l')
left.push_back(i + 1);
else
rigth.push_back(i + 1);
}
for (int i = 0; i < rigth.size(); i++) {
printf("%i\n", rigth[i]);
}
for (int i = left.size() - 1; i >= 0; i--) {
printf("%i\n", left[i]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int person1, person2, person3;
cin >> person1 >> person2 >> person3;
int ans = INT_MAX;
int distance = abs(person1 - person2) + abs(person1 - person3);
ans = min(ans, distance);
distance = abs(person2 - person1) + abs(person2 - person3);
ans = min(ans, distance);
distance = abs(person3 - person1) + abs(person3 - person2);
ans = min(ans, distance);
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (1 << 30) - 1;
const long long linf = (1ll << 62) - 1;
const int N = 1e6 + 100;
const int M = 1e9 + 7;
int m, n;
vector<int> cl[N], sign[N];
vector<int> g[N];
bool used[N];
int dp[2], ndp[2], cdp[2];
int rt;
int val;
map<tuple<int, int, int>, int> current;
int self[N], signSelf[N];
inline int f(int v, int pe, int mustValue, int vValue) {
used[v] = true;
if (v == rt && pe != -1) {
return mustValue == 0 && vValue == val;
}
if (current.count(make_tuple(v, mustValue, vValue))) {
return current[make_tuple(v, mustValue, vValue)];
}
int& result = current[make_tuple(v, mustValue, vValue)] = 0;
if (self[v] != -1) {
mustValue ^= signSelf[v] ? !vValue : vValue;
}
int cnt = 0;
for (int e : g[v]) {
int to = cl[e][0] ^ cl[e][1] ^ v;
if (e == pe) {
continue;
}
cnt++;
for (int vTo = 0; vTo < 2; vTo++) {
int cur = (sign[e][cl[e][0] == v ? 0 : 1] ? !vValue : vValue) ||
(sign[e][cl[e][0] == to ? 0 : 1] ? !vTo : vTo);
result += f(to, e, mustValue ^ cur, vTo);
if (result >= M) {
result -= M;
}
}
if (cnt) {
break;
}
}
if (cnt == 0) {
return result = (mustValue == 0);
}
return result;
}
void calcDP(int v) {
rt = v;
cdp[0] = cdp[1] = 0;
current.clear();
val = 0;
cdp[0] += f(rt, -1, 0, 0);
current.clear();
val = 1;
cdp[0] += f(rt, -1, 0, 1);
current.clear();
val = 0;
cdp[1] += f(rt, -1, 1, 0);
current.clear();
val = 1;
cdp[1] += f(rt, -1, 1, 1);
while (cdp[0] >= M) {
cdp[0] -= M;
}
while (cdp[1] >= M) {
cdp[1] -= M;
}
}
int mustHave = 1;
int main() {
cin >> m >> n;
for (int i = 0; i < m; i++) {
int k;
scanf("%d", &k);
cl[i].resize(k);
sign[i].resize(k);
for (int j = 0; j < k; j++) {
scanf("%d", &cl[i][j]);
sign[i][j] = cl[i][j] < 0;
cl[i][j] = abs(cl[i][j]) - 1;
}
if (k == 2 && cl[i][0] == cl[i][1] && sign[i][0] == sign[i][1]) {
cl[i].pop_back();
}
if (((int)(cl[i]).size()) == 2 && cl[i][0] == cl[i][1] &&
sign[i][0] != sign[i][1]) {
i--;
m--;
mustHave ^= 1;
}
}
fill_n(self, n, -1);
for (int i = 0; i < m; i++) {
if (((int)(cl[i]).size()) > 1) {
g[cl[i][1]].push_back(i);
g[cl[i][0]].push_back(i);
} else {
self[cl[i].back()] = i;
signSelf[cl[i].back()] = sign[i].back();
}
}
static int list[N];
for (int i = 0; i < n; i++) {
list[i] = i;
}
sort(list, list + n, [&](int a, int b) {
return ((int)(g[a]).size()) < ((int)(g[b]).size());
});
dp[0] = 1;
dp[1] = 0;
for (int t = 0; t < n; t++) {
int i = list[t];
if (!used[i]) {
cdp[0] = cdp[1] = 0;
calcDP(i);
ndp[0] = (1ll * dp[0] * cdp[0] + 1ll * dp[1] * cdp[1]) % M;
ndp[1] = (1ll * dp[0] * cdp[1] + 1ll * dp[1] * cdp[0]) % M;
dp[0] = ndp[0];
dp[1] = ndp[1];
}
}
printf("%d\n", dp[mustHave]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int OO = 1e9;
const double EPS = 1e-9;
struct exam {
int a, b;
};
int sort_by_essay(exam e1, exam e2) { return (e1.b < e2.b); }
int main() {
long long n, r, avg;
cin >> n >> r >> avg;
long long target = avg * n;
exam exams[100000];
long long curr = 0;
for (int i = 0; i < n; i++) {
exam x;
cin >> x.a >> x.b;
curr += x.a;
exams[i] = x;
}
sort(exams, exams + n, sort_by_essay);
long long ans = 0;
int index = 0;
while (curr < target) {
long long grades = min((long long)(r - exams[index].a), target - curr);
long long essays = grades * (long long)exams[index].b;
ans += essays;
curr += grades;
index++;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, k;
cin >> n >> k;
string s;
long long int mn = 1, mx = n, ms;
int wasone = 0;
do {
ms = (mx + mn) / 2;
cout << "1 " << ms << " " << (ms + 1) << endl;
fflush(stdout);
cin >> s;
if (mx - mn == 1) {
break;
}
if (s == "TAK")
mx = ms;
else
mn = ms + 1;
if (mn + 1 == mx) {
wasone++;
}
if (wasone > 1) break;
} while (mn < mx);
long long int fp;
if (s == "TAK")
fp = mn;
else
fp = mx;
long long int dec = 1;
int found = 0;
while (mn > 0) {
mn = fp - dec;
if (mn < 1) break;
cout << "1 " << mn << " " << (mn + 1) << endl;
fflush(stdout);
cin >> s;
if (s == "TAK") {
mn = max((mn - dec - 1), (long long int)1);
mx = fp - 1;
found = 1;
break;
}
dec *= 2;
}
if (!found) {
mn = fp + 1;
mx = n;
}
if (mn == mx) {
cout << "2 " << fp << " " << mn << endl;
return 0;
}
wasone = 0;
do {
ms = (mx + mn) / 2;
cout << "1 " << ms << " " << (ms + 1) << endl;
fflush(stdout);
cin >> s;
if (s == "TAK")
mx = ms;
else
mn = ms + 1;
if (mn + 1 == mx) {
wasone++;
}
if (wasone > 1) break;
} while (mn < mx);
cout << "2 " << fp << " " << mn << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> v(m, 1000001);
for (int i = 0; i < n; i++) {
int r, c;
cin >> r >> c;
r--;
v[r] = min(v[r], c);
}
int s = 0;
for (int i = 0; i < m; i++) s += v[i];
cout << min(s, k) << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long pot(long long a, long long ex) {
if (ex == 0) return 1;
if (ex % 2) {
return (a * pot(a, ex - 1)) % MOD;
}
long long r = pot(a, ex / 2);
return (r * r) % MOD;
}
int main() {
long long n;
cin >> n;
long long a = 1, b = 1;
for (int i = 1; i <= n - 1; ++i) {
a = (a * i) % MOD;
}
for (int i = n + 1; i <= 2 * n - 1; ++i) {
b = (b * i) % MOD;
}
cout << (2 * (b * pot(a, MOD - 2)) % MOD - n + MOD) % MOD << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int nol, bir, n;
string s;
int E[200010];
vector<int> A[200010];
queue<int> q[2];
int main() {
cin >> s;
int p = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
int x = 0;
if (q[0].size() > 0) x = q[0].front(), q[0].pop();
if (x) {
A[E[x]].push_back(i + 1);
q[1].push(i + 1);
E[i + 1] = E[x];
} else
return puts("-1"), 0;
} else {
int x = 0;
if (q[1].size() > 0) x = q[1].front(), q[1].pop();
if (x) {
A[E[x]].push_back(i + 1);
q[0].push(i + 1);
E[i + 1] = E[x];
} else {
A[++p].push_back(i + 1);
q[0].push(i + 1);
E[i + 1] = p;
}
}
}
for (int i = 1; i <= p; i++)
if (s[A[i][A[i].size() - 1] - 1] == '1') return puts("-1"), 0;
cout << p << '\n';
for (int i = 1; i <= p; i++) {
cout << A[i].size() << ' ';
for (int j = 0; j < A[i].size(); j++) cout << A[i][j] << ' ';
cout << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string A, B, S;
cin >> A >> B >> S;
unordered_map<char, int> M;
for (int i = 0; i < A.length(); i++) {
M[A[i]]++;
}
for (int i = 0; i < B.length(); i++) {
M[B[i]]++;
}
for (int i = 0; i < S.length(); i++) {
M[S[i]]--;
}
for (auto x : M) {
if (x.second != 0) {
cout << "NO\n";
return 0;
}
}
cout << "YES\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const int LGN = 25;
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
int n;
vector<int> T[N];
int ans[N];
int sub[N], l[N], r[N], t, vx[N];
multiset<int> so, sa;
void dfs0(int u) {
sub[u] = 1;
l[u] = ++t;
vx[t] = u;
for (int v : T[u]) dfs0(v), sub[u] += sub[v];
sort((T[u]).begin(), (T[u]).end(),
[](int a, int b) { return sub[a] > sub[b]; });
so.insert(sub[u]);
r[u] = t;
}
vector<int> st[4 * N];
void build(int p = 1, int L = 1, int R = t) {
if (L == R) {
st[p].push_back(sub[vx[L]]);
return;
}
int m = (L + R) >> 1;
build(2 * p, L, m), build(2 * p + 1, m + 1, R);
st[p] = vector<int>(R - L + 1);
merge((st[2 * p]).begin(), (st[2 * p]).end(), (st[2 * p + 1]).begin(),
(st[2 * p + 1]).end(), st[p].begin());
}
void find(int l, int r, int v, function<void(int)> foo, int p = 1, int L = 1,
int R = t) {
if (r < L || R < l) return;
if (l <= L && R <= r) {
int i = lower_bound((st[p]).begin(), (st[p]).end(), v) - st[p].begin();
for (long long j = max(i - 3, 0), __R = min((int)st[p].size() - 1, i + 3);
j <= __R; j++)
foo(st[p][j]);
return;
}
int m = (L + R) >> 1;
find(l, r, v, foo, 2 * p, L, m), find(l, r, v, foo, 2 * p + 1, m + 1, R);
}
void add(int u) { so.erase(so.find(sub[u])); }
void dfsadd(int u) {
add(u);
for (int v : T[u]) dfsadd(v);
}
void rem(int u) { so.insert(sub[u]); }
void dfsrem(int u) {
rem(u);
for (int v : T[u]) dfsrem(v);
}
void answer(int u) {
if (sub[u] == 1) {
ans[u] = n - 1;
return;
}
int sz = so.size() + sa.size();
int mi = sub[T[u][T[u].size() - 1]];
if (sz) mi = min(mi, sz);
int ma = sub[T[u][0]];
auto relax = [&](multiset<int>& st, int z, int off) {
auto it = st.upper_bound(z + off);
if (it != st.begin()) it--;
for (long long i = 1, __R = 2; i <= __R; i++)
if (it != st.end()) {
int val = (*it) - off;
ans[u] = min(ans[u], max(ma - val, mi + val));
it++;
}
};
if (sz <= ma) {
ans[u] = ma;
if (sz < ma && (T[u].size() == 1 || sub[T[u][1]] < ma)) {
find(l[T[u][0]] + 1, r[T[u][0]], (ma - mi) >> 1,
[&](int x) { ans[u] = min(ans[u], max(ma - x, mi + x)); });
}
} else {
ma = sz;
ans[u] = ma;
int z = (ma - mi) >> 1;
relax(so, z, 0);
relax(sa, z, sub[u]);
}
if (mi < sz && sz < ma) ans[u] = max(ans[u], sz);
for (int v : T[u])
if (mi < sub[v] && sub[v] < ma) {
ans[u] = max(ans[u], sub[v]);
}
}
void dfs(int u, bool keep = false) {
if (T[u].size() == 0) {
answer(u);
if (keep) add(u);
return;
}
add(u);
sa.insert(sub[u]);
for (long long i = 1, __R = T[u].size() - 1; i <= __R; i++) dfs(T[u][i]);
dfs(T[u][0], true);
sa.erase(sa.find(sub[u]));
for (long long i = 1, __R = T[u].size() - 1; i <= __R; i++) dfsadd(T[u][i]);
answer(u);
if (!keep) dfsrem(u);
}
int main() {
std::ios::sync_with_stdio(false);
scanf("%d", &(n));
int root;
for (long long i = 1, __R = n; i <= __R; i++) {
int u, v;
scanf("%d%d", &(u), &(v));
T[u].push_back(v);
if (!u) root = v;
}
dfs0(root);
build();
dfs(root);
for (long long i = 1, __R = n; i <= __R; i++) printf("%d\n", ans[i]);
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, i, a, p = 0, q = 0, r = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> a;
p = p + a;
}
for (i = 0; i < n - 1; i++) {
cin >> a;
q = q + a;
}
for (i = 0; i < n - 2; i++) {
cin >> a;
r = r + a;
}
cout << p - q << "\n" << q - r;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, re, sum;
int pr[300005], ti[300005];
priority_queue<pair<int, int> > pq;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> pr[i];
for (int i = 1; i <= n + k; i++) {
if (i <= n) {
pq.push(make_pair(pr[i], i));
sum += pr[i];
}
if (i > k) {
ti[pq.top().second] = i;
sum -= pq.top().first;
pq.pop();
}
re += sum;
}
cout << re << endl;
for (int i = 1; i <= n; i++) cout << ti[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
struct line {
double d, t;
} l[105];
struct point {
double x, y;
};
double det(double a, double b, double c, double d) { return a * d - b * c; }
point inter(line a, line b) {
point ret;
ret.x = det(a.d, -cos(a.t), b.d, -cos(b.t)) /
det(sin(a.t), -cos(a.t), sin(b.t), -cos(b.t));
ret.y = det(sin(a.t), a.d, sin(b.t), b.d) /
det(sin(a.t), -cos(a.t), sin(b.t), -cos(b.t));
return ret;
}
double dist(point a, line b) {
return fabs(a.x * sin(b.t) - a.y * cos(b.t) - b.d);
}
int n;
int main() {
scanf("%d", &n);
if (n <= 4) {
printf("No solution\n");
return 0;
}
double w = 2 * pi / n;
for (int i = 0; i < n; i++) l[i] = (line){26.0 * n, w * i};
for (int i = 1; i < n; i++) {
double di = dist(inter(l[i - 1], l[(i + 1) % n]), l[i]);
l[i].d += (i * 1.0 / n) * di;
}
for (int i = 0; i < n; i++) {
point t = inter(l[i], l[(i + 1) % n]);
printf("%.6lf %.6lf\n", t.x, t.y);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 30005;
const int M = 1005;
const int P = 1e7 + 19;
const int Q = 1e9 + 7;
int n, K, q;
int v[N], w[N], st[N], ed[N];
vector<int> t[N << 1];
inline int id(int l, int r) { return l + r | l != r; }
void insert(int l, int r, int top, int bot, int x) {
if (top <= l && r <= bot) {
t[id(l, r)].push_back(x);
} else {
int mid = l + r >> 1;
if (top <= mid) insert(l, mid, top, bot, x);
if (bot > mid) insert(mid + 1, r, top, bot, x);
}
}
void query(int l, int r, vector<int> f) {
int p = id(l, r);
for (auto &x : t[p]) {
int V = v[x], W = w[x];
for (int i = K; i >= W; --i) f[i] = max(f[i], f[i - W] + V);
}
if (l == r) {
int res = 0;
for (int i = K; i > 0; --i) {
res = ((long long)res * P + f[i]) % Q;
}
printf("%d\n", res);
} else {
int mid = l + r >> 1;
query(l, mid, f);
query(mid + 1, r, f);
}
}
void work() {
scanf("%d%d", &n, &K);
for (int i = 1; i <= n; ++i) {
scanf("%d%d", &v[i], &w[i]);
st[i] = 1;
ed[i] = -1;
}
scanf("%d", &q);
int T = 0;
for (int i = 0; i < q; ++i) {
int j, x;
scanf("%d", &j);
if (j == 1) {
++n;
scanf("%d%d", &v[n], &w[n]);
st[n] = T + 1;
ed[n] = -1;
} else if (j == 2) {
scanf("%d", &x);
ed[x] = T;
} else {
++T;
}
}
for (int i = 1; i <= n; ++i) {
if (!~ed[i]) ed[i] = T;
if (st[i] <= ed[i]) insert(1, T, st[i], ed[i], i);
}
vector<int> f(K + 1);
query(1, T, f);
}
int main() {
work();
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1;
int a, i = 1;
cin >> s1;
a = s1.find("at", i);
s1.replace(a, 2, "@");
a = s1.find("dot", i);
while (a != s1.npos && a != s1.size() - 3) {
s1.replace(a, 3, ".");
a = s1.find("dot", i);
}
cout << s1 << " " << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1000111222, inf = 1000111222;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
if (n % 2 == 0) {
if (n == 2) {
cout << -1 << "\n";
continue;
} else {
cout << n / 4 << "\n";
continue;
}
} else {
if (n <= 7 || n == 11) {
cout << -1 << "\n";
} else {
int ans = 1;
n -= 9;
ans += (n / 4);
cout << ans << "\n";
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<long long int> v[1000010];
vector<long long int> cum[1000010];
vector<long long int> amid[1000010];
void build() {
cum[0][0] = v[0][0];
for (int i = 1; i < m; i++) cum[0][i] = cum[0][i - 1] + v[0][i];
for (int i = 1; i < n; i++) cum[i][0] = cum[i - 1][0] + v[i][0];
for (int i = 1; i < n; i++) {
for (int j = 1; j < m; j++) {
cum[i][j] = cum[i - 1][j] + cum[i][j - 1] - cum[i - 1][j - 1] + v[i][j];
}
}
return;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << cum[i][j] << " ";
}
cout << '\n';
}
}
long long int get(int sx, int sy, long long int c) {
int ex = sx + 2 * c, ey = sy + 2 * c;
long long int ret = cum[ex][ey];
if (sx) ret -= cum[sx - 1][ey];
if (sy) ret -= cum[ex][sy - 1];
if (sx && sy) ret += cum[sx - 1][sy - 1];
return ret;
}
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {0, 1, -1, 1, -1, 0, 1, -1};
bool in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
bool ok(int c) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
amid[i][j] = -1;
}
}
queue<pair<long long int, long long int> > q;
long long int s = (2 * c + 1) * (2 * c + 1);
for (int i = 0; i + 2 * c + 1 <= n; i++) {
for (int j = 0; j + 2 * c + 1 <= m; j++) {
long long int x = get(i, j, c);
if (x == s) {
amid[i + c][j + c] = 0;
q.push({i + c, j + c});
}
}
}
while (!q.empty()) {
pair<long long int, long long int> cur = q.front();
q.pop();
if (amid[cur.first][cur.second] == c) continue;
for (int i = 0; i < 8; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (in(nx, ny) && amid[nx][ny] == -1) {
amid[nx][ny] = amid[cur.first][cur.second] + 1;
q.push({nx, ny});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (v[i][j] == 1 && amid[i][j] == -1) return 0;
if (v[i][j] == 0 && amid[i][j] != -1) return 0;
}
}
return 1;
}
int main() {
cin >> n >> m;
int mn = 1000000;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int j = 0; j < m; j++) {
char c;
amid[i].push_back(0);
cum[i].push_back(0);
scanf(" %c", &c);
int x = 0;
if (c == 'X') x = 1;
v[i].push_back(x);
if (x == 1)
cnt++;
else {
if (cnt) mn = min(mn, cnt);
cnt = 0;
}
}
if (cnt) mn = min(mn, cnt);
}
for (int j = 0; j < m; j++) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (v[i][j] == 1)
cnt++;
else {
if (cnt) mn = min(mn, cnt);
cnt = 0;
}
}
if (cnt) mn = min(mn, cnt);
}
build();
int st = 0, en = 1000000;
while (st + 1 < en) {
int md = (st + en) / 2;
if (ok(md))
st = md;
else
en = md;
}
cout << st << '\n';
ok(st);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (amid[i][j] == 0)
cout << "X";
else
cout << ".";
}
cout << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int ToString(string s) {
int x;
stringstream ss;
ss << s;
ss >> x;
return x;
}
bool Valid(string s) { return !(s[0] == '0' && s.size() > 1); }
int main() {
string s;
while (cin >> s) {
if (s.size() < 3) {
cout << -1 << endl;
continue;
}
int res = -1;
for (int i = 1; i <= s.size() - 2; i++) {
for (int j = i; j < s.size() - 1; j++) {
string a = s.substr(0, i), b = s.substr(i, j - i + 1),
c = s.substr(j + 1);
if (Valid(a) && Valid(b) && Valid(c)) {
int x = ToString(a), y = ToString(b), z = ToString(c);
if (x <= 1000000 && y <= 1000000 && z <= 1000000)
res = max(res, x + y + z);
}
}
}
cout << res << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s[0] > 96) {
s[0] -= 32;
}
cout << s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int an[100010];
int main() {
long long n, tn, k, tmp, sum;
while (scanf("%I64d %I64d", &n, &k) != EOF) {
long long i, j;
sum = 0;
for (i = 0; i < n; i++) {
scanf("%d", &an[i]);
sum += an[i];
}
if (sum < k) {
printf("-1\n");
continue;
}
if (sum == k) {
printf("\n");
continue;
}
tn = n;
tmp = k / tn;
while (tmp > 0) {
for (i = 0; i < n; i++) {
if (an[i] > 0 && an[i] <= tmp) {
k -= an[i];
an[i] = 0;
tn--;
} else if (an[i] > 0 && an[i] > tmp) {
k -= tmp;
an[i] -= tmp;
}
}
tmp = k / tn;
}
for (i = 0; i < n && k > 0; i++) {
if (an[i] > 0) {
an[i]--;
k--;
}
}
i = i % n;
j = (i + 1) % n;
int flag = 1;
if (an[i] > 0) {
printf("%I64d", i + 1);
flag = 0;
}
while (j != i) {
if (an[j] > 0) {
if (flag) {
printf("%I64d", j + 1);
flag = 0;
} else
printf(" %I64d", j + 1);
}
j = (j + 1) % n;
}
printf("\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 9;
const long long mod = 1000000007;
long long pr[N];
bool vis[N];
int cnt;
void getpr() {
for (int i = 2; i < N; i++) {
if (!vis[i]) pr[++cnt] = i;
for (int j = 1; j <= cnt && pr[j] * i < N; j++) {
vis[i * pr[j]] = 1;
if (i % pr[j] == 0) break;
}
}
}
bool ispr(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
void solve() {
getpr();
long long n = 2;
cin >> n;
if (ispr(n)) {
cout << 1 << "\n" << n << endl;
return;
}
if (n % 2 != 0) {
cout << 3 << endl;
cout << 3 << " ";
n -= 3;
} else {
cout << 2 << endl;
}
for (int i = 1; i <= cnt; i++) {
long long d = n - pr[i];
if (ispr(d)) {
cout << d << " " << pr[i] << endl;
return;
}
}
getchar();
return;
}
signed main() {
ios::sync_with_stdio(0);
long long t = 1;
while (t--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int mx = 0, n, sum = 0, sum1 = 0;
cin >> n;
std::vector<long long int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
sum |= a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
sum1 |= b[i];
}
cout << sum1 + sum << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[102][10002];
int val[102][102], a[102][102], cnt[102];
int n, m;
int main() {
int i, j, k;
while (scanf("%d%d", &n, &m) != EOF) {
memset(dp, 0, sizeof(dp));
memset(val, 0, sizeof(val));
memset(a, 0, sizeof(a));
for (i = 1; i <= n; i++) {
scanf("%d", &cnt[i]);
for (j = 1; j <= cnt[i]; j++) {
scanf("%d", &a[i][j]);
a[i][j] += a[i][j - 1];
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= cnt[i]; j++) {
for (k = 0; k <= j; k++) {
val[i][j] =
max(val[i][j], a[i][k] + a[i][cnt[i]] - a[i][cnt[i] - (j - k)]);
}
}
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
for (k = 0; k <= cnt[i] && k <= j; k++) {
dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + val[i][k]);
}
}
}
int res = 0;
for (i = 1; i <= m; i++) res = max(res, dp[n][i]);
cout << res << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
int n, m, k;
set<pair<int, int> > mp;
set<int> tp;
int cnt[maxn];
namespace Segtree {
int mx[maxn << 2], lz[maxn << 2];
void pushup(int o) { mx[o] = max(mx[o << 1], mx[o << 1 | 1]); }
void build(int o, int L, int R) {
if (L == R) {
mx[o] = L - n - 1;
return;
}
int mid = (L + R) / 2;
build(o << 1, L, mid);
build(o << 1 | 1, mid + 1, R);
pushup(o);
}
void pushdown(int o) {
if (!lz[o]) return;
mx[o << 1] += lz[o];
mx[o << 1 | 1] += lz[o];
lz[o << 1] += lz[o];
lz[o << 1 | 1] += lz[o];
lz[o] = 0;
}
void update(int o, int L, int R, int l, int r, int v) {
if (l <= L && R <= r) {
lz[o] += v;
mx[o] += v;
return;
}
pushdown(o);
int mid = (L + R) / 2;
if (l <= mid) update(o << 1, L, mid, l, r, v);
if (r > mid) update(o << 1 | 1, mid + 1, R, l, r, v);
pushup(o);
}
int query(int o, int L, int R, int l, int r) {
if (l <= L && R <= r) {
return max(0, mx[o]);
}
int mid = (L + R) / 2;
pushdown(o);
int ret = 0;
if (l <= mid) ret = max(ret, query(o << 1, L, mid, l, r));
if (r > mid) ret = max(ret, query(o << 1 | 1, mid + 1, R, l, r));
pushup(o);
return ret;
}
}; // namespace Segtree
using namespace Segtree;
int main() {
scanf("%d%d%d", &n, &k, &m);
build(1, 1, 2 * n);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
auto p = make_pair(x, y);
int pos = y + abs(x - k);
if (mp.count(p)) {
mp.erase(p);
--cnt[pos];
if (!cnt[pos]) tp.erase(pos);
update(1, 1, 2 * n, 1, pos, -1);
} else {
mp.insert(p);
++cnt[pos];
tp.insert(pos);
update(1, 1, 2 * n, 1, pos, 1);
}
if (tp.empty())
printf("0\n");
else
printf("%d\n", query(1, 1, 2 * n, 1, *tp.rbegin()));
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300;
int query(const string& s) {
cout << s << endl;
int res;
cin >> res;
if (res == 0) {
exit(0);
}
return res;
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
string a;
string b;
for (int i = 0; i < MAXN; ++i) {
a.push_back('a');
b.push_back('b');
}
int qa = query(a);
int qb = query(b);
int cnta = MAXN - qa;
int cntb = MAXN - qb;
int n = cnta + cntb;
string cur;
for (int i = 0; i < n; ++i) {
cur.push_back('a');
}
int lst = cntb;
for (int i = 0; i < n - 1; ++i) {
cur[i] = 'b';
int res = query(cur);
if (res > lst) {
cur[i] = 'a';
} else {
lst = res;
}
}
if (lst > 0) {
cur[n - 1] = 'b';
}
cout << cur;
}
| 7 |
#include <bits/stdc++.h>
int grid[1001][1001];
int start[100000];
int main(void) {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &grid[i][j]);
}
}
for (int i = 0; i < k; i++) {
scanf("%d", &start[i]);
}
for (int i = 0; i < k; i++) {
int r = 1;
int c = start[i];
while (r <= n) {
int rr, cc;
switch (grid[r][c]) {
case 1:
rr = r;
cc = c + 1;
break;
case 2:
rr = r + 1;
cc = c;
break;
case 3:
rr = r;
cc = c - 1;
break;
}
grid[r][c] = 2;
r = rr;
c = cc;
}
printf("%d ", c);
}
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, a, b, c;
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> a >> b >> c;
std::cout << a - std::min(b, c) + 1 << std::endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
bool vis[3][3] = {};
int ops[3][3] = {};
enum compare { EQUAL, LITTLE, BIG };
void process() {
char t[20];
scanf("%s", t);
int a = t[0] - 'A';
int b = t[2] - 'A';
int op = t[1];
if (a == b || vis[a][b]) {
puts("Impossible");
exit(0);
}
vis[a][b] = vis[b][a] = true;
ops[a][b] = op == '>' ? BIG : LITTLE;
ops[b][a] = op == '>' ? LITTLE : BIG;
}
int main() {
process();
process();
process();
int p[] = {0, 1, 2};
do {
int a = p[0], b = p[1], c = p[2];
if (ops[a][b] == LITTLE && ops[b][c] == LITTLE && ops[a][c] == LITTLE) {
char A = a + 'A', B = b + 'A', C = c + 'A';
printf("%c%c%c\n", A, B, C);
return 0;
}
} while (std::next_permutation(p, p + 3));
puts("Impossible");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, a[20], cnt;
string s;
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) a[s[i] - '0']++;
if (a[0] != 0) {
if (a[1] != 0 || a[2] != 0 || a[3] != 0)
cout << "YES";
else
cout << "NO";
} else if (cnt == 0) {
if (a[9] != 0 || a[7] != 0) cnt++;
if (a[1] != 0 || a[4] != 0 || a[7] != 0) cnt++;
if (a[1] != 0 || a[2] != 0 || a[3] != 0) cnt++;
if (a[3] != 0 || a[6] != 0 || a[9] != 0) cnt++;
if (cnt == 4)
cout << "YES";
else
cout << "NO";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int N3 = 1e3 + 10;
const int INF = 2e9 + 10;
const int mod = 1e9 + 7;
const long long LINF = 4e18 + 10;
int n, m, k, looked[N], sz[N], sum, cnt, vertex, mx, pos, edge;
bool u[N], is[N];
vector<int> v[N];
void dfs(int x) {
u[x] = 1;
sz[cnt]++;
for (int i = 0; i < v[x].size(); ++i) {
int to = v[x][i];
looked[cnt]++;
if (!u[to]) {
dfs(to);
}
}
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
is[x] = 1;
}
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; ++i) {
if (is[i]) {
cnt++;
dfs(i);
sum += sz[cnt];
looked[cnt] /= 2;
}
}
for (int i = 1; i <= cnt; ++i) {
vertex += sz[i];
edge += looked[i];
}
vertex = n - vertex;
edge = m - edge;
int ans = 0;
for (int i = 1; i <= cnt; ++i) {
int temp = sz[i] + vertex;
if (temp * (temp - 1) / 2 - looked[i] - edge > mx) {
mx = temp * (temp - 1) / 2 - looked[i] - edge;
pos = i;
}
ans += sz[i] * (sz[i] - 1) / 2 - looked[i];
}
ans = mx;
for (int i = 1; i <= cnt; ++i) {
if (i == pos) {
continue;
}
ans += sz[i] * (sz[i] - 1) / 2 - looked[i];
}
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
template <class T>
inline void read(T &x) {
x = 0;
register char c = getchar();
register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T>
inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
template <class T>
inline void print(T x, char c) {
print(x), putchar(c);
}
const int N = 5e5 + 10;
int T, n, m, col[N], deg[N];
std::vector<int> G[N];
std::vector<std::pair<int, int>> E;
std::vector<int> without(const std::vector<int> &a) {
std::vector<bool> sta(n + 1, false);
for (int i = 0; i < a.size(); i++) sta[a[i]] = true;
std::vector<int> b;
for (int i = 1; i <= n; i++)
if (!sta[i]) b.push_back(i);
return b;
}
std::vector<int> getpath(int u, const std::vector<int> &nxt) {
std::vector<int> res;
while (u) res.push_back(u), u = nxt[u];
return res;
}
void init() {
read(n), read(m);
E.clear();
for (int i = 1; i <= n; i++) G[i].clear();
for (int u, v, i = 1; i <= m; i++) {
read(u), read(v);
E.push_back({u, v});
G[u].push_back(v), G[v].push_back(u);
}
}
void solve() {
int tem = 0;
for (int i = 1; i <= n; i++) {
deg[i] = G[i].size();
col[i] = G[i].size() % 3;
if (col[i] == 1 && !tem) tem = i;
}
std::vector<std::function<void(void)>> checker = {
[&]() {
bool fl = true;
for (int i = 1; i <= n; i++) fl &= deg[i] == 2;
if (fl) throw std::vector<int>{};
},
[&]() {
for (int i = 1; i <= n; i++)
if (!col[i]) throw without({i});
},
[&]() {
int cnt = 0;
for (int i = 1; i <= n; i++) cnt += col[i] == 1;
if (cnt < 2) return;
int s = tem, e = 0, u;
std::queue<int> q;
std::vector<int> pre(n + 1), dis(n + 1);
dis[s] = 1, q.push(s);
while (q.size()) {
u = q.front(), q.pop();
for (int v : G[u])
if (!dis[v]) {
dis[v] = dis[u] + 1;
pre[v] = u;
q.push(v);
}
}
for (int i = 1; i <= n; i++)
if (col[i] == 1 && i != s) {
if (!e || dis[e] > dis[i]) e = i;
}
throw without(getpath(e, pre));
},
[&]() {
std::vector<int> pre(n + 1), dep(n + 1);
std::vector<bool> vis(n + 1, false);
std::vector<std::pair<int, int>> pos;
std::function<std::vector<int>(int, int)> findway = [&](int s, int e) {
try {
std::vector<int> stk;
std::function<void(int, int)> dfs = [&](int u, int fa) {
stk.push_back(u);
if (u == e) throw stk;
for (int v : G[u])
if (v != fa && col[v] == 2) {
dfs(v, u);
}
stk.pop_back();
};
dfs(s, 0);
} catch (std::vector<int> res) {
return res;
}
return std::vector<int>{};
};
std::function<void(int, int, int, int &)> dfs = [&](int u, int fa,
int cur, int &tar) {
vis[u] = true;
dep[u] = dep[fa] + 1;
for (int v : G[u])
if (col[v] == 1) {
if (u != cur && !~tar) tar = u;
}
std::vector<std::pair<int, int>> ret;
for (int v : G[u])
if (v != fa && vis[v]) {
ret.push_back({dep[v], v});
}
if (ret.size()) {
int v = (*std::max_element(ret.begin(), ret.end())).second;
pre[v] = 0;
throw without(getpath(u, pre));
}
for (int v : G[u])
if (v != fa && col[v] == 2) {
pre[v] = u;
dfs(v, u, cur, tar);
}
};
int tar = -1;
if (tem) {
for (int cur : G[tem])
if (!vis[cur]) {
tar = -1, dfs(cur, 0, cur, tar);
pos.push_back({cur, tar});
}
if (pos.size() < 2) throw std::vector<int>{};
std::vector<int> res = {tem}, now;
now = findway(pos[0].first, pos[0].second),
res.insert(res.end(), now.begin(), now.end());
now = findway(pos[1].first, pos[1].second),
res.insert(res.end(), now.begin(), now.end());
throw without(res);
} else {
dfs(1, 0, 0, tar);
throw std::vector<int>{};
}
},
};
for (const auto &func : checker) func();
assert(false);
}
void stealData(int x, int y) {
if (T != x) return;
for (int u, v, i = 1; i <= T; i++) {
read(n), read(m);
if (i == y) printf("%d %d\n", n, m);
for (int j = 1; j <= m; j++) {
read(u), read(v);
if (i == y) printf("%d %d\n", u, v);
}
}
exit(0);
}
int main() {
for (read(T); T--;) {
init();
try {
solve();
} catch (std::vector<int> ans) {
if (ans.size()) {
putchar('Y'), putchar('E'), putchar('S'), putchar('\n');
print(ans.size(), '\n');
for (int i = 0; i < ans.size(); i++)
print(ans[i], " \n"[i + 1 == ans.size()]);
} else {
putchar('N'), putchar('O'), putchar('\n');
}
}
}
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long modulo(long long x, long long m) {
if (x >= 0) return x % m;
long long y = (-1 * x) % m;
if (y == 0) return 0;
return (m - y);
}
int main() {
long long a[15], i, j, add[15];
for (i = 1; i <= 14; i++) scanf("%lld", &a[i]);
long long mx = LONG_LONG_MIN;
for (i = 1; i <= 14; i++) {
memset(add, 0, sizeof(add));
long long save = a[i];
long long d = 14 - i;
if (a[i] <= d) {
long long k = 1;
for (j = i + 1; k <= a[i]; j++) {
add[j] += 1;
k++;
}
} else {
long long x, y;
long long z = a[i] - d;
for (j = i + 1; j <= 14; j++) add[j] += 1;
x = z / 14;
for (j = 1; j <= 14; j++) add[j] += x;
y = z % 14;
for (j = 1; j <= y; j++) add[j] += 1;
}
a[i] = 0;
long long sum = 0;
for (j = 1; j <= 14; j++) {
if ((a[j] + add[j]) % 2 == 0) sum += a[j] + add[j];
}
if (sum > mx) mx = sum;
a[i] = save;
}
printf("%lld\n", mx);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T next_int() {
T x = 0, p = 1;
char ch;
do {
ch = getchar();
} while (ch <= ' ');
if (ch == '-') {
p = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * p;
}
const int max_n = (int)1e3 + 227 + 1;
const int max_int = (int)1e9 + 227 + 1;
const int mod = (int)1e9 + 7;
int dp[4][max_n][max_n][4];
int main() {
int n = next_int<int>();
vector<string> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] = '?' + a[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 0; k <= 3; k++) {
dp[0][i][j][k] = dp[0][i - 1][j][k];
dp[1][i][j][k] = dp[1][i][j - 1][k];
if (a[i][j] - '0' == k) dp[1][i][j][k]++;
dp[2][i][j][k] = dp[2][i - 1][j - 1][k];
}
}
for (int j = n; j >= 1; j--) {
for (int k = 0; k <= 3; k++) {
dp[3][i][j][k] = dp[3][i - 1][j + 1][k];
}
dp[0][i][j][a[i][j] - '0']++;
dp[2][i][j][a[i][j] - '0']++;
dp[3][i][j][a[i][j] - '0']++;
}
}
pair<long double, pair<int, int> > ans = make_pair(-1, make_pair(0, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (a[i][j] == '0') continue;
int l = 0;
int r = min(min(i - 1, j - 1), min(n - i, n - j));
while (r - l > 1) {
int c = (l + r) / 2;
if ((dp[0][i + c][j][0] - dp[0][i - c - 1][j][0]) +
(dp[1][i][j + c][0] - dp[1][i][j - c - 1][0]) ==
0)
l = c;
else
r = c;
}
int c = r;
if ((dp[0][i + c][j][0] - dp[0][i - c - 1][j][0]) +
(dp[1][i][j + c][0] - dp[1][i][j - c - 1][0]))
c = l;
int k[2];
k[0] = (dp[0][i + c][j][2] - dp[0][i - c - 1][j][2]) +
(dp[1][i][j + c][2] - dp[1][i][j - c - 1][2]);
k[1] = (dp[0][i + c][j][3] - dp[0][i - c - 1][j][3]) +
(dp[1][i][j + c][3] - dp[1][i][j - c - 1][3]);
if (a[i][j] == '2') k[0]--;
if (a[i][j] == '3') k[1]--;
if (log10(2) * k[0] + log10(3) * k[1] > ans.first) {
ans.first = log10(2) * k[0] + log10(3) * k[1];
ans.second.first = k[0];
ans.second.second = k[1];
}
l = 0;
r = min(min(i - 1, j - 1), min(n - i, n - j));
while (r - l > 1) {
int c = (l + r) / 2;
if ((dp[2][i + c][j + c][0] - dp[2][i - c - 1][j - c - 1][0]) +
(dp[3][i + c][j - c][0] - dp[3][i - c - 1][j + c + 1][0]) ==
0)
l = c;
else
r = c;
}
c = r;
if ((dp[2][i + c][j + c][0] - dp[2][i - c - 1][j - c - 1][0]) +
(dp[3][i + c][j - c][0] - dp[3][i - c - 1][j + c + 1][0]))
c = l;
k[0] = (dp[2][i + c][j + c][2] - dp[2][i - c - 1][j - c - 1][2]) +
(dp[3][i + c][j - c][2] - dp[3][i - c - 1][j + c + 1][2]);
k[1] = (dp[2][i + c][j + c][3] - dp[2][i - c - 1][j - c - 1][3]) +
(dp[3][i + c][j - c][3] - dp[3][i - c - 1][j + c + 1][3]);
if (a[i][j] == '2') k[0]--;
if (a[i][j] == '3') k[1]--;
if (log10(2) * k[0] + log10(3) * k[1] > ans.first) {
ans.first = log10(2) * k[0] + log10(3) * k[1];
ans.second.first = k[0];
ans.second.second = k[1];
}
}
}
long long mda = 1;
if (ans.first == -1) mda = 0;
while (ans.second.first) {
mda = mda * 2 % mod;
ans.second.first--;
}
while (ans.second.second) {
mda = mda * 3 % mod;
ans.second.second--;
}
cout << mda << "\n";
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m, on, sum[100001];
pair<char, int> q[100000], last[100000];
bool val[100000];
char neg = '-', pos = '+';
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
last[i].first = '?';
last[i].second = -1;
}
for (int i = 0; i < m; i++) {
char foo[2];
int bar;
scanf("%s %d", &foo, &bar);
q[i].first = foo[0];
q[i].second = bar - 1;
if (foo[0] == '-' && !val[bar - 1]) {
on++;
}
val[bar - 1] = true;
}
memset(val, 1, sizeof val);
int poss = n;
sum[0] = on;
for (int i = 0; i < m; i++) {
char foo = q[i].first;
int bar = q[i].second;
if (foo == neg) {
if (on > 1) {
if (val[bar]) {
val[bar] = false;
poss--;
}
}
on--;
last[bar].first = foo;
last[bar].second = i;
sum[i + 1] = sum[i];
} else {
if (last[bar].first == '?') {
if (sum[i]) {
if (val[bar]) {
val[bar] = false;
poss--;
}
}
} else if (on || sum[i] > sum[last[bar].second + 1]) {
if (val[bar]) {
val[bar] = false;
poss--;
}
}
on++;
last[bar].first = foo;
last[bar].second = i;
sum[i + 1] = sum[i] + 1;
}
}
for (int i = 0; i < n; i++) {
if (last[i].first == neg) {
if (sum[m] > sum[last[i].second + 1]) {
if (val[i]) {
val[i] = false;
poss--;
}
}
}
}
printf("%d\n", poss);
int counter = 0;
for (int i = 0; i < n; i++) {
if (!val[i]) continue;
if (counter++) printf(" ");
printf("%d", i + 1);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int o = 0, c = 0, newLength, minAllowed;
long long int dp[2005][2005][2];
bool check(long long int open, long long int close) { return open >= close; }
long long int solve(long long int idx, long long int openBraces,
long long int ok) {
long long int openNow = o * ok, closedNow = c * ok;
openNow += openBraces;
closedNow += (idx - openBraces);
if (!check(openNow, closedNow)) return 0;
if (idx == newLength) {
if (ok == 0) {
openNow += o, closedNow += c;
return openNow == closedNow &&
openBraces - (idx - openBraces) >= minAllowed;
}
return openNow == closedNow;
}
if (dp[idx][openBraces][ok] != -1LL) return dp[idx][openBraces][ok];
long long int count = 0;
if (check(openNow, closedNow + 1))
count = solve(idx + 1, openBraces, ok) % 1000000007;
if (check(openNow + 1, closedNow))
count = (count + solve(idx + 1, openBraces + 1, ok)) % 1000000007;
if (ok == 0) {
if (check(openNow, closedNow) && openNow - closedNow >= minAllowed)
count = (count + solve(idx, openBraces, 1)) % 1000000007;
}
return dp[idx][openBraces][ok] = count;
}
int main(void) {
long long int desiredLength, length, i, flag = 1;
string str;
cin >> desiredLength >> length;
newLength = desiredLength - length;
cin >> str;
memset(dp, -1LL, sizeof dp);
for (i = 0; i < str.size(); i++) {
if (str[i] == '(')
o++;
else
c++;
if (c > o) flag = 0;
minAllowed = max(minAllowed, c - o);
}
if (newLength == 0) {
if (flag == 1 && o == c)
flag = 1;
else
flag = 0;
cout << (flag) << endl;
return 0;
}
cout << (solve(0, 0, 0)) % 1000000007;
return 0;
}
| 6 |
#include <bits/stdc++.h>
int n, m;
char s[1000001];
char t[1000001];
int ptr[1000001];
int num[1000001][3][3];
int main() {
scanf("%s %s", s, t);
n = strlen(s);
m = strlen(t);
for (int i = 0; i < n; i++) {
if (s[i] == 'R') s[i] = 0;
if (s[i] == 'G') s[i] = 1;
if (s[i] == 'B') s[i] = 2;
}
for (int i = 0; i < m; i++) {
if (t[i] == 'R') t[i] = 0;
if (t[i] == 'G') t[i] = 1;
if (t[i] == 'B') t[i] = 2;
}
int pos = -1;
for (int i = 1; i < n; i++) {
if (pos + 1 < m && t[pos + 1] == s[i - 1]) {
pos++;
}
ptr[i] = pos;
}
int cur[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) cur[i][j] = 0;
for (int i = 0; i < m; i++) {
if (i - 1 >= 0) cur[t[i - 1]][t[i]]++;
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++) num[i][j][k] = cur[j][k];
}
long long ans = 0;
for (int i = 1; i < n; i++) {
ans += ptr[i] + 1;
if (ptr[i] + 1 > 0 && s[i] != s[i - 1]) ans -= num[ptr[i]][s[i]][s[i - 1]];
}
pos = -1;
for (int i = 1; i < m; i++) {
if (pos + 1 < n && s[pos + 1] == t[i - 1]) {
pos++;
}
ptr[i] = pos;
}
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) cur[i][j] = 0;
for (int i = 0; i < n; i++) {
if (i - 1 >= 0) cur[s[i - 1]][s[i]]++;
for (int j = 0; j < 3; j++)
for (int k = 0; k < 3; k++) num[i][j][k] = cur[j][k];
}
for (int i = 1; i < m; i++) {
ans += ptr[i] + 1;
if (ptr[i] + 1 > 0 && t[i] != t[i - 1]) ans -= num[ptr[i]][t[i]][t[i - 1]];
}
for (int i = 1; i < m; i++) {
if (t[i] != t[i - 1]) ans += num[n - 1][t[i]][t[i - 1]];
}
printf("%I64d\n", ((long long)n) * m - ans);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
double calc(vector<double> v) {
int length = v.size();
double temp, ans = 0;
for (int i = length - 1; i >= 0; i--) {
temp = 1;
for (int j = length - 1; j >= 0; j--) {
if (i == j)
continue;
else
temp *= (1 - v[j]);
}
ans += temp * v[i];
}
return ans;
}
int main() {
int N;
cin >> N;
vector<double> input;
double temp;
for (int i = 0; i < N; i++) {
cin >> temp;
input.push_back(temp);
}
sort(input.begin(), input.end());
int length = input.size();
double final = 0;
if (input[length - 1] >= .5) {
printf("%.9lf", input[length - 1]);
} else {
vector<double> v;
for (int i = length - 1; i >= 0; i--) {
v.push_back(input[i]);
if (final > calc(v)) {
v.pop_back();
} else
final = calc(v);
}
}
cout << setprecision(10) << final;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, n;
string s;
cin >> t;
while (t--) {
cin >> n;
cin >> s;
long long c = 0;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1] && s[i] == '1') c++;
}
long long ans = c;
c = 0;
for (int i = 1; i < n; i++) {
if (s[i] == s[i - 1] && s[i] == '0') c++;
}
cout << max(ans, c);
printf("\n");
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, u;
scanf("%d", &n);
vector<pair<int, int> > v;
for (int i = 0; i < n; i++) {
scanf("%d", &u);
v.push_back(make_pair(u, i));
}
sort(v.begin(), v.end());
vector<int> a(n), b(n);
for (int i = 0; i < n / 3; i++) {
a[v[i].second] = i;
b[v[i].second] = v[i].first - i;
}
for (int i = n / 3; i < 2 * n / 3; i++) {
b[v[i].second] = i;
a[v[i].second] = v[i].first - i;
}
for (int i = n - 1, j = 0; i >= 2 * n / 3; i--, j++) {
b[v[i].second] = j;
a[v[i].second] = v[i].first - j;
}
printf("YES\n");
for (int i = 0; i < n; i++) {
if (i) putchar(' ');
printf("%d", a[i]);
}
printf("\n");
for (int i = 0; i < n; i++) {
if (i) putchar(' ');
printf("%d", b[i]);
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 505;
int j;
int n, k;
int tab[MAXN][MAXN];
bool used[MAXN * MAXN];
void clear_tab() {
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++) tab[i][j] = 0;
}
int main() {
cin >> n >> k;
clear_tab();
int row = n;
int col = n;
int last;
for (int i = n * n; i >= 1; i--) {
if (row == 0) break;
if (j == n - k) {
tab[row--][k] = i;
j = 0;
col = n;
} else {
tab[row][col--] = i;
j++;
}
last = i;
}
long long sum = 0;
for (int i = 1; i <= n; i++) sum += tab[i][k];
cout << sum << endl;
if (last == 1) {
for (int i = 1; i <= n * n; i++) {
printf("%d ", i);
if (i % n == 0) cout << endl;
}
return 0;
}
row = 1;
col = 1;
for (int i = 1; i <= last; i++) {
if (col == k) {
for (; col <= n; col++) {
printf("%d ", tab[row][col]);
}
printf("\n");
row++;
col = 1;
}
if (i == last) break;
printf("%d ", i);
col++;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string a, b, c;
int poz, cn;
int main() {
cin >> a;
cin >> b;
do {
poz = a.find(b);
if (poz != -1) {
cn++;
a.erase(a.begin(), a.begin() + poz + b.size());
}
} while (poz != -1 and a.size());
cout << cn;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int l, m;
struct mat {
long long t[2][2];
mat() { t[0][0] = t[1][0] = t[0][1] = t[1][1] = 0; }
};
mat operator*(mat &a, mat &b) {
mat res;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
res.t[i][j] += a.t[i][k] * b.t[k][j];
}
res.t[i][j] %= m;
}
}
return res;
}
mat pot(mat a, long long p) {
mat res;
res.t[0][0] = res.t[1][1] = 1;
for (int i = 0; i < 60; ++i) {
if (p & (1ll << i)) res = res * a;
a = a * a;
}
return res;
}
long long fpow(long long a, long long b) {
if (b == 0) return 1;
if (b == 1) return a % m;
if (b % 2)
return a * fpow(a, b - 1) % m;
else {
long long q = fpow(a, b / 2);
return q * q % m;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> k >> l >> m;
for (int i = l; i < 60; ++i) {
if (k & (1ll << i)) {
cout << 0;
return 0;
}
}
mat q;
q.t[0][0] = q.t[0][1] = q.t[1][0] = 1;
q = pot(q, n - 1);
long long ile = (q.t[0][0] + q.t[0][1] + q.t[1][0] + q.t[1][1]) % m,
pot = fpow(2, n), ans = 1;
for (int i = 0; i < min(60, l); ++i) {
if (k & (1ll << i))
ans *= (pot - ile + m) % m;
else
ans *= ile;
ans %= m;
}
for (int i = 60; i < l; ++i) {
ans *= ile;
ans %= m;
}
cout << ans % m;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int NR = 105;
const int MR = 1e6 + 10;
const int mod = 1e9 + 7;
void Min(int& x, int y) { x = min(x, y); }
void Max(int& x, int y) { x = max(x, y); }
void Add(int& x, int y) { x = (x + y) % mod; }
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
int n, l, m, ans;
int cnt[NR];
int a[MR], b[MR];
int zy[MR];
struct Mashiro {
int x[NR][NR];
void init() { memset(x, 0, sizeof(x)); }
Mashiro operator*(const Mashiro& A) const {
Mashiro B;
B.init();
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++)
for (int k = 1; k <= m; k++)
Add(B.x[i][j], 1ll * x[i][k] * A.x[k][j] % mod);
return B;
}
} s, t;
Mashiro ksm(Mashiro A, int b) {
Mashiro res = A;
b--;
while (b) {
if (b & 1) res = res * A;
A = A * A;
b >>= 1;
}
return res;
}
int main() {
n = read(), l = read(), m = read();
l -= 2;
for (int i = 1; i <= n; i++) {
a[i] = read();
s.x[1][a[i] % m + 1]++;
}
for (int i = 1; i <= n; i++) a[i] = read(), cnt[a[i] % m + 1]++;
for (int j = 1; j <= m; j++) t.x[1][j] = j;
for (int i = 2; i <= m; i++)
for (int j = 1; j <= m; j++)
t.x[i][j] = (t.x[i - 1][j] - 1) + m * (t.x[i - 1][j] - 1 == 0);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= m; j++) t.x[i][j] = cnt[t.x[i][j]];
if (l) s = s * ksm(t, l);
for (int i = 1; i <= n; i++) {
b[i] = read();
int p = (a[i] + b[i]) % m;
if (!p) p += m;
Add(ans, s.x[1][m - p + 1]);
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod=998244353;
template<int M>
struct modnum{
int v;
static int minv(int a,int m){return (a%=m)==1?1:m-ll(minv(m,a))*m/a;}
modnum():v(0){}
modnum(ll v_):v(int(v_%M)){if(v<0)v+=M;}
explicit operator int()const{return v;}
friend ostream&operator<<(ostream&out,const modnum&a){return out<<int(a);}
friend istream&operator>>(istream&in,modnum&a){ll v;in>>v;a=modnum(v);return in;}
friend bool operator==(const modnum&a,const modnum&b){return a.v==b.v;}
friend bool operator!=(const modnum&a,const modnum&b){return a.v!=b.v;}
modnum inv()const{return modnum(minv(v,M));}
friend modnum inv(const modnum&a){return a.inv();}
modnum neg()const{return modnum(v?M-v:0);}
friend modnum neg(const modnum&a){return a.neg();}
modnum operator-()const{return neg();}
modnum operator+()const{return *this;}
modnum&operator++(){if(++v==M)v=0;return*this;}
modnum&operator--(){if(!v--)v=M-1;return*this;}
modnum&operator+=(const modnum&a){if((v+=a.v)>=M)v-=M;return*this;}
modnum&operator-=(const modnum&a){if((v-=a.v)<0)v+=M;return*this;}
modnum&operator*=(const modnum&a){v=ll(v)*a.v%M;return*this;}
modnum&operator/=(const modnum&a){return*this*=a.inv();}
friend modnum operator++(modnum&a,int){modnum r=a;++a;return r;}
friend modnum operator--(modnum&a,int){modnum r=a;--a;return r;}
friend modnum operator+(const modnum&a,const modnum&b){return modnum(a)+=b;}
friend modnum operator-(const modnum&a,const modnum&b){return modnum(a)-=b;}
friend modnum operator*(const modnum&a,const modnum&b){return modnum(a)*=b;}
friend modnum operator/(const modnum&a,const modnum&b){return modnum(a)/=b;}
};
using mint = modnum<mod>;
const int N=200100;
int suf[N],pos[N];
mint F[N];
int n,k;
mint choose(int n,int k){
if(k>n||k<0)return 0;
return F[n]*F[k].inv()*F[n-k].inv();
}
void TenIQBenchmark(){
cin>>n>>k;
for(int i=0;i<n;++i)cin>>suf[i],pos[suf[i]]=i;
int lt=0;
for(int i=0;i<n-1;++i)if(!(suf[i]==n-1||(suf[i+1]!=n-1&&pos[suf[i]+1]<pos[suf[i+1]+1])))++lt;
mint res=0;
for(int add=0;add+lt<n;++add)res+=choose(k,add+lt+1)*choose(n-1-lt,add);
cout<<res<<endl;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
F[0]=1;
for(int i=1;i<N;++i)F[i]=i*F[i-1];
int tc=1;
//cin>>tc;
while(tc--)TenIQBenchmark();
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:146777216")
int main() {
int n;
cin >> n;
int ar[100002] = {0};
for (int i = 0; i <= 100000; i++) ar[i] = -1;
bool ex = 0;
for (int i = 0; i < n; i++) {
int x, k;
cin >> x >> k;
if (x - 1 <= ar[k])
ar[k] = max(ar[k], x);
else
ex = 1;
}
if (ex)
cout << "NO";
else
cout << "YES";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
static const int maxn = 2e5 + 5;
static const int logn = 19;
struct edge {
int v;
long long w;
edge(int vv = 0, long long ww = 0) : v(vv), w(ww) {}
};
struct node {
int f;
long long cost;
node(int ff = 0, long long costt = 0) : f(ff), cost(costt) {}
};
int tnode;
long long a[maxn];
vector<edge> graph[maxn];
node father[maxn][logn];
int depth[maxn];
int sz[maxn];
void dfs(int u = 1, int p = -1) {
for (int i = 1; i < logn; i++) {
node f = father[u][i - 1];
father[u][i].f = father[f.f][i - 1].f;
father[u][i].cost = father[f.f][i - 1].cost + father[u][i - 1].cost;
}
sz[u] = 1;
for (auto &it : graph[u]) {
int v = it.v;
long long w = it.w;
if (it.v == p) continue;
father[v][0] = {u, w};
depth[v] = depth[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
}
int chainNo, chainHead[maxn], chainInd[maxn], chainPos[maxn], chainSize[maxn],
baseArray[maxn], posBase[maxn], ptr, S, E;
void hld(int u = 1, int p = -1) {
if (chainHead[chainNo] == -1) chainHead[chainNo] = u;
chainInd[u] = chainNo;
ptr++;
posBase[u] = ptr;
int bigChild(-1), mx(-1);
for (auto &it : graph[u]) {
int v = it.v;
if (v == p) continue;
if (sz[v] > mx) mx = sz[v], bigChild = v;
}
if (bigChild != -1) hld(bigChild, u);
for (auto &it : graph[u]) {
int v = it.v;
if (v == p) continue;
if (bigChild != v) {
chainNo++;
hld(v, u);
}
}
}
int tree[maxn << 2];
void update(int n, int a, int b, int i, int j) {
if (a > b || a > j || b < i) return;
if (a >= i && b <= j) {
tree[n] += 1;
return;
}
int left = n << 1;
int right = left | 1;
int mid = (a + b) >> 1;
update(left, a, mid, i, j);
update(right, mid + 1, b, i, j);
}
int sum;
void query(int n, int a, int b, int pos) {
if (a > b || a > pos || b < pos) return;
sum += tree[n];
if (a >= pos && b <= pos) return;
int left = n << 1;
int right = left | 1;
int mid = (a + b) >> 1;
query(left, a, mid, pos);
query(right, mid + 1, b, pos);
}
void updatehld(int u, int v) {
int uChain, vChain = chainInd[v];
while (true) {
uChain = chainInd[u];
if (uChain == vChain) {
int l = posBase[v];
int r = posBase[u];
if (l > r) break;
update(1, S, E, l, r);
break;
}
int l = posBase[chainHead[uChain]];
int r = posBase[u];
update(1, S, E, l, r);
u = father[chainHead[uChain]][0].f;
}
}
void goup(int u, int cc) {
int v = u;
for (int i = logn - 1; i >= 0; i--) {
node ff = father[u][i];
if (ff.f != 0 && ff.cost <= cc) {
u = ff.f;
cc -= ff.cost;
}
}
if (v == u) return;
updatehld(father[v][0].f, u);
}
void init() {
ptr = 0;
chainNo = 0;
for (int i = 0; i < maxn; i++) {
chainHead[i] = -1;
}
}
int main() {
scanf("%d", &tnode);
for (int i = 1; i <= tnode; i++) scanf("%lld", a + i);
for (int u = 2; u <= tnode; u++) {
int v;
long long w;
scanf("%d %lld", &v, &w);
graph[u].push_back(edge(v, w));
graph[v].push_back(edge(u, w));
}
init();
depth[1] = 1;
dfs();
hld();
S = 1;
E = ptr;
for (int i = 2; i <= tnode; i++) goup(i, a[i]);
for (int i = 1; i <= tnode; i++) {
sum = 0;
query(1, S, E, posBase[i]);
printf("%d ", sum);
}
}
| 5 |
#include <bits/stdc++.h>
const long long int maxn = 2e5 + 5;
const long long mod = 998244353;
using namespace std;
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b % 2 == 1) res = res * a;
a = a * a;
b = b / 2;
}
return res;
}
long long po(long long x, long long y) {
long long res = 1;
x = x % mod;
while (y > 0) {
if (y & 1) res = ((res % mod) * (x % mod)) % mod;
y = y >> 1;
x = ((x % mod) * (x % mod)) % mod;
}
return res;
}
int a[maxn];
vector<int> adj[maxn];
int dp[maxn];
int dfs(int node, int par) {
for (int i = 0; i < adj[node].size(); i++) {
if (adj[node][i] == par) continue;
dp[node] += dfs(adj[node][i], node);
}
if (a[node] == 1)
return ++dp[node];
else {
--dp[node];
return max(0, dp[node]);
}
}
void dfs1(int node, int par) {
for (int i = 0; i < adj[node].size(); i++) {
if (adj[node][i] == par) continue;
dp[adj[node][i]] += max(0, dp[node] - max(0, dp[adj[node][i]]));
dfs1(adj[node][i], node);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
int n;
cin >> n;
vector<pair<long long int, long long int> > a(n);
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
if (n % 2 != 0)
cout << "NO" << '\n';
else {
int flag = 1;
long long int x =
(a[0].first + a[n / 2].first) + (a[0].second + a[n / 2].second);
for (int i = 1; i < n / 2; i++) {
long long int tx =
a[i].first + a[n / 2 + i].first + a[i].second + a[n / 2 + i].second;
if (x != tx) flag = 0;
}
if (flag)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
}
}
| 5 |
#include <bits/stdc++.h>
const int maxn = 200005;
int a[maxn], b[maxn], n;
int main() {
int i, j;
scanf("%d", &n);
int id = 1, op;
int pos, val;
a[1] = 1;
double sum = 0;
for (i = 0; i < n; i++) {
scanf("%d", &op);
if (op == 1) {
scanf("%d%d", &pos, &val);
b[pos] += val;
sum += val * pos;
} else if (op == 2) {
scanf("%d", &val);
a[++id] = val;
b[id] = 0;
sum += val;
} else {
sum -= b[id] + a[id];
b[id - 1] += b[id];
b[id] = 0;
--id;
}
printf("%.9f\n", sum / id);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, cs, ce, v, s[100005], e[100005], q, a, b, c, d, p, w, ans;
int main() {
scanf("%d%d%d%d%d", &n, &m, &cs, &ce, &v);
for (int i = 1; i <= cs; i++) scanf("%d", &s[i]);
for (int i = 1; i <= ce; i++) scanf("%d", &e[i]);
scanf("%d", &q);
while (q--) {
ans = 1E9;
scanf("%d%d%d%d", &a, &b, &c, &d);
if (a == c) ans = abs(b - d);
p = lower_bound(s + 1, s + cs + 1, b) - s;
if (p <= cs) ans = min(ans, abs(s[p] - b) + abs(c - a) + abs(s[p] - d));
if (p > 1)
ans = min(ans, abs(s[p - 1] - b) + abs(c - a) + abs(s[p - 1] - d));
w = lower_bound(e + 1, e + ce + 1, b) - e;
if (w <= ce)
ans = min(ans, abs(e[w] - b) + (abs(c - a) - 1) / v + 1 + abs(e[w] - d));
if (w > 1)
ans = min(ans, abs(e[w - 1] - b) + (abs(c - a) - 1) / v + 1 +
abs(e[w - 1] - d));
printf("%d\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, r, c, R[1005], C[1005];
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
cin >> n >> m;
R[1] = C[1] = R[n] = C[n] = 1;
for (int i = 1; i <= m; i++) {
cin >> r >> c;
R[r] = 1;
C[c] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!C[i]) ans++;
if (!R[i]) ans++;
}
if (n % 2 && !C[n / 2 + 1] && !R[n / 2 + 1]) ans--;
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int _n = 50 + 2;
const int _m = _n * 10000;
int a[_n], n;
bool vis[_m];
int main() {
int d;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
std::sort(a, a + n);
int sum = 0;
for (int i = 0; i < n; ++i)
if (a[i] - sum <= d)
sum += a[i];
else {
n = i;
break;
}
printf("%d ", sum);
vis[0] = true;
for (int i = 0; i < n; ++i)
for (int j = _m - 1; j >= 0; --j)
if (vis[j]) vis[j + a[i]] = true;
int step = 0, cur = 0, nex;
while (cur < sum) {
nex = cur;
for (int i = cur + 1; i <= sum && i - cur <= d; ++i)
if (vis[i]) nex = i;
cur = nex;
++step;
}
printf("%d\n", step);
return 0;
}
| 7 |
#include <cmath>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
#include <random>
#include <sstream>
#include <bitset>
#include <complex>
#include <chrono>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
int n, m; std::cin >> n >> m;
std::vector<std::vector<int>> edge(n, std::vector<int>(n, INT_MAX >> 1));
for (auto i = 0; i < m; ++i) {
int a, b, c; std::cin >> a >> b >> c;
edge[a][b] = c;
}
for (auto i = 0; i < n; ++i) {
auto min = INT_MAX >> 1;
for (int j = 0; j < n; ++j) {
edge[i][j] = std::min(++min, edge[i][j]);
min = edge[i][j];
}
for (int j = 0; j < n; ++j) {
edge[i][j] = std::min(++min, edge[i][j]);
min = edge[i][j];
}
}
std::vector<std::vector<long long int>> min_distance;
for (auto start = 0; start < n; ++start) {
std::vector<long long int> distance(n, LLONG_MAX);
for (auto i = 0; i < n; ++i) {
distance[i] = edge[start][i];
}
distance[start] = 0;
std::vector<bool> first(n, false);
for (auto _i = 0; _i < n; ++_i) {
int v = -1;
for (auto i = 0; i < n; ++i) {
if (first[i]) continue;
if (v < 0 || distance[i] < distance[v]) v = i;
}
first[v] = true;
for (auto next = 0; next < n; ++next) {
if (first[next]) continue;
distance[next] = std::min(distance[next], distance[v] + edge[v][(next + n - distance[v] % n) % n]);
}
}
min_distance.emplace_back(std::move(distance));
}
for (const auto& vec : min_distance) {
for (auto i = 0; i < vec.size(); ++i) {
if (i > 0) std::cout << ' ';
std::cout << vec[i];
}
std::cout << '\n';
}
} | 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long tab[1000007];
long long s[1000007];
long long p[1000007];
long long wyn;
long long odp(int a, int b) {
a--;
return p[b] - p[a] - (s[b] - s[a]) * a;
}
void rek(int a, int b, int graa, int grab) {
if (a > b) return;
int s = (a + b) >> 1;
int c = -1;
for (int i = grab; i >= graa && i >= s; i--) {
if (c == -1 || odp(s, i) > odp(s, c)) {
c = i;
}
}
wyn = max(wyn, odp(s, c));
rek(a, s - 1, graa, c);
rek(s + 1, b, c, grab);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &tab[i]);
s[i] = s[i - 1] + tab[i];
p[i] = p[i - 1] + tab[i] * i;
}
rek(1, n, 1, n);
printf("%lld\n", wyn);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
bitset<10006> val;
bitset<10006> ans;
vector<bitset<10006>> roll;
void addnew(int x) {
roll.push_back(val);
val = (val | (val << x));
}
void goback() {
val = roll.back();
roll.pop_back();
}
vector<int> seg[40009];
void add(int v, int tl, int tr, int l, int r, int val) {
if (tr < l || r < tl) return;
if (l <= tl && tr <= r) {
seg[v].push_back(val);
return;
} else {
add(2 * v, tl, (tl + tr) / 2, l, r, val);
add(2 * v + 1, (tl + tr) / 2 + 1, tr, l, r, val);
}
}
void dfs(int v, int tl, int tr) {
for (auto u : seg[v]) addnew(u);
if (tl == tr)
ans = ans | val;
else {
dfs(2 * v, tl, (tl + tr) / 2);
dfs(2 * v + 1, (tl + tr) / 2 + 1, tr);
}
for (auto u : seg[v]) goback();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
while (q--) {
int l, r, x;
cin >> l >> r >> x;
add(1, 1, n, l, r, x);
}
val[0] = 1;
dfs(1, 1, n);
vector<int> v;
for (int i = 1; i <= n; ++i)
if (ans[i]) v.push_back(i);
cout << v.size() << '\n';
for (auto u : v) cout << u << ' ';
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
string s1[] = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine"};
string s2[] = {"ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
string s3[] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen"};
int main() {
int n;
cin >> n;
if (n == 0)
cout << "zero";
else if (n < 10)
cout << s1[n - 1];
else if (n > 10 && n < 20)
cout << s3[n % 10 - 1];
else if (n % 10 == 0)
cout << s2[n / 10 - 1];
else
cout << s2[n / 10 - 1] << "-" << s1[n % 10 - 1];
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
int n, k, cnt;
char s[maxn];
stack<char> sss, ss;
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
int del = n - k, i = 1;
for (i = 1; i <= n && cnt < del; ++i) {
if (s[i] == ')' && sss.size() && sss.top() == '(') {
sss.pop();
cnt += 2;
} else
sss.push(s[i]);
}
while (!sss.empty()) {
ss.push(sss.top());
sss.pop();
}
while (!ss.empty()) {
putchar(ss.top());
ss.pop();
}
for (; i <= n; ++i) {
putchar(s[i]);
}
putchar('\n');
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const int N = 1e5 + 100;
int q, n;
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> q;
while (q--) {
cin >> n;
cin >> s;
bool flg = 1;
for (int i = 0; (i) < (int((s).size())); ++(i))
if (s[i] == '1') flg = 0;
if (flg)
cout << int((s).size()) << endl;
else {
int maxn = 0;
for (int i = 0; (i) < (int((s).size())); ++(i)) {
if (s[i] == '1') {
maxn = max(maxn, max(i + 1, int((s).size()) - i));
}
}
cout << maxn * 2 << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
pair<long long int, long long int> arr[M];
long long int val[M];
bool flag[M];
pair<long long int, long long int> Min[M];
long long int power(long long int a, long long int b) {
long long int val = 1;
while (b) {
if (b % 2) val = (val * a);
a = (a * a);
b /= 2;
}
return val;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int i;
long long int a, b;
cin >> n;
cin >> a >> b;
long long int ans = 0;
long long int sum = 0;
for (i = 0; i < n; i++) {
cin >> arr[i].first >> arr[i].second;
val[i] = arr[i].first * power(2, a);
sum = sum + arr[i].second;
}
for (i = 0; i < n; i++) {
Min[i].first = min(arr[i].second - arr[i].first, (long long int)0);
Min[i].second = i;
}
sort(Min, Min + n);
for (i = 0; i < n; i++) {
Min[i].first *= -1;
}
for (i = 0; i < min(n, (int)b); i++) {
flag[Min[i].second] = 1;
sum += Min[i].first;
}
long long int Max = sum;
if (b == 0) {
cout << Max << endl;
return 0;
}
for (i = 0; i < n; i++) {
if (flag[i] == 1) {
ans = sum +
max(val[i] - max(arr[i].first, arr[i].second), (long long int)0);
Max = max(Max, ans);
} else {
ans = sum + max(val[i] - arr[i].second, (long long int)0) -
Min[b - 1].first;
Max = max(Max, ans);
}
}
cout << Max << "\n";
}
| 6 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <typename T>
constexpr long long infValue =
std::is_same<T, int>::value ? 2000100007 : 8600000000000000007ll;
template <typename T>
constexpr long long maxValue =
std::is_same<T, int>::value ? 1000000007 : 1000000000000000001ll;
const long long INF = infValue<long long>;
const long long MOD = 1000000007ll;
const double EPS = 1e-6;
const long long MAX = maxValue<long long>;
using pr = pair<long long, long long>;
long long n, k;
long long answ = 1ll;
long long binPow(long long a, long long p) {
long long r = 1;
while (p) {
if (p & 1) r = r * a % MOD;
a = a * a % MOD;
p >>= 1;
}
return r;
}
long long dp[10010][64];
long long compute(long long prime, long long pow) {
if (pow == 0) return 1;
dp[0][0] = 1;
for (register long long i = 1; i <= (pow); ++i)
dp[0][i] = dp[0][i - 1] * prime % MOD;
for (register long long i = 1; i <= (k); ++i) {
dp[i][0] = 1;
for (register long long j = 1; j <= (pow); ++j) {
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] * j % MOD) *
binPow(j + 1, MOD - 2) % MOD;
}
}
return dp[k][pow];
}
int main(void) {
scanf("%lld %d", &n, &k);
for (long long i = 2; i * i <= n; ++i) {
long long d = 0;
while (n % i == 0) n /= i, ++d;
answ = answ * compute(i, d) % MOD;
}
if (n != 1) answ = answ * compute(n, 1) % MOD;
printf("%lld\n", answ);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000 * 1000 * 1000;
const int mod = 1000 * 1000 * 1000 + 7;
const int maxn = 3000000;
long long n, m;
long long ans;
pair<long long, long long> a[maxn];
bool check(int x) {
if (x % 2 == 1 && x * 1ll * (x - 1) / 2 + 1 <= n) return true;
if (x % 2 == 0 && x * 1ll * (x - 1) / 2 + x / 2 <= n) return true;
return false;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> a[i].second >> a[i].first;
sort(a + 1, a + 1 + m);
reverse(a + 1, a + 1 + m);
int x = 0;
for (int i = 1; i <= m; ++i)
if (check(i)) x = i;
for (int i = 1; i <= x; i++) ans += a[i].first;
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1 << 28;
const long long LINF = 1ll << 61;
inline long long getnum() {
register long long r = 0;
register bool ng = 0;
register char c;
c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') ng = 1, c = getchar();
while (c != ' ' && c != '\n') r = r * 10 + c - '0', c = getchar();
if (ng) r = -r;
return r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline void putsp() { putchar(' '); }
inline void putendl() { putchar('\n'); }
inline char mygetchar() {
register char c = getchar();
while (c == ' ' || c == '\n') c = getchar();
return c;
}
int n;
int head[200111], nxt[400111], to[400111], tot = 1;
bool vis[200111];
int p[200111];
int X, Y;
void dfs(int x, int pre) {
vis[x] = 1;
for (int i = head[x]; i; i = nxt[i]) {
if (!vis[to[i]]) {
dfs(to[i], i / 2);
} else if (i / 2 != pre) {
X = x;
Y = to[i];
}
}
}
int main() {
n = getnum();
int ans = 0, fb = 0;
for (int i = 1; i <= n; i++) {
p[i] = getnum();
nxt[++tot] = head[i];
head[i] = tot;
to[tot] = p[i];
nxt[++tot] = head[p[i]];
head[p[i]] = tot;
to[tot] = i;
if (i == p[i]) fb = i;
}
if (fb) dfs(fb, -1);
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
X = Y = -1;
dfs(i, -1);
if (p[Y] == X) swap(X, Y);
if (!fb) {
ans++;
p[X] = X, fb = X;
} else {
ans++;
p[X] = fb;
}
}
}
putnum(ans), putendl();
for (int i = 1; i <= n; i++) putnum(p[i]), putsp();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int d[100];
int dd = 0;
int a[100][100];
int dis(int x) { return lower_bound(d + 1, d + dd + 1, x) - d; }
int main() {
int x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6;
int flag = 0;
cin >> x1 >> y1 >> x2 >> y2;
cin >> x3 >> y3 >> x4 >> y4;
cin >> x5 >> y5 >> x6 >> y6;
d[++dd] = x1;
d[++dd] = x2;
d[++dd] = x3;
d[++dd] = x4;
d[++dd] = x5;
d[++dd] = x6;
d[++dd] = y1;
d[++dd] = y2;
d[++dd] = y3;
d[++dd] = y4;
d[++dd] = y5;
d[++dd] = y6;
sort(d + 1, d + dd + 1);
dd = unique(d + 1, d + dd + 1) - d - 1;
x1 = dis(x1) * 2;
x2 = dis(x2) * 2;
x3 = dis(x3) * 2;
x4 = dis(x4) * 2;
x5 = dis(x5) * 2;
x6 = dis(x6) * 2;
y1 = dis(y1) * 2;
y2 = dis(y2) * 2;
y3 = dis(y3) * 2;
y4 = dis(y4) * 2;
y5 = dis(y5) * 2;
y6 = dis(y6) * 2;
for (int i = x3; i <= x4; i++) {
for (int j = y3; j <= y4; j++) {
a[i][j] = 1;
}
}
for (int i = x5; i <= x6; i++) {
for (int j = y5; j <= y6; j++) {
a[i][j] = 1;
}
}
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
if (a[i][j] == 0) flag = 1;
}
}
if (flag == 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, m, n, o, p, l, t, sum, tot;
char s1[100005], s2[100005];
int a[100005], b[100005], ans[100005][2];
long long s;
void pr() {
if (p) return;
p = 1;
for (int i = 1; i <= sum; i++) printf("%d %d\n", ans[i][0], ans[i][1]);
}
void change(int pos, int ad) {
if (ad == -1) {
if (o == 1) return;
if (a[pos + 1] == 0) change(pos + 1, -ad);
if (o == 1) return;
ans[++sum][0] = pos, ans[sum][1] = ad, a[pos]--, a[pos + 1]--;
if (sum >= 100000) {
pr();
o = 1;
return;
}
} else {
if (o == 1) return;
if (a[pos + 1] == 9) change(pos + 1, -ad);
if (o == 1) return;
ans[++sum][0] = pos, ans[sum][1] = ad, a[pos]++, a[pos + 1]++;
if (sum >= 100000) {
pr();
o = 1;
return;
}
}
}
int main() {
scanf("%d\n", &n);
scanf("%s\n", s1 + 1);
scanf("%s\n", s2 + 1);
for (i = 1; i <= n; i++) a[i] = s1[i] - '0', b[i] = s2[i] - '0';
for (i = n; i >= 1; i--) {
k = b[i] - a[i];
ans[++sum][0] = i - 1, ans[sum][1] = k;
a[i] += k, a[i - 1] += k;
}
for (i = 0; i <= n; i++)
if (a[i] < 0 || a[i] > 9 || a[i] != b[i]) {
puts("-1");
return 0;
}
for (i = 1; i <= sum; i++)
if (ans[i][1] != 0) s += abs(ans[i][1]);
printf("%lld\n", s), sum = 0;
for (i = 1; i <= n; i++) a[i] = s1[i] - '0';
memset(ans, 0, sizeof(ans));
for (i = 1; i <= n; i++) {
while (a[i] > b[i]) {
if (a[i + 1] == 0) change(i + 1, 1);
a[i]--, a[i + 1]--, ans[++sum][0] = i, ans[sum][1] = -1;
if (sum >= 100000) {
pr();
return 0;
}
}
while (a[i] < b[i]) {
if (a[i + 1] == 9) change(i + 1, -1);
a[i]++, a[i + 1]++, ans[++sum][0] = i, ans[sum][1] = 1;
if (sum >= 100000) {
pr();
return 0;
}
}
}
pr();
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
c_p_c();
long long t;
cin >> t;
while (t--) {
long long n, m, a, b;
cin >> n >> m >> a >> b;
string s[n];
for (long long i = 0; i < n; i++) cin >> s[i];
long long cost = 0;
for (long long i = 0; i < n; i++) {
bool ok = true;
long long j;
for (j = 0; j < m - 1;) {
if (s[i][j] == s[i][j + 1] && s[i][j] == '.') {
cost += min(a * 2, b);
ok = false;
j += 2;
} else if (s[i][j] != s[i][j + 1]) {
if (s[i][j] == '.') cost += a;
j++;
} else if (s[i][j] == '*')
j++;
}
if (j == m - 1 && s[i][m - 1] == '.') {
cost += a;
}
}
cout << cost << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1000000007LL;
const int N = 1e2 + 10;
const long long Inf = 2242545357980376863LL;
const long long Log = 30;
int l[N], r[N], mk[N];
int la[N];
int a[N * N];
int Main() {
int n, k;
cin >> n >> k;
int m = n * k;
for (int i = 0; i < m; i++) cin >> a[i];
for (int i = 1; i <= n; i += k - 1) {
int lv = i;
int rv = min(n, i + k - 2);
cerr << "!! " << lv << ' ' << rv << '\n';
memset(la, -1, sizeof la);
for (int j = 0; j < m; j++) {
if (a[j] < lv || a[j] > rv) continue;
if (mk[a[j]]) continue;
if (la[a[j]] != -1) {
mk[a[j]] = 1;
r[a[j]] = j + 1;
l[a[j]] = la[a[j]] + 1;
memset(la, -1, sizeof la);
} else {
la[a[j]] = j;
}
}
}
for (int i = 1; i <= n; i++) cout << l[i] << ' ' << r[i] << '\n';
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
while (tc--) Main();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int groupId;
vector<int> friends;
Node() { groupId = -1; }
} G[2000];
int groupCount = 0, groupSize[2000], n;
int enemyX[100000], enemyY[10000], enemyCnt;
void dfs(int u);
int getMax();
int main() {
int k, u, v;
scanf("%d%d", &n, &k);
for (int i = 0; i < k; ++i) {
scanf("%d%d", &u, &v);
G[u - 1].friends.push_back(v - 1);
G[v - 1].friends.push_back(u - 1);
}
scanf("%d", &enemyCnt);
for (int i = 0; i < enemyCnt; ++i) {
scanf("%d%d", enemyX + i, enemyY + i);
--enemyX[i];
--enemyY[i];
}
int ans = getMax();
printf("%d\n", ans);
}
int getMax() {
for (int i = 0; i < n; ++i) {
if (G[i].groupId == -1) {
dfs(i);
++groupCount;
}
}
int g;
for (int i = 0; i < n; ++i) {
g = G[i].groupId;
++groupSize[g];
}
int x, y, gx, gy;
for (int i = 0; i < enemyCnt; ++i) {
x = enemyX[i];
y = enemyY[i];
gx = G[x].groupId;
gy = G[y].groupId;
if (gx == gy) groupSize[gx] = 0;
}
return *max_element(groupSize, groupSize + groupCount);
}
void dfs(int u) {
G[u].groupId = groupCount;
const int f = G[u].friends.size();
for (int i = 0; i < f; ++i) {
int v = G[u].friends[i];
if (G[v].groupId == -1) dfs(v);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
a %= 1000000007;
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
string a, b, c;
int q, n, t;
int arr[1000009];
int brr[1000009];
int main() {
cin >> a;
int len = a.size();
int s = 0;
int i;
int d;
for (int j = 0; j < len; j++)
if (a[j] == '.') d = j;
cerr << d << "f";
for (i = len - 1; i >= 0; i--) {
if (a[i] == 'e') break;
}
for (int j = i + 1; j < len; j++) {
s *= 10;
s += a[j] - '0';
}
int f = d + s;
bool fl = false;
cerr << f << " ";
string ans = "";
for (int i = 0; i < 1000000; i++) ans += '$';
ans[f] = '.';
int cur = 0;
for (int i = 0; i < len; i++) {
if (a[i] == 'e') break;
if (a[i] == '.') continue;
if (cur == f) cur++;
if (a[i] >= '0' && a[i] <= '9') {
ans[cur] = a[i];
cur++;
}
}
i = f - 1;
while (ans[i] == '$') {
ans[i] = '0';
i--;
}
bool di = false;
string fans = "";
for (int i = 1000000; i >= 0; i--) {
if (ans[i] == '$')
continue;
else if (ans[i] == '0' && di) {
fans += ans[i];
continue;
}
if (ans[i] > '0' && ans[i] <= '9' && ans[i] != '.') {
fans += ans[i];
di = true;
}
if (ans[i] == '.' && di) {
fans += ans[i];
di = true;
}
if (ans[i] == '.' && !di) di = true;
}
reverse(fans.begin(), fans.end());
cout << fans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (; t > 0; t--) {
long long int a, b, c;
cin >> a >> b;
if (a > b)
c = a - b;
else
c = b - a;
cout << c / 5 + (c % 5) / 2 + (c % 5) % 2 << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[110];
int main() {
scanf("%d%s", &n, s);
for (int i = 0; i < n; i += 2)
if (n - i <= 3) {
printf("%s\n", s + i);
break;
} else
printf("%c%c-", s[i], s[i + 1]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int a, n, ans, A[4444], B[4444];
string S;
map<long long, long long> M;
int main() {
cin >> a >> S;
for (int i = 0; i < S.size(); i++) A[i + 1] = S[i] - '0', n = i + 1;
for (int i = 1; i <= n; i++) {
B[0] = 0;
for (int j = 0; i + j <= n; j++) {
B[j + 1] = B[j] + A[i + j];
M[B[j + 1]] += 1;
}
}
map<long long, long long>::iterator i;
for (i = M.begin(); i != M.end(); i++) {
if ((*i).first == 0 || a % (*i).first == 0) {
ans += (*i).first ? (*i).second * M[a / (*i).first]
: a == 0 ? M[0] * (n * (n + 1)) / 2
: 0;
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
long long a[200020], count[1000010];
long long two[200020], cnt[1000010], sum[1000010];
int main() {
int n;
scanf("%d", &n);
two[0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
count[a[i]]++;
two[i] = (two[i - 1] * 2) % 1000000007;
}
for (int i = 1; i <= 1000000; i++) {
for (int j = i; j <= 1000000; j += i) {
cnt[i] += count[j];
}
}
for (int i = 1000000; i >= 1; i--) {
if (cnt[i] == 0) continue;
sum[i] = (cnt[i] * two[cnt[i] - 1]) % 1000000007;
for (int j = i + i; j <= 1000000; j += i)
sum[i] = ((sum[i] + 1000000007) - sum[j]) % 1000000007;
}
long long ans = 0;
for (int i = 2; i <= 1000000; i++) {
ans = (ans + (long long)i * sum[i]) % 1000000007;
}
printf("%lld", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool comp(int x, int y) { return x > y; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
int t = 1;
cin >> t;
long long m, n, k;
while (t--) {
cin >> n;
int arr[101] = {0};
for (long long i = 0; i < n; i++) {
int input;
cin >> input;
arr[input] += 1;
}
if (arr[0] == 0) {
cout << 0 << '\n';
} else {
int n1 = -1, n2 = -1;
int flag = 0;
for (long long i = 0; i < 101; i++) {
if (flag == 0) {
if (arr[i] == 0) {
n1 = n2 = i;
break;
} else if (arr[i] == 1) {
n1 = i;
flag = 1;
}
} else {
if (arr[i] == 0) {
n2 = i;
break;
}
}
}
if (n2 == -1) n2 = 101;
cout << n1 + n2 << '\n';
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int t[300005][26], st[300005], h[300005], result[300005], depth[300005];
void count_size(int v) {
st[v] = 1;
for (int i = 0; i < 26; i++) {
int x = t[v][i];
if (x) {
h[x] = h[v] + 1;
count_size(x);
st[v] += st[x];
}
}
}
vector<pair<int, int> > undo;
int merge(int to, int from) {
int ret = 0;
for (int i = 0; i < 26; i++) {
if (t[from][i]) {
if (t[to][i] == 0) {
ret += st[t[from][i]];
t[to][i] = t[from][i];
undo.emplace_back(to, i);
} else {
ret += merge(t[to][i], t[from][i]);
}
}
}
return ret;
}
void dfs(int v, bool keep) {
int mx = -1, bigChild = -1;
for (int i = 0; i < 26; i++) {
int x = t[v][i];
if (x && st[x] > mx) mx = st[x], bigChild = x;
}
for (int i = 0; i < 26; i++) {
int x = t[v][i];
if (x && x != bigChild) dfs(x, 0);
}
if (bigChild != -1) dfs(bigChild, 1);
if (bigChild == -1)
result[v] = 1;
else {
result[v] = mx;
for (int i = 0; i < 26; i++) {
int x = t[v][i];
if (x && x != bigChild) {
result[v] += merge(bigChild, x);
}
}
for (pair<int, int> p : undo) t[p.first][p.second] = 0;
}
depth[h[v]] += st[v] - result[v];
undo.clear();
}
int main() {
int n, a, b;
char c;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d %c", &a, &b, &c);
t[a][c - 'a'] = b;
}
h[1] = 1;
count_size(1);
dfs(1, 1);
int mx = -1, lvl = -1;
for (int i = 1; i <= n; i++)
if (depth[i] > mx) mx = depth[i], lvl = i;
printf("%d\n%d\n", n - mx, lvl);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n * n / 2; i++) cout << i + 1 << " " << n * n - i << " ";
cout << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int m, k, s[1005], d[1005], t, r, sum = 0, u, e, ans[1005], f;
int main() {
t = 1;
for (int i = 1; i <= t; i++) {
sum = 0;
r = 0;
cin >> m >> k;
for (int j = 1; j <= m; j++) {
cin >> d[j];
}
for (int j = 1; j <= m; j++) {
cin >> s[j];
}
for (int j = 2; j <= m + 1; j++) {
for (int y = 1; y < j; y++) {
if (s[y] > sum) {
sum = s[y];
f = y;
}
}
if (d[j - 1] > s[j - 1] + r) {
u = d[j - 1] - s[j - 1] - r;
if (u % s[f] == 0) {
r = 0;
ans[j] = (u / s[f] * k) + ans[j - 1] + d[j - 1];
} else {
e = u / s[f];
r = (e + 1) * s[f] - u;
ans[j] = ((e + 1) * k) + ans[j - 1] + d[j - 1];
}
} else {
ans[j] = ans[j - 1] + d[j - 1];
r = s[j - 1] + r - d[j - 1];
}
}
cout << ans[m + 1] << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
cout << min(n, m) + 1 << '\n';
for (int i = 0; i < min(n, m) + 1; i++)
cout << i << ' ' << min(n, m) - i << '\n';
cout.flush();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2 * 300000 + 10;
struct node {
long long m, id;
} a[maxn];
struct cmp1 {
bool operator()(node x, node y) {
if (x.m != y.m) {
return x.m < y.m;
} else {
return x.id < y.id;
}
}
};
long long tong[maxn] = {0};
long long n, k, mo, lie, hang;
long long ans = 0;
node tmp;
priority_queue<node, vector<node>, cmp1> q;
int main() {
register long long i, j;
scanf("%lld%lld", &n, &k);
for (i = 1; i <= n; i++) {
scanf("%lld", &mo);
a[i].m = mo;
a[i].id = i;
}
for (i = 1; i <= 1 + k; i++) {
q.push(a[i]);
}
lie = k + 1, hang = k + 2;
tong[q.top().id] = lie;
ans += (lie - q.top().id) * q.top().m;
q.pop();
for (i = 2; i <= n; i++) {
if (hang <= n) {
q.push(a[hang]);
}
if (hang <= n) {
hang++;
}
lie++;
ans += (lie - q.top().id) * q.top().m;
tong[q.top().id] = lie;
q.pop();
}
printf("%lld\n", ans);
for (i = 1; i <= n; i++) {
printf("%lld ", tong[i]);
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string c, s, t;
int dp[1005][55][55];
vector<int> pt, ps;
vector<vector<int> > ppt, pps;
void KMP(string a, vector<int> &p) {
int k = 0;
for (int i = 1; i < a.size(); ++i) {
while (k > 0 && a[i] != a[k]) k = p[k - 1];
if (a[k] == a[i]) ++k;
p[i] = k;
}
}
void cmp(string a, vector<int> p, vector<vector<int> > &q) {
for (int i = 0; i < a.size(); ++i)
for (char r = 'a'; r <= 'z'; ++r) {
int k = i;
while (k > 0 && a[k] != r) k = p[k - 1];
if (a[k] == r) ++k;
q[r - 'a'][i] = k;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> c >> s >> t;
s = s + ' ';
t = t + ' ';
int lc = c.size(), ls = s.size(), lt = t.size();
pps.resize(30, vector<int>(s.size() + 5));
ppt.resize(30, vector<int>(t.size() + 5));
pt.resize(t.size() + 5, 0);
ps.resize(s.size() + 5, 0);
for (int i = 0; i < lc + 5; ++i)
for (int j = 0; j < ls + 5; ++j)
for (int k = 0; k < lt + 5; ++k) dp[i][j][k] = -1000000007;
KMP(s, ps);
KMP(t, pt);
cmp(s, ps, pps);
cmp(t, pt, ppt);
dp[0][0][0] = 0;
for (int k = 0; k < lc; ++k)
for (int i = 0; i < ls; ++i)
for (int j = 0; j < lt; ++j)
for (char x = 'a'; x <= 'z'; ++x)
if (c[k] == x || c[k] == '*') {
int rs = pps[x - 'a'][i], rt = ppt[x - 'a'][j];
int r = dp[k][i][j] + (rs == ls - 1) - (rt == lt - 1);
dp[k + 1][rs][rt] = max(dp[k + 1][rs][rt], r);
}
int res = -99999999;
for (int i = 0; i < ls; ++i)
for (int j = 0; j < lt; ++j) res = max(res, dp[lc][i][j]);
printf("%d", res);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double Fib(double x, double z);
string BinDec(long long x);
string StringInt(long long x);
long long StringInt(string x);
long long BinDec(string x);
long long po(long long x, long long y, long long mod);
long long POWE(long long, long long);
int main() {
long long x;
cin >> x;
if (x == 1) {
cout << 1 << endl;
return 0;
}
unsigned long long z = 1, i = 1, mod = 1e9 + 7, c, d = x * 2;
for (i = 1; i <= x; i++) {
z *= (d - i + 1);
z %= mod;
z *= (po(i, mod - 2, mod));
z %= mod;
}
cout << (z - x) % mod << endl;
}
double Fib(double x, double z) {
int i = 0;
double f = x, r = x, t;
for (i = 2; i < z; i++) {
t = f;
f += r;
r = t;
}
return f;
}
string BinDec(long long x) {
string z;
while (1) {
if (x % 2 == 0)
z += '0';
else
z += '1';
x /= 2;
if (x == 0) break;
}
reverse(z.begin(), z.end());
return z;
}
long long BinDec(string x) {
long long i;
unsigned long long z = 1, a = 0;
for (i = x.size() - 1; i >= 0; i--) {
if (x[i] == '1') a += z;
z *= 2;
}
return a;
}
long long StringInt(string x) {
long long z = 0, i;
for (i = 0; i < x.size(); i++) {
z *= 10;
z += (x[i] - '0');
}
return z;
}
string StringInt(long long x) {
string z;
while (x != 0) {
z += (x % 10 + '0');
x /= 10;
}
reverse(z.begin(), z.end());
return z;
}
long long po(long long x, long long y, long long mod) {
long long z = 1;
long long d = y, q;
y = x;
for (int i = 0; i < 32; i++) {
q = (1 << i);
if ((d & q) != 0) {
z *= y;
z %= mod;
}
y *= y;
y %= mod;
}
return z;
}
long long POWE(long long base, long long exp) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base);
exp /= 2;
base = (base * base);
}
return res;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1 << 28;
const long long LINF = 1ll << 61;
inline long long getnum() {
register long long r = 0;
register bool ng = 0;
register char c;
c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') ng = 1, c = getchar();
while (c != ' ' && c != '\n') r = r * 10 + c - '0', c = getchar();
if (ng) r = -r;
return r;
}
template <class T>
inline void putnum(T x) {
if (x < 0) putchar('-'), x = -x;
register short a[20] = {}, sz = 0;
while (x > 0) a[sz++] = x % 10, x /= 10;
if (sz == 0) putchar('0');
for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]);
}
inline void putsp() { putchar(' '); }
inline void putendl() { putchar('\n'); }
struct edge {
int to, v, id;
};
edge make_edge(int to, int v, int id) {
edge tmp;
tmp.to = to;
tmp.v = v;
tmp.id = id;
return tmp;
}
int n;
long long sz[5011], dis[5011], ans, res = LINF;
vector<edge> con[5011];
pair<pair<int, int>, int> E[5011];
void dfs1(int x, int pre, long long &sum) {
sum += dis[x];
sz[x] = 1;
for (int i = 0; i < con[x].size(); i++) {
int v = con[x][i].to;
if (v != pre) {
dis[v] = dis[x] + con[x][i].v;
dfs1(v, x, sum);
sz[x] += sz[v];
}
}
}
long long dfs2(int x, int pre, long long sum, int t) {
long long ret = sum;
for (int i = 0; i < con[x].size(); i++) {
int v = con[x][i].to;
if (v != pre) {
ret = min(
ret,
dfs2(v, x, sum - con[x][i].v * sz[v] + con[x][i].v * (sz[t] - sz[v]),
t));
ans += con[x][i].v * sz[v] * (sz[t] - sz[v]);
}
}
return ret;
}
int main() {
n = getnum();
int x, y, z;
for (int i = 1; i < n; i++) {
x = getnum(), y = getnum(), z = getnum();
con[x].push_back(make_edge(y, z, i));
con[y].push_back(make_edge(x, z, i));
E[i] = make_pair(make_pair(x, y), z);
}
for (int i = 1; i < n; i++) {
x = E[i].first.first, y = E[i].first.second, z = E[i].second;
dis[x] = dis[y] = 0;
ans = 0;
long long sum1 = 0, sum2 = 0;
dfs1(x, y, sum1);
dfs1(y, x, sum2);
ans += z * sz[x] * sz[y];
ans += dfs2(x, y, sum1, x) * sz[y];
ans += dfs2(y, x, sum2, y) * sz[x];
res = min(res, ans);
}
putnum(res), putendl();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll, ll>;
const ll MOD = 998244353, N = 3e3 + 10;
string s, t;
ll dp[N][N];
ll n, m;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> t;
n = s.length();
m = t.length();
s = '*' + s;
t = '*' + t;
for (ll i = m; i <= n; i++) {
if (i > m or s[i] == t[m]) {
dp[i][0] = 1;
}
if (s[i] == t[1]) {
dp[i][1] = 1;
}
}
for (ll i = n; i > 1; i--) {
for (ll j = 0; j <= n - i + 1; j++) {
if (j + 1 > m or s[i - 1] == t[j + 1]) {
dp[i - 1][j + 1] += dp[i][j];
dp[i - 1][j + 1] %= MOD;
}
ll one = j + i - 1;
if (one > m or s[i - 1] == t[one]) {
dp[i - 1][j] += dp[i][j];
dp[i - 1][j] %= MOD;
}
}
}
ll ans = 0;
for (ll j = 0; j <= n; j++) {
ans += dp[1][j];
ans %= MOD;
}
cout << ans << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAXM = int(1e5) + 10;
const int MAXN = int(2e5) + 10;
int n, m, k, q;
int can[MAXN];
int X1[MAXN], Y1[MAXN], X2[MAXN], Y2[MAXN], x[MAXN], y[MAXN];
vector<int> yline[MAXM];
vector<int> yrec[MAXM];
struct Tree {
int l, r, key;
Tree *lc, *rc;
Tree(int _l, int _r) : l(_l), r(_r) {
key = 1000000000;
if (l + 1 < r) {
lc = new Tree(l, (l + r) >> 1);
rc = new Tree((l + r) >> 1, r);
}
}
void modify(int x, int y) {
if (l + 1 == r)
key = y;
else {
x < (l + r) >> 1 ? lc->modify(x, y) : rc->modify(x, y);
key = max(lc->key, rc->key);
}
}
int query(int ll, int rr) {
if (ll <= l && rr >= r) return key;
int res = -int(1e9);
if (ll < (l + r) >> 1) res = max(res, lc->query(ll, rr));
if (rr > (l + r) >> 1) res = max(res, rc->query(ll, rr));
return res;
}
} * root;
void solve() {
for (int i = 1; i <= m; i++) yline[i].clear(), yrec[i].clear();
for (int i = 0; i < k; i++) yline[y[i]].push_back(i);
for (int i = 0; i < q; i++) yrec[Y1[i]].push_back(i);
root = new Tree(1, n + 1);
for (int yy = m; yy >= 1; yy--) {
for (auto id : yline[yy]) root->modify(x[id], y[id]);
for (auto id : yrec[yy])
if (root->query(X1[id], X2[id] + 1) <= Y2[id]) can[id] = 1;
}
}
int main() {
scanf("%d%d%d%d", &n, &m, &k, &q);
for (int i = 0; i < k; i++) scanf("%d%d", x + i, y + i);
for (int i = 0; i < q; i++) scanf("%d%d%d%d", X1 + i, Y1 + i, X2 + i, Y2 + i);
solve();
swap(n, m);
for (int i = 0; i < k; i++) swap(x[i], y[i]);
for (int i = 0; i < q; i++) swap(X1[i], Y1[i]), swap(X2[i], Y2[i]);
solve();
for (int i = 0; i < q; i++) can[i] ? puts("YES") : puts("NO");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned int n, k, temp, time;
scanf("%u", &n);
scanf("%u", &k);
unsigned int* f = new unsigned int[n];
for (unsigned int i = 0; i < n; i++) scanf("%u", &f[i]);
for (unsigned int i = 0; i < n; i++) {
for (unsigned int j = i + 1; j < n; j++) {
if (f[i] < f[j]) {
temp = f[i];
f[i] = f[j];
f[j] = temp;
}
}
}
temp = 0;
time = 0;
for (unsigned int i = 0; i < n; i += k) {
time += (2 * (f[i] - 1));
}
printf("%u", time);
return 0;
};
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mn = 1200000;
int n, L, la;
int a[mn];
pair<int, int> f[mn];
void simp(int r, int s, int t) {
int k = s ^ t, i;
if (r == n) {
for (i = 1; i <= a[r]; ++i) f[++L] = make_pair(s, t);
return;
}
simp(r + 1, s, k);
for (i = 1; i <= a[r]; ++i) f[++L] = make_pair(s, t);
simp(r + 1, k, t);
}
void dfs(int r, int s, int t) {
int k = s ^ t, i;
if (r == n) {
for (i = 1; i <= a[r] - 1; ++i) f[++L] = make_pair(s, k);
f[++L] = make_pair(s, t);
for (i = 1; i <= a[r] - 1; ++i) f[++L] = make_pair(k, t);
return;
}
if (a[r] == 1) {
simp(r, s, t);
return;
}
simp(r + 1, s, t);
for (i = 1; i <= a[r]; ++i) f[++L] = make_pair(s, k);
simp(r + 1, t, s);
for (i = 1; i <= a[r]; ++i) f[++L] = make_pair(k, t);
dfs(r + 1, s, t);
}
int main() {
scanf("%d", &n);
int i, v;
L = 0, la = 0;
for (i = 1; i <= n; ++i) {
scanf("%d", &v);
if (v != la) ++L;
++a[L], la = v;
}
n = L, L = 0;
dfs(1, 1, 3);
printf("%d\n", L);
for (i = 1; i <= L; ++i) printf("%d %d\n", f[i].first, f[i].second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e6 + 100;
const long long M = 20;
const long long SQ = 320;
const long long INF = 1e16;
const long long MOD = 1e9 + 7;
const long long base = 12347;
const long long MOD1 = 1e9 + 7;
const long long MOD2 = 1e9 + 9;
int n;
string s;
char tmp[N];
long long t[N], hsh[N], rhsh[N];
int f[N];
long long get_hash(int x, int y) { return hsh[y] - 1LL * t[y - x] * hsh[x]; }
long long rev_hash(int x, int y) { return rhsh[x] - 1LL * t[y - x] * rhsh[y]; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
scanf("%s", tmp);
s = tmp;
n = s.size();
t[0] = 1;
for (int i = 1; i <= n; i++) {
t[i] = 1LL * t[i - 1] * base;
}
for (int i = 1; i <= n; i++) {
hsh[i] = (1LL * hsh[i - 1] * base + s[i - 1]);
}
for (int i = n - 1; i >= 0; i--) {
rhsh[i] = (1LL * rhsh[i + 1] * base + s[i]);
}
f[0] = 1;
long long ans = 1;
for (int i = 1; i < n; i++) {
if (get_hash(0, (i + 1) / 2) == rev_hash(i / 2 + 1, i + 1))
f[i] = f[(i - 1) / 2] + 1;
else
f[i] = 0;
ans += f[i];
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool cmax(T& a, const U& b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
bool cmin(T& a, const U& b) {
return b < a ? a = b, 1 : 0;
}
void _BG(const char* s) { cerr << s << endl; }
template <class T, class... TT>
void _BG(const char* s, T a, TT... b) {
for (size_t c = 0; *s && (c || *s != ','); cerr << *s++)
c += count(begin("([{"), end("([{"), *s) -
count(begin(")]}"), end(")]}"), *s);
cerr << " = " << a;
if (*s) {
cerr << ", ";
++s;
}
_BG(s, b...);
}
bool RD() { return 1; }
bool RD(char& a) { return scanf(" %c", &a) == 1; }
bool RD(char* a) { return scanf("%s", a) == 1; }
bool RD(double& a) { return scanf("%lf", &a) == 1; }
bool RD(int& a) { return scanf("%d", &a) == 1; }
bool RD(long long& a) { return scanf("%lld", &a) == 1; }
template <class T, class... TT>
bool RD(T& a, TT&... b) {
return RD(a) && RD(b...);
}
void PT(const char& a) { putchar(a); }
void PT(char const* const& a) { fputs(a, stdout); }
void PT(const double& a) { printf("%.16f", a); }
void PT(const int& a) { printf("%d", a); }
void PT(const long long& a) { printf("%lld", a); }
template <char s = ' ', char e = '\n'>
void PL() {
if (e) PT(e);
}
template <char s = ' ', char e = '\n', class T, class... TT>
void PL(const T& a, const TT&... b) {
PT(a);
if (sizeof...(b) && s) PT(s);
PL<s, e>(b...);
}
const int N = 1e5 + 87;
int n, p, t[N], f[N];
long long ans[N];
void add(int i, int v) {
for (; i <= n; i += i & -i) f[i] += v;
}
int qry(int i) {
int r = 0;
for (; i > 0; i -= i & -i) r += f[i];
return r;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> p;
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
greater<pair<long long, long long>>>
pq;
set<int> rd;
for (int i = 1; i <= n; ++i) {
cin >> t[i];
add(i, 1);
pq.push({t[i], i});
}
long long lt = 0;
while (pq.size()) {
long long x, i;
tie(x, i) = pq.top();
while (pq.size() && pq.top().first == x) {
tie(x, i) = pq.top();
pq.pop();
if (i < 0) {
add(-i, 1);
} else {
rd.insert(i);
}
}
if (rd.size()) {
i = *begin(rd);
if (qry(i) == i) {
add(i, -1);
ans[i] = max(lt, x) + p;
lt = ans[i];
pq.push({ans[i], -i});
rd.erase(begin(rd));
}
}
}
for (int i = 1; i <= n; ++i) cout << ans[i] << " \n"[i == n];
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int l, r, p;
bool operator<(const node &a) const {
if (l == a.l) return p < a.p;
return l < a.l;
}
} N[100001][4];
set<node> S[4];
bool dfs(set<node> L[4]) {
if (L[0].size() <= 1) return 1;
set<node> R[4];
set<node>::iterator it[4], t;
int m[4];
for (int i = 0; i < 4; i++)
it[i] = L[i].begin(), m[i] = (*it[i]).r, it[i]++, R[i].clear();
for (int i = 1; i < L[0].size(); i++)
for (int j = 0; j < 4; j++) {
if ((*it[j]).l >= m[j]) {
for (t = L[j].begin(); t != it[j];) {
int p = (*t).p;
t++;
for (int k = 0; k < 4; k++) R[k].insert(N[p][k]), L[k].erase(N[p][k]);
}
return dfs(L) && dfs(R);
}
m[j] = max(m[j], (*it[j]).r), it[j]++;
}
return 0;
}
int main() {
int a, i, j, x, y, n, m;
cin >> a;
for (i = 1; i <= a; i++) {
scanf("%d%d%d%d", &x, &y, &n, &m),
N[i][0] = {x, n, i}, N[i][1] = {-n, -x, i}, N[i][2] = {y, m, i},
N[i][3] = {-m, -y, i};
for (j = 0; j < 4; j++) S[j].insert(N[i][j]);
}
if (dfs(S))
puts("YES");
else
puts("NO");
}
| 8 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int i, b[n];
int sum = 0;
char out[n];
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
for (i = n - 1; i >= 0; i--) {
if (sum <= 0) {
sum = sum + b[i];
out[i] = '+';
} else {
sum -= b[i];
out[i] = '-';
}
}
for (i = 0; i < n; i++) {
if (sum < 0) {
if (out[i] == '+')
out[i] = '-';
else if (out[i] == '-')
out[i] = '+';
}
printf("%c", out[i]);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long d[1234567], a[1234567], b, c[1234567], c1[123456][6], e, i, j, n, k,
x, y;
pair<long long, long long> p[1234567];
vector<long long> v[1234567], v1;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
long long ans = 0;
for (int i = 3; i <= n - 2; i++)
if (a[i] == 1 && a[i - 2] == 1 && a[i + 2] == 1 && a[i - 1] == 0 &&
a[i + 1] == 0)
ans++, a[i] = 0;
for (int i = 2; i < n; i++)
if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) ans++, a[i + 1] = 0;
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
const int maxn = 1005;
const int inf = 1e7;
char mp[maxn][maxn];
int d[5][maxn][maxn];
int n, m;
int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
void work(int idx) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
d[idx][i][j] = inf;
}
}
queue<pair<int, int> > q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] - '0' == idx) {
d[idx][i][j] = 0;
q.push({i, j});
}
}
}
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int tmpx = x.first + dir[i][0];
int tmpy = x.second + dir[i][1];
if (tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m) {
if (mp[tmpx][tmpy] != '#' &&
d[idx][tmpx][tmpy] > d[idx][x.first][x.second] + 1) {
d[idx][tmpx][tmpy] = d[idx][x.first][x.second] + 1;
q.push({tmpx, tmpy});
}
}
}
}
}
int get_mi1() {
vector<int> v;
for (int i = 1; i <= 3; i++) {
for (int j = i + 1; j <= 3; j++) {
int tmpmi = inf;
for (int x = 1; x <= n; x++) {
for (int y = 1; y <= m; y++) {
if (mp[x][y] - '0' == j) {
tmpmi = min(tmpmi, d[i][x][y]);
}
}
}
42;
v.push_back(tmpmi - 1);
}
}
sort(v.begin(), v.end());
return v[0] + v[1];
}
int get_mi2() {
int mi = inf;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (mp[i][j] == '.')
mi = min(mi, d[1][i][j] + d[2][i][j] + d[3][i][j] - 2);
}
}
return mi;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", mp[i] + 1);
}
for (int i = 1; i <= 3; i++) {
work(i);
}
int mi1 = get_mi1();
int mi2 = get_mi2();
42;
int mi = min(mi1, mi2);
if (mi + 2 >= inf) {
puts("-1");
} else {
cout << mi << endl;
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int main() {
int a, b;
vector<int> factors;
scanf("%d%d", &a, &b);
int g = gcd(a, b);
for (int i = 1; i * i <= g; i++) {
if (g % i == 0) {
if (i * i == g) {
factors.push_back(i);
} else {
factors.push_back(i);
factors.push_back(g / i);
}
}
}
sort(factors.begin(), factors.end());
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
int x = upper_bound(factors.begin(), factors.end(), b) - factors.begin();
x--;
if (factors[x] <= b && factors[x] >= a)
printf("%d\n", factors[x]);
else
puts("-1");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void A4E() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
long long a, b, c[100001], d[100001];
bool check(long long x) {
long long t = 0;
for (int i = 0; i < a; i++) {
long long e = x * c[i];
if (e > d[i]) t += e - d[i];
if (t > b) return 0;
}
return 1;
}
int main() {
A4E();
cin >> a >> b;
for (int i = 0; i < a; i++) cin >> c[i];
for (int i = 0; i < a; i++) cin >> d[i];
long long r = 2e9, l = 0, ans = 2e9;
while (l <= r) {
long long mid = (r + l) / 2;
if (check(mid)) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, ans = 0;
cin >> a;
ans = a;
while (!0) {
bool f = 1;
for (int i = 2; i * i <= a; i++)
if (a % i == 0) {
f = 0;
ans += a / i;
a /= i;
break;
}
if (f) break;
}
cout << ans + 1;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
int n, m, a, b;
vector<int> g[N];
vector<int> v;
long long calc(int val) {
v.clear();
int all = (int)g[0].size();
long long sum = 0;
for (int i = 1; i < m; i++) {
if ((int)g[i].size() >= val) {
for (int j = 0; j < (int)g[i].size(); j++) {
sum += g[i][j];
all++;
if ((g[i].size() - j - 1) < val) {
for (j = j + 1; j < (int)g[i].size(); j++) {
v.push_back(g[i][j]);
}
break;
}
}
} else {
for (int j = 0; j < g[i].size(); j++) v.push_back(g[i][j]);
}
}
sort(v.begin(), v.end());
for (int i = 0; i < (int)v.size() && all < val; i++) {
all++;
sum += v[i];
}
if (all < val) return (long long)1e18;
return sum;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d%d", &a, &b);
g[a - 1].push_back(b);
}
for (int i = 0; i < m; i++) {
sort(g[i].begin(), g[i].end());
}
long long ans = 1e16;
for (int i = 1; i <= n; i++) ans = min(ans, calc(i));
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
void solve() {
long long n;
cin >> n;
long long div = 1;
long long ans = 0;
for (long long i = 1; i <= 60; i++) {
ans += (n / div);
div = div << 1;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("O2")
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
return N;
}
template <typename T>
int SIZE(const T &t) {
return t.size();
}
string to_string(string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return string({c}); }
template <size_t N>
string to_string(bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B>
string to_string(pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0) res += '\n';
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += '\n';
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0) res += '\n';
return res;
}
void dbgs() { ; }
template <typename Heads, typename... Tails>
void dbgs(Heads H, Tails... T) {
cout << to_string(H) << " | ";
dbgs(T...);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
long long k, ans, n;
cin >> k;
ans = 0;
long long a[26];
long long mn = 0;
for (long long i = 0; i < 26; i++) {
cin >> a[i];
mn = max(mn, a[i]);
}
n = s.size();
for (int i = 0; i < n; i++) {
ans += a[s[i] - 97] * (i + 1);
}
for (int i = 0; i < k; i++) ans += (i + n + 1) * mn;
cout << ans << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int const N = 301;
int n, m, dp[N][N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) scanf("%d", &dp[i][j]);
scanf("%d", &m);
for (int i = 0, a, b, c; i < m; ++i) {
scanf("%d %d %d", &a, &b, &c);
--a, --b;
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k)
dp[j][k] = min(dp[j][k],
min(dp[j][a] + c + dp[b][k], dp[j][b] + c + dp[a][k]));
long long res = 0;
for (int j = 0; j < n; ++j)
for (int k = 0; k < n; ++k) res += dp[j][k];
printf("%lld ", res / 2);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long lim = 1e18;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
long double t = pow(a * b, 1.0 / 3);
long long tt = (long long)round(t);
if (tt * tt * tt == a * b) {
if (a < tt || b < tt || (a % (long long)tt) != 0 ||
(b % (long long)tt) != 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
} else
cout << "No" << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e8;
const int LOGMAX = 18;
const int MOD = 70657;
const long double pi = acos(-1.0);
const int NMAX = 1000 + 69;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
const int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};
int n, k, dd[55];
string getrand() {
string rt = "";
for (int i = 1; i <= 9; ++i) {
if (i == 1) {
rt += char('A' + (rand() % 26));
} else {
rt += char('a' + (rand() % 26));
}
}
return rt;
}
string s;
string rss[100];
int solve() {
srand(clock());
cin >> n >> k;
for (int i = 1; i <= 55; ++i) {
rss[i] = "";
}
for (int i = 1; i <= n - k + 1; ++i) {
cin >> s;
if (s == "YES") {
dd[i] = 1;
}
}
for (int i = 1; i <= k; ++i) {
rss[i] = getrand();
}
if (!dd[1]) {
rss[2] = rss[1];
}
for (int i = 2; i <= n - k + 1; ++i) {
if (dd[i]) {
rss[i + k - 1] = getrand();
} else {
rss[i + k - 1] = rss[i];
}
}
for (int i = 1; i <= n; ++i) {
cout << rss[i] << " ";
}
cout << "\n";
return 0;
}
int main() {
cout << setprecision(6) << fixed;
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
int totalrounds = 1;
int x = 2;
while (x != n) {
x = pow(2, totalrounds);
totalrounds++;
}
totalrounds--;
int curr = 0;
while (true) {
if (a > x / 2 && b > x / 2) {
a = x - a + 1;
b = x - b + 1;
}
vector<int> v1;
v1.push_back(a);
v1.push_back(b);
sort(v1.begin(), v1.end());
int curr_half = x / 2;
curr++;
if (v1[0] < curr_half && v1[1] <= curr_half) {
if (curr_half == 0) break;
} else if (v1[0] > curr_half && v1[1] >= curr_half) {
if (curr_half == 0) break;
} else if (v1[0] <= curr_half && v1[1] >= curr_half) {
break;
}
x = x / 2;
}
if (curr == 1)
cout << "Final!";
else {
if (curr > totalrounds)
cout << "1";
else {
cout << totalrounds - curr + 1;
}
}
}
| 2 |
Subsets and Splits