solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010, P = 998244353;
int n, a[maxn], f[maxn][maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + n + 1, greater<int>());
f[1][1] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++)
if (f[i - 1][j]) {
bool t = a[j] >= 2 * a[i];
f[i][j] = (f[i][j] + 1LL * (i - 1 - !t) * f[i - 1][j]) % P;
if (t) (f[i][i] += f[i - 1][j]) %= P;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
(ans += f[n][i]) %= P;
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
int const N = 100000;
map<int, int> mp[N + 1];
int pa[N + 1];
int main(){
int n, m;
scanf("%d%d", &n, &m);
f(i, 1, n + 1)pa[i] = i;
f(i, 1, n + 1)mp[i][i] = 0;
while (m--){
int l, r, d;
scanf("%d%d%d", &l, &r, &d);
if (pa[l] == pa[r]) { if (mp[pa[l]][r] - mp[pa[l]][l] != d) { printf("No\n"); return 0; } }
else {
int pl = pa[l], pr = pa[r];
if (mp[pl].size() > mp[pr].size())swap(pl, pr), swap(l, r), d = -d;
int z = mp[pr][r] - d - mp[pl][l];
for (map<int, int>::iterator it = mp[pl].begin(); it != mp[pl].end(); mp[pl].erase(it++)){
pa[it->first] = pr;
mp[pr][it->first] = it->second + z;
}
}
}
printf("Yes\n");
} | 0 |
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main(void){
int n;
while(cin >> n && n != -1){
double x = 1.0, y = 0.0;
for(int i = 1; i < n; i++){
double newx, newy;
newy = x / sqrt(x * x + y * y);
newx = -y / sqrt(x * x + y * y);
x += newx; y += newy;
}
printf("%.2f\n", x);
printf("%.2f\n", y);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A,B;
cin >> A >>B;
cout << ((A == B) ? A+B : max(A,B)*2-1) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INFint = 2147483647;
const long long INF = 9223372036854775807ll;
const long long MOD = 1000000007ll;
const long double EPS = 1e-9;
int main() {
ios_base::sync_with_stdio(0);
long long n;
cin >> n;
long long ans = INF;
long long ans1 = -INF;
for (long long a = 1; a * a * a <= n; a++)
if (n % a == 0)
for (long long b = a; b * b <= n / a; b++) {
if ((n / a) % b == 0) {
long long c = n / a / b;
ans = min(ans, (a + 1) * (b + 2) * (c + 2));
ans1 = max(ans1, (a + 1) * (b + 2) * (c + 2));
ans = min(ans, (a + 2) * (b + 1) * (c + 2));
ans1 = max(ans1, (a + 2) * (b + 1) * (c + 2));
ans = min(ans, (a + 2) * (b + 2) * (c + 1));
ans1 = max(ans1, (a + 2) * (b + 2) * (c + 1));
}
}
cout << ans - n << ' ' << ans1 - n;
fprintf(stderr, "\nTIME = %lf\n", 1.0 * clock() / CLOCKS_PER_SEC);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<pair<long long, int>, pair<long long, int> > mp;
pair<long long, int> cal(long long n, int x) {
if (n == 0) return make_pair(0, 0);
if (n < 10) return make_pair(1, max(0, (int)(x - n)));
pair<long long, int> now = make_pair(n, x);
if (mp.count(now)) return mp[now];
pair<long long, int> ans = make_pair(0, 0);
long long ten = 1;
while (ten <= n / 10) ten *= 10;
while (true) {
int df = n / ten;
int nx = max(df, x);
pair<long long, int> cur = cal(n % ten, nx);
ans.first += cur.first;
n -= n % ten;
if (cur.second > 0)
n -= cur.second;
else if (nx > 0)
n -= nx, ++ans.first;
if (df == 0) break;
}
ans.second = -n;
mp[now] = ans;
return ans;
}
int main() {
long long n;
cin >> n;
cout << cal(n, 0).first << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> A, RES;
map<long long int, set<long long int>> EDGES;
vector<long long int> E, P;
long long int SUM = 0;
long long int AA;
vector<long long int> STACK;
vector<long long int> SUMSTACK;
vector<long long int> STACKIDX;
vector<long long int> RETURN;
vector<long long int> RESULT;
bool cmpStack(long long int x) { return SUM - SUMSTACK[x] <= AA; }
long long int search(long long int low, long long int high,
bool (*cmp)(long long int)) {
long long int mid;
while (low < high) {
mid = (low + high) / 2;
if (cmp(mid))
high = mid;
else
low = mid + 1;
}
return low;
}
void solve(long long int n) {
SUM += E[n];
STACK.push_back(E[n]);
SUMSTACK.push_back(E[n] + (SUMSTACK.size() ? SUMSTACK.back() : 0));
STACKIDX.push_back(n);
AA = A[n];
long long int p = search(0, STACK.size(), cmpStack);
long long int pidx = STACKIDX[p];
RETURN[pidx] -= 1;
RETURN[n] += 1;
for (auto x : EDGES[n]) {
solve(x);
RESULT[n] += RETURN[x];
RETURN[n] += RETURN[x];
}
SUM -= E[n];
STACK.pop_back();
SUMSTACK.pop_back();
STACKIDX.pop_back();
}
int main() {
ios::sync_with_stdio(false);
long long int N;
cin >> N;
A = vector<long long int>(N);
for (long long int i = (0); i < (long long int)(N); ++i) {
cin >> A[i];
}
E = P = RETURN = RESULT = vector<long long int>(N);
P[0] = 0;
E[0] = 0;
for (long long int i = (0); i < (long long int)(N - 1); ++i) {
long long int p, w;
cin >> p >> w;
--p;
P[i + 1] = p;
E[i + 1] = w;
EDGES[p].insert(i + 1);
}
SUM = 0;
solve(0);
for (long long int i = (0); i < (long long int)(N); ++i) {
cout << RESULT[i] << " ";
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
struct Node {
int x;
int y;
int forward;
};
set<int> rightup_line[N << 1];
set<int> leftup_line[N << 1];
map<pair<int, int>, bool> vis;
int n;
int m;
int k;
long long sum;
inline bool check(int x, int y) { return vis[make_pair(x, y)] == 1; }
inline bool Check(Node x, Node y) { return x.x == y.x && x.y == y.y; }
inline int Leftup_Line(int x, int y) { return x - y + m + 1; }
inline int Rightup_Line(int x, int y) { return x + y; }
inline void cale(int x, int y) {
rightup_line[Rightup_Line(x, y)].insert(x);
leftup_line[Leftup_Line(x, y)].insert(x);
vis[make_pair(x, y)] = true;
}
inline void build() {
for (int i = 0; i <= m + 1; i++) {
cale(0, i);
cale(n + 1, i);
}
for (int i = 1; i <= n; i++) {
cale(i, 0);
cale(i, m + 1);
}
}
pair<Node, int> solve(Node now) {
Node node;
set<int>::iterator it;
if (now.forward == 1) {
it = leftup_line[Leftup_Line(now.x, now.y)].lower_bound(now.x);
it--;
node.x = now.x - (abs(*it - now.x) - 1);
node.y = now.y - (abs(*it - now.x) - 1);
if (check(node.x - 1, node.y) && check(node.x, node.y - 1))
node.forward = 3;
else if (check(node.x - 1, node.y)) {
node.y -= 1;
node.forward = 4;
} else if (check(node.x, node.y - 1)) {
node.x -= 1;
node.forward = 2;
} else
node.forward = 3;
}
if (now.forward == 2) {
it = rightup_line[Rightup_Line(now.x, now.y)].lower_bound(now.x);
it--;
node.x = now.x - (abs(*it - now.x) - 1);
node.y = now.y + (abs(*it - now.x) - 1);
if (check(node.x - 1, node.y) && check(node.x, node.y + 1))
node.forward = 4;
else if (check(node.x - 1, node.y)) {
node.y += 1;
node.forward = 3;
} else if (check(node.x, node.y + 1)) {
node.x -= 1;
node.forward = 1;
} else
node.forward = 4;
}
if (now.forward == 3) {
it = leftup_line[Leftup_Line(now.x, now.y)].lower_bound(now.x);
node.x = now.x + (abs(*it - now.x) - 1);
node.y = now.y + (abs(*it - now.x) - 1);
if (check(node.x + 1, node.y) && check(node.x, node.y + 1))
node.forward = 1;
else if (check(node.x + 1, node.y)) {
node.y += 1;
node.forward = 2;
} else if (check(node.x, node.y + 1)) {
node.x += 1;
node.forward = 4;
} else
node.forward = 1;
}
if (now.forward == 4) {
it = rightup_line[Rightup_Line(now.x, now.y)].lower_bound(now.x);
node.x = now.x + (abs(*it - now.x) - 1);
node.y = now.y - (abs(*it - now.x) - 1);
if (check(node.x + 1, node.y) && check(node.x, node.y - 1))
node.forward = 2;
else if (check(node.x + 1, node.y)) {
node.y -= 1;
node.forward = 1;
} else if (check(node.x, node.y - 1)) {
node.x += 1;
node.forward = 3;
} else
node.forward = 2;
}
return make_pair(node, abs(*it - now.x));
}
inline bool Solve(Node now) {
Node node = now;
pair<Node, int> ans;
do {
ans = solve(now);
sum += (long long)ans.second;
if (ans.first.forward == 1 && now.forward == 3) return 1;
if (ans.first.forward == 2 && now.forward == 4) return 1;
if (ans.first.forward == 3 && now.forward == 1) return 1;
if (ans.first.forward == 4 && now.forward == 2) return 1;
now = ans.first;
} while (!Check(node, now));
return 0;
}
int main() {
n = read();
m = read();
k = read();
build();
for (int i = 1; i <= k; i++) {
int x = read();
int y = read();
cale(x, y);
}
int x = read();
int y = read();
int forward;
string s;
Node now;
cin >> s;
if (s == "NW") forward = 1;
if (s == "NE") forward = 2;
if (s == "SE") forward = 3;
if (s == "SW") forward = 4;
now.x = x;
now.y = y;
now.forward = forward;
now = solve(now).first;
if (Solve(now)) {
sum--;
if (now.forward == 1)
now.forward = 3;
else if (now.forward == 2)
now.forward = 4;
else if (now.forward == 3)
now.forward = 1;
else if (now.forward == 4)
now.forward = 2;
Solve(now);
}
cout << sum << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 7340033;
int main() {
long long a[31][1001];
long long b[1001];
for (int i = 0; i <= 30; ++i) {
a[i][0] = 1;
for (int j = 1; j <= 1000; ++j) a[i][j] = 0;
}
b[0] = 1;
for (int i = 1; i <= 1000; ++i) b[i] = 0;
for (int i = 1; i <= 30; ++i) {
for (int j = 0; j <= 999; ++j)
for (int k = 999 - j; k >= 0; --k) a[i][j + k + 1] += b[j] * b[k];
for (int j = 0; j <= 1000; ++j) {
a[i][j] %= mod;
b[j] = 0;
}
for (int j = 0; j <= 1000; ++j)
for (int k = 1000 - j; k >= 0; --k) b[j + k] += a[i][j] * a[i][k];
for (int j = 0; j <= 1000; ++j) b[j] %= mod;
}
int q, n, k;
cin >> q;
while (q--) {
cin >> n >> k;
int m = 0;
for (; n % 2; n /= 2) ++m;
if (n == 0) --m;
cout << a[m][k] << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int INF = 1e8 + 7;
int n;
int Min[N][20], Max[N][20];
struct Node {
int x, y;
bool operator<(const Node& p) const { return x < p.x; }
} nod[N];
long long sqr(long long x) { return x * x; }
void rmq() {
for (int i = 0; i < n; ++i) {
Min[i][0] = nod[i].y;
Max[i][0] = nod[i].y;
}
for (int j = 1; (1 << j) <= n; ++j)
for (int i = 0; i + (1 << j) - 1 < n; ++i) {
Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);
}
}
int query(int t, int l, int r) {
if (l > r) return (1 - (t << 1)) * INF;
int k = (int)log2(r - l + 1.0);
switch (t) {
case 0:
return min(Min[l][k], Min[r - (1 << k) + 1][k]);
case 1:
return max(Max[l][k], Max[r - (1 << k) + 1][k]);
}
return 0;
}
bool isok(long long D, long long x, long long ymin, long long ymax) {
if (x > D) return false;
if (ymin != -INF && x + sqr(ymin) > D) return false;
if (ymax != INF && x + sqr(ymax) > D) return false;
if (ymin != -INF && ymax != INF && sqr(ymax - ymin) > D) return false;
return true;
}
bool check(long long D) {
if (sqr(nod[n - 1].x - nod[0].x) <= D) return true;
if (sqr(query(1, 0, n - 1) - query(0, 0, n - 1)) <= D) return true;
int l = 0, r = n - 1, ymin, ymax;
r = 0;
for (int i = 0; i < n; ++i) {
if (r < i) r = i;
while (r + 1 < n && abs(nod[r + 1].x) <= abs(nod[i].x) &&
sqr(nod[r + 1].x - nod[i].x) <= D)
++r;
while (abs(nod[r].x) > abs(nod[i].x)) --r;
l = i;
ymin = min(query(0, 0, l - 1), query(0, r + 1, n - 1));
ymax = max(query(1, 0, l - 1), query(1, r + 1, n - 1));
long long x = sqr(nod[l].x);
if (isok(D, x, ymin, ymax)) return true;
}
l = 0, r = n - 1;
l = n - 1;
for (int i = n - 1; i >= 0; --i) {
if (l > i) l = i;
while (l - 1 >= 0 && abs(nod[l - 1].x) <= abs(nod[i].x) &&
sqr(nod[l - 1].x - nod[i].x) <= D)
--l;
while (abs(nod[l].x) > abs(nod[i].x)) ++l;
r = i;
ymin = min(query(0, 0, l - 1), query(0, r + 1, n - 1));
ymax = max(query(1, 0, l - 1), query(1, r + 1, n - 1));
long long x = max(sqr(nod[l].x), sqr(nod[r].x)),
y = max(sqr(ymin), sqr(ymax));
if (isok(D, x, ymin, ymax)) return true;
}
return false;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &nod[i].x, &nod[i].y);
}
sort(nod, nod + n);
rmq();
long long L = 0, R = sqr(nod[n - 1].x - nod[0].x);
while (L + 1 < R) {
long long M = (L + R) >> 1;
check(M) ? R = M : L = M;
}
long long ans = check(L) ? L : R;
printf("%I64d", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int mod = 1e9 + 7;
int p[maxn], cnt[maxn], d[maxn];
long long sum[maxn];
long long gcd(long long x, long long y) {
if (!y) return x;
return gcd(y, x % y);
}
void add(int &ans, int tmp) { ans = (ans + tmp) % mod; }
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &sum[i]);
for (int i = 2; i <= n; i++) scanf("%d", &p[i]);
for (int i = n; i > 1; i--) sum[p[i]] += sum[i];
for (int i = 1; i <= n; i++) {
long long k = sum[1] / gcd(sum[1], sum[i]);
if (k <= n) cnt[k]++;
}
for (int i = n; i; i--)
for (int j = 2 * i; j <= n; j += i) cnt[j] += cnt[i];
int ans = 0;
d[1] = 1;
for (int i = 1; i <= n; i++) {
if (cnt[i] != i) continue;
add(ans, d[i]);
for (int j = 2 * i; j <= n; j += i) add(d[j], d[i]);
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, b;
cin >> n >> b;
int nums[n];
int sum = 0;
int maxMil = 0;
for (int i = 0; i < n; i++) {
cin >> nums[i];
if (nums[i] > maxMil) {
maxMil = nums[i];
}
sum += nums[i];
}
int diff = maxMil * n - sum;
if (b >= diff) {
for (int i = 0; i < n; i++) {
double res = (0.0 + maxMil - nums[i]);
res += ((b - diff + 0.0) / n);
printf("%.6f\n", res);
}
} else {
cout << -1;
}
}
| 1 |
#include <bits/stdc++.h>
const int N = 200001;
using namespace std;
int f(string &s) {
long long int pr = 1;
long long int hash = 0;
for (char c : s) {
hash += (c - 'a' + 1) * pr;
pr = (pr * 33) % 1000000007;
}
return hash % 1000000007;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<pair<string, int>> a[n];
map<string, int> ind;
long long int hash[n];
for (int i = 0; i < n; ++i) {
string var, just, op1, op, op2;
cin >> var >> just >> op1;
if (op1[0] == '0' || op1[0] == '1') {
a[i] = {make_pair(var, 0), make_pair(op1, 0)};
} else {
cin >> op >> op2;
a[i] = {make_pair(var, 0), make_pair(op1, ind[op1]), make_pair(op, 0),
make_pair(op2, ind[op2])};
}
hash[i] = f(var);
ind[var] = i;
}
string all[3] = {"XOR", "OR", "AND"};
for (int j = 0; j < m; ++j) {
unordered_map<int, int> d;
int o = 0;
for (int i = 0; i < n; ++i) {
if (a[i].size() == 4) {
string &op1 = a[i][1].first;
string &op2 = a[i][3].first;
string &op = a[i][2].first;
int fb = (op1 == "?" ? 1 : d[a[i][1].second]);
int sb = (op2 == "?" ? 1 : d[a[i][3].second]);
int h;
if (op == "OR") h = fb | sb;
if (op == "AND") h = fb & sb;
if (op == "XOR") h = fb ^ sb;
o += h;
d[i] = h;
} else {
string &var = a[i][0].first;
string &op1 = a[i][1].first;
if (op1[j] == '1') {
++o;
d[i] = 1;
}
}
}
int z = 0;
d.clear();
for (int i = 0; i < n; ++i) {
if (a[i].size() == 4) {
string &op1 = a[i][1].first;
string &op2 = a[i][3].first;
string &op = a[i][2].first;
int fb = (op1 == "?" ? 0 : d[a[i][1].second]);
int sb = (op2 == "?" ? 0 : d[a[i][3].second]);
int h;
if (op == "OR") h = fb | sb;
if (op == "AND") h = fb & sb;
if (op == "XOR") h = fb ^ sb;
z += h;
d[i] = h;
} else {
string &var = a[i][0].first;
string &op1 = a[i][1].first;
if (op1[j] == '1') {
++z;
d[i] = 1;
}
}
}
cout << (o < z ? 1 : 0);
}
cout << '\n';
for (int j = 0; j < m; ++j) {
unordered_map<int, int> d;
int o = 0;
for (int i = 0; i < n; ++i) {
if (a[i].size() == 4) {
string &op1 = a[i][1].first;
string &op2 = a[i][3].first;
string &op = a[i][2].first;
int fb = (op1 == "?" ? 1 : d[a[i][1].second]);
int sb = (op2 == "?" ? 1 : d[a[i][3].second]);
int h;
if (op == "OR") h = fb | sb;
if (op == "AND") h = fb & sb;
if (op == "XOR") h = fb ^ sb;
o += h;
d[i] = h;
} else {
string &var = a[i][0].first;
string &op1 = a[i][1].first;
if (op1[j] == '1') {
++o;
d[i] = 1;
}
}
}
int z = 0;
d.clear();
for (int i = 0; i < n; ++i) {
if (a[i].size() == 4) {
string &op1 = a[i][1].first;
string &op2 = a[i][3].first;
string &op = a[i][2].first;
int fb = (op1 == "?" ? 0 : d[a[i][1].second]);
int sb = (op2 == "?" ? 0 : d[a[i][3].second]);
int h;
if (op == "OR") h = fb | sb;
if (op == "AND") h = fb & sb;
if (op == "XOR") h = fb ^ sb;
z += h;
d[i] = h;
} else {
string &var = a[i][0].first;
string &op1 = a[i][1].first;
if (op1[j] == '1') {
++z;
d[i] = 1;
}
}
}
cout << (o > z ? 1 : 0);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int id[3010][6010], par[600001], cnt;
int fnd(int k) { return par[k] == k ? k : par[k] = fnd(par[k]); }
constexpr int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1, 0},
dy[] = {-1, 0, 1, -1, 1, -1, 0, 1, 0};
int n, m;
vector<int> neighbor(int x, int y) {
vector<int> ret;
for (int dx = -1; dx <= 1; ++dx) {
int ox = x + dx;
if (ox >= 0 && ox < n)
for (int dy = -1; dy <= 1; ++dy) {
int oy = (y + dy + 2 * m) % (2 * m);
if (id[ox][oy] != -1) ret.push_back(id[ox][oy]);
}
}
return ret;
}
void mark(int x, int y) {
id[x][y] = cnt;
par[cnt] = cnt++;
auto v = neighbor(x, y);
for (auto c : v) par[fnd(c)] = fnd(id[x][y]);
}
bool can_mark(int x, int y) {
auto v1 = neighbor(x, y), v2 = neighbor(x, m + y);
for (auto c1 : v1)
for (auto c2 : v2)
if (fnd(c1) == fnd(c2)) return false;
return true;
}
int main() {
int q, ans = 0;
scanf("%d%d%d", &n, &m, &q);
if (m == 1) {
printf("0\n");
return 0;
}
memset(id, -1, sizeof(id));
while (q--) {
int t1, t2;
scanf("%d%d", &t1, &t2);
--t1;
--t2;
if (can_mark(t1, t2)) {
++ans;
mark(t1, t2);
mark(t1, t2 + m);
}
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
vector<int> a(5);
string s;
long long q = 0;
for (int i = 1; i < 5; ++i) {
cin >> a[i];
}
cin >> s;
for (int i = 0; i < (int)s.size(); ++i) {
q += a[s[i] - '0'];
}
cout << q;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
if (rsz <= 0) {
ptr = 0;
rsz = fread(bf, 1, bfsz, stdin);
if (rsz <= 0) return EOF;
}
--rsz;
return bf[ptr++];
}
void ga(char &c) {
c = EOF;
while (!isalpha(c)) c = gc();
}
int gs(char s[]) {
int l = 0;
char c = gc();
while (isspace(c)) c = gc();
while (c != EOF && !isspace(c)) {
s[l++] = c;
c = gc();
}
s[l] = '\0';
return l;
}
template <class T>
bool gi(T &v) {
v = 0;
char c = gc();
while (c != EOF && c != '-' && !isdigit(c)) c = gc();
if (c == EOF) return false;
bool neg = c == '-';
if (neg) c = gc();
while (isdigit(c)) {
v = v * 10 + c - '0';
c = gc();
}
if (neg) v = -v;
return true;
}
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int toInt(char xx) { return xx - '0'; }
char toChar(int xx) { return xx + '0'; }
int isDigit(char xx) { return ('0' <= xx && xx <= '9'); }
void swapInt(int &x, int &y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
class bigint {
public:
int a[100];
void add(bigint &y);
void print();
};
void bigint::add(bigint &y) {
int i, d;
if (a[0] < y.a[0]) a[0] = y.a[0];
d = 0;
for (i = 1; i <= a[0]; i++) {
a[i] += y.a[i] + d;
d = a[i] / 1000000;
a[i] %= 1000000;
}
while (d > 0) {
a[0]++;
a[a[0]] = d % 1000000;
d /= 1000000;
}
}
void bigint::print() {
int i, n = a[0];
cout << a[n];
for (i = n - 1; i >= 1; i--) {
if (a[i] < 10)
cout << "00000";
else if (a[i] < 100)
cout << "0000";
else if (a[i] < 1000)
cout << "000";
else if (a[i] < 10000)
cout << "00";
else if (a[i] < 100000)
cout << "0";
cout << a[i];
}
}
int exitInput = 0;
int ntest = 1, itest = 1;
void read();
void solve();
int main() {
clock_t t1, t2;
t1 = clock();
for (itest = 1; itest <= ntest; ++itest) {
read();
if (exitInput) {
break;
}
if (itest > 1) {
}
solve();
}
t2 = clock();
return 0;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const long long Mod = 1e9 + 7;
const int maxn = 24;
int n, a[maxn], k, bad[2];
int f[1 << maxn];
int isbad(int val) {
if (k >= 1 && val == bad[0]) return 1;
if (k >= 2 && val == bad[1]) return 1;
return 0;
}
void dp() {
int mask, i, sum;
f[0] = 1;
for (mask = 1; mask < (1 << n); ++mask) {
int &ref = f[mask];
ref = sum = 0;
for (i = 0; i < n; ++i) {
if (mask & (1 << i)) {
sum += a[i];
ref += f[mask & ~(1 << i)];
if (ref >= Mod) {
ref -= Mod;
}
}
}
if (isbad(sum)) {
ref = 0;
}
}
}
void read() {
gi(n);
int i;
for (i = 0; i < n; ++i) {
gi(a[i]);
}
gi(k);
for (i = 0; i < k; ++i) {
gi(bad[i]);
}
}
void solve() {
dp();
cout << f[(1 << n) - 1];
}
| 5 |
#include <bits/stdc++.h>
unsigned A[111], Ai, Q[111], Qi, Ri;
char R[11111][111];
unsigned F() {
unsigned i, m, m2, d;
Qi = m = m2 = d = 0;
for (i = -1; ++i < Ai;) {
if (A[i] == m)
Q[Qi++] = i;
else if (A[i] > m) {
m = A[i];
Q[0] = i;
Qi = 1;
}
}
if (Qi == Ai) return 0;
if (Qi == 1) {
for (i = -1; ++i < Ai;)
if (A[i] < m && A[i] >= m2) m2 = A[d = i];
Q[Qi++] = d;
}
return 1;
}
int main() {
unsigned i = -1, k;
for (scanf("%u", &Ai); ++i < Ai;) scanf("%u", A + i);
for (; F(); ++Ri) {
for (i = -1; ++i < Ai;) R[Ri][i] = '0';
if (Qi > 6)
k = 5;
else if (Qi == 6)
k = 4;
else
k = Qi;
for (i = -1; ++i < k;) {
R[Ri][Q[i]] = '1';
if (A[Q[i]]) --A[Q[i]];
}
}
printf("%u\n%u\n", *A, Ri);
while (Ri) printf("%s\n", R[--Ri]);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e7 + 5e6 + 20;
int a[2][5];
long long ans[2][N];
void getAns(int t) {
iota(ans[t], ans[t] + N - 10, 0);
for (int i = 0; i < N - 10; i++) {
for (int j = 0; j < 3; j++) {
if (a[t][j] <= i)
ans[t][i] = max(1ll * ans[t][i],(long long) ans[t][i - a[t][j]] + a[1 - t][j]);
}
}
}
int main() {
ios_base::sync_with_stdio (false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 3; j++)
cin >> a[i][j];
getAns(0);
getAns(1);
cout << ans[1][ans[0][n]];
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int h, a, c;
int H, A;
scanf("%d %d %d", &h, &a, &c);
scanf("%d %d", &H, &A);
int i;
int fightArr[100000];
for (i = 0; H > 0; ++i) {
if (a >= H) {
fightArr[i] = 1;
H -= a;
continue;
}
if (A >= h) {
fightArr[i] = 0;
h += c;
} else {
fightArr[i] = 1;
H -= a;
}
h -= A;
}
printf("%d\n", i);
fightArr[i] = -1;
for (int j = 0; fightArr[j] != -1; ++j) {
if (fightArr[j]) {
printf("STRIKE\n");
}
if (!fightArr[j]) {
printf("HEAL\n");
}
}
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int x,y;
int minx=100000;
for(int i=0;i<n;i++){
cin>>x>>y;
if(y<=m){
minx=min(minx,x);
}
}
if(minx==100000){
cout<<"TLE";
}else{
cout<<minx;
}
}
| 0 |
#include <bits/stdc++.h>
int len = 0;
int cha[200008];
double sum = 0;
int main() {
int i, j, k, n, m, a, x;
scanf("%d", &n);
cha[len++] = 0;
while (n--) {
scanf("%d", &m);
if (m == 1) {
scanf("%d%d", &a, &x);
sum += (a * x);
if (a == len)
cha[len - 1] += x;
else
cha[a - 1] -= x;
} else if (m == 2) {
scanf("%d", &a);
cha[len] = a;
cha[len - 1] = a - cha[len - 1];
len++;
sum += a;
} else {
sum -= cha[len - 1];
cha[len - 2] = cha[len - 1] - cha[len - 2];
len--;
}
printf("%.6lf\n", sum * 1.0 / len);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
class Cht {
vector<pair<long long, long long>> lines;
vector<long double> intersections;
public:
void addLine(long long k, long long b) {
pair<long long, long long> line = {k, b};
if (!lines.empty() && k == lines.back().first) {
if (b >= lines.back().second) {
return;
}
lines.pop_back();
if (!intersections.empty()) {
intersections.pop_back();
}
}
while (lines.size() > 1) {
long double x = intersections[lines.size() - 2];
if (1. * line.first * x + line.second <=
1. * lines.back().first * x + lines.back().second) {
lines.pop_back();
intersections.pop_back();
} else {
break;
}
}
if (!lines.empty()) {
intersections.push_back(1. * (lines.back().second - line.second) /
(line.first - lines.back().first));
}
lines.push_back(line);
}
long long getMin(long long x) const {
if (lines.size() == 1) {
return 1LL * lines[0].first * x + lines[0].second;
}
int bl = 1;
int br = lines.size() - 1;
int bRes = 0;
while (bl <= br) {
int mid = (bl + br) / 2;
if (intersections[mid - 1] <= x) {
bRes = mid;
bl = mid + 1;
} else {
br = mid - 1;
}
}
return 1LL * lines[bRes].first * x + lines[bRes].second;
}
};
long long dp[100000][101];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, p;
cin >> n >> m >> p;
vector<long long> dist(n, 0);
for (int i = 1; i < n; i++) {
cin >> dist[i];
dist[i] += dist[i - 1];
}
vector<long long> t(m);
long long ansAdd = 0;
long long mn = 0;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
t[i] = 1LL * y - dist[x - 1];
mn = min(mn, t[i]);
}
for (auto &x : t) {
x += mn;
}
sort(t.begin(), t.end());
vector<long long> pref(m);
for (int i = 0; i < m; i++) {
pref[i] = t[i];
if (i != 0) {
pref[i] += pref[i - 1];
}
}
dp[0][1] = 0;
for (int i = 1; i < m; i++) {
dp[i][1] = i * t[i] - pref[i - 1];
}
for (int ppl = 2; ppl <= p; ppl++) {
Cht cht;
dp[0][ppl] = 0;
for (int i = 1; i < m; i++) {
dp[i][ppl] = dp[i - 1][ppl - 1];
if (i > 1) {
dp[i][ppl] =
min(dp[i][ppl], (i - 1) * t[i] - pref[i - 1] + cht.getMin(t[i]));
}
cht.addLine(-(i - 1), dp[i - 1][ppl - 1] + pref[i - 1]);
}
}
cout << dp[m - 1][p] + ansAdd << endl;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const int INF = 1 << 30;
template <typename T>
inline bool chkmax(T& aa, T bb) {
return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T& aa, T bb) {
return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int maxn = 100000;
clock_t start;
int a[maxn], b[maxn];
bitset<maxn> divm;
int n, m, nm;
inline bool checkneigh(int x, int y) {
if (x > y) swap(x, y);
int diff = y - x;
return ((diff == m) || diff == 1 && divm[y]);
}
inline void check() {
for (int x = 0; x < nm - m; x++) {
if (checkneigh(a[x], a[x + m])) return;
}
for (int x = 1; x < nm; x++) {
if (divm[x] && checkneigh(a[x], a[x - 1])) return;
}
printf("YES");
for (int x = 0; x < nm; x++) {
if (!divm[x]) printf("\n");
printf("%d ", a[x] + 1);
}
exit(0);
}
void pr() {
for (int x = 0; x < nm; x++) {
if (!divm[x]) printf("\n");
printf("%d ", a[x]);
}
}
void brute() {
for (int x = 0; x < nm; x++) a[x] = x;
while (1) {
clock_t now = clock();
if (now - start > 1.72 * CLOCKS_PER_SEC) break;
srand(now);
random_shuffle(a, a + nm);
check();
}
}
void smart1(int mode) {
if (mode & 2) swap(n, m);
int pos = 0;
for (int x = 0; x < n; x++) {
int base = x * m;
for (int y = base; y < base + m; y += 2) {
a[pos++] = y;
}
for (int y = base + 1; y < base + m; y += 2) {
a[pos++] = y;
}
if (x & 1) {
reverse(a + base, a + base + m);
if (mode & 1)
for (int y = base + 1; y < base + m; y += 2) {
swap(a[y - 1], a[y]);
}
}
}
{
memcpy(b, a, sizeof(a));
int id = 0;
for (int x = 0; x < n; x += 2) {
for (int y = 0; y < m; y++) {
a[id++] = b[x * m + y];
}
}
for (int x = 1; x < n; x += 2) {
for (int y = 0; y < m; y++) {
a[id++] = b[x * m + y];
}
}
}
for (int x = 1; x < n; x += 2) {
int base = x * m;
for (int y = base + 1; y < base + m; y += 2) {
swap(a[y], a[y - m]);
}
}
if (mode & 2) {
memcpy(b, a, sizeof(a));
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
int val = b[x * m + y];
int r = val / m, c = val % m;
a[y * n + x] = c * n + r;
}
}
swap(n, m);
}
check();
}
int main() {
start = clock();
ios_base::sync_with_stdio(false);
cin.tie(0);
scanf("%d%d", &n, &m);
nm = n * m;
divm.set();
for (int x = 0; x < nm; x += m) divm[x] = false;
for (int mode = 0; mode < 4; mode++) smart1(mode);
brute();
printf("NO\n");
}
static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char* stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
size_t remaining = stdinDataEnd - stdinPos;
if (remaining < amount) {
memmove(stdinBuffer, stdinPos, remaining);
size_t sz = fread(stdinBuffer + remaining, 1,
sizeof(stdinBuffer) - remaining, stdin);
stdinPos = stdinBuffer;
stdinDataEnd = stdinBuffer + remaining + sz;
if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
}
}
int rint() {
readAhead(16);
int x = 0;
bool neg = false;
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
if (*stdinPos == '-') {
++stdinPos;
neg = true;
}
while (*stdinPos >= '0' && *stdinPos <= '9') {
x *= 10;
x += *stdinPos - '0';
++stdinPos;
}
return neg ? -x : x;
}
char rch() {
readAhead(16);
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
char ans = *stdinPos;
++stdinPos;
return ans;
}
long long rlong() {
readAhead(32);
long long x = 0;
bool neg = false;
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
if (*stdinPos == '-') {
++stdinPos;
neg = true;
}
while (*stdinPos >= '0' && *stdinPos <= '9') {
x *= 10;
x += *stdinPos - '0';
++stdinPos;
}
return neg ? -x : x;
}
| 2 |
#include <bits/stdc++.h>
class Solve {
private:
int H, W;
std::vector<std::string> data;
bool is_founded[26]{};
bool is_suspicious()
{
scanf("%d%d", &H, &W);
data.resize(H);
for (auto& e: data) std::cin >> e;
while (true)
{
if (square_exist()) continue;
return alphabet_exist();
}
}
bool alphabet_exist()
{
for (auto& e: data)
for (auto& f: e)
if ('A' <= f && f <= 'Z')
return true;
return false;
}
bool square_exist()
{
for (char c{'A'}; c <= 'Z'; c++)
if (is_square(c))
{
change_available(c);
return true;
}
return false;
}
bool is_square(char c)
{
int left{W}, right{-1}, top{H}, bottom{-1};
for (int i{}; i < H; i++)
for (int j{}; j < W; j++)
if (data[i][j] == c)
{
left = std::min(left, j);
right = std::max(right, j);
top = std::min(top, i);
bottom = std::max(bottom, i);
}
if (left == W) return false;
for (int i{top}; i <= bottom; i++)
for (int j{left}; j <= right; j++)
if (data[i][j] != c && data[i][j] != '?')
return false;
return true;
}
void change_available(char c)
{
for (auto& e: data)
for (auto& f: e)
if (f == c)
f = '?';
}
public:
bool is_last_query{};
Solve()
{
if (is_suspicious()) puts("SUSPICIOUS");
else puts("SAFE");
}
};
int main()
{
int n;
scanf("%d", &n);
for (int i{}; i < n; i++)
Solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest;
cin >> ntest;
for(int z = 1;z <= ntest; z++)
{
int n;
string a, b;
cin >> n >> a >> b;
bool check = true;
int wrong = 0;
int dem0 = 0;
int dem1 = 0;
int len = 0;
for(int i = 0; i < n; i++)
{
len++;
if(a[i] != b[i]) wrong++;
if(a[i] == '0') dem0++;
else dem1++;
//cout << i << " " << dem0 << " " << dem1 << endl;
if(dem0 == dem1)
{
dem0 = dem1 = 0;
if(wrong == len)
{
//cout << 4 << endl;
for(int j = i - len + 1; j <= i; j++)
{
int t = a[j];
t -= 48;
t = 1 - t;
a[j] = (t + 48);
}
//cout << i << " " << len << " " << wrong << endl;
//cout << a << endl << b << endl;
wrong = 0;
len = 0;
continue;
}
if(wrong == 0)
{
len = 0;
continue;
}
}
}
//cout << a << endl << b << endl;
if(a != b) cout << "NO" << '\n';
else cout << "YES" << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
long long n, m, cap, mod, b[503], dp[503][503], mx;
using namespace std;
int main() {
cin >> n >> m >> cap >> mod;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (long long i = 0; i < n; i++) cin >> b[i];
for (long long p = 0; p < n; p++)
for (long long l = 0; l <= m; l++)
for (long long e = 0; e + b[p] <= cap; e++)
dp[l + 1][e + b[p]] =
((dp[l + 1][e + b[p]]) % mod + dp[l][e] % mod) % mod;
long long ans = 0;
for (long long e = 0; e <= cap; e++) ans = (ans % mod + dp[m][e] % mod) % mod;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const long long mod = 1e9 + 7;
int a[N];
int random(int n) { return (int)((long long)rand() * rand()) % n; }
set<pair<int, int> > ans;
void solve() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
for (int i = 1; i <= n; i++) {
long long sum = a[i + 1];
for (int j = i + 2; j <= n; j++) {
int x = log2(a[i]) + 1;
int y = log2(a[j]) + 1;
if (sum == (a[i] ^ a[j])) {
ans.insert(pair<int, int>(i, j));
}
sum += a[j];
if (sum >= (1 << max(x, y))) break;
}
}
for (int i = n; i >= 1; i--) {
long long sum = a[i - 1];
for (int j = i - 2; j >= 1; j--) {
int x = log2(a[i]) + 1;
int y = log2(a[j]) + 1;
if (sum == (a[i] ^ a[j])) {
ans.insert(pair<int, int>(j, i));
}
sum += a[j];
if (sum >= (1 << max(x, y))) break;
}
}
printf("%d\n", ans.size());
}
int main() {
srand((unsigned)time(NULL));
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline long long qpow(long long a, int b) {
long long res = 1;
for (; b; a = a * a % 1000000007, b >>= 1)
if (b & 1) res = res * a % 1000000007;
return res;
}
int main() {
long long n, m;
cin >> n >> m;
cout << qpow((qpow(2, m) - 1), n) << endl;
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
set<string>s;
while(t--) {
string ss;
cin>>ss;
s.insert(ss);
}
cout<<s.size();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
char st[N];
vector<int> sta[2][2], d[N];
int ans, n, q, now;
int main() {
scanf("%s", st);
n = strlen(st);
int k = 0, p = 0, m = 0, snow = 0;
for (int i = 0; i < n; i++) {
int p = (st[i] == 'R'), t = 1;
for (int q = p, k = 1; k <= 2; q ^= 1, k++)
if (sta[q][p ^ 1].size() > 0) {
int now = sta[q][p ^ 1][sta[q][p ^ 1].size() - 1];
sta[q][p].push_back(now);
sta[q][p ^ 1].pop_back();
d[now].push_back(i + 1);
t = 0;
break;
}
if (t) d[++m].push_back(i + 1), sta[p][p].push_back(m);
if (p)
snow++;
else
snow--;
}
printf("%d\n", m - 1);
if (abs(snow) > 1) {
printf("CRASH");
return 0;
}
if (snow == 0)
snow = st[0] == 'R';
else if (snow == -1)
snow = 0;
p = snow;
while (1) {
if (p)
q = (sta[p][0].size() == 0);
else
q = (sta[p][1].size() > 0);
int i = sta[p][q][sta[p][q].size() - 1];
for (int j = 0; j < d[i].size(); j++) {
printf("%d", d[i][j]);
ans++;
if (ans == n) {
puts("");
exit(0);
} else
putchar(' ');
}
sta[p][q].pop_back();
p = q ^ 1;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[10000001];
long long sum;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (n == 1) {
cout << a[1];
return 0;
}
for (int i = 2; i <= n; i++) {
sum += ((a[i] - 1) * (i - 1));
}
cout << sum;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 2147483647;
int a1[100005], a2[100005], a3[100005], d[100005], b[100005];
int sum = 0, ans[100005], num[100005], n, m;
bool comp(int i, int j) {
return a3[i] < a3[j] || (a3[i] == a3[j] && a2[i] < a2[j]);
}
void init() {
int tmp, i;
scanf("%d%d", &n, &m);
tmp = max(1, (int)sqrt((double)n));
for (i = 1; i <= n; i++) scanf("%d", d + i);
for (i = 1; i <= m; i++) {
scanf("%d%d", a1 + i, a2 + i);
a3[i] = (a2[i] - a1[i]) / tmp;
b[i] = i;
}
sort(b + 1, b + 1 + m, comp);
}
void add(int v) {
if (v > n) return;
if (num[v] == v) sum--;
num[v]++;
if (num[v] == v) sum++;
}
void mirus(int v) {
if (v > n) return;
if (num[v] == v) sum--;
num[v]--;
if (num[v] == v) sum++;
}
void work() {
int i, j, t1, t2, t3, t4;
for (i = a1[b[1]]; i <= a2[b[1]]; i++) {
add(d[i]);
}
ans[b[1]] = sum;
for (i = 2; i <= m; i++) {
t1 = a1[b[i - 1]];
t2 = a2[b[i - 1]];
t3 = a1[b[i]];
t4 = a2[b[i]];
if (t1 < t3)
for (j = t1; j <= t3 - 1; j++) mirus(d[j]);
if (t1 > t3)
for (j = t3; j <= t1 - 1; j++) add(d[j]);
if (t2 < t4)
for (j = t2 + 1; j <= t4; j++) add(d[j]);
if (t2 > t4)
for (j = t4 + 1; j <= t2; j++) mirus(d[j]);
ans[b[i]] = sum;
}
for (i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
int main() {
init();
work();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN=5e5+10;
char s[MAXN];
int len,res=0,nxt1[MAXN],nxt2[MAXN];
void cal_nxt(int* nxt)
{
int k=0;
for(int i=2;i<=len;i++)
{
while(k&&s[k+1]!=s[i]) k=nxt[k];
if(s[k+1]==s[i]) k++;nxt[i]=k;
}
}
bool check(int* nxt,int pos)
{
if(!nxt[pos]) return false;
return (pos%(pos-nxt[pos])==0);
}
int main()
{
scanf("%s",s+1);len=strlen(s+1);
cal_nxt(nxt1);
if(!check(nxt1,len)) printf("1\n1");
else if(nxt1[len]==len-1) printf("%d\n1",len);
else
{
reverse(s+1,s+len+1);
cal_nxt(nxt2);
for(int i=1;i<=len;i++)
res+=(!check(nxt1,i))&(!check(nxt2,len-i));
printf("2\n%d",res);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt;
int main() {
int arr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
for (int i = 0; i < 15; ++i) {
cout << arr[i] << endl;
cin >> s;
if (s == "yes") ++cnt;
if (i < 4 && s == "yes") {
cout << arr[i] * arr[i] << endl;
cin >> s;
if (s == "yes") return cout << "composite" << endl, 0;
}
if (cnt > 1) return cout << "composite" << endl, 0;
}
cout << "prime" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T sqr(const T &x) {
return x * x;
}
inline long long sqr(int x) { return sqr<long long>(x); }
template <class T>
T binpow(const T &a, long long n) {
return n == 0 ? 1 : sqr(binpow(a, n / 2)) * (n % 2 ? a : 1);
}
long long binpow(long long a, long long n, long long modulo) {
return n == 0 ? 1
: sqr(binpow(a, n / 2, modulo)) % modulo * (n % 2 ? a : 1) %
modulo;
}
long long gcd(long long a, long long b, long long &x, long long &y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
long long x1, y1;
long long d = gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
inline long long phi(long long n) {
long long result = n;
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
if (n > 1) result -= result / n;
return result;
}
inline vector<long long> inverseAll(long long m) {
vector<long long> r(m);
r[1] = 1;
for (int i = 2; i < m; ++i) r[i] = (m - (m / i) * r[m % i] % m) % m;
return r;
}
inline long long gcd(long long a, long long b) { return gcd(a, b, a, b); }
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long llrand() {
const long long lsbToFill = (sizeof(long long) << 3) - 1;
const long long bitsFilledInIteration = __builtin_popcountll(RAND_MAX);
long long number = 0;
for (long long lsbFilled = 0; lsbFilled <= lsbToFill;
lsbFilled += bitsFilledInIteration) {
number ^= (((long long)rand()) << lsbFilled);
}
return number & numeric_limits<long long>::max();
}
inline long long llrand(long long begin, long long end) {
return begin + llrand() % (end - begin);
}
struct Dinic {
struct Edge {
int u, v;
long long cap, flow;
Edge() {}
Edge(int u, int v, long long cap) : u(u), v(v), cap(cap), flow(0) {}
};
int N;
vector<Edge> E;
vector<vector<int>> g;
vector<int> d, pt;
Dinic(int N) : N(N), E(0), g(N), d(N), pt(N) {}
void AddEdge(int u, int v, long long cap) {
if (u != v) {
E.emplace_back(Edge(u, v, cap));
g[u].emplace_back(E.size() - 1);
E.emplace_back(Edge(v, u, 0));
g[v].emplace_back(E.size() - 1);
}
}
bool BFS(int S, int T) {
queue<int> q({S});
fill(d.begin(), d.end(), N + 1);
d[S] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == T) break;
for (int k : g[u]) {
Edge &e = E[k];
if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
d[e.v] = d[e.u] + 1;
q.emplace(e.v);
}
}
}
return d[T] != N + 1;
}
long long DFS(int u, int T, long long flow = -1) {
if (u == T || flow == 0) return flow;
for (int &i = pt[u]; i < g[u].size(); ++i) {
Edge &e = E[g[u][i]];
Edge &oe = E[g[u][i] ^ 1];
if (d[e.v] == d[e.u] + 1) {
long long amt = e.cap - e.flow;
if (flow != -1 && amt > flow) amt = flow;
if (long long pushed = DFS(e.v, T, amt)) {
e.flow += pushed;
oe.flow -= pushed;
return pushed;
}
}
}
return 0;
}
long long MaxFlow(int S, int T) {
long long total = 0;
while (BFS(S, T)) {
fill(pt.begin(), pt.end(), 0);
while (long long flow = DFS(S, T)) total += flow;
}
return total;
}
};
vector<long long> Dijkstra(const vector<list<pair<int, long long>>> &g, int s) {
vector<long long> d(int((g).size()), numeric_limits<long long>::max() / 2LL);
priority_queue<pair<long long, int>> q;
d[s] = 0;
q.emplace(-0, s);
while (!q.empty()) {
while (q.top().first > d[q.top().second]) {
q.pop();
}
int v = q.top().second;
q.pop();
for (const auto &cw : g[v]) {
if (d[v] + cw.second < d[cw.first]) {
d[cw.first] = d[v] + cw.second;
q.emplace(-d[cw.first], cw.first);
}
}
}
return d;
}
struct BinarySearchIterator
: public std::iterator<std::forward_iterator_tag, bool> {
long long value;
typename iterator_traits<BinarySearchIterator>::difference_type operator-(
const BinarySearchIterator &it) const {
return value - it.value;
}
BinarySearchIterator &operator++() {
++value;
return *this;
}
bool operator!=(const BinarySearchIterator &it) const {
return value != it.value;
}
bool operator*() const { return true; }
};
template <int ALPHA>
class AhoCorasick {
public:
static const int ILLEGAL_INDEX;
static const int ROOT;
struct Node {
bool leaf;
int parent;
int parentCharacter;
int link;
int next[ALPHA];
int go[ALPHA];
int outputFunction;
Node(int parent = ILLEGAL_INDEX, int parentCharacter = ALPHA)
: leaf(false),
parent(parent),
parentCharacter(parentCharacter),
link(ILLEGAL_INDEX),
outputFunction(ILLEGAL_INDEX) {
fill_n(next, ALPHA, ILLEGAL_INDEX);
fill_n(go, ALPHA, ILLEGAL_INDEX);
}
};
vector<Node> tree = vector<Node>(1);
AhoCorasick() {}
AhoCorasick(int maxStatesNumber) { tree.reserve(maxStatesNumber); }
template <class Iterator>
void add(int length, const Iterator begin) {
int vertex = ROOT;
for (int i = 0; i < length; ++i) {
if (ILLEGAL_INDEX == tree[vertex].next[begin[i]]) {
tree[vertex].next[begin[i]] = int((tree).size());
tree.push_back(Node(vertex, begin[i]));
}
vertex = tree[vertex].next[begin[i]];
}
tree[vertex].leaf = true;
}
int getLink(int vertex) {
assert(0 <= vertex && vertex < tree.size());
if (ILLEGAL_INDEX == tree[vertex].link) {
if (ROOT == vertex || ROOT == tree[vertex].parent) {
tree[vertex].link = ROOT;
} else {
tree[vertex].link =
go(getLink(tree[vertex].parent), tree[vertex].parentCharacter);
}
}
return tree[vertex].link;
}
int go(int vertex, int character) {
assert(0 <= character && character < ALPHA);
assert(0 <= vertex && vertex < tree.size());
if (ILLEGAL_INDEX == tree[vertex].go[character]) {
if (ILLEGAL_INDEX == tree[vertex].next[character]) {
tree[vertex].go[character] =
ROOT == vertex ? ROOT : go(getLink(vertex), character);
} else {
tree[vertex].go[character] = tree[vertex].next[character];
}
}
return tree[vertex].go[character];
}
int getOutputFunction(int vertex) {
assert(0 <= vertex && vertex < tree.size());
if (ILLEGAL_INDEX == tree[vertex].outputFunction) {
if (tree[vertex].leaf || ROOT == vertex) {
tree[vertex].outputFunction = vertex;
} else {
tree[vertex].outputFunction = getOutputFunction(getLink(vertex));
}
}
return tree[vertex].outputFunction;
}
};
template <int ALPHA>
const int AhoCorasick<ALPHA>::ILLEGAL_INDEX = -1;
template <int ALPHA>
const int AhoCorasick<ALPHA>::ROOT = 0;
int main(int argc, const char *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
srand((unsigned int)time(NULL));
int n;
while (cin >> n) {
vector<long long> s(n);
for (int i = (0); i < (n); ++i) {
cin >> s[i];
}
vector<long long> ps(n + 1, 0);
partial_sum((s).begin(), (s).end(), ps.begin() + 1);
int q;
cin >> q;
for (int i = (0); i < (q); ++i) {
int l, r;
cin >> l >> r;
--l;
--r;
cout << (ps[r + 1] - ps[l]) / 10 << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int64_t mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int casi;
cin >> casi;
while (casi--) {
int n, k;
cin >> n >> k;
int arr[n * k];
for (int i = 0; i < n * k; i++) cin >> arr[i];
int64_t sol = 0;
int tmp = n * k - (n + 2) / 2;
for (int i = 0; i < k; i++) sol += arr[tmp - ((n + 2) / 2) * i];
cout << sol << '\n';
}
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define ll long long
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define P pair<ll,ll>
#define ld long double
ll INF = (1LL << 60);
int mod = 1000000007;
ll H, W;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> H >> W;
// first -> 終点, second -> 始点
map<int, int>now_place;
multiset<int>diff;
rep(i, W) {
now_place[i] = i;
diff.insert(0);
}
rep(i, H) {
int A, B; cin >> A >> B; A--;
auto itr = now_place.lower_bound(A);
int ma = -1;
while (itr != now_place.end() && itr->first <= B) {
ma = max(ma, itr->second);
int x = itr->first - itr->second;
diff.erase(diff.find(x));
now_place.erase(itr++);
}
if (ma != -1 && B < W) {
diff.insert(B - ma);
now_place[B] = ma;
}
int ans = -1;
if (diff.size() > 0) {
ans = i + 1 + *diff.begin();
}
cout << ans << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
long long int a[n], sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sum = a[0] + n - 1;
;
for (int i = 1; i < n; i++) {
sum += (a[i] - 1) * (i + 1);
}
cout << sum;
}
| 1 |
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 2e5;
int n, m;
char col[maxn + 1];
vector<int> g[maxn * 4];
void Add(int u, int v) {
char a = col[u], b = col[v];
int t;
if (a == 'A') {
if (b == 'A') {
t = 0;
} else {
t = 1;
}
} else {
if (b == 'A') {
t = 3;
} else {
t = 2;
}
}
g[t * n + u].push_back((t + 1 & 3) * n + v);
}
int vis[maxn * 4];
void Dfs(int u) {
if (vis[u] == 1) {
puts("Yes");
exit(0);
}
vis[u] = 1;
for (int v : g[u]) {
if (vis[v] != 2) {
Dfs(v);
}
}
vis[u] = 2;
}
bool Main(void) {
for (int i = 0; i < 4 * n; ++i) {
if (vis[i] == 0) Dfs(i);
}
return false;
}
int main(void) {
scanf("%d%d", &n, &m);
scanf("%s", col);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
--u; --v;
Add(u, v);
Add(v, u);
}
puts(Main() ? "Yes" : "No");
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
void Draganov47(string click) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void submit() {
int n;
cin >> n;
vector<pair<int, int>> a(n);
multiset<int> x, y;
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
x.insert(a[i].first);
y.insert(a[i].second);
}
int best = 0;
for (int i = 0; i < n; i++) {
x.erase(x.lower_bound(a[i].first));
y.erase(y.lower_bound(a[i].second));
best = max(best, *y.begin() - *prev(x.end()));
x.insert(a[i].first);
y.insert(a[i].second);
}
cout << best;
}
int main() {
Draganov47("vk.com/gotovseros");
int T = 1;
for (int c = 1; c <= T; c++) {
submit();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
int n, x1, y1, x2, y2, sum = 0;
scanf("%d", &n);
while (n--) {
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
sum += (x2 - x1 + 1) * (y2 - y1 + 1);
}
printf("%d\n", sum);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int arr[127];
int main() {
string s;
int ans = 0;
cin >> s;
for (int i = 0, n = s.size(); i < n; i++) arr[(int)s[i]]++;
while (true) {
if (arr['n'] >= 3 && arr['e'] >= 3 && arr['t'] >= 1 && arr['i'] >= 1) {
ans++;
arr['n'] -= 2;
arr['e'] -= 3;
arr['t']--;
arr['i']--;
} else
break;
}
cout << ans;
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
bool passed[101][101];
vector<pair<int,int> > d;
int cnt=0;
// Eº©çÔÉèµÄ¢
void dfs(int x,int y,int left){
// cèª0ÉÈÁ½çJEg
if(left==0)
cnt++;
else{
// »ÝÌêðè
// à¤ÊßÏÝÈçAÖ
if(passed[y][x]){
int ny=y;
int nx=x-1;
if(nx<0){
ny--;nx=3;
if(ny<0)
return;
}
dfs(nx,ny,left);
}
else{
// ÊßÏÝÅÈ¢ÈçAܾÇÁū鯱ëð
// ÇÁµAÄA
passed[y][x]=true;
for(int i = 0; i < d.size(); i++){
int ny=d[i].first+y;
int nx=d[i].second+x;
// ÍÍàÅ é
if(ny>=0&&nx>=0&&ny<4&&nx<4&&!passed[ny][nx]){
// ÊßÏÝÉ·é
passed[ny][nx]=true;
// »ÝÊuÌÌêÉÚ®µAÄA
int nny=y;int nnx=x-1;
if(nnx<0){
nny--;nnx=3;
}
dfs(nnx,nny,left-2);
// ³Éß·
passed[ny][nx]=false;
}
}
passed[y][x]=false;
}
}
}
int main(){
while(1){
memset(passed,0,sizeof(passed));
d.clear();cnt=0;
for(int i = 0; i < 4; i++){
int x,y;
cin>>x;
if(x>4)
return 0;
cin>>y;
d.push_back(make_pair(-y,-x));
}
dfs(3,3,16);
cout<<cnt<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int main() {
ios::sync_with_stdio(false);
string s;
vector<int> op, f;
int n, oNeg = 0, oPos = 1;
op.push_back(1);
do {
cin >> s;
if (s == "+") {
oPos++;
op.push_back(1);
} else if (s == "-") {
oNeg++;
op.push_back(-1);
}
} while (s != "=");
cin >> n;
if (oPos * n - n < oNeg || oPos - n - oNeg * n > 0)
cout << "Impossible\n";
else {
cout << "Possible\n";
long long sPos, sNeg;
if (oNeg < oPos) {
sNeg = oNeg * n;
sPos = sNeg + n;
} else if (oNeg == oPos) {
sNeg = oNeg * n - n;
sPos = sNeg + n;
} else {
sPos = oPos * n;
sNeg = sPos - n;
}
for (int i = 0; i < op.size(); i++) {
if (op[i] == 1) {
if (sPos == oPos) {
f.push_back(1);
sPos--;
oPos--;
continue;
}
if (sPos - (oPos - 1) < n) {
f.push_back(sPos - (oPos - 1));
sPos = oPos - 1;
oPos--;
} else {
f.push_back(n);
sPos -= n;
oPos--;
}
} else {
if (sNeg == oNeg) {
f.push_back(1);
sNeg--;
oNeg--;
continue;
}
if (sNeg - (oNeg - 1) < n) {
f.push_back(sNeg - (oNeg - 1));
sNeg = oNeg - 1;
oNeg--;
} else {
f.push_back(n);
sNeg -= n;
oNeg--;
}
}
}
cout << f[0];
for (int i = 1; i < f.size(); i++)
cout << " " << (op[i] == 1 ? "+" : "-") << " " << f[i];
cout << " = " << n;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-8;
char str[1000010];
int main() {
scanf("%s", str + 1);
int len = (int)strlen(str + 1);
int ans = 0;
int M = 0, last = 0;
for (int i = 1; i <= len; i++) {
if (str[i] == 'M')
++M;
else if (M) {
last = max(M, last + 1);
ans = max(ans, last);
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
set <int> m;
int q;
cin>>q;
while(q--)
{
int a;
cin>>a;
if(a==0)
{
int x;
cin>>x;
m.emplace(x);
cout<<m.size()<<endl;
}
else if(a==1)
{
int x;
cin>>x;
cout<<m.count(x)<<endl;
}
else if(a==2)
{
int x;
cin>>x;
m.erase(x);
}
else
{
int l,r;
cin>>l>>r;
auto p=m.lower_bound(l);
while(p!=m.end()&&*p<=r)
{
cout<<*p<<endl;
p++;
}
}
}
return 0;
}
| 0 |
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = 110000;
const ll Mod = 1e9+7;
struct mat{ll a[3][3];mat(){memset(a,0,sizeof a);}}one,st,trans;
mat mul(mat &x,mat &y)
{
mat re;
for(int i=0;i<3;i++) for(int j=0;j<3;j++) for(int k=0;k<3;k++)
(re.a[i][k]+=x.a[i][j]*y.a[j][k]%Mod)%=Mod;
return re;
}
mat pw(mat x,int k)
{
mat re=one;
for(;k;k>>=1,x=mul(x,x)) if(k&1)
re=mul(re,x);
return re;
}
int n,m;
int main()
{
one.a[0][0]=one.a[1][1]=one.a[2][2]=1ll;
st.a[0][0]=1ll;
trans.a[0][0]=2ll,trans.a[0][1]=2ll,trans.a[0][2]=1ll;
trans.a[1][0]=1ll; trans.a[1][1]=1ll,trans.a[1][2]=1ll;
trans.a[2][0]=1ll,trans.a[2][1]=0ll,trans.a[2][2]=1ll;
scanf("%d%d",&m,&n); int las=0;
for(int i=1;i<=n;i++)
{
int p; scanf("%d",&p);
mat temp=pw(trans,p-las-1); las=p;
st=mul(st,temp);
trans.a[0][0]=1ll,trans.a[1][0]=0ll,trans.a[2][0]=0;
st=mul(st,trans);
trans.a[0][0]=2ll,trans.a[1][0]=1ll,trans.a[2][0]=1ll;
}
mat temp=pw(trans,m-las);
st=mul(st,temp);
printf("%lld\n",st.a[0][2]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
int a[N];
int getSum(int l, int r) { return a[r] - (l > 0 ? a[l - 1] : 0); }
int main() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n; i++) {
a[i] = s[i] == 'I' || s[i] == 'E' || s[i] == 'A' || s[i] == 'O' ||
s[i] == 'U' || s[i] == 'Y';
if (i > 0) a[i] += a[i - 1];
}
double sum = a[n - 1];
double ans = sum;
for (int i = 2; i <= (n + 1) / 2; i++) {
double t = getSum(i - 1, n - i);
sum += t;
ans += sum / i;
}
for (int i = (n + 1) / 2 + 1; i <= n; i++) {
double t = getSum(n - i + 1, i - 2);
sum -= t;
ans += sum / i;
}
cout << fixed << setprecision(6) << ans;
return 0;
}
| 5 |
#include<cmath>
#include<cstdio>
#define N 3003
using namespace std;
int a[N],i,j,m,n;double b,c,d,e,s,t,x,y;const double p=acos(-1);
int main()
{
for(scanf("%d%d",&n,&m),i=1;i<=n;i++)scanf("%d",a+i);
for(i=1;i<=n;i++)for(j=i+1,s=t=0;j<=n;j++)b=cos(p*(a[i]+a[j])/m+p),c=sin(p*(a[i]+a[j])/m+p),d=cos(2*p*a[i]/m)-b,e=sin(2*p*a[i]/m)-c,x+=(j-i-1)*b+d*s-e*t,y+=(j-i-1)*c+d*t+e*s,s+=cos(p*(a[j]-a[i])/m),t+=sin(p*(a[j]-a[i])/m);
return 0*printf("%.15lf %.15lf\n",6*x/(n*(n-1ll)*(n-2)),6*y/(n*(n-1ll)*(n-2)));
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
using namespace std;
const long double eps = 1e-7;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 100010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
if (n == m && m == 1) return cout << 0 << '\n', 0;
if (n == 1) {
for (int i = 1; i <= m; i++) cout << i + 1 << ' ';
cout << '\n';
return 0;
}
if (m == 1) {
for (int i = 1; i <= n; i++) cout << i + 1 << '\n';
return 0;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cout << i * (j + n) << " \n"[j == m];
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int)1e9 + 23111992;
const long long LINF = (long long)1e18 + 23111992;
const long double PI = acos((long double)-1);
const long double EPS = 1e-9;
inline long long gcd(long long a, long long b) {
long long r;
while (b) {
r = a % b;
a = b;
b = r;
}
return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long fpow(long long n, long long k, int p = MOD) {
long long r = 1;
for (; k; k >>= 1) {
if (k & 1) r = r * n % p;
n = n * n % p;
}
return r;
}
template <class T>
inline int chkmin(T& a, const T& val) {
return val < a ? a = val, 1 : 0;
}
template <class T>
inline int chkmax(T& a, const T& val) {
return a < val ? a = val, 1 : 0;
}
inline unsigned long long isqrt(unsigned long long k) {
unsigned long long r = sqrt(k) + 1;
while (r * r > k) r--;
return r;
}
inline long long icbrt(long long k) {
long long r = cbrt(k) + 1;
while (r * r * r > k) r--;
return r;
}
inline void addmod(int& a, int val, int p = MOD) {
if ((a = (a + val)) >= p) a -= p;
}
inline void submod(int& a, int val, int p = MOD) {
if ((a = (a - val)) < 0) a += p;
}
inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }
inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }
inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; }
inline int sign(long double x, long double y) { return sign(x - y); }
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int myrand() { return abs((int)mt()); }
const int maxn = 300 + 5;
int n, m;
string a[maxn][maxn];
string b[maxn][maxn];
vector<pair<pair<int, int>, pair<int, int> > > ff() {
static queue<char> c[maxn][maxn];
for (int i = (0); i < (n); ++i) {
for (int j = (0); j < (m); ++j) {
while (int((c[i][j]).size())) c[i][j].pop();
string t = a[i][j];
reverse((t).begin(), (t).end());
for (char e : t) {
c[i][j].push(e);
}
}
}
vector<pair<pair<int, int>, pair<int, int> > > moves;
int p = int((c[0][0]).size());
int q = int((c[0][m - 1]).size());
for (int i = (0); i < (p); ++i) {
char e = c[0][0].front();
c[0][0].pop();
if (e == '0') {
c[n - 1][0].push(e);
moves.push_back(make_pair(make_pair(0, 0), make_pair(n - 1, 0)));
} else {
c[0][m - 1].push(e);
moves.push_back(make_pair(make_pair(0, 0), make_pair(0, m - 1)));
}
}
for (int i = (0); i < (q); ++i) {
char e = c[0][m - 1].front();
c[0][m - 1].pop();
if (e == '1') {
c[n - 1][m - 1].push(e);
moves.push_back(make_pair(make_pair(0, m - 1), make_pair(n - 1, m - 1)));
} else {
c[0][0].push(e);
moves.push_back(make_pair(make_pair(0, m - 1), make_pair(0, 0)));
}
}
for (int i = (1); i < (n); ++i) {
int p = int((c[i][0]).size());
int q = int((c[i][m - 1]).size());
for (int j = (0); j < (p); ++j) {
char e = c[i][0].front();
c[i][0].pop();
if (e == '0') {
c[0][0].push(e);
moves.push_back(make_pair(make_pair(i, 0), make_pair(0, 0)));
} else {
c[i][m - 1].push(e);
moves.push_back(make_pair(make_pair(i, 0), make_pair(i, m - 1)));
}
}
for (int j = (0); j < (q); ++j) {
char e = c[i][m - 1].front();
c[i][m - 1].pop();
if (e == '1') {
c[0][m - 1].push(e);
moves.push_back(make_pair(make_pair(i, m - 1), make_pair(0, m - 1)));
} else {
c[i][0].push(e);
moves.push_back(make_pair(make_pair(i, m - 1), make_pair(i, 0)));
}
}
}
for (int i = (0); i < (n); ++i)
for (int j = (0); j < (m); ++j) {
if (make_pair(i, j) != make_pair(0, 0) &&
make_pair(i, j) != make_pair(0, m - 1)) {
while (int((c[i][j]).size())) {
char e = c[i][j].front();
c[i][j].pop();
if (e == '0') {
if (!i || !j) {
moves.push_back(make_pair(make_pair(i, j), make_pair(0, 0)));
} else {
moves.push_back(make_pair(make_pair(i, j), make_pair(i, 0)));
moves.push_back(make_pair(make_pair(i, 0), make_pair(0, 0)));
}
} else {
if (!i || j == m - 1) {
moves.push_back(make_pair(make_pair(i, j), make_pair(0, m - 1)));
} else {
moves.push_back(make_pair(make_pair(i, j), make_pair(i, m - 1)));
moves.push_back(
make_pair(make_pair(i, m - 1), make_pair(0, m - 1)));
}
}
}
}
}
return moves;
}
void chemthan() {
deque<int> dq;
cin >> n >> m;
for (int i = (0); i < (n); ++i)
for (int j = (0); j < (m); ++j) cin >> a[i][j];
vector<pair<pair<int, int>, pair<int, int> > > res = ff();
for (int i = (0); i < (n); ++i)
for (int j = (0); j < (m); ++j)
cin >> a[i][j], reverse((a[i][j]).begin(), (a[i][j]).end());
vector<pair<pair<int, int>, pair<int, int> > > res2 = ff();
cout << int((res).size()) + int((res2).size()) << "\n";
for (auto it : res) {
cout << it.first.first + 1 << " " << it.first.second + 1 << " "
<< it.second.first + 1 << " " << it.second.second + 1 << "\n";
}
reverse((res2).begin(), (res2).end());
for (auto it : res2) {
cout << it.second.first + 1 << " " << it.second.second + 1 << " "
<< it.first.first + 1 << " " << it.first.second + 1 << "\n";
}
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, t = 0, counter = 0, p1 = 0, p2 = 0;
cin >> N;
int Array[N];
int a = 0, b = N - 1;
for (int i = 0; i < N; i++) {
cin >> Array[i];
}
while (a <= b) {
if (Array[a] >= Array[b]) {
t = Array[a];
a++;
} else {
t = Array[b];
b--;
}
if (counter % 2 == 0) {
p1 += t;
} else {
p2 += t;
}
counter++;
}
cout << p1 << " " << p2 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using ld = long double;
using pii = pair<int, int>;
const ll M = 982451653;
void solve() {
int n, q;
cin >> n >> q;
vector<vi> start(n), end(n);
for (int i = 0; i < q; i++) {
int l, r, x;
cin >> l >> r >> x;
start[l - 1].push_back(x);
end[r - 1].push_back(x);
}
vector<ll> dp(n + 1);
dp[0] = 1;
vi can(n + 1);
for (int i = 0; i < n; i++) {
for (int x : start[i])
for (int j = n; j >= x; j--) dp[j] = (dp[j] + dp[j - x]) % M;
for (int j = 0; j < n + 1; j++) can[j] |= dp[j] != 0;
for (int x : end[i])
for (int j = x; j < n + 1; j++) dp[j] = (dp[j] - dp[j - x] + M) % M;
}
vi ans;
for (int i = 1; i < n + 1; i++)
if (can[i]) ans.push_back(i);
cout << ans.size() << '\n';
for (int i : ans) cout << i << " ";
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, r, count = 1;
cin >> k >> r;
while (true) {
if ((k * count) % 10 == r || (k * count) % 10 == 0) {
cout << count;
return 0;
}
count++;
}
}
| 1 |
#include<iostream>
using namespace std;
int S,W;
int main(){
cin >> S >> W;
if(S <= W)
cout << "unsafe";
else
cout << "safe";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
while (true) {
a %= b;
if (a == 0) return b;
b %= a;
if (b == 0) return a;
}
}
bool isB(long long n) {
n = abs(n);
while (n > 0) {
if (n % 10 == 8) return true;
n /= 10;
}
return false;
}
bool isPrime(int n) {
if (n == 1) return false;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return false;
return true;
}
long long cntt, cnt = 1, cnt1, cnti, cnt11 = 1, cnt22, t, mi1 = 10000000,
ma1 = -9999;
long long ans, ans2, state, d, mi = 100000000, ma = -9999, sum, sum1, i, j, jj,
jjj, ii;
map<int, int> mapp;
pair<long long, long long> pa;
long long a[200];
long long b[201];
int c[200];
double x, y, x2, y2, x3, y3, r, ras;
int main() {
cin >> r >> x >> y >> x2 >> y2;
ras = (sqrt((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y)));
if (ras == 0) {
cout << 0;
} else {
ans = ceil(ras / (2 * r));
cout << ans;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
return a.first + a.second > b.first + b.second;
}
void solve() {
long long n, r;
cin >> n >> r;
vector<pair<long long, long long>> pos;
vector<pair<long long, long long>> neg;
while (n--) {
long long a, b;
cin >> a >> b;
if (b > 0) {
pos.push_back({a, b});
} else {
neg.push_back(make_pair(a, b));
}
}
sort(pos.begin(), pos.end());
for (auto c : pos) {
if (r >= c.first) {
r += c.second;
} else {
cout << "NO"
<< "\n";
exit(0);
}
}
sort(neg.begin(), neg.end(), cmp);
for (auto c : neg) {
if (r >= c.first) {
r += c.second;
} else {
cout << "NO"
<< "\n";
exit(0);
}
if (r < 0) {
cout << "NO"
<< "\n";
exit(0);
}
}
cout << "YES"
<< "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int n, m, k;
long long cut;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
if (!b)
d = a, x = 1, y = 0;
else
exgcd(b, a % b, d, y, x), y -= x * (a / b);
}
long long cal(long long c) {
long long a, b, d, x, y;
a = 2LL * n, b = 2LL * m;
exgcd(a, b, d, x, y);
if (c % d != 0) return -1;
x *= c / d, y *= c / d;
a /= d, b /= d;
if (x < 0 || y > 0) {
long long t = max((-x) / b, y / a);
x += t * b, y -= t * a;
if (x < 0 || y > 0) x += b, y -= a;
}
if (x >= 0 && y <= 0) {
long long t = min(x / b, (-y) / a);
x -= t * b, y -= t * a;
}
return x;
}
long long solve(int x, int y) {
long long temp, res = INF, tres;
temp = cal(1LL * y - x);
tres = 2LL * n * temp + x;
if (temp != -1 && tres <= cut) res = min(res, tres);
temp = cal(2LL * m - x - y);
tres = 2LL * n * temp + x;
if (temp != -1 && tres <= cut) res = min(res, tres);
temp = cal(1LL * x + y - 2 * n);
tres = 2LL * n * temp + 2 * n - x;
if (temp != -1 && tres <= cut) res = min(res, tres);
temp = cal(1LL * x - y + 2 * m - 2 * n);
tres = 2LL * n * temp + 2 * n - x;
if (temp != -1 && tres <= cut) res = min(res, tres);
return res == INF ? -1LL : res;
}
int main() {
scanf("%d%d%d", &n, &m, &k);
cut = 1LL * n * m / gcd(1LL * n, 1LL * m);
for (int i = 0; i < k; i++) {
int x, y;
scanf("%d%d", &x, &y);
printf("%I64d\n", solve(x, y));
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int maxn = 100000;
int n, dep[maxn + 10], a[maxn + 10], ans = 1;
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; ++i) {
int x;
scanf("%d", &x);
a[dep[i] = dep[x] + 1] ^= 1;
}
for (int i = 1; i <= n; ++i) ans += a[i];
printf("%d", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
long long int arr[n];
set<long long int> s;
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) {
s.insert(arr[i]);
}
if (s.size() == 1)
cout << n << "\n";
else
cout << 1 << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
out << "[";
for (auto vi : v) out << vi << ", ";
out << "]\n";
return out;
}
int DX[] = {0, +1, 0, -1, 0};
int DY[] = {+1, 0, -1, 0, 0};
const int MAX_N = 500 + 3;
vector<int> c;
queue<int> q;
priority_queue<int, std::vector<int>, std::greater<int> > pq;
string s;
int n, m, k;
char _f[MAX_N][MAX_N][MAX_N];
bool f(int sum1, int sum2, int i) {
if (sum1 < 0 || sum2 < 0) return false;
if (i == n) {
return sum1 == 0 && sum2 == 0;
}
auto& _fi = _f[sum1][sum2][i];
if (_fi == -1) {
_fi = f(sum1 - c[i], sum2, i + 1) || f(sum1, sum2 - c[i], i + 1) ||
f(sum1, sum2, i + 1);
}
return _fi;
}
bool canGet(int sum1, int sum2) { return f(sum1, sum2, 0); }
vector<bool> used(MAX_N, false);
void solve() {
vector<int> answer;
memset(_f, -1, sizeof _f);
for (int sum = 0; sum + sum <= k; sum++) {
if (canGet(sum, k - sum)) {
used[sum] = used[k - sum] = true;
}
}
for (int sum = 0; sum <= k; sum++) {
if (used[sum]) {
answer.push_back(sum);
}
}
cout << answer.size() << "\n";
for (int sum : answer) {
cout << sum << " ";
}
}
void readData() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &m);
c.push_back(m);
}
}
int main() {
ios::sync_with_stdio(false);
readData();
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 5;
int sum[N << 2], ans[N << 2], trans[N << 2], lmin[N << 2], rmin[N << 2],
lmax[N << 2], rmax[N << 2], pre[N << 2], nxt[N << 2];
void pushup(int p) {
sum[p] = sum[p << 1] + sum[p << 1 | 1];
lmin[p] = min(lmin[p << 1], sum[p << 1] + lmin[p << 1 | 1]);
rmin[p] = min(rmin[p << 1 | 1], sum[p << 1 | 1] + rmin[p << 1]);
lmax[p] = max(lmax[p << 1], sum[p << 1] + lmax[p << 1 | 1]);
rmax[p] = max(rmax[p << 1 | 1], sum[p << 1 | 1] + rmax[p << 1]);
pre[p] = max(pre[p << 1], max(ans[p << 1] + lmax[p << 1 | 1],
pre[p << 1 | 1] - sum[p << 1]));
nxt[p] = max(nxt[p << 1 | 1], max(ans[p << 1 | 1] - rmin[p << 1],
sum[p << 1 | 1] + nxt[p << 1]));
ans[p] = max(ans[p << 1] + sum[p << 1 | 1], ans[p << 1 | 1] - sum[p << 1]);
trans[p] =
max(max(trans[p << 1], trans[p << 1 | 1]),
max(pre[p << 1 | 1] - rmin[p << 1], nxt[p << 1] + lmax[p << 1 | 1]));
}
char s[N];
void build(int p, int l, int r) {
if (l == r) {
int v = s[l - 1] == '(' ? 1 : -1;
sum[p] = v;
lmin[p] = rmin[p] = min(v, 0);
lmax[p] = rmax[p] = max(v, 0);
pre[p] = nxt[p] = ans[p] = trans[p] = 1;
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}
void upt(int p, int l, int r, int pos, int v) {
if (l == r) {
sum[p] = v;
lmin[p] = rmin[p] = min(v, 0);
lmax[p] = rmax[p] = max(v, 0);
pre[p] = nxt[p] = ans[p] = trans[p] = 1;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid)
upt(p << 1, l, mid, pos, v);
else
upt(p << 1 | 1, mid + 1, r, pos, v);
pushup(p);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
cin >> s;
n = (n - 1) << 1;
build(1, 1, n);
int o1, o2, v1, v2;
printf("%d\n", trans[1]);
while (m--) {
scanf("%d%d", &o1, &o2);
if (o1 != o2) swap(s[o1 - 1], s[o2 - 1]);
v1 = s[o1 - 1] == '(' ? 1 : -1;
v2 = s[o2 - 1] == '(' ? 1 : -1;
upt(1, 1, n, o1, v1);
upt(1, 1, n, o2, v2);
printf("%d\n", trans[1]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
const int N = 3e5 + 5;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
const int INFi = 0x7f7f7f7f;
long long test = 1, n, m, vis[N], rec[N], cyc[N], p[N], sz[N];
vector<long long> adj[N];
stack<long long> Stack;
void make(long long n) {
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
}
int find(long long x) { return (p[x] == x) ? x : p[x] = find(p[x]); }
void merge(long long x, long long y) {
long long a = find(x);
long long b = find(y);
if (a != b) {
if (sz[a] >= sz[b]) swap(a, b);
p[a] = b;
sz[b] += sz[a];
}
}
void TSUtil(int v) {
vis[v] = 1;
for (auto it : adj[v])
if (!vis[it]) TSUtil(it);
Stack.push(v);
}
void TS() {
for (int i = 1; i <= n; i++)
if (!vis[i]) TSUtil(i);
while (Stack.empty() == 0) {
cout << Stack.top() << " ";
Stack.pop();
}
}
bool CycleUtil(int v) {
if (vis[v] == 0) {
vis[v] = 1, rec[v] = 1;
for (auto it : adj[v]) {
if (!vis[it] && CycleUtil(it))
return 1;
else if (rec[it])
return 1;
}
}
rec[v] = 0;
return 0;
}
void Cycle() {
for (int i = 1; i <= n; i++)
if (CycleUtil(i)) cyc[find(i)] = 1;
}
void solve() {
cin >> n >> m;
make(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
merge(u, v);
}
Cycle();
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (find(i) == i) {
if (cyc[i] == 1)
ans += sz[i];
else
ans += sz[i] - 1;
}
}
cout << ans << " "
<< "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
for (int i = 1; i <= test; i++) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int ct = 0;
while (n > 0 && m > 0) {
if (n >= m) {
m++;
n -= 2;
} else {
m -= 2;
n++;
}
if (n < 0 || m < 0) break;
ct++;
}
cout << ct << endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
string str;
int n;
ll dp[4005][4005];
int main(){
cin>>str;
n=str.size();
dp[0][0]=1;
ll cnt=0;
for(int i=0;i<n*2;i++){
if(i<n)cnt+=2;
for(int j=0;j<=4000;j++){
if(dp[i][j]==0)continue;
ll R=j;
if(i<n){
if(str[i]=='0'){
R+=2;
}else if(str[i]=='1'){
R++;
}
}
ll B=cnt-R;
assert(B>=0);
if(R>0){
int nj=j;
if(R==j+2)nj++;
if(R==j)nj--;
assert(nj>=0);
dp[i+1][nj]+=dp[i][j];
dp[i+1][nj]%=mod;
}
if(B>0){
int nj=j;
if(R==j+2)nj+=2;
if(R==j+1)nj++;
dp[i+1][nj]+=dp[i][j];
dp[i+1][nj]%=mod;
}
}
cnt--;
}
/*
for(int i=0;i<=n*2;i++){
for(int j=0;j<=n*2;j++){
printf("%02d ",(int)dp[i][j]);
}
cout<<endl;
}
*/
cout<<dp[2*n][0]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, k, path[N], dist[N];
vector<int> v[N], r[N];
void getdist(int src) {
for (int i = 1; i <= n; i++) dist[i] = 1e9;
queue<pair<int, int> > q;
q.push(make_pair(0, src));
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (dist[p.second] > p.first) {
dist[p.second] = p.first;
for (auto i : r[p.second]) {
q.push(make_pair(p.first + 1, i));
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
r[b].push_back(a);
}
cin >> k;
for (int i = 1; i <= k; i++) cin >> path[i];
getdist(path[k]);
int minans = 0, maxans = 0;
for (int i = 2; i < k; i++) {
int prev = path[i - 1];
int d = dist[prev];
int count = 0;
for (auto j : v[prev]) {
if (dist[j] + 1 == dist[prev]) count++;
}
if (dist[path[i]] != (dist[prev] - 1)) {
minans++;
maxans++;
} else {
if (count > 1) {
maxans++;
}
}
}
cout << minans << " " << maxans;
}
| 4 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX 10001
#define INF (1<<29)
int main()
{
int Tc,N;
string s;
cin >> Tc;
while(Tc--)
{
cin >> N >> s;
bool b[2][MAX] = {{}};
for(int i = 0 ; i <= N ; i++)
{
if(i != 0 && s[2*i-1] == 'Y') b[0][i] = 1;
if(i != N && s[2*i] == 'Y') b[0][i] = 1;
if(i != 0 && s[2*i-1+2*N] == 'Y') b[1][i] = 1;
if(i != N && s[2*i+2*N] == 'Y') b[1][i] = 1;
}
auto solve = [=]()
{
vector<vector<int>> dp(2, vector<int>(MAX, INF));
if(b[0][0] && b[1][0])
{
dp[1][0] = 2;
}
if(b[0][0] && !b[1][0])
{
dp[0][0] = 1;
dp[1][0] = 1;
}
if(!b[0][0] && b[1][0])
{
dp[1][0] = 2;
}
if(!b[0][0] && !b[1][0])
{
dp[0][0] = 0;
dp[1][0] = 1;
}
for(int i = 1 ; i <= N ; i++)
{
if(b[0][i] && b[1][i])
{
dp[0][i] = min(dp[0][i],min(dp[0][i-1]+4,dp[1][i-1]+3));
dp[1][i] = min(dp[1][i],min(dp[0][i-1]+3,dp[1][i-1]+3));
}
if(b[0][i] && !b[1][i])
{
dp[0][i] = min(dp[0][i],min(dp[0][i-1]+2,dp[1][i-1]+2));
dp[1][i] = min(dp[1][i],min(dp[0][i-1]+2,dp[1][i-1]+2));
}
if(!b[0][i] && b[1][i])
{
dp[0][i] = min(dp[0][i],min(dp[0][i-1]+4,dp[1][i-1]+3));
dp[1][i] = min(dp[1][i],min(dp[0][i-1]+3,dp[1][i-1]+2));
}
if(!b[0][i] && !b[1][i])
{
dp[0][i] = min(dp[0][i],min(dp[0][i-1]+1,dp[1][i-1]+2));
dp[1][i] = min(dp[1][i],min(dp[0][i-1]+2,dp[1][i-1]+1));
}
}
return dp[0][N];
};
cout << solve() << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> t(n + 1);
vector<long long> ff(n + 1);
vector<long long> bb(n + 1);
for (int i = 1; i <= n; i++) {
cin >> t[i];
}
if (n == 1) {
cout << t[1] << endl;
return 0;
}
ff[1] = t[1];
ff[2] = t[2];
bb[n] = t[n];
bb[n - 1] = t[n - 1];
for (int i = 3; i <= n; i++) {
ff[i] = t[i] + ff[i - 2];
}
for (int i = n - 2; i >= 1; i--) {
bb[i] = t[i] + bb[i + 2];
}
long long ans = bb[1];
for (int i = 2; i <= n; i++) {
ans = max(ans, bb[i] + ff[i - 1]);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;
using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(ll x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(ld x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.f, ", ", x.s, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void print() { pr("\n"); }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
print(ts...);
}
} // namespace output
using namespace output;
template <class T>
struct Seg {
const T ID = {1e9, 1e9};
T combine(T a, T b) { return min(a, b); }
int n;
vector<T> seg;
void init(int _n) {
n = _n;
seg.assign(2 * n, ID);
}
void update(int p, T value) {
seg[p += n] = value;
for (p /= 2; p; p /= 2) seg[p] = combine(seg[2 * p], seg[2 * p + 1]);
}
T query(int l, int r) {
T ra = ID, rb = ID;
for (l += n, r += n + 1; l < r; l /= 2, r /= 2) {
if (l & 1) ra = combine(ra, seg[l++]);
if (r & 1) rb = combine(seg[--r], rb);
}
return combine(ra, rb);
}
};
struct LCA {
vector<int> first;
vector<int> vertex_name, vertex_depth;
Seg<pii> seg;
int N;
LCA(vector<vector<int>>& graph, int root = 0) {
N = graph.size();
first.resize(N);
dfs(graph, root, -1, 0);
seg.init(vertex_name.size());
for (int i = (0); i < (vertex_name.size()); ++i)
seg.update(i, {vertex_depth[i], vertex_name[i]});
}
void dfs(vector<vector<int>>& graph, int cur, int par, int depth) {
first[cur] = vertex_name.size();
vertex_name.push_back(cur);
vertex_depth.push_back(depth);
for (int n : graph[cur])
if (n != par) {
dfs(graph, n, cur, depth + 1);
vertex_name.push_back(cur);
vertex_depth.push_back(depth);
}
}
int lca(int v1, int v2) {
int l = first[v1];
int r = first[v2];
if (l > r) swap(l, r);
return seg.query(l, r).second;
}
};
vector<vi> graph;
int main() {
int N, M;
cin >> N >> M;
vi origPlacement(N);
for (int i = (0); i < (N); ++i) {
int x;
cin >> x;
--x;
origPlacement[i] = x;
}
graph.resize(M);
vi curNode(N);
vi creationTime(M);
for (int i = (0); i < (N); ++i) curNode[i] = i;
int newNode = M;
for (int i = (0); i < (M - 1); ++i) {
int a, b;
cin >> a >> b;
--a;
--b;
graph.push_back({});
creationTime.push_back(i + 1);
graph[newNode].push_back(curNode[a]);
graph[newNode].push_back(curNode[b]);
graph[curNode[a]].push_back(newNode);
graph[curNode[b]].push_back(newNode);
curNode[a] = newNode;
++newNode;
}
LCA lca = LCA(graph, newNode - 1);
vi adds;
for (int i = (0); i < (N - 1); ++i) {
adds.push_back(
creationTime[lca.lca(origPlacement[i], origPlacement[i + 1])]);
}
sort(adds.begin(), adds.end());
int ans = N - 1;
int j = 0;
for (int i = (0); i < (M); ++i) {
while (j < N - 1 && adds[j] == i) {
--ans;
++j;
}
print(ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4e5 + 5;
int stable[MAXN + 5], father[MAXN + 5], vis[MAXN + 5];
struct XXX {
int x, sum;
} c[MAXN + 5];
int find(int x) {
if (father[x] == x) return x;
return father[x] = find(father[x]);
}
int cmp(XXX a, XXX b) { return a.sum > b.sum; }
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 1; i <= k; i++) {
int x;
scanf("%d", &x);
vis[x] = 1;
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
if (find(u) != find(v)) father[find(u)] = find(v);
}
for (int i = 1; i <= n; i++)
if (vis[i] == 1) vis[find(i)] = 1;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int x = find(i);
if (vis[x] == 1) {
if (stable[x] == 0)
stable[x] = 1, c[x].x = x, c[x].sum = 1, cnt++;
else
c[x].x = x, c[x].sum++;
}
}
stable_sort(c + 1, c + 1 + 10000, cmp);
int sum = 0;
for (int i = 1; i <= n; i++)
if (vis[find(i)] == 0) sum++;
c[1].sum += sum;
int ans = 0;
for (int i = 1; i <= cnt; i++) {
int a = c[i].sum;
ans += a * (a - 1) / 2;
}
printf("%d\n", ans - m);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 30, Mod = 1e9 + 7;
const long long SQ = 330;
set<long long> G[N];
set<pair<long long, long long> > second;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
long long n, q;
cin >> n >> q;
long long res = 0;
long long ans = 0;
for (int i = 0; i < q; i++) {
long long t, x;
cin >> t >> x;
if (t == 1) {
G[x].insert(++res);
second.insert({res, x});
}
if (t == 2) {
for (auto u : G[x]) second.erase({u, x});
G[x].clear();
}
if (t == 3) {
vector<pair<long long, long long> > vec;
for (auto it = second.begin(); it != second.end(); it++) {
auto f = *it;
if (f.first > x) break;
vec.push_back(f);
G[f.second].erase(f.first);
}
for (auto u : vec) second.erase(u);
}
cout << second.size() << '\n';
}
return (0);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 9;
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int> > guys;
set<pair<int, int> > alive;
map<pair<int, int>, int> id, in;
map<pair<int, int>, set<pair<int, int> > > adj, back_adj;
map<int, pair<int, int> > back;
for (int i = int(0); i < int(n); i++) {
int x, y;
scanf("%d %d", &x, &y);
guys.emplace_back(x, y);
alive.emplace(x, y);
id[make_pair(x, y)] = i;
back[i] = make_pair(x, y);
}
for (auto &each : guys) {
int x, y;
tie(x, y) = each;
if (y > 0) {
if (alive.count(make_pair(x - 1, y - 1))) {
adj[make_pair(x, y)].emplace(x - 1, y - 1);
back_adj[make_pair(x - 1, y - 1)].emplace(x, y);
in[make_pair(x - 1, y - 1)]++;
}
if (alive.count(make_pair(x, y - 1))) {
adj[make_pair(x, y)].emplace(x, y - 1);
back_adj[make_pair(x, y - 1)].emplace(x, y);
in[make_pair(x, y - 1)]++;
}
if (alive.count(make_pair(x + 1, y - 1))) {
adj[make_pair(x, y)].emplace(x + 1, y - 1);
back_adj[make_pair(x + 1, y - 1)].emplace(x, y);
in[make_pair(x + 1, y - 1)]++;
}
}
}
set<int> order;
for (int i = int(0); i < int(n); i++) {
int x, y;
tie(x, y) = back[i];
if (in[make_pair(x, y)] == 0) {
order.insert(i);
}
pair<int, int> p = make_pair(x, y);
bool good = true;
for (auto &each_back : back_adj[p]) {
good &= adj[each_back].size() > 1;
}
if (good) {
order.insert(i);
}
}
set<int> rolled;
int player = 0;
vector<int> vals;
while (!order.empty()) {
int val;
if (player == 0) {
val = *order.rbegin();
order.erase(prev(order.end()));
} else {
val = *order.begin();
order.erase(order.begin());
}
for (auto &each_back : back_adj[back[val]]) {
adj[each_back].erase(back[val]);
if (adj[each_back].size() == 1) {
order.erase(id[*adj[each_back].begin()]);
}
}
vals.push_back(val);
rolled.insert(val);
int x, y;
tie(x, y) = back[val];
for (auto &each : adj[make_pair(x, y)]) {
in[each]--;
auto p = make_pair(x, y);
back_adj[each].erase(p);
bool good = true;
for (auto &each_back : back_adj[each]) {
good &= adj[each_back].size() > 1;
}
if ((good || in[each] == 0) && !rolled.count(id[each])) {
order.insert(id[each]);
}
}
player = !player;
}
int ans = 0;
int mult = 1;
for (int i = int(vals.size() - 1); i >= int(0); i--) {
ans = (ans + ((1LL * mult * vals[i]) % MOD)) % MOD;
mult = (1LL * mult * n) % MOD;
}
printf("%d\n", ans);
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;typedef vector<int> v;int h,w,k;v x(v a,v b){return a.size()>b.size()?a:a.size()<b.size()?b:max(a,b);}int main(){while(cin>>w>>h&&h){v d[75][75],s;r(i,h)r(j,w){char z;cin>>z;z-='0';if(!(z/10)&&(z||d[i][j].size())){d[i][j].push_back(z);d[i+1][j]=x(d[i+1][j],d[i][j]);d[i][j+1]=x(d[i][j+1],d[i][j]);}s=x(s,d[i][j]);}r(i,s.size())cout<<s[i];cout<<endl;}return 0;} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < n; j++) {
if (s[i] == s[j] && j < i) {
flag = 1;
break;
}
}
if (flag == 1)
cout << "YES\n";
else
cout << "NO\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int l = n;
long long sum = 0;
for (int i = 1; i <= n / 2; i++) {
sum = sum + ((i + l) % (n + 1)) + ((i + l + 1) % (n + 1));
l--;
if (i == n / 2) sum -= (i + l + 1 + 1) % (n + 1);
}
if (n == 1)
cout << "0";
else if (n % 2 == 0)
cout << sum;
else
cout << sum + 1;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool setmax(T &_a, T _b) {
if (_b > _a) {
_a = _b;
return true;
}
return false;
}
template <class T>
bool setmin(T &_a, T _b) {
if (_b < _a) {
_a = _b;
return true;
}
return false;
}
template <class T>
T gcd(T _a, T _b) {
return _b == 0 ? _a : gcd(_b, _a % _b);
}
const int MAXN = 110, MAXM = 110;
const int MAXNum = 3030;
int a[MAXN], xi[MAXM], xj[MAXM];
int n, m;
int vals[MAXNum], st[MAXN], en[MAXN];
int num;
vector<int> es[MAXNum];
int lnk[MAXNum];
bool vis[MAXNum];
bool find(int x) {
for (int y : es[x])
if (!vis[y]) {
vis[y] = true;
if (lnk[y] == 0 || find(lnk[y])) {
lnk[y] = x;
return true;
}
}
return false;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = int(1); i <= int(n); ++i) scanf("%d", &a[i]);
for (int j = int(1); j <= int(m); ++j) scanf("%d%d", &xi[j], &xj[j]);
num = 0;
for (int i = int(1); i <= int(n); ++i) {
int x = a[i];
st[i] = num + 1;
for (int y = 2; y * y <= x; ++y)
while (x % y == 0) {
vals[++num] = y;
x /= y;
}
if (x > 1) vals[++num] = x;
en[i] = num;
}
for (int i = int(1); i <= int(num); ++i) es[i].clear();
for (int k = int(1); k <= int(m); ++k) {
int i = xi[k], j = xj[k];
if (i % 2 != 1) swap(i, j);
for (int x = int(st[i]); x <= int(en[i]); ++x)
for (int y = int(st[j]); y <= int(en[j]); ++y)
if (vals[x] == vals[y]) es[x].push_back(y);
}
memset(lnk, 0, sizeof(lnk));
int ans = 0;
for (int i = int(1); i <= int(n); ++i)
if (i % 2 == 1)
for (int x = int(st[i]); x <= int(en[i]); ++x) {
memset(vis, false, sizeof(vis));
if (find(x)) ++ans;
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
priority_queue < int > pq;
int main() {
int n,m;
cin>>n>>m;
long long cost=0;
for(int i=0;i<n;i++) {
int a;
cin>>a;
pq.push(a);
}
while(m--) {
int a=pq.top();
pq.pop();
pq.push(a/2);
}
for(int i=0;i<n;i++) {
cost+=pq.top();
pq.pop();
}
cout<<cost<<endl;
return 0;
} | 0 |
#include<iostream>
#include<cassert>
using namespace std;
#define N 8
#define FREE -1
#define NOT_FREE 1
int row[N],col[N],dpos[2*N-1],dneg[2*N-1];
bool X[N][N];
void initialize()
{
int i;
for(i=0;i<N;i++)
{
row[i]=FREE,col[i]=FREE;
}
for(i=0;i<N;i++)
{
dpos[i]=FREE,dneg[i]=FREE;
}
}
void printBoard()
{
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(X[i][j])
{
if(row[i]!=j) return;
}
}
}
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cout<<((row[i]==j)?"Q":".");
}
cout<<endl;
}
}
void recursive(int i)
{
int j;
if(i==N)
{
printBoard();return;
}
for(j=0;j<N;j++)
{
if(NOT_FREE==col[j] || NOT_FREE == dpos[i+j] || NOT_FREE == dneg[i-j+N-1])
{
continue;
}
row[i]=j;
col[j]=dpos[i+j]=dneg[i-j+N-1]=NOT_FREE;
recursive(i+1);
row[i]=col[j]=dpos[i+j]=dneg[i-j+N-1]=FREE;
}
}
int main()
{
initialize();
int i,j,k,r,c;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
X[i][j]=false;
}
}
cin>>k;
for(i=0;i<k;i++)
{
cin>>r>>c;
X[r][c]=true;
}
recursive(0);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long min(long long a, long long b) {
if (a < b) return a;
return b;
}
long long max(long long a, long long b) {
if (a > b) return a;
return b;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
return gcd(b, a % b);
}
long long power(long long x, long long y) {
x = x % 1000000007;
if (x == 1) return 1;
if (y == 0) return 1;
if (y == 1) return x;
long long temp = power(x, y >> 1);
if (y & 1) {
return ((((temp * temp) % 1000000007) * x) % 1000000007);
} else
return ((temp * temp) % 1000000007);
}
void factorial(long long n, vector<long long> &fact) {
fact[0] = 1;
fact[1] = 1;
for (long long i = 2; i <= n; i++)
fact[i] = (fact[i - 1] * (i % 1000000007)) % 1000000007;
return;
}
long long inv(long long n) { return power(n, 1000000007 - 2); }
long long ncr(long long n, long long r, vector<long long> &fact,
vector<long long> &invfact) {
if (r > n || r < 0 || n < 0) return 0;
return ((((fact[n] * invfact[n - r]) % 1000000007) * invfact[r]) %
1000000007);
}
void find_primes(long long n, vector<long long> &primes) {
bool isprime[n + 1];
for (long long i = 0; i <= n; i++) isprime[i] = 1;
isprime[0] = 0;
isprime[1] = 0;
for (long long i = 2; i <= n; i++) {
if (isprime[i]) {
for (long long j = i * i; j <= n; j += i) {
isprime[j] = 0;
}
}
}
for (long long i = 0; i <= n; i++) {
if (isprime[i]) {
primes.push_back(i);
}
}
return;
}
void find_fact1(long long n, vector<long long> &primes,
vector<unordered_map<long long, long long> > &primfact) {
vector<long long> cur(n + 1);
for (long long i = 0; i <= n; i++) {
cur[i] = i;
}
for (auto x : primes) {
for (long long i = x; i <= n; i += x) {
long long cnt = 0;
while (cur[i] % x == 0) {
cur[i] /= x;
cnt++;
}
primfact[i][x] = cnt;
}
}
return;
}
void find_fact2(long long n, vector<long long> &primes, vector<long long> &sp) {
for (long long i = 0; i <= n; i++) {
sp[i] = i;
}
for (auto x : primes) {
for (long long i = x * 2; i <= n; i += x) {
sp[i] = min(sp[i], x);
}
}
return;
}
void bfs(long long u, vector<vector<long long> > &adj,
vector<long long> &dist) {
long long n = adj.size() - 1;
vector<bool> vis(n + 1, 0);
vis[u] = 1;
dist[u] = 0;
queue<long long> q;
q.push(u);
long long level = 1;
while (!q.empty()) {
long long k = q.size();
for (long long i = 0; i < k; i++) {
long long x = q.front();
q.pop();
for (auto y : adj[x]) {
if (!vis[y]) {
vis[y] = 1;
dist[y] = level;
q.push(y);
}
}
}
level++;
}
return;
}
void solve() {
long long n, k;
cin >> n >> k;
long long l1, r1;
cin >> l1 >> r1;
long long l2, r2;
cin >> l2 >> r2;
if (l2 < l1) {
swap(l1, l2);
swap(r1, r2);
}
long long ans = 9223372036854775807;
long long cur = 0;
long long len = 0;
if (l2 > r1) {
len = 0;
} else if (r2 > r1) {
len = r1 - l2;
} else
len = r2 - l2;
long long area = len * n;
if (area >= k) {
cout << 0 << "\n";
return;
}
for (long long i = 1; i <= n; i++) {
if (area >= k) break;
if (l2 >= r1) {
cur += (l2 - r1);
cur += (min(r2 - l1, k - area));
area += min(r2 - l1, k - area);
if (area >= k) {
ans = min(ans, cur);
break;
}
ans = min(ans, cur + (k - area) * 2);
} else if (r2 >= r1) {
cur += (min(l2 - l1 + r2 - r1, k - area));
area += min(l2 - l1 + r2 - r1, k - area);
if (area >= k) {
ans = min(ans, cur);
break;
}
ans = min(ans, cur + (k - area) * 2);
} else {
cur += (min(l2 - l1 + r1 - r2, k - area));
area += min(l2 - l1 + r1 - r2, k - area);
if (area >= k) {
ans = min(ans, cur);
break;
}
ans = min(ans, cur + (k - area) * 2);
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) solve();
}
| 4 |
#include <bits/stdc++.h>
const int MaxN = 1000000 + 5;
int N, K, L;
int A[MaxN];
std::pair<int, int> F[MaxN];
void init() {
scanf("%d %d %d", &N, &K, &L);
static char s[MaxN];
scanf("%s", s + 1);
for (int i = 1; i <= N; ++i)
if (s[i] >= 'a' && s[i] <= 'z') A[i] = 1;
}
inline bool check(int mid) {
F[0] = std::make_pair(0, 0);
for (int i = 1; i <= N; ++i) {
F[i] = F[i - 1];
F[i].first += A[i];
std::pair<int, int> p = F[std::max(0, i - L)];
p.first += mid, p.second++;
F[i] = std::min(F[i], p);
}
if (F[N].second <= K)
return true;
else
return false;
}
int getMin() {
int lbound = 0, rbound = N + 1, ans = 0;
while (lbound <= rbound) {
int mid = (lbound + rbound) >> 1;
if (check(mid) == false) {
ans = mid + 1;
lbound = mid + 1;
} else
rbound = mid - 1;
}
check(ans);
return F[N].first - ans * K;
}
void solve() {
int res = getMin();
for (int i = 1; i <= N; ++i) A[i] ^= 1;
res = std::min(res, getMin());
printf("%d\n", res);
}
int main() {
init();
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
long long dp[101][101][101];
long long choose[101][101];
long long fac[101];
int main() {
int n, m, x;
long long MOD;
cin >> n >> m >> x >> MOD;
if (x > (n + 3) / 2) {
cout << 0 << "\n";
return 0;
}
fac[0] = 1LL;
for (int k = 0; k <= 100; k++) {
dp[0][0][k] = 1;
choose[0][k] = 0;
choose[k][0] = 1;
if (k > 0) fac[k] = fac[k - 1] * 1LL * k % MOD;
}
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= 100; j++) {
choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];
choose[i][j] %= MOD;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= min((i + 3) / 2, x); j++) {
bool ge = false;
for (int k = 0; k <= m; k++) {
if (k == 0) {
if (j == 0) {
dp[i][j][k] = 1;
}
continue;
}
if (k == 1) {
if (j == 1) {
dp[i][j][k] = fac[i];
}
continue;
}
if (k > i) {
if (j == 0) {
dp[i][j][k] = fac[i];
}
continue;
}
for (int pos = 1; pos <= i; pos++) {
for (int y = 0; y <= j; y++) {
if (y <= (pos - 1 + 3) / 2 and (j - y) <= (i - pos + 3) / 2) {
dp[i][j][k] +=
(dp[pos - 1][y][k - 1] * dp[i - pos][j - y][k - 1]) % MOD *
choose[i - 1][pos - 1] % MOD;
dp[i][j][k] %= MOD;
}
}
}
if (dp[i][j][k] != 0 and not ge) {
ge = true;
}
if (ge and dp[i][j][k] == 0 and MOD >= 20000) {
break;
}
}
}
}
if (dp[n][x][m] < 0) dp[n][x][m] += MOD;
cout << dp[n][x][m] << "\n";
}
| 2 |
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;
int main()
{
for(;;){
string s;
cin >> s;
if(s == ".")
return 0;
int n = s.size();
string ans;
stack<int> stk;
int d = 0;
for(int i=0; i<n; ++i){
switch(s[i]){
case '+':
++ d;
break;
case '-':
-- d;
break;
case '[':
stk.push(ans.size());
break;
case ']':
reverse(ans.begin() + stk.top(), ans.end());
stk.pop();
break;
case '?':
ans += 'A';
d = 0;
break;
default:
char c = ((s[i] - 'A' + d) % 26 + 26) % 26 + 'A';
ans += c;
d = 0;
break;
}
}
cout << ans << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct Person {
string name;
int pizza, taxi, another;
};
bool isPizza(string num) {
char ch = num[0];
for (int i = 1; i < num.size(); i++) {
if (num[i] == '-')
continue;
else if (ch - num[i] <= 0)
return false;
else
ch = num[i];
}
return true;
}
bool isTaxi(string num) {
char ch = num[0];
for (int i = 1; i < num.size(); i++) {
if (num[i] == '-')
continue;
else if (num[i] != ch)
return false;
}
return true;
}
void output(string ss, vector<string> v) {
cout << ss;
for (int i = 0; i < v.size(); i++) {
cout << v[i];
if (i + 1 == v.size())
cout << ".\n";
else
cout << ", ";
}
}
int main(void) {
vector<Person> v;
int n, m, maxp, maxt, maxa;
cin >> n;
maxp = maxt = maxa = -1;
for (int i = 0; i < n; i++) {
Person p;
cin >> m >> p.name;
p.pizza = p.taxi = p.another = 0;
string num;
for (int j = 0; j < m; j++) {
cin >> num;
if (isPizza(num))
p.pizza++;
else if (isTaxi(num))
p.taxi++;
else
p.another++;
}
maxp = max(p.pizza, maxp);
maxt = max(p.taxi, maxt);
maxa = max(p.another, maxa);
v.push_back(p);
}
vector<string> outP, outT, outA;
for (int i = 0; i < v.size(); i++) {
if (v[i].pizza == maxp) outP.push_back(v[i].name);
if (v[i].taxi == maxt) outT.push_back(v[i].name);
if (v[i].another == maxa) outA.push_back(v[i].name);
}
output("If you want to call a taxi, you should call: ", outT);
output("If you want to order a pizza, you should call: ", outP);
output("If you want to go to a cafe with a wonderful girl, you should call: ",
outA);
}
| 2 |
#include <bits/stdc++.h>
//#include <math.h>
using namespace std;
#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;
struct edge {
int to, cost;
edge(int t,int c):to(t),cost(c) {}
};
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
//-----------------------------------------------------------------------------
int X,A[100000],R[100000],T[100000];
int K,Q;
int dir=-1,l,u,m;
int t,dif,j;
void calc(int tmp) {
t+=abs(tmp),dif+=tmp;
l=max(0,min(X,l+tmp));
u=max(0,min(X,u+tmp));
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin>>X>>K;
REP(i,K) cin>>R[i];
cin>>Q;
REP(i,Q) cin>>T[i]>>A[i];
u=X;
REP(i,Q) {
while(j<K&&R[j]<T[i]) calc((R[j++]-t)*dir),dir=-dir;
calc((T[i]-t)*dir);
cout<<max(l,min(u,A[i]+dif))<<endl;
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int k,x;
scanf("%d%d",&k,&x);
for(int i=x-k+1;i<=x+k-1;++i)printf("%d ",i);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int Q,N,s,m,i;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> Q;
for (;Q--;){
cin >> N;
for(i=0;N>10;++i){
s=10,m=0;
while(s<N){
m=max(m,(N/s)*(N%s));
s*=10;
}
N=m;
}
cout << i << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char mat[1002][1002];
int mincost[1002][1002];
deque<pair<int, int> > d;
int xa, ya, xb, yb;
bool viz[1002][1002];
int ox[] = {-1, 0, 1, 0};
int oy[] = {0, 1, 0, -1};
bool gut(int L, int C) {
if (!L || L > n) return 0;
if (!C || C > m) return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> k;
for (int i = 1; i <= n; ++i) cin >> mat[i] + 1;
cin >> xa >> ya >> xb >> yb;
viz[xa][ya] = 1;
d.push_back({xa, ya});
while (!d.empty() && !viz[xb][yb]) {
int L = d[0].first;
int C = d[0].second;
d.pop_front();
for (int i = 0; i <= 3; ++i)
for (int j = 1; gut(L + j * ox[i], C + j * oy[i]) && j <= k; ++j)
if (!viz[L + j * ox[i]][C + j * oy[i]] &&
mat[L + j * ox[i]][C + j * oy[i]] != '#') {
mincost[L + j * ox[i]][C + j * oy[i]] = mincost[L][C] + 1;
viz[L + j * ox[i]][C + j * oy[i]] = 1;
d.push_back({L + j * ox[i], C + j * oy[i]});
} else if (mat[L + j * ox[i]][C + j * oy[i]] == '#')
break;
}
if (!viz[xb][yb])
cout << -1;
else
cout << mincost[xb][yb] << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace {
const int N = 250005;
int n, deg[N], lim, vis[N];
long long dp[N][2];
vector<pair<int, int> > G[N];
vector<int> buckvis[N], buckdel[N];
struct Dat {
multiset<long long> st;
long long sum;
Dat() { sum = 0; }
void insert(long long x) {
st.insert(x);
sum += x;
}
void erase(long long x) {
st.erase(st.find(x));
sum -= x;
}
long long erase_max() {
long long x = *--st.end();
st.erase(--st.end());
sum -= x;
return x;
}
int size() { return st.size(); }
} tr[N];
void dfs(int u) {
vis[u] = lim;
int cur = deg[u] - lim;
while (tr[u].size() > cur) tr[u].erase_max();
vector<long long> ch, vd;
long long sum = 0;
for (auto &e : G[u]) {
int v = e.first;
if (deg[v] <= lim) break;
if (vis[v] == lim) continue;
dfs(v);
dp[v][0] = min(dp[v][0], dp[v][1] + e.second);
long long val = dp[v][1] + e.second - dp[v][0];
ch.push_back(val);
tr[u].insert(val);
sum += dp[v][0];
}
while (tr[u].size() > cur) vd.push_back(tr[u].erase_max());
dp[u][0] = sum + tr[u].sum;
while (tr[u].size() > cur - 1) vd.push_back(tr[u].erase_max());
dp[u][1] = sum + tr[u].sum;
for (auto &val : vd) tr[u].insert(val);
for (auto &val : ch) tr[u].erase(val);
}
} // namespace
vector<long long> minimum_closure_costs(int _n, vector<int> U, vector<int> V,
vector<int> W) {
n = _n;
long long SW = 0;
for (int i = 0; i < n - 1; i++) {
G[U[i]].emplace_back(V[i], W[i]);
G[V[i]].emplace_back(U[i], W[i]);
deg[U[i]]++, deg[V[i]]++;
SW += W[i];
}
for (int i = 1; i <= n; i++) {
sort(G[i].begin(), G[i].end(), [](pair<int, int> a, pair<int, int> b) {
return deg[a.first] > deg[b.first];
});
buckdel[deg[i]].push_back(i);
for (int j = 1; j < deg[i]; j++) buckvis[j].push_back(i);
}
vector<long long> ans;
ans.push_back(SW);
for (lim = 1; lim < n; lim++) {
for (int u : buckdel[lim]) {
for (auto &e : G[u]) {
int v = e.first;
if (deg[v] <= deg[u]) break;
tr[v].insert(e.second);
}
}
long long sum = 0;
for (int u : buckvis[lim])
if (vis[u] != lim) {
dfs(u);
sum += dp[u][0];
}
ans.push_back(sum);
}
return ans;
}
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> U(N - 1), V(N - 1), W(N - 1);
for (int i = 0; i < N - 1; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
std::vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);
for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%lld", closure_costs[i]);
}
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main() {
int n, i;
scanf("%d", &n);
int ans = 0x3f3f3f3f;
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + 1 + n);
for (i = 1; i + n / 2 <= n; i++) {
ans = min(ans, a[i + n / 2] - a[i]);
}
printf("%d\n", ans);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int T, q, i, x;
long long p, ans;
inline void upd(int x) {
long long p = ::p;
int q = ::q;
for (; p % q == 0; p /= x)
;
ans = max(ans, p);
}
inline void factor(int x) {
for (i = 2; i * i <= x; ++i)
if (x % i == 0)
for (upd(i); x % i == 0; x /= i)
;
if (x > 1) upd(x);
}
int main() {
for (scanf("%d", &T); T--;) {
scanf("%lld%d", &p, &q);
if (p % q)
ans = p;
else
ans = 0, factor(q);
printf("%lld\n", ans);
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& s, vector<T>& v) {
s << '{';
for (int i = 0; i < v.size(); ++i) s << (i ? "," : "") << v[i];
return s << '}';
}
template <typename S, typename T>
ostream& operator<<(ostream& s, pair<S, T>& p) {
return s << "(" << p.first << "," << p.second << ")";
}
void swap(long long* a, long long* b) {
int t = *a;
*a = *b;
*b = t;
}
long long C, Hr, Hb, Wr, Wb;
int main() {
cin >> C >> Hr >> Hb >> Wr >> Wb;
long long ir = C / Wr;
long long best = 0;
int times = 0;
while (true) {
long long ib = (C - ir * Wr) / Wb;
if (ir < 0) break;
if (ib < 0) break;
long long obj = ir * Hr + ib * Hb;
if (obj > best) best = obj;
if (times == 1000000) break;
ir--;
times++;
}
times = 0;
long long ib = C / Wb;
while (true) {
long long ir = (C - ib * Wb) / Wr;
if (ir < 0) break;
if (ib < 0) break;
long long obj = ir * Hr + ib * Hb;
if (obj > best) best = obj;
if (times == 1000000) break;
ib--;
times++;
}
cout << best << endl;
}
| 3 |
// template version 1.14
using namespace std;
#include <bits/stdc++.h>
// varibable settings
#define int long long
const int INF=1e18;
const int MOD=1e9+7;
// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(a, b, c) make_tuple(a, b, c)
#define ceil(a,b) ((a)+(b)-1)/(b)
#define is_in(x, a, b) ((a)<=(x) && (x)<(b))
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;
#if defined(PCM) || defined(LOCAL)
#include "lib/dump.hpp"
#else
#define dump(...) 42
#define dump_1d(...) 42
#define dump_2d(...) 42
#endif
//}}}
bool is_prime(int n) {/*{{{*/
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return n != 1;
}/*}}}*/
signed main() {
int n;cin>>n;
set<int> d;
set<int> d2;
for(int i=1; i*i<=n; i++){
if (n%i==0){
d.insert(i);
if (is_prime(n/i)) d2.insert(i);
d.insert(n/i);
if (is_prime(n/(n/i))) d2.insert(n/i);
}
}
d.erase(n);
d2.erase(n);
dump(d);
dump(d2);
int M = sz(d);
int m = sz(d2);
cout << m << " " << M << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int M, F, B;
cin >> M >> F >> B;
if(M + F < B) {
cout << "NA" << endl;
} else {
cout << max(0, B - M) << endl;
}
return (0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
register long long ret = 0;
while (!(ch >= '0' && ch <= '9')) ch = getchar();
while (ch >= '0' && ch <= '9')
ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
return ret;
}
long long n, k, m;
long long yu[11][1010];
long long yu2[1010];
long long dp[1010][200][2];
long long dodp(long long pos, long long nowsum, long long flag) {
if (pos == n + 1) return flag;
if (dp[pos][nowsum][flag] != -1) return dp[pos][nowsum][flag];
long long lim = pos == n ? 1 : 0;
long long ans = 0;
for (register long long i = lim; i <= 9; i++) {
long long sb = (nowsum + i * yu2[pos] % k) % k;
ans += dodp(pos + 1, sb, (flag || (sb == 0 && i > 0)));
ans %= m;
}
dp[pos][nowsum][flag] = ans % m;
return ans;
}
signed main() {
scanf("%lld%lld%lld", &n, &k, &m);
memset(dp, -1, sizeof dp);
yu2[1] = 1;
for (register long long i = 2; i <= 1001; i++) {
yu2[i] = yu2[i - 1] * 10 % k;
}
printf("%lld\n", dodp(1, 0, false));
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n, p1, p2, p3, t1, t2;
cin >> n >> p1 >> p2 >> p3 >> t1 >> t2;
long long ans = 0, p = -1;
for (int i = 0; i < n; i++) {
long long l, r;
cin >> l >> r;
if (i > 0) {
long long mn = min(t1, l - p);
ans += (mn * p1);
p += mn;
mn = min(t2, l - p);
ans += (mn * p2);
p += mn;
mn = (l - p);
ans += (mn * p3);
}
ans += ((r - l) * p1);
p = r;
}
cout << ans << endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(void){
int x,a,b;
cin>>x>>a>>b;
if(-a+b<=0)
cout<<"delicious"<<endl;
else if(-a+b<=x)
cout<<"safe"<<endl;
else
cout<<"dangerous"<<endl;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimizer("O3")
#pragma GCC target("sse4")
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> a(n);
for (long long int i = 0; i < n; i++) cin >> a[i];
if ((a[0] + a[1]) <= a[n - 1]) {
cout << 1 << " " << 2 << " " << n << endl;
} else
cout << "-1\n";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int h = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '<') {
if (s[i + 1] == '/') h--;
for (int i = 0; i < 2 * h; i++) cout << " ";
if (s[i + 1] == '/')
cout << s.substr(i, 4) << endl;
else {
cout << s.substr(i, 3) << endl;
h++;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
#define debug(a) cout << #a ": " << a << endl
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int a[N], b[N], pre[N], suf[N], len[N][2];
vector<int> up, down;
int get_l(vector<int> &v, int x){
int p = lower_bound(v.begin(), v.end(), x) - v.begin();
if (!p) return x;
return v[p-1];
}
int get_r(vector<int> &v, int x){
if (v.size() == 0) return x;
int p = lower_bound(v.begin(), v.end(), x) - v.begin();
if (p == v.size()) return x;
return v[p];
}
int main(void){
IOS;
int n; cin >> n;
for (int i = 1; i <= n; ++i){
cin >> a[i];
}
b[1] = 0;
a[n+1] = a[n-1];
for (int i = 2; i <= n; ++i){
if (a[i] > a[i-1]){
b[i] = 1;
if (b[i-1] != 1) b[i-1] = 0;
}else{
b[i] = -1;
if (b[i-1] != -1) b[i-1] = 0;
}
}
b[n] = 0;
//printf("\n");
for (int i = 1; i <= n; ++i){
//printf("%d ", b[i]);
if (!b[i]){
if (a[i+1] < a[i]) up.push_back(i);
else down.push_back(i);
}
}
for (int i = 0; i < down.size(); ++i){
int l = get_l(up, down[i]);
int r = get_r(up, down[i]);
len[i][0] = down[i] - l;
len[i][1] = r - down[i];
if (i > 0) pre[i] = pre[i-1];
pre[i] = max(pre[i], max(len[i][0], len[i][1]));
}
for (int i = down.size()-1; i >= 0; --i){
if (i != down.size()-1) suf[i] = suf[i+1];
suf[i] = max(suf[i], max(len[i][0], len[i][1]));
}
int ans = 0;
for (int i = 0; i < up.size(); ++i){
int l = get_l(down, up[i]);
int r = get_r(down, up[i]);
if (l == up[i] || r == up[i]) continue;
int k = max(up[i] - l, r - up[i]);
int x = max(up[i] - l, r - up[i]), y = min(up[i] - l, r - up[i]);
if (k & 1) continue;
l = lower_bound(down.begin(), down.end(), l) - down.begin();
r = lower_bound(down.begin(), down.end(), r) - down.begin();
if ((l-1 >= 0 && pre[l-1] >= k) || (r+1 < down.size() && suf[r+1] >= k) || len[l][0] >= k || len[r][1] >= k) continue;
if (x-1 >= y) continue;
ans++;
}
cout << ans << endl;
/*for (int x : up) cout << x << ' '; cout << endl;
for (int x : down) cout << x << ' '; cout << endl;*/
return 0;
} | 2 |
#include"bits/stdc++.h"
#include<cassert>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
const long long inf = 1ll << 62;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int b[100006], l[100006], u[100006];
int dif(int n, int x) {
return max((x - b[n])*l[n], (x - b[n])*u[n]);
}
signed main() {
int n, x; cin >> n >> x;
rep(i, n) {
cin >> b[i] >> l[i] >> u[i];
}
int alt = -1, aut = n*x + 1;
while (aut - alt > 1) {
int mi = (aut + alt) / 2;
int score = 0;
int maxscore;
vector<P>R;
rep(i, n) {
R.push_back(P((x - b[i])*u[i] + l[i] * b[i], i));
score -= l[i] * b[i];
}
sort(R.begin(), R.end());
reverse(R.begin(), R.end());
rep(i, mi / x) {
P p = R[i];
score += p.first;
}
maxscore = score;
int m = mi%x;
for (int i = mi / x; i < n; i++) {
int now = R[i].second;
int ms = dif(now,m) + l[now] * b[now];
maxscore = max(maxscore, score + ms);
}
if (mi != n*x) {
score += R[mi / x].first;
rep(i, mi / x) {
int ns = score - R[i].first;
int now = R[i].second;
int ms = dif(now,m) + l[now] * b[now];
maxscore = max(maxscore, ns + ms);
}
}
if (maxscore >= 0)aut = mi;
else alt = mi;
}
cout << aut << endl;
}
| 0 |
Subsets and Splits