solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
vector<pair<int, int>> all;
set<int> ugly;
int getidx(int tp, int d) {
int l = -1, r = all.size();
while (r - l > 1) {
int c = (l + r) / 2;
if (all[c].first != d) {
if (all[c].first < d)
l = c;
else
r = c;
} else {
if (all[c].second < tp)
l = c;
else
r = c;
}
}
return r;
}
int F[MAXN][2];
long long G[MAXN][2];
void update(int pos, int delta) {
int tp = all[pos].second;
int inf = all[pos].first * delta;
for (; pos < all.size(); pos = (pos | (pos + 1))) {
F[pos][tp] += delta;
G[pos][tp] += inf;
}
}
pair<int, int> get(int pos) {
pair<int, int> res(0, 0);
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
res.first += F[pos][0];
res.second += F[pos][1];
}
return res;
}
pair<long long, long long> getsum(int pos) {
pair<long long, long long> res(0, 0);
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
res.first += G[pos][0];
res.second += G[pos][1];
}
return res;
}
void solve() {
int n;
scanf("%d", &n);
vector<pair<int, int>> q;
for (int i = 0; i < n; ++i) {
int tp, d;
scanf("%d%d", &tp, &d);
q.push_back(pair<int, int>(tp, d));
all.push_back(pair<int, int>(abs(d), tp));
}
sort(all.begin(), all.end());
{
vector<pair<int, int>>::iterator z = unique(all.begin(), all.end());
all.resize(z - all.begin());
}
for (int i = 0; i < n; ++i) {
int tp = q[i].first;
int d = q[i].second;
int idx = getidx(tp, abs(d));
if (all[idx].first != abs(d) || all[idx].second != tp) {
printf("wut\n");
fflush(stdout);
throw "qwe";
}
if (d > 0)
update(idx, 1);
else
update(idx, -1);
if (tp == 1) {
if (d > 0)
ugly.insert(idx);
else
ugly.erase(idx);
}
pair<long long, long long> sum = getsum(all.size() - 1);
pair<int, int> total_pair = get(all.size() - 1);
if (total_pair.second == 0) {
printf("%I64d\n", sum.first);
} else if (total_pair.first == 0) {
printf("%I64d\n", sum.second * 2 - all[*ugly.begin()].first);
} else {
int u = *ugly.begin();
update(u, -1);
int total = total_pair.first + total_pair.second - 1;
int want = total - total_pair.second;
int l = -2, r = all.size();
while (r - l > 1) {
int c = (l + r) / 2;
pair<int, int> cnt = get(c);
if (cnt.first + cnt.second < want)
l = c;
else
r = c;
}
pair<int, int> cnt = get(r);
pair<long long, long long> sum1 = getsum(r);
printf("%I64d\n", (sum.first + sum.second) * 2 - sum1.first -
sum1.second - all[u].first);
update(u, 1);
}
}
}
int main() {
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
long long t[maxn];
long long n, m, a, d;
long long cnt, ed, nt;
int main() {
cin >> n >> m >> a >> d;
for (int i = 1; i <= m; ++i) scanf("%lld", &t[i]);
int idx = 1;
while (idx <= m) {
long long k = ed / a;
if (k >= n)
ed = t[idx] + d, cnt++;
else {
if (t[idx] > (k + 1) * a) {
long long tmp = min(t[idx] / a, n);
long long times =
(tmp - k) / (d / a + 1) + ((tmp - k) % (d / a + 1) > 0);
cnt += times;
ed = (k + (times - 1) * (d / a + 1) + 1) * a + d;
} else
ed = t[idx] + d, cnt++;
}
for (; t[idx] <= ed && idx <= m; idx++)
;
}
long long pre = ed / a;
if (pre < n)
cnt += (n - pre) / ((d / a) + 1) + ((n - pre) % ((d / a) + 1) > 0);
cout << cnt << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
unsigned long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
const int maxn = 1 << 20;
int n;
string s;
void chemthan() {
cin >> s;
n = int((s).size());
for (int i = (1); i < (n); ++i) {
string t = s.substr(i) + s.substr(0, i);
string t2 = t;
reverse((t2).begin(), (t2).end());
if (s != t && t == t2) {
cout << 1 << "\n";
return;
}
}
for (int i = 0; i + i < n - 1; i++) {
for (int j = 0; j <= i; j++) {
if (s[j] != s[i - j]) {
cout << 2 << "\n";
return;
}
}
}
cout << "Impossible\n";
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
int f, g, t, vf;
int main() {
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
cin >> n;
for (int d = 2; d * d <= n; d++)
if (n % d == 0) {
f = d;
g = n / d;
for (int h1 = 1; h1 < min(f, 5000); h1++) {
int h2 = n - 1 - h1 * g;
if (h2 % f) continue;
h2 /= f;
vf = h1 / f + h2 / g;
if (h1 * g + h2 * f == n - 1) {
cout << "YES" << '\n';
cout << 2 << '\n';
cout << h1 << ' ' << d << '\n';
cout << h2 << ' ' << n / d << '\n';
return 0;
}
}
for (int h1 = f - 1; h1 >= max(1, f - 5000); h1--) {
int h2 = n - 1 - h1 * g;
if (h2 % f) continue;
h2 /= f;
vf = h1 / f + h2 / g;
if (h1 * g + h2 * f == n - 1) {
cout << "YES" << '\n';
cout << 2 << '\n';
cout << h1 << ' ' << d << '\n';
cout << h2 << ' ' << n / d << '\n';
return 0;
}
}
}
cout << "NO";
}
| 6 |
#include <bits/stdc++.h>
int m[1000];
int main() {
int n, r;
int ptr1 = -1, ptr2;
int cnt = 0;
scanf("%d %d", &n, &r);
for (int i = 0; i < n; i++) scanf("%d", m + i);
ptr2 = r - 1;
while (ptr1 != ptr2) {
if (m[ptr2] == 1) {
cnt++;
ptr1 = ptr2;
if (ptr2 + r >= n) {
printf("%d\n", cnt);
return 0;
} else if (ptr2 + 2 * r >= n) {
ptr2 = n - 1;
} else {
ptr2 += 2 * r - 1;
}
} else {
ptr2--;
}
}
printf("-1\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n % 2 == 0) {
long long cnt = 1;
for (int i = 0; i < n / 2; i++) {
cnt *= 2;
}
cout << cnt;
} else {
cout << 0;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
long long n = s.length();
long long sum = 0;
for (long long i = 0; i < n; i++) {
long long a = s[i] - '0';
if (a % 4 == 0) sum++;
}
for (long long i = n - 1; i >= 1; i--) {
string g;
g.push_back(s[i - 1]);
g.push_back(s[i]);
long long a = stoi(g);
if (a % 4 == 0) sum += i;
}
cout << sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c, g, x, y;
inline long long fixMod(long long a, long long m) { return (a % m + m) % m; }
inline long long floor(long long a, long long b) {
return (a - fixMod(a, b)) / b;
}
void getNewTerm(long long& g0, long long& g1, long long q) {
long long t = g0 - q * g1;
g0 = g1;
g1 = t;
}
long long eGCD(long long a, long long b, long long& x, long long& y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
long long g = eGCD(b, a % b, y, x);
y -= (a / b) * x;
return g;
}
bool solveLDE(long long a, long long b, long long c, long long& x, long long& y,
long long& g) {
g = eGCD(a, b, x, y);
x *= c / g;
y *= c / g;
return !(c % g);
}
int main() {
cin >> a >> b >> c;
bool sol = solveLDE(a, b, -1 * c, x, y, g);
if (!sol) {
cout << "-1";
return 0;
} else {
cout << x << " " << y;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100010;
const int mod = 1000000007;
template <typename T>
using mat = vector<vector<T>>;
template <typename T>
mat<T> mult(mat<T>& a, mat<T>& b) {
assert(a[0].size() == b.size());
mat<T> c(a.size(), vector<T>(b[0].size(), 0));
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b[0].size(); j++) {
for (int k = 0; k < a[0].size(); k++) {
c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % mod;
}
}
}
return c;
}
template <typename T>
mat<T> pow(mat<T> base, ll exp) {
mat<T> ret(base.size(), vector<T>(base[0].size()));
for (int i = 0; i < base.size(); i++) {
ret[i][i] = 1;
}
while (exp) {
if (exp & 1) {
ret = mult(ret, base);
}
base = mult(base, base);
exp >>= 1;
}
return ret;
}
template <typename T>
void print(mat<T>& m) {
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m[i].size(); j++) {
cout << m[i][j] << " ";
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
int n, x;
cin >> n >> x;
mat<ll> A(101, vector<ll>(101, 0));
for (int i = 0; i < n; i++) {
int d;
cin >> d;
A[d - 1][0]++;
}
for (int i = 0; i < 99; i++) {
A[i][i + 1] = 1;
}
A[100][100] = 1;
A[100][0] = 1;
A = pow(A, x);
mat<ll> B(1, vector<ll>(101));
B[0][0] = B[0][100] = 1;
mat<ll> C = mult(B, A);
cout << C[0][0] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long pre[2000005], a[2000005], cnt[2000005], visit[2000005];
int32_t main() {
long long n, x, y;
scanf("%lld%lld%lld", &n, &x, &y);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
pre[a[i]] += a[i];
cnt[a[i]]++;
}
long long p = x / y, ans = min(n * y, n * x);
sort(a + 1, a + 1 + n);
if (a[n] == 1) {
for (long long i = 1; i <= n; i++) ans = min(ans, i * y + (n - i) * x);
printf("%lld", ans);
exit(0);
}
for (long long i = 1; i <= 2e6; i++) {
pre[i] += pre[i - 1];
cnt[i] += cnt[i - 1];
}
for (long long i = 2; i <= 2e6; i++) {
if (visit[i]) continue;
long long q = p, s = 0;
if (p > i - 1) q = i - 1;
for (long long j = i; j <= 2e6; j += i) {
visit[j] = 1;
s += ((cnt[j - 1] - cnt[j - q - 1]) * j - (pre[j - 1] - pre[j - q - 1])) *
y;
s += (cnt[j - q - 1] - cnt[j - i]) * x;
}
ans = min(ans, s);
}
printf("%lld", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 5;
int par[MAXN];
int n, m, q;
int sz[MAXN];
int a[301][301];
int dx[5] = {-1, 0, 1, 0};
int dy[5] = {0, 1, 0, -1};
vector<pair<int, int>> del[MAXN];
vector<pair<int, int>> add[MAXN];
int diff[MAXN];
bool check(int x, int y) { return (x >= 1 && x <= n && y <= m && y >= 1); }
int findpar(int a) {
if (par[a] == a) {
return a;
}
return par[a] = findpar(par[a]);
}
bool merge(int x, int y) {
x = findpar(x);
y = findpar(y);
if (x == y) {
return false;
}
if (sz[x] < sz[y]) {
swap(x, y);
}
par[y] = x;
sz[x] += sz[y];
return true;
}
void solve(vector<pair<int, int>> v1, int coeff) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = 0;
}
}
for (int i = 1; i <= (n * m) + m; i++) {
par[i] = i;
sz[i] = 1;
}
for (auto z : v1) {
int tmp = 1;
int curr = z.first;
int ind = z.second;
int x = (curr - 1) / (m);
int y = ((curr - 1) % (m)) + 1;
a[x][y] = 1;
for (int i = 0; i < 4; i++) {
int nextx = x + dx[i];
int nexty = y + dy[i];
if (check(nextx, nexty) && a[nextx][nexty] == 1) {
tmp -= merge(x * m + y, nextx * m + nexty);
}
}
diff[ind] += tmp * coeff;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> q;
int lst;
for (int i = 1; i <= q; i++) {
int x, y, c;
cin >> x >> y >> c;
if (a[x][y] == c) {
continue;
}
lst = c + 1;
add[c].push_back(make_pair(x * m + y, i));
del[a[x][y]].push_back(make_pair(x * m + y, i));
a[x][y] = c;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
del[a[i][j]].push_back(make_pair(i * m + j, q + 1));
}
}
for (int i = 0; i < lst; i++) {
reverse(del[i].begin(), del[i].end());
}
for (int i = 0; i < lst; i++) {
solve(add[i], 1);
}
for (int i = 0; i < lst; i++) {
solve(del[i], -1);
}
int curr = 1;
for (int i = 1; i <= q; i++) {
curr += diff[i];
cout << curr << '\n';
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, seq[30100], aux[30100];
int s1, s2, t1, t2, ds, dt, ls, lt;
bool fill(int start) {
bool ok = true;
for (int i = start; i < n; i++) {
if (seq[i] == seq[ls] + ds) {
ls = i;
aux[i] = 0;
} else if (t1 < 0) {
t1 = i;
aux[i] = 1;
} else if (t2 < 0) {
t2 = i;
aux[i] = 1;
dt = seq[t2] - seq[t1];
lt = t2;
} else if (ok && seq[i] == seq[lt] + dt) {
lt = i;
aux[i] = 1;
} else {
aux[i] = 1;
ok = false;
}
}
if (ok) return true;
aux[n - 1] = 1;
aux[n - 2] = 1;
dt = seq[n - 1] - seq[n - 2];
lt = n - 2;
bool allowed = true;
for (int i = n - 3; i >= 0; i--) {
if (seq[i] + dt == seq[lt]) {
if (aux[i] == 1 || allowed) {
aux[i] = 1;
lt = i;
}
} else if (aux[i] == 0) {
allowed = false;
} else {
return false;
}
}
return true;
}
int main() {
int i, j, k;
cin >> n;
for (i = 0; i < n; i++) cin >> seq[i];
bool ok = true;
if (n <= 4) {
for (i = 0; i < n; i++) aux[i] = i % 2;
} else {
aux[0] = 0;
s1 = 0;
aux[1] = 0;
s2 = 1;
ds = seq[s2] - seq[s1];
ls = s2;
t1 = -1;
t2 = -1;
dt = -1;
lt = -1;
ok = fill(2);
if (ok) {
if (t1 < 0) aux[0] = 1;
} else {
aux[1] = 1;
aux[2] = 0;
s2 = 2;
ds = seq[s2] - seq[s1];
ls = s2;
t1 = 1;
t2 = -1;
dt = -1;
lt = -1;
ok = fill(3);
if (!ok) {
aux[0] = 1;
aux[1] = 0;
aux[2] = 0;
s1 = 1;
s2 = 2;
ds = seq[s2] - seq[s1];
ls = s2;
t1 = 0;
t2 = -1;
dt = -1;
lt = -1;
ok = fill(3);
}
}
}
if (ok) {
bool fst = true;
for (i = 0; i < n; i++) {
if (aux[i] == 0) {
if (!fst) {
cout << " ";
} else {
fst = false;
}
cout << seq[i];
}
}
cout << endl;
fst = true;
for (i = 0; i < n; i++) {
if (aux[i] == 1) {
if (!fst) {
cout << " ";
} else {
fst = false;
}
cout << seq[i];
}
}
cout << endl;
} else {
cout << "No solution\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main() {
int n, x, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &x);
a[x]++;
}
int h = 0;
for (i = 1; i <= 100; i++) {
if (a[i] > 1) {
h = h + a[i] / 2;
}
}
printf("%d\n", h / 2);
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
using namespace std;
const int mod = 2011;
vector<string> s;
vector<int> inv(mod,1);
inline int get_base(int l, int t, int b){
FOR(i,t,b)if(s[i][l]!='.')return i;
return t;
}
int expr(int &l, int &t, int &r, int &b);
int fraction(int &l, int &t, int &r, int &b){
int base = t;
FOR(i,t,b)if(s[i][l]!='.')base = i;
int ll = l+1;
while(++l < r && s[base][l]=='-');
int ul = ll, ur = l-1, dl = ll, dr = l-1;
int ut = t, ub = base, dt = base+1, db = b;
return expr(ul,ut,ur,ub) * inv[expr(dl,dt,dr,db)] % mod;
}
int primary(int &l, int &t, int &r, int &b){
int base = get_base(l,t,b);
if(isdigit(s[base][l]))return s[base][l++]-'0';
l+=2;
int res = expr(l,t,r,b);
l+=2;
return res;
}
int powexpr(int &l, int &t, int &r, int &b){
int res = primary(l,t,r,b);
int base = get_base(l,t,b);
if(l<r && isdigit(s[base][l])){
int p = s[base][l]-'0';
l++;
int tmp = res; res = 1;
while(p--) (res *= tmp) %= mod;
}
return res;
}
int factor(int &l, int &t, int &r, int &b){
int base = get_base(l,t,b);
if(s[base][l] == '-'){
if(s[base][l+1] == '.'){
l+=2;
return mod - factor(l,t,r,b);
}else return fraction(l,t,r,b);
}
return powexpr(l,t,r,b);
}
int term(int &l, int &t, int &r, int &b){
int base = get_base(l,t,b);
int res = factor(l,t,r,b);
while(l+1<r && s[base][l+1]=='*'){
l+=3;
res = (res * factor(l,t,r,b)) % mod;
}
return res;
}
int expr(int &l, int &t, int &r, int &b){
//remove spaces
for(bool f=true;l<r;l++){FOR(i,t,b)f &= (s[i][l]=='.'); if(!f)break;}
for(bool f=true;l<r;r--){FOR(i,t,b)f &= (s[i][r-1]=='.'); if(!f)break;}
for(bool f=true;t<b;t++){FOR(i,l,r)f &= (s[t][i]=='.'); if(!f)break;}
for(bool f=true;t<b;b--){FOR(i,l,r)f &= (s[b-1][i]=='.'); if(!f)break;}
int base = get_base(l,t,b);
int res = term(l,t,r,b);
while(l+1<r && (s[base][l+1]=='+' || s[base][l+1]=='-')){
l+=3;
if(s[base][l-2]=='+')res = (res + term(l,t,r,b)) % mod;
else res = (res - term(l,t,r,b) + mod) % mod;
}
return res;
}
int main(){
cin.tie(); ios::sync_with_stdio(0);
FOR(i,2,mod)inv[i] = (mod - mod/i) * inv[mod%i] % mod;
int n;
while(cin>>n,n){
s.resize(n);
FOR(i,0,n)cin >> s[i];
int l = 0, t = 0, r = s[0].size(), b = n;
cout << expr(l,t,r,b) << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1001;
char l[MAX][10][10];
int g[MAX][MAX];
bool c[MAX];
struct edge {
int w;
int p;
int n;
edge(int w, int p, int n) {
this->w = w;
this->p = p;
this->n = n;
}
edge(int p, int n) {
this->p = p;
this->n = n;
this->w = g[p][n];
}
bool operator<(edge const& x) const { return this->w > x.w; }
};
priority_queue<edge> q;
set<int> notCheck;
vector<edge> ans;
int main() {
ios::sync_with_stdio(false);
memset(g, 0, sizeof(g));
memset(c, false, sizeof(c));
int n, m, k, w;
cin >> n >> m >> k >> w;
int base = n * m;
for (int i = 0; i < k; i++) {
g[MAX - 1][i] = base;
g[i][MAX - 1] = base;
char tmp;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
cin >> tmp;
l[i][x][y] = tmp;
}
}
notCheck.insert(i);
}
for (int i = 0; i < k - 1; i++) {
g[i][i] = 0;
for (int j = i + 1; j < k; j++) {
int count = 0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (l[i][x][y] != l[j][x][y]) count++;
}
}
count *= w;
g[i][j] = count;
g[j][i] = count;
}
}
int r = 0;
for (int i = 0; i < k; i++) {
q.push(edge(MAX - 1, i));
}
while (!notCheck.empty()) {
edge tmp = q.top();
q.pop();
if (notCheck.count(tmp.n) > 0) {
ans.push_back(tmp);
r += tmp.w;
notCheck.erase(tmp.n);
c[tmp.n] = true;
for (int i = 0; i < k; i++) {
if (!c[i]) q.push(edge(tmp.n, i));
}
} else if (notCheck.count(tmp.p) > 0) {
ans.push_back(tmp);
r += tmp.w;
notCheck.erase(tmp.n);
c[tmp.p] = true;
for (int i = 0; i < k; i++) {
if (!c[i]) q.push(edge(tmp.p, i));
}
}
}
cout << r << endl;
for (edge e : ans) {
if (e.n == MAX - 1) {
cout << e.p + 1 << ' ' << 0 << endl;
} else if (e.p == MAX - 1) {
cout << e.n + 1 << ' ' << 0 << endl;
} else {
cout << e.n + 1 << ' ' << e.p + 1 << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x1, y1, x2, y2;
cin >> n >> x1 >> y1 >> x2 >> y2;
int xmin = x1, xmax = x2, ymin = y1, ymax = y2;
int a = (xmin - xmax) * (ymin - ymax);
for (int i = 1; i < n; i++) {
cin >> x1 >> y1 >> x2 >> y2;
a += ((x2 - x1) * (y2 - y1));
if (x1 < xmin) xmin = x1;
if (x2 > xmax) xmax = x2;
if (y1 < ymin) ymin = y1;
if (y2 > ymax) ymax = y2;
}
if (a == (xmax - xmin) * (ymax - ymin) && (xmax - xmin) == (ymax - ymin))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> p;
int a[100][100];
int find_set(int v) {
if (p[v] == v) return v;
return p[v] = find_set(p[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (rand() & 1) swap(a, b);
p[a] = b;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<int> v[m];
for (int i = 0; i < n; i++) p.push_back(i);
for (int i = 0; i < n; i++) {
int k;
scanf("%d", &k);
while (k--) {
int x;
scanf("%d", &x);
x--;
for (int j : v[x]) union_sets(i, j);
v[x].push_back(i);
}
}
int ans = 0;
for (int i = 0; i < n; i++) ans += p[i] == i;
char flag = true;
for (int i = 0; i < m; i++)
if (!v[i].empty()) flag = false;
if (flag) ans = n + 1;
printf("%d", ans - 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 79;
vector<vector<int> > w(maxn);
vector<int> who(maxn, -1), when(maxn, -1), wait(maxn, -1), tin(maxn, -1),
tout(maxn);
int timer = 0;
void dfs(int v) {
tin[v] = timer++;
for (int i : w[v]) {
dfs(i);
}
tout[v] = timer - 1;
}
bool in_subtree(int p, int v) {
if (tin[p] <= tin[v] && tout[v] <= tout[p]) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k, q;
cin >> n >> m >> k >> q;
for (int i = 0; i < k; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
if (wait[a] != -1) continue;
if (who[b] != -1) {
w[who[b]].push_back(a);
wait[a] = who[b];
}
who[b] = a;
}
for (int i = 0; i < n; i++)
if (tin[i] == -1) dfs(i);
while (q--) {
int x, y;
cin >> x >> y;
x--;
y--;
if (who[y] == -1) {
cout << "0\n";
continue;
}
int z = who[y];
if (in_subtree(x, who[y])) {
cout << tout[x] - tin[x] + 1 << "\n";
} else
cout << "0\n";
}
return 0;
}
| 4 |
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
while (1){
int i,m,n,sum=0;
int L[1000];
scanf("%d %d",&n,&m);
if (n==0 && m==0) return 0;
for (i=0;i<n;i++){
scanf("%d",&L[i]);
sum+=L[i];
}
sort(L,L+n);
for (i=n-m;i>=0;i-=m)
sum-=L[i];
printf("%d\n",sum);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void balance(string s, int n, int l) {
long long int sum = 0;
for (int i = 0; i < l; i++) {
if (s[i] != '=' && i != n) {
long long int p = s[i] - 48;
sum += p * (n - i);
}
}
if (sum == 0)
cout << "balance";
else if (sum > 0)
cout << "left";
else
cout << "right";
}
int main() {
string s;
cin >> s;
int n;
int l = s.length();
for (int i = 0; i < l; i++) {
if (s[i] == '^') {
n = i;
break;
}
}
balance(s, n, l);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int difcol[N], difpos[N], num[N], last[N], pos[N], far[N], ans[N], can[N];
vector<int> gr[N], ys[N];
int n, m;
int lowbit(int x) { return x & (-x); }
void add(int a, int* pArr, int v) {
while (a <= n) {
(*(pArr + a)) += v;
a += lowbit(a);
}
}
void add(int a, int b, int* pArr, int v) {
add(a, pArr, v);
add(b + 1, pArr, -v);
}
int getsum(int* pArr, int a) {
int rev = 0;
while (a) {
rev += (*(pArr + a));
a -= lowbit(a);
}
return rev;
}
void dealcan(int pos) {
if (last[last[pos]]) {
if ((last[last[pos]] - last[pos]) != (last[pos] - pos))
can[pos] = 0;
else
can[pos] = 1;
} else
can[pos] = 1;
if ((can[pos]) && (last[pos]))
far[pos] = far[last[pos]];
else
far[pos] = last[last[pos]] + 1;
}
void work() {
for (int i = 1; i <= n; i++) dealcan(i);
for (int i = 1; i <= n; i++) {
add(last[i] + 1, i, difcol, 1);
add(last[i] + 1, i, difpos, 1);
if (!can[i]) add(far[last[i]], last[last[i]], difpos, -1);
for (int j = 0; j < gr[i].size(); j++) {
int sumcol = getsum(difcol, gr[i][j]);
int sumpos = getsum(difpos, gr[i][j]);
if (sumpos)
ans[ys[i][j]] = sumcol;
else
ans[ys[i][j]] = sumcol + 1;
}
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &num[i]);
last[i] = pos[num[i]];
pos[num[i]] = i;
}
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
gr[b].push_back(a);
ys[b].push_back(i);
}
work();
return 0;
}
| 4 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 200005;
set<int> adj[maxn];
int sz[maxn];
set<pair<int,int>> s;
void calcSizes(int u, int par) {
sz[u] = 1;
for (int v : adj[u]) {
if (v == par) continue;
calcSizes(v, u);
sz[u] += sz[v];
}
}
pair<int,int> findEdge(int u, int par, int nodes) {
pair<int,int> e = {-1,-1};
for (int v : adj[u]) {
if (v == par) continue;
pair<int,int> p = findEdge(v,u,nodes);
if (p.first != -1) e = p;
if (s.count({nodes-sz[v],sz[v]}) != 0 || s.count({sz[v],nodes-sz[v]}) != 0) e = {u,v};
}
return e;
}
void rec(int r) {
calcSizes(r,r);
if (sz[r] == 1) return;
pair<int,int> e = findEdge(r,r,sz[r]);
if (e.first == -1) {
cout << "NO\n";
exit(0);
}
adj[e.first].erase(e.second);
adj[e.second].erase(e.first);
rec(e.first);
rec(e.second);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int x = 1;
int y = 1;
s.insert({x,y});
for (int i = 0; i < 30; i++) {
int tmp = y;
y = x+y;
x = tmp;
s.insert({x,y});
}
int n;
cin >> n;
for (int i = 0; i < n-1; i++) {
int u, v;
cin >> u >> v;
adj[u].insert(v);
adj[v].insert(u);
}
rec(1);
cout << "YES\n";
}
/*
*/
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long a, b;
cin >> a >> b;
while (1) {
if (a == 0 || b == 0)
break;
else if (a >= (2 * b))
a = a % (2 * b);
else if (b >= (2 * a))
b = b % (2 * a);
else
break;
}
cout << a << " " << b << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, mod = 1e9 + 7;
int n, cnt, prime[N], phi[N], mu[N], a[N], id[N], c[N];
bool vis[N];
int Pow(int x, int k) {
int ret = 1;
while (k) {
if (k & 1) ret = (long long)ret * x % mod;
k >>= 1;
x = (long long)x * x % mod;
}
return ret;
}
void init() {
mu[1] = phi[1] = 1;
for (int i = 2; i <= 200000; i++) {
if (!vis[i]) {
prime[++prime[0]] = i;
mu[i] = -1;
phi[i] = i - 1;
}
for (int j = 1; j <= prime[0] && i * prime[j] <= 200000; j++) {
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
mu[i * prime[j]] = -mu[i];
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
int h[N], dep[N], re[N * 2], fir[N * 2], bin[N * 2], p[N * 2][22];
bool mark[N];
int q[N], st[N], tot, top, dfn[N], sign;
int f[N], g[N], size[N];
struct edge {
int to, next, w;
} e[N * 2];
void addedge(int x, int y, int z) {
e[++cnt] = (edge){y, h[x], z};
h[x] = cnt;
}
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
dfn[x] = ++dfn[0];
re[++sign] = x;
fir[x] = sign;
for (int i = h[x]; i; i = e[i].next) {
int y = e[i].to;
if (y == fa) continue;
dfs(y, x);
re[++sign] = x;
}
}
bool cmp(int x, int y) { return dfn[x] < dfn[y]; }
int lca(int x, int y) {
x = fir[x];
y = fir[y];
if (x > y) swap(x, y);
int k = bin[y - x + 1];
return dep[p[x][k]] < dep[p[y - (1 << k) + 1][k]] ? p[x][k]
: p[y - (1 << k) + 1][k];
}
void dp(int x) {
size[x] = f[x] = g[x] = 0;
if (mark[x]) size[x] = phi[a[x]];
for (int &i = h[x]; i; i = e[i].next) {
int y = e[i].to;
dp(y);
f[x] =
(f[x] + f[y] + (long long)size[x] * g[y] + (long long)size[y] * g[x] +
(long long)size[x] * size[y] % mod * e[i].w) %
mod;
g[x] = (g[x] + g[y] + (long long)size[y] * e[i].w) % mod;
size[x] = (size[x] + size[y]) % mod;
}
}
int main() {
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int tmp = (long long)i * Pow(phi[i], mod - 2) % mod;
for (int j = 1; i * j <= n; j++)
c[i * j] = (c[i * j] + (long long)mu[j] * tmp) % mod;
}
bin[1] = 0;
for (int i = 2; i <= 2 * n; i++) bin[i] = bin[i >> 1] + 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
id[a[i]] = i;
}
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
addedge(x, y, 1);
addedge(y, x, 1);
}
dfs(1, 0);
for (int i = 1; i <= sign; i++) p[i][0] = re[i];
for (int j = 1; (1 << j) <= sign; j++) {
for (int i = 1; i + (1 << j) - 1 <= sign; i++)
p[i][j] = dep[p[i][j - 1]] < dep[p[i + (1 << j - 1)][j - 1]]
? p[i][j - 1]
: p[i + (1 << j - 1)][j - 1];
}
memset(h, 0, sizeof(h));
cnt = 0;
int ans = 0;
for (int T = 1; T <= n; T++) {
tot = top = cnt = 0;
for (int i = T; i <= n; i += T) {
q[++tot] = id[i];
mark[id[i]] = 1;
}
sort(q + 1, q + tot + 1, cmp);
if (q[1] != 1) st[++top] = 1;
for (int i = 1; i <= tot; i++) {
int now = q[i], f = 0;
while (top > 0) {
f = lca(st[top], now);
if (top > 1 && dep[f] < dep[st[top - 1]]) {
addedge(st[top - 1], st[top], dep[st[top]] - dep[st[top - 1]]);
top--;
} else if (dep[f] < dep[st[top]]) {
addedge(f, st[top], dep[st[top]] - dep[f]);
top--;
break;
} else
break;
}
if (st[top] != f) st[++top] = f;
st[++top] = now;
}
while (top > 1)
addedge(st[top - 1], st[top], dep[st[top]] - dep[st[top - 1]]), top--;
dp(1);
for (int i = 1; i <= tot; i++) mark[q[i]] = 0;
ans = (ans + (long long)f[1] * c[T]) % mod;
}
cout << ((long long)ans * Pow((long long)n * (n - 1) / 2 % mod, mod - 2) %
mod +
mod) %
mod
<< "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long MOD = 998244353;
int n;
long long k;
long long fac[MAXN];
long long inv[MAXN];
long long Pow(long long a, long long b) {
long long result = 1;
while (b) {
if (b & 1) {
result *= a;
result %= MOD;
}
a *= a;
a %= MOD;
b /= 2;
}
return result;
}
long long C(int n, int m) {
if (m > n) {
return 0;
}
return fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
long long Stirling2(int n, int k) {
int len = max(n, k);
fac[0] = 1;
for (int i = 1; i <= len; ++i) {
fac[i] = fac[i - 1] * i % MOD;
}
inv[len] = Pow(fac[len], MOD - 2);
for (int i = len - 1; i >= 0; --i) {
inv[i] = inv[i + 1] * (i + 1) % MOD;
}
long long answer = 0;
for (int i = 0; i <= k; ++i) {
answer += Pow(-1, k - i) * C(k, i) * Pow(i, n) % MOD;
answer %= MOD;
}
answer %= MOD;
answer += MOD;
answer %= MOD;
return inv[k] * answer % MOD;
}
int main() {
scanf("%d%lld", &n, &k);
long long answer = 0;
if (k == 0) {
answer = 1;
for (int i = 2; i <= n; ++i) {
answer *= i;
answer %= MOD;
}
printf("%lld\n", answer);
return 0;
}
if (k >= n) {
printf("0\n");
return 0;
}
long long s = Stirling2(n, n - k);
answer = 2 * inv[k] * s % MOD * fac[n] % MOD;
printf("%lld\n", answer);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))
int n, k;
int a[100100], b[100100];
ll ans, ret;
int c;
int main()
{
cin >> n >> k;
F(i, 1, n) scanf("%d %d", a + i, b + i);
ans = 0;
F(i, 1, n)
{
if ((a[i] | k) == k) ans += b[i];
}
F(i, 0, 30)if ((1 << i)&k)
{
c = (k - (1 << i)) | ((1 << i) - 1);
ll ret = 0;
F(j, 1, n) if ((c | a[j]) == c) ret += b[j];
ans = max(ans, ret);
}
cout << ans << endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
using namespace std;
const int N=2e5+5,inf=1e9;
int n,p[N],q[N],b[N],b2[N],bs1,bs,ans;
int la[N],ne[N*2],to[N*2],l[N*2],h[N],st[N],d[N],js;
bool bz[N];
void link(int x,int y,int z){
// printf("%d %d\n",x,y);
ne[++js]=la[x];la[x]=js;to[js]=y;l[js]=z;
ne[++js]=la[y];la[y]=js;to[js]=x;
}
bool is(){
fo(i,0,bs+1)st[i]=la[i],h[i]=-1;
d[1]=0;h[0]=0;
for(int s=0,t=1,x;x=d[++s],s<=t;)
for(int i=la[x];i;i=ne[i])if(l[i]&&h[to[i]]==-1)
h[to[i]]=h[x]+1,d[++t]=to[i];
return h[bs+1]>-1;
}
int dinic(int x,int su){
if(x==bs+1)return su;int us=0;
for(int i=st[x];i;i=ne[i])if(l[i]&&h[to[i]]==h[x]+1){
st[x]=i;int tt=dinic(to[i],min(su-us,l[i]));
if(tt){
l[i]-=tt;l[i^1]+=tt;us+=tt;
if(us==su)return us;
}
}
h[x]=-1;
return us;
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
fo(i,1,n)cin>>p[i],++p[i];
fo(i,1,n)cin>>q[i],++q[i];
fo(i,1,n)if(!bz[i]){
int j=i;++bs;
for(;!bz[j];j=p[j])b[j]=bs,bz[j]=1;
}
memset(bz,0,sizeof bz);
fo(i,1,n)if(!bz[i]){
int j=i;++bs;
for(;!bz[j];j=q[j])b2[j]=bs,bz[j]=1;
}
js=1;ans=n;
fo(i,1,n)
if(p[i]==i){
if(q[i]==i)--ans;else link(0,b2[i],1);
}else{
if(q[i]==i)link(b[i],bs+1,1);
else{
link(b[i],b2[i],1);
if(p[i]==q[i])link(b2[i],b[i],1);
}
}
for(;is();)ans-=dinic(0,inf);
cout<<ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long binary(long long mid) {
long long i = 2, possibilities = 0;
while (i * i * i <= mid) {
possibilities += mid / (i * i * i);
i++;
}
return possibilities;
}
int main() {
long long m, min, max;
scanf("%I64d", &m);
min = -1;
max = 1e17;
while (max > min + 1) {
long long mid = (min + max) / 2;
if (binary(mid) >= m) {
max = mid;
} else {
min = mid;
}
}
if (binary(max) == m) {
printf("%I64d\n", max);
} else {
printf("-1\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long merge(long long a[], long long l, long long mid, long long r) {
long long cnt = 0;
long long size_l = mid - l + 1, size_r = r - mid;
long long left[size_l], right[size_r];
for (long long i = 0; i < size_l; i++) left[i] = a[l + i];
for (long long i = 0; i < size_r; i++) right[i] = a[mid + i + 1];
long long i = 0, j = 0, k = l;
while (i < size_l && j < size_r) {
if (left[i] <= right[j]) {
a[k++] = left[i++];
} else {
a[k++] = right[j++];
cnt += size_l - i;
}
}
while (i < size_l) {
a[k++] = left[i++];
}
while (j < size_r) {
a[k++] = right[j++];
}
return cnt;
}
long long mergeSort(long long a[], long long l, long long r) {
long long ans = 0;
long long mid;
if (r > l) {
mid = (l + r) / 2;
ans += mergeSort(a, l, mid);
ans += mergeSort(a, mid + 1, r);
ans += merge(a, l, mid, r);
}
return ans;
}
void solve() {
long long n;
cin >> n;
string s;
cin >> s;
vector<queue<long long>> pos(30);
string tmp = s;
reverse((tmp).begin(), (tmp).end());
for (long long i = 0; i < n; i++) {
long long x = tmp[i] - 'a';
pos[x].push(i);
}
long long a[n];
for (long long i = 0; i < n; i++) {
long long x = s[i] - 'a';
a[i] = pos[x].front();
pos[x].pop();
}
cout << mergeSort(a, 0, n - 1) << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long OO = 1e14;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long t = 1, n, x;
while (t--) {
cin >> n;
long long ans = 1;
map<long long, long long> mp1, mp2;
for (long long i = 1; i < n + 1; ++i) {
cin >> x;
mp1[x]++, mp2[mp1[x]]++;
if (mp1[x] * mp2[mp1[x]] == i && i != n)
ans = i + 1;
else if (mp1[x] * mp2[mp1[x]] == i - 1)
ans = i;
}
cout << ans << '\n';
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int parent[100010];
int sz[100010];
int Time, cnt;
struct operation {
int type, id, old, New, Time;
operation() {}
operation(int type, int id, int old, int New, int Time)
: type(type), id(id), old(old), New(New), Time(Time) {}
operation(const operation &op)
: type(op.type), id(op.id), old(op.old), New(op.New), Time(op.Time) {}
};
stack<operation> st;
void rollback(const operation &op) {
if (op.type == 0)
parent[op.id] = op.old;
else if (op.type == 1)
sz[op.id] = op.old;
else
cnt = op.old;
}
int find(int cur) {
if (parent[cur] == cur) return cur;
return find(parent[cur]);
}
void Union(int x, int y) {
int xroot = find(x);
int yroot = find(y);
if (xroot == yroot) return;
++Time;
st.push(operation(2, 0, cnt, cnt - 1, Time));
cnt--;
if (sz[xroot] < sz[yroot]) swap(xroot, yroot);
st.push(operation(1, xroot, sz[xroot], sz[xroot] + sz[yroot], Time));
sz[xroot] += sz[yroot];
st.push(operation(1, yroot, sz[yroot], 0, Time));
sz[yroot] = 0;
st.push(operation(0, yroot, parent[yroot], xroot, Time));
parent[yroot] = xroot;
}
void init(int n) {
Time = 0;
while (!st.empty()) st.pop();
cnt = n;
for (int i = 0; i <= n; ++i) {
sz[i] = 1;
parent[i] = i;
}
}
int x[100010], y[100010], Out[100010];
vector<int> List[600][600];
vector<int> befor[100010], after[100010];
int main() {
int n, k, m, q, i, j;
scanf("%d", &n);
scanf("%d", &k);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
--x;
--y;
if (x < y) {
after[x].push_back(y);
befor[y].push_back(x);
} else {
after[y].push_back(x);
befor[x].push_back(y);
}
}
init(n);
scanf("%d", &q);
for (i = 0; i < q; i++) {
scanf("%d", &x[i]);
scanf("%d", &y[i]);
--x[i];
--y[i];
if (x[i] >= n) {
Out[i] = 0;
continue;
}
if (y[i] >= n) y[i] = n - 1;
int sb = x[i] / 300;
if (sb * 300 != x[i]) sb++;
int eb = y[i] / 300;
if ((eb + 1) * 300 != y[i] + 1) eb--;
if (eb < sb) {
int lim = Time;
for (j = x[i]; j <= y[i]; j++)
for (auto z : befor[j])
if (z >= x[i]) Union(j, z);
Out[i] = cnt;
while (!st.empty()) {
rollback(st.top());
st.pop();
}
} else
List[sb][eb].push_back(i);
}
for (i = 0; i * 300 < n; i++) {
init(n);
for (j = i; (j + 1) * 300 <= n; j++) {
for (int k = j * 300; k < (j + 1) * 300; k++)
for (auto x : befor[k])
if (x >= i * 300) Union(k, x);
int lim = Time;
for (int k = 0; k < List[i][j].size(); k++) {
for (int l = x[List[i][j][k]]; l < i * 300; l++)
for (auto x : after[l])
if (x < (j + 1) * 300) Union(l, x);
for (int l = (j + 1) * 300; l <= y[List[i][j][k]]; l++)
for (auto z : befor[l])
if (z >= x[List[i][j][k]]) Union(l, z);
Out[List[i][j][k]] = cnt;
while (!st.empty()) {
if (st.top().Time <= lim) break;
rollback(st.top());
st.pop();
}
}
}
}
for (i = 0; i < q; i++) printf("%d\n", Out[i] - (n - (y[i] - x[i] + 1)));
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
void out(string s) {
cout << s << endl;
cout.flush();
}
int getdist(string i) {
out(i);
int res;
scanf("%d", &res);
if (res == 0) exit(0);
return res;
}
string gets(int pos, int length) {
string s = "";
for (int i = (0); i < (length); i++) {
if (i == pos)
s += "a";
else
s += "b";
}
return s;
}
int main() {
int as, bs;
string s = "";
for (int i = (0); i < (300); i++) s += "a";
as = 300 - getdist(s);
for (int i = (0); i < (300); i++) s[i] = 'b';
bs = 300 - getdist(s);
if (as == 0) {
s = "";
for (int i = (0); i < (bs); i++) s += "b";
out(s);
return 0;
}
if (bs == 0) {
s = "";
for (int i = (0); i < (as); i++) s += "a";
out(s);
return 0;
}
int l = as + bs, am = 0;
string ans = "";
for (int i = (0); i < (l - 1); i++) {
if (getdist(gets(i, l)) == (as - 1)) {
ans += "a";
am++;
} else
ans += "b";
}
if (as == am)
ans += "b";
else
ans += "a";
cout << ans << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long const INF = 2e18 + 100;
int const MAX = 3 * 1e5 + 10;
long long l = (1LL << 62);
unsigned long long m = (1LL << 63);
vector<pair<int, int> > v;
multiset<int> mset;
multiset<int, greater<int> > ms;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, i, j, x;
cin >> q;
while (q--) {
cin >> n;
int arr[n + 1];
for (i = 1; i <= n; i++) {
cin >> arr[i];
}
for (i = 1; i <= n; i++) {
j = arr[i];
x = 1;
while (1) {
if (arr[j] == arr[i]) {
cout << x << " ";
break;
}
j = arr[j];
x++;
}
}
cout << '\n';
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
struct edge{int to,cost;};
typedef pair<int,int> P;
int V;
vector<edge> G[200001];
int mincost[200001],INF=1e9,used[200001]={0};
int prim(){
priority_queue<P,vector<P>,greater<P> > que;
fill(mincost,mincost+V,INF);
mincost[0]=0;
int res=0;
que.push(P(0,0));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(used[v]) continue;
res+=mincost[v];
used[v]++;
rep(i,G[v].size()){
edge e=G[v][i];
if(mincost[e.to]>e.cost) mincost[e.to]=e.cost,que.push(P(mincost[e.to],e.to));
}
}
return res;
}
int main(){
int E,a,b,c;
cin>>V>>E;
rep(i,E) cin>>a>>b>>c,G[a].push_back({b,c}),G[b].push_back({a,c});
cout<<prim()<<endl;
}
| 0 |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
using namespace std;
const long long int MOD = 1000000007;
const long double EPS = 1e-8;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(15);
long double ans = 0;
long double d;
cin >> d;
cout << max((long double)((int)d + 1), d*sqrt((long double)2)) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n,m,q,a1,b1,a2,b2,a[2005][2005],s1[2005][2005],s2[2005][2005],s3[2005][2005];char c[2005];
int gao1(int a1,int b1,int a2,int b2){return s1[a2][b2]-s1[a1-1][b2]-s1[a2][b1-1]+s1[a1-1][b1-1];}
int gao2(int a1,int b1,int a2,int b2){return s2[a2][b2]-s2[a1-1][b2]-s2[a2][b1-1]+s2[a1-1][b1-1];}
int gao3(int a1,int b1,int a2,int b2){return s3[a2][b2]-s3[a1-1][b2]-s3[a2][b1-1]+s3[a1-1][b1-1];}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=1;i<=n;i++){scanf("%s",c+1);for(int j=1;j<=m;j++) a[i][j]=c[j]-'0';}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
s1[i][j]=a[i][j];
if(a[i][j]&&a[i][j+1]) s2[i][j]=1;
if(a[i][j]&&a[i+1][j]) s3[i][j]=1;
}
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
{
s1[i][j]+=s1[i-1][j]+s1[i][j-1]-s1[i-1][j-1];
s2[i][j]+=s2[i-1][j]+s2[i][j-1]-s2[i-1][j-1];
s3[i][j]+=s3[i-1][j]+s3[i][j-1]-s3[i-1][j-1];
}
while(q--) scanf("%d%d%d%d",&a1,&b1,&a2,&b2),printf("%d\n",gao1(a1,b1,a2,b2)-gao2(a1,b1,a2,b2-1)-gao3(a1,b1,a2-1,b2));
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 1e18;
const double pi = acos(-1.0);
const int MAXN = 1e6 + 10;
const int MAXM = 1e5 + 10;
const double eps = 1e-8;
char s[MAXN];
int m;
deque<char> q;
int main() {
int n;
scanf("%d", &n);
scanf("%s", s);
scanf("%d", &m);
while (m--) {
int x;
char ch;
scanf("%d %c", &x, &ch);
int y = x;
int sum = 0;
int maxn = 0;
while (!q.empty()) q.pop_front();
if (x >= n) {
printf("%d\n", n);
continue;
}
for (int i = 0; i < n; ++i) {
if (s[i] == ch) {
q.push_back(s[i]);
++sum;
} else {
if (!x) {
while (q.size() && q.front() == ch) q.pop_front(), --sum;
q.pop_front();
--sum;
++x;
}
--x;
q.push_back(s[i]);
++sum;
}
maxn = max(maxn, sum);
}
printf("%d\n", maxn);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void build(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[], long long a[]) {
if (tl == tr) {
st[v] = a[tl];
lz[v] = 0LL;
f[v] = false;
return;
}
build((v << 1), tl, ((tl + tr) >> 1), st, lz, f, a);
build((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, st, lz, f, a);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
lz[v] = 0LL;
f[v] = false;
return;
}
void push(long long v, long long tl, long long tr, long long st[],
long long lz[], bool f[]) {
if (f[v]) {
st[(v << 1)] = lz[(v << 1)] = st[(v << 1) | 1] = lz[(v << 1) | 1] = 0LL;
f[(v << 1)] = f[(v << 1) | 1] = true;
f[v] = false;
}
st[(v << 1)] += lz[v] * (((tl + tr) >> 1) - tl + 1);
lz[(v << 1)] += lz[v];
st[(v << 1) | 1] += lz[v] * (tr - ((tl + tr) >> 1));
lz[(v << 1) | 1] += lz[v];
lz[v] = 0LL;
return;
}
void update(long long v, long long tl, long long tr, long long l, long long r,
long long val, bool set, long long st[], long long lz[], bool f[]) {
if (l > r) {
return;
}
if (l == tl && tr == r) {
if (set) {
st[v] = lz[v] = 0LL;
f[v] = true;
}
st[v] += val * (tr - tl + 1);
lz[v] += val;
return;
}
push(v, tl, tr, st, lz, f);
update((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), val, set,
st, lz, f);
update((v << 1) | 1, ((tl + tr) >> 1) + 1, tr, max(l, ((tl + tr) >> 1) + 1),
r, val, set, st, lz, f);
st[v] = st[(v << 1)] + st[(v << 1) | 1];
return;
}
long long query(long long v, long long tl, long long tr, long long l,
long long r, long long st[], long long lz[], bool f[]) {
if (l > r) {
return 0LL;
}
if (l == tl && tr == r) {
return st[v];
}
push(v, tl, tr, st, lz, f);
return query((v << 1), tl, ((tl + tr) >> 1), l, min(r, ((tl + tr) >> 1)), st,
lz, f) +
query((v << 1) | 1, ((tl + tr) >> 1) + 1, tr,
max(l, ((tl + tr) >> 1) + 1), r, st, lz, f);
}
int power(long long k) {
long long temp = 1;
for (long long i = 0; i <= k - 1; i++) {
temp = temp * 10;
}
return temp;
}
bool compare(const pair<long long, long long>& a,
const pair<long long, long long>& b) {
return a.second < b.second;
}
pair<int, int> mostFrequent(long long arr[], long long n) {
unordered_map<long long, long long> hash;
for (long long i = 0; i < n; i++) hash[arr[i]]++;
long long max_count = 0, res = -1;
for (auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
pair<long long, long long> temp;
temp.first = res;
temp.second = max_count;
return temp;
}
long long div(long long n) {
if (n % 2 == 0) return 2;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) return i;
}
return n;
}
long long gcd(long long a, long long b) {
if (!b) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i <= n - 1; i++) {
cin >> a[i];
}
unordered_map<long long, long long> umap;
unordered_set<long long> uset;
unordered_set<long long>::iterator itr;
for (long long i = 0; i <= n - 1; i++) {
umap[a[i]]++;
if (uset.find(a[i]) == uset.end()) {
uset.insert(a[i]);
}
}
long long flag = 0;
for (itr = uset.begin(); itr != uset.end(); itr++) {
long long b = umap[*itr] % (n - (*itr));
if ((b != 0)) {
cout << "Impossible";
flag = 1;
break;
}
}
if (flag == 0) {
cout << "Possible" << endl;
vector<long long> v[n + 1];
long long x = 1;
for (itr = uset.begin(); itr != uset.end(); itr++) {
long long y = umap[*itr] / (n - (*itr));
while (y != 0) {
long long z = (n - (*itr));
while (z != 0) {
v[*itr].push_back(x);
z--;
}
x++;
y--;
}
}
for (long long i = 0; i <= n - 1; i++) {
cout << v[a[i]].back() << " ";
v[a[i]].pop_back();
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int test;
cin>>test;
while(test--){
int a,b,c;
cin>>a>>b>>c;
int a1=a,b1=b;
while(a1%2==0){
a1=a1/2;
}
while(b1%2==0){
b1=b1/2;
}
if(a*b/(a1*b1)>=c){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long SIZE = 100000;
const int INF = 0x3f3f3f3f;
const long long ll_INF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acos(-1);
const long long MAXN = numeric_limits<long long>::max();
const long long MAX = 2000000;
void solve() {
string s, t;
cin >> s >> t;
long long n = s.size();
long long dis = 0;
for (long long i = 0; i < n; i++) {
if (s[i] != t[i]) dis++;
}
if (dis % 2 != 0)
cout << "impossible" << endl;
else {
long long num = dis / 2;
long long a[n], b[n];
for (long long i = 0; i < n; i++) a[i] = s[i] - '0';
for (long long i = 0; i < n; i++) b[i] = t[i] - '0';
long long d = 0;
for (long long i = 0; i < n; i++) {
if (a[i] != b[i] && d != num) {
if (a[i] == 1)
a[i] = 0;
else
a[i] = 1;
d++;
}
if (d == num) break;
}
for (long long i = 0; i < n; i++) cout << a[i];
}
}
int main() {
std::ios::sync_with_stdio(false);
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long cnt[10010];
long long ans = 0;
string s;
int n,p;
cin >> n >> p >> s;
if(p == 2 || p == 5)
{
for(int i = 0;i < n;i++)
if((s[i] - '0') % p == 0)
ans += i + 1;
}
else
{
cnt[0] = 1;
long long cur = 0,t = 1;
for(int i = n - 1;i >= 0;i--)
{
cur = (cur + (s[i] - '0')*t) % p;
t = t*10%p;
ans += cnt[cur]++;
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool hasCycle = false;
int VISITED = 2;
int UNVISITED = 0;
int VISITING = 1;
void dfs(int u, int p, vector<vector<int>>& g, vector<int>& vis) {
if (hasCycle) return;
vis[u] = VISITING;
for (int e : g[u]) {
if (e != p && vis[e] == VISITING) {
hasCycle = true;
return;
}
if (vis[e] == UNVISITED) {
dfs(e, u, g, vis);
}
}
vis[u] = VISITED;
}
bool cyclic(vector<int>& a) {
vector<vector<int>> g(a.size(), vector<int>());
for (int i = 0; i < a.size(); i++) {
for (int j = i - 1; j >= 0; j--) {
if (a[i] < a[j]) {
g[i].push_back(j);
g[j].push_back(i);
break;
}
}
for (int j = i + 1; j < a.size(); j++) {
if (a[i] < a[j]) {
g[i].push_back(j);
g[j].push_back(i);
break;
}
}
}
hasCycle = false;
vector<int> vis(a.size(), UNVISITED);
for (int i = 0; i < a.size(); i++) {
if (vis[i] == UNVISITED && !hasCycle) dfs(i, -1, g, vis);
}
bool ret = hasCycle;
hasCycle = false;
return ret;
}
const long long MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
long long f = 1;
long long p = 1;
for (int i = 2; i <= n; i++) {
f = (f * i) % MOD;
p = (2 * p) % MOD;
}
cout << (f - p + MOD) % MOD << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, num = 0, mx = 0, f[200010], fa[200010][20], st[200010][20],
ans[200010], val[200010], h[200010], flag[200010], d[200010];
struct node {
int x, y, z, id;
} a[200010];
struct edge {
int x, y, z, next;
} mp[200010 << 1];
inline char gc() {
static char *S, *T, buf[1 << 16];
if (T == S) {
T = (S = buf) + fread(buf, 1, 1 << 16, stdin);
if (T == S) return EOF;
}
return *S++;
}
inline int read() {
int x = 0, f = 1;
char ch = gc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = gc();
}
while ('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = gc();
return x * f;
}
int find(int x) {
if (f[x] != x) f[x] = find(f[x]);
return f[x];
}
inline void ins(int x, int y, int z) {
mp[++num].x = x;
mp[num].y = y;
mp[num].z = z;
mp[num].next = h[x];
h[x] = num;
mp[++num].x = y;
mp[num].y = x;
mp[num].z = z;
mp[num].next = h[y];
h[y] = num;
}
inline bool cmp(node x, node y) { return x.z < y.z; }
inline void kruskal() {
sort(a + 1, a + m + 1, cmp);
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
int x = find(a[i].x), y = find(a[i].y);
if (x == y) continue;
f[x] = y;
flag[i] = 1;
ins(a[i].x, a[i].y, a[i].z);
}
}
void dfs(int x) {
for (int i = 1; i <= 18; i++) {
if (!fa[x][i - 1]) break;
fa[x][i] = fa[fa[x][i - 1]][i - 1];
st[x][i] = max(st[x][i - 1], st[fa[x][i - 1]][i - 1]);
}
for (int i = h[x]; i; i = mp[i].next) {
int y = mp[i].y;
if (y == fa[x][0]) continue;
fa[y][0] = x;
st[y][0] = mp[i].z;
d[y] = d[x] + 1;
dfs(y);
}
}
inline int lca(int x, int y) {
mx = 0;
if (d[x] < d[y]) swap(x, y);
int len = d[x] - d[y];
for (int i = 0; i <= 18; i++) {
if (len & (1 << i)) mx = max(mx, st[x][i]), x = fa[x][i];
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) {
mx = max(mx, max(st[x][i], st[y][i]));
x = fa[x][i], y = fa[y][i];
}
}
mx = max(mx, max(st[x][0], st[y][0]));
return fa[x][0];
}
inline void merge(int x, int y, int z) {
x = find(x);
while (d[x] > d[y]) {
val[x] = z;
x = f[x] = find(fa[x][0]);
}
}
int main() {
n = read();
m = read();
for (int i = 1; i <= m; i++)
a[i].x = read(), a[i].y = read(), a[i].z = read(), a[i].id = i;
kruskal();
dfs(1);
for (int i = 1; i <= n; i++) f[i] = i, val[i] = 0x3f3f3f3f;
for (int i = 1; i <= m; i++) {
if (!flag[i]) {
int x = a[i].x, y = a[i].y, z = lca(x, y);
ans[a[i].id] = mx - 1;
merge(x, z, a[i].z);
merge(y, z, a[i].z);
}
}
for (int i = 1; i <= m; i++) {
if (flag[i]) {
int x = a[i].x, y = a[i].y;
if (d[x] < d[y]) x = y;
ans[a[i].id] = val[x] - 1;
}
}
for (int i = 1; i <= m; i++)
printf("%d ", ans[i] == 0x3f3f3f3f - 1 ? -1 : ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long n, ans, x, y, c, d, a[1000000];
priority_queue<long long, vector<long long>, greater<long long> > pq;
int main() {
cin >> n >> x >> y;
for (int i = 0; i < n; i++) cin >> a[i], pq.push(a[i]);
if (x > y)
cout << n;
else {
ans = 0;
while (pq.size()) {
c = pq.top();
pq.pop();
if (c <= x) {
ans++;
} else
break;
d = pq.top();
pq.pop();
pq.push(d + y);
}
cout << ans;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int inf = 1e18;
const int maxN = 2e5 + 10;
int n, q, a, b, p, c, i;
int st[2][maxN * 4], ans[maxN], tmp = 0, res = inf;
void build(int l, int r, int id)
{
if (l == r)
{
st[0][id] = ans[l] - l;
st[1][id] = ans[l] + l;
}
else
{
build(l, (l + r) / 2, 2 * id);
build((l + r) / 2 + 1, r, 2 * id + 1);
st[0][id] = min(st[0][2 * id], st[0][2 * id + 1]);
st[1][id] = min(st[1][2 * id], st[1][2 * id + 1]);
}
}
void update(int l, int r, int a, int id)
{
if (r < a || l > a)
return;
else if (l == a && r == a)
{
st[0][id] = ans[l] - l;
st[1][id] = ans[l] + l;
}
else
{
update(l, (l + r) / 2, a, 2 * id);
update((l + r) / 2 + 1, r, a, 2 * id + 1);
st[0][id] = min(st[0][2 * id], st[0][2 * id + 1]);
st[1][id] = min(st[1][2 * id], st[1][2 * id + 1]);
}
}
int query(int l, int r, int L, int R, int x, int id)
{
if (r < L || l > R)
return inf;
else if (L <= l && r <= R)
return st[x][id];
else{
int mid = (l + r) / 2;
return min(query(l, mid, L, R, x, 2 * id), query(mid + 1, r, L, R, x, 2 * id + 1));
}
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(); cout.tie();
cin >> n >> q >> a >> b;
fill(ans + 1, ans + n + 1, inf);
cin >> p;
ans[a] = abs(b - p);
ans[b] = abs(a - p);
--q;
build(1, n, 1);
while (q--)
{
cin >> c;
tmp += abs(c - p);
ans[p] = min(ans[p], min(query(1, n, 1, c - 1, 0, 1) + c, query(1, n, c, n, 1, 1) - c) - abs(c - p));
update(1, n, p, 1);
p = c;
}
for (i = 1; i <= n; i++)
res = min(res, ans[i]);
cout << res + tmp;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 5;
int to[maxn][26];
int leaf[maxn];
int deg[maxn];
int h[maxn];
int iter = 0;
int res = 0;
int term[maxn];
void add_string(string &s) {
vector<int> path;
int raz = -1;
res = 0;
int z = -1;
int v = 0;
for (int i = 0; i < s.size(); ++i) {
int cp = s[i] - 'a';
if (!to[v][cp]) {
to[v][cp] = ++iter;
h[iter] = h[v] + 1;
}
v = to[v][cp];
path.push_back(v);
if (deg[v] >= 2) raz = v;
if (leaf[v]) {
z = v;
leaf[v] = 0;
}
deg[v]++;
}
if (term[v]) {
while (!path.empty()) {
deg[path.back()]--;
path.pop_back();
}
}
term[v] = 1;
if (z == -1) {
res = h[v];
} else if (raz == -1) {
res = h[v] - h[z] + min(2, h[z]);
} else {
res = h[v] - h[z] + min(h[z], h[raz] + 2);
}
leaf[v] = 1;
}
string z;
bool delim(char c) {
return (c == ',' || c == '.' || c == '!' || c == '?' || c == '\'' ||
c == '\n' || c == '-' || c == ' ');
}
int ans = 0;
int main() {
char c;
while ((c = getchar()) != EOF) {
if (delim(c)) {
++ans;
if (z.empty()) continue;
add_string(z);
z.clear();
ans += res;
} else {
z += c;
}
}
if (!z.empty()) {
add_string(z);
ans += res;
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mn = INT_MIN;
const int mx = INT_MAX;
int a[505][505];
int kng = 0, k = 0;
int n, m;
bool vst[505][505];
void dfs(int i, int j) {
vst[i][j] = 1;
for (auto k = i - 1; k < int(i + 2); k++)
for (auto l = j - 1; l < int(j + 2); l++) {
if ((k == i || l == j) && k < n && k >= 0 && l < m && l >= 0) {
if (!vst[k][l] && a[k][l] == 1 && a[k][l] != -2) dfs(k, l);
};
};
if (kng < k) {
a[i][j] = -2;
kng++;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
memset(a, 0, sizeof(a));
memset(vst, 0, sizeof(vst));
cin >> n >> m >> k;
int x, y;
for (auto i = 0; i < int(n); i++)
for (auto j = 0; j < int(m); j++) {
char c;
cin >> c;
if (c == '.') {
a[i][j] = 1;
x = i;
y = j;
} else
a[i][j] = 0;
};
dfs(x, y);
for (auto i = 0; i < int(n); i++) {
for (auto j = 0; j < int(m); j++) {
if (a[i][j] == 1) {
cout << '.';
} else if (a[i][j] == 0) {
cout << '#';
} else
cout << 'X';
};
cout << "\n";
};
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int i = 1;
while ((i * (i + 1) / 2) <= n) {
if ((i * (i + 1) / 2) == n) {
printf("YES");
return 0;
}
i++;
}
printf("NO");
}
| 1 |
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))
ll gcd(ll a, ll b) {
if (b == 0) { return a; }
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
//a x + b y = gcd(a, b)
long long extgcd(long long a, long long b, long long &x, long long &y) {
long long g = a; x = 1; y = 0;
if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
return g;
}
long long InvMod(long long a, long long mod) {
long long x, y;
if (extgcd(a, mod, x, y) == 1) { return (x + mod) % mod; }
return 0;
}
pair<ll, ll> LinearCongruence(const vector<ll> &A, const vector<ll> &B, const vector<ll> &M) {
ll x = 0;
ll m = 1;
for (ll i = 0; i < (ll)A.size(); i++) {
ll a = A[i] * m;
ll b = B[i] - A[i] * x;
ll d = gcd(M[i], a);
if (b % d != 0) { return make_pair(0, -1); }
ll t = b / d * InvMod(a / d, M[i] / d) % (M[i] / d);
x = x + m * t;
m *= M[i] / d;
}
x %= m;
if (x < 0) { x += m; }
return make_pair(x % m, m);
}
ll solve(ll l1, ll l2, ll r1, ll r2, ll m1, ll m2) {
vector<ll> a, b, m;
if (l1 == 0 && r1 != 0) { return -1; }
if (l2 == 0 && r2 != 0) { return -1; }
if (l1 != 0) {
a.push_back(l1);
b.push_back(r1);
m.push_back(m1);
}
if (l2 != 0) {
a.push_back(l2);
b.push_back(r2);
m.push_back(m2);
}
if (a.size() == 0) { return 1; }
pair<ll, ll> nret = LinearCongruence(a, b, m);
if (nret.second > 0) {
if (nret.first == 0) { nret.first += nret.second; }
return nret.first;
}
return -1;
}
ll n, m, a, b, c, d;
int main() {
while (scanf("%lld %lld %lld %lld %lld %lld", &m, &n, &a, &b, &c, &d) > 0) {
a %= m; c %= m;
b %= n; d %= n;
ll e = 0;
if (a == 0 && b == 0) {
swap(a, c);
swap(b, d);
}
if (a == 0 && b == 0) {
puts("0");
continue;
} else if (a == 0) {
e = n / gcd(b, n);
} else if (b == 0) {
e = m / gcd(a, m);
} else {
e = lcm(m / gcd(a, m), n / gcd(b, n));
}
if (c == 0 && d == 0) {
printf("%lld\n", e - 1);
continue;
}
ll sqnm = sqrt(n * m) + EPS;
ll f = 1LL << 60;
if (e <= sqrt(n * m)) {
REP(g, e) {
ll v = solve(c, d, g * a % m, g * b % n, m, n);
if (v == -1) { continue; }
assert(v > 0);
f = min(f, v);
}
} else {
f = 1;
FOREQ(i, 1, sqnm + 5) {
ll v = solve(a, b, i * c % m, i * d % n, m, n);
if (v == -1) { continue; }
f = i;
break;
}
}
printf("%lld\n", e * f - 1);
}
} | 0 |
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<bitset>
#include<complex>
using namespace std;
template<typename __T>
inline void read(__T &x)
{
x=0;
int f=1;char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)) {x=x*10+c-'0';c=getchar();}
x*=f;
}
int box[100005],las[200005],edv[200005],cnt=0;
void adde(int u,int v)
{
las[++cnt]=box[u];
box[u]=cnt;
edv[cnt]=v;
}
int dep[100005],dfsin[100005],dfn=0;
int tot=0;
int pre[100005];
void dfs(int now,int fa)
{
pre[now]=fa;
dfsin[now]=++dfn;
for(int i=box[now];i;i=las[i])
if(edv[i]!=fa)
{
dep[edv[i]]=dep[now]+1;
dfs(edv[i],now);
}
}
int S,T;
int n;
int ans[100005];
int ans2[100005];
int deg[100005];
bool fg=0;
bool onl[100005];
void solve(int now,int fa)
{
dfsin[now]=++dfn;
if(!onl[now])
{
if(deg[now]>1) fg=1;
return;
}
if(now==T)
{
ans[tot++]=dfsin[now];
return;
}
int s=0;
for(int i=box[now];i;i=las[i])
if(edv[i]!=fa)
{
int v=edv[i];
if(!onl[v])
{
solve(v,now);
ans[tot++]=dfsin[v];
}
else
s=v;
}
ans[tot++]=dfsin[now];
solve(s,now);
}
int main()
{
read(n);
int a,b;
for(int i=1;i<n;i++)
{
read(a);
read(b);
deg[a]++;
deg[b]++;
adde(a,b);
adde(b,a);
}
dfs(1,0);
for(int i=1;i<=n;i++)
if(dep[i]>dep[S]) S=i;
dfn=0;
dfs(S,0);
for(int i=1;i<=n;i++)
if(dep[i]>dep[T]) T=i;
for(int i=T;i;i=pre[i]) onl[i]=1;
dfn=tot=0;
solve(S,0);
if(fg)
{
printf("-1\n");
return 0;
}
for(int i=0;i<n;i++)
ans2[i]=ans[i];
swap(S,T);
tot=dfn=0;
solve(S,0);
for(int i=0;i<n;i++)
{
if(ans[i]<ans2[i])
{
for(int i=0;i<n;i++)
printf("%d ",ans[i]);
printf("\n");
return 0;
}
else if(ans[i]>ans2[i])
{
for(int i=0;i<n;i++)
printf("%d ",ans2[i]);
printf("\n");
return 0;
}
}
for(int i=0;i<n;i++)
printf("%d ",ans[i]);
printf("\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m;
cin >> n >> m;
vector<long long> a(m), b(m);
vector<vector<long long>> stat(n);
for (long long i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
stat[a[i]].push_back(b[i]);
}
for (long long o = 0; o < n; o++) {
long long maxTime = 0;
for (long long oo = 0; oo < n; oo++) {
long long i = oo + o;
long long delT = 0;
if (i >= n) i %= n, delT += n;
long long minTime = -1;
for (long long j = 0; j < stat[i].size(); j++) {
long long t = stat[i][j] - i;
if (t < 0) t += n;
t += i + (stat[i].size() - 1) * n - o + delT;
if (minTime == -1 || minTime > t) minTime = t;
}
maxTime = max(maxTime, minTime);
}
cout << maxTime << " ";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3010;
struct GG {
int x, cnt;
bool operator<(const GG& obj) const { return cnt > obj.cnt; }
} p[maxn];
int vis[maxn][maxn];
long long group[maxn][maxn];
long long sum[maxn][maxn];
long long rest[maxn];
int n, m;
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i < n + 1; i++) {
int g;
long long c;
scanf("%d %lld", &g, &c);
p[g].cnt++;
group[g][0]++;
group[g][group[g][0]] = c;
}
int wrong = 0;
for (int i = 2; i < m + 1; i++) {
p[i].x = i;
sort(group[i] + 1, group[i] + 1 + group[i][0]);
for (int j = 1; j < group[i][0] + 1; j++)
sum[i][j] = sum[i][j - 1] + group[i][j];
if (group[i][0] >= group[1][0]) wrong = max(wrong, (int)group[i][0]);
}
if (!wrong) {
printf("0\n");
return 0;
}
sort(p + 2, p + m + 1);
long long ans = 1e18;
for (int i = group[1][0] + 1; i < wrong + 2; i++) {
int cnt = group[1][0];
long long s = 0;
for (int j = 2; j < n + 1; j++) {
if (p[j].cnt >= i) {
int num = (p[j].cnt - (i - 1));
s += sum[p[j].x][num];
cnt += num;
for (int k = 1; k < num + 1; k++) vis[p[j].x][k] = i;
}
}
if (cnt > i) continue;
int num = 0;
for (int j = 2; j < m + 1; j++) {
for (int k = 1; k < group[j][0] + 1; k++)
if (vis[j][k] != i) rest[num++] = group[j][k];
}
sort(rest, rest + num);
for (int j = 0; j < i - cnt; j++) s += rest[j];
ans = min(ans, s);
}
printf("%lld\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int N;
cin >> N;
int num;
vector<int> P;
for(int i=0;i<N;i++){
cin >> num;
P.push_back(num-1);
}
vector<int> Q(N);
for(int i=0;i<N;i++){
Q[P[i]] = i;
}
multiset<int> M = {-1,-1,N,N};
long long result = 0;
for(int i=N-1;i>=0;i--){
int now = Q[i];
auto L = M.upper_bound(now);
int x = *(L);
++L;
int y = *(L);
L = prev(L,2);
int b = *(L);
L--;
int a = *(L);
result += (i+1)*(1LL*(b-a)*(x-now)+1LL*(now-b)*(y-x));
M.emplace(now);
}
cout << result << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
const long long INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
long long pow_mod(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
template <typename T>
void read(T &res) {
bool flag = false;
char ch;
while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
for (res = ch - 48; isdigit(ch = getchar());
res = (res << 1) + (res << 3) + ch - 48)
;
flag && (res = -res);
}
const double pi = 3.1415926535897932384626;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const long long mod = 1e9 + 7;
const long long N = 2e5 + 1000;
long long UUU;
long long n, m, k, x[N], y[N], dep[N], fa[N], b[N];
;
long long vis[N];
vector<long long> G[N];
void dfs(long long f, long long u, long long cha) {
dep[u] = cha;
++UUU;
vis[u] = 1;
++UUU;
if (UUU < -5) {
return;
} else {
for (auto vec : G[u]) {
if (vis[vec] || vec == f) continue;
++UUU;
fa[vec] = u;
++UUU;
dfs(u, vec, cha + 1);
++UUU;
}
}
}
long long vis2[N];
queue<long long> qe;
vector<long long> vec[2];
void SSFFFFFF() {
if (UUU < -6) {
cout << "0\n";
return;
}
memset(vis2, -1, sizeof(vis2));
++UUU;
qe.push(1);
++UUU;
vis2[1] = 0;
++UUU;
while (!qe.empty()) {
long long u = qe.front();
++UUU;
qe.pop();
++UUU;
if (UUU < -5) {
return;
}
for (auto vec : G[u]) {
if (UUU < -5) {
return;
}
if (vis2[vec] != -1) continue;
++UUU;
qe.push(vec), vis2[vec] = 1 - vis2[u];
++UUU;
}
}
}
vector<long long> ans;
void solve() {
long long u, v;
++UUU;
if (UUU < -5) {
return;
}
read(n), read(m), read(k);
++UUU;
if (UUU < -5) {
return;
}
for (long long i = 1; i <= m; i++) {
read(u), read(v);
++UUU;
G[u].push_back(v), G[v].push_back(u);
++UUU;
x[i] = u, y[i] = v;
++UUU;
}
++UUU;
dfs(1, 1, 1);
++UUU;
for (long long i = 1; i <= m; i++)
if (dep[x[i]] > dep[y[i]]) swap(x[i], y[i]);
if (UUU < -5) {
return;
}
if (m + 1 == n) {
long long cnt = 0;
++UUU;
if (UUU < -5) {
return;
}
SSFFFFFF();
++UUU;
for (long long i = 1; i <= n; i++) vec[vis2[i]].push_back(i);
++UUU;
cout << 1 << '\n';
++UUU;
if (vec[0].size() >= vec[1].size())
for (auto x : vec[0]) {
++UUU;
cout << x << ' ';
cnt++;
++UUU;
if (cnt == (k + 1) / 2) break;
}
else
for (auto x : vec[1]) {
cout << x << ' ';
++UUU;
cnt++;
++UUU;
if (cnt == (k + 1) / 2) break;
}
if (UUU < -5) {
return;
}
cout << '\n';
} else {
++UUU;
for (long long i = 1; i <= m; i++) {
++UUU;
if (UUU < -5) {
return;
}
long long cha = dep[y[i]] - dep[x[i]];
if (cha >= 2 && cha <= k - 1) {
++UUU;
ans.push_back(y[i]);
while (1) {
if (y[i] == x[i]) break;
y[i] = fa[y[i]];
ans.push_back(y[i]);
}
cout << "2\n";
++UUU;
cout << ans.size() << '\n';
++UUU;
for (auto it : ans) cout << it << ' ';
++UUU;
cout << '\n';
return;
}
}
if (UUU < -5) {
return;
}
for (long long i = 1; i <= m; i++) {
++UUU;
long long cha = dep[y[i]] - dep[x[i]];
++UUU;
if (UUU < -5) {
return;
}
if (cha >= k) {
++UUU;
long long cnt = 0;
ans.push_back(y[i]);
while (1) {
if (y[i] == x[i]) break;
++UUU;
y[i] = fa[y[i]];
ans.push_back(y[i]);
}
cout << "1\n";
if (UUU < -5) {
cout << "1\n";
}
for (auto it : ans) {
++UUU;
long long cha = dep[y[i]] - dep[it];
++UUU;
if (cha % 2 == 0) cout << it << ' ', cnt++;
++UUU;
if (cnt == (k + 1) / 2) break;
}
cout << '\n';
return;
}
}
if (UUU < -5) {
return;
}
}
}
void dd() {
solve();
++UUU;
if (UUU < -5) {
return;
}
}
signed main() {
++UUU;
dd();
if (UUU == -5) {
cout << "-1\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int a[15000200], t[15000200], b[15000200];
char c[6000];
int n, k, lst, dp;
void build(int p, int v) {
t[v]++;
if (p == n) return;
if (c[p] == 'a') {
if (a[v] == 0) {
a[v] = lst;
lst++;
}
build(p + 1, a[v]);
} else {
if (b[v] == 0) {
b[v] = lst;
lst++;
}
build(p + 1, b[v]);
}
}
bool ch(int p) {
int i;
for (i = 0; i * 2 <= p; i += 2)
if (c[i] != c[p - i]) return false;
return true;
}
void deep(int p, int v) {
if (k <= 0) return;
if (ch(p) && k <= t[v]) {
k = 0;
dp = p;
return;
}
if (ch(p)) k -= t[v];
if (k > 0 && a[v] != 0) {
c[p + 1] = 'a';
deep(p + 1, a[v]);
}
if (k > 0 && b[v] != 0) {
c[p + 1] = 'b';
deep(p + 1, b[v]);
}
}
void solve() {
int i, j, z, x = 2, q, w;
scanf("%d", &k);
n = strlen(c);
for (i = 0; i < 15000000; i++) {
a[i] = 0;
b[i] = 0;
t[i] = 0;
}
lst = 2;
for (i = 0; i < n; i++) build(i, 1);
t[1] = 0;
c[0] = 'a';
deep(0, a[1]);
if (k > 0) {
c[0] = 'b';
deep(0, b[1]);
}
for (i = 0; i <= dp; i++) printf("%c", c[i]);
printf("\n");
gets(c);
return;
}
int main() {
#pragma comment(linker, "/STACK:1073741824")
int n, m, k, i;
while (gets(c)) solve();
return 0;
}
| 5 |
#include <stdio.h>
int main(){
int arr[26]={0}, i;
int c;
while ((c=getc(stdin))!=EOF){
if (c>='A' && c<='Z') arr[c-65]++;
else if (c>='a' && c<='z') arr[c-97]++;
}
for (i=0; i<26; i++) printf("%c : %d\n",i+97 , arr[i]);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, S, ans;
int a[3], b[3], s[3];
vector<long long> cc;
map<int, int> sp, np;
inline void trans_(map<int, int>::iterator x, long long sum) {
if (sum > n) return;
if (x == sp.end()) return ++ans, void();
long long now = 1;
auto it = x;
it++;
for (int i = 0; i <= x->second; ++i) {
trans_(it, sum * now);
now *= x->first;
}
}
int main() {
int Test_;
scanf("%d", &Test_);
while (Test_--) {
ans = 0;
n = m = 1, S = 2;
sp.clear(), np.clear(), cc.clear();
scanf("%d%d%d%d%d%d%d%d%d", a, a + 1, a + 2, b, b + 1, b + 2, s, s + 1,
s + 2);
n = n * a[0] * a[1] * a[2], m = m * b[0] * b[1] * b[2],
S = S * s[0] * s[1] * s[2];
sp[2] = 1;
for (int i = 2; i <= 1000; ++i) {
for (int j = 0; j <= 2; ++j)
while (a[j] % i == 0) a[j] /= i, ++np[i];
for (int j = 0; j <= 2; ++j)
while (s[j] % i == 0) s[j] /= i, ++sp[i];
}
for (int i = 0; i <= 2; ++i)
if (a[i] > 1) ++np[a[i]];
for (int i = 0; i <= 2; ++i)
if (s[i] > 1) ++sp[s[i]];
trans_(sp.begin(), 1);
for (auto i = np.begin(); i != np.end(); i++)
if (sp[i->first] < i->second) {
long long x = 1;
for (int j = sp[i->first] + 1; j >= 1; --j) x = x * i->first;
cc.push_back(x);
}
int size = cc.size();
for (int i = (1 << size) - 1; i >= 0; --i) {
long long x = m;
int opt = 1;
for (int j = 1; j <= size; ++j)
if (i >> j - 1 & 1) opt *= -1, x /= cc[j - 1];
ans += opt * x;
}
printf("%lld\n", ans);
}
}
| 3 |
// #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define fastIO cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c) for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
//#define foreach_it(type,it,c) for(type::iterator it=c.begin(); it!=c.end(); ++it)
// fast?
#define foreach_it(type,it,c) for(type::iterator it=c.begin(), c_end=c.end(); it!=c_end; ++it)
int Yen(int x, int y, int h, int w){
int ret = 0;
int inSize = x + y + h;
int inWeight = w;
// C++11
vector<pair<int, int> > size_weight_List
{MP(60,2),MP(80,5),MP(100,10),MP(120,15),MP(140,20),MP(160,25)};
VI feeList{ 600, 800, 1000, 1200, 1400, 1600 };
FOR(i, 0, size_weight_List.size()){
if (inSize <= size_weight_List[i].first && inWeight <= size_weight_List[i].second){
ret = feeList[i];
break;
}
}
return ret;
}
int main(){
int n;
int x, y, h, w;
long long ans = 0;
fastIO;
while (cin >> n,n){
while (n--){
cin >> x >> y >> h >> w;
ans += Yen(x, y, h, w);
}
cout << ans << endl;
ans = 0;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a;
cin>>a;
for (int i=0;i<a.size();i++)
cout<<"x";
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int i, n, m;
scanf("%d%d", &n, &m);
if (m == 3 && n >= 5)
printf("-1\n");
else
for (i = 0; i < n; ++i)
printf("%d %d\n", i, (10000000 + i * i) * (i >= m ? -1 : 1));
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2,popcnt,fma")
#pragma GCC optimize("fast-math")
using namespace std;
const long long mod = 1e9 + 7, mod2 = 998244353;
const long double eps = 1e-6;
const long long inf = 1e18;
const int base = 1e9;
const long double PI = 3.14159265358979;
long long a[1000 * 1000], n, r1, r2, r3, d;
long long dp[1000 * 1000][2];
unsigned int bitcount(unsigned long long val) {
return __builtin_popcount(val) + __builtin_popcount(val >> 32);
}
void local() { freopen("test.in", "r", stdin); }
void remote() {}
void read() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> r1 >> r2 >> r3 >> d;
for (int i = 0; i < n; i++) cin >> a[i];
}
int main() {
remote();
read();
dp[0][0] = a[0] * r1 + r3;
dp[0][1] = min(a[0] * r1 + r1, r2);
for (int i = 1; i < n; i++) {
long long prev = min(dp[i - 1][0] + d, 3 * d + dp[i - 1][1] + r1);
dp[i][1] = prev + min(a[i] * r1 + r1, r2);
dp[i][0] =
min(a[i] * r1 + r3 + prev, min(dp[i - 1][0], dp[i - 1][1] + r1) +
3 * d + min(r2, a[i] * r1 + r1) + r1);
}
cout << min(dp[n - 1][0], n > 1 ? min(dp[n - 2][0], dp[n - 2][1] + r1) +
2 * d + a[n - 1] * r1 + r3
: INT64_MAX)
<< '\n';
;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int cl = 0, ha = 0, op = 0;
string s, t;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(') {
op++;
}
if (s[i] == ')') {
cl++;
}
if (s[i] == '#') {
ha++;
}
}
int req = op - cl;
int temp2 = req;
int temp = ha;
if (op == cl || op == 0 || cl > op || ha > op || ha > req) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < s.length(); i++) {
if (s[i] == '#' && ha > 1) {
t.push_back(')');
ha--;
temp2--;
} else if (s[i] == '#' && ha == 1) {
for (int j = 1; j <= temp2; j++) {
t.push_back(')');
}
ha = 0;
} else {
t.push_back(s[i]);
}
}
int x = 0;
for (int i = 0; i < t.length(); i++) {
if (t[i] == '(') {
x++;
}
if (t[i] == ')') {
x--;
}
if (x < 0) {
cout << -1 << endl;
return 0;
}
}
if (x == 0) {
for (int i = 1; i < temp; i++) {
cout << 1 << endl;
req--;
}
cout << req << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int h,w,n;
char initField[51][51];
char field[51][51];
int cnt=0;
bool del[51][51];
bool search(){
int sum=0;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)if(field[i][j]!='.')sum++;
while(sum){
// º
for(int j=0;j<w;j++){
for(int i=h-2;i>=0;i--){
if(field[i][j]!='.'){
char ch=field[i][j];
field[i][j]='.';
for(int k=i;k<h;k++){
if(k+1==h||field[k+1][j]!='.'){
field[k][j]=ch;
break;
}
}
}
}
}
memset(del,0,sizeof(del));
int dec=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(field[i][j]=='.')continue;
// tate
int cnt=0;
for(int k=i;k<h;k++){
if(field[i][j]==field[k][j])cnt++;
else break;
}
if(cnt>=n){
for(int k=i;k<h;k++){
if(field[i][j]==field[k][j])del[k][j]=true;
else break;
}
}
// yoko
cnt=0;
for(int k=j;k<w;k++){
if(field[i][j]==field[i][k])cnt++;
else break;
}
if(cnt>=n){
for(int k=j;k<w;k++){
if(field[i][j]==field[i][k])del[i][k]=true;
else break;
}
}
}
}
for(int i=0;i<h;i++)for(int j=0;j<w;j++)if(del[i][j])field[i][j]='.',dec++;
if(dec==0)return false;
sum-=dec;
}
return true;
}
int main(){
cin>>h>>w>>n;
bool ok=false;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)cin>>initField[i][j];
for(int i=0;i<h;i++){
for(int j=0;j<w-1;j++){
for(int k=0;k<h;k++)for(int l=0;l<w;l++)field[k][l]=initField[k][l];
cnt++;
swap(field[i][j],field[i][j+1]);
ok|=search();
if(ok)break;
}
if(ok)break;
}
if(ok)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int knight[100000];
int is_possible(int t, int n) {
vector<int> r(t, 1);
for (int i = 0; i < n; i++) r[i % t] &= knight[i];
for (int i = 0; i < r.size(); i++)
if (r[i]) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> knight[i];
for (int i = 1; i * 3 <= n; i++) {
if (n % i == 0 and is_possible(i, n)) {
cout << "YES" << endl;
return 0;
}
}
cout << "NO" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int H, W, K; cin >> H >> W >> K;
vector<string> table(H);
int ans = 0;
for (int i=0; i<H; i++){
cin >> table.at(i);
}
for (int i=0; i < (1<<H); i++){
for (int j=0; j < (1<<W); j++){
int cnt =0;
for (int a=0; a<H; a++){
for (int b=0; b<W; b++){
if ((i >> a) & 1 && (j >> b) &1 && table.at(a).at(b) == '#'){
cnt ++;
}
}
}
if(cnt==K){ans++;}
}
}
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int sumPos = 0, sumNeg = 0;
while (n > 0) {
int temp;
cin >> temp;
if (temp > 0)
sumPos += temp;
else
sumNeg += temp;
n--;
}
cout << sumPos - sumNeg;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod())
v = static_cast<Type>(x);
else
v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const {
return static_cast<U>(value);
}
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) {
if ((value += other.value) >= mod()) value -= mod();
return *this;
}
Modular& operator-=(const Modular& other) {
if ((value -= other.value) < 0) value += mod();
return *this;
}
template <typename U>
Modular& operator+=(const U& other) {
return *this += Modular(other);
}
template <typename U>
Modular& operator-=(const U& other) {
return *this -= Modular(other);
}
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}
Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(static_cast<int64_t>(value) *
static_cast<int64_t>(rhs.value));
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value,
Modular>::type&
operator*=(const Modular& rhs) {
int64_t q = static_cast<int64_t>(static_cast<long double>(value) *
rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) {
return *this *= Modular(inverse(other.value, mod()));
}
template <typename U>
friend const Modular<U>& abs(const Modular<U>& v) {
return v;
}
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend std::istream& operator>>(std::istream& stream, Modular<U>& number);
private:
Type value;
};
template <typename T>
bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T>& lhs, U rhs) {
return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T>& lhs, U rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
return stream << number();
}
template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, int64_t>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
constexpr int md = 998244353;
using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
vector<Mint> fact(1, 1);
vector<Mint> inv_fact(1, 1);
Mint C(int n, int k) {
if (k < 0 || k > n) {
return 0;
}
while ((int)fact.size() < n + 1) {
fact.push_back(fact.back() * (int)fact.size());
inv_fact.push_back(1 / fact.back());
}
return fact[n] * inv_fact[k] * inv_fact[n - k];
}
int A[200005];
Mint a[200005], b[200005], p[200005];
void solve() {
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> A[i];
a[i] = A[i];
a[i] = a[i] / 100;
}
p[0] = 1;
for (int i = 1; i <= n; i++) p[i] = p[i - 1] * a[i];
b[0] = 1;
Mint tmp = 1;
for (int i = 1; i <= n; i++) {
tmp *= a[i];
b[i] = b[i - 1] + tmp;
}
set<int> s;
s.insert(1);
s.insert(n + 1);
Mint ans;
ans = b[n - 1] / p[n];
while (q--) {
int m;
cin >> m;
if (!s.count(m)) {
s.insert(m);
auto it = s.find(m);
int l = *(prev(it));
int r = *(next(it));
if (l == 1) {
ans -= (b[r - 2]) / p[r - 1];
} else {
ans -= (b[r - 2] - b[l - 2]) / p[r - 1];
}
ans += (b[r - 2] - b[m - 2]) / p[r - 1];
ans += (b[m - 2] - b[l - 2]) / p[m - 1];
} else {
auto it = s.find(m);
int l = *(prev(it));
int r = *(next(it));
if (l == 1) {
ans += b[r - 2] / p[r - 1];
} else {
ans += (b[r - 2] - b[l - 2]) / p[r - 1];
}
ans -= (b[r - 2] - b[m - 2]) / p[r - 1];
ans -= (b[m - 2] - b[l - 2]) / p[m - 1];
s.erase(it);
}
cout << ans << endl;
}
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;
const int vmax=110;
const int inf=1023456789;
int N,M;
int S,T;
struct node{int to,cap,rev;};
vector<node> graph[vmax];
bool used[vmax];
int dfs(int v,int t,int f){
if(v==t) return f;
used[v]=true;
for(auto &e:graph[v]){
if(!used[e.to] && e.cap > 0){
int d= dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap-=d;
graph[e.to][e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow=0;
for(;;){
rep(i,vmax) used[i]=false;
int f=dfs(s,t,inf);
if(f==0) break;
flow+=f;
}
return flow;
}
int main(void){
cin >> N >> M;
rep(i,M){
int a,b,c;
cin >> a >> b >> c;
graph[a].push_back({b,c,(int)graph[b].size()});
graph[b].push_back({a,0,(int)graph[a].size()-1});
}
cout << max_flow(0,N-1) << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void run();
int main() {
ios::sync_with_stdio(0);
run();
}
void run() {
int v[2];
cin >> v[0] >> v[1];
int best = 0;
for (int i = 2; i--;) {
int w[2] = {v[0], v[1]};
swap(v[0], v[1]);
int got = -1;
for (int j = 0, k = 0, t = 0; w[0] + w[1]; j++, t = !t) {
if (w[k ^ t])
got += !t, k ^= t, --w[k];
else
--w[k], got += t;
}
best = max(best, got);
}
cout << best << " " << v[0] + v[1] - best - 1 << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b,c;
cin>>a>>b>>c;
int ans=(a-b)%c;
cout<<ans<<"\n";
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin>>n,n){
int a[10000];
double s=0;
for(int i=0;i<n;i++)cin>>a[i],s+=a[i];
int ans=0;
for(int i=0;i<n;i++)ans+=s/n>=a[i];
cout<<ans<<endl;
}
return 0;
}
| 0 |
#include <iostream>
using namespace std;
template <class T>
void extgcd(T a, T b, T &x, T &y) {
T r0 = a, r1 = b, r2;
T a0 = 1, a1 = 0, a2;
T b0 = 0, b1 = 1, b2;
while (r1 > 0) {
T q1 = r0 / r1;
T r2 = r0 % r1;
T a2 = a0 - q1 * a1;
T b2 = b0 - q1 * b1;
r0 = r1, r1 = r2;
a0 = a1, a1 = a2;
b0 = b1, b1 = b2;
}
x = a0, y = b0;
}
int main() {
int p, q, x, y;
cin >> p >> q;
extgcd(p, q, x, y);
cout << x << ' ' << y << endl;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); i++)
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
int r = 0, ans = 0;
rep(i,n) if (s[i] == 'R') r++;
rep(i,r) if (s[i] == 'W') ans++;
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
std::cin.tie();
std::ios::sync_with_stdio(false);
int n;
cin >> n;
int ch[n + 1];
memset(ch, 0, sizeof ch);
int a[n];
bool done[n + 1];
memset(done, 0, sizeof ch);
for (int i = 0; i < n; i++) {
cin >> a[i];
ch[a[i]]++;
;
}
queue<int> q;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!ch[i])
q.push(i);
else
ans += ch[i] - 1;
}
cout << ans << endl;
for (int i = 0; i < n; i++) {
if (q.empty()) break;
if (done[a[i]]) {
a[i] = q.front();
q.pop();
continue;
}
if (ch[a[i]] == 1) continue;
if (a[i] < q.front())
done[a[i]] = 1;
else {
ch[a[i]]--;
a[i] = q.front();
q.pop();
}
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
| 4 |
#include <stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>=c)printf("0\n");
else if(a+b<c)printf("NA\n");
else printf("%d\n",c-a);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, c;
const long long MOD = 998244353ll;
const int MAXN = 5005;
long long fans = 0;
long long cx[MAXN][MAXN];
long long ax[MAXN];
long long solve(long long x, long long y) {
long long ans = 0;
long long mn = min(x, y);
long long mx = max(x, y);
ans += 1;
ax[0] = 1;
for (long long i = 1; i <= mx; i++) ax[i] = (ax[i - 1] * (mx - i + 1)) % MOD;
for (long long i = 1; i <= mn; i++) ans = (ans + cx[mn][i] * ax[i]) % MOD;
return ans;
}
int main() {
cx[1][0] = cx[1][1] = 1;
for (int i = 2; i < MAXN; i++) {
cx[i][0] = 1;
for (int j = 1; j < MAXN; j++)
cx[i][j] = (cx[i - 1][j] + cx[i - 1][j - 1]) % MOD;
}
cin >> a >> b >> c;
long long aa, bb, cc;
aa = solve(a, b) % MOD;
bb = solve(c, b) % MOD;
cc = solve(c, a) % MOD;
fans = (aa * bb) % MOD;
fans = (fans * cc) % MOD;
cout << fans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, k, Mod, des, A[N], M[N], Pw[N], Inv[N], H[N], sz[N], In[N], Out[N],
up[N], dn[N];
long long tot;
vector<int> Adj[N];
map<int, int> U, C, T;
inline int Power(int a, int b) {
int ret = 1;
for (; b; b >>= 1, a = 1ll * a * a % Mod)
if (b & 1) ret = 1ll * ret * a % Mod;
return (ret);
}
void PREDFS(int v, int p = -1) {
sz[v] = 1;
for (int &u : Adj[v])
if (!M[u] && u != p) PREDFS(u, v), sz[v] += sz[u];
}
int GetCentroid(int v, int p = -1, int _n = -1) {
if (p == -1) PREDFS(v), _n = sz[v];
for (int &u : Adj[v])
if (!M[u] && u != p && sz[u] * 2 > _n) return (GetCentroid(u, v, _n));
return (v);
}
void DFS(int v, int p) {
H[v] = H[p] + 1;
dn[v] = (1ll * dn[p] * k + A[v]) % Mod;
up[v] = (1ll * Pw[H[p]] * A[v] + up[p]) % Mod;
U[up[v]]++;
for (int &u : Adj[v])
if (!M[u] && u != p) DFS(u, v);
}
void Add(int v, int p, int w) {
U[up[v]] += w;
for (int &u : Adj[v])
if (!M[u] && u != p) Add(u, v, w);
}
void Finalize(int v, int p) {
int trg = 1ll * (des - dn[v] + Mod) * Inv[H[v] + 1] % Mod;
In[v] += U[trg];
C[trg]++;
T[trg]--;
for (int &u : Adj[v])
if (!M[u] && u != p) Finalize(u, v);
}
void Absorb(int v, int p, map<int, int> &Mp) {
Out[v] += Mp[up[v]];
for (int &u : Adj[v])
if (!M[u] && u != p) Absorb(u, v, Mp);
}
void Solve(int v) {
H[v] = up[v] = 0;
dn[v] = A[v];
for (int &u : Adj[v])
if (!M[u]) DFS(u, v);
int trg = 1ll * (des - dn[v] + Mod) * Inv[H[v] + 1] % Mod;
In[v] += U[trg];
U[up[v]]++;
for (int &u : Adj[v])
if (!M[u]) {
Add(u, v, -1);
Finalize(u, v);
Absorb(u, v, T);
Add(u, v, 1);
T.clear();
}
Out[v] += C[0];
C[trg]++;
for (int &u : Adj[v])
if (!M[u]) Absorb(u, v, C);
C.clear();
U.clear();
}
void Decompose(int v) {
v = GetCentroid(v);
M[v] = 1;
Solve(v);
for (int &u : Adj[v])
if (!M[u]) Decompose(u);
}
int main() {
scanf("%d%d%d%d", &n, &Mod, &k, &des);
for (int i = 1; i <= n; i++) scanf("%d", &A[i]);
for (int i = 1, v, u; i < n; i++)
scanf("%d%d", &v, &u), Adj[u].push_back(v), Adj[v].push_back(u);
for (int i = Pw[0] = Inv[0] = 1; i < N; i++)
Pw[i] = 1ll * Pw[i - 1] * k % Mod, Inv[i] = Power(Pw[i], Mod - 2);
Decompose(1);
for (int i = 1; i <= n; i++) {
if (A[i] == des) In[i]++, Out[i]++;
tot -= 2ll * Out[i] * (n - Out[i]) + 2ll * In[i] * (n - In[i]) +
1ll * In[i] * (n - Out[i]) + 1ll * Out[i] * (n - In[i]);
}
tot >>= 1;
tot += 1ll * n * n * n;
return !printf("%lld", tot);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
string l = "312931303130313130313031";
string input = "";
string s = "312831303130313130313031";
string s1 = s + s + s;
string s2 = l + s + s;
string s3 = s + l + s;
string s4 = s + s + l;
long long int i;
for (i = 0; i < n; i++) {
string ch;
cin >> ch;
input += ch;
}
long long int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;
for (i = 0; i < s1.length() - 2 * n; i++) {
if (input.compare(s1.substr(i, n * 2)) == 0) {
flag1 = 1;
}
}
for (i = 0; i < s2.length() - 2 * n; i++) {
if (input.compare(s2.substr(i, 2 * n)) == 0) {
flag2 = 1;
}
}
for (i = 0; i < s3.length() - 2 * n; i++) {
if (input.compare(s3.substr(i, 2 * n)) == 0) {
flag3 = 1;
}
}
for (i = 0; i < s4.length() - 2 * n; i++) {
if (input.compare(s4.substr(i, 2 * n)) == 0) {
flag4 = 1;
}
}
if (flag1 || flag2 || flag3 || flag4) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
| 2 |
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int A,B;
cin>>A>>B;
if (A*B%2) printf("Yes");
else printf("No");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
struct point {
long long int x, y;
};
vector<long long int> arr(25);
bool same4(long long int i) {
if (arr[i] == arr[i + 1] && arr[i] == arr[i + 2] && arr[i] == arr[i + 3]) {
return true;
}
return false;
}
vector<long long int> get_arr(vector<long long int> v1) {
vector<long long int> v2(v1);
long long int n = v1.size();
for (long long int i = 0; i < n; i++) {
v2[i] = arr[v1[i]];
}
return v2;
}
vector<long long int> shift2(vector<long long int> v1) {
vector<long long int> v2(v1);
long long int n = v1.size();
for (long long int i = 0; i < n; i++) {
v1[i] = v2[(i + 2) % n];
}
return v1;
}
bool comp_side(vector<long long int> v1, vector<long long int> v2) {
if (get_arr(shift2(v1)) == get_arr(v2) ||
get_arr(shift2(v2)) == get_arr(v1)) {
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (long long int i = 1; i < 25; i++) {
cin >> arr[i];
}
bool flag = false;
if (same4(13) && same4(17)) {
if (comp_side({1, 3, 5, 7, 9, 11, 24, 22}, {2, 4, 6, 8, 10, 12, 23, 21})) {
flag = true;
}
} else if (same4(1) && same4(9)) {
if (comp_side({13, 14, 5, 6, 17, 18, 21, 22},
{15, 16, 7, 8, 19, 20, 23, 24})) {
flag = true;
}
} else if (same4(5) && same4(21)) {
if (comp_side({3, 4, 17, 19, 10, 9, 14, 16},
{1, 2, 18, 20, 12, 11, 15, 13})) {
flag = true;
}
}
if (flag) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) { return a > b; }
int main() {
int a[100005];
int n, n1, n2, i, j, t;
long long sum1 = 0, sum2 = 0;
scanf("%d%d%d", &n, &n1, &n2);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n, cmp);
if (n1 > n2) {
t = n1;
n1 = n2;
n2 = t;
}
for (i = 0, j = 0; j < n1; i++, j++) {
sum1 += a[i];
}
for (j = 0; j < n2; i++, j++) {
sum2 += a[i];
}
printf("%.8f\n", 1.0 * sum1 / n1 + 1.0 * sum2 / n2);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
for (int l = 0; l < q; ++l) {
int n;
cin >> n;
string a[n];
int ot;
int i, j, kln = 0, norm = 0, kls = 0;
for (i = 0; i < n; ++i) {
int kl1 = 0, kl0 = 0;
cin >> a[i];
if (a[i].size() % 2 != 0) kls++;
for (j = 0; j < a[i].size(); ++j) {
if (a[i][j] == '1')
kl1++;
else
kl0++;
}
if (kl1 % 2 != 0 && kl0 % 2 != 0)
kln++;
else
norm++;
}
if (kln % 2 == 1 && kls == 0) kln--;
cout << kln + norm << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100020;
int val[MAXN];
long long dp[MAXN];
int main() {
int N, upper = 0;
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%d", &val[i]);
while ((1 << (1 + upper)) <= N) ++upper;
for (int i = 1; i < N; i++) {
int pre = i;
while (pre < N) {
for (int j = upper; j >= 0; j--)
if (pre + (1 << j) <= N) {
dp[pre] += val[i];
pre += (1 << j);
break;
}
}
}
for (int i = 1; i < N; i++) {
dp[i] += dp[i - 1];
cout << dp[i] << endl;
}
return 0;
}
| 1 |
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=2e5+10;
int d[maxn],f[maxn];
int main(){
int n,a,b;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b;
f[i]=a+b;
d[i]=a-b;
}
sort(f+1,f+1+n);
sort(d+1,d+1+n);
ll x=max(f[n]-f[1],d[n]-d[1]);
cout<<x<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int vis[209];
int q[4];
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < m; i++) {
int q;
cin >> q;
vis[q] = 1;
}
for (int i = 1; i <= 300; i++) {
int flag = 0;
for (int j = 1; j <= n; j++) {
if (!vis[j] && (vis[j + 1] == i || vis[j - 1] == i)) {
vis[j] = i + 1;
flag = 1;
}
}
if (!flag) {
cout << i << endl;
break;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline void srand() {
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& v) {
os << "(" << v.first << "," << v.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (const T& it : v) os << it << " ";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& v) {
os << "{ ";
for (const T& it : v) os << it << " ";
os << "}";
return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& v) {
os << "{ ";
for (const pair<T, U>& it : v)
os << "{" << it.first << "," << it.second << "} ";
os << "}";
return os;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const long long INF = 0x3f3f3f3f;
const long long INFLL = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 1e9 + 7;
const long double EPS = 1e-9;
struct Princess {
int a, b, c;
Princess() {}
Princess(int a, int b, int c) : a(a), b(b), c(c) {}
bool operator<(const Princess& other) const { return c < other.c; }
bool operator>(const Princess& other) const { return c > other.c; }
};
namespace DSU {
vector<int> root;
vector<bool> dead;
void init(int n) {
root.resize(n, -1);
dead.resize(n);
}
int find(int i) {
if (root[i] == -1) return i;
return root[i] = find(root[i]);
}
bool link(int i, int j) {
int a = find(i);
int b = find(j);
if (a == b) {
if (dead[a]) return false;
dead[a] = true;
return true;
}
root[a] = b;
if (dead[a] && dead[b]) return false;
if (dead[a]) dead[b] = true;
return true;
}
} // namespace DSU
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand();
int n, m;
cin >> n >> m;
vector<Princess> gods;
int a, b, c;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
gods.push_back(Princess(a, b, c));
}
sort(gods.begin(), gods.end(), greater<Princess>());
DSU::init(n + 1);
int ans = 0;
for (const Princess& it : gods) {
if (DSU::link(it.a, it.b)) ans += it.c;
}
cout << ans << '\n';
return 0;
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a,b;
cin >> n >> a >> b;
if(n==a+b||a==b+n||b==a+n){
cout << "Yes";
}
else{
cout << "No";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[150] = {0};
int main() {
int i;
string str;
cin >> str;
for (i = 0; i < str.length(); i++) arr[(int)str[i]]++;
vector<int> v;
v.push_back(arr[(int)'B']);
v.push_back(arr[(int)'l']);
v.push_back(arr[(int)'b']);
v.push_back(arr[(int)'s']);
v.push_back(arr[(int)'a'] / 2);
v.push_back(arr[(int)'u'] / 2);
v.push_back(arr[(int)'r']);
cout << *min_element(v.begin(), v.end());
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
unordered_map<long long, long long> cnt;
int n, p, k;
long long qpow(long long a, long long b, long long c) {
long long ret = 1;
for (; b; b >>= 1, (a *= a) %= c)
if (b & 1) (ret *= a) %= p;
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> p >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
long long x = (qpow(a[i], 4, p) + p - (long long)k * a[i] % p) % p;
cnt[x]++;
}
long long res = 0;
for (auto w : cnt) res += (long long)w.second * (w.second - 1) / 2;
cout << res << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct debugger {
template <typename T>
debugger& operator,(const T v) {
cerr << v << " ";
return *this;
}
} dbg;
const double pi = acos(-1.0);
const double eps = 1e-7;
template <class TT>
TT sqr(TT a) {
return a * a;
}
template <class TT>
TT abs(TT a) {
if (a < 0) return -a;
return a;
}
template <class ZZ>
ZZ max(ZZ a, ZZ b, ZZ c) {
return max(a, max(b, c));
}
template <class ZZ>
ZZ min(ZZ a, ZZ b, ZZ c) {
return min(a, min(b, c));
}
const long long S = 100010;
const long long Mod = 1000000007;
bool flag;
vector<long long> co, ord;
long long dp[S];
long long v[S];
long long les[S];
long long gre[S];
long long temp;
long long t;
int col[S], n, q;
void add(long long& a, long long b) {
a += b;
if (a >= Mod) a -= Mod;
}
void dfs(int u) {
col[u] = 1;
if (gre[u]) {
if (col[gre[u]] == 1) flag = true;
dfs(gre[u]);
}
ord.push_back(u);
col[u] = 2;
}
void go(int u) {
col[u] = 1;
if (gre[u]) {
if (col[gre[u]] == 1)
flag = true;
else
go(gre[u]);
}
ord.push_back(u);
col[u] = 2;
}
long long CalAns() {
temp = 0;
for (int i = 1; i <= n; i++)
if (col[i] == 0) {
go(i);
if (flag) return 0;
}
memset(col, 0, sizeof(col));
co.clear();
int cnt = 0, i, j, k;
long long cur;
for (i = 1; i <= n; i++)
if (col[i] == 0 && les[i] == 0) {
cnt++;
ord.clear();
dfs(i);
if (flag) return 0;
if (((int)ord.size()) == 1) {
co.push_back(v[i]);
continue;
}
cur = 0;
for (j = 0; j < ((int)ord.size()) - 1; j++) {
cur += v[ord[j]] * (((int)ord.size()) - 1 - j);
if (cur > t) return 0;
}
temp += cur;
if (temp > t) return 0;
cur = 0;
for (j = 0; j < ((int)ord.size()); j++) {
cur += v[ord[j]];
co.push_back(cur);
}
}
if (flag) return 0;
if (cnt == 0) return 0;
if (temp > t) return 0;
dp[temp] = 1;
for (j = 0; j < ((int)co.size()); j++)
for (i = temp; i <= t; i++) {
if (co[j] + i <= t) add(dp[co[j] + i], dp[i]);
}
return dp[t];
}
int main() {
int i, j, k, tks = 1, ks = 1, a, b;
while (scanf("%d%d%I64d", &n, &q, &t) == 3) {
for (i = 0; i < n; i++) scanf("%I64d", &v[i + 1]);
memset(dp, 0, sizeof(dp));
memset(les, 0, sizeof(les));
memset(gre, 0, sizeof(gre));
memset(col, 0, sizeof(col));
flag = false;
for (i = 0; i < q; i++) {
scanf("%d%d", &a, &b);
les[a] = b;
gre[b] = a;
}
cout << CalAns() << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const double e = 1e-11;
double a[N];
int n;
double miin, maax;
double Min, Max;
void Do(double t) {
Min = 1e9;
Max = -1e9;
double sum = 0;
double in = 0, ax = 0;
for (int i = 0; i < n; ++i) {
sum += a[i] + t;
in = min(in, sum);
ax = max(ax, sum);
Min = min(Min, sum - ax);
Max = max(Max, sum - in);
}
if (!Min) Min = miin + t;
if (!Max) Max = maax + t;
}
void binup() {
double l = 0, r = 10000;
while (r - l > e) {
double m = (l + r) / 2;
Do(m);
if (-Min < Max)
r = m;
else
l = m;
}
Do(l);
}
void bindown() {
double l = -10000, r = 0;
while (r - l > e) {
double m = (l + r) / 2;
Do(m);
if (-Min > Max)
l = m;
else
r = m;
}
Do(l);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
miin = 1e9;
maax = -1e9;
for (int i = 0; i < n; ++i) {
cin >> a[i];
miin = min(miin, a[i]);
maax = max(maax, a[i]);
}
Do(0);
if (-Min < Max)
bindown();
else
binup();
printf("%0.12f", (Max - Min) / 2);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
int flag[100001];
int degree[100001];
int pre[100001];
int trace[100001];
int cnt;
int main() {
int i, j;
int n;
while (scanf("%d", &n) != EOF) {
vec.clear();
memset(degree, 0, sizeof(degree));
for (i = 1; i <= n; i++) {
scanf("%d", &flag[i]);
if (flag[i]) vec.push_back(i);
}
for (i = 1; i <= n; i++) {
scanf("%d", &pre[i]);
if (pre[i]) degree[pre[i]]++;
}
cnt = 0;
for (i = 0; i < vec.size(); i++) {
int now = vec[i];
j = pre[now];
int tcnt = 1;
while (j && flag[j] == 0 && degree[j] < 2) {
j = pre[j];
tcnt++;
}
if (tcnt > cnt) {
cnt = 0;
j = now;
while (cnt < tcnt) {
trace[cnt++] = j;
j = pre[j];
}
}
}
printf("%d\n", cnt);
printf("%d", trace[cnt - 1]);
for (i = cnt - 2; i >= 0; i--) printf(" %d", trace[i]);
printf("\n");
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
double p[105];
int n;
bool cmp(double a, double b) { return a > b; }
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> p[i];
sort(p, p + n, cmp);
double ans = 0;
double zero = 1, one = 0;
for (int i = 0; i < n; ++i) {
one = zero * p[i] + one * (1 - p[i]);
zero = zero * (1 - p[i]);
ans = max(ans, one);
}
cout << fixed << setprecision(10) << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long ans, sum;
struct ming {
int price, size, num;
} a[100005], b[100005];
bool cmp(ming a, ming b) { return a.price > b.price; }
map<int, int> mp;
vector<int> G[100005];
int p[100005];
bool dfs(int x) {
int siz = G[x].size();
for (int i = 0; i < siz; i++)
if (p[G[x][i]] != x)
if (p[G[x][i]] == -1 || dfs(p[G[x][i]])) {
p[G[x][i]] = x;
return 1;
}
return 0;
}
signed main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d %d", &a[i].price, &a[i].size), a[i].num = i;
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d %d", &b[i].price, &b[i].size);
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) mp[a[i].size] = i;
for (int i = 1; i <= m; i++) {
if (mp[b[i].size + 1] && b[i].price >= a[mp[b[i].size + 1]].price)
G[mp[b[i].size + 1]].push_back(i);
if (mp[b[i].size] && b[i].price >= a[mp[b[i].size]].price)
G[mp[b[i].size]].push_back(i);
}
memset(p, -1, sizeof(p));
for (int i = 1; i <= n; i++)
if (dfs(i)) ans += a[i].price * 1ll, sum++;
printf("%lld\n%lld\n", ans, sum);
for (int i = 1; i <= m; i++)
if (p[i] != -1) printf("%d %d\n", i, a[p[i]].num);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MAXN = N;
const long long INF = 1e18 + 7;
const long long Q = -(1ll << 60);
struct line {
long long m, p;
mutable set<line>::iterator prev;
};
set<line>::iterator null;
bool operator<(const line& a, const line& b) {
if (b.p != Q && a.p != Q) {
return a.m < b.m;
}
if (b.p == Q) {
if (a.prev == null) return true;
bool ok = true;
if ((a.prev->m - a.m) < 0) ok = !ok;
if (ok) {
return (a.p - a.prev->p) < (a.prev->m - a.m) * b.m;
} else {
return (a.p - a.prev->p) > (a.prev->m - a.m) * b.m;
}
} else {
if (b.prev == null) return false;
bool ok = true;
if ((b.prev->m - b.m) < 0) ok = !ok;
if (ok) {
return !((b.p - b.prev->p) < a.m * (b.prev->m - b.m));
} else {
return !((b.p - b.prev->p) > a.m * (b.prev->m - b.m));
}
}
}
class DynamicConvexHull {
public:
set<line> convex;
set<line>::iterator next(set<line>::iterator ii) {
set<line>::iterator gg = ii;
gg++;
return gg;
}
set<line>::iterator prev(set<line>::iterator ii) {
set<line>::iterator gg = ii;
gg--;
return gg;
}
bool bad(set<line>::iterator jj) {
set<line>::iterator ii, kk;
if (jj == convex.begin()) return false;
kk = next(jj);
if (kk == convex.end()) return false;
ii = prev(jj);
line a = *ii, c = *kk, b = *jj;
bool ok = true;
if ((b.m - a.m) < 0) ok = !ok;
if ((b.m - c.m) < 0) ok = !ok;
if (ok) {
return (c.p - b.p) * (b.m - a.m) <= (a.p - b.p) * (b.m - c.m);
} else {
return (c.p - b.p) * (b.m - a.m) >= (a.p - b.p) * (b.m - c.m);
}
}
void del(set<line>::iterator ii) {
set<line>::iterator jj = next(ii);
if (jj != convex.end()) {
jj->prev = ii->prev;
}
convex.erase(ii);
}
void add_progression(long long m, long long p) {
null = convex.end();
line g;
g.m = m;
g.p = p;
set<line>::iterator ii = convex.find(g);
if (ii != convex.end()) {
if (ii->p >= p) return;
del(ii);
}
convex.insert(g);
ii = convex.find(g);
set<line>::iterator jj = next(ii);
if (jj != convex.end()) jj->prev = ii;
if (ii != convex.begin()) {
ii->prev = prev(ii);
} else {
ii->prev = convex.end();
}
if (bad(ii)) {
del(ii);
return;
}
jj = next(ii);
while (jj != convex.end() && bad(jj)) {
del(jj);
jj = next(ii);
}
if (ii != convex.begin()) {
jj = prev(ii);
while (ii != convex.begin() && bad(jj)) {
del(jj);
jj = prev(ii);
}
}
}
long long get_max_value_at(long long x) {
null = convex.end();
line y;
y.m = x;
y.p = Q;
set<line>::iterator ii = convex.lower_bound(y);
ii--;
return ii->m * x + ii->p;
}
};
class LineTree {
private:
struct Node {
int l;
int r;
DynamicConvexHull dch;
} tree[MAXN * 4];
public:
void init(int pos, int l, int r) {
tree[pos].l = l;
tree[pos].r = r;
if (l < r) {
init(pos + pos, l, (l + r) / 2);
init(pos + pos + 1, (l + r) / 2 + 1, r);
}
}
void modify(int pos, int j, long long x, long long y) {
tree[pos].dch.add_progression(x, y);
if (tree[pos].l == tree[pos].r) return;
if (tree[pos + pos].r >= j)
modify(pos + pos, j, x, y);
else
modify(pos + pos + 1, j, x, y);
}
long long query(int pos, int l, int r, long long par) {
if (tree[pos].l == l && tree[pos].r == r)
return tree[pos].dch.get_max_value_at(par);
else {
long long ret = -INF;
if (l <= min(r, tree[pos + pos].r))
ret = max(ret, query(pos + pos, l, min(r, tree[pos + pos].r), par));
if (max(l, tree[pos + pos + 1].l) <= r)
ret = max(ret,
query(pos + pos + 1, max(l, tree[pos + pos + 1].l), r, par));
return ret;
}
}
} lt;
long long A[N], S[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> A[i];
S[i] = A[i] + S[i - 1];
}
lt.init(1, 1, n);
for (int k = 1; k <= n; k++) {
lt.modify(1, k, -A[k], -(k)*A[k] + S[k]);
}
int m;
cin >> m;
long long i, j;
while (m--) {
cin >> i >> j;
long long ans = lt.query(1, j - i + 1, j, i - j) - S[j];
cout << -ans << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
class Node {
public:
int n = 0;
bool fixed = true;
int cost = 0;
int d = 0;
Node() {}
Node(int n, bool fixed) {
this->n = n;
this->fixed = fixed;
}
Node(int n, int d, int cost) {
this->n = n;
this->cost = cost;
this->d = d;
}
};
class comp {
public:
bool operator()(Node o1, Node o2) {
if (o1.d - o2.d != 0)
return o1.d > o2.d;
else
return o1.cost > o2.cost;
}
};
vector<vector<Node>> g;
set<pair<int, pair<int, int>>> ans;
set<pair<int, int>> unFixed, path;
int dist[100100], cost[100100], parent[100100];
void dijkstra() {
fill(dist, dist + 100100, INT_MAX);
fill(cost, cost + 100100, INT_MAX);
memset(parent, -1, sizeof parent);
priority_queue<Node, vector<Node>, comp> pq;
dist[1] = 0;
pq.push(Node(1, 0, 0));
Node top;
while (!pq.empty()) {
top = pq.top(), pq.pop();
int d = top.d, c = top.cost, u = top.n;
if (d > dist[u]) continue;
for (int i = 0; i < (int)g[u].size(); i++) {
Node child = g[u][i];
if (dist[child.n] > d + 1) {
dist[child.n] = d + 1;
cost[child.n] = c + (child.fixed ? 0 : 1);
pq.push(Node(child.n, dist[child.n], cost[child.n]));
parent[child.n] = u;
} else if (dist[child.n] == d + 1 &&
cost[child.n] > c + (child.fixed ? 0 : 1)) {
cost[child.n] = c + (child.fixed ? 0 : 1);
pq.push(Node(child.n, dist[child.n], cost[child.n]));
parent[child.n] = u;
}
}
}
int x = n;
while (parent[x] != -1) {
if (unFixed.count(make_pair(parent[x], x))) {
ans.insert(make_pair(parent[x], make_pair(x, 1)));
}
path.insert(make_pair(parent[x], x));
path.insert(make_pair(x, parent[x]));
x = parent[x];
}
}
void getAns() {
for (int i = 0; i < (int)g.size(); i++) {
for (int j = 0; j < (int)g[i].size(); j++) {
if (g[i][j].fixed && !path.count(make_pair(i, g[i][j].n)) &&
!ans.count(make_pair(g[i][j].n, make_pair(i, 0))))
ans.insert(make_pair(i, make_pair(g[i][j].n, 0)));
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(), cout.tie();
cin >> n >> m;
g.resize(n + 5);
int x, y, z;
for (int i = 0; i < m; i++) {
cin >> x >> y >> z;
g[x].push_back(Node(y, z == 1));
g[y].push_back(Node(x, z == 1));
if (z == 0) {
unFixed.insert(make_pair(x, y));
unFixed.insert(make_pair(y, x));
}
}
dijkstra();
getAns();
cout << ans.size() << endl;
for (set<pair<int, pair<int, int>>>::iterator it = ans.begin();
it != ans.end(); it++) {
auto x = *it;
cout << x.first << ' ' << x.second.first << ' ' << x.second.second << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, t, u;
cin >> s;
cin >> t;
u = "";
for (int i = 0; i < s.length(); i++)
if (t[i] > s[i]) {
cout << -1;
return 0;
} else if (t[i] == s[i])
u += "z";
else
u += t[i];
cout << u;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const double PI = 4 * atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
const int MAX_N = 2e6 + 10;
int n, k;
void solve() {
const int len = (n - 1) / k;
if ((n - 1) % k <= 1) {
cout << 2 * len + (n - 1) % k << '\n';
} else {
cout << 2 * len + 2 << '\n';
}
int i;
for (i = 2; i <= k + 1 && i <= n; ++i) cout << i << ' ' << 1 << '\n';
while (i <= n) {
cout << i << ' ' << i - k << '\n';
i++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> k;
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int ansa, ansb;
list<stack<int> > otro;
void generarserie(int valor) {
int inicial = ansb / valor;
inicial *= valor;
stack<int> algo;
if (inicial < ansb) inicial += valor;
while (inicial <= ansa) {
algo.push(inicial);
inicial += valor;
}
otro.push_back((algo));
}
void f() {
int valorbandera = ansa;
int ansfinal = 0;
stack<list<stack<int> >::iterator> st;
while (valorbandera > ansb && otro.size() > 0) {
int maxx = valorbandera;
for (list<stack<int> >::iterator ite = otro.begin(); ite != otro.end();
ite++) {
if ((*ite).size() > 0) maxx = min(maxx, (*ite).top());
}
if (maxx == valorbandera) {
maxx--;
}
for (list<stack<int> >::iterator ite = otro.begin(); ite != otro.end();
ite++) {
while ((*ite).size() > 0 && (*ite).top() > maxx) {
(*ite).pop();
}
if ((*ite).size() == 0) st.push(ite);
}
while (!st.empty()) {
otro.erase(st.top());
st.pop();
}
valorbandera = maxx;
ansfinal++;
}
ansfinal += valorbandera - ansb;
cout << ansfinal << endl;
}
int main() {
int n;
int valor;
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> valor;
s.insert(valor);
}
cin >> ansa >> ansb;
int c = 0;
for (set<int>::iterator ite = s.begin(); ite != s.end(); ite++) {
generarserie(*ite);
}
f();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int MAXN = 1e5 + 5;
const int MAXM = 2e6 + 5;
const int MOD = 1e9 + 7;
int sgn(double x) {
if (fabs(x) < eps) return 0;
if (x < 0)
return -1;
else
return 1;
}
long long pw(long long a, long long n, long long mod) {
long long ret = 1;
while (n) {
if (n & 1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void inc(long long &a, long long b) {
a += b;
if (a >= MOD) a -= MOD;
}
void dec(long long &a, long long b) {
a -= b;
if (a < 0) a += MOD;
}
int mul(int a, int b) {
long long c = 1ll * a * b;
return c - c / MOD * MOD;
}
int n, mx, p[] = {2, 3, 5, 7, 11};
vector<int> ve;
int main() {
scanf("%d", &n);
mx = 2 * n * n;
ve.push_back(1);
while (1) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < ((int)(ve).size()); j++) {
if (ve[j] * p[i] <= mx) ve.push_back(ve[j] * p[i]);
}
if (((int)(ve).size()) > n) break;
}
if (((int)(ve).size()) > n) break;
}
reverse((ve).begin(), (ve).end());
for (int i = 0; i < n; i++) printf("%d ", ve[i]);
}
| 3 |
Subsets and Splits