solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 2E5 + 10;
const int inf = 1 << 30;
long long read() {
long long a = 0;
char b = 1, c;
do
if ((c = getchar()) == 45) b = -1;
while (c < 48 || c > 57);
do a = (a << 3) + (a << 1) + (c & 15);
while ((c = getchar()) > 47 && c < 58);
return a * b;
}
void write(long long x, char c) {
if (x < 0) putchar('-'), x = -x;
char a[20], s = 0;
do a[++s] = x % 10 | 48;
while (x /= 10);
do putchar(a[s]);
while (--s);
putchar(c);
}
int a[N], b[N];
set<int> vis;
queue<pair<int, int> > q;
int main() {
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
vis.insert(a[i]);
q.push({a[i], -1});
q.push({a[i], 1});
}
long long sum = 0;
for (int s = 0; s < m;) {
pair<int, int> x = q.front();
q.pop();
int k = x.second, w = x.first + k;
if (vis.count(w)) continue;
vis.insert(w);
b[++s] = w;
if (k > 0) {
sum += k;
q.push({x.first, k + 1});
} else {
sum -= k;
q.push({x.first, k - 1});
}
}
write(sum, '\n');
for (int i = 1; i <= m; ++i) write(b[i], ' ');
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, k, s;
int head[maxn], tot, dis[105][maxn];
vector<int> g[maxn];
struct Edge {
int to, next;
} edge[maxn << 1];
void init() {
tot = 0;
memset(head, -1, sizeof head);
memset(dis, -1, sizeof dis);
}
void addedge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
}
void bfs(int st) {
queue<int> q;
while (!q.empty()) q.pop();
for (int i = 0; i < g[st].size(); ++i) {
dis[st][g[st][i]] = 0;
q.push(g[st][i]);
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (dis[st][v] == -1) {
dis[st][v] = dis[st][u] + 1;
q.push(v);
}
}
}
}
int main() {
while (~scanf("%d%d%d%d", &n, &m, &k, &s)) {
init();
for (int i = 1; i <= n; ++i) {
int a;
scanf("%d", &a);
g[a].push_back(i);
}
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
}
for (int i = 1; i <= k; ++i) {
bfs(i);
}
int path[105];
for (int i = 1; i <= n; ++i) {
int res = 0;
for (int j = 1; j <= k; ++j) {
path[j] = dis[j][i];
}
sort(path + 1, path + k + 1);
for (int j = 1; j <= s; ++j) {
res += path[j];
}
if (i != n)
printf("%d ", res);
else
printf("%d\n", res);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
int t;
int main() {
cin >> t;
while (t--) {
long long a;
long long k;
scanf("%lld %lld", &a, &k);
for (int i = 1; i < k; i++) {
long long aa = a;
long long minv = aa % 10, maxv = aa % 10;
while (aa) {
minv = min(minv, aa % 10);
maxv = max(maxv, aa % 10);
aa /= 10;
}
if (minv == 0) break;
a += minv * maxv;
}
printf("%lld\n", a);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int INF = INT_MAX;
const long long LLINF = 1000000000000000000LL;
const long long MAX = 200005;
const long long MOD = 998244353;
long long N;
long long X[MAX], Y[MAX];
bool S[MAX];
long long DP[MAX], SDP[MAX];
long long findpos(long long n) {
long long l, r, mid;
l = 1;
r = N;
while (l < r) {
mid = (l + r) >> 1;
if (X[mid] < n) {
l = mid + 1;
} else {
r = mid;
}
}
return l;
}
void solve() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> X[i] >> Y[i] >> S[i];
}
DP[1] = (X[1] - Y[1]) % MOD;
SDP[1] = DP[1];
for (int i = 2; i <= N; i++) {
if (Y[i] > X[i - 1]) {
DP[i] = (X[i] - Y[i]) % MOD;
} else {
long long j = findpos(Y[i]);
DP[i] = (X[i] - Y[i] + SDP[i - 1] - SDP[j - 1] + 5 * MOD) % MOD;
}
SDP[i] = (SDP[i - 1] + DP[i]) % MOD;
}
long long ans = (X[N] + 1) % MOD;
for (int i = 1; i <= N; i++) {
if (S[i]) {
ans += DP[i];
ans %= MOD;
}
}
cout << ans << "\n";
}
int main() {
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
solve();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
char s[20];
long long maxm[1030000 << 2], tag[1030000 << 2], sum[1030000 << 2];
int opt[1030000], dd[1030000];
void build(int i, int l, int r) {
if (l == r) {
maxm[i] = l;
return;
}
int mid = l + r >> 1;
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
maxm[i] = max(maxm[i << 1], maxm[i << 1 | 1]);
}
inline void pushdown(int i) {
if (tag[i]) {
long long x = tag[i];
maxm[i << 1] += x;
tag[i << 1] += x;
maxm[i << 1 | 1] += x;
tag[i << 1 | 1] += x;
tag[i] = 0;
}
}
void update(int i, int l, int r, int L, int R, int k) {
if (L <= l && r <= R) {
tag[i] += k;
maxm[i] += k;
return;
}
pushdown(i);
int mid = l + r >> 1;
if (L <= mid) update(i << 1, l, mid, L, R, k);
if (mid < R) update(i << 1 | 1, mid + 1, r, L, R, k);
maxm[i] = max(maxm[i << 1], maxm[i << 1 | 1]);
}
void modify(int i, int l, int r, int pos, int k) {
if (l == r) {
sum[i] += k;
return;
}
int mid = l + r >> 1;
if (pos <= mid)
modify(i << 1, l, mid, pos, k);
else
modify(i << 1 | 1, mid + 1, r, pos, k);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
}
long long qs(int i, int l, int r, int L, int R) {
if (L <= l && r <= R) return sum[i];
int mid = l + r >> 1;
if (R <= mid) return qs(i << 1, l, mid, L, R);
if (mid < L) return qs(i << 1 | 1, mid + 1, r, L, R);
return qs(i << 1, l, mid, L, R) + qs(i << 1 | 1, mid + 1, r, L, R);
}
long long query(int i, int l, int r, int L, int R) {
if (L <= l && r <= R) return maxm[i];
pushdown(i);
int mid = l + r >> 1;
if (R <= mid)
return query(i << 1, l, mid, L, R);
else if (L > mid)
return query(i << 1 | 1, mid + 1, r, L, R);
return max(query(i << 1, l, mid, L, R), query(i << 1 | 1, mid + 1, r, L, R));
}
inline int read() {
int n = 0;
char a;
bool z = false;
while (a = getchar()) {
if (a > '9' || a < '0')
if (z)
break;
else
continue;
if (!z) z = true;
n = (n << 1) + (n << 3) + (a ^ 48);
}
return n;
}
int main() {
int Q = read(), U = 1000000, t, d, c = 0;
build(1, 1, U);
for (int i = 1; i <= Q; ++i) {
scanf("%s", s + 1);
switch (s[1]) {
case '+':
t = read();
d = read();
opt[i] = t;
dd[i] = d;
update(1, 1, U, 1, t, d);
modify(1, 1, U, t, d);
break;
case '-':
t = read();
update(1, 1, U, 1, opt[t], -dd[t]);
modify(1, 1, U, opt[t], -dd[t]);
break;
case '?':
t = read();
long long ed = query(1, 1, U, 1, t),
ans = qs(1, 1, U, min(t + 1, U), U);
printf("%lld\n", ed - ans - t);
break;
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int maxn = 200005, mod = 998244353;
int n, top, ans;
int a[maxn], stk[maxn][2];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
int val = (ans + (i == 1)) % mod;
ans = (ans - 1ll * val * a[i] % mod + mod) % mod;
while (top > 0 && stk[top][1] > a[i])
ans = (ans + 1ll * stk[top][0] * (stk[top][1] - a[i])) % mod,
val = (val + stk[top][0]) % mod, top--;
stk[++top][0] = val, stk[top][1] = a[i];
}
printf("%d\n", n % 2 == 0 ? ans : (mod - ans) % mod);
return 0;
}
| 8 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
long long sz[200005];
int main(){
ios::sync_with_stdio(false);
ll t;
cin>>t;
while(t--){
ll n,d;
ll ff;
cin>>n>>d;
ll cs=0;
for(ll i=0;i<n;i++){
cout<<(i^cs)<<endl;
cin>>ff;
if(ff==1)break;
cs=i;
}
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > pq;
long long int arr[1000005], l[1000005], r[1000005], ans[1000005];
long long int i, j, k;
int main() {
long long int n;
cin >> n;
for (i = 1; i <= n; i++) cin >> arr[i];
long long int num = 0;
for (i = 1; i <= n; i++) {
num = min(num, arr[i] - i);
ans[i] = num + i;
}
num = n + 1;
for (i = n; i > 0; i--) {
num = min(num, arr[i] + i);
ans[i] = min(ans[i], num - i);
}
long long int res = 0;
for (i = 1; i <= n; i++) res = max(res, ans[i]);
cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
multiset<int> horizontalLengths, verticalLengths;
set<int> horizontalCuts, verticalCuts;
int main() {
int w, h, n;
cin >> w >> h >> n;
horizontalLengths.insert(h);
verticalLengths.insert(w);
horizontalCuts.insert(0);
horizontalCuts.insert(h);
verticalCuts.insert(0);
verticalCuts.insert(w);
while (n--) {
char dimension;
int cutPos;
cin >> dimension >> cutPos;
if (dimension == 'H') {
int left = *(--horizontalCuts.lower_bound(cutPos));
int right = *(horizontalCuts.lower_bound(cutPos));
horizontalCuts.insert(cutPos);
horizontalLengths.erase(horizontalLengths.lower_bound(right - left));
horizontalLengths.insert(right - cutPos);
horizontalLengths.insert(cutPos - left);
} else {
int left = *(--verticalCuts.lower_bound(cutPos));
int right = *(verticalCuts.lower_bound(cutPos));
verticalCuts.insert(cutPos);
verticalLengths.erase(verticalLengths.lower_bound(right - left));
verticalLengths.insert(right - cutPos);
verticalLengths.insert(cutPos - left);
}
cout << (*(--horizontalLengths.end())) *
(long long)(*(--verticalLengths.end()))
<< endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename NT = T>
class Graph {
public:
struct EDGE {
int to;
T val;
EDGE() {}
EDGE(int TO, T V) { to = TO, val = V; }
void debug() { printf("to=%d val=%d\n", to, (int)val); }
};
int n;
vector<vector<EDGE>> g;
vector<NT> nv;
vector<int> in, out;
Graph() {}
Graph(int N) { init(N); }
inline void add(int x, int y, T v = 1) {
in[y]++, out[x]++;
g[x].push_back(EDGE(y, v));
}
void debug() {
for (int x = 1; x <= n; x++) {
printf("x=%d nv=%d\n", x, (int)nv[x]);
for (auto e : g[x]) e.debug();
}
puts("");
}
template <typename Type>
Type trans() const {
Type ans(n);
ans.n = n;
ans.in = in;
ans.out = out;
ans.g = g;
ans.nv = nv;
return ans;
}
private:
void init(int N) {
n = N;
g.clear();
nv.clear();
in.clear();
out.clear();
g.resize(N + 1);
nv.resize(N + 1);
in.resize(N + 1);
out.resize(N + 1);
}
protected:
bool equalT(const T &x, const T &y) const { return (!(x < y)) && (!(y < x)); }
};
template <typename T, typename NT = T, int STA = 0>
class Forest : public Graph<T, NT> {
public:
using Graph<T, NT>::g;
using Graph<T, NT>::n;
vector<int> root, siz, deep, fa, wson, bg, ed, nodeList;
vector<T> depv;
vector<bool> vis;
Forest() {}
Forest(int N) : Graph<T, NT>(N) { init(n); }
void setTreeInfo(int s) {
if (s) {
dfs(s, 0);
root.push_back(s);
} else {
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, 0);
root.push_back(i);
}
}
}
}
int getCentre(int s) {
assert(!vis[s]);
assert(STA == 2);
nodeList[0] = s;
fa[s] = -1;
int p = 1;
for (int i = 0; i < p; i++) {
int x = nodeList[i];
for (auto e : g[x]) {
if (!vis[e.to] && fa[x] != e.to) {
fa[nodeList[p++] = e.to] = x;
}
}
siz[x] = 1, wson[x] = 0;
}
for (int i = p - 1; i >= 0; i--) {
int x = nodeList[i];
wson[x] = max(wson[x], p - siz[x]);
if (wson[x] * 2 <= p) return x;
siz[fa[x]] += siz[x];
wson[fa[x]] = max(wson[fa[x]], siz[x]);
}
return 0;
}
private:
int cnt;
void init(int N) {
if (STA < 2) {
root.clear();
vis.clear();
depv.clear();
siz.resize(N + 1);
deep.resize(N + 1);
fa.resize(N + 1);
bg.resize(N + 1);
ed.resize(N + 1);
vis.resize(N + 1);
depv.resize(N + 1);
nodeList.resize(1);
deep[0] = cnt = 0;
} else {
vis.clear();
vis.resize(N + 1);
fa.resize(N + 1);
siz.resize(N + 1);
nodeList.resize(N + 1);
}
if (!STA) return;
wson.resize(N + 1);
}
void dfs(int x, int f) {
vis[x] = true;
deep[x] = deep[f] + 1;
fa[x] = f;
siz[x] = 1;
wson[x] = 0;
bg[x] = ++cnt;
if (STA != 1) nodeList.push_back(x);
for (auto e : g[x]) {
if (e.to != f) {
depv[e.to] = depv[x] + e.val;
dfs(e.to, x);
siz[x] += siz[e.to];
if (STA != 1) continue;
if (siz[e.to] > siz[wson[x]]) wson[x] = e.to;
}
}
ed[x] = cnt;
}
};
template <typename T, T DK = T(0), T DB = T(0), class CMP = greater<T>>
class LiChaoSegmentTree {
public:
int n;
LiChaoSegmentTree() {}
LiChaoSegmentTree(int N, T *POSV = NULL) {
n = N;
posv.resize(N + 1);
if (POSV == NULL) {
for (int i = 1; i <= n; i++) posv[i] = T(i);
} else {
for (int i = 1; i <= n; i++) posv[i] = POSV[i];
}
mv.resize((N << 2) + 1);
v.resize((N << 2) + 1);
build(1, N, 1);
}
void insert(int l, int r, int rt, int nl, int nr, pair<T, T> p) {
bool staL = cmpPos(l, v[rt], p);
bool staR = cmpPos(r, v[rt], p);
if (staL && staR) return;
int mid = (l + r) >> 1;
if (nl <= l && nr >= r) {
if ((!staL) && (!staR)) {
v[rt] = p;
} else {
if (staL) {
if (cmpPos(mid, v[rt], p))
insert(mid + 1, r, rt << 1 | 1, nl, nr, p);
else {
swap(p, v[rt]);
insert(l, mid, rt << 1, nl, nr, p);
}
} else {
if (!cmpPos(mid, v[rt], p)) {
swap(p, v[rt]);
insert(mid + 1, r, rt << 1 | 1, nl, nr, p);
} else
insert(l, mid, rt << 1, nl, nr, p);
}
}
} else {
if (nl <= mid) insert(l, mid, rt << 1, nl, nr, p);
if (nr > mid) insert(mid + 1, r, rt << 1 | 1, nl, nr, p);
}
updata(l, r, rt);
}
void insert(int nl, int nr, pair<T, T> p) {
assert(nl <= nr);
assert(1 <= nl && nr <= n);
insert(1, n, 1, nl, nr, p);
}
void insert(pair<T, T> p) { insert(1, n, p); }
T query(int l, int r, int rt, int nl, int nr) {
if (nl <= l && nr >= r) {
return mv[rt];
}
nl = max(nl, l), nr = min(nr, r);
T ans = getMx(eval(nl, v[rt]), eval(nr, v[rt]));
int mid = (l + r) >> 1;
if (nl <= mid) ans = getMx(ans, query(l, mid, rt << 1, nl, nr));
if (nr > mid) ans = getMx(ans, query(mid + 1, r, rt << 1 | 1, nl, nr));
return ans;
}
T query(int nl, int nr) {
assert(nl <= nr);
assert(1 <= nl && nr <= n);
return query(1, n, 1, nl, nr);
}
private:
CMP cmp;
vector<pair<T, T>> v;
vector<T> posv, mv;
pair<T, T> Default = make_pair(DK, DB);
inline T eval(int pos, pair<T, T> p) {
return p.first * posv[pos] + p.second;
}
inline T getMx(const T &x, const T &y) { return cmp(x, y) ? x : y; }
inline bool cmpPos(int pos, pair<T, T> x, pair<T, T> y) {
return cmp(eval(pos, x), eval(pos, y));
}
void updata(int l, int r, int rt) {
assert(rt < v.size());
mv[rt] = getMx(eval(l, v[rt]), eval(r, v[rt]));
if (l != r) mv[rt] = getMx(mv[rt], getMx(mv[rt << 1], mv[rt << 1 | 1]));
}
void build(int l, int r, int rt) {
v[rt] = Default;
if (l == r) {
mv[rt] = eval(l, v[rt]);
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
updata(l, r, rt);
}
};
const int maxn = 150000 + 5;
int n;
long long ans = 0;
Forest<int, long long, 2> F;
long long dis[maxn], val[maxn];
long long rval[maxn];
int dep[maxn];
vector<vector<int>> Q;
vector<int> q;
int mxdp = 0;
pair<int, int> stk[maxn];
void dfs(int x, int fa) {
int top = 1;
stk[0] = make_pair(x, fa);
for (int i = 0; i < top; i++) {
x = stk[i].first, fa = stk[i].second;
bool flag = true;
dis[x] = dis[fa] + F.nv[x];
dep[x] = dep[fa] + 1;
val[x] = val[fa] + dis[x];
rval[x] = F.nv[x] * dep[x] + rval[fa];
for (auto e : F.g[x]) {
if (!F.vis[e.to] && fa != e.to) {
flag = false;
stk[top++] = (make_pair(e.to, x));
}
}
mxdp = max(mxdp, dep[x]);
if (flag) q.push_back(x);
}
}
void solve(int x) {
F.vis[x] = true;
val[x] = dis[x] = rval[x] = dep[x] = 0;
q.clear();
Q.clear();
q.push_back(x);
Q.push_back(q);
mxdp = 0;
for (auto e : F.g[x])
if (!F.vis[e.to]) {
q.clear();
dfs(e.to, x);
Q.push_back(q);
}
LiChaoSegmentTree<long long> LC(mxdp + 1);
for (int i = 0; i < Q.size(); i++) {
for (auto now : Q[i]) {
long long tv = F.nv[x] * (dep[now] + 1) + val[now];
ans = max(ans, tv + LC.query(dep[now] + 1, dep[now] + 1));
}
for (auto now : Q[i]) {
LC.insert(make_pair(dis[now], rval[now]));
}
}
LC = LiChaoSegmentTree<long long>(mxdp + 1);
for (int i = Q.size() - 1; i >= 0; i--) {
for (auto now : Q[i]) {
long long tv = F.nv[x] * (dep[now] + 1) + val[now];
ans = max(ans, tv + LC.query(dep[now] + 1, dep[now] + 1));
}
for (auto now : Q[i]) {
LC.insert(make_pair(dis[now], rval[now]));
}
}
for (auto e : F.g[x])
if (!F.vis[e.to]) {
solve(F.getCentre(e.to));
}
}
int main() {
scanf("%d", &n);
F = Forest<int, long long, 2>(n);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
F.add(x, y);
F.add(y, x);
}
for (int i = 1; i <= n; i++) scanf("%lld", &F.nv[i]);
solve(F.getCentre(1));
printf("%lld\n", ans);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int M = 1e9 + 7;
int a[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(a, -1, sizeof a);
int n, m, c, u = 0;
cin >> n >> m >> c;
while (m--) {
int v;
cin >> v;
bool done = false;
if (v <= c / 2) {
for (int j = 1; j <= n && !done; j++) {
if (a[j] == -1) {
a[j] = v;
cout << j << endl;
u++;
done = 1;
} else if (a[j] > v) {
a[j] = v;
cout << j << endl;
done = 1;
}
}
} else {
for (int j = n; j >= 1 && !done; j--) {
if (a[j] == -1) {
a[j] = v;
cout << j << endl;
u++;
done = 1;
} else if (a[j] < v) {
a[j] = v;
cout << j << endl;
done = 1;
}
}
}
if (u == n) return 0;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
bool _cmp(const pair<int, int>& i, const pair<int, int>& j) {
return ((i.first == j.first) ? (i.second > j.second) : (i.first < j.first));
}
struct Node {
int x, y;
Node(int a = 0, int b = 0) : x(a), y(b) {}
bool operator<(const Node& a) const {
return ((x == a.x) ? (y > a.y) : (x < a.x));
}
};
const int LOG = 30;
const int maxn = 1e5;
vector<int> G[maxn + 5];
int par[maxn + 5][LOG + 1];
int time_in[maxn + 5], time_out[maxn + 5], timer;
int level[maxn + 5];
void lca_dfs(int u, int p, int l) {
level[u] = l;
par[u][0] = p;
time_in[u] = ++timer;
for (int v : G[u])
if (v != p) lca_dfs(v, u, l + 1);
time_out[u] = ++timer;
}
inline bool anc(int x, int y) {
return time_in[x] <= time_in[y] && time_out[y] <= time_out[x];
}
inline int lca(int x, int y) {
if (anc(y, x)) return y;
for (int j = LOG; j >= 0; --j)
if (!anc(par[y][j], x)) y = par[y][j];
return par[y][0];
}
void build_lca(int root, int N) {
lca_dfs(root, root, 0);
for (int j = 1; j <= LOG; ++j)
for (int i = 1; i <= N; ++i) par[i][j] = par[par[i][j - 1]][j - 1];
}
int N, M;
inline int cal(int a, int b, int t, int at, int bt, int ab) {
int res = level[t] - max(level[at], level[bt]) + 1;
if (at == bt) res += level[ab] - level[at];
return res;
}
void pre() { return; }
void init() {
int in;
N = getint();
M = getint();
for (int i = 2; i <= N; ++i) {
in = getint();
G[in].push_back(i);
G[i].push_back(in);
}
build_lca(1, N);
}
void sol() {
int a, b, c, ab, bc, ac, ans;
while (M--) {
a = getint(), b = getint(), c = getint();
ab = lca(a, b), bc = lca(b, c), ac = lca(a, c);
ans = cal(b, c, a, ab, ac, bc);
ans = max(ans, cal(a, c, b, ab, bc, ac));
ans = max(ans, cal(a, b, c, ac, bc, ab));
printf("%d\n", ans);
}
return;
}
int main() {
init();
sol();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
char s[200010], t[200010];
double s1[200010][26], s2[200010][26];
double ans = 0.0;
int main() {
scanf("%d\n", &n);
memset(s1, 0, sizeof(s1));
memset(s2, 0, sizeof(s2));
for (int i = 1; i <= n; i++) {
scanf("%c", &s[i]);
for (int c = 0; c < 26; c++) s1[i][c] = s1[i - 1][c];
s1[i][s[i] - 'A'] += i;
}
scanf("\n");
for (int i = 1; i <= n; i++) {
scanf("%c", &t[i]);
for (int c = 0; c < 26; c++) s2[i][c] = s2[i - 1][c];
s2[i][t[i] - 'A'] += i;
}
scanf("\n");
double total = 0.0;
for (int i = 1; i <= n; i++) {
total += 1.0 * i * i;
ans += 1.0 * (s2[i - 1][s[i] - 'A']) * (n - i + 1);
ans += 1.0 * (s1[i - 1][t[i] - 'A']) * (n - i + 1);
if (t[i] == s[i]) ans += 1.0 * i * (n - i + 1);
}
double ret = ans / total;
printf("%.12lf\n", ret);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char c[4][4];
const int N = 3e5 + 7;
bool check() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (c[i][j] == '.') return true;
}
}
return false;
}
long long psum[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
sort(a.rbegin(), a.rend());
psum[0] = a[0];
for (int i = 1; i < n; i++) {
psum[i] += psum[i - 1] + a[i];
}
if (n == 1) {
cout << psum[n - 1] << "\n";
return 0;
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += 1LL * psum[i];
}
cout << ans + psum[n - 1] - a[0] << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > servers;
void print(int l1, int r1, int l2, int r2) {
cout << "Yes" << endl << r1 - l1 + 1 << " " << r2 - l2 + 1 << endl;
for (int(i) = (l1); (i) < (r1 + 1); ++(i)) cout << servers[i].second << " ";
cout << endl;
for (int(i) = (l2); (i) < (r2 + 1); ++(i)) cout << servers[i].second << " ";
cout << endl;
}
int main() {
int n, x1, x2;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> x1 >> x2;
vector<pair<int, int> > s1, s2;
servers.resize(n);
for (int(i) = (0); (i) < (n); ++(i)) {
servers[i].second = i + 1;
cin >> servers[i].first;
}
sort(servers.begin(), servers.end());
for (int(i) = (0); (i) < (n); ++(i)) {
int bound = servers[i].first;
int end1 =
x1 / servers[i].first + i - 1 + ((x1 % servers[i].first > 0) ? 1 : 0);
int end2 =
x2 / servers[i].first + i - 1 + ((x2 % servers[i].first > 0) ? 1 : 0);
if (end1 < n) s1.push_back({i, end1});
if (end2 < n) s2.push_back({i, end2});
}
for (const pair<int, int>& p : s1) {
int idx =
lower_bound(s2.begin(), s2.end(), pair<int, int>(p.second + 1, -1)) -
s2.begin();
if (idx >= 0 && idx < (int)s2.size()) {
print(p.first, p.second, s2[idx].first, s2[idx].second);
return 0;
}
}
for (const pair<int, int>& p : s2) {
int idx =
lower_bound(s1.begin(), s1.end(), pair<int, int>(p.second + 1, -1)) -
s1.begin();
if (idx >= 0 && idx < (int)s1.size()) {
print(s1[idx].first, s1[idx].second, p.first, p.second);
return 0;
}
}
cout << "No" << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
const double eps = 1e-7, PI = 3.1415926;
const int N = 1e6;
using namespace std;
long long n, m, k, a[100100], r[100100], l[100100], q, sum, mx = -1, mn = 1e9;
string c[50009];
string s;
vector<int> vec[27];
map<char, long long> M;
int main() {
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
long long g = s[i] - 97;
vec[g].push_back(i);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> c[i];
}
for (int i = 0; i < m; i++) {
mx = -1;
M['a'] = 0;
M['b'] = 0;
M['c'] = 0;
M['d'] = 0;
M['e'] = 0;
M['f'] = 0;
M['g'] = 0;
M['h'] = 0;
M['i'] = 0;
M['j'] = 0;
M['k'] = 0;
M['l'] = 0;
M['m'] = 0;
M['n'] = 0;
M['o'] = 0;
M['p'] = 0;
M['q'] = 0;
M['r'] = 0;
M['s'] = 0;
M['t'] = 0;
M['u'] = 0;
M['v'] = 0;
M['w'] = 0;
M['x'] = 0;
M['y'] = 0;
M['z'] = 0;
for (int j = 0; j < c[i].size(); j++) {
M[c[i][j]]++;
long long h = vec[c[i][j] - 97][M[c[i][j]] - 1];
mx = max(mx, h);
}
cout << mx + 1 << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int arr[MAX];
char result[MAX];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> arr[i];
int l = 0, r = n - 1, nowmax = -1, tot = 0;
while (1) {
if (arr[l] < nowmax && arr[r] < nowmax) break;
if (l > r) break;
if (tot == n) break;
if (arr[l] < nowmax)
result[tot++] = 'R', nowmax = arr[r--];
else if (arr[r] < nowmax)
result[tot++] = 'L', nowmax = arr[l++];
else if (arr[l] <= arr[r])
result[tot++] = 'L', nowmax = arr[l++];
else if (arr[l] > arr[r])
result[tot++] = 'R', nowmax = arr[r--];
}
cout << tot << endl;
for (int i = 0; i < tot; ++i) {
cout << result[i];
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
queue<pair<int, int> > q, q1;
int read() {
char ch = getchar();
int f = 0, x = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') x = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
f = (f << 1) + (f << 3) + ch - '0';
ch = getchar();
}
return f * x;
}
int a[1005], b[2005], c[2005], d[2005];
int main() {
int n = read();
for (int i = 1; i <= n; i++) {
a[i] = read(), b[i] = read();
}
for (int i = 1; i <= n; i++) {
int x = read(), y = read();
mp[pair<int, int>(x, y)] = i;
c[i] = x;
d[i] = y;
}
for (int i = 1; i <= n; i++) {
int plax = a[1] + c[i], play = b[1] + d[i];
bool ac = 1;
for (int j = 1; j <= n; j++) {
if (!mp[pair<int, int>(plax - a[j], play - b[j])]) {
ac = 0;
break;
}
}
if (ac) {
cout << plax << " " << play << endl;
return 0;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long double PI = acosl(-1);
const long long infl = 3e18 + 100;
const int inf = 1e9 + 100;
const int nmax = 1e3 + 10;
int di[4] = {0, -1, 0, 1};
int dj[4] = {-1, 0, 1, 0};
int n, m, query;
vector<string> g;
int shurutime[nmax][nmax];
bool vis[nmax][nmax];
bool thikase(int i, int j) {
if (i < n and i >= 0 and j < m and j >= 0) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> query;
g.resize(n);
for (auto &x : g) cin >> x;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
for (int k = 0; k < 4; k++) {
int i2 = di[k] + i, j2 = j + dj[k];
if (thikase(i2, j2)) {
if (g[i][j] == g[i2][j2]) {
shurutime[i][j] = shurutime[i2][j2] = 1;
}
}
}
}
queue<pair<int, long long> > q;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (shurutime[i][j]) {
q.push({i, j});
}
}
while (q.size()) {
pair<int, long long> z = q.front();
q.pop();
int i = z.first, j = z.second;
if (vis[i][j]) continue;
vis[i][j] = true;
for (int k = 0; k < 4; k++) {
int i2 = di[k] + i, j2 = j + dj[k];
if (thikase(i2, j2)) {
if (!vis[i2][j2] and shurutime[i2][j2] == 0) {
shurutime[i2][j2] = shurutime[i][j] + 1;
q.push({i2, j2});
}
}
}
}
while (query--) {
int i, j;
long long taim;
cin >> i >> j >> taim;
i--, j--;
if (shurutime[i][j] == 0)
cout << g[i][j] << "\n";
else {
if (taim < shurutime[i][j])
cout << g[i][j] << "\n";
else {
if ((taim - shurutime[i][j]) % 2)
cout << g[i][j] << "\n";
else
cout << char((g[i][j] - '0') ^ 1 + '0') << "\n";
}
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
map<pair<long long int, long long int>, long long int> edge;
long long int a[100005];
vector<long long int> graph[100005];
long long int ans;
long long int count_node(long long int src, long long int par) {
long long int count = 1;
for (long long int i = 0; i < graph[src].size(); i++) {
if (graph[src][i] != par) {
count += count_node(graph[src][i], src);
}
}
return count;
}
void dfs(long long int src, long long int par, long long int mini,
long long int dist) {
dist += edge[{par, src}];
mini = min(mini, dist);
if (dist - mini > a[src]) {
ans += count_node(src, par);
return;
}
for (long long int i = 0; i < graph[src].size(); i++) {
if (graph[src][i] != par) {
dfs(graph[src][i], src, mini, dist);
}
}
}
int main() {
ios_base::sync_with_stdio;
cin.tie(NULL);
long long int i, j, n, t, k, m;
t = 1;
while (t--) {
cin >> n;
for (long long int i = 1; i < n + 1; i++) {
cin >> a[i];
}
for (i = 1; i <= n - 1; i++) {
long long int u;
cin >> u;
long long int v = i + 1;
graph[u].push_back(v);
graph[v].push_back(u);
long long int num;
cin >> num;
edge[{u, v}] = num;
edge[{v, u}] = num;
}
ans = 0;
dfs(1, -1, 1e10, 0);
cout << ans;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct per {
char name[15];
int v;
int id;
} p[3050];
int hi[3050] = {};
int rak[3050];
bool mycmp(per a, per b) { return a.v < b.v; }
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%s", p[i].name);
scanf("%d", &p[i].v);
p[i].id = i;
}
sort(p + 1, p + n + 1, mycmp);
for (int i = 1; i <= n; i++) {
if (p[i].v > i - 1) {
printf("-1");
return 0;
}
}
vector<int> ans;
int rkk = n;
for (int i = 1; i <= n; i++) {
ans.insert(ans.begin() + p[i].v, i);
rak[i] = rkk--;
}
for (int i = 0; i < n; i++) {
printf("%s %d\n", p[ans[i]].name, rak[ans[i]]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, m, i, j;
cin >> n >> m;
char ch[n][m];
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) cin >> ch[i][j];
}
long long int pos1, pos2, c = 0, x, y;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (ch[i][j] == 'B') {
if (c == 0) {
pos1 = i;
pos2 = j;
}
c++;
}
}
if (c == 1) {
cout << pos1 + 1 << " " << pos2 + 1 << endl;
break;
} else if (c > 1) {
x = (c / 2) + pos1 + 1;
y = (c / 2) + pos2 + 1;
cout << x << " " << y << endl;
break;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 105, MMAX = 1e4 + 5;
int n, m;
string table[NMAX];
int idx[NMAX][MMAX];
int main() {
cin >> n >> m;
for (int r = 0; r < n; ++r) {
cin >> table[r];
memset(idx[r], MMAX, sizeof(idx[r]));
bool ok = false;
for (int c = 0; c < m; ++c) {
if (table[r][c] == '1') {
idx[r][c] = 0;
ok = true;
}
}
if (!ok) {
puts("-1");
return 0;
}
for (int i = 1; i <= 2 * m; ++i)
idx[r][i % m] = min(idx[r][i % m], idx[r][(i - 1) % m] + 1);
for (int i = 2 * m; i >= 1; --i)
idx[r][i % m] = min(idx[r][i % m], idx[r][(i + 1) % m] + 1);
}
int ans = NMAX * MMAX;
for (int c = 0; c < m; ++c) {
int res = 0;
for (int r = 0; r < n; ++r) res += idx[r][c];
ans = min(ans, res);
}
cout << ans << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int m, n, res, maxi, arr[200000];
vector<long long int> v[200000];
long long int solve(vector<long long int> v, long long int x) {
sort(v.begin(), v.end());
long long int res1 = 0, res2 = 0, median;
for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
res1 += abs((*it) - x);
median = v[v.size() / 2];
for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
res2 += abs((*it) - median);
return res1 - res2;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> m >> n;
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= n - 1; i++) res += abs(arr[i] - arr[i + 1]);
for (int i = 1; i <= n; i++) {
if (i != 1 and arr[i] != arr[i - 1]) v[arr[i - 1]].push_back(arr[i]);
if (i != n and arr[i] != arr[i + 1]) v[arr[i + 1]].push_back(arr[i]);
}
for (int i = 1; i <= m; i++)
if (v[i].size()) maxi = max(maxi, solve(v[i], i));
cout << res - maxi << endl;
}
| 5 |
#include <bits/stdc++.h>
template <typename T>
bool Chkmax(T &x, T y) {
return x < y ? x = y, true : false;
}
template <typename T>
bool Chkmin(T &x, T y) {
return y < x ? x = y, true : false;
}
const int MAXN = 5e6;
int totpri;
int rub[MAXN], pi[MAXN];
int pri[MAXN];
bool flag[MAXN];
void Work(int x) {
int x0 = x, y = 0, base = 1;
while (x0) {
y = y * 10 + x0 % 10;
x0 /= 10;
base *= 10;
}
std::swap(x, y);
if (y * base + x >= MAXN) return;
rub[y * base + x] = 1;
for (int i = 0, _ = 9; i <= _; i++) {
int z = y * base * 10 + i * base + x;
if (z >= MAXN) return;
rub[z] = 1;
}
}
void Init(int N = MAXN - 1) {
for (int i = 2, _ = N; i <= _; i++) {
if (!flag[i]) {
pri[++totpri] = i;
pi[i] = 1;
}
for (int j = 1; j <= totpri && i * pri[j] <= N; j++) {
flag[i * pri[j]] = true;
if (i % pri[j] == 0) break;
}
}
for (int i = 0, _ = 9999; i <= _; i++) Work(i);
rub[0] = 0;
for (int i = 2, _ = N; i <= _; i++) {
rub[i] += rub[i - 1], pi[i] += pi[i - 1];
}
}
int main() {
if (fopen("568A.in", "r") != NULL) {
freopen("568A.in", "r", stdin);
freopen("568A.out", "w", stdout);
}
int p, q;
Init();
scanf("%d%d", &p, &q);
for (int i = MAXN - 1, _ = 1; i >= _; i--)
if ((long long)pi[i] * q <= (long long)rub[i] * p) {
printf("%d\n", i);
return 0;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<int, int> h;
vector<pair<int, int> > ans, x;
for (int i = 0; i < n; i++) {
int k;
cin >> k;
x.push_back(pair<int, int>(k, i));
}
sort(x.begin(), x.end());
x.push_back(pair<int, int>(0, n));
int cnt = 0;
for (int i = 0; i < n; i++) {
if (x[i].first == x[i + 1].first) {
int sub = x[i + 1].second - x[i].second;
if (h[x[i].first] != sub) {
cnt++;
h[x[i].first] = sub;
}
} else {
if (cnt <= 1) ans.push_back(pair<int, int>(x[i].first, h[x[i].first]));
cnt = 0;
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = LONG_LONG_MAX;
const int N = 5e5 + 5;
int fa[N], sz[N];
int find(int x) {
if (fa[x] == x)
return x;
else
return fa[x] = find(fa[x]);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1;
while (m--) {
int SZ, cur;
scanf("%d", &SZ);
if (SZ == 0) continue;
scanf("%d", &cur);
int U = find(cur);
for (int i = 1; i < SZ; i++) {
scanf("%d", &cur);
int V = find(cur);
if (V != U) {
fa[V] = U;
sz[U] += sz[V];
sz[V] = 0;
}
}
}
for (int i = 1; i <= n; i++) printf("%d ", sz[find(i)]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, m, b, g, arr1[1000], arr2[1000], c1, c2, p1, p2, max = 0, flag = 0,
temp, i;
memset(arr1, 0, sizeof(arr1));
memset(arr2, 0, sizeof(arr2));
int sum = 0;
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &b);
for (i = 0; i < b; i++) {
scanf("%d", &temp);
arr1[temp] = 1;
}
scanf("%d", &g);
for (i = 0; i < g; i++) {
scanf("%d", &temp);
arr2[temp] = 1;
}
max = (n > m) ? n : m;
while (1) {
if (max % n == 0 && max % m == 0) {
flag = max;
break;
}
++max;
}
sum = b + g;
for (i = 0; i < 101101; i++) {
int p1 = i % n;
int p2 = i % m;
if (arr1[p1] == 1 && arr2[p2] == 1)
continue;
else if (arr1[p1] == 1 && arr2[p2] == 0) {
sum++;
arr2[p2] = 1;
} else if (arr1[p1] == 0 && arr2[p2] == 1) {
sum++;
arr1[p1] = 1;
}
}
if (sum == n + m)
printf("YES\n");
else
printf("NO\n");
return 0;
}
| 11 |
#include <bits/stdc++.h>
int main() {
int n, p, q, check;
std::cin >> n;
int count = 0;
while (n--) {
std::cin >> p >> q;
check = (q - p);
if (check >= 2) {
count++;
}
}
std::cout << count << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int i, j, n;
void solve() {
long long int n;
cin >> n;
long long int ans = 0;
long long int pw = 1e9;
while (n > 0) {
while (n < pw) pw /= 10;
ans += pw;
n += -pw + pw / 10;
}
cout << ans;
cout << endl;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int bitct(long long r) {
int c = 0;
for (; r; r &= r - 1) c++;
return c;
}
long long gcd(long long x, long long y) { return x ? gcd(y % x, x) : y; }
long long choose(long long n, long long q) {
if (n == 0 || q == 0) return 1;
if (q == 1)
return n;
else
return (choose(n, q - 1) * (n - q + 1) / q);
}
template <typename T>
ostream& operator<<(ostream& o, vector<T> v) {
o << "[";
int i = 0, s = v.size();
for (; i + 1 < s; i++) o << v[i] << ", ";
if (s) o << v[i];
o << "]";
return o;
}
template <typename K, typename V>
ostream& operator<<(ostream& o, unordered_map<K, V> m) {
o << "{";
for (auto i : m) o << i.first << " -> " << i.second << ", ";
o << "}";
return o;
}
template <typename K, typename V>
ostream& operator<<(ostream& o, map<K, V> m) {
o << "{";
for (auto i : m) o << i.first << " -> " << i.second << ", ";
o << "}";
return o;
}
template <typename K>
ostream& operator<<(ostream& o, set<K> m) {
o << "{";
for (auto i : m) o << i << ", ";
o << "}";
return o;
}
template <typename K>
ostream& operator<<(ostream& o, unordered_set<K> m) {
o << "{";
for (auto i : m) o << i << ", ";
o << "}";
return o;
}
template <typename T>
ostream& printv(vector<T> v) {
int i = 0, s = v.size();
for (; i + 1 < s; i++) cout << v[i] << " ";
if (s) cout << v[i];
return cout;
}
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
long long th = 3000, tn = 100;
map<char, int> dna{{'A', 0}, {'C', 1}, {'T', 2}, {'G', 3}};
vector<vector<vector<vector<long long> > > > pres(
1 + s.size() / tn,
vector<vector<vector<long long> > >(
10, vector<vector<long long> >(10, vector<long long>(4, 0))));
for (int i = 0; i < pres.size(); i++) {
for (int j = 0; j < tn && j < s.size() - i * tn; j++) {
char me = s[tn * i + j];
long long nme = dna[me];
for (int k = 0; k < 10; k++) {
long long span = k + 1;
pres[i][k][j % span][nme]++;
}
}
}
vector<vector<vector<vector<long long> > > > pre(
1 + s.size() / th,
vector<vector<vector<long long> > >(
10, vector<vector<long long> >(10, vector<long long>(4, 0))));
for (int i = 0; i < pre.size(); i++) {
for (int j = 0; j < th && j < s.size() - i * th; j++) {
char me = s[th * i + j];
long long nme = dna[me];
for (int k = 0; k < 10; k++) {
long long span = k + 1;
pre[i][k][j % span][nme]++;
}
}
}
long long Q;
cin >> Q;
for (int q = 0; q < Q; q++) {
long long type;
cin >> type;
if (1 == type) {
long long x;
string c;
cin >> x >> c;
x--;
long long nme = dna[c[0]];
long long oldnme = dna[s[x]];
s[x] = c[0];
for (int k = 0; k < 10; k++) {
long long span = k + 1;
pre[x / th][k][(x % th) % span][nme]++;
pre[x / th][k][(x % th) % span][oldnme]--;
}
for (int k = 0; k < 10; k++) {
long long span = k + 1;
pres[x / tn][k][(x % tn) % span][nme]++;
pres[x / tn][k][(x % tn) % span][oldnme]--;
}
} else {
long long l, r;
string e;
cin >> l >> r >> e;
l--, r--;
vector<int> ee;
for (char eee : e) ee.push_back(dna[eee]);
long long match = 0;
for (long long i = l; i <= r; i++) {
if (i % th == 0 && r - i > th) {
for (int k = 0; k < e.size(); k++)
match += pre[i / th][e.size() - 1][k][ee[(i + k - l) % e.size()]];
i += th - 1;
continue;
}
if (i % tn == 0 && r - i > tn) {
for (int k = 0; k < e.size(); k++)
match += pres[i / tn][e.size() - 1][k][ee[(i + k - l) % e.size()]];
i += tn - 1;
continue;
}
if (e[(i - l) % e.size()] == s[i]) match++;
}
cout << match << endl;
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int len(const T &c) {
return (int)c.size();
}
template <class T>
void cmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
void cmax(T &a, T b) {
if (b > a) a = b;
}
int l[110], n, t;
int main() {
cin >> n;
for (int i(0), _n(n); i < _n; ++i) {
cin >> t;
l[t]++;
}
vector<int> v;
for (int i(0), _n(109); i < _n; ++i) {
if (l[i] > 1) v.push_back(l[i] - l[i] % 2);
}
sort(v.begin(), v.end());
int ans = 0;
for (int i(1), _b(len(v)); i < _b; ++i) {
ans += v[i - 1] / 2;
v[i] -= v[i - 1];
}
if (len(v)) ans += v.back() / 4;
cout << ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5e3 + 10;
char A[MAX];
int main() {
scanf("%s", (A));
int N = int(strlen(A));
vector<int> v;
for (int i = 0; i < (N - 3); ++i) {
if (A[i] == 'b' && A[i + 1] == 'e' && A[i + 2] == 'a' && A[i + 3] == 'r') {
v.push_back(i);
}
}
if (int((v).size()) == 0) {
printf("0");
return 0;
}
int res = 0;
for (int i = 0; i < (N); ++i) {
for (int j = (i + 3); j < (N); ++j) {
int a = 0, b = int((v).size());
while (a < b) {
int m = a + (b - a) / 2;
if (i <= v[m]) {
b = m;
} else {
a = m + 1;
}
}
res += (i <= v[a] && v[a] + 3 <= j);
}
}
printf("%d", res);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<char, long long> > data;
int main() {
long long T;
cin >> T;
char hehe = 'a';
while (T > 0) {
long long cost = 0;
long long i = 0;
for (i = 0; i <= T; ++i) {
T -= i;
}
data.push_back(make_pair(hehe, i));
++hehe;
}
if (data.size()) {
for (long long i = 0; i < data.size(); ++i) {
for (long long j = 0; j < data[i].second; ++j) {
cout << data[i].first;
}
}
cout << endl;
} else {
cout << 'a' << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, x, l;
cin >> n >> m;
vector<long long> v(n);
vector<vector<long long> > g(n);
vector<vector<long long> > e(m);
for (int i = 0; i < n; i++) {
v[i] = i;
g[i].push_back(i);
}
for (int i = 0; i < m; i++) {
cin >> l;
for (int j = 0; j < l; j++) {
cin >> x;
x--;
e[i].push_back(x);
}
}
for (int i = 0; i < m; i++) {
long long mx = n;
for (auto z : e[i]) mx = min(mx, v[z]);
for (auto z : e[i]) {
if (v[z] != mx) {
long long buff = v[z];
for (auto k : g[v[z]]) {
v[k] = mx;
g[mx].push_back(k);
}
while (g[buff].size()) g[buff].pop_back();
}
}
}
for (int i = 0; i < n; i++) cout << g[v[i]].size() << " ";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long d[50], n;
d[0] = 0;
d[1] = 1;
for (long long i = 2; i < 50; i++) {
d[i] = d[i - 1] + d[i - 2];
}
cin >> n;
long long a, b, c, j;
for (long long i = 0; i < 50; i++) {
if (d[i] <= n) {
j = i;
}
}
a = d[j - 1];
b = d[lower_bound(d, d + 50, n - a) - d];
c = d[lower_bound(d, d + 50, n - a - b) - d];
cout << c << " " << b << " " << a;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> par, val;
map<int, vector<int> > v;
void build(int p) {
if (val.find(p) == val.end()) {
val[p] = 0;
par[p] = p;
v[p].push_back(p);
}
}
int main() {
int q, last = 0;
scanf("%d", &q);
while (q--) {
int t;
scanf("%d", &t);
if (t == 1) {
int l, r, x;
scanf("%d%d%d", &l, &r, &x);
l ^= last;
r ^= last;
x ^= last;
if (l > r) {
swap(l, r);
}
r++;
build(l);
build(r);
if (par[l] == par[r]) {
continue;
}
x ^= val[l] ^ val[r];
l = par[l];
r = par[r];
if (v[l].size() > v[r].size()) {
swap(l, r);
}
for (auto it : v[l]) {
par[it] = r;
val[it] ^= x;
v[r].push_back(it);
}
v.erase(l);
} else {
int l, r;
scanf("%d%d", &l, &r);
l ^= last;
r ^= last;
if (l > r) {
swap(l, r);
}
r++;
build(l);
build(r);
if (par[l] != par[r]) {
printf("-1\n");
last = 1;
} else {
printf("%d\n", val[l] ^ val[r]);
last = val[l] ^ val[r];
}
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
for (int i = 0; i < n; i++) {
cin >> s;
vector<int> v(26, 0);
int c = 1;
for (int i = 0; i <= s.length() - 1; i++) {
if (s[i] == s[i + 1]) {
c++;
} else {
if (c % 2 == 1) {
v[s[i] - 'a'] = 1;
}
c = 1;
}
}
for (int i = 0; i < v.end() - v.begin(); i++) {
if (v[i] == 1) {
cout << (char)(i + 'a');
}
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long nsd(long long a, long long b) {
while (b != 0) {
a = a % b;
swap(a, b);
}
return a;
}
long long nsk(long long a, long long b) { return a / nsd(a, b) * b; }
bool cmp(long long a, long long b) { return a > b; }
void sorts(string& str) { sort(str.begin(), str.end()); }
long long sumd(long long n) {
if (n < 0) {
n = -n;
}
long long s = 0;
while (n != 0) {
n /= 10;
s++;
}
return s;
}
int suma(long long n) {
long long s = 0;
while (n != 0) {
s += n % 10;
n /= 10;
}
return s;
}
long long fact(long long n) {
if (n == 1)
return 1;
else
return n * fact(n - 1);
}
int factnum(int n) {
int c = 0;
for (int i = 5; n / i >= 1; i *= 5) c += n / i;
return c;
}
int a[101], b[101], c[101];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) cin >> c[i];
for (int i = 0; i < n - 1; i++) {
if (a[i - 1] == a[i]) a[i] = b[i];
cout << a[i] << ' ';
}
if (a[n - 1] != a[0] && a[n - 1] != a[n - 2])
cout << a[n - 1] << endl;
else if (b[n - 1] != a[0] && b[n - 1] != a[n - 2])
cout << b[n - 1] << endl;
else if (c[n - 1] != a[0] && c[n - 1] != a[n - 2])
cout << c[n - 1] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double d, r;
scanf("%lf %lf", &r, &d);
int ans = 0;
double x, y, ri;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%lf %lf %lf", &x, &y, &ri);
double dist = sqrt(x * x + y * y);
if (dist + ri <= r and (dist - ri > 0.0 ? dist - ri : 0.0) >= (r - d)) {
ans++;
}
}
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
string st(string a) {
string aa = "";
for (int i = 0; i < a.length(); i++) {
if (a[i] >= 'a' && a[i] <= 'z') aa += a[i];
if (a[i] >= 'A' && a[i] <= 'Z') aa += char(97 + a[i] - 'A');
}
return aa;
}
int main() {
string a, b, c;
cin >> a >> b >> c;
string aa = st(a), bb = st(b), cc = st(c);
vector<string> ch;
ch.push_back(aa + bb + cc);
ch.push_back(aa + cc + bb);
ch.push_back(bb + aa + cc);
ch.push_back(bb + cc + aa);
ch.push_back(cc + aa + bb);
ch.push_back(cc + bb + aa);
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int b = 0;
string ans, ans1 = "";
cin >> ans;
ans1 = st(ans);
for (int j = 0; j < 6; j++) {
b = 0;
if (ch[j].length() != ans1.length()) {
b = 1;
continue;
}
for (int l = 0; l < ans1.length(); l++)
if (ch[j][l] != ans1[l]) {
b = 1;
break;
}
if (b == 0) break;
}
if (b == 0)
cout << "ACC" << endl;
else
cout << "WA" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7, N = 1e6 + 60;
bool isPalindrome(string s) {
for (int i = 0; i < s.length(); i++) {
if (s[i] != s[s.length() - 1 - i]) return false;
}
return true;
}
bool solve(string s) {
string t = s;
for (int i = 0; i < s.length(); i++) {
t = t.back() + t;
t.pop_back();
if (s != t && isPalindrome(t)) {
return true;
}
}
return false;
}
void solve() {
string s;
cin >> s;
int nt = 0;
for (int i = 0; i < s.length(); i++) {
nt += (s[i] != s[0]);
}
if (nt > 1) {
cout << (solve(s) ? 1 : 2);
} else {
cout << "Impossible";
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = 1;
while (tt--) solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
if (!b)
d = a, x = 1LL, y = 0LL;
else
exgcd(b, a % b, d, y, x), y -= x * (a / b);
}
long long inv(long long a, long long m) {
long long d, x, y;
exgcd(a, m, d, x, y);
return d == 1LL ? (x + m) % m : -1LL;
}
vector<int> vc[100010];
long long dp[100010][2], MOD = 1000000007;
int root, vis[100010], col[100010];
void dfs(int u) {
int i, j, k, len = vc[u].size();
if (col[u] == 1) {
dp[u][1] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][1] *= dp[vc[u][i]][1];
else
dp[u][1] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][1] %= MOD;
}
} else if (col[u] == 0) {
dp[u][0] = 1;
for (i = 0; i < len; i++) {
dfs(vc[u][i]);
if (col[vc[u][i]] == 1)
dp[u][0] *= dp[vc[u][i]][1];
else
dp[u][0] *= (dp[vc[u][i]][0] + dp[vc[u][i]][1]);
dp[u][0] %= MOD;
}
for (i = 0; i < len; i++) {
if (col[vc[u][i]] == 1)
dp[u][1] +=
dp[u][0] * inv(dp[vc[u][i]][1], MOD) % MOD * dp[vc[u][i]][1];
else
dp[u][1] += dp[u][0] * inv(dp[vc[u][i]][0] + dp[vc[u][i]][1], MOD) %
MOD * dp[vc[u][i]][1];
dp[u][1] %= MOD;
}
}
dp[u][1] %= MOD;
dp[u][0] %= MOD;
}
void solve() {
int T, t, n, m, i, j, k = 0, u;
scanf("%d", &n);
for (i = 1; i < n; i++) {
scanf("%d", &u);
vc[u].push_back(i);
}
for (i = 0; i < n; i++) {
scanf("%d", &col[i]);
k += col[i];
}
if (k == 0) {
printf("0\n");
return;
}
if (k == 1) {
printf("1\n");
return;
}
dfs(0);
printf("%I64d\n", dp[0][1]);
}
int main() {
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, maxx = -2100000000, maxy = -2100000000, minx = 2100000000,
miny = 2100000000, i, x, y;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
maxx = max(maxx, x + y);
maxy = max(maxy, x - y);
minx = min(minx, x + y);
miny = min(miny, x - y);
}
printf("%d\n", maxx - minx + maxy - miny + 4);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
multiset<int> a, b;
int main() {
int n, i, q, c;
long long ans = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> q;
int c = 1e9;
int d = 1e9;
if (a.size()) {
c = *a.begin();
}
if (b.size()) {
d = *b.begin();
}
if (c > d) {
if (q > d) {
ans += (q - d);
b.erase(b.begin());
a.insert(q);
} else {
b.insert(q);
}
} else {
if (q > c) {
a.erase(a.begin());
a.insert(q);
b.insert(c);
ans += (q - c);
} else {
b.insert(q);
}
}
}
cout << ans;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int m[4][4];
vector<int> a;
void swap(int& x, int& y) {
int t = x;
x = y;
y = t;
}
int abs(int x) { return x < 0 ? -x : x; }
int func(int n, int s) {
int sum, ret = 0;
for (int i = 0; i < n; i++) {
sum = 0;
for (int j = 0; j < n; j++) sum += m[i][j];
ret += abs(sum - s);
}
for (int j = 0; j < n; j++) {
sum = 0;
for (int i = 0; i < n; i++) sum += m[i][j];
ret += abs(sum - s);
}
sum = 0;
for (int i = 0; i < n; i++) sum += m[i][i];
ret += abs(sum - s);
sum = 0;
for (int i = 0; i < n; i++) sum += m[i][n - i - 1];
ret += abs(sum - s);
return ret;
}
int main() {
bool updated;
int cur_value, new_value, n, s, sum = 0;
cin >> n;
a.resize(n * n);
for (int i = 0; i < n * n; i++) {
cin >> a[i];
sum += a[i];
}
s = sum / n;
for (;;) {
random_shuffle(a.begin(), a.end());
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) m[i][j] = a[i * n + j];
for (;;) {
updated = false;
cur_value = func(n, s);
for (int i = 0; i < n * n; i++)
for (int j = i; j < n * n; j++) {
int r1 = i / n;
int c1 = i % n;
int r2 = j / n;
int c2 = j % n;
if (m[r1][c1] == m[r2][c2]) continue;
swap(m[r1][c1], m[r2][c2]);
new_value = func(n, s);
if (new_value < cur_value) {
i = j = n * n;
updated = true;
break;
}
swap(m[r1][c1], m[r2][c2]);
}
if (!updated) break;
}
cur_value = func(n, s);
if (cur_value == 0) break;
}
cout << s << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cout << m[i][j] << " ";
cout << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<long long>;
template <class T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T>
istream &operator>>(istream &, vector<T> &);
template <class T>
ostream &operator<<(ostream &, const vector<T> &);
template <class T, class U>
istream &operator>>(istream &, pair<T, U> &);
template <class T, class U>
ostream &operator<<(ostream &, const pair<T, U> &);
template <class T>
struct Inf {
static constexpr T inf() {
return std::numeric_limits<T>::has_infinity
? std::numeric_limits<T>::infinity()
: std::numeric_limits<T>::max();
}
};
template <>
struct Inf<int> {
static constexpr int inf() { return 0x3f3f3f3f; }
};
template <>
struct Inf<long long> {
static constexpr long long inf() { return 0x3f3f3f3f3f3f3f3fLL; }
};
constexpr int INF = Inf<int>::inf();
constexpr ll BINF = Inf<ll>::inf();
int solve() {
int n;
ll m;
cin >> n >> m;
vll a(n);
cin >> a;
ll sm = accumulate(a.begin(), a.end(), 0);
cout << min(sm, m) << endl;
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for (int i = 1; i <= t; i++) solve();
return 0;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto it = v.begin(); it != v.end(); ++it) is >> *it;
return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto it = v.begin(); it != v.end();)
os << *it, os << " \n"[++it == v.end()];
return os;
}
template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
return is >> p.first >> p.second;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << p.first << ' ' << p.second;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void setIO(string name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if ((int)(name).size()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
char arr[] = {'^', '>', 'v', '<'};
char brr[] = {'^', '<', 'v', '>'};
char st, ed;
cin >> st >> ed;
long long n;
cin >> n;
long long st_ind1;
long long ed_ind1;
long long st_ind2;
long long ed_ind2;
for (long long i = 0; i < 4; i++) {
if (st == arr[i]) {
st_ind1 = i;
}
if (ed == arr[i]) {
ed_ind1 = i;
}
if (st == brr[i]) {
st_ind2 = i;
}
if (ed == brr[i]) {
ed_ind2 = i;
}
}
if (arr[(st_ind1 + n) % 4] == ed) {
if (brr[(st_ind2 + n) % 4] == ed) {
cout << "undefined" << '\n';
} else {
cout << "cw" << '\n';
}
} else if (brr[(st_ind2 + n) % 4] == ed) {
if (arr[(st_ind1 + n) % 4] == ed) {
cout << "undefined" << '\n';
} else {
cout << "ccw" << '\n';
}
} else {
cout << "undefined" << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
std::string str;
std::cin >> str;
if (str[0] == '-') {
size_t nMax = str.size() - 1;
if (str[nMax] < str[nMax - 1]) --nMax;
str.erase(nMax, 1);
}
std::cout << atoi(str.c_str());
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
void solve() {
long long n, k;
cin >> n >> k;
vector<string> mp(n);
for (auto& x : mp) cin >> x;
vector<vector<int>> mtx(n, vector<int>(n));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n - k + 1; ++j) {
bool fl = true;
for (int s = 0; s < k && fl; ++s) fl &= mp[i][j + s] == '.';
for (int s = 0; s < k && fl; ++s) mtx[i][j + s]++;
}
for (int i = 0; i < n - k + 1; ++i)
for (int j = 0; j < n; ++j) {
bool fl = true;
for (int s = 0; s < k && fl; ++s) fl &= mp[i + s][j] == '.';
for (int s = 0; s < k && fl; ++s) mtx[i + s][j]++;
}
int imx = 0, jmx = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (mtx[i][j] > mtx[imx][jmx]) {
imx = i;
jmx = j;
}
cout << imx + 1 << ' ' << jmx + 1 << endl;
}
int main() {
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long isprime(long long a) {
long long sq = sqrt(a);
for (long long i = 2; i <= sq; i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
if (a != b + 1)
cout << "NO\n";
else if (isprime(2 * a - 1)) {
cout << "YES\n";
} else
cout << "NO\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 4 * 1e5;
long long int dp[MAX];
pair<int, int> cnts[MAX];
vector<int> first, second, third;
void sortRev(vector<int> &arr) {
sort(arr.begin(), arr.end());
reverse(arr.begin(), arr.end());
}
int main() {
int n, m, w, c;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d%d", &w, &c);
if (w == 1) {
first.push_back(c);
} else if (w == 2) {
second.push_back(c);
} else if (w == 3) {
third.push_back(c);
}
}
sortRev(first);
sortRev(second);
sortRev(third);
dp[0] = 0;
for (int i = 1; i <= m; i++) {
dp[i] = dp[i - 1];
cnts[i] = cnts[i - 1];
if (cnts[i - 1].first < first.size()) {
cnts[i] = cnts[i - 1];
dp[i] = dp[i - 1] + first[cnts[i].first];
cnts[i].first++;
}
if (i >= 2) {
if (cnts[i - 2].second < second.size()) {
long long int ones = 0;
int last = cnts[i].first;
if (last >= 2) {
ones += first[last - 1] + first[last - 2];
} else if (last >= 1) {
ones += first[last - 1];
}
if ((dp[i - 2] + second[cnts[i - 2].second]) >= dp[i]) {
cnts[i] = cnts[i - 2];
dp[i] = dp[i - 2] + second[cnts[i - 2].second];
cnts[i].second++;
}
}
}
}
long long int curr = 0, ans = dp[m];
for (int i = 0; i < third.size(); i++) {
curr += third[i];
int x = m - (3 * (i + 1));
if (x >= 0) {
ans = max(ans, dp[x] + curr);
}
}
printf("%lld\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void manupulate(string &str) {
for (long long int i = 0; i < str.size(); ++i) str[i] -= 48;
}
bool isprime(long long int n) {
if (n <= 1) return false;
for (long long int i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
long long int expo(long long int a, long long int n) {
if (n == 0) return 1;
if (n == 1) return a;
long long int tmp = expo(a, n / 2);
tmp = (tmp * tmp) % (long)(1e9 + 7);
if (n & 1) tmp = (tmp * a) % (long)(1e9 + 7);
return tmp;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
;
long long int k;
cin >> k;
;
long long int ar[n], br[n];
for (long long int i = 0; i < n; ++i) cin >> ar[i];
;
for (long long int i = 0; i < n; ++i) cin >> br[i];
;
sort(ar, ar + n);
sort(br, br + n, greater<long long int>());
long long int ans = 0;
long long int x = 0;
for (long long int i = 0; i < k; ++i) {
if (br[i] > ar[i]) {
ans += br[i];
++x;
}
}
for (long long int i = x; i < n; ++i) ans += ar[i];
cout << ans << "\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long co[100009], cz[100009], uo[100009], uz[100009];
char a[100009], b[100009];
int main() {
long long n;
cin >> n;
scanf("%s", a + 1);
scanf("%s", b + 1);
for (long long i = 1; i <= n; i++) {
if (a[i] == '1') co[i] = 1;
if (a[i] == '0') cz[i] = 1;
co[i] += co[i - 1];
cz[i] += cz[i - 1];
}
long long way = 0;
for (long long i = 1; i <= n; i++) {
if (b[i] == '1') {
if (a[i] == '1')
uo[i] = 1;
else
uz[i] = 1;
uo[i] += uo[i - 1];
uz[i] += uz[i - 1];
continue;
}
if (a[i] == '0') {
way += (co[n] - co[i]);
way += uo[i - 1];
} else {
way += (cz[n] - cz[i]);
way += uz[i - 1];
}
uo[i] += uo[i - 1];
uz[i] += uz[i - 1];
}
cout << way << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, g, b, j;
cin >> n >> m;
g = max(n, m);
b = min(n, m);
cout << g + (b - 1) << "\n";
for (int i = 1; i <= m; i++) cout << "1 " << i << endl;
for (int j = 2; j <= n; j++) cout << j << " 1" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
void Print(int *a, int n) {
for (int i = 1; i < n; i++) printf("%d ", a[i]);
printf("%d\n", a[n]);
}
int e[N], nt[N];
void Gnt(string a, int *nt) {
int k = 0, p = 0, n = (int)a.size();
nt[0] = n;
for (int i = 1; i < n; i++) {
if (i + nt[i - k] < p)
nt[i] = nt[i - k];
else {
if (i >= p) p = i;
while (p < n && a[p] == a[p - i]) p++;
nt[i] = p - i;
k = i;
}
}
}
void Exd(string a, string b, int *e, int *nt) {
int k = 0, p = 0, n = (int)a.size(), m = (int)b.size();
Gnt(b, nt);
for (int i = 0; i < n; i++) {
if (i + nt[i - k] < p)
e[i] = nt[i - k];
else {
if (i >= p) p = i;
while (p < n && p - i < m && a[p] == b[p - i]) p++;
e[i] = p - i;
k = i;
}
}
}
int cycle(string a) {
int n = a.size();
Gnt(a, nt);
int t = n;
for (int i = 1; i < n; i++)
if (i + nt[i] == n) {
t = n % i ? n : i;
break;
}
return t;
}
string a, b;
int k, dp[N][2];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> a >> b >> k;
dp[0][a != b] = 1;
int n = (int)a.size();
a = a + a;
Exd(a, b, e, nt);
int x = 0, len = (int)b.size();
for (int i = 0; i < n; i++) {
if (e[i] >= len) x++;
}
int y = n - x;
for (int i = 1; i <= k; i++) {
dp[i][0] = (1LL * x * dp[i - 1][1] + 1LL * (x - 1) * dp[i - 1][0]) % mod;
dp[i][1] = (1LL * y * dp[i - 1][0] + 1LL * (y - 1) * dp[i - 1][1]) % mod;
}
printf("%d\n", dp[k][0]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
if (n <= 1) return false;
if (n == 2) return true;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int n;
cin >> n;
int num = n;
vector<int> ans;
while (!is_prime(n)) n--;
ans.push_back(n);
num -= n;
int m = num;
if (num) {
int k = 0;
while (!(is_prime(num) && is_prime(k)) && num > 0 && k <= m) num--, k++;
if (num > 0) ans.push_back(num);
if (k > 0) ans.push_back(k);
}
cout << ans.size() << "\n";
for (auto i : ans) cout << i << " ";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MXN = 21;
const int MXY = 4;
const int MXT = 12;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, t;
cin >> n >> t;
long long dp[MXN][MXY][MXT][3];
memset(dp, 0, sizeof(dp));
for (int j = 0; j < MXY - 1; j++) {
dp[0][j][0][1] = 1;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < MXY; j++) {
for (int k = 0; k <= t; k++) {
if (dp[i][j][k][0]) {
for (int y = j + 1; y < MXY - 1; y++) {
dp[i + 1][y][k][1] += dp[i][j][k][0];
dp[i + 1][y][k + 1][2] += dp[i][j][k][0];
}
dp[i + 1][3][k + 1][2] += dp[i][j][k][0];
for (int y = 0; y < j; y++) {
dp[i + 1][y][k][0] += dp[i][j][k][0];
}
}
if (dp[i][j][k][1]) {
for (int y = j + 1; y < MXY - 1; y++) {
dp[i + 1][y][k][1] += dp[i][j][k][1];
dp[i + 1][y][k + 1][2] += dp[i][j][k][1];
}
dp[i + 1][3][k + 1][2] += dp[i][j][k][1];
}
if (dp[i][j][k][2]) {
for (int y = 0; y < j; y++) {
dp[i + 1][y][k][0] += dp[i][j][k][2];
}
}
}
}
}
long long ans = 0;
for (int j = 0; j < MXY - 1; j++) {
ans += dp[n - 1][j][t][0];
}
cout << ans;
}
| 5 |
#include <bits/stdc++.h>
#define ll long long
#define R return
#define pb push_back
#define F first
#define S second
#define B break
#define C continue
#define SI size()
using namespace std;
ll x,INF=1e9+7,y,n;
set <ll> s;
int main()
{
ll t;
cin>>t;
while(t--){
ll h=1;
s.clear();
cin>>n;
s.insert(-INF);
s.insert(INF);
cin>>x;
s.insert(x);
y=x;
for(ll i=1;i<n;i++){
cin>>x;
auto mt=s.lower_bound(y);
auto ms=s.upper_bound(y);
auto it=s.upper_bound(x);
auto it2=s.lower_bound(x);
//cout<<(*it)<<" "<<(*it2)<<" "<<*ms<<" "<<*mt<<endl;
if(((*it2)==(*(ms)))||((*it2)==(*(mt)))||((*it)==(*(ms)))||((*it)==(*(mt)))){
s.insert(x);
y=x;
}
else{
h=0;
}
}
if(h==0){cout<<"NO\n";C;}
cout<<"YES\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, flag;
string s;
bool OK(long long l, long long r) {
printf("%lld %lld\n", l, r);
cin >> s;
if (s[0] == 'N')
return false;
else if (s[0] == 'Y') {
if (l == r) flag = 1;
return true;
}
}
int main() {
srand(time(NULL));
cin >> n >> k;
long long l, r, mid;
l = 1, r = n;
while (1) {
mid = (l + r) / 2;
if (OK(l, mid)) {
r = mid;
if (flag) break;
} else {
l = mid + 1;
}
l = max((long long)1, l - k);
r = min(n, r + k);
mid = rand() % (r - l + 1) + l;
OK(mid, mid);
if (flag) break;
l = max((long long)1, l - k);
r = min(n, r + k);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = int(1e6) + 3;
const int MOD = 7 + int(1e9);
const double PI = 4 * atan(1);
const double eps = 1e-10;
const long long oo = 1e10;
struct adj {
int head[N], nxt[N], to[N], ecnt, n;
int ti[N], col[N];
void addEdge(int a, int b, int color = 0, int t = 0) {
nxt[ecnt] = head[a];
head[a] = ecnt;
ti[ecnt] = t;
col[ecnt] = color;
to[ecnt++] = b;
}
void init() {
ecnt = 0;
memset(head, -1, n * sizeof(head[0]));
}
} adj, vadj;
int vis[N], vid = 0, nodes[N], ndcnt = 0, comp[N], comps;
int notsrc[N], notsink[N], dfst[N], low[N], curt;
int val[N];
void dfs(int u) {
vis[u] = vid;
low[u] = dfst[u] = curt++;
nodes[ndcnt++] = u;
comp[u] = -1;
for (int e = adj.head[u], v; ~e && (v = adj.to[e], 1); e = adj.nxt[e]) {
if (vis[v] != vid) {
dfs(v);
low[u] = (low[u] < low[v] ? low[u] : low[v]);
} else {
if (comp[v] == -1) low[u] = (low[u] < low[v] ? low[u] : low[v]);
}
}
if (low[u] == dfst[u]) {
do {
comp[nodes[--ndcnt]] = comps;
} while (nodes[ndcnt] != u);
comps++;
}
}
void tarjan() {
ndcnt = 0;
vid++;
curt = 0;
comps = 0;
for (int i = 0; i < adj.n; i++)
if (vis[i] != vid) dfs(i);
}
int var(int x) { return 2 * x; }
int notvar(int x) { return x ^ 1; }
void addor(int x, int y) {
adj.addEdge(notvar(x), y);
adj.addEdge(notvar(y), x);
}
void buildsol(int u, int par = -1) {
if (vis[u] == vid) return;
vis[u] = vid;
for (int e = adj.head[u], v; ~e && (v = adj.to[e], 1); e = adj.nxt[e]) {
buildsol(v, u);
}
if (val[comp[u]] == -1 && (par == -1 || comp[u] != comp[par])) {
val[comp[u]] = 1;
val[comp[notvar(u)]] = 0;
}
}
int n, m;
int va[N];
bool ok(int cur_t) {
adj.n = 6 * m;
adj.init();
vector<int> ve;
vector<int> vee;
vector<int> vaa;
int cn = m;
for (int u = 0; u < n; u++) {
ve.clear();
vee.clear();
vaa.clear();
for (int e = vadj.head[u], v; ~e && (v = vadj.to[e], 1); e = vadj.nxt[e]) {
if (vadj.ti[e] > cur_t) {
adj.addEdge(var(e / 2), notvar(var(e / 2)));
}
if (vadj.col[e] == va[u]) {
vaa.push_back(e / 2);
}
ve.push_back(cn++);
vee.push_back(e / 2);
}
if (vaa.size()) {
adj.addEdge(notvar(var(vaa[0])), var(vaa[1]));
adj.addEdge(notvar(var(vaa[1])), var(vaa[0]));
}
for (int i = 0; i < ve.size(); i++) {
adj.addEdge(notvar(var(ve[i])), notvar(var(vee[i])));
adj.addEdge(var(vee[i]), var(ve[i]));
if (i > 0) {
adj.addEdge(notvar(var(ve[i])), notvar(var(ve[i - 1])));
adj.addEdge(var(ve[i - 1]), var(ve[i]));
}
if (i < ve.size() - 1) {
adj.addEdge(var(ve[i]), notvar(var(vee[i + 1])));
adj.addEdge(var(vee[i + 1]), notvar(var(ve[i])));
}
}
}
tarjan();
for (int i = 0; i < adj.n; i += 2) {
if (comp[i] == comp[i + 1]) return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(va, -1, sizeof(va));
memset(val, -1, sizeof(val));
int a, b, t, c;
cin >> n >> m;
vadj.n = n;
vadj.init();
for (int i = 0; i < m; i++) {
cin >> a >> b >> c >> t;
a--;
b--;
vadj.addEdge(a, b, c, t);
vadj.addEdge(b, a, c, t);
}
for (int u = 0; u < n; u++) {
map<int, int> maa;
for (int e = vadj.head[u], v; ~e && (v = vadj.to[e], 1); e = vadj.nxt[e]) {
maa[vadj.col[e]]++;
}
for (auto t : maa) {
if (t.second >= 3) {
cout << "No\n";
return 0;
}
if (t.second == 2) {
if (va[u] == -1)
va[u] = t.first;
else {
cout << "No\n";
return 0;
}
}
}
}
int l = 0;
int r = 1e9 + 5;
while (l < r - 1) {
int mid = (l + r) / 2;
if (ok(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
if (l > 1e9) {
cout << "No\n";
}
int ans = r;
if (ok(l)) {
ans = l;
} else {
ok(r);
}
cout << "Yes\n";
cout << ans << " ";
int coun = 0;
vid++;
for (int i = 0; i < adj.n; i++) {
if (vis[i] == vid) continue;
buildsol(i);
}
for (int i = 0; i < m; i++) {
if (val[comp[var(i)]] == 1) {
coun++;
}
}
cout << coun << endl;
for (int i = 0; i < m; i++) {
if (val[comp[var(i)]] == 1) {
cout << i + 1 << " ";
}
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int spf[1000002];
void sieve() {
spf[1] = 1;
for (int i = 2; i < 1000002; i++) spf[i] = i;
for (int i = 4; i < 1000002; i += 2) spf[i] = 2;
for (int i = 3; i * i < 1000002; i++) {
if (spf[i] == i) {
for (int j = i * i; j < 1000002; j += i)
if (spf[j] == j) spf[j] = i;
}
}
}
vector<int> getFactorization(int x) {
vector<int> ret;
while (x != 1) {
ret.push_back(spf[x]);
x = x / spf[x];
}
return ret;
}
int main() {
sieve();
vector<int> v;
vector<int> v1;
int i, j, k, p, s, n, minf, b[1000003];
scanf("%d", &n);
for (i = 1; i <= n; i++) b[i] = 0;
v = getFactorization(n);
minf = n;
i = 0;
while (i < v.size()) {
for (j = n - v[i] + 1; j < n; j++) {
if (b[j] == 1)
break;
else {
b[j] = 1;
v1 = getFactorization(j);
if (v1.size() > 1) {
sort(v1.begin(), v1.end());
if (j - v1[v1.size() - 1] + 1 < minf)
minf = j - v1[v1.size() - 1] + 1;
} else {
if (v1[0] < minf) minf = v1[0];
}
v1.clear();
}
}
i += upper_bound(v.begin(), v.end(), v[i]) -
lower_bound(v.begin(), v.end(), v[i]);
}
printf("%d\n", minf);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T GCD(T a, T b) {
a = abs(a);
b = abs(b);
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
template <typename T>
inline T LCM(T x, T y) {
T tp = GCD(x, y);
if ((x / tp) * 1. * y > 9e18) return 9e18;
return (x / tp) * y;
}
template <typename T>
inline T BigMod(T A, T B, T M) {
T ret = 1;
while (B) {
if (B & 1) ret = (ret * A) % M;
A = (A * A) % M;
B = B >> 1;
}
return ret;
}
template <typename T>
inline T InvMod(T A, T M) {
return BigMod(A, M - 2, M);
}
long long gcdr(long long a, long long b) {
if (a == 0) return b;
return gcdr(b % a, a);
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
void FastIO() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
}
const long long size3 = 300005;
const long long size2 = 200005;
const long long size1 = 100005;
int main() {
FastIO();
long long t, i, j, m, x, y, n;
cin >> t;
while (t--) {
cin >> n;
vector<string> V;
long long one = 0, zero = 0;
long long zore = 0;
for (i = 0; i < n; i++) {
string s;
cin >> s;
for (j = 0; j < s.size(); j++) {
if (s[j] == '1') {
one++;
} else {
zero++;
}
}
if (s.size() % 2 == 0) {
zore++;
}
}
if (zore == n && (one % 2 == 1 || zero % 2 == 1)) {
cout << (n - 1) << endl;
} else {
cout << n << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 1000005;
const double PI = 4 * atan(1);
const double eps = 1e-7;
const long long oo = 1e18;
long long n, b, g;
int main() {
ios::sync_with_stdio(0);
cin >> b >> g >> n;
b = (b < n ? b : n);
g = (g < n ? g : n);
long long x = 0, y = b;
long long a = n - g, c = n + 1;
a = (a > x ? a : x);
b = (b < y ? b : y);
cout << b - a + 1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> divisors[301];
int leastPrimeDivisor[1000001], numberToIndex[1000001], dp[301][301], a[301],
parent[301];
map<vector<int>, int> vectorToIndex;
void Initialise() {
for (int i = 2; i < 1000001; ++i) {
for (int j = i; j < 1000001; j += i) {
if (leastPrimeDivisor[j] == 0) {
leastPrimeDivisor[j] = i;
}
}
}
a[0] = 1;
for (int i = 1; i < 1000001; ++i) {
vector<int> v;
for (int x = i; x > 1;) {
int k = 1;
for (int p = leastPrimeDivisor[x]; x % p == 0; ++k, x /= p)
;
v.push_back(k);
}
sort(v.begin(), v.end());
int k;
if (vectorToIndex.count(v) == 0) {
k = vectorToIndex.size();
vectorToIndex[v] = k;
} else {
numberToIndex[i] = vectorToIndex[v];
continue;
}
numberToIndex[i] = k;
if (v.size()) {
a[k] = v.back();
v.pop_back();
parent[k] = vectorToIndex[v];
}
}
for (int i = 1; i < 301; ++i) {
for (int j = i; j < 301; j += i) {
divisors[j].push_back(i);
}
}
}
void PreProcess() {
int m = vectorToIndex.size();
for (int j = 1; j < m; ++j) {
dp[1][j] = a[j] + dp[1][parent[j]];
}
for (int i = 2; i < 301; ++i) {
for (int j = 0; j < m; ++j) {
int ans = INT_MAX, x = a[j], k = parent[j];
for (auto d : divisors[i]) {
if (j == 0 && d == 1) {
continue;
}
ans = min(ans, abs(d - x) + dp[(i / d)][k]);
}
dp[i][j] = ans;
}
}
}
int main() {
int t;
scanf("%d", &t);
Initialise();
PreProcess();
while (t--) {
int i, j, ans = INT_MAX;
scanf("%d%d", &i, &j);
i = numberToIndex[i], j = numberToIndex[j];
for (int d = 1; d < 301; ++d) {
ans = min(ans, dp[d][i] + dp[d][j]);
}
printf("%d\n", ans);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int n, m, mn, ss;
long long dp[2005][2005][2];
string s;
long long fun(int now, int b, int l, int p, int neg) {
if (now < 0 || l > n || neg > n - m || p > n - m) return 0;
if (l == n) return (now == 0 && b);
long long &ans = dp[p][neg][b];
if (ans != -1) return ans;
ans = 0;
ans += fun(now - 1, b, l + 1, p, neg + 1) % 1000000007;
ans += fun(now + 1, b, l + 1, p + 1, neg) % 1000000007;
if (!b && now + mn >= 0) ans += fun(now + ss, 1, l + m, p, neg) % 1000000007;
return ans % 1000000007;
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(')
ss++;
else
ss--;
mn = min(ss, mn);
}
memset(dp, -1, sizeof dp);
cout << fun(0, 0, 0, 0, 0) % 1000000007;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s1 = 0, s2 = 0, par = 0;
pair<int, int> temp;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> temp.first >> temp.second;
s1 += temp.first;
s2 += temp.second;
if ((temp.first + temp.second) % 2) par++;
}
if (s1 % 2 == 0 && s2 % 2 == 0)
cout << 0;
else if ((s1 + s2) % 2)
cout << -1;
else if (s1 % 2 && s2 % 2) {
if (par)
cout << 1;
else
cout << -1;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f;
const double pi = 3.1415926535897932384626;
inline long long read() {
long long 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 << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
const int maxn = 1e2 + 5;
int t, n, k, l, deep[maxn];
int main() {
t = read();
while (t--) {
n = read(), k = read(), l = read();
int flag = 1, now = -k;
for (int i = 1; i <= n; i++) deep[i] = read();
for (int i = 1; i <= n; i++) {
if (!flag) break;
int tmp = l - deep[i];
if (tmp < 0)
flag = 0;
else {
if (tmp >= k)
now = -k;
else
now = max(now + 1, -tmp);
if (now > tmp) flag = 0;
}
}
if (flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int n, k, p, T, ans, g;
int quickmi(int a, int b) {
int t = 1;
while (b) {
if (b & 1) t = 1ll * t * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return t;
}
int gcd(int a, int b) { return (b) ? gcd(b, a % b) : a; }
pair<int, int> solve(int n, int k, int a_k, int a_b, int b_k, int b_b) {
if (a_k < 0) a_k += mod;
if (n == 1) return make_pair(1ll * a_b * quickmi(1 - a_k, mod - 2) % mod, 0);
pair<int, int> ans;
int a = n / k;
if (a_k == 1)
ans =
solve(k, n % k, quickmi(b_k, mod - 2),
(1ll * (-a + 1) * a_b - 1ll * b_b * quickmi(b_k, mod - 2)) % mod,
quickmi(b_k, mod - 2),
(-1ll * a * a_b - 1ll * b_b * quickmi(b_k, mod - 2)) % mod);
else {
int axk = quickmi(quickmi(a_k, a - 1), mod - 2),
axb = 1ll * (1 - quickmi(a_k, a - 1)) * quickmi(a_k - 1, mod - 2) %
mod * a_b % mod * axk % mod;
int bxk = quickmi(quickmi(a_k, a), mod - 2),
bxb = 1ll * (1 - quickmi(a_k, a)) * quickmi(a_k - 1, mod - 2) % mod *
a_b % mod * bxk % mod;
ans = solve(k, n % k, 1ll * quickmi(b_k, mod - 2) * axk % mod,
(axb - 1ll * quickmi(b_k, mod - 2) * axk % mod * b_b) % mod,
1ll * quickmi(b_k, mod - 2) * bxk % mod,
(bxb - 1ll * quickmi(b_k, mod - 2) * bxk % mod * b_b) % mod);
}
int an = 1ll * quickmi(a_k, a) * ans.second % mod;
if (a_k == 1)
an = (an + 1ll * (n % k) * a_b % mod * a) % mod;
else
an = (an + 1ll * (n % k) * a_b % mod * (quickmi(a_k, a) - 1) % mod *
quickmi(a_k - 1, mod - 2)) %
mod;
if (a_k != 1) {
int ik = quickmi(a_k - 1, mod - 2);
an = (an +
(1ll * (quickmi(a_k, a) - a_k) * ik - a + 1) % mod * ik % mod * a_b %
mod * k +
1ll * (quickmi(a_k, a) - 1) * ik % mod * ans.first) %
mod;
} else
an = (an + 1ll * ans.first * a +
1ll * a * (a - 1) / 2 % mod * a_b % mod * k) %
mod;
return make_pair(an, ans.first);
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &k);
p = quickmi(2, mod - 2);
g = gcd(n, k);
n /= g;
k /= g;
if (n == 1 && k == 1) {
printf("%d\n", quickmi(p, mod - 2));
continue;
}
ans = 1ll * quickmi(n, mod - 2) * solve(n, k, 1, 1, 1 - p, 1).first % mod;
printf("%d\n", (ans + mod) % mod);
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int input[555555];
int n, i, m, j, answer = 0;
int start, last;
int main() {
cin >> n;
for (i = 0; i < n; i++) cin >> input[i];
for (i = 0; i < n; i++) {
if (input[i + 1] != input[i]) {
start = i;
i++;
while (input[i] != input[i + 1] && (i + 1) < n) i++;
last = i;
if ((last - start + 1) % 2) {
for (j = start; j <= last; j++) input[j] = input[start];
answer =
answer > (last - start + 1) / 2 ? answer : (last - start + 1) / 2;
} else {
for (j = start; j <= (last - start + 1) / 2 + start - 1; j++)
input[j] = input[start];
for (; j <= last; j++) input[j] = input[last];
answer = answer > (last - start + 1) / 2 - 1
? answer
: (last - start + 1) / 2 - 1;
}
}
}
cout << answer << endl;
cout << input[0];
for (i = 1; i < n; i++) cout << " " << input[i];
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
std::ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
long long b[5];
for (long long i = 0; i < 5; i++) cin >> b[i];
long long j = 0, sum = 0;
long long mp[5] = {};
for (long long i = 0; i < n; i++) {
sum += arr[i];
if (sum >= b[4]) {
mp[4] += sum / b[4];
sum %= b[4];
}
if (sum >= b[3]) {
mp[3] += sum / b[3];
sum %= b[3];
}
if (sum >= b[2]) {
mp[2] += sum / b[2];
sum %= b[2];
}
if (sum >= b[1]) {
mp[1] += sum / b[1];
sum %= b[1];
}
if (sum >= b[0]) {
mp[0] += sum / b[0];
sum %= b[0];
}
}
for (auto x : mp) cout << x << " ";
cout << "\n";
cout << sum;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int const nmax = 5000;
int const sigma = 26;
int const modulo = 1000000007;
int v[5 + nmax], frec[5 + nmax];
int dp[5 + nmax], sum[5 + nmax];
int dp2[5 + nmax], sum2[5 + nmax];
int getsum(int x, int y) {
if (x == 0)
return sum[y];
else
return (modulo + sum[y] - sum[x - 1]) % modulo;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
dp[0] = sum[0] = 1;
for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + dp[i];
for (int i = 1; i <= n; i++) {
char val;
cin >> val;
v[i] = val - 'a' + 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sigma; j++) frec[j] = -1;
for (int j = 1; j <= n; j++) {
dp2[j] = getsum(frec[v[j]] + 1, j);
sum2[j] = (sum2[j - 1] + dp2[j]) % modulo;
frec[v[j]] = j;
}
for (int j = 0; j <= n; j++) {
dp[j] = dp2[j];
sum[j] = sum2[j];
}
}
cout << sum[n];
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n;
void Correct() {
for (int i = 0; i < n - 1; i++) cout << i + 1 << " " << i + 2 << "\n";
}
void In_Correct() {
if (n % 2 == 0) {
for (int i = 2; i <= n / 2 + 1; i++)
cout << "1"
<< " " << i << "\n";
for (int i = n / 2 + 2; i <= n; i++)
cout << "2"
<< " " << i << "\n";
} else {
for (int i = 2; i <= (n / 2) + 1; i++)
cout << "1"
<< " " << i << "\n";
for (int i = (n / 2) + 2; i <= n; i++)
cout << "2"
<< " " << i << "\n";
}
}
int main() {
cin >> n;
if (n == 1 || n == 2 || n == 3 || n == 4 || n == 5)
cout << "-1"
<< "\n";
else
In_Correct();
Correct();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % mod;
y >>= 1ll;
x = x * x % mod;
}
return ret;
}
long long qpow(long long x, long long y, long long m) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % m;
y >>= 1ll;
x = x * x % m;
}
return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long n, k, g, ans, a[500010], b[500010], m[500010], fa[500010][25];
int main() {
cin >> n;
for (register int i = n; i >= 1; --i) a[i] = read(), a[n + i] = a[i];
for (register int i = 1; i <= n << 1; ++i) {
a[i] = i + a[i];
if (a[i] > (n << 1)) a[i] = n << 1;
}
for (register int i = n << 1; i >= 1; --i) {
for (int j = a[i]; j; j -= j & -j)
if (a[m[j]] > a[b[i]]) b[i] = m[j];
for (int j = i; j <= n + n; j += j & -j)
if (a[i] > a[m[j]]) m[j] = i;
if (!b[i]) b[i] = i;
}
for (register int i = n << 1; i >= 1; --i) {
fa[i][0] = b[i];
for (register int j = 0; j <= 20; ++j) fa[i][j + 1] = fa[fa[i][j]][j];
}
for (register int i = 1; i <= n; ++i) {
g = i + n - 1;
k = i;
for (register int j = 20; j >= 0; --j)
if (a[fa[k][j]] < g) k = fa[k][j], ans += 1 << j;
ans++;
if (a[k] < g) ++ans;
}
cout << ans << '\n';
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
if (n == 1) {
cout<<1<<endl;
return 0;
}
if (n == 0) {
cout<<2<<endl;
return 0;
}
cout<<2-n*n<<endl;
return 0;
int SZ = (n+100)*1024*1000/4;
vector<int> v(SZ);
for (int i=0; i<SZ; i++) v[i]= rand();
if (v[rand()%SZ] == 0) cout<<2<<endl;
else cout<<1<<endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n, sx, sy, dx, dy, t, mod;
struct mat {
long long m[15][15];
mat() { memset(m, 0, sizeof m); }
long long *operator[](long long r) { return m[r]; }
const long long *operator[](long long r) const { return m[r]; }
void build() {
memset(m, 0, sizeof m);
for (long long i = 1; i <= 10; ++i) m[i][i] = 1;
}
} p, q, ans;
mat operator*(const mat &a, const mat &b) {
mat res;
for (long long i = 1; i <= 6; ++i)
for (long long j = 1; j <= 6; ++j)
for (long long l = 1; l <= 6; ++l)
res[i][j] = (res[i][j] % mod + a[i][l] * b[l][j] % mod) % mod;
return res;
}
mat pow(mat &a, long long n) {
mat res;
res.build();
while (n) {
if (n & 1) res = res * a;
a = a * a;
n >>= 1;
}
return res;
}
signed main() {
scanf("%lld%lld%lld%lld%lld%lld", &n, &sx, &sy, &dx, &dy, &t);
if (!t) {
printf("%lld %lld\n", sx, sy);
return 0;
}
mod = n;
p[1][1] = sx - 1, p[2][1] = sy - 1, p[3][1] = dx, p[4][1] = dy, p[5][1] = 0,
p[6][1] = 1;
q[1][1] = q[1][6] = q[2][2] = q[2][6] = q[3][6] = q[4][6] = 2;
q[1][2] = q[1][3] = q[1][5] = q[2][1] = q[2][4] = q[2][5] = q[3][1] =
q[3][2] = q[3][3] = q[3][5] = q[4][1] = q[4][2] = q[4][4] = q[4][5] =
q[5][5] = q[5][6] = q[6][6] = 1;
ans = pow(q, t) * p;
printf("%lld %lld\n", (ans[1][1] + mod) % mod + 1,
(ans[2][1] + mod) % mod + 1);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1E-8;
const int dx4[4] = {1, 0, 0, -1};
const int dy4[4] = {0, -1, 1, 0};
const int inf = 0x3f3f3f3f;
const int N = 2E5 + 7;
int n;
int a[N];
long long dis[N];
vector<pair<int, long long> > edge[N];
vector<pair<long long, int> > path;
int ans[N];
void dfs(int u, int pre) {
ans[u]++;
int idx =
lower_bound(path.begin(), path.end(), make_pair(dis[u] - a[u], -1)) -
path.begin() - 1;
if (idx >= 0) ans[path[idx].second]--;
path.push_back(make_pair(dis[u], u));
for (auto x : edge[u]) {
int v = x.first;
if (v == pre) continue;
dis[v] = dis[u] + x.second;
dfs(v, u);
ans[u] += ans[v];
}
path.pop_back();
}
int main() {
cin >> n;
memset(dis, 0, sizeof(dis));
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 2; i <= n; i++) {
int u, v;
long long w;
u = i;
scanf("%d%lld", &v, &w);
edge[u].push_back(make_pair(v, w));
edge[v].push_back(make_pair(u, w));
}
dfs(1, 0);
for (int i = 1; i <= n; i++) printf("%d ", ans[i] - 1);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 5;
long long arr[maxn];
void solve() {
long long n, m;
scanf("%lld", &n);
if (n % 4) {
puts("! -1");
return;
}
long long start, end;
printf("? %lld\n", 1ll);
fflush(stdout);
scanf("%lld", &start);
printf("? %lld\n", 1 + n / 2);
fflush(stdout);
scanf("%lld", &end);
if (start == end) {
printf("! %lld\n", 1ll);
return;
}
long long dir1 = start - end;
start = 2, end = n / 2;
long long dir11, dir21;
while (start <= end) {
long long mid = (start + end) >> 1ll;
printf("? %lld\n", mid);
fflush(stdout);
scanf("%lld", &dir11);
printf("? %lld\n", mid + n / 2);
fflush(stdout);
scanf("%lld", &dir21);
dir11 = dir11 - dir21;
if (!dir11) {
printf("! %lld\n", mid);
return;
}
if ((dir1 * dir11) > 0) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
int32_t main() {
long long t = 1;
while (t--) {
solve();
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double a, b, c, d;
int main(void) {
std::ios_base::sync_with_stdio(false);
cin >> a >> b >> c >> d;
double p = 0, p2 = 0;
for (int i = 0; i < 1000000; i++) {
p = p + (1 - p2 - p) * a / b;
p2 = p2 + (1 - p2 - p) * c / d;
}
printf("%.9f", p);
}
| 2 |
#include <bits/stdc++.h>
///#pragma GCC target ("sse4.2")
///#pragma GCC opimize("O1")
///#pragma GCC opimize("O2")
///#pragma GCC opimize("Os")
///#pragma GCC opimize("Ofast")
///#pragma GCC target("avx,avx2,fma")
///#pragma GCC target("avx2")
///#pragma GCC opimize("O3")
///#pragma GCC opimization ("unroll-loops")
using namespace std;
bool home = 1;
typedef long long ll;
typedef long double ld;
///#define int ll
signed realMain();
signed main() {
#ifdef ONLINE_JUDGE
home = 0;
#endif
if (home) {
freopen ("tony_stark", "r", stdin);
} else {
ios::sync_with_stdio(0); cin.tie(0);
}
realMain();
}
const int N = (int) 2e5 + 7;
int n, m, total, t[N];
set<int> g[N];
struct edge {
int a, b, c;
} e[N];
bool operator < (edge a, edge b) {
return a.c < b.c;
}
int root(int a) {
if (t[a] == a) return a;
return t[a] = root(t[a]);
}
void unite(int a, int b) {
t[root(a)] = root(b);
}
mt19937 rng((long long) (new char));
vector<int> ord;
set<int> s;
signed realMain() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
t[i] = i;
g[i] = {i};
}
vector<edge> edges(m);
for (auto &it : edges) {
cin >> it.a >> it.b >> it.c;
g[it.a].insert(it.b);
g[it.b].insert(it.a);
if (it.a > it.b) {
swap(it.a, it.b);
}
total ^= it.c;
}
if (!home && n <= 1000) {
/// fie folosesc doar 0-uri, fie intru in cazul specific
vector<vector<int>> vals(n + 1, vector<int> (n + 1, 0));
for (auto &it : edges) {
vals[it.a][it.b] = it.c;
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (!vals[i][j]) {
edges.push_back({i, j, 0});
}
}
}
sort(edges.begin(), edges.end());
vector<edge> luate;
int zero = 0;
ll ret = 0;
for (auto &it : edges) {
if (root(it.a) != root(it.b)) {
unite(it.a, it.b);
ret += it.c;
luate.push_back(it);
zero += (it.c == 0);
}
}
if (m + zero == n * (n - 1) / 2) {
ll bef = ret;
ret += total;
for (int i = 1; i <= n; i++) {
t[i] = i;
}
for (auto &it : luate) {
if (it.c > 0) {
unite(it.a, it.b);
}
}
/// acum efectiv simulez ce alta muchie ar fi putut sa fie aleasa
for (auto &it : edges) {
if (it.c > 0 && root(it.a) != root(it.b)) {
ret = min(ret, bef + it.c);
}
}
}
cout << ret << "\n";
return 0;
}
ll ret = 0;
/// in cazul n e suficient de mare sa folosesc doar 0-uri
ord.resize(n);
iota(ord.begin(), ord.end(), 1);
shuffle(ord.begin(), ord.end(), rng);
for (int i = 0; i < n; i++) {
s.insert(i);
}
while (!s.empty()) {
int i = ord[*s.begin()];
s.erase(s.begin());
///dfs(i);
queue<int> q;
q.push(i);
while (!q.empty()) {
int a = q.front();
q.pop();
auto it = s.begin();
while (it != s.end()) {
int b = ord[*it];
if (g[a].count(b)) {
it++;
} else {
unite(a, b);
q.push(b);
it = s.erase(it);
}
}
}
}
sort(edges.begin(), edges.end());
for (auto &it : edges) {
if (root(it.a) != root(it.b)) {
unite(it.a, it.b);
ret += it.c;
}
}
cout << ret << "\n";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long N = 10e4;
const long long mn = 0;
const long long mx = 10e17;
map<string, long long> mp;
long long ans, n, k, m, cnt;
vector<long long> dp, v, v1;
deque<long long> dq;
queue<long long> qe;
string s, a, d;
set<string> st;
int main() {
ios_base::sync_with_stdio(0);
;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> m;
ans += (i * m);
}
cout << ans;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
const long long inf = 1e15;
int n, k;
int i, j;
int tot, rt;
int a[maxn];
int pre[2 * maxn], other[2 * maxn], last[2 * maxn];
int jump[maxn][23];
long long ans;
void add(int x, int y) {
tot++;
pre[tot] = last[x];
last[x] = tot;
other[tot] = y;
}
void dfs(int x, int fa) {
int p, q;
for (p = last[x]; p; p = pre[p]) {
q = other[p];
if (q != fa) {
jump[q][0] = x;
dfs(q, x);
}
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= n - 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
int mx = 1000000000;
for (i = 1; i <= n; i++) {
if (a[i] <= mx) {
rt = i;
mx = a[i];
}
}
dfs(rt, 0);
for (j = 1; j <= 20; j++) {
for (i = 1; i <= n; i++) {
jump[i][j] = jump[jump[i][j - 1]][j - 1];
}
}
for (i = 1; i <= n; i++) {
if (i == rt) continue;
long long cur = inf;
int p;
for (j = 0; j <= 20; j++) {
if (jump[i][j] == 0) {
p = j - 1;
break;
}
cur = min((long long)(j + 1) * (long long)a[jump[i][j]] + (long long)a[i],
cur);
}
if (jump[i][p] != rt) {
cur = min((long long)(p + 2) * (long long)a[rt] + (long long)a[i], cur);
}
ans += cur;
}
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long dp[2220][2220];
long long mo = 1e9 + 7;
int main() {
long long n, m;
dp[1][1] = 1;
dp[0][0] = 1;
for (long long a = 2; a <= 2000; a++) {
for (long long b = 0; b <= a; b++) {
if (b == 0) {
dp[a][b] = dp[a - 1][b + 1] % mo;
continue;
}
dp[a][b] = (dp[a - 1][b - 1] + dp[a - 1][b + 1]) % mo;
}
}
cin >> n >> m;
string q;
cin >> q;
long long jsd = 0;
long long xx = 0;
for (long long a = 0; a < q.size(); a++) {
if (q[a] == '(')
jsd++;
else
jsd--;
xx = min(jsd, xx);
}
if (jsd == q.size() && jsd >= n / 2) {
cout << 0 << endl;
return 0;
}
long long js = 0;
for (long long a = 0; a <= n - m; a++) {
for (long long b = 0; b <= a; b++) {
if (xx + b >= 0) {
js = (js + dp[a][b] * dp[n - m - a][jsd + b]) % mo;
}
}
}
cout << js;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string str[4];
string s[10][1000];
int cnt;
int main(int argc, const char* argv[]) {
std::ios_base::sync_with_stdio(false);
int n, k, i, q, j, x;
string ts;
char chr;
cin >> x;
str[0] = "++**";
str[1] = "+*+*";
str[2] = "++++";
str[3] = "+**+";
if (x >= 2) {
s[2][0] = str[0];
s[2][1] = str[1];
s[2][2] = str[2];
s[2][3] = str[3];
for (i = 3; i <= x; i++) {
j = 1;
for (k = 0; k < i; k++) j = j * 2;
for (k = 0; k < j / 2; k++) {
s[i][k] = s[i - 1][k] + s[i - 1][k];
}
n = 0;
for (k = j / 2; k < j; k++) {
ts.clear();
for (q = 0; q < j / 2; q++) {
if (s[i - 1][n][q] == '*')
ts += '+';
else
ts += '*';
}
s[i][k] = s[i - 1][n] + ts;
n++;
}
}
j = 1;
for (k = 0; k < x; k++) j = j * 2;
for (i = 0; i < j; i++) cout << s[x][i] << "\n";
} else {
if (x == 0)
cout << "*";
else if (x == 1) {
cout << "++\n";
cout << "+*\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, f, k, full;
cin >> a >> b >> f >> k;
full = b;
int ans = 0;
bool flag = 0;
if (a - f > full) {
flag = 1;
}
for (int i = 0; i < k; i++) {
int rem2, rem1;
if (i % 2 == 0) {
rem2 = a + a - f;
rem1 = f;
if (b < rem1) {
flag = 1;
break;
}
if (i == k - 1) {
if (b < a) ans++;
break;
}
if (b < rem2) {
b = full - (a - f);
ans++;
} else {
b = b - a;
}
} else {
rem1 = a - f;
rem2 = a + f;
if (b < rem1) {
flag = 1;
break;
}
if (i == k - 1) {
if (b < a) ans++;
break;
}
if (b < rem2) {
b = full - f;
ans++;
} else {
b = b - a;
}
}
}
if (flag) {
cout << -1 << endl;
} else
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
namespace Tzh {
const int maxn = 1010;
long long n, k, g[maxn];
void work() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> g[i];
if (k >= n) {
cout << *max_element(g + 1, g + n + 1);
return;
}
long long winner = 1, num = 0;
for (int i = 2; i <= n; i++) {
if (g[winner] > g[i])
num++;
else
winner = i, num = 1;
if (num == k) {
cout << g[winner];
return;
}
}
cout << g[winner];
return;
return;
}
} // namespace Tzh
int main() {
ios::sync_with_stdio(false);
Tzh::work();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int v;
cin >> v;
if (v == 2) {
cout << 2 << endl;
return 0;
}
cout << 1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int ar[5], t, i, s, count = 0;
for (i = 0; i < 4; i++) {
cin >> ar[i];
}
for (i = 0; i < 3; i++) {
for (int j = i + 1; j < 4; j++) {
if (ar[i] > ar[j]) {
t = ar[i];
ar[i] = ar[j];
ar[j] = t;
}
}
}
for (i = 0; i < 4; i++) {
if (ar[i - 1] == ar[i]) count++;
}
cout << count;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << ": " << a << " ";
err(++it, args...);
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<pii>;
using vs = vector<string>;
const int DR[] = {-1, 0, 1, 0};
const int DC[] = {0, -1, 0, 1};
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = 1073741823;
const ll INFLL = 4e18;
const int MAX = 5e5;
int N, R;
ll K;
ll arr[MAX + 5], tmp[MAX + 5];
ll BIT[MAX + 5];
void update(int x, ll val) {
x++;
for (; x <= N; x += x & -x) BIT[x] += val;
}
ll query(int x) {
x++;
ll ret = 0;
for (; x >= 1; x -= x & -x) ret += BIT[x];
return ret;
}
void read() {
cin >> N >> R >> K;
R = min(R, N - 1);
for (int i = 0; i < (int)N; i++) {
int x;
cin >> x;
update(i, x);
}
for (int i = 0; i < (int)N; i++) {
int r = min(N - 1, i + R);
arr[i] += query(r);
if (i - R - 1 >= 0) arr[i] -= query(i - R - 1);
}
}
bool check(ll val) {
memset(tmp, 0, sizeof tmp);
ll avail = K;
for (int i = 0; i < (int)N; i++) {
if (i > 0) tmp[i] += tmp[i - 1];
ll need = val - (arr[i] + tmp[i]);
if (need > 0) {
if (need > avail) return false;
avail -= need;
tmp[i] += need;
tmp[min(N, i + 2 * R + 1)] -= need;
}
}
return true;
}
void solve() {
ll lo = 0, hi = 2e18;
for (int loop = 0; loop < (int)65; loop++) {
ll mid = (lo + hi) / 2;
if (check(mid))
lo = mid;
else
hi = mid;
}
cout << lo << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int TC = 1;
while (TC--) {
read();
solve();
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
long long dp[200010], v[200010], sum[200010];
int head[200010], to[200010 * 2], pre[200010 * 2];
int n, e;
long long ans;
void addedge(int x, int y) {
to[e] = y;
pre[e] = head[x];
head[x] = e++;
}
void dfs(int x, int pa) {
sum[x] = v[x];
for (int i = head[x]; i != -1; i = pre[i]) {
if (to[i] == pa) continue;
dfs(to[i], x);
if (dp[x] != -inf) ans = max(ans, dp[x] + dp[to[i]]);
sum[x] += sum[to[i]];
dp[x] = max(dp[x], dp[to[i]]);
}
dp[x] = max(dp[x], sum[x]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%I64d", &v[i]);
memset(head, -1, sizeof(head));
e = 0;
int x, y;
for (int i = 1; i < n; ++i) {
scanf("%d%d", &x, &y);
addedge(x, y);
addedge(y, x);
}
ans = -inf;
for (int i = 1; i <= n; ++i) dp[i] = -inf;
memset(sum, 0, sizeof(sum));
dfs(1, 0);
if (ans == -inf)
printf("Impossible");
else
printf("%I64d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long powm(long long a, long long n, long long x = 1000000007) {
long long r = 1;
if (n == 0) return 1;
while (n > 1) {
if (n % 2) r = (r * a) % x;
a = (a * a) % x;
n /= 2;
}
return (a * r) % x;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long _ = 1;
while (_--) {
string a, b;
cin >> a >> b;
long long j = 0;
for (long long i = 0; i < ((long long)b.size()); i++) {
if (b[i] == a[j]) j++;
}
cout << j + 1;
cout << '\n';
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int maxn = 10000 + 10;
int n, m;
vector<vector<int> > num;
void solve() {
long long sol = INF;
for (int i = 1; i <= n; ++i) {
long long ans = 0;
int ct = num[1].size();
vector<int> indx(m + 1, 0);
for (int j = 2; j <= m; j++) {
while ((int)num[j].size() - indx[j] >= i) {
ans += num[j][indx[j]];
indx[j]++;
ct++;
}
}
if (ct >= i)
sol = min(sol, ans);
else {
vector<int> rest;
for (int j = 2; j <= m; j++) {
for (int k = indx[j]; k < (int)num[j].size(); k++)
rest.push_back(num[j][k]);
}
sort(rest.begin(), rest.end());
int k = 0;
while (ct < i) {
ans += rest[k++];
ct++;
}
sol = min(sol, ans);
}
}
printf("%I64d\n", sol);
}
int main() {
scanf("%d%d", &n, &m);
num.resize(m + 1);
for (int i = 0; i < n; ++i) {
int id, cst;
scanf("%d%d", &id, &cst);
num[id].push_back(cst);
}
for (int i = 2; i <= m; ++i) sort(num[i].begin(), num[i].end());
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[200005], adj_rev[200005];
vector<bool> vis(200005, false);
vector<int> finished;
int sum[2], cnt;
void dfs(int u) {
vis[u] = 1;
for (int v : adj[u]) {
if (!vis[v]) dfs(v);
}
finished.push_back(u);
}
int sccs = 0;
int scc[200005];
void dfs2(int u) {
vis[u] = 1;
scc[u] = sccs;
for (int v : adj_rev[u]) {
if (!vis[v]) dfs2(v);
}
cnt++;
}
int main() {
int n, m, u, v;
cin >> n;
vector<pair<int, int> > edges;
for (int i = 1; i <= n; i++) {
cin >> u;
adj[i].push_back(u);
adj_rev[u].push_back(i);
edges.push_back({i, u});
}
int summ = 0;
for (int i = 0; i < n; i++) {
cin >> u;
summ += u;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i);
reverse(finished.begin(), finished.end());
for (int i = 1; i <= n; i++) vis[i] = false;
for (int i = 0; i < n; i++) {
int last = finished[i];
cnt = 0;
if (!vis[last]) dfs2(last), sccs++;
sum[cnt % 2] += cnt;
}
if (sccs == 1) {
if (summ % 2 == 0)
cout << 1;
else
cout << 0;
return 0;
}
if (summ % 2 == 0)
cout << sccs + 1;
else
cout << sccs;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long int maxn = 3e6 + 1;
double pie = 3.1415926535;
long long int cnt[maxn];
vector<pair<long long int, long long int> > v, a;
long long int modularExponentiation(long long int x, long long int n,
long long int M) {
long long int result = 1;
while (n > 0) {
if (n % 2 == 1) result = (result * x) % M;
x = (x * x) % M;
n = n / 2;
}
return result;
}
int main() {
long long int i, n, k, left, right, bal = 0, ans, l, r;
scanf("%lld", &n), scanf("%lld", &k);
bool flag = 1;
for (i = 1; i <= n; i++) {
scanf("%lld", &l), scanf("%lld", &r);
a.push_back(make_pair(l, -1));
a.push_back(make_pair(r, 1));
}
sort(a.begin(), a.end());
for (i = 0, ans = 0; i < int(a.size()); i++) {
if (a[i].second == -1)
bal++;
else
bal--;
if (bal == k) {
if (ans == k - 1) {
left = a[i].first;
flag = 0;
}
} else if (bal == k - 1) {
if (ans == k) {
right = a[i].first;
v.push_back(make_pair(left, right));
flag = 1;
}
}
ans = bal;
}
if (flag == 0) v.push_back(make_pair(left, a[int(a.size()) - 1].first));
ans = int(v.size());
printf("%lld", ans);
printf("\n");
;
for (i = 0; i < int(v.size()); i++)
printf("%lld %lld\n", v[i].first, v[i].second);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long A[16777216];
long long hashin(char *str) {
long long ans = 0;
for (long long i = 0; i < 3; i++) {
ans |= (1 << (str[i] - 'a'));
}
return ans;
}
void summation(long long a, long long b) {
if (b < 0) return;
summation(a, b - 1);
summation(a + (1 << b), b - 1);
for (long long i = 0; i < (1 << b); i++) A[i + (1 << b) + a] += A[a + i];
return;
}
signed main() {
long long n;
char x[5];
long long ans = 0;
scanf("%lld", &n);
for (long long i = 0; i < n; i++) {
scanf("%s", x);
A[hashin(x)] += 1;
}
summation(0, 23);
for (long long i = 0; i < 16777216; i++)
ans = ans ^ ((n - A[i]) * (n - A[i]));
printf("%lld\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
namespace std {
template <typename...>
using void_t = void;
}
template <typename T, typename _ = void>
struct is_container : std::false_type {};
template <typename... Ts>
struct is_container_helper {};
template <typename T>
struct is_container<
T, typename std::conditional<
false,
is_container_helper<decltype(std::declval<T>().size()),
decltype(std::declval<T>().begin()),
decltype(std::declval<T>().end()),
decltype(std::declval<T>().cbegin()),
decltype(std::declval<T>().cend())>,
void>::type> : public std::true_type {};
class IO {
static const int bufSize = 1 << 18;
char inBuf[bufSize], outBuf[bufSize];
char *ip1 = inBuf, *ip2 = inBuf;
int goodReadBit = 1, op1 = -1, op2 = bufSize - 1;
__inline__ __attribute__((always_inline)) int gc() {
return ip1 == ip2 && (ip2 = (ip1 = inBuf) + fread(inBuf, 1, bufSize, stdin),
ip1 == ip2)
? EOF
: *ip1++;
}
template <typename T>
__inline__ __attribute__((always_inline)) void __RI(T &x) {
int ch = gc(), neg = 1;
x = 0;
for (; !(isdigit(ch) || ch == '-'); ch = gc()) {
}
if (ch == EOF) {
goodReadBit = 0;
return;
}
if (ch == '-') neg = -1, ch = gc();
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - 48) * neg;
}
template <typename T>
__inline__ __attribute__((always_inline)) void __RC(T &x) {
int ch;
while (isspace(ch = gc())) {
}
if (ch == EOF) {
goodReadBit = 0;
return;
}
x = ch;
}
__inline__ __attribute__((always_inline)) void __RS(std::string &x) {
char ch;
x.clear();
for (ch = gc(); isspace(ch); ch = gc()) {
}
if (ch == EOF) {
goodReadBit = 0;
return;
}
for (; !isspace(ch) && ch != EOF; ch = gc()) x.push_back(ch);
}
public:
__inline__ __attribute__((always_inline)) IO &R(char &x) {
return __RC(x), (*this);
}
__inline__ __attribute__((always_inline)) IO &R(unsigned char &x) {
return __RC(x), (*this);
}
__inline__ __attribute__((always_inline)) IO &R(std::string &x) {
return __RS(x), (*this);
}
template <typename T1, typename T2>
__inline__ __attribute__((always_inline)) IO &R(std::pair<T1, T2> &x) {
return R(x.first), R(x.second), (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &RA(T *a, int n) {
for (int i = 0; i < n; ++i) R(a[i]);
return (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &R(T &x, Args &...args) {
return R(x), R(args...), (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &RA(T *a, int n, Args... args) {
for (int i = 0; i < n; ++i) RA(a[i], args...);
return (*this);
}
template <typename T>
__inline__ __attribute__((always_inline))
typename std::enable_if<std::is_integral<T>::value, IO>::type &
R(T &x) {
return __RI(x), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline))
typename std::enable_if<is_container<T>::value, IO>::type &
R(T &x) {
for (auto &i : x) R(i);
return (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) typename std::enable_if<
std::is_void<std::void_t<decltype(std::declval<T>().read())>>::value,
IO>::type &
R(T &x) {
return x.read(), (*this);
}
private:
char space;
__inline__ __attribute__((always_inline)) void pc(const char &x) {
if (op1 == op2) flush();
outBuf[++op1] = x;
}
template <typename T>
__inline__ __attribute__((always_inline)) void __WI(T x) {
static char buf[64];
static int len = -1;
if (x >= 0) {
do {
buf[++len] = x % 10 + 48, x /= 10;
} while (x);
} else {
pc('-');
do {
buf[++len] = -(x % 10) + 48, x /= 10;
} while (x);
}
while (len >= 0) {
pc(buf[len]), --len;
}
}
public:
__inline__ __attribute__((always_inline)) void flush() {
fwrite(outBuf, 1, op1 + 1, stdout), op1 = -1;
}
__inline__ __attribute__((always_inline)) IO &W(const char &x) {
return pc(x), (*this);
}
__inline__ __attribute__((always_inline)) IO &W(const char *x) {
while (*x != '\0') pc(*(x++));
return (*this);
}
__inline__ __attribute__((always_inline)) IO &W(const std::string &x) {
return W(x.c_str()), (*this);
}
template <typename T1, typename T2>
__inline__ __attribute__((always_inline)) IO &W(const std::pair<T1, T2> &x) {
WS(x.first);
W(x.second);
return (*this);
}
__inline__ __attribute__((always_inline)) IO &WL() {
return W('\n'), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) IO &WL(const T &x) {
return W(x), W('\n'), (*this);
}
__inline__ __attribute__((always_inline)) IO &WS() {
return W(space), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) IO &WS(const T &x) {
return W(x), W(space), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) IO &WA(T *a, int n) {
for (int i = 0; i < n; i++) WS(a[i]);
WL();
return (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &W(const T &x,
const Args &...args) {
W(x), W(space), W(args...);
return (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &WS(const T &x,
const Args &...args) {
return W(x), W(space), W(args...), W(space), (*this);
}
template <typename... Args>
__inline__ __attribute__((always_inline)) IO &WL(const Args &...args) {
return W(args...), W('\n'), (*this);
}
template <typename T, typename... Args>
__inline__ __attribute__((always_inline)) IO &WA(T *a, int n, Args... args) {
for (int i = 0; i < n; i++) WA(a[i], args...);
return (*this);
}
template <typename T>
__inline__ __attribute__((always_inline))
typename std::enable_if<std::is_integral<T>::value, IO>::type &
W(const T &x) {
return __WI(x), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline))
typename std::enable_if<is_container<T>::value, IO>::type &
W(const T &x) {
for (auto &i : x) WS(i);
WL();
return (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) typename std::enable_if<
std::is_void<std::void_t<decltype(std::declval<T>().write())>>::value,
IO>::type &
W(const T &x) {
return x.write(), (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) IO &operator>>(T &x) {
R(x);
return (*this);
}
template <typename T>
__inline__ __attribute__((always_inline)) IO &operator<<(const T &x) {
W(x);
return (*this);
}
IO() { space = ' '; }
~IO() { flush(); }
} io;
const std::int32_t INF = 0x3f3f3f3f;
const std::int64_t INFL = 0x3f3f3f3f3f3f3f3f;
using namespace std;
map<char, int> safetyTable = {{'+', 1}, {'-', 2}, {'*', 3}, {'/', 4}};
map<string, int> safetyVal;
struct Expression {
string value;
enum Type { EXPR = 1, VARI = 2 } type;
Expression *lexpr, *rexpr;
int safety;
Expression(string value = "", Type type = EXPR, Expression *lexpr = nullptr,
Expression *rexpr = nullptr, int safety = -1)
: value(value), type(type), lexpr(lexpr), rexpr(rexpr), safety(safety) {}
~Expression() {
delete lexpr;
delete rexpr;
}
};
string str;
string getToken(int pos) {
if (isalnum(str[pos])) {
string res;
for (; pos < str.size() && isalnum(str[pos]); pos++)
res.push_back(str[pos]);
return res;
} else
return string(1, str[pos]);
}
Expression *ParseExpr(int &);
Expression *ParseTerm(int &);
Expression *ParseItem(int &);
Expression *ParseExpr(int &pos) {
if (pos == str.size()) return nullptr;
Expression *expr = ParseTerm(pos);
while (str[pos] == '+' || str[pos] == '-') {
int tPos = pos;
++pos;
expr = new Expression(getToken(tPos), Expression::EXPR, expr,
ParseTerm(pos), safetyTable[str[tPos]]);
if (pos == str.size()) break;
}
return expr;
}
Expression *ParseTerm(int &pos) {
if (pos == str.size()) return nullptr;
Expression *expr = ParseItem(pos);
while (str[pos] == '*' || str[pos] == '/') {
int tPos = pos;
++pos;
expr = new Expression(getToken(tPos), Expression::EXPR, expr,
ParseItem(pos), safetyTable[str[tPos]]);
if (pos == str.size()) break;
}
return expr;
}
Expression *ParseItem(int &pos) {
if (pos == str.size()) return nullptr;
Expression *expr = nullptr;
if (isalnum(str[pos])) {
string tok = getToken(pos);
expr = new Expression(tok, Expression::VARI);
expr->safety = safetyVal.count(tok) ? safetyVal[tok] : 5;
pos += tok.size();
return expr;
} else {
pos++;
expr = ParseExpr(pos);
expr->safety = expr->safety ? 5 : 0;
pos++;
}
return expr;
}
Expression *ParseExpr() {
int pos = 0;
return ParseExpr(pos);
}
int getSafety(Expression *expr) {
if (expr == nullptr) return 5;
if (expr->safety == 0) return 0;
if (expr->type == Expression::VARI) return expr->safety;
auto lSafety = getSafety(expr->lexpr);
auto rSafety = getSafety(expr->rexpr);
if (lSafety == 0 || rSafety == 0) return expr->safety = 0;
if (expr->value[0] == '+') return expr->safety;
if (expr->value[0] == '-') {
if (rSafety <= 2) expr->safety = 0;
return expr->safety;
}
if (expr->value[0] == '*') {
if (rSafety <= 2 || lSafety <= 2) expr->safety = 0;
return expr->safety;
}
if (expr->value[0] == '/') {
if (rSafety <= 4 || lSafety <= 2) expr->safety = 0;
return expr->safety;
}
return 0;
}
int main() {
int n;
string s;
cin >> n;
getline(cin, s);
for (int i = 0; i < n; i++) {
getline(cin, s);
stringstream ss(s);
char c;
while (ss >> c)
if (c == '#') break;
string name;
ss >> name;
ss >> name;
string sExpr, t;
while (ss >> t) sExpr += t;
::str = sExpr;
Expression *expr = ParseExpr();
safetyVal[name] = getSafety(expr);
delete expr;
}
getline(cin, s);
string sExpr;
for (auto i : s) {
if (i != ' ') sExpr.push_back(i);
}
::str = sExpr;
Expression *expr = ParseExpr();
io.WL(getSafety(expr) ? "OK" : "Suspicious");
delete expr;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int n, m, p;
int a[310][310];
vector<pair<int, int> > v[90900];
int d[310][310];
int td[310][310];
queue<pair<int, int> > q;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void bfs(int c) {
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++) {
td[i][j] = 999999999;
if (a[i][j] == c) {
q.push(make_pair(i, j));
td[i][j] = d[i][j];
}
}
while (!q.empty()) {
pair<int, int> ver = q.front();
q.pop();
for (int i = 0; i < (4); i++) {
pair<int, int> v = make_pair(ver.first + dx[i], ver.second + dy[i]);
if (v.first >= 0 && v.first < n && v.second >= 0 && v.second < m &&
td[v.first][v.second] > td[ver.first][ver.second] + 1) {
td[v.first][v.second] = td[ver.first][ver.second] + 1;
if (a[v.first][v.second] == c + 1)
d[v.first][v.second] = td[v.first][v.second];
q.push(make_pair(v.first, v.second));
}
}
}
}
int main() {
cin >> n >> m >> p;
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++) {
cin >> a[i][j];
v[a[i][j]].push_back(make_pair(i, j));
d[i][j] = 999999999;
if (a[i][j] == 1) d[i][j] = i + j;
}
for (int i = 1; i < p; i++) {
if (v[i].size() * v[i + 1].size() > n * m)
bfs(i);
else {
for (int j = 0; j < (v[i].size()); j++)
for (int k = 0; k < (v[i + 1].size()); k++)
d[v[i + 1][k].first][v[i + 1][k].second] =
min(d[v[i + 1][k].first][v[i + 1][k].second],
d[v[i][j].first][v[i][j].second] +
abs(v[i][j].first - v[i + 1][k].first) +
abs(v[i][j].second - v[i + 1][k].second));
}
}
int ans = 0;
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++)
if (a[i][j] == p) cout << d[i][j];
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
string a;
vector<string> v, b;
bool u = false, l = false;
for (int i = 0; i < 4 * n; i++) {
cin >> a;
int k = 0;
u = false;
for (int j = (int)a.length() - 1; j >= 0; j--) {
if (a[j] == 'a' || a[j] == 'e' || a[j] == 'i' || a[j] == 'o' ||
a[j] == 'u')
k++;
if (k >= m) {
v.push_back(a.substr(j));
u = true;
break;
}
}
if (!u) {
l = true;
}
}
if (l) {
cout << "NO";
return 0;
}
for (int i = 0; i < n; i++) {
if (v[i * 4] == v[i * 4 + 1] && v[i * 4 + 1] == v[i * 4 + 2] &&
v[i * 4 + 2] == v[i * 4 + 3]) {
continue;
}
if (v[i * 4] == v[i * 4 + 1] && v[i * 4 + 2] == v[i * 4 + 3]) {
b.push_back("aabb");
continue;
}
if (v[i * 4 + 0] == v[i * 4 + 3] && v[i * 4 + 1] == v[i * 4 + 2]) {
b.push_back("abba");
continue;
}
if (v[i * 4 + 0] == v[i * 4 + 2] && v[i * 4 + 1] == v[i * 4 + 3]) {
b.push_back("abab");
continue;
}
cout << "NO";
return 0;
}
if ((int)b.size() == 0) {
cout << "aaaa";
return 0;
}
bool h = true;
for (int i = 1; i < (int)b.size(); i++) {
if (b[i] != b[i - 1]) {
h = false;
break;
}
}
if (h)
cout << b[0];
else
cout << "NO";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long fact(long long n) {
long long res = 1;
for (long long i = 2; i <= n; i++) res = res * i;
return res;
}
long long nCr(long long n, long long r) {
return fact(n) / (fact(r) * fact(n - r));
}
bool isPrime(long long n) {
if (n == 1) {
return false;
}
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
vector<long long> primeFactors(long long n) {
vector<long long> a;
while (n % 2 == 0) {
a.push_back(2);
n = n / 2;
}
for (long long i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
a.push_back(i);
n = n / i;
}
}
if (n > 2) a.push_back(n);
return a;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n, m;
cin >> n >> m;
vector<pair<long long, long long> > a[n - 1 + m - 1 + 1];
long long b[n][m];
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cin >> b[i][j];
long long sum = i + j;
a[sum].push_back({i, j});
}
}
long long i = 0;
long long j = n + m - 2;
long long count = 0;
while (i < j) {
long long count0 = 0;
long long count1 = 0;
for (auto x : a[i]) {
if (b[x.first][x.second] == 1) {
count1++;
} else {
count0++;
}
}
for (auto x : a[j]) {
if (b[x.first][x.second] == 1) {
count1++;
} else {
count0++;
}
}
long long req;
if (count1 > count0)
req = 1;
else
req = 0;
for (auto x : a[i]) {
if (b[x.first][x.second] != req) {
count++;
}
}
for (auto x : a[j]) {
if (b[x.first][x.second] != req) {
count++;
}
}
i++;
j--;
}
cout << count << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int an[110][15][15];
int a, b, c, d, n, m, t, j;
string s;
int main() {
cin >> t;
for (int i = 0; i < t; i++) {
cin >> s;
c = 0;
for (int u = 0; u < s.size(); u++) {
if (s[u] == 'X') c++;
}
if (c > 0) {
j = 6;
an[i][0][13] = 1;
while (j > 1) {
for (int u = 0; u < s.size(); u++) {
if (s[u] == 'X') {
an[i][j][u % j]++;
}
}
if (j == 2) {
for (int u = 0; u < j; u++) {
if (an[i][j][u] > 5) an[i][j][13] = 1;
}
if (an[i][j][13] == 1) an[i][0][13]++;
j = 1;
} else if (j == 3) {
for (int u = 0; u < j; u++) {
if (an[i][j][u] > 3) an[i][j][13] = 1;
}
if (an[i][j][13] == 1) an[i][0][13]++;
j = 2;
} else if (j == 4) {
for (int u = 0; u < j; u++) {
if (an[i][j][u] > 2) an[i][j][13] = 1;
}
if (an[i][j][13] == 1) an[i][0][13]++;
j = 3;
} else if (j == 6) {
for (int u = 0; u < j; u++) {
if (an[i][j][u] > 1) an[i][j][13] = 1;
}
if (an[i][j][13] == 1) an[i][0][13]++;
j = 4;
}
}
if (c > 11) {
an[i][0][13]++;
an[i][12][13] = 1;
}
cout << an[i][0][13] << " ";
cout << "1x12 ";
if (an[i][6][13] == 1) {
cout << "2x6 ";
}
if (an[i][4][13] == 1) {
cout << "3x4 ";
}
if (an[i][3][13] == 1) {
cout << "4x3 ";
}
if (an[i][2][13] == 1) {
cout << "6x2 ";
}
if (an[i][12][13] == 1) {
cout << "12x1 ";
}
} else
cout << 0;
cout << endl;
}
return 0;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.