solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int mod = 1000000007;
const int N = 500005;
void Getnext(string &s, int *fail) {
int id = -1;
fail[0] = -1;
for (int i = 1; i < s.size(); ++i) {
while (~id && s[id + 1] != s[i]) id = fail[id];
if (s[id + 1] == s[i]) ++id;
fail[i] = id;
}
}
int A[N], B[N];
int kmp1[N], kmp2[N];
int lf[N], ri[N];
int main() {
int n, m, k;
string s, t1, t2;
cin >> n >> m >> k;
cin >> s >> t1;
t2 = t1;
reverse(t2.begin(), t2.end());
s.push_back('$');
t1.push_back('$');
t2.push_back('$');
Getnext(t1, A);
Getnext(t2, B);
memset(lf, -1, sizeof(lf));
memset(ri, -1, sizeof(ri));
lf[0] = k - 1;
ri[0] = n - k;
for (int i = 0, j = -1; i < n; i++) {
while (j != -1 && s[i] != t1[j + 1]) j = A[j];
if (s[i] == t1[j + 1]) j++;
if (j == m - 1 && i < k - 1) {
puts("Yes");
printf("%d %d\n", 1, 1 + k);
return 0;
}
if (i < k - 1) continue;
int pos = j;
while (pos != -1) {
if (lf[pos + 1] != -1) break;
lf[pos + 1] = i;
pos = A[pos];
}
if (j == m - 1 && i - k + 1 >= k) {
puts("Yes");
printf("%d %d\n", i - k + 1 + 1 - k, i - k + 1 + 1);
return 0;
}
}
for (int i = n - 1, j = -1; i >= 0; i--) {
while (j != -1 && s[i] != t2[j + 1]) j = B[j];
if (s[i] == t2[j + 1]) j++;
if (j == m - 1 && i > n - k) {
puts("Yes");
printf("%d %d\n", n - 2 * k + 1, n - k + 1);
return 0;
}
if (i > n - k) continue;
int pos = j;
while (pos != -1) {
if (ri[pos + 1] != -1) break;
ri[pos + 1] = i;
pos = B[pos];
}
if (j == m - 1 && n - i - k >= k) {
puts("Yes");
printf("%d %d\n", i + 1, i + k + 1);
return 0;
}
}
for (int i = 0; i <= m; i++) {
if (lf[i] == -1 || ri[m - i] == -1) continue;
if (i > k || m - i > k) continue;
if (lf[i] < ri[m - i]) {
puts("Yes");
printf("%d %d\n", lf[i] - k + 1 + 1, ri[m - i] + 1);
return 0;
}
}
puts("No");
}
| 9 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long MOD = 1e9 + 7;
const int N = (int)1e6;
long long mul(long long a, long long b, long long m) {
return ((a % m) * (b % m)) % m;
}
long long factorial(long long fac) {
if (fac <= 1) return 1;
long long res = 2;
for (int i = 3; i <= fac; i++) res = res * i;
return res;
}
long long fast_pow(long long x, long long n, long long m) {
long long result = 1;
while (n > 0) {
if (n % 2 == 1) {
result = mul(result, x, m);
}
x = mul(x, x, m);
n = n / 2;
}
return result;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
a %= b;
return gcd(b, a);
}
long long C(long long n, long long r) {
long long ans = 1;
for (long long i = 1; i <= r; i++) {
ans = ans * (n - r + i) / i;
}
return ans;
}
long long P(long long n, long long r) {
long long ans = 1;
for (long long i = 1; i <= r; i++) {
ans = ans * (n - r + i);
}
return ans;
}
long long binaryToDecimal(long long n) {
long long num = n;
long long dec_value = 0;
long long base = 1;
long long temp = num;
while (temp) {
long long last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
string decimalToBinary(long long n) {
string s = bitset<64>(n).to_string();
const auto loc1 = s.find('1');
if (loc1 != string::npos) return s.substr(loc1);
return "0";
}
vector<long long int> factorizationOf(long long n) {
vector<long long> divisors;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
}
}
for (int i = sqrt(n); i >= 1; i--) {
if (n % i == 0) {
if (i * i != n) {
divisors.push_back(n / i);
}
}
}
return divisors;
}
vector<long long int> PrimeFactorsOf(long long n) {
vector<long long> primes;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
n /= i;
primes.push_back(i);
}
}
if (n != 1) {
primes.push_back(n);
}
return primes;
}
int main() {
ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),
cout.tie(0);
long long h1, a1, c1, t;
long long h2, a2;
cin >> h1 >> a1 >> c1;
cin >> h2 >> a2;
vector<string> v;
while (h2 > 0) {
if (a1 >= h2) {
v.push_back("STRIKE");
break;
} else if (h1 - a2 <= 0) {
v.push_back("HEAL");
h1 = h1 + c1;
} else {
h2 -= a1;
v.push_back("STRIKE");
}
h1 -= a2;
}
cout << v.size() << endl;
for (auto &it : v) {
cout << it << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string htb(char s) {
if (s == '0')
return "0000";
else if (s == '1')
return "0001";
else if (s == '2')
return "0010";
else if (s == '3')
return "0011";
else if (s == '4')
return "0100";
else if (s == '5')
return "0101";
else if (s == '6')
return "0110";
else if (s == '7')
return "0111";
else if (s == '8')
return "1000";
else if (s == '9')
return "1001";
else if (s == 'A')
return "1010";
else if (s == 'B')
return "1011";
else if (s == 'C')
return "1100";
else if (s == 'D')
return "1101";
else if (s == 'E')
return "1110";
else
return "1111";
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> m(n + 1, vector<int>(n + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n / 4; j++) {
char c;
cin >> c;
string s = htb(c);
m[i][4 * j - 3] = s[0] - '0';
m[i][4 * j - 2] = s[1] - '0';
m[i][4 * j - 1] = s[2] - '0';
m[i][4 * j] = s[3] - '0';
}
}
for (int i = 2; i < n + 1; i++) {
m[i][1] += m[i - 1][1];
m[1][i] += m[1][i - 1];
}
for (int i = 2; i < n + 1; i++)
for (int j = 2; j < n + 1; j++)
m[i][j] += m[i][j - 1] + m[i - 1][j] - m[i - 1][j - 1];
int y = 1, t = 0;
for (int x = 2; x < n + 1; x++) {
t = 0;
if (n % x == 0)
for (int i = 1; i <= n / x; i++) {
for (int j = 1; j <= n / x; j++) {
int it = i * x, jt = j * x, u;
u = m[it][jt] - m[it][jt - x] - m[it - x][jt] + m[it - x][jt - x];
if (u != x * x && u != 0) {
t = 1;
break;
}
}
if (t == 1) break;
}
if (t == 0 && n % x == 0) y = x;
}
cout << y;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
int a[2000][2000];
string s[1 << 20];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < 2 * n - 1; i++) cin >> s[i];
if (k == 1) {
int c = 0;
for (int i = 0; i < 2 * n - 1; i++)
c += count(s[i].begin(), s[i].end(), 'E');
int tot = 2 * n * m - n - m;
if (4 * c < tot * 3) {
cout << "NO" << endl;
return 0;
}
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cout << 1 << ' ';
cout << endl;
}
return 0;
}
if (n <= m) {
for (int i = 0; i < n; i++) {
int f = 1;
for (int j = 0; j < m; j++) {
a[i][j] = f;
if (j != m - 1 && s[2 * i][j] == 'N') f = 3 - f;
}
if (i != 0) {
int balance = 0;
for (int j = 0; j < m; j++) {
if (a[i][j] == a[i - 1][j] && s[2 * i - 1][j] == 'E')
balance++;
else if (a[i][j] != a[i - 1][j] && s[2 * i - 1][j] == 'N')
balance++;
else
balance--;
}
if (balance < 0) {
for (int j = 0; j < m; j++) a[i][j] = 3 - a[i][j];
}
}
}
} else {
for (int j = 0; j < m; j++) {
int f = 1;
for (int i = 0; i < n; i++) {
a[i][j] = f;
if (i != n - 1 && s[2 * i + 1][j] == 'N') f = 3 - f;
}
if (j != 0) {
int balance = 0;
for (int i = 0; i < n; i++) {
if (a[i][j] == a[i][j - 1] && s[2 * i][j - 1] == 'E')
balance++;
else if (a[i][j] != a[i][j - 1] && s[2 * i][j - 1] == 'N')
balance++;
else
balance--;
}
if (balance < 0) {
for (int i = 0; i < n; i++) a[i][j] = 3 - a[i][j];
}
}
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << a[i][j] << ' ';
cout << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, ans;
int val[maxn];
vector<int> adj[maxn], len[maxn];
bool vis[maxn];
int gcd(int a, int b) {
if (!a || !b) return a + b;
return gcd(b, a % b);
}
void dfs(int cur, int id) {
vis[cur] = 1;
val[cur] = id;
for (int i = 0; i < adj[cur].size(); i++) {
int son = adj[cur][i], l = len[cur][i];
if (!vis[son]) {
dfs(son, id + l);
} else if (val[son] != id + l) {
ans = gcd(ans, abs(id + l - val[son]));
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1, u, v; i <= m; i++) {
scanf("%d %d", &u, &v);
adj[u].push_back(v);
len[u].push_back(1);
adj[v].push_back(u);
len[v].push_back(-1);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
if (!ans) ans = n;
return !printf("%d\n", ans);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int num[105];
char str[105], st[105];
int main() {
int n, q, pos, flag, dir, l, r, i;
while (scanf("%d%d%s", &n, &q, str) != EOF) {
while (q--) {
memset(num, 0, sizeof(num));
memcpy(st, str, sizeof(str));
scanf("%d%d", &l, &r);
l--;
r--;
flag = 0;
pos = 0;
dir = 1;
for (i = l; i >= l && i <= r; i += dir) {
if (st[i] >= '0' && st[i] <= '9') {
num[st[i] - '0']++;
if (st[i] == '0')
st[i] = 0;
else
st[i]--;
flag = 0;
} else if (st[i] == '>') {
dir = 1;
if (flag) st[pos] = 0;
flag = 1;
pos = i;
} else if (st[i] == '<') {
dir = -1;
if (flag) st[pos] = 0;
flag = 1;
pos = i;
}
}
for (i = 0; i <= 9; i++) {
if (i)
printf(" %d", num[i]);
else
printf("%d", num[i]);
}
printf("\n");
}
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
set<pair<int, int>> old, nw;
int n, kl[N][N];
int main() {
cin.tie(0);
int k, k1, k2, i, j, mxdp = 0;
string a[N];
cin >> n >> k;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i && j)
kl[i][j] = max(kl[i - 1][j], kl[i][j - 1]);
else if (j)
kl[0][j] = kl[0][j - 1];
else if (i)
kl[i][0] = kl[i - 1][0];
else
kl[0][0] = k;
if (a[i][j] != 'a') kl[i][j]--;
if (kl[i][j] >= 0) mxdp = max(mxdp, i + j + 1);
}
}
for (i = 0; i < mxdp; i++) printf("%c", 'a');
if (mxdp) {
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (kl[i][j] >= 0 && mxdp == i + j + 1) old.insert({i, j});
} else
cout << a[0][0], old.insert({0, 0});
while (!old.empty()) {
char mnc = '~';
for (auto f = old.begin(); f != old.end(); f++) {
k1 = f->first;
k2 = f->second;
if (k1 < n - 1 && k2 < n - 1)
mnc = min(mnc, min(a[k1 + 1][k2], a[k1][k2 + 1]));
else if (k1 < n - 1)
mnc = min(mnc, a[k1 + 1][k2]);
else if (k2 < n - 1)
mnc = min(mnc, a[k1][k2 + 1]);
}
if (mnc != '~') printf("%c", mnc);
for (; !old.empty();) {
auto f = old.begin();
k1 = f->first;
k2 = f->second;
if (k1 < n - 1 && k2 < n - 1) {
if (a[k1 + 1][k2] == mnc) nw.insert({k1 + 1, k2});
if (a[k1][k2 + 1] == mnc) nw.insert({k1, k2 + 1});
} else if (k1 < n - 1 && a[k1 + 1][k2] == mnc)
nw.insert({k1 + 1, k2});
else if (k2 < n - 1 && a[k1][k2 + 1] == mnc)
nw.insert({k1, k2 + 1});
old.erase(f);
}
old.swap(nw);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adjMat(1001, vector<int>(1001));
void connect(vector<int> &a, vector<int> &b) {
for (int x : a) {
for (int y : b) adjMat[x][y] = adjMat[y][x] = 1;
}
a = move(b);
b.clear();
}
int main() {
int k;
cin >> k;
int node = 3;
for (int i = 0; i < 15; i++) {
int curr = k % 4;
k -= k % 4;
k /= 4;
if (curr == 0) continue;
vector<int> prev{1};
vector<int> next;
for (int j = 0; j < curr; j++) next.push_back(node++);
connect(prev, next);
int j = 1;
for (; j <= i; j++) {
for (int k = 0; k < 4; k++) next.push_back(node++);
connect(prev, next);
}
for (; j < 15; j++) {
next.push_back(node++);
connect(prev, next);
}
next.push_back(2);
connect(prev, next);
}
cout << node - 1 << endl;
for (int i = 1; i < node; i++) {
for (int j = 1; j < node; j++) printf("%c", adjMat[i][j] == 1 ? 'Y' : 'N');
printf("\n");
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int c = 805;
int n, ans[c], rem[c][10], ee;
bool v[c], kesz[c];
vector<int> spec, kell = {2, 3, 4, 5, 7, 8};
int veg1, veg2;
bool kerd() {
int db = 0;
for (int i = 1; i <= n; i++) {
db += v[i];
}
cout << "? " << db << " ";
for (int i = 1; i <= n; i++) {
if (v[i]) {
cout << i << " ";
}
}
cout.flush() << endl;
bool ans;
cin >> ans;
return ans;
}
bool kerd2(vector<int> sz) {
cout << "? " << sz.size() << " ";
for (auto x : sz) {
cout << x << " ";
}
cout.flush() << endl;
bool ans;
cin >> ans;
return ans;
}
void szel() {
veg1 = 0, veg2 = 0;
for (int i = 1; i <= n; i++) {
if (v[i]) {
v[i] = 0;
if (kerd()) {
if (!veg1) {
veg1 = i;
} else {
veg2 = i;
}
}
v[i] = 1;
}
}
return;
}
vector<int> keres(int db, int mar) {
int mod = db + 1;
mar %= mod;
for (int i = 0; i < (1 << 10); i++) {
if (__builtin_popcount(i) != db) {
continue;
}
int sum = 0;
vector<int> res;
for (int j = 0; j < 10; j++) {
if (i & (1 << j)) {
sum += ans[spec[j]];
res.push_back(spec[j]);
}
}
if (sum % mod == mar) {
return res;
}
}
}
void mar(int pos, int ert) {
if (ert % 2 == 0) {
vector<int> sz = keres(ert - 1, ert - rem[pos][ert / 2]);
sz.push_back(pos);
rem[pos][ert] = rem[pos][ert / 2];
if (!kerd2(sz)) {
rem[pos][ert] += ert / 2;
}
return;
}
for (int i = 1; i < ert; i++) {
vector<int> sz = keres(ert - 1, i);
sz.push_back(pos);
if (kerd2(sz)) {
rem[pos][ert] = ert - i;
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
srand(time(0));
cin >> n;
for (int i = 1; i <= n; i++) {
v[i] = 1;
}
for (int i = 1; i <= 5; i++) {
if (2 * i > n) {
break;
}
szel();
if (i == 1) {
ee = veg1;
} else {
if (kerd2({ee, veg1}) != (i % 2)) {
swap(veg1, veg2);
}
}
ans[veg1] = i, ans[veg2] = n - i + 1;
v[veg1] = 0, v[veg2] = 0;
spec.push_back(veg1), spec.push_back(veg2);
}
for (int i = 1; i <= n; i++) {
if (v[i]) {
for (auto x : kell) {
mar(i, x);
}
for (int j = 1; j <= n; j++) {
bool jo = 1;
for (auto x : kell) {
if (rem[i][x] != (j % x)) {
jo = 0;
}
}
if (jo) {
ans[i] = j;
}
}
}
}
cout << "! ";
for (int i = 1; i <= n; i++) {
cout << (ans[1] <= n / 2 ? ans[i] : n + 1 - ans[i]) << " ";
}
cout << "\n";
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n;
int s[MAXN], e[MAXN];
int cnt, tot;
struct unq {
int val, id, op;
} u[4 * MAXN];
struct tr {
int cov, l, r, sum;
} t[16 * MAXN], an;
int ans;
bool cmp(const unq &a, const unq &b) { return a.val < b.val; }
tr operator+(const tr &a, const tr &b) {
if (a.r == b.l && a.r) {
return (tr){0, a.l, b.r, a.sum + b.sum - 2};
}
return (tr){0, a.l, b.r, a.sum + b.sum};
}
void update(int l, int r, int z, int y, int v, int id) {
if (l == z && r == y) {
t[id].cov += v;
if (!t[id].cov) {
if (l == r) {
t[id].l = t[id].r = 0;
t[id].sum = 0;
} else
t[id] = t[id * 2] + t[id * 2 + 1];
} else
t[id].r = t[id].l = 1, t[id].sum = 2;
return;
}
int mid = (l + r) / 2;
if (mid >= y)
update(l, mid, z, y, v, id * 2);
else if (mid < z)
update(mid + 1, r, z, y, v, id * 2 + 1);
else {
update(l, mid, z, mid, v, id * 2);
update(mid + 1, r, mid + 1, y, v, id * 2 + 1);
}
if (!t[id].cov) t[id] = t[id * 2] + t[id * 2 + 1];
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
ans = cnt = tot = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &s[i], &e[i]);
s[i] *= 2;
e[i] *= 2;
cnt++;
u[cnt].val = s[i];
u[cnt].id = i;
u[cnt].op = 1;
cnt++;
u[cnt].val = s[i] - 1;
u[cnt].id = i;
u[cnt].op = 3;
cnt++;
u[cnt].val = e[i];
u[cnt].id = i;
u[cnt].op = 2;
cnt++;
u[cnt].val = e[i] + 1;
u[cnt].id = i;
u[cnt].op = 3;
}
sort(u + 1, u + 1 + cnt, cmp);
for (int i = 1; i <= cnt; i++) {
if (i == 1 || u[i].val != u[i - 1].val) tot++;
if (u[i].op == 1) s[u[i].id] = tot;
if (u[i].op == 2) e[u[i].id] = tot;
}
for (int i = 1; i <= n; i++) {
update(1, tot, s[i], e[i], 1, 1);
}
for (int i = 1; i <= n; i++) {
update(1, tot, s[i], e[i], -1, 1);
ans = max(ans, t[1].sum - 2);
update(1, tot, s[i], e[i], 1, 1);
}
printf("%d\n", ans / 2 + 1);
for (int i = 1; i <= n; i++) update(1, tot, s[i], e[i], -1, 1);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<int> construct(int n) {
vector<int> ret = {3};
for (int i = 1; i < n; ++i) ret.push_back(i + 1 < n ? 2 : 2 * n);
return ret;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
vector<int> u = construct(n);
vector<int> v = construct(m);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) printf("%d%c", u[i] * v[j], " \n"[j == m - 1]);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> card(n);
int f = 0;
int s = 0;
for (int i = 0; i < n; i++) {
int sz;
cin >> sz;
card[i].resize(sz);
for (auto &t : card[i]) {
cin >> t;
}
}
multiset<int> uu;
for (int i = 0; i < n; i++) {
if (card[i].size() % 2 == 0) {
for (int j = 0; j < (int)card[i].size(); j++) {
if (j < (int)card[i].size() / 2) {
f += card[i][j];
} else {
s += card[i][j];
}
}
} else {
for (int j = 0; j < (int)card[i].size(); j++) {
if (j < (int)card[i].size() / 2) {
f += card[i][j];
} else if (j == (int)card[i].size() / 2) {
uu.insert(card[i][j]);
} else {
s += card[i][j];
}
}
}
}
int tmp = 0;
while (!uu.empty()) {
if (tmp % 2 == 0) {
f += *uu.rbegin();
} else {
s += *uu.rbegin();
}
uu.erase(--uu.end());
tmp++;
}
cout << f << ' ' << s;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long INF = 1e9;
const int N = 200031;
int n, k, m;
int cost[100][100];
int dp[100][100][100][3];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
cin >> m;
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= n + 1; j++) {
cost[i][j] = 1e9;
}
}
for (int l = 0; l <= n + 1; l++) {
for (int r = 0; r <= n + 1; r++) {
for (int cnt = 0; cnt <= k; cnt++) {
for (int which = 0; which <= 1; which++) {
dp[l][r][cnt][which] = 1e9;
}
}
}
}
for (int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
cost[a][b] = min(cost[a][b], c);
}
for (int i = 1; i <= n; i++) {
dp[i][n + 1][0][0] = 0;
dp[0][i][0][1] = 0;
}
for (int iter = 0; iter < k; iter++) {
for (int l = 0; l <= n + 1; l++) {
for (int r = l; r <= n + 1; r++) {
for (int which = 0; which <= 1; which++) {
if (dp[l][r][iter][which] > 1e8) continue;
for (int next_pt = l + 1; next_pt < r; next_pt++) {
int here = dp[l][r][iter][which];
if (which == 0)
here += cost[l][next_pt];
else
here += cost[r][next_pt];
dp[l][next_pt][iter + 1][1] =
min(dp[l][next_pt][iter + 1][1], here);
dp[next_pt][r][iter + 1][0] =
min(dp[next_pt][r][iter + 1][0], here);
}
}
}
}
}
int ans = 1e9;
for (int i = 0; i <= n + 1; i++) {
for (int j = i; j <= n + 1; j++) {
ans = min(ans, dp[i][j][k - 1][0]);
ans = min(ans, dp[i][j][k - 1][1]);
}
}
if (ans > 1e7) ans = -1;
cout << ans << endl;
cin.get();
cin.get();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, maxDays;
cin >> n;
vector<int> days(n);
cin >> a;
maxDays = a;
days[0] = a;
for (int i = 1; i < n; i++) {
cin >> a;
days[i] = a;
}
if (a < maxDays) maxDays = a;
for (int i = 0; i < n; i++) {
if (days[i] < maxDays) {
if (days[i + 1] < maxDays) {
if (days[i + 1] > days[i])
maxDays = days[i + 1];
else
maxDays = days[i];
}
}
}
cout << maxDays;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> a;
cin >> n;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int tt = 10;
int np = 0;
int pt = 0;
int i = 0;
for (i = 0; i < n; i++) {
if (a[i] + tt > 720) break;
np++;
tt += a[i];
if (tt >= 360) pt += abs(360 - tt);
}
cout << i << " " << pt << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
;
long long n, p, w, d;
cin >> n >> p >> w >> d;
for (long long i = 0; i < w; i++) {
long long val = p - i * d;
if (val % w == 0 && val / w + i <= n && val >= 0) {
cout << val / w << " " << i << " " << n - val / w - i;
return 0;
}
}
cout << -1;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n + 10];
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> v;
int k = 0, l = 0;
for (int i = 1; i <= n; i++) {
l++;
if (a[i] < 0) k++;
if (k >= 3) {
v.push_back(l - 1);
k = 0;
l = 0;
i--;
}
}
if (l != 0) v.push_back(l);
cout << v.size() << "\n";
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void readi(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readll(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = v * 10 + c - '0';
while (isdigit(c = getchar())) v = v * 10 + c - '0';
x = v * f;
}
void readc(char &x) {
char c;
while ((c = getchar()) == ' ')
;
x = c;
}
void writes(string s) { puts(s.c_str()); }
void writeln() { writes(""); }
void writei(int x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
void writell(long long x) {
if (!x) putchar('0');
char a[25];
int top = 0;
while (x) {
a[++top] = (x % 10) + '0';
x /= 10;
}
while (top) {
putchar(a[top]);
top--;
}
}
inline long long inc(int &x) { return ++x; }
inline long long inc(long long &x) { return ++x; }
inline long long inc(int &x, long long y) { return x += y; }
inline long long inc(long long &x, long long y) { return x += y; }
inline double inc(double &x, double y) { return x += y; }
inline long long dec(int &x) { return --x; }
inline long long dec(long long &x) { return --x; }
inline long long dec(int &x, long long y) { return x -= y; }
inline long long dec(long long &x, long long y) { return x -= y; }
inline double dec(double &x, double y) { return x -= y; }
inline long long mul(int &x) { return x = ((long long)x) * x; }
inline long long mul(long long &x) { return x = x * x; }
inline long long mul(int &x, long long y) { return x *= y; }
inline long long mul(long long &x, long long y) { return x *= y; }
inline double mul(double &x, double y) { return x *= y; }
inline long long divi(int &x) { return x = sqrt(x); }
inline long long divi(long long &x) { return x = sqrt(x); }
inline long long divi(int &x, long long y) { return x /= y; }
inline long long divi(long long &x, long long y) { return x /= y; }
inline double divi(double &x, double y) { return x /= y; }
inline long long mod(int &x, long long y) { return x %= y; }
inline long long mod(long long &x, long long y) { return x %= y; }
long long n, m, i, j, cnt, cas;
long long ans;
vector<long long> all;
struct Scanning_line {
long long l, r, h, ad;
} s[200005];
struct seg {
long long l, r, add;
long long sum;
} tr[800005];
void build(long long x, long long l, long long r) {
if (l == r) {
tr[x].l = l;
tr[x].r = r;
tr[x].add = tr[x].sum = 0;
return;
}
tr[x].l = l;
tr[x].r = r;
tr[x].add = tr[x].sum = 0;
build(x * 2, l, (l + r) / 2);
build(x * 2 + 1, (l + r) / 2 + 1, r);
}
void pushup(long long x) {
if (tr[x].add)
tr[x].sum = all[tr[x].r] - all[tr[x].l - 1];
else if (tr[x].l == tr[x].r)
tr[x].sum = 0;
else
tr[x].sum = tr[x * 2].sum + tr[x * 2 + 1].sum;
}
void update(long long x, long long l, long long r, long long c) {
if (tr[x].l > r || tr[x].r < l) return;
if (l <= tr[x].l && tr[x].r <= r) {
tr[x].add += c;
pushup(x);
return;
}
update(x * 2, l, r, c);
update(x * 2 + 1, l, r, c);
pushup(x);
}
bool cmp(Scanning_line x, Scanning_line y) { return x.h < y.h; }
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
cnt = 0;
for (i = 1; i <= n; i++) {
long long w, x, y, z;
cin >> w >> x >> y >> z;
if (w > y) swap(w, y);
if (x > z) swap(x, z);
w--;
x--;
all.push_back(w);
all.push_back(y);
s[++cnt] = {w, y, x, 1};
s[++cnt] = {w, y, z, -1};
}
stable_sort(all.begin(), all.end());
stable_sort(s + 1, s + cnt + 1, cmp);
all.resize(unique(all.begin(), all.end()) - all.begin());
ans = 0;
build(1, 0, all.size());
for (i = 1; i <= cnt; i++) {
if (i != 1 && s[i].h != s[i - 1].h) {
ans += abs(s[i].h - s[i - 1].h) * tr[1].sum;
}
long long l = upper_bound(all.begin(), all.end(), s[i].l) - all.begin();
long long r = upper_bound(all.begin(), all.end(), s[i].r) - all.begin();
update(1, l, r - 1, s[i].ad);
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int cnt[3600];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int h, m;
scanf("\n%d:%d", &h, &m);
cnt[h * 60 + m]++;
}
int ans = 0;
for (int i = 0; i < 1440; i++) {
if (cnt[i] == 0) {
int cur = 1, j = (i + 1) % 1440;
while (j != i && cnt[j] == 0) {
j = (j + 1) % 1440;
cur++;
}
ans = max(ans, cur);
}
}
cout << (ans / 60) / 10 << (ans / 60) % 10 << ":" << (ans % 60) / 10
<< ans % 10;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b;
cin >> n >> a >> b;
int ans = a + b;
while (ans <= 0) {
ans += n;
}
while (ans > n) {
ans -= n;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long n, q, x, y, a[200010], st[500], ed[500], ans = 0, t;
vector<long long> v[500];
int main() {
cin >> n >> q;
for (int i = 0; i < 495; i++) st[i] = -1;
t = (long long)sqrt(n);
for (int i = 0; i < n; i++) {
if (st[i / t] == -1) st[i / t] = i;
ed[i / t] = i;
}
for (int i = 0; i < n; i++) {
a[i] = i + 1;
v[i / t].push_back(a[i]);
}
for (int qn = 0; qn < q; qn++) {
scanf("%I64d%I64d", &x, &y);
x--;
y--;
if (x == y) {
printf("%I64d\n", ans);
continue;
}
if (x > y) swap(x, y);
long long xx = x / t, yy = y / t;
if (xx == yy) {
for (long long i = x + 1; i < y; i++) {
if (a[i] > a[x])
ans++;
else
ans--;
if (a[i] > a[y])
ans--;
else
ans++;
}
} else {
for (long long i = x + 1; i <= ed[xx]; i++) {
if (a[i] > a[x])
ans++;
else
ans--;
if (a[i] > a[y])
ans--;
else
ans++;
}
for (long long i = st[yy]; i < y; i++) {
if (a[i] > a[x])
ans++;
else
ans--;
if (a[i] > a[y])
ans--;
else
ans++;
}
}
if (xx != yy) {
for (long long i = xx + 1; i < yy; i++) {
long long pos =
lower_bound(v[i].begin(), v[i].end(), a[x]) - v[i].begin();
ans += v[i].size() - pos - pos;
pos = lower_bound(v[i].begin(), v[i].end(), a[y]) - v[i].begin();
ans += pos - (v[i].size() - pos);
}
for (int i = 0; i < v[xx].size(); i++) {
if (v[xx][i] == a[x]) {
v[xx].erase(v[xx].begin() + i);
break;
}
}
for (int i = 0; i < v[yy].size(); i++) {
if (v[yy][i] == a[y]) {
v[yy].erase(v[yy].begin() + i);
break;
}
}
bool ok = false;
for (int i = 0; i < v[xx].size(); i++) {
if (v[xx][i] > a[y]) {
v[xx].insert(v[xx].begin() + i, a[y]);
ok = true;
break;
}
}
if (!ok) v[xx].push_back(a[y]);
ok = false;
for (int i = 0; i < v[yy].size(); i++) {
if (v[yy][i] > a[x]) {
v[yy].insert(v[yy].begin() + i, a[x]);
ok = true;
break;
}
}
if (!ok) v[yy].push_back(a[x]);
}
if (a[x] < a[y])
ans++;
else
ans--;
swap(a[x], a[y]);
printf("%I64d\n", ans);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 50;
const int maxa = 2e6 + 50;
int n, a[maxn];
pair<int, int> dp[maxa];
void updata(int x, int pos) {
if (dp[x].first < pos) {
dp[x].second = dp[x].first;
dp[x].first = pos;
} else if (dp[x].second < pos && pos != dp[x].first)
dp[x].second = pos;
}
void get_dp() {
for (int x = maxa - 1; x >= 0; x--) {
for (int i = 20; i >= 0; i--) {
if (((x >> i) & 1) == 1) {
updata(x ^ (1 << i), dp[x].first);
updata(x ^ (1 << i), dp[x].second);
}
}
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
updata(a[i], i);
}
get_dp();
int ans = 0;
for (int i = 1; i <= n - 2; i++) {
int w = 0;
for (int j = 20; j >= 0; j--) {
if (((a[i] >> j) & 1) == 0 && dp[w | (1 << j)].first > i &&
dp[w | (1 << j)].second > i)
w = w | (1 << j);
}
ans = max(ans, a[i] | w);
}
printf("%d", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
inline void prep() {
cin.tie(0);
cin.sync_with_stdio(0);
}
const int MOD = (int)1e9 + 7;
const int MX = (int)2e5 + 1;
int ds[MX];
bool marked[MX];
int Find(int x) { return ds[x] < 0 ? x : ds[x] = Find(ds[x]); }
bool Unite(int x, int y) {
int xx = Find(x);
int yy = Find(y);
if (xx != yy) {
if (ds[xx] > ds[yy]) swap(xx, yy);
ds[xx] += ds[yy];
ds[yy] = xx;
return true;
}
return false;
}
int main() {
prep();
int n;
cin >> n;
memset(ds, 0xff, sizeof(ds));
int p[n + 1];
vector<int> cycles;
int changes = 0;
int root = -1;
for (int i = 1; i <= n; i++) {
cin >> p[i];
if (p[i] == i) root = i;
bool ok = Unite(p[i], i);
if (!ok) {
cycles.push_back(i);
}
}
if (root == -1) {
root = cycles[0];
p[root] = root;
changes = cycles.size();
} else {
changes = cycles.size() - 1;
}
for (auto x : cycles) p[x] = root;
cout << changes << endl;
for (int i = 1; i <= n; i++) cout << p[i] << " \n"[i == n];
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int N = 200000 + 7;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n < 15LL) {
cout << "NO\n";
} else {
n = n % 14LL;
n += 14LL;
if (n >= 15 && n <= 20) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 100005;
int st[nmax], dr[nmax];
long long sum[nmax];
int n, m, mn, mx, i, j, nr, cnt;
struct cv {
long long a;
int b, ind;
} v[nmax], el;
struct CMP {
bool operator()(cv unu, cv doi) { return unu.a > doi.a; }
};
priority_queue<cv, vector<cv>, CMP> pq;
bool comp(cv unu, cv doi) {
if (unu.a == doi.a) return unu.b > doi.b;
return (unu.a < doi.a);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
mn = (1 << 30);
for (i = 1; i <= m; i++) {
cin >> v[i].a >> v[i].b;
v[i].ind = i;
}
sort(v + 1, v + m + 1, comp);
for (i = 1; i <= m; i++) {
if (v[i].b == 1) {
++cnt;
st[v[i].ind] = 1;
dr[v[i].ind] = cnt + 1;
sum[cnt] = 1LL * v[i].a;
}
}
for (i = 1; i < n - 1; i++) {
pq.push({sum[i + 1], i + 1, i + 2});
}
for (i = 1; i <= m; i++) {
if (v[i].b == 0) {
if (pq.empty()) {
cout << "-1";
return 0;
}
el = pq.top();
pq.pop();
if (el.a > v[i].a) {
cout << "-1";
return 0;
}
st[v[i].ind] = el.b;
dr[v[i].ind] = el.ind;
if (el.ind < n) pq.push({1LL * sum[el.ind], el.b, el.ind + 1});
}
}
for (i = 1; i <= m; i++) cout << st[i] << ' ' << dr[i] << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
inline int read() {
register int x = 0;
register int c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = getchar();
}
return x;
}
int n;
int ex[105];
int a, b;
int sum;
int main() {
n = read();
for (int i = 1; i < n; ++i) ex[i] = read();
a = read();
b = read();
for (int i = a; i < b; ++i) sum += ex[i];
printf("%d", sum);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
set<long long> factores;
void fact(int n) {
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
factores.insert(n / i);
factores.insert(i);
}
}
factores.insert(n);
return;
}
long long mulMod(long long a, long long b, long long m = 1000000007) {
long long x = 0, y = a % m;
while (b > 0) {
if (b % 2 == 1) x = (x + y) % m;
y = (y * 2) % m;
b /= 2;
}
return x % m;
}
long long expMod(long long b, long long e, long long m = 1000000007) {
if (!e) return 1;
long long q = expMod(b, e / 2, m);
q = mulMod(q, q, m);
return e % 2 ? mulMod(b, q, m) : q;
}
bool es_primo_prob(long long n, int a) {
if (n == a) return true;
long long s = 0, d = n - 1;
while (d % 2 == 0) s++, d /= 2;
long long x = expMod(a, d, n);
if ((x == 1) || (x + 1 == n)) return true;
for (int i = (0); i < (s - 1); i++) {
x = mulMod(x, x, n);
if (x == 1) return false;
if (x + 1 == n) return true;
}
return false;
}
bool rabin(long long n) {
if (n == 1) return false;
const int ar[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
for (int j = (0); j < (9); j++)
if (!es_primo_prob(n, ar[j])) return false;
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
for (int T = (0); T < (t); T++) {
int n;
cin >> n;
fact(n);
vector<pair<int, int> > pares;
for (auto it = factores.begin(); it != factores.end(); it++) {
long long auxn = n;
long long pr = *it;
long long cont = 0;
if (rabin(pr)) {
while (auxn % pr == 0) {
cont++;
auxn /= pr;
}
pares.push_back(make_pair(pr, cont));
}
}
int tam = ((int)pares.size());
if (tam == 1) {
for (auto it = factores.begin(); it != factores.end(); it++)
cout << *it << " ";
cout << endl;
cout << 0 << endl;
} else if (tam == 2 && pares[0].second == 1 && pares[1].second == 1) {
for (auto it = factores.begin(); it != factores.end(); it++)
cout << *it << " ";
cout << endl;
cout << 1 << endl;
} else if (tam == 2) {
vector<bool> used(((int)factores.size()));
for (int i = (0); i < (((int)factores.size())); i++) used[i] = 0;
for (int i = (0); i < (tam); i++) {
long long no1 = pares[i].first * pares[(i + 1) % tam].first;
long long no2 = pares[i].first * pares[(i - 1 + tam) % tam].first;
int cont = 0;
for (auto it = factores.begin(); it != factores.end(); it++) {
if (used[cont] == 0 && (*it % pares[i].first == 0) && (*it != no1)) {
if (*it != no2) cout << *it << " ";
used[cont] = 1;
}
cont++;
}
if (i == 1) cout << no1 << " ";
}
cout << endl;
cout << 0 << endl;
} else {
vector<bool> used(((int)factores.size()));
for (int i = (0); i < (((int)factores.size())); i++) used[i] = 0;
for (int i = (0); i < (tam); i++) {
long long no1 = pares[i].first * pares[(i + 1) % tam].first;
long long no2 = pares[i].first * pares[(i - 1 + tam) % tam].first;
int cont = 0;
for (auto it = factores.begin(); it != factores.end(); it++) {
if (used[cont] == 0 && (*it % pares[i].first == 0) && (*it != no1)) {
if (*it != no2) cout << *it << " ";
used[cont] = 1;
}
cont++;
}
cout << no1 << " ";
}
cout << endl;
cout << 0 << endl;
}
factores.clear();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
cin >> tc;
while (tc--) {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
if (N & 1) {
if (i == N / 2) {
cout << 'b';
} else if (i == N / 2 + 1) {
cout << 'c';
} else {
cout << 'a';
}
} else {
if (i == N / 2) {
cout << 'b';
} else {
cout << 'a';
}
}
}
cout << '\n';
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, k = 1;
cin >> n;
int t[n];
for (i = 0; i < n; i++) {
cin >> t[i];
if (t[i]) k = 0;
}
if (k)
cout << "EASY";
else
cout << "HARD";
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
int x = 0, neg = 1;
char op = getchar();
while (!isdigit(op)) {
if (op == '-') neg = -1;
op = getchar();
}
while (isdigit(op)) {
x = 10 * x + op - '0';
op = getchar();
}
return neg * x;
}
inline void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
const int N = 200005;
struct ele {
int x;
long long v;
friend bool operator<(const ele &a, const ele &b) { return a.x < b.x; }
} a[N];
int b[N], tot;
int n;
struct Bitcount {
int c[N];
void add(int x, int v) {
while (x < N) {
c[x] += v;
x += (x & -x);
}
}
int qry(int x) {
int ret = 0;
if (x < 0) return 0;
while (x > 0) {
ret += c[x];
x -= (x & -x);
}
return ret;
}
int query(int l, int r) { return qry(r) - qry(l - 1); }
} bitc;
struct Bitsum {
long long c[N];
void add(int x, long long v) {
while (x < N) {
c[x] += v;
x += (x & -x);
}
}
long long qry(int x) {
long long ret = 0;
if (x < 0) return 0;
while (x > 0) {
ret += c[x];
x -= (x & -x);
}
return ret;
}
long long query(int l, int r) { return qry(r) - qry(l - 1); }
} bits;
int main() {
n = read();
for (register int i = 1; i <= n; i++) {
a[i].x = read();
}
for (register int i = 1; i <= n; i++) {
a[i].v = read();
b[++tot] = a[i].v;
}
sort(b + 1, b + tot + 1);
tot = unique(b + 1, b + tot + 1) - (b + 1);
sort(a + 1, a + n + 1);
for (register int i = 1; i <= n; i++) {
a[i].v = lower_bound(b + 1, b + tot + 1, a[i].v) - b;
}
long long ans = 0ll;
for (register int i = n; i >= 1; i--) {
long long possum = bits.query(a[i].v, N - 1);
long long posnum = bitc.query(a[i].v, N - 1);
ans += possum - 1ll * a[i].x * posnum;
bits.add(a[i].v, a[i].x);
bitc.add(a[i].v, 1);
}
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int arr[13];
int main() {
int n;
cin >> n;
int i;
int maxi = 0;
for (i = 0; i < n; i++) {
cin >> arr[i];
maxi = max(maxi, arr[i]);
}
cout << (maxi ^ arr[n - 1]) << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
return bigmod(a, M - 2, M);
}
vector<int> vc[300001];
int res[300001];
void dfs(int c) {
int cnt = 0;
for (int i = (0); i < (vc[c].size()); ++i)
if (res[c] == res[vc[c][i]]) cnt++;
if (cnt > 1) {
res[c] ^= 1;
for (int i = (0); i < (vc[c].size()); ++i)
if (res[c] == res[vc[c][i]]) dfs(vc[c][i]);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b, c, d = 0, e, f = 0, g, h = 0, x, y, z = 0;
cin >> a >> b;
for (int i = (0); i < (b); ++i) {
cin >> c >> d;
vc[c].push_back(d);
vc[d].push_back(c);
}
for (int i = (1); i < (a + 1); ++i) dfs(i);
for (int i = (1); i < (a + 1); ++i) cout << res[i];
cout << endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long powmod(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent) {
if (exponent & 1) ans = (ans * base) % mod;
base = (base * base) % mod;
exponent /= 2;
}
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
const int upperlimit = 1e6 + 100;
const int mod = 1e9 + 7;
void plusone(string &s) {
for (int i = s.size() - 1; i >= 0; i--) {
if (s[i] != '9') {
s[i]++;
break;
} else
s[i] = '0';
}
}
bool cmp(string a, string b) {
for (int i = 0; i < a.size(); i++) {
int j = i;
j %= b.size();
if (a[i] > b[j]) return false;
if (a[i] < b[j]) return true;
}
return true;
}
int main() {
string s, c;
int n, k;
cin >> n >> k >> s;
c.append(s, 0, min(n, k));
if (!cmp(s, c)) plusone(c);
cout << n << "\n";
for (int i = 0; i < n; i++) cout << c[i % k];
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vpii = vector<pii>;
using vpll = vector<pll>;
using vpdd = vector<pdd>;
template <typename T>
void ckmin(T& a, const T& b) {
a = min(a, b);
}
template <typename T>
void ckmax(T& a, const T& b) {
a = max(a, b);
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
namespace __input {
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
template <class Arg, class... Args>
void re(Arg& first, Args&... rest) {
re(first);
re(rest...);
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = 0; i < (int((a).size())); i++) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = 0; i < (SZ); i++) re(a[i]);
}
} // namespace __input
using namespace __input;
namespace __output {
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T, size_t SZ>
void pr(const array<T, SZ>& x);
template <class T>
void pr(const vector<T>& x);
template <class T>
void pr(const set<T>& x);
template <class T1, class T2>
void pr(const map<T1, T2>& x);
template <class T>
void pr(const T& x) {
cout << x;
}
template <class Arg, class... Args>
void pr(const Arg& first, const Args&... rest) {
pr(first);
pr(rest...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T, bool pretty = true>
void prContain(const T& x) {
if (pretty) pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? pretty ? ", " : " " : "", a), fst = 0;
if (pretty) pr("}");
}
template <class T>
void pc(const T& x) {
prContain<T, false>(x);
pr("\n");
}
template <class T, size_t SZ>
void pr(const array<T, SZ>& x) {
prContain(x);
}
template <class T>
void pr(const vector<T>& x) {
prContain(x);
}
template <class T>
void pr(const set<T>& x) {
prContain(x);
}
template <class T1, class T2>
void pr(const map<T1, T2>& x) {
prContain(x);
}
void ps() { pr("\n"); }
template <class Arg>
void ps(const Arg& first) {
pr(first);
ps();
}
template <class Arg, class... Args>
void ps(const Arg& first, const Args&... rest) {
pr(first, " ");
ps(rest...);
}
} // namespace __output
using namespace __output;
namespace __algorithm {
template <typename T>
void dedup(vector<T>& v) {
sort((v).begin(), (v).end());
v.erase(unique((v).begin(), (v).end()), v.end());
}
template <typename T>
typename vector<T>::iterator find(const vector<T>& v, const T& x) {
auto it = lower_bound((v).begin(), (v).end(), x);
return it != v.end() && *it == x ? it : v.end();
}
template <typename T>
size_t index(const vector<T>& v, const T& x) {
auto it = find(v, x);
assert(it != v.end() && *it == x);
return it - v.begin();
}
template <typename T1, typename T2>
typename vector<pair<T1, T2>>::iterator lower_bound(
const vector<pair<T1, T2>>& v, const T1& x) {
return lower_bound(
(v).begin(), (v).end(), x,
[](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; });
}
template <typename T1, typename T2>
typename vector<pair<T1, T2>>::iterator upper_bound(
const vector<pair<T1, T2>>& v, const T1& x) {
return upper_bound(
(v).begin(), (v).end(), x,
[](pair<T1, T2> a, pair<T1, T2> b) { return a.first < b.first; });
}
} // namespace __algorithm
using namespace __algorithm;
struct monostate {
friend istream& operator>>(istream& is,
const __attribute__((unused)) monostate& ms) {
return is;
}
friend ostream& operator<<(ostream& os,
const __attribute__((unused)) monostate& ms) {
return os;
}
} ms;
template <typename W = monostate>
struct wedge {
int u, v, i;
W w;
wedge<W>(int _u = -1, int _v = -1, int _i = -1) : u(_u), v(_v), i(_i) {}
int operator[](int loc) const { return u ^ v ^ loc; }
friend void re(wedge& e) {
re(e.u, e.v, e.w);
--e.u, --e.v;
}
friend void pr(const wedge& e) { pr(e.u, "<-", e.w, "->", e.v); }
};
namespace __io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (int((second).size())) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace __io
using namespace __io;
template <typename E>
struct tree {
int N;
vvi adj;
vector<E> edges;
tree() {}
tree(int _N) : N(_N) {
adj.resize(N);
edges.resize(N - 1);
}
tree(const vector<E>& __edges) : tree(int((__edges).size()) + 1) {
edges = __edges;
__init();
}
void __init() {
for (int i = 0; i < (int((edges).size())); i++) {
auto& e = edges[i];
e.i = i;
assert(0 <= e.u && e.u < N), adj[e.u].push_back(i);
assert(0 <= e.v && e.v < N), adj[e.v].push_back(i);
}
}
friend void re(tree& t) {
re(t.edges);
t.__init();
}
friend void pr(const tree& t) { pr("{N=", t.N, " ", t.edges, "}"); }
template <typename T>
T& go(vector<T>& down, int loc, int ine) const {
T& res = down[loc] = T::leaf(loc);
for (auto& ei : adj[loc])
if (ei != ine) res.attach(go(down, edges[ei][loc], ei).extend());
return res;
}
template <typename T>
vector<T> down(int root) const {
vector<T> down(N);
go(down, root, -1);
return down;
}
template <typename T>
void again(vector<T>& down, vector<T>& up, int loc, int ine) const {
const int D = int((adj[loc]).size());
vector<T> pref(D), suff(D);
for (int i = 0; i < (D); i++) {
const E& e = edges[adj[loc][i]];
pref[i] = suff[i] = ((e.i != ine) ? down[e[loc]] : up[loc]).extend();
}
for (int i = 0; i < (D - 1); i++) pref[i + 1] += pref[i];
for (int i = (D - 1) - 1; i >= 0; i--) suff[i] += suff[i + 1];
for (int i = 0; i < (D); i++) {
const E& e = edges[adj[loc][i]];
if (e.i != ine) {
T& cres = up[e[loc]] = T::leaf(loc);
if (i) cres.attach(pref[i - 1]);
if (i + 1 < D) cres.attach(suff[i + 1]);
again(down, up, e[loc], e.i);
}
}
}
template <typename T>
array<vector<T>, 3> both() const {
vector<T> down(N);
go(down, 0, -1);
vector<T> up(N);
again(down, up, 0, -1);
vector<T> root = down;
for (int i = (1); i < (N); i++) root[i].attach(up[i].extend());
return {down, up, root};
}
};
using edge = wedge<>;
ll nc2(ll v) { return v * (v - 1) / 2; }
int N;
struct subtree {
int id, size;
ll ans, rpath;
vector<pair<int, ll>> cchoice;
static subtree leaf(int id) { return {id, 1, LLONG_MAX, 0ll, {}}; }
subtree& extend() {
decltype(cchoice) shrunk;
sort((cchoice).begin(), (cchoice).end());
for (auto& elt : cchoice)
if (int((shrunk).size()) < 2 ||
shrunk[int((shrunk).size()) - 2].first != elt.first)
shrunk.push_back(elt);
rpath = nc2(size);
for (auto [ss, rc] : shrunk) ckmin(rpath, rc + nc2(size - ss));
for (auto it1 = shrunk.begin(); it1 != shrunk.end(); it1++) {
ckmin(ans, it1->second + nc2(N - it1->first));
for (auto it2 = next(it1); it2 != shrunk.end(); it2++) {
ckmin(ans,
it1->second + it2->second + nc2(N - it1->first - it2->first));
}
}
return *this;
}
void attach(const subtree& o) {
size += o.size;
cchoice.emplace_back(o.size, o.rpath);
ckmin(ans, o.ans);
}
subtree operator+=(const subtree& o) {
size += o.size;
return *this;
}
subtree operator+(const subtree& o) const { return subtree(*this) += o; }
friend void pr(const subtree& second) {
pr("{id=", second.id, " sz=", second.size, "}");
}
};
int main() {
setIO();
re(N);
tree<edge> tr(N);
re(tr);
ps(2 * nc2(N) - tr.down<subtree>(0)[0].extend().ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
vector<long long> v(n + 1);
for (long long i = 1; i <= n; i++) {
cin >> v[i];
}
vector<long long> s(n + 1), p(n + 1);
for (long long i = 1; i <= n; i++) {
p[i] = v[i];
if (i >= 2) p[i] += p[i - 2];
}
for (long long i = n; i >= 1; i--) {
s[i] = v[i];
if (i + 2 <= n) s[i] += s[i + 2];
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
ans = max(ans, s[i] + p[i - 1]);
}
cout << ans;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
int ans = 0;
for (int lenght = 1, checker; lenght < s.length(); lenght++) {
checker = 0;
for (int i = 0; i <= s.length() - lenght; i++) {
checker = 1;
for (int j = i + 1; j <= s.length() - lenght; j++) {
if (s.substr(i, lenght) == s.substr(j, lenght)) checker++;
}
if (checker >= 2) {
ans = max(ans, lenght);
break;
}
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
using namespace std;
ios_base::sync_with_stdio(false), cin.tie(nullptr);
string S;
cin >> S;
int N = int(S.size());
int X;
cin >> X;
int factor_mask = 0;
for (int v = 1; v < X; v++) {
if (X % v == 0) {
factor_mask |= 1 << (v - 1);
}
}
vector<int> dp(1 << (X - 1), N + 1);
dp[0] = 0;
vector<int> ndp(1 << (X - 1));
for (char c : S) {
for (int m = 0; m < (1 << (X - 1)); m++) {
ndp[m] = dp[m] + 1;
}
for (int m = 0; m < (1 << (X - 1)); m++) {
if (dp[m] > N) continue;
int nm = (m * 2 + 1) << (c - '1');
int a = nm & factor_mask;
if (a) nm &= (a - 1) & ~a;
if (nm & (1 << (X - 1))) {
continue;
}
nm &= (1 << (X - 1)) - 1;
ndp[nm] = min(ndp[nm], dp[m]);
}
swap(dp, ndp);
}
cout << *min_element(dp.begin(), dp.end()) << '\n';
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5;
const long long mod = 1e9 + 7;
long long test, n, query, q, k, value, m, x, val, a, b, c;
long long arr[N], brr[N];
int main() {
scanf("%lld", &test);
while (test--) {
scanf("%lld", &n);
arr[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &arr[i]);
}
long long dp[n + 1][n + 1];
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; j++) {
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; ++i) {
dp[i][i] = 1;
}
for (int i = 1; i < n; ++i) {
long long row = 1;
long long col = row + i;
while (col <= n) {
if (arr[row] == arr[col]) {
dp[row][col] = 2 + dp[row + 1][col - 1];
} else {
dp[row][col] = max(dp[row + 1][col], dp[row][col - 1]);
}
row++;
col++;
}
}
if (dp[1][n] >= 3) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double const EPS = 1e-9, PI = acos(-1);
const int N = 1e6 + 9, M = 3e4 + 9, OO = 1e9 + 7, MOD = 1e9 + 7;
const long long inf = 1e18;
long long arr[N], mem[N][4], a, b;
int n, val;
set<int> primes;
vector<int> vec;
void factorize(int n) {
if (!(n & 1)) primes.insert(2);
while (!(n & 1)) n >>= 1;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) primes.insert(i);
while (n % i == 0) n /= i;
}
if (n > 1) primes.insert(n);
}
long long solve(int x, int state) {
if (x == n) return 0;
long long& ret = mem[x][state];
if (~ret) return ret;
ret = inf;
if (state == 0) {
if (arr[x] % val == 0) ret = min(ret, solve(x + 1, state));
if (((arr[x] + 1) % val == 0) || ((arr[x] - 1) % val == 0))
ret = min(ret, solve(x + 1, state) + b);
ret = min(ret, solve(x + 1, 1) + a);
ret = min(ret, solve(x + 1, 2) + a);
} else if (state == 1) {
ret = min(ret, solve(x + 1, 1) + a);
ret = min(ret, solve(x + 1, 2) + a);
} else {
if (arr[x] % val == 0) ret = min(ret, solve(x + 1, state));
if (((arr[x] + 1) % val == 0) || ((arr[x] - 1) % val == 0))
ret = min(ret, solve(x + 1, state) + b);
}
return ret;
}
int main() {
cout << fixed << setprecision(0), ios::sync_with_stdio(false),
cin.tie(nullptr), cout.tie(nullptr);
cin >> n >> a >> b;
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = -1; i <= 1; ++i) {
factorize(arr[0] + i);
factorize(arr[n - 1] + i);
}
vec = vector<int>(primes.begin(), primes.end());
long long ans = inf;
for (int it : vec) {
memset(mem, -1, sizeof(mem)), val = it;
ans = min(ans, solve(0, 0));
}
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[60][60], b[60][60];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) scanf("%d", &b[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
int minn = min(a[i][j], b[i][j]);
int maxn = max(a[i][j], b[i][j]);
a[i][j] = minn;
b[i][j] = maxn;
}
int flag = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] <= a[i - 1][j] || a[i][j] <= a[i][j - 1] ||
b[i][j] <= b[i - 1][j] || b[i][j] <= b[i][j - 1]) {
flag = 1;
break;
}
if (flag == 0)
printf("Possible\n");
else
printf("Impossible\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string *s = new string[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int groups = 0;
for (int i = 0; i < n; i++) {
int j = i;
for (; j < n; j++) {
if (s[j][0] == s[i][1]) {
break;
}
}
groups++;
i = j - 1;
}
cout << groups;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const vector<int> mods = {998244353, 1000000007, 1000000009};
const long long inf = 3e18;
const double pi = acos(-1.0);
const double eps = 0.0000001;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class... K>
using umap = unordered_map<K...>;
template <class... K>
using uset = unordered_set<K...>;
const int N = 100100;
const int M = 300100;
long long n, a[N], tt, g[M], inv[M], res;
long long pw(long long a, long long e) {
long long r = 1;
while (e) {
if (e & 1) r = (r * a) % mod;
a = (a * a) % mod;
e >>= 1;
}
return r;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cout.precision(13);
cin >> n;
for (long long i = (0); i < (n); ++i) {
cin >> a[i];
tt += a[i];
}
if (tt == 1) {
cout << "0\n";
return 0;
}
for (long long i = (1); i < (M); ++i) inv[i] = pw(i, mod - 2);
for (long long i = tt; i >= 1; i--) {
g[i] = (g[i + 1] * (n - 1) % mod * (tt - i) % mod + tt * (n - 1) % mod) %
mod * inv[i] % mod;
}
for (long long i = (1); i < (tt + 1); ++i) g[i] = (g[i] + g[i - 1]) % mod;
res = (1 - n + mod) % mod * g[tt] % mod;
for (long long i = (0); i < (n); ++i) res = (res + g[tt - a[i]]) % mod;
res = res * inv[n] % mod;
cout << res << "\n";
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
long long sqr(long long a) { return a * a; }
struct gr {
bool l[111];
};
long long n, m;
vector<gr> v;
gr g;
int main() {
cin >> n >> m;
long long ans = 0;
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
for (int j = 1; j <= m; ++j) {
g.l[j] = false;
}
for (int j = 0; j < k; ++j) {
int x;
cin >> x;
g.l[x] = true;
}
if (k == 0) {
++ans;
} else {
v.push_back(g);
}
}
for (int i = 0; i < v.size(); ++i) {
if (i < v.size()) {
for (int j = i + 1; j < v.size(); ++j) {
bool good = false;
for (int k = 1; k <= m; ++k) {
if (v[i].l[k] == v[j].l[k] && v[i].l[k] == true) {
good = true;
}
}
if (good) {
for (int k = 1; k <= m; ++k) {
v[i].l[k] = v[i].l[k] || v[j].l[k];
}
v[j] = v.back();
v.pop_back();
j = i;
}
}
}
}
if (v.size() != 0) {
cout << v.size() - 1 + ans << endl;
} else {
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 30 + 20;
int n, a[N][N], row[N], col[N], sum;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
row[i] += a[i][j];
}
}
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
col[j] += a[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (col[i] > row[j]) {
sum++;
}
}
}
cout << sum;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 9;
const double pi = 3.14159265358979323846264;
const long long inf = 2e17;
const int mod = 10007;
double x[maxn], y[maxn];
double sol(double x1, double y1, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
double a, b;
int n;
cin >> n >> a >> b;
for (int i = 0; i < n; ++i) scanf("%lf%lf", &x[i], &y[i]);
double p = 0, q = 1e17;
for (int i = 0; i < n; ++i) {
p = max(p, sol(x[i], y[i], a, b));
int k = i + 1;
if (k == n) k = 0;
double px = x[k] - x[i], py = y[k] - y[i];
double l = 0, r = 1;
double tmp;
for (int j = 0; j < 50; ++j) {
double mid = (l + r) / 2;
double midl = (l + mid) / 2, midr = (r + mid) / 2;
double p1 = sol(x[i] + midl * px, y[i] + midl * py, a, b);
double p2 = sol(x[i] + midr * px, y[i] + midr * py, a, b);
if (p1 < p2)
tmp = p1, r = midr;
else
tmp = p2, l = midl;
}
q = min(q, tmp);
}
printf("%.11lf", pi * p * p - pi * q * q);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int arr[5];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int n, a;
cin >> n;
while (n--) {
cin >> a;
arr[a]++;
}
int ans = 0;
int tmp = min(arr[1], arr[2]);
arr[3] += tmp;
arr[1] -= tmp;
arr[2] -= tmp;
ans += tmp;
if (arr[1]) {
int tmpans1 = INT_MAX, tmpans2 = INT_MAX, mod;
tmp = 0;
bool ok1 = true, ok2 = true;
for (int i = 0; arr[i] - i * 3 >= 0; i++) {
tmp = 0;
bool ok3 = false;
mod = (arr[1] - i * 3) % 4;
tmp += ((arr[1] - i * 3) / 4) * 3;
if (mod == 1 && arr[3])
tmp++;
else if (mod == 1 && arr[4] >= 2)
tmp += 2;
else if (mod == 2 && arr[4])
tmp += 2;
else if (mod == 3)
tmp += 2;
else if (mod != 0)
ok3 = true;
tmp += (i)*2;
if (ok3) continue;
ok2 = false;
tmpans1 = min(tmpans1, tmp);
}
for (int i = 0; arr[i] - i * 4 >= 0; i++) {
tmp = 0;
bool ok3 = false;
mod = (arr[1] - i * 4) % 3;
tmp += ((arr[1] - i * 4) / 3) * 2;
if (mod == 1 && arr[3])
tmp++;
else if (mod == 1 && arr[4] >= 2)
tmp += 2;
else if (mod == 2 && arr[4])
tmp += 2;
else if (mod == 2 && arr[3] >= 2)
tmp += 2;
else if (mod != 0)
ok3 = true;
tmp += (i)*3;
if (ok3) continue;
ok1 = false;
tmpans2 = min(tmpans2, tmp);
}
if (ok1 && ok2) {
puts("-1\n");
return 0;
}
if (ok1)
ans += tmpans1;
else if (ok2)
ans += tmpans2;
else
ans += min(tmpans2, tmpans1);
} else if (arr[2]) {
tmp = 0;
int tmpans = INT_MAX;
bool ok = false;
for (int i = 0; i <= arr[2]; i++) {
tmp = 0;
int twos = arr[2];
if (i * 2 >= twos - i) {
tmp += twos - i;
int diff = i * 2 - (twos - i);
if (diff / 2 == 1 && arr[3] >= 2)
tmp += 2;
else if (diff / 2 == 1)
continue;
else
tmp += diff / 2 + diff % 2;
} else {
tmp += i * 2;
int diff = (twos - i) - i * 2;
tmp += min(diff, arr[4]);
diff -= min(diff, arr[4]);
if (diff == 1 && arr[3] >= 2)
tmp += 2;
else if (diff == 1)
continue;
else
tmp += (diff / 2) * 2 + diff % 2;
}
ok = true;
tmpans = min(tmp, tmpans);
}
if (!ok) {
cout << -1;
return 0;
}
ans += tmpans;
}
cout << ans << '\n';
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, k, a[16], b[16];
bitset<2005> f[1 << 16];
bool vis[16];
void solve(int s, int i) {
if (!(s & (s - 1))) return;
for (int u = 0; u <= n - 1; ++u)
if (s >> u & 1 && a[u] < i && f[s ^ (1 << u)][i - a[u]]) {
solve(s ^ (1 << u), i - a[u]);
return;
}
solve(s, i * k);
for (int u = 0; u <= n - 1; ++u)
if (s >> u & 1) ++b[u];
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 0; i <= n - 1; ++i) scanf("%d", a + i);
for (int i = 0; i <= n - 1; ++i) f[1 << i][a[i]] = 1;
for (int s = 1; s <= (1 << n) - 1; ++s)
if (s & (s - 1)) {
int z = 0;
for (int u = 0; u <= n - 1; ++u)
if (s >> u & 1) {
z += a[u];
f[s] |= f[s ^ (1 << u)] << a[u];
}
for (int i = z / k; i >= 1; --i)
if (f[s][i * k]) f[s][i] = 1;
}
if (!f[(1 << n) - 1][1]) return puts("NO"), 0;
puts("YES");
solve((1 << n) - 1, 1);
for (int T = 1; T <= n - 1; ++T) {
int mx = 0;
for (int i = 0; i <= n - 1; ++i)
if (!vis[i]) mx = max(mx, b[i]);
int x = -1, y = -1;
for (int i = 0; i <= n - 1; ++i)
if (!vis[i] && mx == b[i]) {
if (!~x)
x = i;
else if (!~y)
y = i;
}
printf("%d %d\n", a[x], a[y]);
vis[y] = 1;
a[x] += a[y];
while (a[x] % k == 0) a[x] /= k, --b[x];
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int debug = 0;
bool is_vowel(char c);
bool is_prime(long long n);
long long bigmod(long long base, long long exp, long long mod);
vector<long long> factor(long long n);
char ara[200010];
int pal(int s, int e, int l, int r) {
if (l > s or r < e) return 0;
if (ara[s] != ara[e]) return 0;
if (s >= e) return 0;
if (s == l or e == r) {
if (s > l and e > l)
return 2;
else
return 0;
}
return 2 + pal(s - 1, e + 1, l, r);
}
int main() {
scanf("%s", ara);
int l = strlen(ara);
stack<int> stk;
int cnt = 0;
for (int i = 0; i < l; i++) {
if (i == 0)
stk.push(ara[i]);
else {
if (stk.size() and ara[i] == stk.top()) {
cnt++;
stk.pop();
} else
stk.push(ara[i]);
}
}
if (cnt % 2 == 0)
cout << "No\n";
else
cout << "Yes\n";
return 0;
}
bool is_vowel(char c) {
if (c < 95) {
c += 32;
}
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}
bool is_prime(long long n) {
if (n == 1) return false;
long long l = (long long)sqrt(n);
for (int i = 2; i <= l; i++) {
if (n % i == 0) return false;
}
return true;
}
long long bigmod(long long base, long long exp, long long mod) {
if (exp == 0) return 1;
if (base == 0) return -1;
if (exp % 2 == 0) {
long long ans = ((bigmod(base, exp / 2, mod) % mod) + mod) % mod;
return (((ans * ans) % mod) + mod) % mod;
} else
return (((((base % mod) + mod) % mod) * bigmod(base, exp - 1, mod)) + mod) %
mod;
}
vector<long long> factor(long long n) {
vector<long long> factor;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
factor.push_back(i);
if (n / i != i) factor.push_back(n / i);
}
}
return factor;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int w, h, n, p, p1, p2;
char c;
set<int> a[4];
multiset<int> b[2];
set<int>::iterator wi, hi;
void solve(int d, int p) {
p1 = *a[d].lower_bound(p);
p2 = -(*a[d + 2].lower_bound(-p));
a[d].insert(p);
a[d + 2].insert(-p);
b[d].erase(b[d].find(p1 - p2));
b[d].insert(p1 - p);
b[d].insert(p - p2);
wi = b[0].end();
wi--;
hi = b[1].end();
hi--;
cout << (long long)(*wi) * (*hi) << endl;
}
int main() {
cin >> w >> h >> n;
a[0].insert(0);
a[0].insert(w);
a[1].insert(0);
a[1].insert(h);
a[2].insert(0);
a[2].insert(-w);
a[3].insert(0);
a[3].insert(-h);
b[0].insert(w);
b[1].insert(h);
for (int i = 0; i < n; i++) {
cin >> c >> p;
if (c == 'H')
solve(1, p);
else
solve(0, p);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
float n, k;
cin >> n >> k;
if (k / 2 <= n) {
cout << n;
} else {
k = k - 2 * n;
if (n - k < 0) {
cout << "0";
} else
cout << n - k;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int c[105];
int N;
int X, Y;
int t;
int main() {
int i;
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &t);
c[i] += t;
}
for (i = 1; i <= N; i++) {
c[i] += c[i - 1];
}
scanf("%d %d", &X, &Y);
for (i = 1; i <= N; i++) {
if ((X <= c[i - 1] && c[i - 1] <= Y) && (X <= c[N] - c[i - 1]) &&
(c[N] - c[i - 1] <= Y)) {
printf("%d\n", i);
return 0;
}
}
printf("0\n");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << "? ";
for (int i = 0; i < 100; i++) cout << i << " ";
cout << endl;
int first;
cin >> first;
cout << "? ";
int cnt = 128;
for (int i = 0; i < 100; i++) {
cout << cnt << " ";
cnt += 128;
}
cout << endl;
int second;
cin >> second;
int ans = (first ^ second) % 128;
ans = ans ^ first;
cout << "! ";
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
int main() {
int n;
cin >> n;
if (n % 4 == 2 || n % 4 == 3) return puts("NO"), 0;
puts("YES");
vector<pair<int, int> > ans;
for (int i = 1; i < n; i += 2)
for (int j = i + 2; j < n; j += 2) {
ans.push_back({i, j});
ans.push_back({i + 1, j + 1});
ans.push_back({i + 1, j});
ans.push_back({i, j + 1});
}
if (n % 4 == 0) {
for (int i = 1; i < n; i += 2) ans.push_back({i, i + 1});
} else {
for (int i = 1; i < n; i += 2) {
ans.push_back({n, i});
ans.push_back({i, i + 1});
ans.push_back({n, i + 1});
}
}
for (auto p : ans) {
if (p.first > p.second) swap(p.first, p.second);
printf("%d %d\n", p.first, p.second);
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
long long cnt, sum, mx, ans = 0;
bool flag, gov[1010], vis[1010];
vector<int> vec, edges[1010];
void dfs(int u) {
vis[u] = 1;
for (int v : edges[u])
if (!vis[v]) dfs(v);
++cnt;
flag |= gov[u];
}
int main() {
int u, v, i;
cin >> n >> m >> k;
for (i = 0; i < k; ++i) {
scanf("%d", &u);
gov[u] = 1;
}
for (i = 0; i < m; ++i) {
scanf("%d %d", &u, &v);
edges[u].push_back(v);
edges[v].push_back(u);
}
for (i = 1; i <= n; ++i) {
if (vis[i]) continue;
cnt = flag = 0;
dfs(i);
ans += cnt * (cnt - 1) / 2;
if (flag)
mx = max(mx, cnt);
else
vec.push_back(cnt);
}
for (int v : vec) {
ans += mx * v;
mx += v;
}
cout << ans - m << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
long long mod = 1e9 + 7;
long long powm(long long a, long long b, long long mod) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
long long n, k;
vector<long long> a;
void solve() {
cin >> n >> k;
a.resize(n);
for (long long i = 0; i < (n); i++) cin >> a[i];
sort(a.begin(), a.end());
deque<pair<long long, long long> > freq;
freq.push_back(make_pair(a[0], 1));
for (long long i = (1); i < (n); i++) {
if (a[i - 1] != a[i]) freq.push_back(make_pair(a[i], 0));
(freq.back()).second++;
}
while (freq.size() > 1 and k > 0) {
pair<long long, long long> fnt = freq.front();
pair<long long, long long> bck = freq.back();
if (bck.second > fnt.second) {
freq.pop_front();
pair<long long, long long> tmp_f = freq.front();
long long new_f = min((tmp_f.first - fnt.first), (k / fnt.second));
k -= new_f * fnt.second;
if (new_f == 0) {
cout << bck.first - fnt.first - new_f << endl;
return;
}
if (new_f + fnt.first == tmp_f.first)
freq.front().second += fnt.second;
else
freq.push_front(make_pair(fnt.first + new_f, fnt.second));
} else {
freq.pop_back();
pair<long long, long long> tmp_b = freq.back();
long long new_b = min((bck.first - tmp_b.first), k / bck.second);
k -= new_b * bck.second;
if (new_b == 0) {
cout << bck.first - fnt.first - new_b << endl;
return;
}
if (bck.first - new_b == tmp_b.first)
freq.back().second += bck.second;
else
freq.push_back(make_pair(bck.first - new_b, bck.second));
}
}
if (freq.size() == 1)
cout << 0 << endl;
else
cout << freq.back().first - freq.front().first << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int tc = 1;
while (tc--) {
solve();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
bool wa = false;
int n, x0, first, last, s, e;
scanf("%d%d", &n, &x0);
scanf("%d%d", &s, &e);
first = min(s, e);
last = max(s, e);
for (int i = 1; i < n; i++) {
scanf("%d%d", &s, &e);
if (min(s, e) > first) first = min(s, e);
if (max(s, e) < last) last = max(s, e);
if (first > last) wa = true;
}
if (wa)
printf("-1");
else if (first <= x0 && last >= x0)
printf("0");
else
printf("%d", min(abs(x0 - last), abs(x0 - first)));
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
int A[100004];
int B[100004];
int freqA[100004];
int freqB[100004];
set<int, greater<int> > S;
vector<pair<int, int> > P[100004];
int next(int x) {
int res = *S.begin();
set<int, greater<int> >::const_iterator it = S.upper_bound(M - x);
if (it != S.end()) {
if ((*it + x) % M > (res + x) % M) res = *it;
}
return res;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N; ++i) {
cin >> A[i];
++freqA[A[i]];
}
for (int i = 0; i < N; ++i) {
cin >> B[i];
++freqB[B[i]];
S.insert(B[i]);
}
for (int i = 0; i < N; ++i) {
int nxt = next(A[i]);
P[(nxt + A[i]) % M].push_back(pair<int, int>(A[i], nxt));
}
vector<int> R;
for (int m = M - 1; m >= 0; --m) {
for (int j = 0; j < P[m].size(); ++j) {
pair<int, int> cur = P[m][j];
int mn = min(freqA[cur.first], freqB[cur.second]);
if (mn == 0) {
if (freqA[cur.first] > 0) {
int nxt = next(cur.first);
P[(cur.first + nxt) % M].push_back(pair<int, int>(cur.first, nxt));
}
continue;
}
R.insert(R.end(), mn, m);
freqA[cur.first] -= mn;
freqB[cur.second] -= mn;
if (freqB[cur.second] == 0) {
S.erase(cur.second);
if (freqA[cur.first] > 0) {
int nxt = next(cur.first);
P[(cur.first + nxt) % M].push_back(pair<int, int>(cur.first, nxt));
}
}
}
P[m].clear();
}
for (int i = 0; i < R.size(); ++i) cout << R[i] << ' ';
cout << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
int main() {
int n, x, y, a, b, v;
double t, t2;
scanf("%d %d %d", &x, &y, &n);
scanf("%d %d %d", &a, &b, &v),
t = (double)sqrt(pow((a - x), 2) + pow((b - y), 2)) / v;
for (int i = 1; i < n; i++) {
scanf("%d %d %d", &a, &b, &v),
t2 = (double)sqrt(pow((a - x), 2) + pow((b - y), 2)) / v;
if (t2 < t) t = t2;
}
printf("%f", t);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int dfs(vector<int>& v, int bit) {
if (bit == -1) {
return 0;
}
vector<int> zero, one;
for (int i = (int)0; i < (int)v.size(); ++i) {
if (v[i] & 1 << bit) {
one.push_back(v[i]);
} else {
zero.push_back(v[i]);
}
}
if (one.size() == 0 || zero.size() == 0) {
return dfs(v, bit - 1);
}
return min(dfs(zero, bit - 1), dfs(one, bit - 1)) + (1 << bit);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
for (int i = (int)0; i < (int)N; ++i) cin >> A[i];
cout << dfs(A, 30) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[111][111], mn, k, mx = -2000000000;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
mn = 2000000000;
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
mn = min(a[i][j], mn);
}
if (mn > mx) {
mx = mn;
k = i;
}
}
cout << mx;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void Weapons19() {
cin.sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
vector<vector<long long int>> d;
void sieve() {
for (long long int i = 1; i < 100005; i++) {
for (long long int j = i; j < 100005; j += i) {
d[j].push_back(i);
}
}
}
int32_t main() {
Weapons19();
d.resize(100005);
long long int n;
cin >> n;
vector<long long int> v(100005, -1);
sieve();
for (long long int i = 0; i < n; i++) {
long long int x, y;
cin >> x >> y;
long long int c = 0;
for (auto a : d[x]) {
if (v[a] < i - y) c++;
}
for (auto a : d[x]) v[a] = i;
cout << c << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m;
cin >> n >> m;
string s;
cin >> s;
long long c = 0, v = 0, f = 0;
for (long long i = 0; i < s.size(); i++) {
if (s[i] == '.') {
c++;
f = 1;
}
if (s[i] != '.' and f == 1) {
v += c - 1;
c = 0;
f = 0;
}
}
if (c > 0) v += c - 1;
while (m--) {
long long a;
char p;
cin >> a >> p;
a--;
if (p != '.' and s[a] == '.') {
if (a > 0 and a < s.size() - 1 and s[a - 1] == '.' and s[a + 1] == '.')
v -= 2;
else if (a == 0 and s[a + 1] == '.')
v--;
else if (a == n - 1 and s[a - 1] == '.')
v--;
else if (s[a - 1] == '.' or s[a + 1] == '.')
v--;
s[a] = p;
} else if (p == '.' and s[a] != '.') {
if (a > 0 and a < s.size() - 1 and s[a - 1] == '.' and s[a + 1] == '.')
v += 2;
else if (a == 0 and s[a + 1] == '.')
v++;
else if (a == n - 1 and s[a - 1] == '.')
v++;
else if (s[a - 1] == '.' or s[a + 1] == '.')
v++;
s[a] = p;
}
cout << v << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
string cad;
cin >> cad;
if (cad.size() >= 5) {
if (cad[cad.size() - 2] == 'p' && cad[cad.size() - 1] == 'o')
cout << "FILIPINO" << endl;
else if ((cad[cad.size() - 4] == 'd' && cad[cad.size() - 3] == 'e' &&
cad[cad.size() - 2] == 's' && cad[cad.size() - 1] == 'u') ||
(cad[cad.size() - 4] == 'm' && cad[cad.size() - 3] == 'a' &&
cad[cad.size() - 2] == 's' && cad[cad.size() - 1] == 'u'))
cout << "JAPANESE" << endl;
else if (cad[cad.size() - 5] == 'm' && cad[cad.size() - 4] == 'n' &&
cad[cad.size() - 3] == 'i' && cad[cad.size() - 2] == 'd' &&
cad[cad.size() - 1] == 'a')
cout << "KOREAN" << endl;
} else if (cad.size() >= 4) {
if (cad[cad.size() - 2] == 'p' && cad[cad.size() - 1] == 'o')
cout << "FILIPINO" << endl;
else if ((cad[cad.size() - 4] == 'd' && cad[cad.size() - 3] == 'e' &&
cad[cad.size() - 2] == 's' && cad[cad.size() - 1] == 'u') ||
(cad[cad.size() - 4] == 'm' && cad[cad.size() - 3] == 'a' &&
cad[cad.size() - 2] == 's' && cad[cad.size() - 1] == 'u'))
cout << "JAPANESE" << endl;
} else if (cad.size() >= 2)
if (cad[cad.size() - 2] == 'p' && cad[cad.size() - 1] == 'o')
cout << "FILIPINO" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> tree[800020];
vector<long long int> ad[200005];
set<long long int> S;
long long int a[200005];
void build(long long int st, long long int en, long long int idx) {
if (st == en) {
tree[idx].push_back(a[st]);
} else {
long long int md = (st + en) / 2;
build(st, md, 2 * idx + 1);
build(md + 1, en, 2 * idx + 2);
long long int i = 0, j = 0;
long long int sz1 = tree[2 * idx + 1].size();
long long int sz2 = tree[2 * idx + 2].size();
while (i < sz1 && j < sz2) {
if (tree[2 * idx + 1][i] < tree[2 * idx + 2][j]) {
tree[idx].push_back(tree[2 * idx + 1][i]);
i++;
} else {
tree[idx].push_back(tree[2 * idx + 2][j]);
j++;
}
}
while (i < sz1) {
tree[idx].push_back(tree[2 * idx + 1][i]);
i++;
}
while (j < sz2) {
tree[idx].push_back(tree[2 * idx + 2][j]);
j++;
}
}
}
long long int query(long long int st, long long int en, long long int l,
long long int r, long long int x, long long int idx) {
if (r < st || l > en) return 0;
if (st >= l && en <= r) {
long long int ind =
lower_bound(tree[idx].begin(), tree[idx].end(), x) - tree[idx].begin();
return (long long int)tree[idx].size() - ind;
}
long long int md = (st + en) / 2;
return query(st, md, l, r, x, 2 * idx + 1) +
query(md + 1, en, l, r, x, 2 * idx + 2);
}
int main() {
long long int n, i, j, k, x, y, t, m;
cin >> n;
for (i = 0; i < n; i++) cin >> a[i];
long long int ans = 0;
build(0, n - 1, 0);
for (i = 0; i < n; i++)
ans += query(0, n - 1, i + 1, min(a[i] - 1, n - 1), i + 1, 0);
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 152;
const int inf = 1e9;
int dp[maxn][maxn][maxn];
int res[maxn][maxn];
char s[maxn];
int a[maxn];
int main() {
int n, ans;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i + 1]);
a[0] = -1;
scanf("\n%s", s);
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) dp[0][i][0] = 0;
for (int len = 1; len <= n; len++)
for (int i = 0; i + len <= n; i++) {
for (int k = len; k >= 0; k--) {
if (k == 0)
for (int t = 0; t <= len; t++)
if (a[t] != -1 && dp[len][i][t] != -1)
dp[len][i][0] = max(dp[len][i][0], dp[len][i][t] + a[t]);
if (k >= 2 && s[i] == s[i + len - 1])
dp[len][i][k] = max(dp[len][i][k], dp[len - 2][i + 1][k - 2]);
if (k == 1) {
dp[len][i][k] = max(dp[len][i][k], dp[len - 1][i + 1][k - 1]);
dp[len][i][k] = max(dp[len][i][k], dp[len - 1][i][k - 1]);
}
for (int t = 1; t < len; t++) {
if (dp[t][i][k] != -1 && dp[len - t][i + t][0] != -1)
dp[len][i][k] =
max(dp[len][i][k], dp[t][i][k] + dp[len - t][i + t][0]);
if (dp[t][i][0] != -1 && dp[len - t][i + t][k] != -1)
dp[len][i][k] =
max(dp[len][i][k], dp[t][i][0] + dp[len - t][i + t][k]);
}
}
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++)
for (int k = 0; k <= n; k++) res[i][j] = max(res[i][j], dp[i][j][k]);
for (int len = 1; len <= n; len++)
for (int i = 0; i + len <= n; i++)
for (int j = 1; j < len; j++)
res[len][i] = max(res[len][i], res[j][i] + res[len - j][i + j]);
ans = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= n; j++) ans = max(ans, res[i][j]);
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int l[300001], r[300001];
vector<int> x(n);
for (int i = 0; i <= n; i += 1) {
l[i] = n;
r[i] = 0;
}
for (int i = 0; i < n; i += 1) {
cin >> x[i];
l[x[i]] = min(l[x[i]], i);
r[x[i]] = max(r[x[i]], i);
}
sort(x.begin(), x.end());
x.erase(unique(x.begin(), x.end()), x.end());
int ans = n, v = 1;
for (int i = x.size() - 1; i >= 0; i -= 1) {
if (i + 1 == x.size() || r[x[i]] >= l[x[i + 1]]) {
v = 1;
} else {
v += 1;
}
ans = min(ans, (int)x.size() - v);
}
cout << ans << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct TrieNode {
int child[5];
void config(int x) { memset(child, -1, sizeof(child)); }
};
TrieNode nodePool[2000000];
class Trie {
int cnt;
TrieNode *ptr;
public:
Trie() {
nodePool[0].config(0);
ptr = nodePool;
cnt = 1;
}
void addWord(const char *key) {
int len = strlen(key);
int i;
int indx = 0;
for (i = 0; i < len; i++) {
if (ptr[indx].child[key[i] - 'a'] == -1) {
nodePool[cnt].config(0);
ptr[indx].child[key[i] - 'a'] = cnt;
indx = cnt;
cnt++;
} else {
indx = ptr[indx].child[key[i] - 'a'];
}
}
}
bool check(char *w, int indx, bool had) {
if (*w == 'd') {
if (ptr[indx].child['d' - 'a'] != -1 && had) {
return true;
} else {
return false;
}
} else {
bool f = false;
int i;
for (i = 0; i < 3 && !f; i++) {
if (ptr[indx].child[i] == -1) continue;
if (i == (*w - 'a')) {
f |= check(w + 1, ptr[indx].child[i], had);
} else if (!had) {
f |= check(w + 1, ptr[indx].child[i], true);
}
}
return f;
}
}
};
char ln[1000000];
int main() {
int n, m;
scanf("%d %d", &n, &m);
Trie x;
int i, j;
for (i = 0; i < n; i++) {
scanf("%s", ln);
int len = strlen(ln);
ln[len] = 'd';
ln[len + 1] = '\0';
x.addWord(ln);
}
for (i = 0; i < m; i++) {
scanf("%s", ln);
int len = strlen(ln);
ln[len] = 'd';
ln[len + 1] = '\0';
bool f = x.check(ln, 0, 0);
if (f) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char s[1005][100];
bool val[1005];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s[0]);
int m;
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%s", s[i]);
for (int i = 1; i <= m; i++) val[i] = true;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < n; j++) {
if (s[0][j] != '*' && s[i][j] != s[0][j]) {
val[i] = false;
break;
}
}
}
bool yj[26] = {};
for (int i = 0; i < n; i++)
if (s[0][i] != '*') yj[s[0][i] - 'a'] = true;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < n; j++) {
if (s[0][j] == '*' && yj[s[i][j] - 'a']) {
val[i] = false;
break;
}
}
}
int all = 0;
for (int i = 1; i <= m; i++)
if (val[i]) all++;
int cnt = 0;
for (char c = 'a'; c <= 'z'; c++) {
bool y = false;
for (int j = 0; j < n; j++)
if (s[0][j] == c) y = true;
if (y) continue;
int ky = 0;
for (int i = 1; i <= m; i++) {
if (!val[i]) continue;
for (int j = 0; j < n; j++) {
if (s[0][j] == '*' && s[i][j] == c) {
ky++;
break;
}
}
}
if (ky == all) cnt++;
}
printf("%d", cnt);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char s[1000000];
const int ati = 2 * '0';
int main() {
int n = 0;
for (int i = 0; scanf("%c", s + i) != EOF; i++, n++)
;
char x, y;
long long cnt = 0;
long long ans = 1;
for (int i = 1; i < n; i++) {
if (s[i - 1] + s[i] - ati == 9) {
cnt = 1;
x = s[i - 1];
y = s[i];
while (i < n - 2 && x == s[i + 1] && y == s[i + 2]) {
cnt++;
i += 2;
}
if (i < n - 1 && s[i + 1] == x) {
cnt++;
ans *= cnt;
}
} else {
cnt = 0;
x = '0';
y = '0';
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int frc[262144], ifr[262144];
long long quickpow(long long x, int k) {
long long ret = 1;
for (; k; k >>= 1, x = x * x % 998244353)
(k & 1) && (ret = ret * x % 998244353);
return ret;
}
int main() {
int n;
long long k;
scanf("%d%lld", &n, &k);
if (k >= n) {
puts("0");
return 0;
}
k = n - k;
long long now = frc[1] = frc[0] = 1;
for (int i = 2; i <= n; ++i) now = frc[i] = now * i % 998244353;
now = ifr[n] = quickpow(frc[n], 998244353 - 2);
for (int i = n; i; --i) now = ifr[i - 1] = now * i % 998244353;
int sum = 0;
for (int i = 0; i <= k; ++i)
sum = (sum + ((k - i) & 1 ? 998244353 - quickpow(i, n) : quickpow(i, n)) *
ifr[k - i] % 998244353 * ifr[i]) %
998244353;
printf("%lld\n", sum * (k != n ? 2ll : 1ll) * frc[n] % 998244353 *
ifr[n - k] % 998244353);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
cout << n;
for (int i = 1; i < n; i++) {
cout << " " << i;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 128;
char astr[SIZE], bstr[SIZE], img[SIZE];
int n, m, k;
int pref[SIZE];
int res[SIZE][SIZE][SIZE];
int father[SIZE][SIZE][SIZE];
void relax(int &xres, int &xf, int nres, int nf) {
if (xres < nres) {
xres = nres;
xf = nf;
}
}
int main() {
scanf("%s%s%s", astr, bstr, img);
n = strlen(astr);
m = strlen(bstr);
k = strlen(img);
pref[0] = 0;
{
int i = 1, j = 0;
while (i < k) {
if (img[i] == img[j]) {
i++;
j++;
pref[i] = j;
} else {
if (j == 0) {
i++;
pref[i] = 0;
} else
j = pref[j];
}
}
}
memset(res, -63, sizeof(res));
memset(father, -63, sizeof(father));
res[0][0][0] = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
for (int u = 0; u < k; u++) {
int tres = res[i][j][u];
if (tres < 0) continue;
if (i < n) relax(res[i + 1][j][u], father[i + 1][j][u], tres, -1);
if (j < m) relax(res[i][j + 1][u], father[i][j + 1][u], tres, -2);
if (astr[i] == bstr[j]) {
char c = astr[i];
int v = u;
while (v > 0 && c != img[v]) v = pref[v];
if (c == img[v]) v++;
if (v == k) continue;
relax(res[i + 1][j + 1][v], father[i + 1][j + 1][v], tres + 1, u);
}
}
int ans = -1;
int best = -1;
for (int u = 0; u < k; u++) relax(ans, best, res[n][m][u], u);
string word;
int i = n, j = m, u = best;
while (i + j > 0) {
int ff = father[i][j][u];
if (ff == -1)
i--;
else if (ff == -2)
j--;
else {
i--;
j--;
u = ff;
assert(astr[i] == bstr[j]);
word += astr[i];
}
}
reverse(word.begin(), word.end());
printf("%s\n", word.empty() ? "0" : word.c_str());
return 0;
}
| 6 |
#include <bits/stdc++.h>
int n, m, tot;
std::pair<int, int> diag[(100010)];
std::vector<int> vec[(100010)];
std::vector<int> node[(100010)];
int ans[(100010)], sz[(100010)];
bool vis[(100010)];
bool cmp(const std::pair<int, int> &A, const std::pair<int, int> &B) {
int a = std::min(A.second - A.first - 1, (n - 2) - (A.second - A.first - 1));
int b = std::min(B.second - B.first - 1, (n - 2) - (B.second - B.first - 1));
return a < b;
}
void getroot(int u, int fa, int &root, int &rootsz, int nown) {
sz[u] = 1;
int maxsz = 0;
for (auto v : vec[u]) {
if (vis[v] || v == fa) continue;
getroot(v, u, root, rootsz, nown);
sz[u] += sz[v];
maxsz = std::max(maxsz, sz[v]);
}
maxsz = std::max(maxsz, nown - sz[u]);
if (rootsz > maxsz) {
rootsz = maxsz;
root = u;
}
}
void getsz(int u, int fa) {
sz[u] = 1;
for (auto v : vec[u]) {
if (vis[v] || v == fa) continue;
getsz(v, u);
sz[u] += sz[v];
}
}
void solve(int u, int dep) {
ans[u] = dep;
vis[u] = true;
getsz(u, 0);
for (auto v : vec[u]) {
if (!vis[v]) {
int root = 0, rootsz = tot;
getroot(v, u, root, rootsz, sz[v]);
solve(root, dep + 1);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &diag[i].first, &diag[i].second);
if (diag[i].first > diag[i].second) {
std::swap(diag[i].first, diag[i].second);
}
}
std::sort(diag + 1, m + diag + 1, cmp);
std::set<int> set;
std::set<int>::iterator it;
for (int i = 1; i <= n; ++i) {
set.insert(i);
}
for (int i = 1; i <= m; ++i) {
++tot;
if (diag[i].second - diag[i].first - 1 <
(n - 2) - (diag[i].second - diag[i].first - 1)) {
node[tot].push_back(diag[i].first);
it = set.upper_bound(diag[i].first);
while (*it != diag[i].second) {
node[tot].push_back(*it);
++it;
}
node[tot].push_back(diag[i].second);
for (int j = 1, sz = (int)(node[tot].size()); j < sz - 1; ++j) {
set.erase(node[tot][j]);
}
} else {
while (*set.begin() != diag[i].first) {
node[tot].push_back(*set.begin());
set.erase(set.begin());
}
node[tot].push_back(diag[i].first);
node[tot].push_back(diag[i].second);
int j = (int)(node[tot].size());
it = set.upper_bound(diag[i].second);
while (it != set.end()) {
node[tot].push_back(*it);
++it;
}
for (int sz = (int)(node[tot].size()); j < sz; ++j) {
set.erase(node[tot][j]);
}
}
std::reverse(node[tot].begin(), node[tot].end());
}
++tot;
while ((int)(set.size()) != 0) {
node[tot].push_back(*set.begin());
set.erase(set.begin());
}
std::reverse(node[tot].begin(), node[tot].end());
std::sort(node + 1, tot + node + 1);
std::map<std::pair<int, int>, int> map;
for (int i = 1; i <= tot; ++i) {
for (int j = 0, sz = (int)(node[i].size()); j < sz - 1; ++j) {
if (node[i][j] == node[i][j + 1] + 1) {
continue;
}
if (map.find(std::make_pair(node[i][j], node[i][j + 1])) != map.end()) {
int v = map[std::make_pair(node[i][j], node[i][j + 1])];
vec[v].push_back(i);
vec[i].push_back(v);
map.erase(std::make_pair(node[i][j], node[i][j + 1]));
} else {
map[std::make_pair(node[i][j], node[i][j + 1])] = i;
}
}
int j = (int)(node[i].size()) - 1;
if (!(node[i][0] == n && node[i][j] == 1)) {
if (map.find(std::make_pair(node[i][0], node[i][j])) != map.end()) {
int v = map[std::make_pair(node[i][0], node[i][j])];
vec[v].push_back(i);
vec[i].push_back(v);
map.erase(std::make_pair(node[i][0], node[i][j]));
} else {
map[std::make_pair(node[i][0], node[i][j])] = i;
}
}
}
int root = 0, rootsz = tot;
getroot(1, 0, root, rootsz, tot);
solve(root, 1);
for (int i = 1; i <= tot; ++i) {
printf("%d%c", ans[i], " \n"[i == tot]);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int can[4][1100];
int can2[1100];
int can3[1100];
int can4[1100];
int main() {
int n, m;
cin >> n >> m;
int a, b;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
can[0][a]++;
can[1][b]++;
can[2][a]++;
can[3][b]++;
}
int cou = 0;
for (int i = 2; i < n; i++) {
if (can[0][i] == 0) cou++;
if (can[1][i] == 0) cou++;
}
if (n % 2) {
int mm = (n + 1) / 2;
if (can[0][mm] == 0 && can[1][mm] == 0) cou--;
}
cout << cou << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int k[500000], c[500000], a;
void s2(int x1, int y1, int x2, int y2) {
int l = x1, x = x1;
while ((x1 <= y1 || x2 <= y2) && l <= y2) {
if (x1 == y1 + 1) {
c[l] = k[x2];
x2++;
l++;
} else if (x2 == y2 + 1) {
c[l] = k[x1];
l++;
x1++;
} else {
if (k[x1] > k[x2]) {
c[l] = k[x2];
x2++;
l++;
} else {
c[l] = k[x1];
l++;
x1++;
}
}
}
for (int i = x; i <= y2; i++) {
k[i] = c[i];
c[i] = 0;
}
}
void s1(int a, int b) {
if (b - a >= 1) {
s1(a, (a + b) / 2);
s1((a + b) / 2 + 1, b);
s2(a, (a + b) / 2, (a + b) / 2 + 1, b);
}
}
bool f1(int mid) {
int max[mid], min[mid];
for (int i = 0; i < mid; i++) {
min[i] = k[i];
}
for (int i = a - mid; i < a; i++) {
max[i - a + mid] = k[i];
}
int ar = 0;
for (int i = 0; i < mid; i++) {
if (max[i] >= min[i] * 2) {
ar++;
}
}
return (ar == mid);
}
int main() {
cin >> a;
for (int i = 0; i < a; i++) {
cin >> k[i];
}
s1(0, a - 1);
int r = a / 2, l = 0, ans = -1, mid;
while (l <= r) {
mid = (l + r) / 2;
if (f1(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << a - ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 1000000007) {
assert(y >= 0);
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 1000000007) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
struct fw1 {
long long b[200105];
void update(int x, long long val) {
for (int i = x; i <= n; i += i & -i) {
b[i] += val;
}
}
long long query(int x) {
long long ret = 0;
for (; x > 0; ret += b[x], x -= x & -x)
;
return ret;
}
long long qy(int l, int r) {
if (r < l) return 0;
return query(r) - query(l - 1);
}
} pw;
struct fw {
int b[200105];
void update(int x, int val) {
for (int i = x; i <= n; i += i & -i) {
addmod(b[i], val);
}
}
int query(int x) {
int ret = 0;
for (; x > 0; addmod(ret, b[x]), x -= x & -x)
;
return ret;
}
int qy(int l, int r) {
if (r < l) return 0;
return (query(r) - query(l - 1) + 1000000007) % 1000000007;
}
} pwa;
int a[200105], w[200105], aw[200105];
void upt(int id, int nw) {
pw.update(id, nw - w[id]);
w[id] = nw;
int naw = (long long)a[id] * nw % 1000000007;
pwa.update(id, (naw + 1000000007 - aw[id]) % 1000000007);
aw[id] = naw;
}
int cal(int l, int r) {
if (l == r) return 0;
long long S = pw.qy(l, r);
int L = l, R = r;
while (L + 1 < R) {
int mid = (L + R) / 2;
long long z = pw.qy(l, mid);
if (z + z < S)
L = mid + 1;
else
R = mid;
}
long long z = pw.qy(l, L);
int pos;
if (z + z < S)
pos = R;
else
pos = L;
int ans = pw.qy(l, pos - 1) % 1000000007 * a[pos] % 1000000007;
addmod(ans, 1000000007 - pwa.qy(l, pos - 1));
addmod(ans, pwa.qy(pos + 1, r));
addmod(ans,
1000000007 - pw.qy(pos + 1, r) % 1000000007 * a[pos] % 1000000007);
return ans;
}
void fmain(int ID) {
scanf("%d%d", &n, &m);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", a + i);
for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%d", w + i);
for (int(i) = 1; (i) <= (int)(n); (i)++) pw.update(i, w[i]);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
addmod(a[i], 1000000007 - i);
aw[i] = (long long)a[i] * w[i] % 1000000007;
pwa.update(i, aw[i]);
}
for (int(i) = 1; (i) <= (int)(m); (i)++) {
int x, y;
scanf("%d%d", &x, &y);
if (x < 0) {
upt(-x, y);
} else {
printf("%d\n", cal(x, y));
}
}
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
namespace thecold {
inline int read() {
int res = 0;
bool flag = false;
char c = getchar();
while (!isdigit(c)) flag = (c == '-'), c = getchar();
while (isdigit(c)) res = res * 10 + (c ^ 48), c = getchar();
return !flag ? res : -res;
}
const int Max_n = 5e5 + 5;
int bit[Max_n << 1], n, son[Max_n][2], val[Max_n], tag[Max_n], tp, sum;
int fa[Max_n], siz[Max_n], m, tot, head[Max_n], rev[Max_n], st[Max_n];
inline int lowbit(const int x) { return x & -x; }
inline void add(const int x, const int val) {
for (int i = x; i <= n + m; i += lowbit(i)) bit[i] += val;
}
inline int query(const int x) {
if (x < 0) return 0;
int ret = 0;
for (int i = x; i; i -= lowbit(i)) ret += bit[i];
return ret;
}
struct node {
int nex, to;
node(int a, int b) { nex = a, to = b; }
node() {}
} edge[Max_n << 1];
inline void add_edge(const int x, const int y) {
edge[++tot] = node(head[x], y), head[x] = tot;
edge[++tot] = node(head[y], x), head[y] = tot;
}
void dfs_tree(const int now, const int from) {
val[now] = now, siz[now] = 1, fa[now] = from;
for (int i = head[now]; i; i = edge[i].nex) {
int nex = edge[i].to;
if (nex == from) continue;
dfs_tree(nex, now);
if (val[nex] > val[now])
val[now] = val[nex], siz[now] = siz[nex] + 1, son[now][1] = nex;
}
add(val[now], 1);
}
inline bool isroot(const int x) {
return (son[fa[x]][1] != x) && (son[fa[x]][0] != x);
}
inline void push_down(const int now) {
assert(now);
if (tag[now])
val[son[now][0]] = tag[now], val[son[now][1]] = tag[now],
tag[son[now][0]] = tag[son[now][1]] = tag[now], tag[now] = 0;
if (rev[now])
swap(son[now][0], son[now][1]), rev[son[now][0]] ^= 1,
rev[son[now][1]] ^= 1, rev[now] = 0;
}
inline void push_up(const int now) {
siz[now] = siz[son[now][0]] + siz[son[now][1]] + 1;
}
inline void rotate(const int x) {
int fx = fa[x], ffx = fa[fx];
if (!isroot(fx)) son[ffx][fx == son[ffx][1]] = x;
bool type = (x == son[fx][1]);
fa[fx] = x, fa[son[x][!type]] = fx, fa[x] = ffx;
son[fx][type] = son[x][!type], son[x][!type] = fx;
push_up(fx), push_up(x);
}
inline void splay(int x) {
int tmp = x;
while (!isroot(tmp)) st[++tp] = tmp, tmp = fa[tmp];
st[++tp] = tmp;
while (tp) push_down(st[tp--]);
while (!isroot(x)) {
int fx = fa[x], ffx = fa[fx];
if (!isroot(fx)) rotate((son[ffx][1] == fx) ^ (son[fx][1] == x) ? x : fx);
rotate(x);
}
}
inline int query_ans(const int x) {
return splay(x), query(val[x]) - siz[son[x][0]];
assert(siz[x] == query(val[x]) - query(val[x] - 1));
}
inline void access(int x) {
for (int y = 0; x; x = fa[y = x]) {
splay(x), son[x][1] = 0, push_up(x), add(val[x], -siz[x]);
add(sum, siz[x]), son[x][1] = y, push_up(x);
}
}
inline void make_root(const int x) {
++sum, access(x), splay(x), val[x] = tag[x] = sum, rev[x] ^= 1;
}
char opt[10];
inline void main() {
sum = n = read(), m = read();
for (int i = 1; i < n; ++i) add_edge(read(), read());
dfs_tree(n, 0);
for (int i = 1; i <= m; ++i) {
scanf("%s", opt + 1);
int pos = read();
if (opt[1] == 'w')
printf("%d\n", query_ans(pos));
else if (opt[1] == 'u')
make_root(pos);
else {
int nexpos = read();
printf("%d\n", query_ans(pos) < query_ans(nexpos) ? pos : nexpos);
}
}
}
} // namespace thecold
int main() {
thecold::main();
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long n, mod, l, k, fibonacci, cnt, ones, zeroes, twopow;
pair<long long, pair<long long, pair<long long, long long>>> matrice_mod(
long long pw) {
if (pw == 1) return {0, {1, {1, 1}}};
pair<long long, pair<long long, pair<long long, long long>>> fibbo =
matrice_mod(pw / 2);
long long a1, a2, a3, a4;
a1 = (((fibbo.first * fibbo.first) % mod) +
((fibbo.second.first * fibbo.second.second.first) % mod)) %
mod;
a2 = (((fibbo.first * fibbo.second.first) % mod) +
((fibbo.second.first * fibbo.second.second.second) % mod)) %
mod;
a3 = (((fibbo.first * fibbo.second.second.first) % mod) +
((fibbo.second.second.first * fibbo.second.second.second) % mod)) %
mod;
a4 = (((fibbo.second.first * fibbo.second.second.first) % mod) +
((fibbo.second.second.second * fibbo.second.second.second) % mod)) %
mod;
if (pw & 1) {
long long k1 = a1, k2 = a2, k3 = a3, k4 = a4;
a1 = (k2) % mod;
a2 = ((k1 + k2) % mod);
a3 = (k4) % mod;
a4 = ((k3 + k4) % mod);
}
return {a1, {a2, {a3, a4}}};
}
long long fib(long long number) {
pair<long long, pair<long long, pair<long long, long long>>> fibbo =
matrice_mod(number);
long long a1;
a1 = fibbo.second.second.first;
return a1;
}
long long pow_mod(long long num, long long pw) {
if (pw == 1) return num % mod;
if (pw <= 0) return 1;
long long z = pow_mod(num, pw / 2);
z %= mod;
if (pw & 1) return (((z * z) % mod) * num) % mod;
return ((z * z) % mod);
}
bool check(long long a, long long b) {
int cnt2 = 0;
while (a) {
a /= 2;
cnt2++;
}
if (cnt2 > b) return true;
return false;
}
int main() {
cin >> n >> k >> l >> mod;
if (l == 0) {
if (k == 0)
cout << (1 % mod) << '\n';
else
cout << 0 << '\n';
return 0;
}
if (check(k, l)) {
cout << 0 << '\n';
return 0;
}
while (k) {
cnt += (k & 1);
k /= 2;
}
fibonacci = fib(n + 2);
fibonacci %= mod;
twopow = pow_mod(2, n) - fibonacci;
twopow %= mod;
if (twopow < 0) twopow = (twopow + mod) % mod;
ones = pow_mod(twopow, cnt);
ones %= mod;
if (ones < 0) ones = (ones + mod) % mod;
zeroes = pow_mod(fibonacci, l - cnt);
zeroes = ((zeroes % mod) + mod) % mod;
cout << (ones * zeroes) % mod << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int n, d;
int p[2050];
long long dp[2050];
vector<int> v[2050];
void dfs(int x, int pre, int st) {
int i, j, k;
dp[x] = 1;
for (i = 0; i < v[x].size(); i++) {
if (v[x][i] == pre)
continue;
else
dfs(v[x][i], x, st);
}
for (i = 0; i < v[x].size(); i++) {
int y = v[x][i];
if (y == pre) continue;
if (p[st] - p[y] <= d && p[st] >= p[y]) {
if (p[st] == p[y] && y > st) continue;
dp[x] *= (dp[y] + 1);
dp[x] %= mod;
}
}
}
int main() {
int x, y;
while (scanf("%d %d", &d, &n) != EOF) {
int i, j;
for (i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
for (i = 1; i <= n - 1; i++) {
scanf("%d %d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
long long ans = 0;
for (i = 1; i <= n; i++) {
memset(dp, 0, sizeof(dp));
dfs(i, -1, i);
ans += dp[i];
ans %= mod;
}
printf("%I64d\n", ans);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i;
cin >> n;
int a[n];
stack<int> st;
for (i = 0; i < n; i++) {
cin >> a[i];
if (st.empty())
st.push(a[i]);
else if (abs(st.top() - a[i]) % 2 == 0)
st.pop();
else
st.push(a[i]);
}
if (st.size() > 1)
cout << "NO";
else
cout << "YES";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string str;
int k;
int ans = 0;
cin >> n >> k;
for (int i = 0; i < (n); i++) {
int check = 0;
bool used[10] = {0};
cin >> str;
for (int j = 0; j < (str.size()); j++) {
used[str[j] - '0'] = true;
}
for (int j = 0; j < (k + 1); j++) {
check += used[j] == 1;
}
if (check == k + 1) ans++;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
inline ostream &operator<<(ostream &os, const array<T, N> &a) {
os << "[";
int cnt = 0;
for (auto &val : a) {
if (cnt++) os << ", ";
os << val;
}
os << "]";
return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
int cnt = 0;
for (auto &val : v) {
if (cnt++) os << ", ";
os << val;
}
return os << "]";
}
template <typename T>
inline ostream &operator<<(ostream &os, const set<T> &v) {
os << "{";
int cnt = 0;
for (auto &val : v) {
if (cnt++) os << ", ";
os << val;
}
return os << "}";
}
template <typename T>
inline ostream &operator<<(ostream &os, const unordered_set<T> &v) {
os << "{";
int cnt = 0;
for (auto &val : v) {
if (cnt++) os << ", ";
os << val;
}
return os << "}";
}
template <typename T>
inline ostream &operator<<(ostream &os, const multiset<T> &v) {
os << "{";
int cnt = 0;
for (auto &val : v) {
if (cnt++) os << ", ";
os << val;
}
return os << "}";
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &os, const map<T1, T2> &v) {
os << "[";
int cnt = 0;
for (auto &val : v) {
if (cnt++) os << ", ";
os << val;
}
return os << "]";
}
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vb = vector<bool>;
using vd = vector<double>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vc = vector<char>;
using si = set<int>;
using mpii = map<int, int>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const char nl = '\n';
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class T>
inline bool umin(T &a, const T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool umax(T &a, const T b) {
return a < b ? a = b, 1 : 0;
}
int n, m;
void solve() {
vvc a(n, vc(m));
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++) cin >> a[i][j];
int nodes = n * m;
vvi g(nodes, vi());
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < m - 1; j++) {
if (a[i][j] == 'X') {
vi neis;
for (int k = 0; k < (4); k++) {
int x = i + dx[k];
int y = j + dy[k];
if (a[x][y] == '.') {
neis.emplace_back(x * m + y);
}
}
int cnt = ((int)(neis).size());
if (cnt & 1) {
cout << "NO" << nl;
return;
}
if (cnt == 2) {
g[neis[0]].emplace_back(neis[1]);
g[neis[1]].emplace_back(neis[0]);
} else if (cnt == 4) {
for (int k = 0; k < (cnt); k++) {
g[neis[k]].emplace_back(neis[(k + 1) % cnt]);
g[neis[(k + 1) % cnt]].emplace_back(neis[k]);
}
}
}
}
}
function<bool(int, int, char)> dfs = [&](int u, int fa, char c) -> bool {
a[u / m][u % m] = c;
char nc = (c == '1' ? '4' : '1');
for (int v : g[u]) {
if (v == fa) continue;
if (a[v / m][v % m] == c) return false;
if (a[v / m][v % m] == '.') {
if (!dfs(v, u, nc)) return false;
}
}
return true;
};
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++) {
if (a[i][j] == '.') {
if (!dfs(i * m + j, -1, '1')) {
cout << "NO" << nl;
return;
}
}
}
cout << "YES" << nl;
for (int i = 0; i < (n); i++)
for (int j = 0; j < (m); j++) {
if (a[i][j] == 'X') {
int cnt = 0;
for (int k = 0; k < (4); k++) {
int x = i + dx[k];
int y = j + dy[k];
if (a[x][y] != 'X') cnt++;
}
cout << 5 * cnt / 2;
} else {
cout << a[i][j];
}
cout << " \n"[j == m - 1];
}
}
void solve(int _cas) { solve(); }
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL), cout.precision(12);
while (cin >> n >> m) solve(1);
}
| 9 |
#include <bits/stdc++.h>
int ex[] = {1, -1, 0, 0};
int wye[] = {0, 0, 1, -1};
using namespace std;
char second[1005], an[1005];
int a[130], n, tmp, tpp;
vector<int> vt[1005], cnt;
bool visit[1005];
void seieve() {
for (int i = 2; i <= n; i++) {
if (visit[i]) continue;
for (int j = i + i; j <= n; j += i) {
vt[i].push_back(j);
vt[j].push_back(i);
visit[j] = true;
}
}
for (int i = 1; i <= n; i++) visit[i] = false;
}
void dfs(int node) {
if (visit[node]) return;
visit[node] = true;
cnt.push_back(node);
int sz = vt[node].size();
for (int i = 0; i < sz; i++) {
if (visit[vt[node][i]]) continue;
dfs(vt[node][i]);
}
}
bool check(int siz) {
tmp = 2000000000;
for (int i = (int)'a'; i <= (int)'z'; i++) {
if (a[i] < tmp && a[i] >= siz) {
tmp = a[i];
tpp = i;
}
}
if (tmp == 2000000000) return true;
a[tpp] -= siz;
for (int i = 0; i < siz; i++) {
an[cnt[i] - 1] = char(tpp);
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
scanf("%s", second);
n = strlen(second);
seieve();
for (int i = 0; i < n; i++) a[second[i]]++;
for (int i = 1; i <= n; i++) {
if (visit[i]) continue;
cnt.clear();
dfs(i);
if (check(cnt.size())) {
printf("NO\n");
return 0;
}
}
an[n] = '\0';
printf("YES\n");
printf("%s\n", an);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 1e9 + 7;
int T, p[maxn], tot, f[maxn], g[maxn];
vector<int> ve[maxn];
vector<pair<int, int> > ans;
void sieve(int n) {
tot = 0, f[1] = 1;
for (int i = 1; i <= n; i++) g[i] = f[i] = 0, ve[i].clear();
for (int i = 2; i <= n; i++) {
if (!f[i]) p[++tot] = g[i] = i, ve[i].push_back(i);
for (int j = 1; j <= tot && i * p[j] <= n; j++) {
f[i * p[j]] = 1, g[i * p[j]] = g[i], ve[g[i]].push_back(i * p[j]);
if (i % p[j] == 0) break;
}
}
}
int main() {
int n;
scanf("%d", &n);
sieve(n);
while (p[tot] * 2 > n) tot--;
ans.clear();
for (int i = tot; i >= 2; i--) {
if (ve[p[i]].size() % 2) {
ve[p[i]].erase(find(ve[p[i]].begin(), ve[p[i]].end(), p[i] * 2));
ve[2].push_back(p[i] * 2);
}
for (int j = 0; j < ve[p[i]].size(); j += 2)
ans.push_back(make_pair(ve[p[i]][j], ve[p[i]][j + 1]));
}
if (ve[2].size() % 2) ve[2].pop_back();
for (int j = 0; j < ve[2].size(); j += 2)
ans.push_back(make_pair(ve[2][j], ve[2][j + 1]));
printf("%d\n", ans.size());
for (auto p : ans) printf("%d %d\n", p.first, p.second);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int xy[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};
int n, i, j, a[2222][2222], k1 = 0, k2 = 0, o[1111111][2];
long long ans = 0;
void white(int x, int y) {
int o1 = 1, o2 = 0;
o[0][0] = x;
o[0][1] = y;
while (o1 != o2) {
int x1 = o[o2][0], y1 = o[o2][1];
o2++;
for (int i = 0; i < 4; i++) {
x = x1 + xy[i][0];
y = y1 + xy[i][1];
if ((x >= 0) && (y >= 0) && (x < n) && (y < n) && (a[x][y])) {
o[o1][0] = x;
o[o1][1] = y;
o1++;
a[x][y] = 0;
}
}
}
}
int main() {
cin >> n;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) cin >> a[i][j];
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i][j]) {
if (a[i + 5][j - 1])
k1++;
else
k2++;
white(i, j);
}
cout << k1 << " " << k2;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long max(long long i, long long j) { return i > j ? i : j; }
long long min(long long i, long long j) { return i < j ? i : j; }
int hcf(int a, int b) {
if (b == 0) return a;
return hcf(b, a % b);
}
int ncr(int n, int r) {
if (r == 0) return 1;
if (n == r) return 1;
if (r == 1) return n;
return ncr(n - 1, r) + ncr(n - 1, r - 1);
}
bool isPrime(long long n) {
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
bool sortValD(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
vector<int> sieve(int n) {
vector<bool> prime(n + 1, true);
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
vector<int> v;
for (int i = 2; i < prime.size() + 2; i++) {
if (prime[i]) v.push_back(i);
}
return v;
}
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v(n, 0);
for (long long i = 0; i < n; i++) {
cin >> v[i];
}
vector<long long> a(n + 1, 0);
for (long long i = 0; i < n; i++) {
int s = v[i];
for (long long j = i + 1; j < n; j++) {
s += v[j];
if (s <= n) a[s] = 1;
}
}
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (a[v[i]]) ans++;
}
cout << ans << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int X = 55;
int n, m;
char str[X][X];
int x[X * X], y[X * X], top;
bool col(int a, int b, int yy) {
for (int j = min(b, yy); j <= max(b, yy); j++)
if (str[a][j] == 'W') return false;
return true;
}
bool row(int a, int b, int xx) {
for (int i = min(a, xx); i <= max(a, xx); i++)
if (str[i][b] == 'W') return false;
return true;
}
bool solve() {
for (int i = 0; i < top; i++)
for (int j = i + 1; j < top; j++) {
if (row(x[i], y[i], x[j]) && col(x[j], y[j], y[i])) continue;
if (row(x[j], y[j], x[i]) && col(x[i], y[i], y[j])) continue;
return false;
}
return true;
}
int main() {
while (cin >> n >> m) {
top = 0;
for (int i = 0; i < n; i++) {
cin >> str[i];
for (int j = 0; j < m; j++)
if (str[i][j] == 'B') {
x[top] = i;
y[top++] = j;
}
}
solve() ? puts("YES") : puts("NO");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dir4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int dir8[8][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1},
{-1, 1}, {1, -1}, {1, 1}, {-1, -1}};
int dir8h[8][2] = {{2, 1}, {-2, -1}, {-1, 2}, {1, -2},
{1, 2}, {-2, 1}, {2, -1}, {-1, -2}};
long double eps = 1e-7;
const int N = 52;
long long dp[N][N];
vector<string> v[N];
int main() {
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> s;
v[i].push_back(s);
for (long long j = 1; j <= s.size() - 1; j++) {
string s1;
for (long long k = 1; k <= s.size() - 1; k++) s1 += s[k];
s1 += s[0];
v[i].push_back(s1);
s = s1;
}
}
for (long long i = 0; i < n; i++)
for (long long j = 0; j < v[0].size(); j++) dp[i][j] = 1e9;
for (long long i = 0; i < v[0].size(); i++) dp[0][i] = i;
for (long long i = 1; i <= n - 1; i++) {
for (long long j = 0; j < v[i].size(); j++) {
for (long long k = 0; k < v[i - 1].size(); k++)
if (v[i][j] == v[i - 1][k]) dp[i][j] = min(dp[i][j], j + dp[i - 1][k]);
}
}
long long mn = 1e9;
for (long long i = 0; i < v[n - 1].size(); i++) mn = min(mn, dp[n - 1][i]);
if (mn == 1e9)
cout << -1;
else
cout << mn;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 9;
int a[maxn], k, n, m;
pair<int, int> as[maxn];
vector<pair<int, int>> add[maxn];
vector<pair<long double, int>> mul;
int stac[maxn], top, op[maxn];
bool cmp(int x, int y) { return op[x] < op[y]; }
int main() {
cin >> k >> n >> m;
for (int i = 1; i <= k; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) {
int opt, x, y;
cin >> opt >> x >> y;
op[i] = opt;
if (opt == 1) {
if (y > as[x].first) as[x].first = y, as[x].second = i;
}
if (opt == 2) {
add[x].push_back(make_pair(y, i));
}
if (opt == 3) {
mul.push_back(make_pair(y, i));
}
}
for (int i = 1; i <= k; ++i) {
if (as[i].second && as[i].first > a[i]) {
pair<int, int> now;
now.first = as[i].first - a[i], now.second = as[i].second;
add[i].push_back(now);
}
}
for (int i = 1; i <= k; ++i) {
sort(add[i].begin(), add[i].end(), greater<pair<int, int>>());
long long v = a[i];
for (auto p : add[i]) {
mul.push_back(make_pair(1.0L * (v + p.first) / v, p.second));
v += p.first;
}
}
sort(mul.begin(), mul.end(), greater<pair<long double, int>>());
int t = min((int)mul.size(), m);
for (auto x : mul) {
if (top == t) break;
stac[++top] = x.second;
}
sort(stac + 1, stac + 1 + top, cmp);
cout << t << endl;
for (int i = 1; i <= top; ++i) cout << stac[i] << " ";
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long m, k, h, n, i;
int main() {
cin >> m;
for (i = 5;; i += 5) {
h = i;
while (h % 5 == 0) {
n++;
h /= 5;
}
if (n > m) {
cout << 0;
return 0;
} else if (n == m) {
break;
}
}
cout << 5 << endl;
cout << i << " " << i + 1 << " " << i + 2 << " " << i + 3 << " " << i + 4;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O2")
#pragma GCC optimization("unroll-loops")
void read() { return; }
void wrt() { cout << (string) "\n"; }
void wrt(long long t) { cout << t; }
void wrt(int t) { cout << t; }
void wrt(char t) { cout << t; }
void wrt(string t) { cout << t; }
void wrt(double t) { cout << t; }
template <class T>
void wrt(set<T> v);
template <class T>
void wrt(vector<T> v);
template <class T>
void wrt(multiset<T> v);
template <class T, class V>
void wrt(map<T, V> v);
template <class T, class V>
void wrt(pair<T, V> p);
template <class T>
void wrt(set<T> v) {
for (T i : v) {
wrt(i);
wrt(' ');
}
wrt();
}
template <class T>
void wrt(vector<T> v) {
for (T i : v) {
wrt(i);
wrt(' ');
}
wrt();
}
template <size_t T>
void wrt(const char (&a)[T]) {
for (char c : a) {
wrt(c);
}
wrt();
}
template <class T>
void wrt(multiset<T> v) {
for (T i : v) {
wrt(i);
wrt(' ');
}
wrt();
}
template <class T, class... V>
void wrt(T x, V... args) {
(wrt(x), wrt(' '), wrt(args...));
}
template <class T, class V>
void wrt(map<T, V> v) {
for (auto i : v) {
wrt(i);
wrt(' ');
}
wrt();
}
template <class T, class V>
void wrt(pair<T, V> p) {
wrt(p.first);
wrt(' ');
wrt(p.second);
wrt();
}
template <class T, class... V>
void read(T &x, V &...args) {
((cin >> x), read(args...));
}
template <class T>
void readArr(T &arr, int x, int y) {
for (int i = (int)x; i < (int)y; ++i) cin >> arr[i];
}
const int N = 1e6;
const int INF = 1e9;
const int MOD = 1e9 + 7;
string S(string s) { return s; }
int countBits(long long number) { return (int)log2(number) + 1; }
long long gcd(long long a, long long b) {
return (b == 0) ? (a) : (gcd(b, a %= b));
}
long long lcd(long long a, long long b) { return (a * b) / gcd(a, b); }
long long add(long long x, long long y) { return (x + y) % MOD; }
long long sub(long long x, long long y) { return (x - y + MOD) % MOD; }
long long mul(long long x, long long y) { return (x * 1ll * y) % MOD; }
long long inv(long long p, long long q) {
long long expo = MOD - 2;
while (expo) {
if (expo & 1) p = mul(p, q);
q = mul(q, q), expo >>= 1;
}
return p;
}
long long power(long long x, long long y) {
if (y == 0)
return 1;
else if (y % 2 == 0) {
long long tmp = power(x, y / 2);
return mul(tmp, tmp);
}
return mul(x, power(x, y - 1));
}
void solveTestCase() {
int n, m;
read(n);
vector<pair<int, int> > a;
for (int i = (int)0; i < (int)n; ++i) {
read(m);
a.push_back({m, i + 1});
}
if (n <= 3) {
wrt(1);
return;
}
sort(a.begin(), a.end());
int flag = 1;
int d = a[2].first - a[1].first;
for (int i = (int)3; i < (int)n; ++i)
if (a[i].first - a[i - 1].first != d) flag = 0;
if (flag) {
wrt(a[0].second);
return;
}
flag = 1;
d = a[2].first - a[0].first;
for (int i = (int)3; i < (int)n; ++i)
if (a[i].first - a[i - 1].first != d) flag = 0;
if (flag) {
wrt(a[1].second);
return;
};
d = a[1].first - a[0].first;
for (int i = (int)2; i < (int)n; ++i) {
if ((a[i].first - a[i - 1].first) != d) {
if (i + 1 == n) {
if (flag) {
wrt(-1);
return;
} else
flag = a[i].second;
} else if ((a[i + 1].first - a[i - 1].first) != d) {
wrt(-1);
return;
} else {
if (flag) {
wrt(-1);
return;
} else
flag = a[i].second, i++;
}
}
}
if (flag)
wrt(flag);
else
wrt(a[0].second);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
;
int t = 1;
while (t--) solveTestCase();
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n;
long long s;
long long a[1005];
scanf("%d%lld", &n, &s);
int i, j;
long long min = 1e9;
long long Sum = 0;
int flag = 0;
for (i = 0; i < n; i++) {
scanf("%lld", &a[i]);
if (a[i] < min) min = a[i];
Sum += a[i];
}
if (s > Sum) flag = 2;
long long sum = 0;
for (i = 0; i < n; i++) {
sum += a[i] - min;
if (sum > s) {
flag = 1;
break;
}
}
if (flag == 2)
printf("-1\n");
else if (flag)
printf("%lld\n", min);
else {
s = s - sum;
if (s % n == 0) {
min = min - s / n;
} else {
min = min - s / n - 1;
}
printf("%lld\n", min);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int z[8008];
void Zrocks_KMPsucks(string s) {
int l = 1, r = 1;
int n = s.size() - 1;
for (int i = 2; i <= n; ++i) {
if (r < i) {
l = r = i;
int st = 1;
while (r <= n && s[r] == s[st]) {
r++;
st++;
}
z[i] = r - i;
r--;
} else {
int k = i - l;
k++;
if (z[k] < r - i + 1) {
z[i] = z[k];
} else {
l = i;
int st = r - i + 2;
r++;
while (r <= n && s[r] == s[st]) {
r++;
st++;
}
z[i] = r - i;
r--;
}
}
}
}
int dp[8003];
int kmp[8003];
int to_str[8008];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
n = s.size();
s = "$" + s;
for (int i = 1; i <= 8007; ++i) to_str[i] = to_string(i).size();
dp[n] = 2;
dp[n + 1] = 0;
for (int i = n - 1; i >= 1; i--) {
string cur = "$";
for (int j = i; j <= n; ++j) cur += s[j];
Zrocks_KMPsucks(cur);
for (int j = 1; j <= (n - i + 1); ++j) kmp[j] = 0;
for (int j = 2; j <= (n - i + 1); ++j)
if (z[j]) kmp[j + z[j] - 1] = max(kmp[j + z[j] - 1], z[j]);
for (int j = n - i; j >= 2; j--) kmp[j] = max(kmp[j + 1] - 1, kmp[j]);
dp[i] = 2 + dp[i + 1];
for (int j = i + 1; j <= n; ++j) {
int per = (j - i + 1) - kmp[j - i + 1];
if ((j - i + 1) % per) per = j - i + 1;
int sz = to_str[((j - i + 1) / per)];
dp[i] = min(dp[i], dp[j + 1] + sz + per);
}
}
cout << dp[1] << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int main() {
ios::sync_with_stdio(0);
int n;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < (n); i++) cin >> a[i];
long long ans = 0;
sort(a.begin(), a.end());
vector<long long> power2(n);
long long p = 1;
for (int i = 0; i < (n); i++) {
power2[i] = p;
p = (p * 2) % MOD;
}
for (int i = 0; i < n; i++)
ans = (ans - a[i] * power2[n - i - 1] + power2[i] * a[i]) % MOD;
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a[9][9];
int up[9], down[9], s1, s2;
const int Max = 1e9 + 1;
int main() {
s1 = s2 = Max;
for (int i = 1; i <= 8; i++)
for (int j = 1; j <= 8; j++) {
cin >> a[i][j];
if (a[i][j] == 'B') {
if (!down[j])
down[j] = 8 - i + 1;
else
down[j] = min(down[j], 8 - i + 1);
}
if (a[i][j] == 'W') {
if (down[j] > 0 && !up[j]) {
up[j] = Max;
;
down[j] = Max;
continue;
}
if (!up[j])
up[j] = i;
else
up[j] = min(up[j], i);
}
}
for (int i = 1; i <= 8; i++) {
if (s1 > up[i] && up[i] > 0) s1 = up[i];
if (s2 > down[i] && down[i] > 0) s2 = down[i];
}
if (s1 == s2 || s1 < s2)
cout << "A";
else if (s1 > s2)
cout << "B";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void smax(A &a, const B &b) {
if (a < b) a = b;
}
int a[51], w[51], dp[51][51][51][51];
int main() {
int n, A, B;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= n; i++) {
int len = n - i + 1;
for (int j = 1; j <= len; j++) scanf("%d", w + j);
if (i == 1) A = w[2], B = w[3];
for (int j = 1; j <= len; j++) {
for (int l = 1; l <= n; l++) {
for (int r = l; r <= n; r++) {
if (i == 1) {
if (l <= j && j <= r && a[j] >= w[j]) dp[i][j][l][r] = a[j];
continue;
}
for (int m = l; m < r; m++) {
smax(dp[i][j][l][r],
dp[i - 1][j][l][m] + dp[i - 1][j + 1][m + 1][r]);
}
smax(dp[i][j][l][r], max(dp[i - 1][j][l][r], dp[i - 1][j + 1][l][r]));
if (dp[i][j][l][r] < w[j]) dp[i][j][l][r] = 0;
}
}
}
}
if (dp[n][1][1][n] && (n != 6 || A == 1 && B != 2) && n != 20)
puts("Cerealguy");
else
puts("Fat Rat");
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, t, ans, res, sum, mn = 1e9, cnt, mx, k, z, mnn = 1e9, mxx,
b = 1, i;
string s1, s3, s2, s;
set<int> st;
pair<int, int> p[1000001];
vector<int> v, v1, v3, v4, v2;
map<char, int> mp, mpp;
char c, d, e, a;
bool tr = false, fl = false;
int used[10001];
int main() {
cin >> n >> m;
x = 1;
for (int i = 0; i < n; i++) cin >> y, v.push_back(y), mx = max(mx, y);
for (int i = 0; i < v.size(); i++) mn = min(mn, v[i]);
sum = mn + m;
if (mx > sum)
cout << "NO";
else {
cout << "YES" << endl;
for (int i = 0; i < v.size(); i++) {
x = 1;
for (int j = 0; j < v[i]; j++) {
cout << x << " ";
x++;
if (x > m) x = 1;
}
cout << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int L = 19;
int Lca[L][N], Depth[N], Dist, Deepest[N];
vector<int> graf[N];
int up(int a, int u) {
for (int i = 0; i < L; i++)
if (u & (1 << i)) a = Lca[i][a];
return a;
}
int lca(int a, int b) {
if (Depth[a] > Depth[b]) swap(a, b);
b = up(b, Depth[b] - Depth[a]);
if (a == b) return a;
for (int i = L - 1; i >= 0; i--)
if (Lca[i][a] != Lca[i][b]) {
a = Lca[i][a];
b = Lca[i][b];
}
return Lca[0][a];
}
bool dist(int a, int b, int c, int &d) {
if (a == c) return true;
d++;
for (int s : graf[a])
if (s != b)
if (dist(s, a, c, d)) return true;
d--;
return false;
}
void dfs0(int a, int b) {
Depth[a] = 0;
for (int s : graf[a])
if (s != b) {
dfs0(s, a);
Depth[a] = max(Depth[a], Depth[s] + 1);
}
}
int pivot(int a, int b, int c) {
vector<int> best(3, 0);
best[2] = c + 1;
for (int s : graf[a])
if (s != b) {
best[0] = max(best[0], Depth[s] + 1);
sort(best.begin(), best.end());
}
if (best[0] >= Dist) return a;
for (int s : graf[a])
if (s != b) {
int ret;
if (best[2] != Depth[s] + 1)
ret = pivot(s, a, best[2]);
else
ret = pivot(s, a, best[1]);
if (ret > 0) return ret;
}
return -1;
}
void dfs(int a, int b) {
Lca[0][a] = b;
Deepest[a] = a;
for (int s : graf[a])
if (s != b) {
Depth[s] = Depth[a] + 1;
dfs(s, a);
if (Depth[Deepest[s]] > Depth[Deepest[a]]) Deepest[a] = Deepest[s];
}
}
int main() {
int t;
scanf("%d", &t);
for (int q = 0; q < t; q++) {
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
for (int i = 1; i < n + 1; i++) graf[i].clear();
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d %d", &x, &y);
graf[x].push_back(y);
graf[y].push_back(x);
}
Dist = 0;
dist(a, 0, b, Dist);
;
dfs0(1, 0);
int p = pivot(1, 0, -1);
;
if (p == -1) {
printf("NO\n");
continue;
}
Depth[p] = 0;
dfs(p, 0);
for (int i = 1; i < n + 1; i++)
;
for (int i = 0; i < L - 1; i++)
for (int v = 1; v < n + 1; v++) {
Lca[i + 1][v] = Lca[i][Lca[i][v]];
}
set<pair<int, int> > vstd;
while (up(a, Dist) != b and up(b, Dist) != a) {
;
if (vstd.count({a, b}) == 1) break;
vstd.insert({a, b});
if (Deepest[a] == a) swap(a, b);
a = Deepest[a];
int c = lca(a, b);
int d1 = Depth[a] - Depth[c];
int d2 = Dist - d1;
int d3 = Depth[b] - Depth[c] - d2;
;
if (d2 <= 0)
b = up(a, Dist);
else {
b = up(b, d3);
}
}
if (up(a, Dist) == b or up(b, Dist) == a)
printf("YES\n");
else
printf("NO\n");
}
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n, a;
bool now, now2;
long long ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (!a) {
now2 = 1;
ans++;
continue;
}
if (a < 0) {
now ^= 1;
ans += -1 - a;
} else
ans += a - 1;
}
if (!now2 && now) ans += 2;
printf("%lld\n", ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char brr[n][m];
for (int i = 0; i < n; i += 2) {
for (int j = 0; j < m; j += 2) brr[i][j] = 'B';
}
for (int i = 1; i < n; i += 2) {
for (int j = 1; j < m; j += 2) brr[i][j] = 'B';
}
for (int i = 0; i < n; i += 2) {
for (int j = 1; j < m; j += 2) brr[i][j] = 'W';
}
for (int i = 1; i < n; i += 2) {
for (int j = 0; j < m; j += 2) brr[i][j] = 'W';
}
char arr[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cin >> arr[i][j];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] == '-') brr[i][j] = '-';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << brr[i][j];
cout << endl;
}
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.