solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
bool down[300000];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, m, acu = 0, foo, saw;
bool seen = false;
cin >> n >> m;
for (long long i = 0; i < n; ++i) {
cin >> foo;
if (foo == m) saw = i;
down[i] = (foo > m);
}
map<pair<long long, long long>, long long> arriba, abajo;
long long l = 0, mo = 0;
for (long long i = saw - 1; i >= 0; --i) {
l += (down[i] == 0);
mo += (down[i] == 1);
abajo[make_pair(l - min(l, mo), mo - min(l, mo))]++;
}
l = 0;
mo = 0;
for (long long i = saw + 1; i < n; ++i) {
l += (down[i] == 0);
mo += (down[i] == 1);
arriba[make_pair(l - min(l, mo), mo - min(l, mo))]++;
}
long long rta = 0;
for (auto i : arriba) {
if (abajo[make_pair(i.first.second, i.first.first)] > 0)
rta += i.second * abajo[make_pair(i.first.second, i.first.first)];
if (abajo[make_pair(i.first.second, i.first.first + 1)] > 0)
rta += i.second *
abajo[make_pair(
i.first.second - min(i.first.second, i.first.first + 1),
i.first.first + 1 - min(i.first.second, i.first.first + 1))];
if (i.first.second > 0 &&
abajo[make_pair(i.first.second - 1, i.first.first)] > 0)
rta += i.second * abajo[make_pair(i.first.second - 1, i.first.first)];
}
cout << rta + (long long)(abajo[make_pair(0, 0)] + abajo[make_pair(0, 1)] +
arriba[make_pair(0, 0)] + arriba[make_pair(0, 1)] +
1);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t, i;
cin >> t;
while (t--) {
long long n, x;
cin >> n >> x;
string s;
cin >> s;
long long ans = 0;
long long a[n];
long long b[n];
long long flag = 0;
map<long long, long long> m;
for (long long i = 0; i < n; i++) {
if (s[i] == '0')
ans += 1;
else
ans -= 1;
a[i] = ans;
if (ans == x) flag = 1;
m[a[i]]++;
}
if ((x == ans && ans == 0) || (ans == 0 && (flag == 1))) {
cout << "-1"
<< "\n";
continue;
} else {
long long count = 0;
if (ans != 0) {
for (auto v : m) {
long long val = v.first;
if ((x > val)) {
if (ans > 0 && ((x - val) % ans == 0)) {
count += v.second;
}
} else if (x < val) {
if (ans < 0 && ((abs(x - val)) % abs(ans) == 0)) {
count += v.second;
}
} else {
count += v.second;
}
}
}
if (x == 0) count++;
cout << count << "\n";
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const long long N = 3e5 + 7;
long long n, x;
long long arr[N];
long long pwr(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res % 1000000007;
}
void solve() {
cin >> n >> x;
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
vector<long long> vec;
for (long long i = 0; i < n; i++) vec.push_back(sum - arr[i]);
sort(vec.rbegin(), vec.rend());
while (1) {
long long v = vec.back(), cnt = 0;
while (!vec.empty() and v == vec.back()) {
vec.pop_back();
cnt++;
}
if (cnt % x) {
v = min(v, sum);
cout << pwr(x, v);
return;
} else {
for (long long i = 0; i < cnt / x; i += 1) {
vec.push_back(v + 1);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) solve();
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a, b, c, d;
int main() {
scanf("%d", &n);
for (int i = 1; i <= 4; i++) {
scanf("%d%d%d%d", &a, &b, &c, &d);
if (min(a, b) + min(c, d) <= n) {
printf("%d %d %d", i, min(a, b), n - min(a, b));
return 0;
}
}
printf("-1");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100001;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
;
int n, m, l[N], r[N], a[N], min = N, ind = -1;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> l[i] >> r[i];
if ((r[i] - l[i] + 1) < min) {
min = r[i] - l[i] + 1;
ind = i;
}
}
cout << min << endl;
for (int i = l[ind] - 1; i < r[ind]; i++) a[i] = i - l[ind] + 1;
for (int i = l[ind] - 2; i >= 0; i--) {
if (a[i + 1] == 0)
a[i] = min - 1;
else
a[i] = a[i + 1] - 1;
}
for (int i = r[ind]; i < n; i++) {
if (a[i - 1] == min - 1)
a[i] = 0;
else
a[i] = a[i - 1] + 1;
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
bool valid(long long x, long long y) {
return x > 0 && x <= n && y > 0 && y <= m;
}
int main() {
cin >> n >> m;
long long x, y, k, mx, my, sol, tsol = 0;
cin >> x >> y >> k;
for (int i = 0; i < k; i++) {
sol = 0;
cin >> mx >> my;
long long l = 0, h = (long long)10e9 + 5, mi;
while (l <= h) {
mi = l + (h - l) / 2;
if (valid(x + mx * mi, y + my * mi)) {
sol = mi;
l = mi + 1;
} else
h = mi - 1;
}
tsol += sol;
x += mx * sol;
y += my * sol;
}
cout << tsol << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long a[1000001] = {};
long long r[1000001] = {};
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
int d[100] = {};
int ct = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i] >= d[j]) {
d[j]++;
if (j > ct) ct = j;
break;
}
}
}
cout << ct + 1 << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> fac;
long long max_ans, min_ans, real_mod;
void get_fac(long long nk) {
long long m = sqrt(nk + 0.5);
for (long long i = 1; i <= m; i++) {
if (nk % i == 0) {
fac.push_back(i);
if (i != nk / i) {
fac.push_back(nk / i);
}
}
}
sort(fac.begin(), fac.end());
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long ex_gcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = ex_gcd(b, a % b, y, x);
y = y - a / b * x;
return d;
}
long long linearmod(long long a, long long b, long long c) {
long long x, y;
long long d = ex_gcd(a, c, x, y);
if (b % d) {
return -1;
}
x = b / d * x;
long long m = c / d;
real_mod = m;
return (x % m + m) % m;
}
void get_ans(long long nk, long long n, long long k, long long extra) {
map<long long, map<long long, long long> > mp;
int sz = fac.size() - 1;
for (int i = sz; i >= 0; i--) {
long long fa = fac[i];
long long ret = linearmod(k, (-(extra % fa) + fa) % fa, fa);
if (ret != -1 &&
(mp.find(ret) == mp.end() || mp[ret].find(real_mod) == mp[ret].end())) {
mp[ret][real_mod] = 1;
} else {
ret = -1;
}
if (ret != -1) {
max_ans = max(max_ans, nk / fa);
min_ans = min(min_ans, nk / fa);
}
}
}
int main() {
long long n, k, a, b;
max_ans = -1e12;
min_ans = 1e12;
scanf("%lld%lld%lld%lld", &n, &k, &a, &b);
if (a == 0 && b == 0) {
printf("1 %lld\n", n);
return 0;
}
long long nk = n * k;
get_fac(nk);
get_ans(nk, n, k, a + b);
get_ans(nk, n, k, k - a + b);
get_ans(nk, n, k, k - b + a);
get_ans(nk, n, k, 2 * k - a - b);
long long l = k - a - b;
if (l > 0) {
min_ans = min(min_ans, nk / gcd(nk, l));
max_ans = max(max_ans, nk / gcd(nk, l));
}
if (l > 0) {
min_ans = min(min_ans, nk / gcd(nk, l));
max_ans = max(max_ans, nk / gcd(nk, l));
}
printf("%lld %lld\n", min_ans, max_ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
inline void JadedBeast() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int MAX = 1e5 + 1;
int main() {
JadedBeast();
int n, u;
double ans = -1;
cin >> n >> u;
int E[n];
for (int i = 0; i < n; i++) cin >> E[i];
int i = 0, j = 1, k = 2;
bool flag = false;
while (i < j && j < k && k < n) {
while (E[k] - E[i] < u && k < n) k++;
if (((E[k] - E[i] > u) && j + 1 < k) || k >= n) k--;
double x = (double)(E[k] - E[j]) / (E[k] - E[i]);
if (E[k] - E[i] <= u && i < j && j < k && k < n) {
flag = true;
ans = max(x, ans);
}
i++, j++;
if (j == k) k++;
}
cout.precision(22);
cout << ans;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
return (p1.second < p2.second);
}
int main() {
int n, k, res = INT_MAX;
cin >> n >> k;
vector<int> v(n);
vector<pair<int, int> > p;
map<int, int> m;
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) {
int c = 0;
int x = v[i];
while (x > 0) {
p.push_back(make_pair(x, c++));
m[x]++;
x /= 2;
}
}
sort(p.begin(), p.end());
for (int i = 0; i < p.size(); i++) {
int sum = 0;
if (m[p[i].first] >= k) {
sort(p.begin() + i, p.begin() + i + m[p[i].first], cmp);
for (int j = i; j < i + k; j++) sum += p[j].second;
if (res > sum) res = sum;
}
i += m[p[i].first] - 1;
}
cout << res << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int i, j, m, n, p, k, x, y, Q[3005], vis[3005], ege;
int dis[3005][3005], s1, t1, s2, t2, l1, l2, ans;
int pre[6005], head[3005], en[6005], v[6005];
queue<int> qu;
void add(int a, int b, int c) {
ege++;
pre[ege] = head[a];
head[a] = ege;
en[ege] = b;
v[ege] = c;
}
void bfs(int x) {
dis[x][x] = 0;
memset(vis, 0, sizeof(vis));
while (!qu.empty()) qu.pop();
vis[x] = 1;
qu.push(x);
while (!qu.empty()) {
int k = qu.front();
qu.pop();
int p = head[k];
while (p != -1) {
if (!vis[en[p]])
dis[x][en[p]] = dis[x][k] + v[p], qu.push(en[p]), vis[en[p]] = 1;
p = pre[p];
}
}
}
void init() {
memset(dis, -1, sizeof(dis));
memset(head, -1, sizeof(head));
ege = 0;
}
int main() {
scanf("%d%d", &n, &m);
init();
for (i = 1; i <= m; ++i) {
scanf("%d%d", &x, &y);
add(x, y, 1);
add(y, x, 1);
}
for (i = 1; i <= n; ++i) bfs(i);
scanf("%d%d%d%d%d%d", &s1, &t1, &l1, &s2, &t2, &l2);
if (dis[s1][t1] > l1 || dis[s2][t2] > l2) {
printf("-1\n");
return 0;
}
ans = dis[s1][t1] + dis[s2][t2];
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
if (dis[s1][i] + dis[i][j] + dis[j][t1] <= l1) {
if (dis[s2][i] + dis[i][j] + dis[j][t2] <= l2)
ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][i] +
dis[j][t2]);
if (dis[s2][j] + dis[i][j] + dis[i][t2] <= l2)
ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][j] +
dis[i][t2]);
}
printf("%d\n", m - ans);
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int RLEN = 1 << 18 | 1;
inline char nc() {
static char ibuf[RLEN], *ib, *ob;
(ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
return (ib == ob) ? -1 : *ib++;
}
inline int rd() {
char ch = nc();
int i = 0, f = 1;
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = nc();
}
while (isdigit(ch)) {
i = (i << 1) + (i << 3) + ch - '0';
ch = nc();
}
return i * f;
}
inline void W(int x) {
static int buf[50];
if (!x) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
while (x) {
buf[++buf[0]] = x % 10;
x /= 10;
}
while (buf[0]) {
putchar(buf[buf[0]--] + '0');
}
}
const int N = 1e5 + 50, B = 30, L = 18;
int n, w, q, ans[N], nxt[N];
int lg[N], x[N], rmq_mx[N][20], rmq_mn[N][20];
vector<pair<int, int> > det;
vector<pair<int, int> > ask;
struct node {
node *fa, *lc, *rc;
int id, sze;
node() : fa(NULL), lc(NULL), rc(NULL), sze(0) {}
inline void upt() { sze = (lc ? lc->sze : 0) + (rc ? rc->sze : 0) + 1; }
} * pos[N];
inline bool isroot(node *x) {
return (!x->fa) || (x->fa->lc != x && x->fa->rc != x);
}
inline bool which(node *x) { return (x->fa->lc == x); }
inline void rotate(node *x) {
node *y = x->fa, *z = y->fa;
if (!isroot(y)) (z->lc == y ? z->lc : z->rc) = x;
x->fa = z;
y->fa = x;
if (y->lc == x) {
node *b = x->rc;
x->rc = y;
y->lc = b;
if (b) b->fa = y;
} else {
node *b = x->lc;
x->lc = y;
y->rc = b;
if (b) b->fa = y;
}
y->upt();
x->upt();
}
inline void splay(node *x) {
while (!isroot(x)) {
node *y = x->fa;
if (!isroot(y)) {
if (which(y) == which(x))
rotate(y);
else
rotate(x);
}
rotate(x);
}
}
inline void access(node *x) {
for (node *y = NULL; x; y = x, x = x->fa) {
splay(x);
x->rc = y;
x->upt();
}
}
inline void cut(node *x, node *f) {
access(f);
splay(x);
x->fa = NULL;
}
inline void link(node *x, node *f) { x->fa = f; }
inline void modify(int lim) {
static int p = 0;
while (p < det.size() && det[p].first <= lim) {
int u = det[p++].second;
if (nxt[u] + u > n)
continue;
else if ((++nxt[u]) == B) {
cut(pos[u], pos[u + nxt[u]]);
} else {
cut(pos[u], pos[u + nxt[u]]);
link(pos[u], pos[u + nxt[u] + 1]);
}
}
}
inline node *getroot(node *x) {
while (x->lc) x = x->lc;
splay(x);
return x;
}
inline int find_nxt(int now, int lim) {
int mx = x[now], mn = x[now];
for (int i = lg[n - now + 1]; ~i && now <= n; i--) {
if ((now + (1 << i) - 1 <= n) &&
max(mx, rmq_mx[now][i]) - min(mn, rmq_mn[now][i]) <= lim) {
mx = max(mx, rmq_mx[now][i]);
mn = min(mn, rmq_mn[now][i]);
now += (1 << i);
}
}
return now;
}
inline int query(int lim) {
int now = 1, cnt = 0;
while (now <= n) {
access(pos[now]);
splay(pos[now]);
cnt += pos[now]->sze - 1;
now = getroot(pos[now])->id;
if (now != n + 1) ++cnt, now = find_nxt(now, lim);
}
return cnt;
}
int main() {
n = rd(), w = rd(), q = rd();
lg[1] = 0;
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; i++) rmq_mn[i][0] = rmq_mx[i][0] = x[i] = rd();
for (int i = 1; i <= lg[n]; i++)
for (int j = 1; j + (1 << i) - 1 <= n; ++j)
rmq_mn[j][i] = min(rmq_mn[j][i - 1], rmq_mn[j + (1 << (i - 1))][i - 1]),
rmq_mx[j][i] = max(rmq_mx[j][i - 1], rmq_mx[j + (1 << (i - 1))][i - 1]);
pos[n + 1] = new node();
pos[n + 1]->id = n + 1;
for (int i = n; i >= 1; i--) {
pos[i] = new node();
pos[i]->fa = pos[i + 1];
pos[i]->sze = 1;
pos[i]->id = i;
}
for (int i = 1; i <= n; i++) {
int mx = x[i], mn = x[i];
for (int j = 1; j <= B && i + j <= n; j++) {
mx = max(mx, x[i + j]);
mn = min(mn, x[i + j]);
det.push_back(pair<int, int>(mx - mn, i));
}
}
sort(det.begin(), det.end());
for (int i = 1; i <= q; i++) {
int k = w - rd();
ask.push_back(pair<int, int>(k, i));
}
sort(ask.begin(), ask.end());
for (int i = 0; i < ask.size(); ++i) {
modify(ask[i].first);
ans[ask[i].second] = query(ask[i].first) - 1;
}
for (int i = 1; i <= q; i++) W(ans[i]), putchar('\n');
}
| 26 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<ll>;
using VB = vector<bool>;
using VD = vector<long double>;
using VC = vector<char>;
using VS = vector<string>;
using V2I = vector<VI>;
using V3I = vector<V2I>;
const ll mod{(ll)1e9 + 7};
const long double pi = 2 * acos(0.0);
void solve() {
ll n;
cin >> n;
VI vec(n);
for (ll &x : vec) cin >> x;
if (n == 1) {
cout << 1 << ' ' << 1 << '\n';
cout << 0 << '\n';
cout << 1 << ' ' << 1 << '\n';
cout << 0 << '\n';
cout << 1 << ' ' << 1 << '\n';
cout << -vec[0];
return;
}
map<ll, ll> mp;
for (ll i{}, j{n - 1}; i < n; i++, j += n - 1) mp[j % n] = j;
cout << 1 << ' ' << n - 1 << '\n';
for (ll i{}; i < n - 1; i++) {
if (vec[i] < 0) {
ll tem{mp[n - -vec[i] % n]};
tem = -tem;
cout << tem << ' ';
vec[i] += tem;
} else {
ll tem{mp[n - vec[i] % n]};
cout << tem << ' ';
vec[i] += tem;
}
}
cout << '\n' << n << ' ' << n << '\n';
if (vec[n - 1] < 0) {
cout << -mp[n - -vec[n - 1] % n] << '\n';
vec[n - 1] -= mp[n - -vec[n - 1] % n];
} else {
cout << mp[n - vec[n - 1] % n] << '\n';
vec[n - 1] += mp[n - vec[n - 1] % n];
}
cout << 1 << ' ' << n << '\n';
for (ll i{}; i < n; i++) {
cout << -vec[i] << ' ';
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, arr[1000000], pos = 0, neg = 0, temp, temp1;
cin >> n;
for (i = 0; i < n; i++) cin >> arr[i];
sort(arr, arr + n);
for (i = 0; i < n; i++) {
if (arr[i] > 0)
pos++;
else if (arr[i] < 0)
neg++;
}
if (n % 2 == 0)
temp = n / 2;
else
temp = (n / 2) + 1;
if (neg >= temp && pos >= temp)
cout << (-1);
else if (pos >= temp)
cout << 1;
else if (neg >= temp)
cout << -1;
else
cout << 0;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
int64_t total = 0;
int x = 0;
vector<vector<int>> vcur = {a};
for (int j = 29; j >= 0; j--) {
int64_t sum0 = 0, sum1 = 0;
vector<vector<int>> vnext;
for (auto& v : vcur) {
int cnt0 = 0, cnt1 = 0;
vector<int> v0, v1;
for (int b : v) {
if (b & (1 << j))
cnt1++, sum1 += cnt0, v1.push_back(b);
else
cnt0++, sum0 += cnt1, v0.push_back(b);
}
if (v0.size() > 1) vnext.push_back(v0);
if (v1.size() > 1) vnext.push_back(v1);
}
if (sum0 <= sum1)
total += sum0;
else
total += sum1, x |= (1 << j);
vcur = vnext;
}
cout << total << " " << x << endl;
}
| 12 |
#include <bits/stdc++.h>
std::array<int, 5005> requests;
std::array<int, 5005> prefix;
int main() {
int n;
std::cin >> n;
int cur = 0, cumsum = 0;
for (int i = 0; i < n; ++i) {
std::cin >> cur;
requests[i] = cur;
cumsum += cur;
prefix[i + 1] = cumsum;
}
auto max = 0;
for (int i = 0; i < n; ++i) {
auto offset = prefix[i];
auto time = 0;
for (int j = i + 1; j <= n; ++j) {
time += 100;
auto diff = prefix[j] - offset;
if (diff > time) {
max = std::max(max, j - i);
}
}
}
std::cout << max;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char str[202030];
int a[202030], c[202030];
int qa[202030], qc[202030];
long long wfPow(int x) {
if (x <= 0) {
return 1;
}
long long v = wfPow(x / 2);
v = (v * v) % 1000000007;
if (x % 2) {
v = (v * 3) % 1000000007;
}
return v;
}
long long wf(int cntA, int cntQ) {
long long x = (cntA * wfPow(cntQ)) % 1000000007;
long long y = (cntQ * wfPow(cntQ - 1)) % 1000000007;
return (x + y) % 1000000007;
}
void solve() {
int i;
a[0] = str[0] == 'a';
qa[0] = str[0] == '?';
for (i = 1; i < n; i++) {
a[i] = a[i - 1] + (str[i] == 'a');
qa[i] = qa[i - 1] + (str[i] == '?');
}
c[n - 1] = str[n - 1] == 'c';
qc[n - 1] = str[n - 1] == '?';
for (i = n - 2; i >= 0; i--) {
c[i] = c[i + 1] + (str[i] == 'c');
qc[i] = qc[i + 1] + (str[i] == '?');
}
long long ans = 0;
for (i = 1; i < n - 1; i++) {
if (str[i] == 'b' || str[i] == '?') {
long long l = wf(a[i - 1], qa[i - 1]);
long long r = wf(c[i + 1], qc[i + 1]);
l = (l * r) % 1000000007;
ans = (ans + l) % 1000000007;
}
}
printf("%lld\n", ans);
}
int main() {
int tc;
while (scanf("%d %s", &n, &str) != EOF) {
solve();
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[1005][1005];
int col[1005], row[1005];
int main() {
cin >> n >> m;
bool emp = true;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < m; j++) {
if (s[j] == '#') {
a[i][j] = 1;
emp = false;
}
}
}
if (emp) {
cout << "0\n";
} else {
bool can = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
row[i] += a[i][j];
col[j] += a[i][j];
}
}
for (int i = 0; i < n; i++) {
if (row[i] == 0) {
can = true;
for (int j = 0; j < m; j++) {
if (col[j] == 0) can = false;
}
if (can) break;
}
}
for (int j = 0; j < m; j++) {
if (col[j] == 0) {
can = true;
for (int i = 0; i < n; i++) {
if (row[i] == 0) can = false;
}
if (can) break;
}
}
if (can) {
cout << "-1\n";
return 0;
}
for (int i = 0; i < n; i++) {
bool was = false;
for (int j = 0; j < m; j++) {
if (a[i][j] == 0 && row[i] && was) can = true;
if (a[i][j] == 1) {
was = true;
row[i]--;
}
}
}
for (int j = 0; j < m; j++) {
bool was = false;
for (int i = 0; i < n; i++) {
if (a[i][j] == 0 && col[j] && was) can = true;
if (a[i][j] == 1) {
was = true;
col[j]--;
}
}
}
if (can) {
cout << "-1\n";
return 0;
}
queue<pair<int, int>> q = queue<pair<int, int>>();
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == 1) {
cnt++;
q.push({i, j});
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
if (a[x][y] == 2) continue;
a[x][y] = 2;
if (x > 0) {
if (a[x - 1][y] == 1) {
q.push({x - 1, y});
}
}
if (y > 0) {
if (a[x][y - 1] == 1) {
q.push({x, y - 1});
}
}
if (x < n - 1) {
if (a[x + 1][y] == 1) {
q.push({x + 1, y});
}
}
if (y < m - 1) {
if (a[x][y + 1] == 1) {
q.push({x, y + 1});
}
}
}
}
}
}
cout << cnt << "\n";
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int ans[300005];
vector<int> e[300005];
queue<int> q;
int main() {
int i, j;
scanf("%d%d", &n, &m);
int x, y;
while (m--) {
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
for (i = 1; i <= n; i++) q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
int cnt = 0;
for (i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (ans[u] == ans[v]) cnt++;
}
if (cnt >= 2) {
ans[u] ^= 1;
for (i = 0; i < e[u].size(); i++) q.push(e[u][i]);
}
}
for (i = 1; i <= n; i++) printf("%d", ans[i]);
puts("");
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(20);
int n;
cin >> n;
vector<long long> c(2 * n);
for (int i = 0; i < 2 * n; i++) cin >> c[i];
sort(c.begin(), c.end());
long long area = LLONG_MAX;
for (int i = 0; i < n; i++) {
long long x = c[n + i - 1] - c[i], y;
if (i == 0)
y = c[2 * n - 1] - c[n];
else
y = c[2 * n - 1] - c[0];
area = min(area, x * y);
}
cout << area << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1000000000;
const long long int mod = 1000000000 + 7;
inline void IO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
inline int dcmp(long double x) { return x < -1e-12 ? -1 : (x > 1e-12); }
template <class T>
inline int CHECK(T MASK, int i) {
return (MASK >> i) & 1;
}
template <class T>
inline T ON(T MASK, int i) {
return MASK | (T(1) << i);
}
template <class T>
inline T OFF(T MASK, int i) {
return MASK & (~(T(1) << i));
}
template <typename T>
inline int CNT(T MASK) {
if (numeric_limits<T>::digits <= numeric_limits<unsigned int>::digits)
return __builtin_popcount(MASK);
else
return __builtin_popcountll(MASK);
}
template <class T>
inline int RIGHT(T MASK) {
return log2(MASK & -MASK);
}
int dx4[] = {0, 0, -1, +1};
int dy4[] = {+1, -1, 0, 0};
int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
inline void I(int& a) { scanf("%d", &a); }
inline void I(long long int& a) { scanf("%I64d", &a); }
inline void I(unsigned long long int& a) { scanf("%I64u", &a); }
inline void I(char* a) { scanf("%s", a); }
char Iarr[2000010];
inline void I(string& a) {
scanf("%s", Iarr);
a = Iarr;
}
template <typename T, typename... Args>
void I(T& a, Args&... args) {
I(a);
I(args...);
}
inline void OUT(int a) { printf("%d", a); }
inline void OUT(long long int a) { printf("%I64d", a); }
inline void OUT(const char* a) { printf("%s", a); }
inline void OUT(char* a) { printf("%s", a); }
inline void OUT(bool a) { printf("%d", a); }
inline void OUT(string a) {
for (__typeof(a.end()) it = (a.begin()) - ((a.begin()) > (a.end()));
it != (a.end()) - ((a.begin()) > (a.end()));
it += 1 - 2 * ((a.begin()) > (a.end())))
printf("%c", *it);
}
inline void OUT(unsigned long long int a) { printf("%I64u", a); }
template <typename T, typename... Args>
void OUT(T a, Args... args) {
OUT(a);
OUT(" ");
OUT(args...);
}
template <typename... Args>
void O(Args... args) {
OUT(args...);
OUT("\n");
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, pair<T1, T2> p) {
os << "{" << p.first << ", " << p.second << "}";
return os;
}
template <typename T>
inline ostream& operator<<(ostream& os, vector<T>& a) {
os << "[";
for (int i = 0; i < (int)a.size(); i++) {
if (i) os << ", ";
os << a[i];
}
os << "]";
return os;
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
const int M = 500;
int c, k;
int g[M][M];
int gg[M][M];
pair<int, int> space;
int nxtNum;
pair<int, int> nxtNumDest;
pair<int, int> nextt;
vector<pair<int, pair<int, int> > > ans;
vector<string> codes;
pair<int, int> init() {
for (int i = 1; i <= c; i++) {
if (g[1][i] == g[2][i] && g[1][i]) {
ans.push_back(make_pair(g[2][i], make_pair(1, i)));
codes.push_back(" init ");
if ((int)codes.size() > 20000)
cout << codes[(int)codes.size() - 1] << endl;
g[2][i] = 0;
}
if (g[3][i] == g[4][i] && g[3][i]) {
ans.push_back(make_pair(g[3][i], make_pair(4, i)));
codes.push_back(" init ");
if ((int)codes.size() > 20000)
cout << codes[(int)codes.size() - 1] << endl;
g[3][i] = 0;
}
}
for (int i = 1; i <= c; i++) {
if (!g[2][i]) return make_pair(2, i);
if (!g[3][i]) return make_pair(3, i);
}
int ret = inf;
for (int i = 1; i <= c; i++) {
if (g[2][i] + g[3][i]) ret = -1;
}
return make_pair(ret, ret);
}
pair<int, int> nxt() {
pair<int, int> ret = {-1, -1};
int dist = inf;
for (int i = 1; i <= c; i++) {
if (g[2][i]) {
int temp = abs(2 - space.first) + abs(i - space.second);
if (temp < dist) {
dist = temp;
ret = make_pair(2, i);
nxtNum = g[2][i];
}
}
if (g[3][i]) {
int temp = abs(3 - space.first) + abs(i - space.second);
if (temp < dist) {
dist = temp;
ret = make_pair(3, i);
nxtNum = g[3][i];
}
}
}
return ret;
}
void spaceToThis(int rr, int cc) {
if (g[rr][cc] == 0) {
space = make_pair(rr, cc);
return;
}
pair<int, int> ret = {-1, -1};
int dist = inf;
for (int i = 1; i <= c; i++) {
if (!g[2][i]) {
int temp = abs(2 - rr) + abs(i - cc);
if (2 == rr && nextt.first == 2) temp += 2;
if (temp < dist) {
dist = temp;
ret = make_pair(2, i);
}
}
if (!g[3][i]) {
int temp = abs(3 - rr) + abs(i - cc);
if (3 == rr && nextt.first == 3) temp += 2;
if (temp < dist) {
dist = temp;
ret = make_pair(3, i);
}
}
}
space = ret;
int cons = 0;
bool flagg = 0;
int cnt = 0;
pair<int, int> iniii = space;
if (dist >= 2) {
if (g[5 - nextt.first][nextt.second] == 0)
space = make_pair(5 - nextt.first, nextt.second);
}
while (space != make_pair(rr, cc)) {
int nxtRow =
space.first +
(((rr - space.first) > 0) ? 1 : (((rr - space.first) == 0) ? 0 : -1));
int nxtCol =
space.second +
(((cc - space.second) > 0) ? 1 : (((cc - space.second) == 0) ? 0 : -1));
if (make_pair(space.first, nxtCol) != nextt && g[space.first][nxtCol]) {
ans.push_back(make_pair(g[space.first][nxtCol], space));
codes.push_back(" spaceToThis ");
swap(g[space.first][nxtCol], g[space.first][space.second]);
space = make_pair(space.first, nxtCol);
if ((int)codes.size() > 20000)
cout << codes[(int)codes.size() - 1] << endl;
cons = 0;
} else if (make_pair(nxtRow, space.second) != nextt &&
g[nxtRow][space.second]) {
ans.push_back(make_pair(g[nxtRow][space.second], space));
codes.push_back(" spaceToThis ");
swap(g[nxtRow][space.second], g[space.first][space.second]);
space = make_pair(nxtRow, space.second);
if ((int)codes.size() > 20000)
cout << codes[(int)codes.size() - 1] << endl;
cons = 0;
} else if (nxtRow == rr) {
flagg = 1;
nxtRow = ((nxtRow == 2) ? 3 : 2);
if (make_pair(nxtRow, space.second) != nextt && g[nxtRow][space.second]) {
ans.push_back(make_pair(g[nxtRow][space.second], space));
codes.push_back(" spaceToThis ");
swap(g[nxtRow][space.second], g[space.first][space.second]);
space = make_pair(nxtRow, space.second);
if ((int)codes.size() > 20000)
cout << codes[(int)codes.size() - 1] << endl;
} else {
}
} else {
}
cnt++;
if (cnt >= 300) {
cout << cnt << endl;
cout << space << make_pair(rr, cc) << endl;
{
string _s = "g[3][34], g[2][34], iniii, nxtNum";
replace(_s.begin(), _s.end(), ',', ' ');
stringstream _ss(_s);
istream_iterator<string> _it(_ss);
err(_it, g[3][34], g[2][34], iniii, nxtNum);
};
}
}
}
void spaceToMyAid() {
if (nextt.first < nxtNumDest.first) {
spaceToThis(nextt.first + 1, nextt.second);
ans.push_back(make_pair(nxtNum, make_pair(nextt.first + 1, nextt.second)));
swap(g[nextt.first][nextt.second], g[nextt.first + 1][nextt.second]);
space = nextt;
nextt = make_pair(nextt.first + 1, nextt.second);
codes.push_back(" spaceToMyAid ");
if ((int)codes.size() > 20000) cout << codes[(int)codes.size() - 1] << endl;
} else if (nextt.first > nxtNumDest.first) {
spaceToThis(nextt.first - 1, nextt.second);
ans.push_back(make_pair(nxtNum, make_pair(nextt.first - 1, nextt.second)));
swap(g[nextt.first][nextt.second], g[nextt.first - 1][nextt.second]);
space = nextt;
nextt = make_pair(nextt.first - 1, nextt.second);
codes.push_back(" spaceToMyAid ");
if ((int)codes.size() > 20000) cout << codes[(int)codes.size() - 1] << endl;
} else if (nextt.second < nxtNumDest.second) {
spaceToThis(nextt.first, nextt.second + 1);
ans.push_back(make_pair(nxtNum, make_pair(nextt.first, nextt.second + 1)));
swap(g[nextt.first][nextt.second], g[nextt.first][nextt.second + 1]);
space = nextt;
nextt = make_pair(nextt.first, nextt.second + 1);
codes.push_back(" spaceToMyAid ");
if ((int)codes.size() > 20000) cout << codes[(int)codes.size() - 1] << endl;
} else {
spaceToThis(nextt.first, nextt.second - 1);
ans.push_back(make_pair(nxtNum, make_pair(nextt.first, nextt.second - 1)));
swap(g[nextt.first][nextt.second], g[nextt.first][nextt.second - 1]);
space = nextt;
nextt = make_pair(nextt.first, nextt.second - 1);
codes.push_back(" spaceToMyAid ");
if ((int)codes.size() > 20000) cout << codes[(int)codes.size() - 1] << endl;
}
}
void goNexttToDest() {
for (int i = 1; i <= c; i++) {
if (g[1][i] == nxtNum) {
nxtNumDest.first = 2;
nxtNumDest.second = i;
break;
}
if (g[4][i] == nxtNum) {
nxtNumDest.first = 3;
nxtNumDest.second = i;
break;
}
}
int cnt = 0;
while (nextt.first != nxtNumDest.first || nextt.second != nxtNumDest.second) {
spaceToMyAid();
cnt++;
if (cnt >= 300) {
cout << nextt << " " << nxtNumDest << endl;
}
}
g[nxtNumDest.first][nxtNumDest.second] = 0;
ans.push_back(make_pair(
nxtNum, make_pair(((nxtNumDest.first == 2) ? 1 : 4), nxtNumDest.second)));
codes.push_back(" goNexttToDest ");
if ((int)codes.size() > 20000) cout << codes[(int)codes.size() - 1] << endl;
}
int main() {
I(c, k);
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= c; j++) {
I(g[i][j]);
gg[i][j] = g[i][j];
}
}
space = init();
if (space.first == -1) {
O(-1);
exit(0);
} else if (space.first != inf) {
while (k--) {
nextt = nxt();
if (nextt.first == -1) {
break;
}
goNexttToDest();
}
}
O((int)ans.size());
int cnt = 0;
for (int i = 1; i <= c; i++) {
gg[1][i] = gg[4][i] = 0;
}
for (pair<int, pair<int, int> > v : ans) {
cnt++;
bool flag = 1;
for (int i = 2; i <= 3 && flag; i++) {
for (int j = 1; j <= c && flag; j++) {
if (gg[i][j] == v.first) {
if (abs(i - v.second.first) + abs(j - v.second.second) != 1) {
cout << cnt << codes[cnt - 1]
<< abs(i - v.second.first) + abs(j - v.second.second) << " "
<< make_pair(i, j)
<< make_pair(v.second.first, v.second.second) << endl;
}
swap(gg[i][j], gg[v.second.first][v.second.second]);
flag = 0;
}
}
}
}
for (pair<int, pair<int, int> > v : ans) {
O(v.first, v.second.first, v.second.second);
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const long long MOD = 1e9 + 7;
int dX[] = {0, 0, 1, -1};
int dY[] = {1, -1, 0, 0};
int n, m, k, cst[N], nearest[N];
long long ans;
bool block[N];
int calc(int S) {
int cur = 0, ret = 0;
while (cur < n) {
ret++;
if (cur + S >= n) return ret;
if (cur == nearest[cur + S]) return 0;
cur = nearest[cur + S];
}
return ret;
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
scanf("%d", &k);
int x;
for (int i = 0; i <= n; ++i) nearest[i] = i;
for (int i = 0; i < m; ++i) {
scanf("%d", &x);
if (x == 0) return puts("-1");
nearest[x] = nearest[x - 1];
}
for (int i = 1; i <= k; ++i) scanf("%d", &cst[i]);
ans = 4000000000000000100;
for (int i = 1; i <= k; ++i) {
int cnt = calc(i);
if (cnt) ans = min(ans, cnt * 1LL * cst[i]);
}
if (ans == 4000000000000000100)
puts("-1");
else
printf("%lld", ans);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
map<long long int, vector<long long int>> t;
int main() {
long long int n;
cin >> n;
map<long long int, long long int> deg;
map<long long int, long long int> p;
for (long long int i = 1; i <= n - 1; i++) {
long long int temp;
cin >> temp;
deg[temp]++;
p[i + 1] = temp;
}
map<long long int, long long int> v;
for (long long int i = 1; i <= n; i++) {
if (deg[i] == 0) v[p[i]]++;
}
long long int flag = 1;
for (long long int i = 1; i <= n; i++) {
if (v[i] < 3 && deg[i] != 0) {
flag = 0;
break;
}
}
if (flag)
cout << "Yes\n";
else
cout << "No\n";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> mp;
int cnt;
int n;
int num[100008];
int main() {
scanf("%d", &n);
int tmp;
for (int i = 1; i <= n; i++) {
scanf("%d", &tmp);
num[i] = tmp;
mp[tmp]++;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &tmp);
if (num[i] == tmp) {
cnt++;
}
mp[tmp]++;
}
long long ans = 1;
long long m;
scanf("%I64d", &m);
for (auto it : mp) {
for (int i = 1; i <= it.second; i++) {
long long t = i;
while (cnt > 0 && t % 2 == 0) {
t /= 2;
cnt--;
}
ans = ans * t % m;
}
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, t;
char s[205][205];
int main() {
cin >> t;
int i, j, k;
while (t--) {
cin >> n;
for (i = 1; i <= n; ++i) {
cin >> (s[i] + 1);
}
int cnt = 0;
bool i1 = 0, i2 = 0;
if (s[1][2] == s[2][1]) {
if (s[n][n - 1] == s[1][2]) {
++cnt;
i1 = 1;
}
if (s[n - 1][n] == s[1][2]) {
++cnt;
i2 = 1;
}
cout << cnt << endl;
if (i1) cout << n << " " << n - 1 << endl;
if (i2) cout << n - 1 << " " << n << endl;
continue;
}
if (s[1][2] != s[2][1]) {
if (s[n][n - 1] == s[n - 1][n]) {
cout << 1 << endl;
if (s[1][2] == s[n][n - 1]) {
cout << 1 << " " << 2 << endl;
} else
cout << 2 << " " << 1 << endl;
} else {
cout << 2 << endl;
cout << 2 << " " << 1 << endl;
if (s[n][n - 1] == s[1][2]) {
cout << n << " " << n - 1 << endl;
} else
cout << n - 1 << " " << n << endl;
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 500010;
long long int n, m;
vector<long long int> adj[N];
long long int val[N], vis[N];
long long int tree[N], graph[N];
long long int istree[N];
void dfs(long long int v, long long int p) {
vis[v] = 1;
tree[v] = 0;
graph[v] = 0;
istree[v] = 1;
for (auto u : adj[v]) {
if (u == p) {
continue;
}
if (vis[u]) {
istree[v] = 0;
continue;
}
dfs(u, v);
if (istree[u]) {
tree[v] = max(tree[v], tree[u]);
} else {
graph[v] += graph[u];
tree[v] = max(tree[v], tree[u]);
istree[v] = 0;
}
}
if (istree[v]) {
tree[v] += val[v];
} else {
graph[v] += val[v];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (long long int i = 1; i <= n; ++i) {
cin >> val[i];
vis[i] = 0;
}
for (long long int i = 0; i < m; ++i) {
long long int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
long long int start;
cin >> start;
dfs(start, 0);
cout << tree[start] + graph[start];
return (0);
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long int n, i, j, h, d[100001];
string s;
bool used[100001];
int main() {
cin >> n;
cin >> s;
for (i = 0; i < n; i++) cin >> d[i];
long long int x = 0;
while (x >= 0 && x < n) {
if (used[x]) {
cout << "INFINITE" << endl;
return 0;
}
used[x] = true;
if (s[x] == '>')
x += d[x];
else
x -= d[x];
}
cout << "FINITE" << endl;
}
| 2 |
#include <bits/stdc++.h>
constexpr auto Inf = 0X3F3F3F3F;
using namespace std;
namespace IO {
inline long long read() {
long long o = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c > '/' && c < ':') {
o = o * 10 + c - '0';
c = getchar();
}
return o * f;
}
inline char recd() {
char o;
while ((o = getchar()) < '0' || o > '9')
;
return o;
}
inline double reod() {
double o = read(), f = 1;
char c;
while ((c = getchar()) > '/' && c < ':') o += (c - '0') / (f *= 10);
return o;
}
} // namespace IO
using namespace IO;
const int SIZE = 1E5 + 7, Mod = 1E9 + 7;
long long a[] = {0,
9,
90,
900,
9000,
90000,
900000,
9000000,
90000000,
900000000,
9000000000,
90000000000,
900000000000,
9000000000000,
90000000000000,
900000000000000,
9000000000000000,
90000000000000000};
long long _pow(long long A, long long B) {
long long res = 1;
while (B) {
if (B & 1) res *= A;
A *= A, B >>= 1;
}
return res;
}
long long _log10(long long w) {
int res = 0;
while (w) res++, w /= 10;
return res;
}
long long F(long long w) {
if (!w) return 0;
long long res = 0, S = 1, dig = log10(w) + 1;
for (int pos = 1; pos < dig; pos++) {
res += (S + S + (a[pos] - 1) * pos) * a[pos] / 2;
S = S + (a[pos] - 1) * pos + pos + 1;
}
long long now = _pow(10, dig - 1);
res += (S + S + (w - now) * dig) * (w - now + 1) / 2;
return res;
}
int main() {
int T = read();
while (T--) {
long long L = 1, R = 5E8, res = 0, M, N = read();
while (L <= R) {
M = (L + R) >> 1;
F(M) < N ? (res = M, L = M + 1) : (R = M - 1);
}
N -= F(res), res += 1;
int Ind = 1;
while (N > a[Ind] * Ind) N -= a[Ind] * Ind, Ind++;
long long ans = pow(10, Ind - 1) + N / Ind;
N %= Ind;
string e = to_string(ans);
if (!N)
printf("%d\n", (ans - 1) % 10);
else
printf("%c\n", e[N - 1]);
}
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100002;
int n, s;
int buy[maxn];
int sell[maxn];
int main() {
memset(buy, 0, sizeof(buy));
memset(sell, 0, sizeof(sell));
cin >> n >> s;
char d;
int p, q, i, t = 0, bp = 0;
for (i = 0; i < n; ++i) {
cin >> d >> p >> q;
if (d == 'B') {
buy[p] += q;
if (p > bp) bp = p;
} else
sell[p] += q;
}
for (i = 0; i <= 100000; i++) {
if (t < s && sell[i]) {
t++;
p = i;
} else if (t >= s)
break;
}
for (i = p; i >= 0; i--) {
if (sell[i])
cout << "S"
<< " " << i << " " << sell[i] << endl;
}
t = 0;
for (i = bp; i >= 0; i--) {
if (t < s && buy[i]) {
cout << "B"
<< " " << i << " " << buy[i] << endl;
t++;
}
if (t >= s) break;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int width = 0;
int n, h;
cin >> n >> h;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (arr[i] <= h)
width += 1;
else
width += 2;
}
cout << width << "\n";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long int gcd(long long int x, long long int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int logx(long long int base, long long int num) {
int cnt = 0;
while (num != 1) {
num /= base;
++cnt;
}
return cnt;
}
vector<long long int> vin(long long int n) {
vector<long long int> a(n);
for (long long int i = 0; i < n; i++) cin >> a[i];
return a;
}
void out(vector<long long int> a) {
for (int i = 0; i < (long long int)a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
void in(long long int *a, long long int n) {
for (long long int i = 0; i < n; i++) cin >> a[i];
}
void out1(long long int *a, long long int n) {
for (long long int i = 1; i < n + 1; i++) {
cout << a[i] << " ";
}
cout << '\n';
}
void out1(vector<long long int> v) {
for (long long int i = 0; i < (long long int)v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
const long long MAX_SIZE = 10000001;
vector<long long> isprime(MAX_SIZE, true);
vector<long long> prime;
vector<long long> SPF(MAX_SIZE);
void seive(long long N) {
isprime[0] = isprime[1] = false;
for (long long i = 2; i < N; i++) {
if (isprime[i]) {
prime.push_back(i);
SPF[i] = i;
}
for (long long j = 0;
j < (long long)prime.size() && i * prime[j] < N && prime[j] <= SPF[i];
j++) {
isprime[i * prime[j]] = false;
SPF[i * prime[j]] = prime[j];
}
}
}
int main() {
fast();
long long int n;
cin >> n;
long long int a[n];
in(a, n);
seive(10000001);
vector<long long int> res1;
vector<long long int> res2;
for (long long int i = 0; i < n; i++) {
long long int k1 = -1;
long long int k2 = -1;
long long int cur = a[i];
while (true) {
if (SPF[cur] == 0)
break;
else {
long long int p = SPF[cur];
while (cur % p == 0) {
cur /= p;
if (k1 == -1) {
k1 = p;
} else {
k1 *= p;
}
}
break;
}
}
if (k1 != -1) {
if (a[i] / k1 != 1) {
res1.push_back(k1);
res2.push_back(a[i] / k1);
} else {
res1.push_back(-1);
res2.push_back(-1);
}
} else {
res1.push_back(-1);
res2.push_back(-1);
}
}
for (long long int i = 0; i < n; i++) cout << res1[i] << " ";
cout << '\n';
for (long long int i = 0; i < n; i++) cout << res2[i] << " ";
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string a, b;
cin >> a >> b;
string ans = "zzzzzzzzzzz";
for (long long int i = 0; i < a.length(); i++) {
string temp;
temp = a.substr(0, i + 1) + b.substr(0, 1);
if (temp < ans) ans = temp;
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T min3(T a, T b, T c) {
return min(a, min(b, c));
};
template <class T>
T max3(T a, T b, T c) {
return max(a, max(b, c));
};
void openFile() {
freopen("/home/khaihanhdk/MyWorkplace/cpp file/a.inp", "r", stdin);
}
int main() {
long long A, B, y, g, b;
scanf("%I64d%I64d", &A, &B);
scanf("%I64d%I64d%I64d", &y, &g, &b);
A -= y * 2LL;
A -= g;
B -= g;
B -= 3LL * b;
long long res = 0;
if (A < 0LL) res -= A;
if (B < 0LL) res -= B;
cout << res;
}
| 0 |
#include <bits/stdc++.h>
long long n, a[2010], b[5010], c[10010];
double ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
b[((a[i] - a[j]) > 0 ? (a[i] - a[j]) : -(a[i] - a[j]))]++;
for (int i = 1; i <= 5000; i++)
for (int j = 1; j <= 5000; j++) c[i + j] += b[i] * b[j];
for (int i = 1; i <= 10000; i++) c[i] += c[i - 1];
int t = n * (n - 1) / 2;
for (int i = 5000; i >= 2; i--) ans += (double)b[i] * c[i - 1] / t;
printf("%.10lf", ans / t / t);
}
| 10 |
#include <bits/stdc++.h>
int main() {
char ary1[102], ary2[102];
scanf("%s%s", ary1, ary2);
if (strcmp(ary1, ary2) == 0) {
printf("%s\n", ary1);
} else {
printf("1\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int tonum(string s) {
int ans = 0;
for (int i = 0; i < s.size(); i++) {
ans = ans * 10 + (s[i] - '0');
}
return ans;
}
int main() {
string a;
cin >> a;
int n = a.size();
int sum[500];
int mice = 499;
memset(sum, 0, sizeof sum);
for (int i = 0, j; i < n; i++) {
if (isalpha(a[i])) continue;
string here;
for (j = i; j < n && isdigit(a[j]) || a[j] == '.'; j++) {
here += a[j];
}
i = j - 1;
string emp;
int ce = 499;
bool did = 0, dot = 0;
for (int j = 0; j < here.size(); j++) {
if (here[j] == '.') {
dot = 1;
break;
}
}
for (int j = here.size() - 1; j >= 0; j--) {
if (here[j] == '.') {
if (!did && (emp.size() > 2 || !dot)) ce--;
did = 1;
sum[ce] += tonum(emp);
emp = "";
if (sum[ce] >= (ce == 499 ? 100 : 1000)) mice = min(mice, ce - 1);
mice = min(mice, ce);
sum[ce - 1] += sum[ce] / (ce == 499 ? 100 : 1000);
sum[ce] %= (ce == 499 ? 100 : 1000);
ce--;
} else {
emp = here[j] + emp;
}
}
if (!emp.empty()) {
if (!did) ce--;
sum[ce] += tonum(emp);
if (sum[ce] >= (ce == 499 ? 100 : 1000)) mice = min(mice, ce - 1);
mice = min(mice, ce);
sum[ce - 1] += sum[ce] / (ce == 499 ? 100 : 1000);
sum[ce] %= (ce == 499 ? 100 : 1000);
ce--;
}
}
for (int i = 499; i >= 0; i--) {
if (sum[i] >= (i == 499 ? 100 : 1000)) {
mice = min(mice, i - 1);
}
sum[i - 1] += sum[i] / (i == 499 ? 100 : 1000);
sum[i] %= (i == 499 ? 100 : 1000);
}
bool done = 0;
for (int i = mice; i < 500 - (!sum[499]); i++) {
if (!done) {
printf("%d", sum[i]);
done = 1;
} else {
if (i == 499) {
printf(".%02d", sum[i]);
} else {
printf(".%03d", sum[i]);
}
}
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
struct atom {
int a, b, c;
unsigned int w;
};
unordered_map<long long, int> H[600];
int n, len, a, b, c, mo = (1 << 30), sign;
vector<int> prime;
int dp(int kk1, int kk2, int kk3, int kk4) {
if (kk4 == -1) return 1;
long long now = 1ll * kk1 * (a + 1) * (a + 1) + kk2 * (a + 1) + kk3,
num = H[kk4][now];
if (num != 0) return num;
int ans = 0;
int k1 = 1, k2 = 1, k3 = 1, k4 = 0;
while (k1 <= kk1) {
k2 = 1;
int k5 = 0;
while (k2 <= kk2) {
k3 = 1;
int k6 = 0;
while (k3 <= kk3) {
ans += dp(kk1 / k1, kk2 / k2, kk3 / k3, kk4 - 1) * (k4 + k5 + k6 + 1);
k3 = k3 * prime[kk4];
k6++;
}
k2 = k2 * prime[kk4];
k5++;
}
k1 = k1 * prime[kk4];
k4++;
}
H[kk4][now] = ans;
return ans;
}
int main() {
scanf("%d%d%d", &a, &b, &c);
if (a < b) swap(a, b);
if (a < c) swap(a, c);
for (int i = 2; i <= a; i++) {
int flag = 0;
for (int j = 2; j <= trunc(sqrt(i)); j++)
if (i % j == 0) {
flag = 1;
break;
}
if (flag == 0) prime.push_back(i);
}
cout << (dp(a, b, c, prime.size() - 1) % mo + mo) % mo << endl;
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
ostream &operator<<(ostream &out, string str) {
for (char c : str) out << c;
return out;
}
template <class L, class R>
ostream &operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template <class T>
auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for (auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template <class T, class... Ts>
void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
template <class T>
int size(T &&a) {
return a.size();
}
using LL = long long;
using PII = pair<int, int>;
struct ans {
int score;
int l, r;
ans() { score = 0, l = 0, r = 0; }
void update(int s, int L, int R) {
if (s > score) score = s, l = L, r = R;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
string str;
cin >> n >> str;
vector<int> d(n);
int cnt = 0;
for (int i = 0; i < n; i++) {
d[i] = (str[i] == '(' ? 1 : -1);
if (d[i] == 1) cnt++;
}
if (cnt * 2 != n) {
cout << "0\n1 1\n";
return 0;
}
vector<int> s(n);
int mn = 0;
for (int i = 0; i < n; i++) {
if (i) s[i] = s[i - 1];
s[i] += d[i];
mn = min(mn, s[i]);
}
vector<vector<int>> q(n, vector<int>(3));
for (int i = 0; i < n; i++) {
if (i) q[i] = q[i - 1];
int r = s[i] - mn;
if (r <= 2) q[i][r]++;
}
ans odp;
cnt = 0;
for (int i = 0; i < n; i++)
if (s[i] == mn) cnt++;
odp.update(cnt, 0, 0);
auto go_up = [&]() {
if (mn < 0) {
int l = n, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] == mn) {
l = min(l, i);
r = max(r, i);
}
}
int p1 = q[l][1] + q[n - 1][1] - q[r][1];
odp.update(p1, l, r + 1);
}
if (mn < -1) {
int l = n, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] <= mn + 1) {
l = min(l, i);
r = max(r, i);
}
}
int p2 = q[l][2] + q[n - 1][2] - q[r][2] + q[n - 1][0];
odp.update(p2, l, r + 1);
}
};
go_up();
auto go_down = [&]() {
int l = -1, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] >= 2 + mn) {
if (l == -1) l = i;
r = i;
int p0 = q[n - 1][0] + q[r][2] - (l == 0 ? 0 : q[l - 1][2]);
odp.update(p0, l, r + 1);
} else
l = -1, r = -1;
}
l = -1, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] >= 1 + mn) {
if (l == -1) l = i;
r = i;
int p1 = q[r][1] - (l == 0 ? 0 : q[l - 1][1]);
odp.update(p1, l, r + 1);
} else
l = -1, r = -1;
}
};
go_down();
cout << odp.score << "\n" << odp.l + 1 << " " << odp.r + 1 << "\n";
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXLINE = 1000;
const int MAXLINELEN = 4 + 1 + 4 + 1 + 4 + 1;
struct Node {
int a;
char op;
int b;
Node() {}
Node(int a, char op, int b) : a(a), op(op), b(b) {}
};
bool operator==(const Node &p, const Node &q) {
return p.a == q.a && p.op == q.op && p.b == q.b;
}
int nline;
char line[MAXLINE][MAXLINELEN + 1];
int nans;
char ans[MAXLINE][MAXLINELEN + 1];
vector<string> names;
map<string, int> mp;
int lhs[MAXLINE], lpar[MAXLINE];
char op[MAXLINE];
int rpar[MAXLINE];
vector<Node> vars;
vector<int> curvar;
vector<bool> valid;
vector<bool> used;
vector<string> newnames;
int lookup(string s) {
auto it = mp.find(s);
if (it != mp.end()) return it->second;
int ret = ((int)(names).size());
names.push_back(s);
mp[s] = ret;
return ret;
}
void printvar(int idx) {
if (vars[idx].op == '=')
printf("%s", names[vars[idx].a].c_str());
else {
printf("(");
printvar(vars[idx].a);
printf("%c", vars[idx].op);
printvar(vars[idx].b);
printf(")");
}
}
void solve() {
names.clear();
mp.clear();
int resname = lookup("res");
for (int i = (0); i < (nline); ++i) {
int eqidx = -1, opidx = -1, linelen = strlen(line[i]);
for (int j = (0); j < (linelen); ++j)
if (line[i][j] == '=') {
assert(eqidx == -1);
eqidx = j;
} else if (line[i][j] == '$' || line[i][j] == '^' || line[i][j] == '#' ||
line[i][j] == '&') {
assert(opidx == -1);
opidx = j;
}
assert(eqidx != -1);
if (opidx == -1) {
string a = string(line[i] + 0, line[i] + eqidx),
b = string(line[i] + eqidx + 1, line[i] + linelen);
lhs[i] = lookup(a);
lpar[i] = lookup(b);
op[i] = '=';
} else {
string a = string(line[i] + 0, line[i] + eqidx),
b = string(line[i] + eqidx + 1, line[i] + opidx),
c = string(line[i] + opidx + 1, line[i] + linelen);
lhs[i] = lookup(a);
lpar[i] = lookup(b);
op[i] = line[i][opidx];
rpar[i] = lookup(c);
}
}
vars.clear();
for (int i = (0); i < (((int)(names).size())); ++i)
vars.push_back(Node(i, '=', -1));
curvar = vector<int>(((int)(names).size()));
for (int i = (0); i < (((int)(curvar).size())); ++i) curvar[i] = i;
for (int i = (0); i < (nline); ++i) {
if (op[i] == '=') {
curvar[lhs[i]] = curvar[lpar[i]];
} else {
int id = ((int)(vars).size());
vars.push_back(Node(curvar[lpar[i]], op[i], curvar[rpar[i]]));
curvar[lhs[i]] = id;
}
}
int resvar = curvar[resname];
valid = vector<bool>(((int)(vars).size()), true);
for (int i = (0); i < (((int)(vars).size())); ++i)
if (valid[i]) {
for (int j = (i + 1); j < (((int)(vars).size())); ++j)
if (valid[j] && vars[j] == vars[i]) {
valid[j] = false;
if (resvar == j) resvar = i;
for (int k = (j + 1); k < (((int)(vars).size())); ++k) {
if (vars[k].a == j) vars[k].a = i;
if (vars[k].b == j) vars[k].b = i;
}
}
}
used = vector<bool>(((int)(vars).size()), false);
queue<int> q;
used[resvar] = true;
q.push(resvar);
while (!q.empty()) {
int at = q.front();
q.pop();
if (vars[at].op == '=') continue;
if (!used[vars[at].a]) used[vars[at].a] = true, q.push(vars[at].a);
if (!used[vars[at].b]) used[vars[at].b] = true, q.push(vars[at].b);
}
newnames = vector<string>(((int)(vars).size()));
int nxtnewname = 0;
set<string> reservednames;
reservednames.insert("res");
if (vars[resvar].op != '=') newnames[resvar] = "res";
for (int i = (0); i < (((int)(vars).size())); ++i)
if (valid[i] && used[i] && vars[i].op == '=')
newnames[i] = names[vars[i].a], reservednames.insert(newnames[i]);
for (int i = (0); i < (((int)(vars).size())); ++i)
if (valid[i] && used[i] && i != resvar && vars[i].op != '=')
while (true) {
int a = nxtnewname / 26 / 27, b = nxtnewname / 26 % 27,
c = nxtnewname % 26;
++nxtnewname;
string name;
if (a != 0) name += string(1, 'a' + a - 1);
if (b != 0) name += string(1, 'a' + b - 1);
name += string(1, 'a' + c);
if (reservednames.count(name)) continue;
newnames[i] = name;
break;
}
nans = 0;
for (int i = (0); i < (((int)(vars).size())); ++i)
if (valid[i] && used[i] && vars[i].op != '=') {
string cur = newnames[i] + '=' + newnames[vars[i].a] + vars[i].op +
newnames[vars[i].b];
sprintf(ans[nans++], "%s", cur.c_str());
}
if (vars[resvar].op == '=' && newnames[vars[resvar].a] != "res") {
string cur = "res=" + newnames[vars[resvar].a];
sprintf(ans[nans++], "%s", cur.c_str());
}
}
void run() {
scanf("%d", &nline);
for (int i = (0); i < (nline); ++i) scanf("%s", line[i]);
solve();
printf("%d\n", nans);
for (int i = (0); i < (nans); ++i) printf("%s\n", ans[i]);
}
int main() {
run();
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
template <class T>
inline bool Min(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
inline bool Max(T &a, T b) {
return a < b ? (a = b, true) : false;
}
inline int ni() {
int a;
scanf("%d", &a);
return a;
}
inline ll nl() {
ll a;
scanf("%lld", &a);
return a;
}
inline double nd() {
double a;
scanf("%lf", &a);
return a;
}
inline void pi(int a) { printf("%d ", a); }
inline void pl(ll a) { printf("%lld ", a); }
inline void pd(double a) { printf("%.12lf ", a); }
void solve() {
int n;
cin >> n;
for (int i = 0; i < (int)(n); ++i) {
string s;
cin >> s;
int year = atoi(s.substr(4).c_str());
int len = int(s.size()) - 4;
int ten = 10;
int sum = 0;
for (int k = 1; k < len; k++) {
sum += ten;
ten *= 10;
}
while (year < 1989 + sum) year += ten;
cout << year << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vpll = vector<pair<ll, ll>>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
const int maxn = 2e5 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, ans = 0;
cin >> n;
pi a[maxn];
for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second;
sort(a + 1, a + n + 1);
vector<int> tv1, tv2;
for (int i = 1; i <= n; ++i) {
if (tv1.empty() || a[tv1.back()].second < a[i].first) {
tv1.push_back(i);
continue;
}
if (tv2.empty() || a[tv2.back()].second < a[i].first) tv2.push_back(i);
}
if (tv1.size() + tv2.size() == n)
cout << "YES";
else
cout << "NO";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline bool ok(long long &mid, vector<long long> &a, long long &n,
long long &m) {
long long temp = 0;
for (long long j = 0; j < mid; j++) {
long long sub = 0;
for (long long k = j; k < n; k += mid) {
temp += max(0ll, a[k] - sub);
sub++;
}
}
if (temp >= m) {
return true;
}
return false;
}
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> a(n);
long long sum = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum < m) {
cout << -1;
return;
}
if (sum == m) {
cout << n;
return;
}
long long xtra = sum - m;
sort(a.rbegin(), a.rend());
long long l = 0, r = n + 1;
while (l + 1 < r) {
long long mid = (l + r) / 2;
if (ok(mid, a, n, m)) {
r = mid;
} else
l = mid;
}
cout << l + 1;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, v, a;
cin >> n >> v;
if (v < n - 1)
a = (v - 1) + ((n - v) * (n - v + 1)) / 2;
else
a = n - 1;
cout << a;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double compute(double w, double b);
double dp[1010][1010];
int main() {
double w, b;
cin >> w >> b;
for (int i = 0; i < 1010; i++) {
for (int j = 0; j < 1010; j++) dp[i][j] = -1;
}
dp[(int)w][(int)b] = compute(w, b);
double ans = dp[(int)w][(int)b];
printf("%.9lf\n", ans);
return 0;
}
double compute(double w, double b) {
if (w <= 0) return 0;
if (b <= 0) return 1;
if (dp[(int)w][(int)b] != -1) return dp[(int)w][(int)b];
double temp1, temp2, ret;
ret = (w) / (w + b);
if (b == 0) return dp[(int)w][(int)b];
if (w + b - 2 > 0)
temp1 = compute(w, b - 3) * ((b - 2) / (w + b - 2));
else
temp1 = 0;
if (w + b - 2 > 0)
temp2 = compute(w - 1, b - 2) * (w / (w + b - 2));
else
temp2 = 0;
ret += ((b) / (w + b)) * ((b - 1) / (w + b - 1)) * (temp1 + temp2);
dp[(int)w][(int)b] = ret;
return dp[(int)w][(int)b];
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long val[300007];
long long mask[300007];
void input() {
cin >> n;
long long sm = 0;
for (int i = 1; i <= n; ++i) {
cin >> val[i] >> mask[i];
sm += val[i];
}
if (sm > 0) {
for (int i = 1; i <= n; ++i) {
val[i] = -val[i];
}
}
}
void solve() {
long long ans = 0;
for (long long bit = 0; bit < 62; ++bit) {
long long sm = 0;
long long aux = (1LL << bit);
for (int i = 1; i <= n; ++i) {
if ((mask[i] & aux) > 0 && 2 * aux > mask[i]) {
sm += val[i];
}
}
if (sm < 0) {
ans += aux;
for (int i = 1; i <= n; ++i) {
if ((mask[i] & aux) > 0) {
val[i] = -val[i];
}
}
}
}
cout << ans << "\n";
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
while (t--) {
input();
solve();
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int ans = 0, a = 0;
while (getline(cin, s)) {
if (s[0] == '+')
a++;
else {
if (s[0] == '-')
a--;
else
ans += a * (s.size() - s.find(':') - 1);
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2, s3;
char winer = '0';
int cnt = 0;
cin >> s1 >> s2 >> s3;
if ((s1[0] == 'p' && s2[0] == 'r' && s3[0] == 'r') ||
(s1[0] == 'r' && s2[0] == 's' && s3[0] == 's') ||
(s1[0] == 's' && s2[0] == 'p' && s3[0] == 'p')) {
winer = 'F';
cnt++;
}
if ((s1[0] == 'r' && s2[0] == 'p' && s3[0] == 'r') ||
(s1[0] == 's' && s2[0] == 'r' && s3[0] == 's') ||
(s1[0] == 'p' && s2[0] == 's' && s3[0] == 'p')) {
winer = 'M';
cnt++;
}
if ((s1[0] == 'r' && s2[0] == 'r' && s3[0] == 'p') ||
(s1[0] == 's' && s2[0] == 's' && s3[0] == 'r') ||
(s1[0] == 'p' && s2[0] == 'p' && s3[0] == 's')) {
winer = 'S';
cnt++;
}
if (cnt != 1)
cout << "?" << endl;
else
cout << winer << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void rj_king() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void solve() {
long long mods[5] = {0, 0, 0, 0, 0};
long long ans = 0, n, m;
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
mods[i % 5]++;
}
for (long long i = 1; i <= n; i++) {
ans += mods[(5 - i % 5) % 5];
}
cout << ans;
}
int32_t main() {
rj_king();
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long i, j, k, x, y, z, n, mi = 1e16, sum = 0;
cin >> n;
long long a[n];
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
if (n == 1 || n == 2) {
cout << a[0] - 1;
return;
}
z = (long long)pow(1e18, 1.0 / (n - 1));
if (n == 3) {
z = (long long)pow(10, 7);
}
for (i = 1; i <= z; i++) {
sum = 0;
for (j = 0; j < n; j++) {
y = (long long)pow(i, j);
sum += abs(a[j] - y);
}
mi = min(mi, sum);
}
cout << mi;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t-- > 0) {
solve();
}
}
| 7 |
#include <bits/stdc++.h>
template <typename C>
int sz(const C &c) {
return c.size();
}
using namespace std;
int main() {
iostream::sync_with_stdio(false);
int n;
cin >> n;
map<string, string> d;
for (int i = 0; i < n; ++i) {
string p;
cin >> p;
string q;
cin >> q;
auto it = d.find(p);
if (it != end(d)) {
d[q] = it->second;
d.erase(it->first);
} else {
d[q] = p;
}
}
cout << sz(d) << "\n";
for (const auto &[k, v] : d) {
cout << v << " " << k << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e3 + 10;
vector<int> adj[N];
void bfs(int n, int src, vector<int>& dist) {
vector<bool> vis(n + 1, false);
dist[src] = 0;
vis[src] = true;
queue<int> q;
q.push(src);
while (!q.empty()) {
int node = q.front();
q.pop();
for (auto child : adj[node]) {
if (!vis[child]) {
dist[child] = 1 + dist[node];
vis[child] = true;
q.push(child);
}
}
}
}
void solve() {
int n, m, s, t;
scanf("%d", &n), scanf("%d", &m), scanf("%d", &s), scanf("%d", &t);
vector<vector<bool>> check(n + 1, vector<bool>(n + 1, false));
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d", &u), scanf("%d", &v);
adj[u].push_back(v);
adj[v].push_back(u);
check[u][v] = check[v][u] = true;
}
vector<int> d1(n + 1, 0), d2(n + 1, 0);
bfs(n, s, d1);
bfs(n, t, d2);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if ((d1[i] + d2[j] + 1) >= d1[t] && (d2[i] + d1[j] + 1) >= d2[s] &&
!check[i][j]) {
ans++;
}
}
}
printf("%d\n", ans);
}
int main() {
int t = 1;
while (t--) {
solve();
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> a[500];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].second;
long long x = a[i].second;
while (x % 3 == 0) {
a[i].first--;
x /= 3;
}
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) cout << a[i].second << " ";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
const double EPS = 1e-8;
struct debugger {
template <typename T>
debugger& operator,(const T& v) {
cerr << v << " ";
return *this;
}
} dbg;
int d, n, m;
vector<pair<int, int> > arr;
int main() {
std::ios_base::sync_with_stdio(false);
cin >> d >> n >> m;
arr.resize(m);
for (int i = 0; i < m; i++) {
cin >> arr[i].first >> arr[i].second;
}
arr.emplace_back(0, 0);
arr.emplace_back(d, 0);
sort(arr.begin(), arr.end());
m += 2;
vector<int> Mi(m, m - 1);
stack<int> stk;
for (int i = 0; i < m; i++) {
while (!stk.empty() && arr[stk.top()].second >= arr[i].second) {
Mi[stk.top()] = i;
stk.pop();
}
stk.push(i);
}
int fuel = 0;
long long res = 0;
for (int i = 0; i < m; i++) {
if (i) {
if (arr[i].first - arr[i - 1].first > n) {
cout << -1 << endl;
return 0;
}
fuel -= (arr[i].first - arr[i - 1].first);
}
int dist = min(n, arr[Mi[i]].first - arr[i].first);
if (fuel >= dist) {
continue;
} else {
res += (long long)(dist - fuel) * arr[i].second;
fuel = dist;
}
}
cout << res << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long res, p2[510], preq[510][510], f;
int n, k, r;
int main() {
cin >> n >> k;
res = 0;
p2[0] = 1;
for (int i = 1; i <= n; i++) {
p2[i] = (p2[i - 1] * 2LL) % 998244353LL;
}
for (int i = 1; i < 510; i++) {
for (int j = 1; j < 510; j++) {
preq[i][j] = 0;
}
}
for (int i = 0; i < 510; i++) {
preq[0][i] = -1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
for (int v = 1; v <= i - j + 1; v++) {
f = ((p2[v - 1] - preq[v - 1][j]) * p2[i - v - j + 1]) % 998244353LL;
preq[i][j] = (preq[i][j] + f + 998244353LL) % 998244353LL;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i * j) < k) {
long long pi =
(preq[n][i] - preq[n][i + 1] + 998244353LL) % 998244353LL;
long long pj =
(preq[n][j] - preq[n][j + 1] + 998244353LL) % 998244353LL;
res = (res + (long long)(pi * pj)) % 998244353LL;
}
}
}
res = (res * 499122177LL) % 998244353LL;
cout << res << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T, typename TT>
inline ostream &operator<<(ostream &os, const pair<T, TT> &t) {
return os << t.first << " " << t.second;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &t) {
for (auto i : t) os << i << " ";
return os;
}
template <typename T>
inline istream &operator>>(istream &is, vector<T> &v) {
for (T &t : v) is >> t;
return is;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &is, pair<T1, T2> &t) {
is >> t.first >> t.second;
return is;
}
const long long mod = 1e9 + 7;
void solve() {
long long n, m;
cin >> n >> m;
vector<long long> v(n);
cin >> v;
if (n < 3 or m < n) {
cout << -1 << '\n';
return;
}
cout << 2 * accumulate(v.begin(), v.end(), 0ll) << '\n';
for (int i = 1; i < n; i++) cout << i << " " << i + 1 << '\n';
cout << 1 << " " << n << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
;
long long T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int b, p, f, x, y, z;
cin >> b >> p >> f;
int h, c;
cin >> h >> c;
x = b / 2;
y = p + f;
if (x >= y) {
z = p * h + f * c;
} else {
if (h >= c) {
if (p <= x) {
z = h * p;
x = x - p;
z += x * c;
} else {
z = h * x;
}
} else {
if (f <= x) {
z = c * f;
x = x - f;
z += x * h;
} else {
z = c * x;
}
}
}
cout << z;
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int getint() {
int _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
bool equal(double _x, double _y) { return _x > _y - 1e-7 && _x < _y + 1e-7; }
int __ = 1, cs;
char c[500010];
int len;
double ans, dp[500010], cnt[500010];
double BIT[500010];
int lb(int x) { return x & (-x); }
void insert(int no, double dlt) {
while (no <= len) {
BIT[no] += dlt;
no += lb(no);
}
}
double query(int no) {
double sum = 0;
while (no) {
sum += BIT[no];
no -= lb(no);
}
return sum;
}
bool vowel(char ctmp) {
return ctmp == 'I' || ctmp == 'E' || ctmp == 'A' || ctmp == 'O' ||
ctmp == 'U' || ctmp == 'Y';
}
void build() {}
void init() {
scanf("%s", c + 1);
len = strlen(c + 1);
}
void solve() {
for (int i = 1; i <= len; i++)
if (vowel(c[i])) {
int lft = i, rgt = len - i + 1;
int tmp = min(lft, rgt);
ans += (double)tmp;
int low = tmp + 1, high = len + 1 - tmp;
if (low <= high) {
insert(low, tmp);
insert(high + 1, -tmp);
}
int st = len + 2 - tmp;
dp[st] += tmp - 1.0;
cnt[st] += 1.0;
}
for (int i = 1; i <= len; i++) {
double tsum = query(i);
ans += tsum / (double)i;
}
for (int i = 1; i <= len; i++) {
ans += dp[i] / (double)i;
dp[i + 1] += dp[i] - cnt[i];
cnt[i + 1] += cnt[i];
}
printf("%.20f\n", ans);
}
int main() {
build();
while (__--) {
init();
solve();
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const long long IINF = 9223372036854775807;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int N = 105;
inline void read(int &x) {
int f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
x *= f;
}
int n, k, ans;
int main() {
read(n), read(k);
ans = n * 3 + min(k - 1, n - k);
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, n, m, t, test, a, r, c, temp;
int row[1010], col[1010];
char str;
while (cin >> n >> m >> test) {
vector<int> v[1010];
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
scanf(" %d", &a);
v[i].push_back(a);
}
for (i = 0; i < n; i++) row[i] = i;
for (i = 0; i < m; i++) col[i] = i;
for (t = 1; t <= test; t++) {
scanf(" %c %d %d", &str, &r, &c);
r--;
c--;
if (str == 'r') {
swap(row[r], row[c]);
} else if (str == 'c') {
swap(col[r], col[c]);
} else if (str == 'g') {
printf("%d\n", v[row[r]][col[c]]);
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, p, w, d;
cin >> n >> p >> w >> d;
long long above = n * w - p;
if (p == 0) {
cout << "0 0 " << n << endl;
} else if (above < 0 || d > p) {
cout << -1 << endl;
} else {
long long xx = p / w;
for (long long k = 0; k <= 1e6; k++) {
long long x = xx - k;
if (x < 0) break;
long long sheng = p - x * w;
if (sheng % d != 0) continue;
long long y = sheng / d;
if (x + y > n) continue;
cout << x << " " << y << " " << n - x - y << endl;
return 0;
}
cout << -1 << endl;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000007;
const int mx = 1e5 + 10;
const int mxt = 1e6 + 10;
long long int d[mx];
long long int t[mxt];
void upd(int idx, long long int v) {
for (; idx < mxt; idx += (idx & -idx)) t[idx] = (t[idx] + v) % mod;
}
long long int query(int idx) {
long long int ans = 0;
for (; idx > 0; idx -= (idx & -idx)) ans = (ans + t[idx]) % mod;
return ans;
}
inline long long int range(int li, int ls) {
return (query(ls) - query(li - 1) + mod) % mod;
}
inline long long int gat(int idx) {
return (query(idx) - query(idx - 1) + mod) % mod;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> d[i];
memset(t, 0, sizeof t);
long long int ans = 0;
for (int i = n; i > 0; i--) {
long long int v = (d[i] + d[i] * range(d[i], mxt - 1)) % mod;
long long int delta = (v - gat(d[i]) + mod) % mod;
upd(d[i], delta);
ans = (ans + delta) % mod;
}
cout << ans << endl;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200009, Q = 2E5 + 5, MOD = 998244353;
int n, q, l, r, len;
char s[N];
long long pw[N];
pair<long long, int> ans[Q][2];
vector<pair<int, int>> que[N];
struct segment_tree {
long long add[4 * N];
int mul[4 * N], tot[4 * N];
void apply(int i, int mu, long long ad, int to) {
mul[i] += mu;
((add[i] *= pw[mu] %= MOD) += ad) %= MOD;
tot[i] ^= to;
}
void down(int i) {
apply(i * 2, mul[i], add[i], tot[i]);
apply(i * 2 + 1, mul[i], add[i], tot[i]);
mul[i] = add[i] = tot[i] = 0;
}
void update(int l, int r, int i, int L, int R, int mu, int ad, int to) {
if (l > R || r < L || L > R) {
return;
} else if (L <= l && r <= R) {
apply(i, mu, ad, to);
return;
} else {
down(i);
update(l, (l + r) / 2, i * 2, L, R, mu, ad, to);
update((l + r) / 2 + 1, r, i * 2 + 1, L, R, mu, ad, to);
}
}
pair<long long, int> query(int l, int r, int i, int u) {
if (l == r) {
return {add[i], tot[i]};
} else {
down(i);
return u <= (l + r) / 2 ? query(l, (l + r) / 2, i * 2, u)
: query((l + r) / 2 + 1, r, i * 2 + 1, u);
}
}
} seg;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> (s + 1) >> q;
pw[0] = 1;
for (int i = 1; i <= n; i++) {
pw[i] = pw[i - 1] * N % MOD;
}
for (int i = 1; i <= q; i++) {
cin >> l >> r >> len;
que[l + len - 1].push_back({l, -i});
que[r + len - 1].push_back({r, i});
}
for (int i = 1, lst = 1; i <= n; i++) {
if (s[i] == '0') {
seg.update(1, n, 1, 1, i, (i - lst) & 1, 1, 0);
lst = i + 1;
} else {
seg.update(1, n, 1, lst, i, 0, 0, 1);
}
for (auto [u, val] : que[i]) {
int ind = abs(val), coe = val / ind;
int it = (coe + 1) / 2;
ans[ind][it] = seg.query(1, n, 1, u);
}
}
for (int i = 1; i <= q; i++) {
cout << (ans[i][0] == ans[i][1] ? "Yes\n" : "No\n");
}
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
int n, m, p;
long long c[N][N], f[N], g[N];
long long dp[N][N], ways[N][N];
long long dp2[N][N], ways2[N][N];
long long dp0[N][N], ways0[N][N];
void add(long long &x, long long y) { x = (x + y) % p; }
long long cal(long long x) { return 1LL * x * (x - 1) / 2 % p; }
int main() {
cin >> n >> m >> p;
for (int i = 0; i < N; i++) c[i][0] = 1;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= i; j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % p;
}
f[0] = 0;
g[0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= i; j++) {
long long add1 =
c[i - 1][j - 1] * f[j - 1] % p * g[i - j] % p * (i + 1) % p;
long long add2 =
c[i - 1][j - 1] * g[j - 1] % p * f[i - j] % p * (i + 1) % p;
add(f[i], add1 + add2 +
(c[i - 1][j - 1] * g[j - 1] % p * g[i - j] % p) *
((cal(j) + cal(i - j + 1)) % p) % p);
add(g[i], c[i - 1][j - 1] * g[j - 1] % p * g[i - j] % p * (i + 1) % p);
}
}
dp[0][0] = 0;
ways[0][0] = 1;
for (int i = 0; i < n; i++) {
memset(dp2, 0, sizeof(dp2));
memset(ways2, 0, sizeof(ways2));
for (int j = 0; j <= min(i, m); j++) {
for (int k = 0; k <= min(j, m); k++) {
if (k == 0) {
dp0[i][j] = dp[j][0];
ways0[i][j] = ways[j][0];
}
add(dp2[j][0], dp[j][k]);
add(ways2[j][0], ways[j][k]);
int rem = m - (j - k);
add(ways2[j + 1][k + 1],
1LL * ways0[i - k][j - k] * c[rem][k + 1] % p * g[k + 1] % p);
add(dp2[j + 1][k + 1],
1LL * dp0[i - k][j - k] * c[rem][k + 1] % p * g[k + 1] % p +
1LL * ways0[i - k][j - k] * c[rem][k + 1] % p * f[k + 1] % p);
}
}
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++) {
dp[j][k] = dp2[j][k], ways[j][k] = ways2[j][k];
}
}
long long ans = 0;
for (int i = 0; i <= m; i++) add(ans, dp[m][i]);
cout << ans << endl;
}
| 23 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int MaxN = 3e5;
vector<int> adj[MaxN];
vector<int> prec[MaxN];
void dfs(const int u, const int p) {
prec[u] = {1, n};
adj[u].erase(remove(adj[u].begin(), adj[u].end(), p), adj[u].end());
for (auto &&v : adj[u]) {
dfs(v, u);
prec[u][0] = max(prec[u][0], 1 + prec[v][0]);
}
for (int i = 2;; ++i) {
int lo = 1;
int hi = adj[u].size();
while (lo < hi) {
int mid = (lo + hi + 1) >> 1;
int count = 0;
for (auto &&v : adj[u])
count += i <= prec[v].size() && prec[v][i - 1] >= mid;
if (count >= mid)
lo = mid;
else
hi = mid - 1;
}
if (lo <= 1) break;
prec[u].push_back(lo);
}
}
void dfs2(const int u, long long &ans) {
for (auto &&v : adj[u]) {
dfs2(v, ans);
if (prec[u].size() < prec[v].size()) prec[u].resize(prec[v].size(), 0);
for (int i = 2; i < prec[v].size(); ++i)
prec[u][i] = max(prec[u][i], prec[v][i]);
}
ans -= prec[u].size() - 1;
for (auto &&x : prec[u]) ans += x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0, -1);
long long ans = 0;
dfs2(0, ans);
cout << ans;
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
int power(int a, int b) {
if (b == 0) return 1;
if (b % 2 == 0)
return power(a * a, b / 2);
else
return a * power(a * a, b / 2);
}
string three(int a) {
string s = "";
while (a) {
s += (a % 3 + '0');
a = a / 3;
}
reverse(s.begin(), s.end());
return s;
}
int main() {
int a, c;
scanf("%d%d", &a, &c);
string as = three(a), cs = three(c);
char bs[30] = {' '};
int i, j, k = max(as.length(), cs.length()) - 1, temp;
temp = k;
for (i = cs.length() - 1, j = as.length() - 1; i >= 0 || j >= 0; i--, j--) {
if (i >= 0 && j >= 0 && cs[i] < as[j]) cs[i] += 3;
if (j >= 0 && i >= 0)
bs[k--] = (cs[i] - as[j]) + '0';
else if (j < 0 && i >= 0)
bs[k--] = cs[i];
else if (j >= 0 && i < 0)
bs[k--] = ('3' - as[j]) % 3 + '0';
}
int ans = 0;
for (int i = temp; i >= 0; i--) ans += (bs[i] - '0') * power(3, temp - i);
printf("%d", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
long long x[200010], y[200010], s[200010], dp[200010][2], c[200010];
vector<array<long long, 2>> v;
vector<long long> adj[200010];
void fx(long long &x, long long p) {
if ((x + p) % 2) x = (x + 998244353) % 1996488706;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (i = 1; i <= n; i++) {
cin >> x[i] >> y[i] >> s[i];
v.push_back({x[i], i});
}
x[n + 1] = x[n] + 1;
for (i = 1; i <= n; i++) {
array<long long, 2> o = {y[i], -(long long)1e18};
auto it = lower_bound(v.begin(), v.end(), o);
c[i] = (*it)[0] - y[i];
y[i] = (*it)[1];
adj[y[i]].push_back(i);
}
dp[n][0] = s[n] + 1;
dp[n][1] = s[n];
for (i = n - 1; i >= 1; i--) {
dp[i][0] = (2 * dp[i + 1][0]) % 1996488706;
for (auto u : adj[i + 1])
dp[i][0] =
(dp[i][0] - 2 * dp[u][1] + 2 * (long long)1996488706) % 1996488706;
dp[i][0] = (dp[i][0] - (s[i] ^ 1) + 1996488706) % 1996488706;
fx(dp[i][0], s[i] ^ 1);
dp[i][1] = dp[i][0] / 2;
}
res = x[1] % 1996488706;
for (i = 1; i <= n; i++) {
res += (dp[i][1] * c[i]);
res %= 1996488706;
res +=
((dp[i][0] - dp[i][1] + 1996488706) * (x[i + 1] - x[i])) % 1996488706;
}
res %= 998244353;
cout << res << "\n";
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long a[3], m;
int main() {
ios_base::sync_with_stdio(0);
cin >> a[0] >> a[1] >> m;
long long tam = a[0] + a[1];
sort(a, a + 2);
if (m <= a[1]) {
cout << 0;
return 0;
}
if (a[1] <= 0) {
cout << -1;
return 0;
}
long long res = 0;
if (tam < a[1]) {
long long u = (abs(a[0]) / a[1]);
if (abs(a[0]) % a[1] != 0) u++;
a[0] = a[0] + u * a[1];
res += u;
}
while (1) {
if (a[1] >= m) {
cout << res;
return 0;
}
tam = a[0] + a[1];
a[0] = a[1];
a[1] = tam;
res++;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int deg[n], x[n];
for (int(i) = 0; (i) < (n); (i)++) cin >> deg[i] >> x[i];
set<pair<int, int> > s;
for (int(i) = 0; (i) < (n); (i)++)
if (deg[i] != 0) s.insert(make_pair(deg[i], i));
vector<pair<int, int> > v;
while (s.size()) {
pair<int, int> tmp = *(s.begin());
s.erase(tmp);
int v1 = tmp.second;
int v2 = x[v1];
v.push_back(make_pair(v1, v2));
s.erase(make_pair(deg[v2], v2));
deg[v2]--;
x[v2] ^= v1;
if (deg[v2] != 0) s.insert(make_pair(deg[v2], v2));
}
cout << v.size() << "\n";
for (int(i) = 0; (i) < (v.size()); (i)++)
cout << v[i].first << " " << v[i].second << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void ga(int N, int *A) {
for (int i(0); i < N; i++) scanf("%d", A + i);
}
int N, dp[5005][5005][2];
char s[5005];
int dyn(int u, int H, bool x) {
if (u == N) return 1;
int &v = dp[u][H][x];
if (~v) return v;
v = dyn(u + 1, H + (s[u] == 102), s[u] == 102);
if (!x && H) v += dyn(u, H - 1, x);
if (v >= 1000000007) v -= 1000000007;
return v;
}
int main(void) {
scanf("%d", &N), (memset(dp, -1, sizeof(dp)));
for (int i(0); i < N; i++) scanf(" %c", s + i);
printf("%d\n", dyn(0, 0, 0));
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void print(int x) {
write(x);
puts("");
}
struct node {
int x, y, c, next;
} a[3110000], b[3110000];
int len1, last1[1111000], len2, last2[1111000];
void ins_a(int x, int y) {
len1++;
a[len1].x = x;
a[len1].y = y;
a[len1].next = last1[x];
last1[x] = len1;
}
void ins_b(int x, int y, int c) {
len2++;
b[len2].x = x;
b[len2].y = y;
b[len2].c = c;
b[len2].next = last2[x];
last2[x] = len2;
}
int low[410000], dfn[410000], sta[410000], tp, id, cnt;
int r[410000];
void link() {
for (int i = 2; i <= r[0]; i++) ins_b(cnt, r[i], 0), ins_b(r[i], cnt, 0);
ins_b(r[1], cnt, 1);
ins_b(cnt, r[1], 1);
}
void tarjan(int x, int fa) {
low[x] = dfn[x] = ++id;
sta[++tp] = x;
for (int k = last1[x]; k; k = a[k].next)
if (a[k].y != fa) {
int y = a[k].y;
if (dfn[y] == -1) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] > dfn[x])
ins_b(x, y, 1), ins_b(y, x, 1), tp--;
else if (low[y] == dfn[x]) {
cnt++;
r[r[0] = 1] = x;
int i;
do {
i = sta[tp--];
r[++r[0]] = i;
} while (i != y);
link();
}
} else
low[x] = min(low[x], dfn[y]);
}
}
int bin[25], fa[611000][25], sum[611000][25], dep[811000];
void pre_tree_node(int x) {
for (int i = 1; bin[i] <= dep[x]; i++)
fa[x][i] = fa[fa[x][i - 1]][i - 1],
sum[x][i] = sum[x][i - 1] + sum[fa[x][i - 1]][i - 1];
for (int k = last2[x]; k; k = b[k].next) {
int y = b[k].y;
if (y != fa[x][0]) {
fa[y][0] = x;
dep[y] = dep[x] + 1;
sum[y][0] = b[k].c;
pre_tree_node(y);
}
}
}
int pa;
int lca(int x, int y) {
pa = 0;
if (dep[x] < dep[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (bin[i] <= dep[x] && dep[fa[x][i]] >= dep[y])
pa += sum[x][i], x = fa[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (bin[i] <= dep[x] && fa[x][i] != fa[y][i])
pa += sum[x][i] + sum[y][i], x = fa[x][i], y = fa[y][i];
pa += sum[x][0] + sum[y][0];
return fa[x][0];
}
int up(int x, int p) {
for (int i = 20; i >= 0; i--)
if (bin[i] <= p) p -= bin[i], x = fa[x][i];
return x;
}
int n, m, Q;
int main() {
bin[0] = 1;
for (int i = 1; i <= 20; i++) bin[i] = bin[i - 1] << 1;
n = read();
m = read();
Q = read();
for (int i = 1; i <= m; i++) {
int x = read(), y = read();
ins_a(x, y);
ins_a(y, x);
}
cnt = n;
memset(dfn, -1, sizeof(dfn));
for (int i = 1; i <= n; i++)
if (dfn[i] == -1) tarjan(i, 0);
for (int i = 1; i <= n; i++)
if (dep[i] == 0) pre_tree_node(i);
while (Q--) {
int x = read(), y = read();
int LA = lca(x, y);
if (LA <= n)
print(pa);
else {
int u = up(x, dep[x] - dep[LA] - 1), v = up(y, dep[y] - dep[LA] - 1);
if (!sum[u][0] && !sum[v][0]) pa++;
print(pa);
}
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
int n, c0, c1, h, one = 0, zero = 0;
cin >> n >> c0 >> c1 >> h;
string s;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '1') {
one++;
} else {
zero++;
}
}
if (c0 > c1) {
if (h + c1 < c0) {
cout << h * zero + c1 * (zero + one) << '\n';
} else {
cout << zero * c0 + one * c1 << '\n';
}
} else {
if (h + c0 < c1) {
cout << h * one + c0 * (zero + one) << '\n';
} else {
cout << zero * c0 + one * c1 << '\n';
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
printf("%I64d\n", (1 + 3LL * n * (1LL * n + 1)));
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0;
int nr = 0;
char pre = 'a';
while (1) {
char c;
scanf("%c", &c);
if (c == '\n' || c == '\r') break;
if (c != pre || nr >= 5) {
n++;
nr = 1;
pre = 'a';
} else {
nr++;
}
pre = c;
}
printf("%d\n", n);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> ss;
string s;
cin >> s;
string t;
cin >> t;
int x1 = s[0] - 'a' + 1, y1 = s[1] - '0';
int x2 = t[0] - 'a' + 1, y2 = t[1] - '0';
if (x1 != x2 && y1 != y2) {
string a = "RU", b = "LU", c = "RD", d = "LD";
string ans;
if (x1 < x2 && y1 < y2)
ans = a;
else if (x1 < x2 && y1 > y2)
ans = c;
else if (x1 > x2 && y1 < y2)
ans = b;
else
ans = d;
while (1) {
if (x1 == x2 || y1 == y2) break;
if (x1 < x2)
x1++;
else
x1--;
if (y1 < y2)
y1++;
else
y1--;
ss.push_back(ans);
}
}
if (x1 != x2 || y1 != y2) {
string a = "R", b = "L", c = "D", d = "U";
string ans;
if (x1 < x2)
ans = a;
else if (x1 > x2)
ans = b;
else if (y1 < y2)
ans = d;
else
ans = c;
for (int i = 1; i <= abs(x1 - x2 + y1 - y2); i++) ss.push_back(ans);
}
cout << ss.size() << "\n";
for (int i = 0; i < ss.size(); i++) cout << ss[i] << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int i, n, m, d = -1;
cin >> m >> n;
vector<int> a(m), b(n), c(10000, 0);
for (i = 0; i < m; i++) {
cin >> a[i];
c[a[i]]++;
}
for (i = 0; i < n; i++) {
cin >> b[i];
if (c[b[i]] > 0) {
d = b[i];
}
}
if (d != -1)
cout << "YES\n1 " << d << '\n';
else
cout << "NO\n";
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Node {
int leftmost;
int size;
int left_child;
int right_child;
int left_id;
int right_id;
int internal_i;
int internal_j;
long long sum;
long long left_sum;
long long right_sum;
long long internal_sum;
};
static Node nodes[200200];
static int dists[100100];
static int free_node;
inline void dump(int node) {
Node &n = nodes[node];
fprintf(
stderr,
"leftmost = %-3d size = %-3d left_child = %-3d right_child = %-3d "
"left_id = %-3d right_id = %-3d internal_i = %-3d internal_j = %-3d sum "
"= %-3lld left_sum = %-3lld right_sum = %-3lld internal_sum = %-3lld\n",
n.leftmost, n.size, n.left_child, n.right_child, n.left_id, n.right_id,
n.internal_i, n.internal_j, n.sum, n.left_sum, n.right_sum,
n.internal_sum);
}
inline void calc(int node, int left, int right) {
Node &n = nodes[node];
Node &l = nodes[left];
Node &r = nodes[right];
if (l.size == 0) {
n = r;
return;
}
if (r.size == 0) {
n = l;
return;
}
n.leftmost = l.size ? l.leftmost : r.leftmost;
n.size = l.size + r.size;
n.left_child = left;
n.right_child = right;
int const between = dists[l.leftmost + l.size - 1];
n.sum = l.sum + between + r.sum;
long long const whole_left = l.sum + between + r.left_sum;
if (whole_left > l.left_sum) {
n.left_sum = whole_left;
n.left_id = r.left_id;
} else {
n.left_sum = l.left_sum;
n.left_id = l.left_id;
}
long long const whole_right = l.right_sum + between + r.sum;
if (whole_right > r.right_sum) {
n.right_sum = whole_right;
n.right_id = l.right_id;
} else {
n.right_sum = r.right_sum;
n.right_id = r.right_id;
}
long long const cross_internal = l.right_sum + between + r.left_sum;
n.internal_sum = max(max(l.internal_sum, r.internal_sum), cross_internal);
if (n.internal_sum == cross_internal) {
n.internal_i = l.right_id;
n.internal_j = r.left_id;
} else if (n.internal_sum == l.internal_sum) {
n.internal_i = l.internal_i;
n.internal_j = l.internal_j;
} else {
n.internal_i = r.internal_i;
n.internal_j = r.internal_j;
}
}
inline pair<int, int> split(int node, int left_size) {
if (nodes[node].size == 0) {
return make_pair(0, 0);
}
Node &n = nodes[node];
if (left_size == n.size) {
return make_pair(node, 0);
}
if (left_size == 0) {
return make_pair(0, node);
}
Node &l = nodes[n.left_child];
Node &r = nodes[n.right_child];
if (left_size <= l.size) {
pair<int, int> const parts = split(n.left_child, left_size);
int const right = free_node++;
calc(right, parts.second, n.right_child);
return make_pair(parts.first, right);
} else {
pair<int, int> const parts = split(n.right_child, left_size - l.size);
int const left = free_node++;
calc(left, n.left_child, parts.first);
return make_pair(left, parts.second);
}
}
int main() {
int n = 0;
int m = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &dists[i]);
}
for (int i = 1; i <= n; ++i) {
int height = 0;
scanf("%d", &height);
Node &n = nodes[i];
n.leftmost = i;
n.size = 1;
n.left_id = i;
n.right_id = i;
n.left_sum = 2 * height;
n.right_sum = 2 * height;
n.internal_sum = numeric_limits<int>::min();
}
free_node = n + 1;
int prev = 1;
int level_size = n;
do {
level_size = (level_size + 1) / 2;
int next = free_node + 1;
for (int i = 0; i < level_size; ++i) {
calc(++free_node, prev, prev + 1);
prev += 2;
}
prev = next;
++free_node;
} while (level_size > 1);
int const root = free_node - 1;
for (int i = 0; i < m; ++i) {
int const old_free = free_node;
int a = 0;
int b = 0;
scanf("%d%d", &a, &b);
if (a <= b) {
pair<int, int> const parts = split(root, a - 1);
int const left = split(parts.second, b - a + 1).second;
int const new_root = free_node;
calc(new_root, left, parts.first);
printf("%I64d\n", nodes[new_root].internal_sum);
} else {
pair<int, int> const parts = split(root, b);
Node &new_root = nodes[split(parts.second, a - b - 1).first];
printf("%I64d\n", new_root.internal_sum);
}
free_node = old_free;
}
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 35;
struct Point {
int x, y;
Point() {}
Point(int _x, int _y) : x(_x), y(_y) {}
bool operator==(const Point &b) const { return x == b.x && y == b.y; }
bool operator!=(const Point &b) const { return !(*this == b); }
};
int N, M, ip[MX][MX], op[MX][MX];
Point p(-1, -1), q;
vector<Point> ans;
vector<Point> seq;
int bad[MX][MX];
Point prv[MX][MX];
Point que[MX * MX];
int vst[MX][MX];
int fr, bk;
int dir[8][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
vector<Point> path;
void BFS(Point ed) {
fr = bk = 0;
memset((vst), 0, sizeof(vst));
que[bk++] = p;
vst[p.x][p.y] = 1;
while (fr < bk) {
Point u = que[fr++];
for (int i = 0; i < 8; i++) {
int nx = u.x + dir[i][0];
int ny = u.y + dir[i][1];
if (nx < 1 || ny < 1 || nx > N || ny > M) continue;
if (bad[nx][ny]) continue;
if (q.x == nx && q.y == ny) continue;
if (vst[nx][ny]) continue;
que[bk++] = Point(nx, ny);
prv[nx][ny] = u;
vst[nx][ny] = 1;
}
}
assert(vst[ed.x][ed.y] == 1);
path.clear();
Point cur = ed;
while (cur != p) {
path.push_back(cur);
cur = prv[cur.x][cur.y];
}
reverse(path.begin(), path.end());
}
void move(Point a) {
if (ans.size()) {
assert(a != ans.back());
}
ans.push_back(a);
swap(ip[a.x][a.y], ip[p.x][p.y]);
p = a;
}
void solve(Point ed) {
q = Point(-1, -1);
for (int i = 1; i <= N && q.x == -1; i++) {
for (int j = 1; j <= M && q.x == -1; j++) {
if (bad[i][j]) continue;
if (p.x == i && p.y == j) continue;
if (ip[i][j] == op[ed.x][ed.y]) q = Point(i, j);
}
}
int dx = 1;
int dy = 1;
if (ed.x < q.x) dx = -1;
if (ed.y < q.y) dy = -1;
while (q != ed) {
if (q.x != ed.x && !bad[q.x + dx][q.y]) {
BFS(Point(q.x + dx, q.y));
for (auto i : path) move(i);
Point tmp = p;
move(q);
q = tmp;
} else if (q.y != ed.y && !bad[q.x][q.y + dy]) {
BFS(Point(q.x, q.y + dy));
for (auto i : path) move(i);
Point tmp = p;
move(q);
q = tmp;
} else {
assert(false);
}
}
}
void solve_seq() {
for (auto v : seq) {
q = Point(-1, -1);
solve(v);
bad[v.x][v.y] = 1;
}
}
void solve22() {
if (p != Point(1, 1)) {
q = Point(-1, -1);
BFS(Point(1, 1));
for (auto i : path) {
move(i);
}
}
if (ip[2][2] != op[2][2]) {
if (ip[1][2] == op[2][2]) {
move(Point(2, 2));
move(Point(1, 2));
} else if (ip[2][1] == op[2][2]) {
move(Point(2, 2));
move(Point(2, 1));
}
}
if (p != Point(1, 1)) {
move(Point(1, 1));
}
if (ip[1][2] != op[1][2]) {
move(Point(2, 1));
move(Point(1, 2));
}
if (p != Point(1, 1)) {
move(Point(1, 1));
}
}
int main() {
int c1[MX * MX] = {0};
int c2[MX * MX] = {0};
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf("%d", &ip[i][j]);
c1[ip[i][j]]++;
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
scanf("%d", &op[i][j]);
c2[op[i][j]]++;
}
}
for (int i = 1; i < MX * MX; i++) {
if (c1[i] != c2[i]) {
puts("-1");
return 0;
}
}
if (N == 1) {
int sy = -1;
int ey = -1;
for (int j = 1; j <= M; j++) {
if (ip[1][j] != op[1][j]) {
if (sy == -1) {
sy = j;
}
ey = j;
}
}
if (sy == -1) {
puts("0");
puts("1 1");
return 0;
}
int fail = 0;
for (int j = sy; j < ey; j++) swap(ip[1][j], ip[1][j + 1]);
for (int j = 1; j <= M; j++) {
if (ip[1][j] != op[1][j]) fail = 1;
}
if (!fail) {
printf("%d\n", ey - sy);
for (int j = sy; j <= ey; j++) printf("%d %d\n", 1, j);
return 0;
}
for (int j = ey; j > sy; j--) swap(ip[1][j], ip[1][j - 1]);
fail = 0;
for (int j = ey; j > sy; j--) swap(ip[1][j], ip[1][j - 1]);
for (int j = 1; j <= M; j++) {
if (ip[1][j] != op[1][j]) fail = 1;
}
if (!fail) {
printf("%d\n", ey - sy);
for (int j = ey; j >= sy; j--) printf("%d %d\n", 1, j);
} else {
puts("-1");
}
return 0;
} else if (M == 1) {
int sx = -1;
int ex = -1;
for (int i = 1; i <= N; i++) {
if (ip[i][1] != op[i][1]) {
if (sx == -1) {
sx = i;
}
ex = i;
}
}
if (sx == -1) {
puts("0");
puts("1 1");
return 0;
}
int fail = 0;
for (int i = sx; i < ex; i++) swap(ip[i][1], ip[i + 1][1]);
for (int i = 1; i <= N; i++)
if (ip[i][1] != op[i][1]) fail = 1;
if (!fail) {
printf("%d\n", ex - sx);
for (int i = sx; i <= ex; i++) printf("%d %d\n", i, 1);
return 0;
}
for (int i = ex; i > sx; i--) swap(ip[i][1], ip[i - 1][1]);
for (int i = ex; i > sx; i--) swap(ip[i][1], ip[i - 1][1]);
fail = 0;
for (int i = 1; i <= N; i++)
if (ip[i][1] != op[i][1]) fail = 1;
if (!fail) {
printf("%d\n", ex - sx);
for (int i = ex; i >= sx; i--) printf("%d %d\n", i, 1);
} else {
puts("-1");
}
return 0;
} else {
p = Point(-1, -1);
for (int i = 1; i <= N && p.x == -1; i++) {
for (int j = 1; j <= M && p.x == -1; j++) {
if (ip[i][j] == op[1][1]) {
p = Point(i, j);
}
}
}
int x = p.x;
int y = p.y;
for (int j = M; j > 2; j--) {
for (int i = N; i >= 1; i--) {
seq.push_back(Point(i, j));
}
}
for (int i = N; i > 2; i--) {
seq.push_back(Point(i, 2));
seq.push_back(Point(i, 1));
}
memset((bad), 0, sizeof(bad));
solve_seq();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (i > 2 && j > 2 && ip[i][j] != op[i][j]) {
assert(false);
}
}
}
solve22();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (ip[i][j] != op[i][j]) {
assert(false);
}
}
}
assert(ans.size() <= 1000000);
printf("%d\n", (int)ans.size());
printf("%d %d\n", x, y);
for (auto i : ans) printf("%d %d\n", i.x, i.y);
}
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<bool> taken(n + 1, false);
vector<pair<int, int>> res;
for (int p = 3; p <= n / 2; ++p) {
if (taken[p] || p % 2 == 0) continue;
int prev = p;
for (int c = 3; c * p <= n; ++c) {
if (!taken[c * p]) {
if (prev == -1) {
prev = c * p;
} else {
res.push_back({prev, c * p});
taken[prev] = taken[c * p] = true;
prev = -1;
}
}
}
if (prev != -1) {
res.push_back({2 * p, prev});
taken[2 * p] = taken[prev] = true;
}
}
int prev = -1;
for (int x = 2; x <= n; x += 2) {
if (taken[x]) continue;
if (prev != -1) {
res.push_back({prev, x});
prev = -1;
} else {
prev = x;
}
}
cout << res.size() << "\n";
for (auto x : res) {
cout << x.first << " " << x.second << "\n";
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int k;
int main() {
cin >> k;
if (k % 2 == 1) {
cout << -1 << '\n';
return 0;
}
vector<string> first;
vector<string> second;
for (int i = 0; i < k / 2; i++) {
string s1 = "", s2 = "";
for (int j = 0; j < k / 2; j++) {
if ((j % 2) ^ (i % 2) == 0) {
s1 += "ww";
s2 += "ww";
} else {
s1 += "bb";
s2 += "bb";
}
}
first.push_back(s1);
first.push_back(s2);
}
for (int i = 0; i < k / 2; i++) {
string s1 = "", s2 = "";
for (int j = 0; j < k / 2; j++) {
if ((j % 2) ^ (i % 2) == 0) {
s1 += "bb";
s2 += "bb";
} else {
s1 += "ww";
s2 += "ww";
}
}
second.push_back(s1);
second.push_back(s2);
}
for (int i = 0; i < k; i++) {
if (i % 2 == 0) {
for (int j = 0; j < first.size(); j++) cout << first[j] << '\n';
} else {
for (int j = 0; j < second.size(); j++) cout << second[j] << '\n';
}
cout << '\n';
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
bool k = true;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] != s3[i] && s2[i] != s3[i]) {
k = false;
}
}
if (k == true)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 666013;
const int mod2 = 666019;
const int base = 31;
const int nmax = 1e5;
const int pmax = 30 + 5;
int d[pmax + 1][nmax + 1];
string s;
struct Hash {
int x, y;
Hash() {}
Hash(int _x, int _y) { x = _x, y = _y; }
inline Hash operator+(const int &k) const {
return Hash((x + k) % mod, (y + k) % mod2);
}
inline Hash operator*(const Hash &k) const {
return Hash(1LL * x * k.x % mod, 1LL * y * k.y % mod2);
}
inline Hash operator-(const Hash &k) const {
return Hash((x - k.x + mod) % mod, (y - k.y + mod2) % mod2);
}
} p[nmax + 1], a[nmax + 1], b[nmax + 1];
void calc_hash(Hash v[nmax + 1], int lg) {
v[0] = Hash(0, 0);
for (int i = 1; i <= lg; ++i) {
v[i] = v[i - 1] * Hash(base, base) + (s[i - 1] - 'a' + 1);
}
}
inline bool check(int x, int y, int lg) {
Hash u, q;
u = a[x + lg] - p[lg] * a[x];
q = b[y + lg] - p[lg] * b[y];
return (u.x == q.x && u.y == q.y);
}
int main() {
int n, m, l;
cin >> n >> s;
calc_hash(a, n);
cin >> m >> s >> l;
calc_hash(b, m);
p[0] = Hash(1, 1);
for (int i = 1; i <= n; ++i) p[i] = p[i - 1] * Hash(base, base);
int n2 = 1;
for (; (n2 << 1) <= m; n2 <<= 1) {
}
d[0][0] = 0;
for (int i = 0; i < l; ++i) {
for (int j = 0; j <= n; ++j) {
if (j - 1 >= 0) d[i][j] = max(d[i][j], d[i][j - 1]);
int x = d[i][j];
int ans = 0;
for (int step = n2; step > 0; step >>= 1) {
if (j + ans + step <= n && x + ans + step <= m &&
check(j, x, ans + step)) {
ans += step;
}
}
d[i + 1][j + ans] = max(d[i + 1][j + ans], x + ans);
d[i + 1][j] = max(d[i + 1][j], d[i][j]);
}
}
for (int i = 0; i <= n; ++i) {
if (d[l][i] == m) {
cout << "YES\n";
return 0;
}
}
cout << "NO\n";
return 0;
}
| 16 |
#include<bits/stdc++.h>
#include<algorithm>
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define ff first
#define ss second
#define mloop(i) for(auto i=m.begin();i!=m.end();i++)
#define pb push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
using namespace std;
vector<bool> prime(200002,true);
void seive(ll mx)
{
prime[0]=prime[1]=false;
for(ll i=2;i*i<=mx;i++)
{
if(prime[i])
{
for(ll j=i*i;j<=mx;j+=i)
{
prime[j]=false;
}
}
}
}
const ll M = 1e9+7;
const ll Mxn = 3e5;
ll power(ll i,ll p)
{
ll ans=1;
while(p)
{
if(p & 1)
ans = (ans*i)%M;
i = (i*i)%M;
p/=2;
}
return ans%M;
}
int main()
{
int t=1;
cin>>t;
while(t--)
{
pll ans = {0,0};
string s;
cin>>s;
ll n = s.length();
ll x=0,y=0;
vector<pll> v;
for(ll i=0;i<n;i++)
{
if(s[i] == 'L')
x--;
else if(s[i] == 'R')
x++;
else if(s[i] == 'D')
y--;
else
y++;
v.pb({x,y});
}
for(ll i=0;i<v.size();i++)
{
x=0;y=0;
for(ll j=0;j<n;j++)
{
ll prevx = x;
ll prevy = y;
if(s[j] == 'L')
x--;
else if(s[j] == 'R')
x++;
else if(s[j] == 'D')
y--;
else
y++;
pll p = {x,y};
if(v[i] == p)
{
x = prevx;
y = prevy;
}
}
if(x == 0 && y== 0)
{
ans = v[i];
break;
}
}
cout<<ans.ff<<" "<<ans.ss<<endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, i, p, r = 1;
vector<pair<int, int> > vec;
int main() {
cin >> n;
int ans[n];
for (i = 0; i < n; i++) {
cin >> p;
vec.push_back(make_pair(p, i));
}
sort(vec.begin(), vec.end());
ans[vec[n - 1].second] = 1;
for (i = n - 2; i >= 0; i--) {
if (vec[i].first != vec[i + 1].first) r = n - i;
ans[vec[i].second] = r;
}
for (i = 0; i < n; i++) cout << ans[i] << " ";
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long min(long long a, long long b) { return a + b - max(a, b); }
long long mulmod(long long a, long long b, long long mod) {
return ((a % 998244353) * (b % 998244353) % 998244353);
}
long long mod(long long a, long long b) {
if (a > b)
return a - b;
else
return b - a;
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &a : v) is >> a;
return is;
}
int bits(long long n) {
long long ctr = 0;
while (n) {
n /= 10;
ctr++;
}
return ctr;
}
vector<long long> z_function(string second) {
long long n = (long long)second.length();
vector<long long> z(n);
for (long long i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && second[z[i]] == second[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
vector<long long> bits1(long long n) {
vector<long long> res;
while (n) {
res.push_back(n % 10);
n /= 10;
}
reverse(res.begin(), res.end());
return res;
}
long long modInv(long long x) { return power(x, 998244353 - 2, 998244353); }
long long fact[200005];
long long ncr(long long n, long long r) {
long long x = fact[n];
long long y = fact[n - r];
long long z = fact[r];
long long ff = (y * z) % 998244353;
long long gg1 = modInv(ff);
return (gg1 * x) % 998244353;
}
long long logs(long long x) {
long long tot = 0;
long long n = x;
while (x > 0) {
x /= 2;
tot++;
}
return tot;
}
long long case_no = 1;
void op() {
cout << "Case #" << case_no << ": ";
case_no++;
}
long long first(long long a, long long b, long long c) {
return (a - b) * (a - b) + (b - c) * (b - c) + (c - a) * (c - a);
}
long long gg(long long x) {
long long ctr = 0;
while (x > 1) {
x /= 2;
ctr++;
}
return ctr;
}
bool search(long long x, long long y, set<long long> s1) {
auto it = s1.lower_bound(x);
if (it == s1.end()) return false;
long long g = *it;
if (g >= x && g <= y) return true;
return false;
}
void solve() {
long long n, k;
cin >> n >> k;
if (n < k) {
cout << "NO\n";
return;
}
if (n % 2 && k % 2 == 0) {
cout << "NO\n";
return;
}
if (n % 2) {
cout << "YES\n";
long long tot = 0;
for (long long i = 0; i < k - 1; i++) cout << 1 << " ";
cout << n + 1 - k << "\n";
return;
} else {
if (k % 2) {
if (n < 2 * k) {
cout << "NO\n";
return;
}
cout << "YES\n";
for (long long i = 0; i < k - 1; i++) cout << 2 << " ";
cout << n - 2 * (k - 1) << "\n";
} else {
cout << "YES\n";
long long tot = 0;
for (long long i = 0; i < k - 1; i++) cout << 1 << " ";
cout << n + 1 - k << "\n";
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long t;
cin >> t;
while (t--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 3e5 + 5;
vector<pair<int, int>> g[N];
int d[N], u[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, h;
cin >> n >> m >> h;
vector<int> q;
fill(d, d + N, 1e9);
for (int i = 0; i < m; ++i) {
int v;
cin >> v;
if (!u[v]) q.push_back(v);
u[v] = 1;
d[v] = 0;
}
for (int k = 1; k < n; ++k) {
int i, j;
cin >> i >> j;
g[i].push_back({j, k});
g[j].push_back({i, k});
}
set<int> ok;
for (int k = 0; k < q.size(); ++k) {
int v = q[k];
for (auto p : g[v]) {
int i = p.first;
if (d[i] > d[v] + 1) {
d[i] = d[v] + 1;
u[i] = 1;
q.push_back(i);
ok.insert(p.second);
}
}
}
cout << n - 1 - ok.size() << endl;
for (int i = 1; i < n; ++i)
if (!ok.count(i)) cout << i << ' ';
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
typedef struct {
double p;
double np;
} fd;
bool operator<(const fd &a, const fd &b) {
if (a.p > b.p) {
return true;
}
return false;
}
int main(void) {
int n;
cin >> n;
vector<fd> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i].p;
arr[i].np = 1 - arr[i].p;
}
sort(arr.begin(), arr.end());
double max = arr[0].p;
for (int i = 1; i < n; i++) {
double prob = 0;
for (int j = 0; j <= i; j++) {
double pp = arr[j].p;
for (int x = 0; x <= i; x++) {
if (x != j) {
pp *= arr[x].np;
}
}
prob += pp;
}
if (max < prob) {
max = prob;
}
}
printf("%.12lf\n", max);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1 << 30;
long long add(long long a, long long b) { return (a + b) % MOD; }
const int MAXN = 1100000;
int dd[MAXN];
int d(int k) { return dd[k]; }
void prec() {
for (int a = 1; a < MAXN; ++a) {
for (int b = a; b < MAXN; b += a) {
dd[b]++;
}
}
}
int main() {
prec();
int A, B, C;
long long sum = 0;
cin >> A >> B >> C;
for (int i = 1; i <= A; ++i) {
for (int j = 1; j <= B; ++j) {
for (int k = 1; k <= C; ++k) {
sum = add(sum, d(i * j * k));
}
}
}
cout << sum << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout.setf(ios_base::fixed);
cout.precision(28);
int n, m;
cin >> n >> m;
vector<vector<int> > g(n);
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
g[x - 1].push_back(y - 1);
}
vector<vector<int> > d(n, vector<int>(n, -1));
for (int i = 0; i < n; ++i) {
queue<int> q;
d[i][i] = 0;
q.push(i);
while (!q.empty()) {
int c = q.front();
q.pop();
for (auto x : g[c]) {
if (d[i][x] == -1) {
d[i][x] = d[i][c] + 1;
q.push(x);
}
}
}
}
vector<vector<pair<int, int> > > beg(n), ed(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i != j && d[i][j] != -1) {
beg[j].push_back({-d[i][j], i});
ed[i].push_back({-d[i][j], j});
}
}
}
for (int i = 0; i < n; ++i) {
sort(beg[i].begin(), beg[i].end());
sort(ed[i].begin(), ed[i].end());
}
int ans = -100, x1, x2, x3, x4;
int cnt = 3;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i == j || d[i][j] == -1) {
continue;
}
if (beg[i].size() > 0 && ed[j].size() > 0) {
auto b = beg[i].begin(), e = ed[j].begin();
for (int x = 0; x < cnt && x < beg[i].size(); ++x) {
for (int y = 0; y < cnt && y < ed[j].size(); ++y) {
if ((b + x)->second != j && (b + x)->second != (e + y)->second &&
i != (e + y)->second) {
if (d[i][j] - (b + x)->first - (e + y)->first > ans) {
ans = d[i][j] - (b + x)->first - (e + y)->first;
x1 = (b + x)->second;
x2 = i;
x3 = j;
x4 = (e + y)->second;
}
}
}
}
}
}
}
cout << x1 + 1 << ' ' << x2 + 1 << ' ' << x3 + 1 << ' ' << x4 + 1 << endl;
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
char a[101][101];
int n;
int m;
int chk(int, int);
int main() {
cin >> n >> m;
string ans = "";
char x;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> x;
a[i][j] = x;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (chk(i, j) == 0) ans += a[i][j];
}
cout << ans;
return 0;
}
int chk(int x, int y) {
for (int j = 0; j < m; j++) {
if (a[x][j] == a[x][y] && j != y) return 1;
}
for (int j = 0; j < n; j++) {
if (a[j][y] == a[x][y] && j != x) return 1;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long n, k;
struct node {
long long x1, y1, x2, y2, num;
long long x, y;
} ju[100005], up[100005], down[100005], Left[100005], Right[100005];
long long Minx, Maxx, Miny, Maxy;
long long ans = 0x7fffffffffffffff;
bool vis[100005];
bool cmpdown(node a, node b) { return a.x < b.x; }
bool cmpup(node a, node b) { return a.x > b.x; }
bool cmpleft(node a, node b) { return a.y < b.y; }
bool cmpright(node a, node b) { return a.y > b.y; }
inline void copy(long long xx) {
up[xx].x = down[xx].x = Left[xx].x = Right[xx].x = ju[xx].x;
up[xx].y = down[xx].y = Left[xx].y = Right[xx].y = ju[xx].y;
up[xx].num = down[xx].num = Left[xx].num = Right[xx].num = ju[xx].num;
}
inline void chuli() {
while (vis[up[Maxx].num]) Maxx++;
while (vis[down[Minx].num]) Minx++;
while (vis[Left[Miny].num]) Miny++;
while (vis[Right[Maxy].num]) Maxy++;
}
void dfs(long long xx) {
if (xx > k) {
long long x = (up[Maxx].x - down[Minx].x + 1) / 2;
long long y = (Right[Maxy].y - Left[Miny].y + 1) / 2;
if (x == 0) x = 1;
if (y == 0) y = 1;
long long num = x * y;
ans = min(num, ans);
return;
}
long long tMaxx = Maxx, tMinx = Minx, tMiny = Miny, tMaxy = Maxy;
vis[up[Maxx].num] = true;
chuli();
dfs(xx + 1);
Maxx = tMaxx, Minx = tMinx, Miny = tMiny, Maxy = tMaxy;
vis[up[Maxx].num] = false;
vis[down[Minx].num] = true;
chuli();
dfs(xx + 1);
Maxx = tMaxx, Minx = tMinx, Miny = tMiny, Maxy = tMaxy;
vis[down[Minx].num] = false;
vis[Left[Miny].num] = true;
chuli();
dfs(xx + 1);
Maxx = tMaxx, Minx = tMinx, Miny = tMiny, Maxy = tMaxy;
vis[Left[Miny].num] = false;
vis[Right[Maxy].num] = true;
chuli();
dfs(xx + 1);
Maxx = tMaxx, Minx = tMinx, Miny = tMiny, Maxy = tMaxy;
vis[Right[Maxy].num] = false;
}
signed main() {
n = read();
k = read();
if (n <= k) {
printf("0\n");
return 0;
}
for (long long i = 1; i <= n; i++) {
ju[i].num = i;
ju[i].x1 = read();
ju[i].y1 = read();
ju[i].x2 = read();
ju[i].y2 = read();
ju[i].x = (ju[i].x1 + ju[i].x2);
ju[i].y = (ju[i].y1 + ju[i].y2);
copy(i);
}
sort(up + 1, up + n + 1, cmpup);
sort(down + 1, down + n + 1, cmpdown);
sort(Left + 1, Left + n + 1, cmpleft);
sort(Right + 1, Right + n + 1, cmpright);
Minx = Maxx = Miny = Maxy = 1;
dfs(1);
printf("%I64d\n", ans);
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1e9 + 7;
int inf = 2e9;
long long INF = 8e18;
int t[200000];
int dp[200000];
int bs(int x, int s, int e) {
if (s == e) {
return s;
}
int mid = (s + e + 1) / 2;
if (t[mid] > x) {
return bs(x, s, mid - 1);
}
return bs(x, mid, e);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
int n;
cin >> n;
t[0] = -inf;
for (int i = 1; i <= n; i++) {
cin >> t[i];
}
dp[0] = 0;
for (int i = 1; i <= n; i++) {
dp[i] = inf;
}
for (int i = 1; i <= n; i++) {
dp[i] = min(dp[i], dp[i - 1] + 20);
dp[i] = min(dp[i], dp[bs(t[i] - 90, 0, n)] + 50);
dp[i] = min(dp[i], dp[bs(t[i] - 1440, 0, n)] + 120);
cout << dp[i] - dp[i - 1] << "\n";
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 10000000007;
const int maxn = 100010;
const int MAX = 10010;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
long long qpow(long long a, long long b) {
return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % MOD
: qpow(a * a % MOD, b >> 1)) %
MOD
: 1;
}
long long qpow(long long a, long long b, long long c) {
return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % c
: qpow(a * a % MOD, b >> 1)) %
c
: 1;
}
inline int ird() {
register int x = 0, f = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
inline long long lrd() {
register long long x = 0, f = 1;
register char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
long long gcd(long long a, long long b) { return (b > 0) ? gcd(b, a % b) : a; }
int dp[110][110][2];
int a[110], sum[110];
int main() {
for (int i = 0; i <= 110; i++) {
for (int j = 0; j <= 110; j++) {
dp[i][j][0] = MAX;
dp[i][j][1] = MAX;
}
}
memset(sum, 0, sizeof sum);
int n, ji = 0, ou = 0;
n = ird();
ou = n / 2;
ji = n - ou;
for (int i = 1; i <= n; i++) {
a[i] = ird();
sum[i] = sum[i - 1];
if (a[i] % 2 == 1) sum[i]++;
}
if (a[1] != 0) {
if (a[1] % 2 == 0) {
dp[1][0][0] = 0;
} else {
dp[1][1][1] = 0;
}
} else {
dp[1][1][1] = 0;
dp[1][0][0] = 0;
}
for (int i = 2; i <= n; i++) {
if (a[i] != 0) {
if (a[i] % 2 == 0) {
for (int j = max(sum[i], i - ji); j <= min(i, ji); j++) {
dp[i][j][0] = min(dp[i - 1][j][1] + 1, dp[i - 1][j][0]);
}
} else {
if (a[i] % 2 == 1) {
for (int j = max(sum[i], i - ji); j <= min(ji, i); j++) {
dp[i][j][1] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1]);
}
}
}
} else {
for (int j = max(sum[i], i - ji); j <= min(i, ji); j++) {
dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + 1);
}
for (int j = max(sum[i], i - ji); j <= min(i, ji); j++)
dp[i][j][1] = min(dp[i - 1][j - 1][0] + 1, dp[i - 1][j - 1][1]);
}
}
cout << min(dp[n][ji][1], dp[n][ji][0]) << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MN = 2e5 + 10;
vector<int> graf[MN];
int minCiclo = INF;
int nodMinCiclo1, nodMinCiclo2;
int vis[MN];
int parent[MN];
void dfs(int nod, int l, int p) {
vis[nod] = l;
parent[nod] = p;
for (int ady : graf[nod])
if (ady != p) {
if (!vis[ady])
dfs(ady, l + 1, nod);
else if (vis[ady] < l && l - vis[ady] + 1 < minCiclo) {
minCiclo = l - vis[ady] + 1;
nodMinCiclo1 = nod;
nodMinCiclo2 = ady;
}
}
}
int col[MN];
void color(int nod, int c, vector<int> &a, vector<int> &b) {
col[nod] = c % 2 + 1;
if (col[nod] % 2 == 1)
a.push_back(nod);
else
b.push_back(nod);
for (int ady : graf[nod]) {
if (!col[ady]) {
color(ady, c + 1, a, b);
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int N, M, K;
cin >> N >> M >> K;
while (M--) {
int a, b;
cin >> a >> b;
graf[a].push_back(b);
graf[b].push_back(a);
}
dfs(1, 1, -1);
vector<int> res;
if (minCiclo == INF) {
cout << "1\n";
vector<int> res1;
vector<int> res2;
color(1, 1, res1, res2);
if (res1.size() >= (K + 1) / 2) {
for (int i = 0; i < (K + 1) / 2; i++) {
res.push_back(res1[i]);
}
} else {
for (int i = 0; i < (K + 1) / 2; i++) {
res.push_back(res2[i]);
}
}
} else if (minCiclo <= K) {
cout << "2\n";
for (int i = nodMinCiclo1; i != nodMinCiclo2; i = parent[i]) {
res.push_back(i);
}
res.push_back(nodMinCiclo2);
cout << res.size() << "\n";
} else {
cout << "1\n";
for (int i = 1, nod = nodMinCiclo1; i <= (K + 1) / 2;
i++, nod = parent[parent[nod]]) {
res.push_back(nod);
}
}
for (int i = 0; i < res.size(); i++) {
if (i) cout << " ";
cout << res[i];
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
struct Value {
char l, r;
array<array<int, 10>, 10> ct;
};
struct Lazy {
char c;
};
struct SegTree {
int n;
vector<Value> t1;
vector<Lazy> t2;
SegTree(string &s) {
n = 1;
while (n < (int)s.size()) n <<= 1;
t1.resize(2 * n - 1, Value());
t2.resize(2 * n - 1, Lazy());
init(s, 0, 0, n);
}
void init(const string &s, int k, int l, int r) {
if (r - l == 1) {
if (l < (int)s.size()) {
t1[k].l = t1[k].r = s[l];
}
} else {
init(s, 2 * k + 1, l, (l + r) / 2);
init(s, 2 * k + 2, (l + r) / 2, r);
update(k, l, r);
}
}
Value calc(Value lv, Value rv) {
if (lv.r == '\0' && rv.l == '\0') {
return Value();
} else if (lv.r == '\0') {
return rv;
} else if (rv.l == '\0') {
return lv;
}
Value v;
v.l = lv.l;
v.r = rv.r;
v.ct = {0};
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
v.ct[i][j] = lv.ct[i][j] + rv.ct[i][j];
}
}
v.ct[lv.r - 'a'][rv.l - 'a']++;
return v;
}
Value value(int k, int l, int r) {
if (t2[k].c != '\0') {
Value v;
v.l = v.r = t2[k].c;
v.ct = {0};
v.ct[t2[k].c - 'a'][t2[k].c - 'a'] = r - l - 1;
return v;
} else {
return t1[k];
}
}
void update(int k, int l, int r) {
t1[k] = calc(value(2 * k + 1, l, (l + r) / 2),
value(2 * k + 2, (l + r) / 2, r));
}
void eval(int k) {
if (t2[k].c != '\0') {
t2[2 * k + 1] = (Lazy){t2[k].c};
t2[2 * k + 2] = (Lazy){t2[k].c};
}
t2[k] = (Lazy){'\0'};
}
void set(int a, int b, char v, int k, int l, int r) {
if (b <= l || r <= a) {
return;
} else if (a <= l && r <= b) {
t2[k] = (Lazy){v};
return;
} else {
eval(k);
set(a, b, v, 2 * k + 1, l, (l + r) / 2);
set(a, b, v, 2 * k + 2, (l + r) / 2, r);
update(k, l, r);
}
}
void set(int a, int b, char v) { set(a, b, v, 0, 0, n); }
Value query(int a, int b, int k, int l, int r) {
if (b <= l || r <= a) {
return Value();
} else if (a <= l && r <= b) {
return value(k, l, r);
} else {
eval(k);
Value ans = calc(query(a, b, 2 * k + 1, l, (l + r) / 2),
query(a, b, 2 * k + 2, (l + r) / 2, r));
update(k, l, r);
return ans;
}
}
Value query(int a, int b) { return query(a, b, 0, 0, n); }
};
char buf[210000];
int n, m, k;
int main() {
scanf("%d%d%d", &n, &m, &k);
scanf("%s", buf);
string s(buf, buf + n);
SegTree st(s);
while (m--) {
int t;
scanf("%d", &t);
if (t == 1) {
int l, r;
char c;
scanf("%d%d %c", &l, &r, &c);
l--;
st.set(l, r, c);
} else if (t == 2) {
int tbl[10];
scanf("%s", buf);
string t(buf, buf + k);
for (int i = 0; i < (int)t.size(); i++) {
tbl[t[i] - 'a'] = i;
}
int ans = n;
Value v = st.query(0, st.n);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (tbl[i] < tbl[j]) {
ans -= v.ct[i][j];
}
}
}
printf("%d\n", ans);
} else {
assert(false);
}
}
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)(1e5) + 5, M = 1001, mod = (int)(1e9) + 7;
int main() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
long long arr[m];
char a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
for (int j = 0; j < m; j++) {
set<char> st;
for (int i = 0; i < n; i++) {
st.insert(a[i][j]);
}
arr[j] = st.size();
}
long long ans = 1;
for (int i = 0; i < m; i++) ans = ((ans % mod) * (arr[i] % mod)) % mod;
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 305;
int n, cnt[305], pre[305], sz;
long long a[maxn], C[maxn][maxn], dp[maxn][maxn], fac[maxn];
bool vis[305];
inline void init() {
C[0][0] = fac[0] = 1;
for (int i = 1; i < maxn; ++i) {
C[i][0] = 1;
fac[i] = fac[i - 1] * i % mod;
for (int j = 1; j <= i; ++j)
C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
}
inline long long check(long long x) {
long long left = 1, right = (long long)sqrt(x) + 100, res = 1;
while (left <= right) {
long long mid = (left + right) >> 1;
if (mid * mid <= x) {
res = mid;
left = mid + 1;
} else
right = mid - 1;
}
return res * res == x;
}
inline void update(long long &a, long long b) { a = (a + b) % mod; }
int main() {
init();
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%I64d", &a[i]);
for (int i = 0; i < n; ++i)
if (!vis[i]) {
for (int j = i; j < n; ++j)
if (!vis[j]) {
if (check(a[i] * a[j])) {
++cnt[sz];
vis[j] = 1;
}
}
++sz;
}
for (int i = 0; i < sz; ++i) pre[i + 1] = pre[i] + cnt[i];
dp[0][0] = 1;
for (int i = 0; i < sz; ++i)
for (int j = 0; j <= pre[i]; ++j)
if (dp[i][j])
for (int k = 1; k <= cnt[i]; ++k)
for (int l = 0; l <= min(j, k); ++l)
update(dp[i + 1][j + cnt[i] - k - l],
fac[cnt[i]] * C[pre[i] + 1 - j][k - l] % mod * C[j][l] %
mod * C[cnt[i] - 1][k - 1] % mod * dp[i][j] % mod);
printf("%I64d\n", dp[sz][0]);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.141592653589793238;
const long long int inf = (long long int)10e16;
int main() {
ios ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int h, k = 0, an = 0, temp, mi = 1000000000000000000, an1, h1;
int n;
cin >> h >> n;
long long int q[n + 1], dp[n + 1];
for (int i = 1; i <= n; i += 1) cin >> q[i];
for (int i = 1; i <= n; i += 1) {
k += q[i];
dp[i] = k;
if (h + dp[i] <= 0) {
cout << i;
return 0;
}
}
if (k >= 0) {
cout << -1;
return 0;
}
k *= -1;
for (int i = 1; i <= n; i += 1) {
h1 = h;
h1 += dp[i];
if (h1 % k == 0) {
an = ((h1 / k) * n);
} else {
an = ((h1 / k) + 1) * n;
}
mi = min(mi, an + i);
}
cout << mi;
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int val[50][50][71];
void add(int& a, int b) {
if (a == -1 || a > b) a = b;
}
int fct(int n, int m, int k) {
int& ret = val[n][m][k];
if (ret >= 0) return ret;
if (n * m < k) return 0x3f3f3f3f;
if (n * m == k || k == 0) return ret = 0;
for (int i = 1; i <= n / 2; i++)
for (int j = 0; j <= k; j++)
add(ret, m * m + fct(i, m, j) + fct(n - i, m, k - j));
for (int i = 1; i <= m / 2; i++)
for (int j = 0; j <= k; j++)
add(ret, n * n + fct(n, i, j) + fct(n, m - i, k - j));
return ret;
}
int main() {
memset(val, 0xff, sizeof val);
int t, n, m, k;
cin >> t;
while (t--) {
cin >> n >> m >> k;
cout << fct(n, m, k) << endl;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
void Solve() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
if (i != j && j != k && arr[j] + arr[k] == arr[i]) {
cout << i + 1 << " " << j + 1 << " " << k + 1 << endl;
return;
}
}
}
}
cout << -1 << endl;
}
int main() {
ios::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
Solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, sd, c;
string s;
bool p, m;
cin >> n;
while (n--) {
c = 0;
sd = 0;
p = false;
m = false;
cin >> s;
if (s == "0") {
cout << "red" << endl;
} else {
for (int i = 0; i < s.size(); i++) {
if (s[i] - 48 == 0) {
c++;
}
if (s[i] - 48 > 0 && (s[i] - 48) % 2 == 0) {
p = true;
}
sd += s[i] - 48;
}
if (sd % 3 == 0) {
m = true;
}
if ((p && m && c > 0) || (m && c > 1)) {
cout << "red" << endl;
} else {
cout << "cyan" << endl;
}
}
}
return 0;
}
| 2 |
Subsets and Splits