solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int maxn = 2e5;
const int INF = 0x7fffffff;
const int mod = 1e9 + 7;
const double eps = 1e-7;
const double Pi = acos(-1.0);
inline int read_int() {
char c;
int ret = 0, sgn = 1;
do {
c = getchar();
} while ((c < '0' || c > '9') && c != '-');
if (c == '-')
sgn = -1;
else
ret = c - '0';
while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
return sgn * ret;
}
inline long long read_ll() {
char c;
long long ret = 0, sgn = 1;
do {
c = getchar();
} while ((c < '0' || c > '9') && c != '-');
if (c == '-')
sgn = -1;
else
ret = c - '0';
while ((c = getchar()) >= '0' && c <= '9') ret = ret * 10 + (c - '0');
return sgn * ret;
}
inline long long quick_pow(long long base, long long index, long long p) {
long long res = 1;
while (index) {
if (index & 1) res = res * base % p;
base = base * base % p;
index >>= 1;
}
return res;
}
int n, k;
int dist[maxn + 5];
int prev(int x) { return x == 0 ? n - 1 : x - 1; }
int next(int x) { return x == n - 1 ? 0 : x + 1; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
vector<int> a(n);
string s;
cin >> s;
for (int i = 0; i < n; i++) {
a[i] = s[i] == 'W';
}
memset(dist, -1, sizeof dist);
queue<int> q;
for (int i = 0; i < n; i++) {
if (i == 0) {
if (a[n - 1] == a[i] || a[i + 1] == a[i]) dist[i] = 0;
} else if (i == n - 1) {
if (a[i - 1] == a[i] || a[0] == a[i]) dist[i] = 0;
} else {
if (a[i - 1] == a[i] || a[i + 1] == a[i]) dist[i] = 0;
}
if (dist[i] == 0) {
q.push(i);
}
}
if (q.empty()) {
for (int i = 0; i < n; i++) {
if (k & 1) {
cout << (s[i] == 'W' ? 'B' : 'W');
} else {
cout << s[i];
}
}
cout << '\n';
return 0;
}
while (!q.empty()) {
int f = q.front();
q.pop();
if (dist[prev(f)] == -1) {
dist[prev(f)] = dist[f] + 1;
q.push(prev(f));
}
if (dist[next(f)] == -1) {
dist[next(f)] = dist[f] + 1;
q.push(next(f));
}
}
for (int i = 0; i < n; i++) {
int K = min(k, dist[i]);
if (K & 1) {
cout << (s[i] == 'W' ? 'B' : 'W');
} else {
cout << s[i];
}
}
cout << '\n';
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
inline void prep() {
cin.tie(0);
cin.sync_with_stdio(0);
};
long long mod = 1e9 + 6;
const int biggg = 1e9 + 1;
const long long bigggest = 1e18;
const double eps = 1e-10;
const double pi = acos(-1.);
long long mymod(long long num) {
if (num < 0) {
if (mod == 1) {
return 0;
}
return (mod - abs(num) % mod) % mod;
} else {
return num % mod;
}
}
vector<vector<long long> > mult2(vector<vector<long long> > frst,
vector<vector<long long> > scnd) {
vector<vector<long long> > res;
for (int i = 0; i < frst.size(); i++) {
vector<long long> row;
long long sum = 0;
for (int k = 0; k < frst[i].size(); k++) {
sum += mymod(frst[i][k] * scnd[k][0]);
sum = mymod(sum);
}
row.push_back(sum);
res.push_back(row);
}
return res;
}
vector<vector<long long> > mult(vector<vector<long long> > frst,
vector<vector<long long> > scnd) {
vector<vector<long long> > res;
for (int i = 0; i < frst.size(); i++) {
vector<long long> row;
for (int j = 0; j < scnd.size(); j++) {
long long sum = 0;
for (int k = 0; k < frst[i].size(); k++) {
sum += mymod(frst[i][k] * scnd[k][j]);
sum = mymod(sum);
}
row.push_back(sum);
}
res.push_back(row);
}
return res;
}
vector<vector<long long> > identity;
vector<vector<long long> > matpow(vector<vector<long long> > x, long long n) {
if (n == 0) return identity;
vector<vector<long long> > u = matpow(x, n / 2);
u = (mult(u, u));
if (n % 2 == 1) u = mult(u, x);
return u;
}
long long modpow(long long x, long long n) {
if (n == 0) return 1LL % mod;
long long u = modpow(x, n / 2LL);
u = (long long)(u * u) % mod;
if (n % 2LL == 1) u = (long long)(u * x) % mod;
return u;
}
int main() {
prep();
long long n, f, ff, fff, c;
cin >> n >> f >> ff >> fff >> c;
vector<vector<long long> > mat = {{1, 1, 1}, {1, 0, 0}, {0, 1, 0}};
identity = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
n -= 3;
vector<vector<long long> > f1 = {{0}, {0}, {1}};
vector<vector<long long> > f2 = {{0}, {1}, {0}};
vector<vector<long long> > f3 = {{1}, {0}, {0}};
vector<vector<long long> > fmat = matpow(mat, n);
vector<vector<long long> > f1mult = mult2(fmat, f1);
vector<vector<long long> > f2mult = mult2(fmat, f2);
vector<vector<long long> > f3mult = mult2(fmat, f3);
mat = {{1, 1, 1, 1, 0},
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 0, 1, 2},
{0, 0, 0, 0, 1}};
identity = {{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 1}};
vector<vector<long long> > cmat = matpow(mat, n);
vector<vector<long long> > cc = {{0}, {0}, {0}, {2}, {1}};
vector<vector<long long> > cmult = mult2(cmat, cc);
mod++;
cout << (((((modpow(f, f1mult[0][0]) * modpow(ff, f2mult[0][0])) % mod) *
modpow(fff, f3mult[0][0])) %
mod) *
modpow(c, cmult[0][0])) %
mod;
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
int n, a, b;
int t[5005][2];
int dp[5005][2];
vector<pair<int, int> > p;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d", &a, &b);
p.push_back({a, b});
}
sort(p.begin(), p.end());
for (int i = 0; i < n; ++i) t[i][0] = p[i].first, t[i][1] = p[i].second;
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < 2; ++j) {
dp[i][j] = inf;
for (int k = 0; k < 2; ++k) {
if (i == n - 1) {
if (t[i - 1][j] <= t[i][k]) dp[i][j] = min(dp[i][j], t[i][k]);
} else {
if (t[i - 1][j] <= t[i][k]) dp[i][j] = min(dp[i][j], dp[i + 1][k]);
}
}
}
}
if (n == 1) {
printf("%d\n", min(t[0][0], t[0][1]));
exit(0);
}
printf("%d\n", min(dp[1][0], dp[1][1]));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
int max_r;
int b[n];
fill(b, b + n, 0);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
max_r = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
if (max_r < arr[i]) {
max_r = arr[i];
} else
b[i] = max_r - arr[i] + 1;
}
for (int i = 0; i < n; i++) cout << b[i] << " ";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, big, ans, a[MAXN], b[MAXN];
bool flag;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
do {
flag = false;
big = 0;
int tmp = 0;
for (int i = 0; i <= 30; ++i)
for (int j = 1; j <= n; ++j)
if ((1 << i) & a[j]) big = (1 << i);
for (int i = 1; i <= n; ++i)
if (a[i] & big) ++tmp;
if (tmp == n) {
for (int i = 1; i <= n; ++i) a[i] -= big;
flag = true;
}
} while (flag);
b[++b[0]] = 0;
for (int i = 1; i <= n; ++i)
if (a[i] & big) b[++b[0]] = i;
b[++b[0]] = n + 1;
for (int i = 2; i < b[0]; ++i) {
int tmp = 0;
for (int j = b[i] - 1; j > b[i - 1]; --j) {
tmp = max(tmp, a[j]);
ans = max(ans, tmp ^ a[b[i]]);
}
tmp = 0;
for (int j = b[i] + 1; j < b[i + 1]; ++j) {
tmp = max(tmp, a[j]);
ans = max(ans, tmp ^ a[b[i]]);
}
}
printf("%d\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long sm[55][55][55][55] = {};
long long dp[55][55][55][55] = {};
int32_t main() {
long long n;
cin >> n;
string g[55];
for (long long i = 0; i < n; ++i) cin >> g[i];
for (long long i = n - 1; i >= 0; --i) {
for (long long j = n - 1; j >= 0; --j) {
for (long long ii = i; ii < n; ++ii) {
for (long long jj = j; jj < n; ++jj) {
sm[i][j][ii][jj] = (g[i][j] == '#') + sm[i + 1][j][ii][jj] +
sm[i][j + 1][ii][jj] - sm[i + 1][j + 1][ii][jj];
}
}
}
}
for (long long i = n - 1; i >= 0; --i) {
for (long long j = n - 1; j >= 0; --j) {
for (long long ii = i; ii < n; ++ii) {
for (long long jj = j; jj < n; ++jj) {
long long &f = dp[i][j][ii][jj];
f = max(jj - j + 1, ii - i + 1);
for (long long k = i; k <= ii; ++k) {
if (!sm[k][j][k][jj])
f = min(f, dp[i][j][k - 1][jj] + dp[k + 1][j][ii][jj]);
}
for (long long k = j; k <= jj; ++k) {
if (!sm[i][k][ii][k])
f = min(f, dp[i][j][ii][k - 1] + dp[i][k + 1][ii][jj]);
}
}
}
}
}
cout << dp[0][0][n - 1][n - 1];
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count());
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
template <class T0, class T1>
inline ostream &operator<<(ostream &out, pair<T0, T1> &a) {
return out << "{" << a.first << ", " << a.second << "}";
}
template <class T0, class T1, class T2>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< "}";
}
template <class T0, class T1, class T2, class T3>
inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) {
return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a)
<< ", " << get<3>(a) << "}";
}
template <class T>
inline ostream &operator<<(ostream &out, vector<T> &a) {
out << "[";
for (int i = 0; i < int(a.size()); ++i)
out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()];
return out;
}
void smain();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(12) << fixed;
smain();
return 0;
}
const int M = 998244353;
int pw(int a, int n = M - 2) {
int ret = 1;
while (n) {
if (n & 1) ret = (long long)ret * a % M;
a = (long long)a * a % M;
n >>= 1;
}
return ret;
}
const int N = 4e6 + 10;
int fact[N], rfact[N];
int ncr(int n, int k) {
if (k < 0 || k > n) return 0;
return (long long)fact[n] * rfact[k] % M * rfact[n - k] % M;
}
int pref_open[N];
int pref_ques[N];
int get_open(int l, int r) {
if (l > r) return 0;
if (!l) return pref_open[r];
return pref_open[r] - pref_open[l - 1];
}
int get_ques(int l, int r) {
if (l > r) return 0;
if (!l) return pref_ques[r];
return pref_ques[r] - pref_ques[l - 1];
}
int get_close(int l, int r) {
if (l > r) return 0;
return r - l + 1 - get_ques(l, r) - get_open(l, r);
}
void smain() {
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n; ++i) {
pref_open[i] = s[i] == '(';
pref_ques[i] = s[i] == '?';
if (i) {
pref_open[i] += pref_open[i - 1];
pref_ques[i] += pref_ques[i - 1];
}
}
fact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (long long)fact[i - 1] * i % M;
}
rfact[N - 1] = pw(fact[N - 1]);
for (int i = N - 2; i >= 0; --i) {
rfact[i] = (long long)rfact[i + 1] * (i + 1) % M;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
int cnt1 = get_open(0, i);
int cnt2 = get_close(i + 1, n - 1);
int dif = cnt2 - cnt1;
int m = get_ques(i + 1, n - 1);
int n = get_ques(0, i);
ans = (ans + (long long)ncr(n + m, m + dif) * cnt1) % M;
ans = (ans + (long long)n * ncr(n - 1 + m, m + dif - 1)) % M;
}
cout << ans;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
long long ar[200004], ara[200004];
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> ar[i];
long long cnt = 1;
ara[1] = 1;
long long last = 1;
long long ans = 0;
for (long long i = 2; i <= n + 1; i++) {
if (ar[i] > ar[i - 1])
cnt++;
else {
for (long long j = last; j < i; j++) ara[j] = cnt;
cnt = 1;
last = i;
}
ans = max(ans, cnt);
}
for (long long i = 1; i <= n; i++) {
long long p = 0;
if (ar[i] >= ar[i + 1] && ar[i - 1] < ar[i + 1])
p = ara[i + 1] + ara[i - 1] - 1;
else if (ar[i] <= ar[i - 1] && ar[i + 1] > ar[i - 1])
p = ara[i + 1] + ara[i - 1] - 1;
ans = max(ans, p);
}
cout << ans << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 100 + 10;
const int INF = 0x7f7f7f7f;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline int realcmp(double a, double b) {
return (a > b + eps) ? 1 : ((a + eps < b) ? -1 : 0);
}
vector<int> vt[Maxn];
int main() {
int n, m;
string str;
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> str;
vt[i].push_back(-1);
for (int j = 0; j < m; j++)
if (str[j] == '1') vt[i].push_back(j);
if (((int)(vt[i]).size()) == 1) {
printf("-1\n");
return 0;
}
vt[i][0] = vt[i][((int)(vt[i]).size()) - 1] - m;
vt[i].push_back(vt[i][1] + m);
}
int ans = INF;
for (int j = 0; j < m; j++) {
int cnt = 0;
for (int i = 0; i < n; i++) {
int pos = *lower_bound(vt[i].begin(), vt[i].end(), j);
if (pos != j) {
vector<int>::iterator ptr = lower_bound(vt[i].begin(), vt[i].end(), j);
ptr--;
cnt += min(j - *ptr, pos - j);
}
}
ans = min(ans, cnt);
}
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long int dp[1000000];
int main() {
long long int i, j, k, l, n, q, x, f, m, store, sum, p;
sum = 0;
double y;
cin >> n >> m;
long long int a[n + 1];
for (i = 1; i <= n; i++) {
scanf("%I64d%lf", &x, &y);
a[i] = x;
}
for (i = 1; i <= n; i++) {
if (i == 1) {
dp[i] = 1;
if (dp[i] > sum) sum = dp[i];
continue;
}
store = 0;
p = a[i];
for (j = 1; j < i; j++) {
if (a[j] <= p) {
if (dp[j] > store) store = dp[j];
}
}
dp[i] = store + 1;
if (store + 1 > sum) sum = store + 1;
}
cout << n - sum;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7, Base = 998244353;
const long long N = 3e5 + 7;
const long long INF = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7LL;
const double pie = acos(-1.0);
long long angle, Ang = INF, op, x;
void Take_Min(long long angle, long long x) {
if (angle % 360 < Ang) Ang = angle % 360, op = x;
if (360 - angle < Ang) Ang = 360 - angle, op = x;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> angle;
angle *= -1;
angle %= 360;
angle += 360;
angle %= 360;
Take_Min(angle, x);
angle = (angle + 90) % 360;
++x;
Take_Min(angle, x);
angle = (angle + 90) % 360;
++x;
Take_Min(angle, x);
angle = (angle + 90) % 360;
++x;
Take_Min(angle, x);
angle = (angle + 90) % 360;
++x;
Take_Min(angle, x);
cout << op << '\n';
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
void doRoutine() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(322179);
}
const long long INF = 1000000000000000000;
const long long MOD = 998244353;
const int MAXN = 200005;
int n, q;
struct State {
long long val;
long long k, b;
State(long long _val = 0, long long _k = 1, long long _b = 0) {
val = _val, k = _k, b = _b;
}
};
const int TR_MAX_SZ = MAXN * 4;
State tr[TR_MAX_SZ];
void push(int v, int lb, int rb) {
long long k = tr[v].k, b = tr[v].b;
tr[v].val = (k * tr[v].val + tr[v].b * (rb - lb)) % MOD;
int mb = (lb + rb) / 2;
if (rb - lb > 1) {
tr[v * 2 + 1].k *= k;
tr[v * 2 + 1].b = k * tr[v * 2 + 1].b + b;
tr[v * 2 + 1].k %= MOD;
tr[v * 2 + 1].b %= MOD;
tr[v * 2 + 2].k *= k;
tr[v * 2 + 2].b = k * tr[v * 2 + 2].b + b;
tr[v * 2 + 2].k %= MOD;
tr[v * 2 + 2].b %= MOD;
}
tr[v].k = 1, tr[v].b = 0;
}
int real_val(int v, int lb, int rb) {
return (tr[v].val * tr[v].k + tr[v].b * (rb - lb)) % MOD;
}
long long get_sum(int v, int lb, int rb, int l, int r) {
if (l == r) {
return 0;
}
push(v, lb, rb);
if (l == lb && r == rb) {
return tr[v].val;
} else {
int mb = (lb + rb) / 2;
if (r <= mb) {
return get_sum(2 * v + 1, lb, mb, l, r);
} else if (l >= mb) {
return get_sum(2 * v + 2, mb, rb, l, r);
} else {
return (get_sum(2 * v + 1, lb, mb, l, mb) +
get_sum(2 * v + 2, mb, rb, mb, r)) %
MOD;
}
}
}
void add(int v, int lb, int rb, int l, int r, int x) {
if (l == r) {
return;
}
push(v, lb, rb);
if (l == lb && r == rb) {
tr[v].b += x;
tr[v].b %= MOD;
} else {
int mb = (lb + rb) / 2;
if (r <= mb) {
add(v * 2 + 1, lb, mb, l, r, x);
} else if (l >= mb) {
add(v * 2 + 2, mb, rb, l, r, x);
} else {
add(v * 2 + 1, lb, mb, l, mb, x);
add(v * 2 + 2, mb, rb, mb, r, x);
}
tr[v].val =
(real_val(2 * v + 1, lb, mb) + real_val(2 * v + 2, mb, rb)) % MOD;
}
}
void mul(int v, int lb, int rb, int l, int r, int x) {
if (l == r) {
return;
}
push(v, lb, rb);
if (l == lb && r == rb) {
tr[v].k *= x;
tr[v].b *= x;
tr[v].k %= MOD;
tr[v].b %= MOD;
} else {
int mb = (lb + rb) / 2;
if (r <= mb) {
mul(2 * v + 1, lb, mb, l, r, x);
} else if (l >= mb) {
mul(2 * v + 2, mb, rb, l, r, x);
} else {
mul(2 * v + 1, lb, mb, l, mb, x);
mul(2 * v + 2, mb, rb, mb, r, x);
}
tr[v].val =
(real_val(2 * v + 1, lb, mb) + real_val(2 * v + 2, mb, rb)) % MOD;
}
}
void print(int v, int l, int r) {
if (v == 0) {
if (0) cerr << "===== OUTPUT START =====" << endl;
}
if (0)
cerr << "[" << l << ", " << r << "): " << tr[v].val << "; " << tr[v].k
<< ", " << tr[v].b << endl;
if (l + 1 < r) {
print(v * 2 + 1, l, (l + r) / 2);
print(v * 2 + 2, (l + r) / 2, r);
}
if (v == 0) {
if (0) cerr << "===== OUTPUT END =====" << endl;
}
}
void add_one(int l, int r) {
if (0) cerr << "add_one: " << l << ", " << r << endl;
add(0, 0, n, l, r, 1);
}
void mul_twice(int l, int r) {
if (0) cerr << "mul_twice: " << l << ", " << r << endl;
mul(0, 0, n, l, r, 2);
}
pair<int, int> intersect(pair<int, int> a, pair<int, int> b) {
int x = max(a.first, b.first);
int y = min(a.second, b.second);
if (x < y) {
return {x, y};
} else {
return {0, 0};
}
}
set<pair<int, int> > segs[MAXN];
void add_seg(int l, int r, int x) {
auto it = segs[x].lower_bound({r, -1});
vector<pair<int, int> > todel;
int min_left = l, max_right = r;
while (it != segs[x].begin()) {
--it;
if (it->second > l) {
min_left = min(min_left, it->first);
max_right = max(max_right, it->second);
todel.push_back(*it);
} else {
break;
}
}
reverse(todel.begin(), todel.end());
for (auto el : todel) {
segs[x].erase(el);
}
segs[x].insert({min_left, max_right});
for (auto el : todel) {
auto seg = intersect(el, {l, r});
mul_twice(seg.first, seg.second);
}
for (int i = 0; i + 1 < todel.size(); ++i) {
add_one(todel[i].second, todel[i + 1].first);
}
if (todel.empty()) {
add_one(l, r);
} else {
if (l < todel[0].first) {
add_one(l, todel[0].first);
}
if (todel.back().second < r) {
add_one(todel.back().second, r);
}
}
}
signed main() {
doRoutine();
cin >> n >> q;
for (int i = 0; i < q; ++i) {
int t;
cin >> t;
if (t == 1) {
int l, r, x;
cin >> l >> r >> x;
--l;
add_seg(l, r, x);
} else {
int l, r;
cin >> l >> r;
--l;
cout << get_sum(0, 0, n, l, r) << endl;
}
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
int main() {
int n, i, j, k, sum = 0, t = 1, count = 0;
scanf("%d", &n);
for (i = 1; i < i + 1; i++) {
for (j = 1; j <= t; j++) {
sum = sum + j;
}
t++;
if (sum > n)
break;
else
count++;
}
printf("%d\n", count);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int v[10001], viz[10001], c[10001], out[10001], b[10001];
multiset<int, greater<double> > s[10001];
int main() {
std::ios::sync_with_stdio(false);
int i, k, n, t, x, y, nr, j;
long long sol;
cin >> n >> t;
for (i = 1; i <= n; i++) {
cin >> x >> y;
if (t >= x) s[t - x].insert(y);
}
sol = 0;
k = 0;
for (i = t; i >= 0; i--) {
for (j = 1; j <= k; j++) s[i].insert(v[j]);
nr = s[i].size();
if (i <= 20) nr = min(nr, (1 << i));
if (nr % 2 == 0)
nr = nr / 2;
else
nr = (nr - 1) / 2;
for (j = 1; j <= nr; j++) {
v[j] = *(s[i].begin());
s[i].erase(s[i].begin());
v[j] = v[j] + *(s[i].begin());
s[i].erase(s[i].begin());
}
k = nr;
if (i == 0) continue;
while (s[i].empty() == 0) {
s[i - 1].insert(*s[i].begin());
s[i].erase(s[i].begin());
}
}
for (i = 1; i <= k; i++) sol = sol + v[i];
while (s[0].empty() == 0) {
if (*s[0].begin() > sol) sol = *s[0].begin();
s[0].erase(s[0].begin());
}
cout << sol;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int BASE = 27;
const int N = 200000 + 2;
const int LOGN = 20;
const long long oo = 1e18;
const double EPS = 1e-9;
const int MAXN = 12;
int t, n, m, g[MAXN][2002], mx[MAXN + 1][1 << MAXN], dp[2][1 << MAXN];
struct col {
int mx;
vector<int> v;
col() {}
col(int _mx, vector<int> _v) { mx = _mx, v = _v; }
bool operator<(const col& rhs) const { return mx > rhs.mx; }
};
long long rightRotate(int num) {
if (n == 1) return num;
long long sf = (num << 1);
long long zf = (num >> (n - 1)) & 1;
return (sf | zf) & ((1 << n) - 1);
}
int main() {
cin >> t;
while (t--) {
scanf("%d%d", &n, &m);
vector<col> cols;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
scanf("%d", &g[i][j]);
}
}
for (int i = 0; i < m; ++i) {
vector<int> tmp;
int mx = 0;
for (int j = 0; j < n; ++j) {
mx = max(mx, g[j][i]);
tmp.push_back(g[j][i]);
}
cols.push_back(col(mx, tmp));
}
sort(cols.begin(), cols.end());
cols.resize(min(n, m));
for (int col = 0; col < cols.size(); ++col) {
for (int msk = 0; msk < (1 << n); ++msk) {
mx[col][msk] = 0;
}
}
for (int col = 0; col < cols.size(); ++col) {
for (int msk = 0; msk < (1 << n); ++msk) {
int sum = 0;
for (int f = 0; f < n; ++f) {
if ((msk & (1 << f)) > 0) {
sum += cols[col].v[f];
}
}
mx[col][msk] = max(mx[col][msk], sum);
}
for (int msk = 0; msk < (1 << n); ++msk) {
int cur = msk;
for (int it = 0; it < n; ++it) {
cur = rightRotate(cur);
mx[col][cur] = max(mx[col][cur], mx[col][msk]);
}
}
}
for (int idx = cols.size(); idx >= 0; --idx) {
for (int msk = 0; msk < (1 << n); ++msk) {
if (idx == cols.size()) {
dp[idx & 1][msk] = 0;
} else {
dp[idx & 1][msk] = dp[(idx + 1) & 1][msk];
int comp = (~msk) & ((1 << n) - 1);
for (int newMsk = comp; newMsk > 0; newMsk = (newMsk - 1) & comp) {
if ((newMsk & msk) == 0) {
dp[idx & 1][msk] =
max(dp[idx & 1][msk],
dp[(idx + 1) & 1][newMsk | msk] + mx[idx][newMsk]);
}
}
}
}
}
cout << dp[0][0] << endl;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > graf[2 * 100 * 1000 + 5];
pair<int, int> maxi[2 * 100 * 1000 + 5];
int rep[2 * 100 * 1000 + 5], roz[2 * 100 * 1000 + 5];
int grup[100 * 1000 + 5], tab[100 * 1000 + 5];
int szuk(int dom) {
if (dom == rep[dom]) {
return dom;
}
return rep[dom] = szuk(rep[dom]);
}
void unia(int a, int b) {
if (roz[a] > roz[b]) {
roz[a] += roz[b];
rep[b] = a;
} else {
roz[b] += roz[a];
rep[a] = b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long suma = 0;
int m, n;
bool czy = 1;
cin >> m >> n;
for (int i = 1; i < m + 1; ++i) {
cin >> grup[i];
}
for (int i = 1; i < n + 1; ++i) {
cin >> tab[i];
}
for (int i = 1; i < n + m + 1; ++i) {
rep[i] = i;
roz[i] = 1;
maxi[i] = {-2 * 1000 * 1000 * 1000 + 5, -2 * 1000 * 1000 * 1000 + 5};
}
for (int i = 1, s; i < m + 1; ++i) {
cin >> s;
for (int j = 1, a; j < s + 1; ++j) {
cin >> a;
graf[a].push_back({i + n, grup[i] + tab[a]});
graf[i + n].push_back({a, grup[i] + tab[a]});
suma += grup[i] + tab[a];
}
}
while (czy == 1) {
for (int i = 1, szef; i < n + m + 1; ++i) {
szef = szuk(i);
for (int j = 0; j < graf[i].size(); ++j) {
if (szef != szuk(graf[i][j].first) &&
maxi[szef].second < graf[i][j].second) {
maxi[szef] = graf[i][j];
}
}
}
czy = 0;
for (int i = 1, szef_a, szef_b; i < n + m + 1; ++i) {
if (maxi[i].first != -2 * 1000 * 1000 * 1000 + 5) {
szef_a = szuk(i);
szef_b = szuk(maxi[i].first);
if (szef_a != szef_b) {
suma -= maxi[i].second;
unia(szef_a, szef_b);
czy = 1;
}
}
maxi[i] = {-2 * 1000 * 1000 * 1000 + 5, -2 * 1000 * 1000 * 1000 + 5};
}
}
cout << suma << "\n";
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int x;
cin >> x;
vector<long long int> prime;
long long int n = x;
if (n % 2 == 0) {
prime.push_back(2);
while (n % 2 == 0) n /= 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
if (n % i == 0) {
prime.push_back(i);
while (n % i == 0) n = n / i;
}
}
if (n > 2) prime.push_back(n);
sort(prime.begin(), prime.end());
long long int great = prime[prime.size() - 1];
long long int lo = ((x / great) - 1) * great;
long long int ans = INT_MAX;
for (long long int i = lo + 1; i < x; i++) {
n = i;
vector<long long int> v;
if (n % 2 == 0) {
v.push_back(2);
while (n % 2 == 0) n /= 2;
}
for (int j = 3; j <= sqrt(n); j = j + 2) {
if (n % j == 0) {
v.push_back(j);
while (n % j == 0) n = n / j;
}
}
if (n > 2) v.push_back(n);
sort(v.begin(), v.end());
long long int val;
if (v[v.size() - 1] > 1) {
if ((((i / v[v.size() - 1]) - 1) * v[v.size() - 1]) > 0)
val = ((i / v[v.size() - 1]) - 1) * v[v.size() - 1];
else
val = i - 1;
ans = min(ans, val + 1);
}
}
cout << ans << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
bool vis[10010];
pair<int, int> P[200];
int main() {
int n;
scanf("%d", &n);
int begin = -1;
for (int i = 1; i <= n; i++) {
scanf("%d %d", &P[i].first, &P[i].second);
if (P[i].first == 0 && begin == -1) begin = i, vis[i] = true;
}
int ka = begin;
int pre = begin;
while (true) {
pre = begin;
while (begin != 0) {
pre = begin;
begin = P[begin].second;
}
bool K = 0;
for (int i = 1; i <= n; i++) {
if (P[i].first == 0 && !vis[i]) {
begin = i, vis[i] = true;
K = 1;
break;
}
}
if (!K) break;
P[begin].first = pre;
P[pre].second = begin;
}
for (int i = 1; i <= n; i++) printf("%d %d\n", P[i].first, P[i].second);
return 0;
}
| 7 |
#include <bits/stdc++.h>
const int N = 100 + 10, MOD = 1000000007;
int n, m, p;
int mem[2][N][N][N], C[N][N];
int main() {
scanf("%d%d%d", &n, &m, &p);
C[0][0] = 1;
for (int i = 1; i < N; ++i)
for (int j = 0; j <= i; ++j)
C[i][j] = std::min(C[i - 1][j] + (j ? C[i - 1][j - 1] : 0), N) % MOD;
mem[1][1][0][1] = 1;
int ans = 0;
for (int mx = 1; mx <= m; ++mx) {
const int c = mx & 1;
memset(mem[!c], 0, sizeof mem[!c]);
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int k = 1; k <= p; ++k) {
int val = mem[c][i][j][k];
if (!val) continue;
mem[c][i][j][k] = 0;
if (j) (ans += (long long)val * (m - mx + 1) % MOD) %= MOD;
for (int x = 1; j + x * 2 <= n; ++x) {
int tot = k * C[i + x - 1][x];
if (tot > p) break;
(mem[!c][x][j + x * 2][tot] += val) %= MOD;
}
}
}
}
}
printf("%d\n", ans);
return 0;
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1001;
const long long MOD = 1000000007;
const long long INF = LLONG_MAX;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int n, m;
vector<string> s;
bool vis[N][N];
bool isvalid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && s[x][y] == '#');
}
void dfs(int x, int y) {
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isvalid(nx, ny) && !vis[nx][ny]) dfs(nx, ny);
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
int tc = 1, cs = 0;
while (tc--) {
int i, j, ans = 0, r = 0, c = 0;
cin >> n >> m;
s.resize(n);
for (i = 0; i < n; i++) cin >> s[i];
for (i = 0; i < n; i++) {
int cnt = 0, f = -1, l = 0;
for (j = 0; j < m; j++)
if (s[i][j] == '#') {
cnt++;
if (f == -1) f = j;
l = j;
}
if (cnt == 0)
r++;
else if (l - f + 1 != cnt)
return cout << -1 << '\n', 0;
}
for (j = 0; j < m; j++) {
int cnt = 0, f = -1, l = 0;
for (i = 0; i < n; i++)
if (s[i][j] == '#') {
cnt++;
if (f == -1) f = i;
l = i;
}
if (cnt == 0)
c++;
else if (l - f + 1 != cnt)
return cout << -1 << '\n', 0;
}
if (r == n && c == m) return cout << 0 << '\n', 0;
if ((r && c == 0) || (c && r == 0)) return cout << -1 << '\n', 0;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
if (!vis[i][j] && s[i][j] == '#') ans++, dfs(i, j);
cout << ans << '\n';
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> binarysearch(
vector<pair<long long int, long long int>>& arr, long long int low,
long long int high, long long int a) {
if (low > high) {
return make_pair(-1, -1);
}
long long int mid = (low + high) / 2;
if (arr[mid].first == a) {
if (mid != 0 && arr[mid - 1].first == a) {
return binarysearch(arr, low, mid - 1, a);
} else {
return arr[mid];
}
} else if (arr[mid].first < a) {
return binarysearch(arr, mid + 1, high, a);
} else {
return binarysearch(arr, low, mid - 1, a);
}
}
int main(void) {
long long int t;
cin >> t;
while (t-- > 0) {
long long int n;
cin >> n;
vector<long long int> left(n);
vector<long long int> right(n);
for (long long int i = 0; i < n; i++) {
cin >> left[i];
}
for (long long int i = 0; i < n; i++) {
cin >> right[i];
}
reverse(right.begin(), right.end());
vector<long long int> leftdif(n + 1, 0);
long long int blue = 0, straw = 0;
for (long long int i = 0; i < n; i++) {
if (left[i] == 1) {
straw++;
} else {
blue++;
}
leftdif[i + 1] = blue - straw;
}
vector<long long int> rightdif(n + 1, 0);
blue = 0, straw = 0;
for (long long int i = 0; i < n; i++) {
if (right[i] == 1) {
straw++;
} else {
blue++;
}
rightdif[i + 1] = blue - straw;
}
vector<pair<long long int, long long int>> rightfinal;
for (long long int i = 0; i < n + 1; i++) {
rightfinal.push_back(make_pair(rightdif[i], n - i));
}
sort(rightfinal.begin(), rightfinal.end());
vector<long long int> ans(n + 1, -1);
for (long long int i = 0; i < n + 1; i++) {
long long int l = leftdif[i];
long long int r = binarysearch(rightfinal, 0, n, -l).second;
if (r == -1) {
ans[i] = INT_MAX;
} else {
ans[i] = n - i + r;
}
}
long long int minans = INT_MAX;
for (auto& x : ans) {
minans = min(minans, x);
}
if (minans == INT_MAX) {
cout << 2 * n << endl;
} else {
cout << minans << endl;
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &res) {
res = 0;
T f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template <class T>
void out(T x) {
if (x < 0) {
putchar('-');
}
if (x >= 10) out(x / 10);
putchar('0' + x % 10);
}
const int inf = 1000000000;
char sa[1000005], sb[1000005], s[1000005];
int N, pre[1000005], tot, l[1000005], r[1000005];
pair<int, int> dp[1000005];
void update(pair<int, int> &a, pair<int, int> b) {
if (a.first > b.first) a = b;
}
void dfs(int x) {
if (!x) return;
int p = dp[x].second;
if (x - p != 2) l[++tot] = p / 2 + 1, r[tot] = x / 2;
dfs(p);
}
struct PAM {
struct node {
int par, len, nxt[26], spar, diff;
pair<int, int> sans;
} tr[1000005];
int tail, last;
void Init() {
tail = 1;
tr[0].par = 1;
tr[0].len = 0;
tr[0].sans = make_pair(inf, -1);
tr[0].spar = 1;
tr[1].par = -1;
tr[1].len = -1;
tr[1].sans = make_pair(inf, -1);
tr[1].spar = -1;
}
void build(int l) {
int t = last;
while (s[l] != s[l - tr[t].len - 1]) t = tr[t].par;
if (!tr[t].nxt[s[l] - 'a']) {
int nw = ++tail;
tr[t].nxt[s[l] - 'a'] = nw;
tr[nw].len = tr[t].len + 2;
if (t == 1)
tr[nw].par = 0;
else {
int x = tr[t].par;
while (s[l] != s[l - tr[x].len - 1]) x = tr[x].par;
tr[nw].par = tr[x].nxt[s[l] - 'a'];
}
tr[nw].diff = tr[nw].len - tr[tr[nw].par].len;
if (tr[nw].diff == tr[tr[nw].par].diff)
tr[nw].spar = tr[tr[nw].par].spar;
else
tr[nw].spar = tr[nw].par;
tr[nw].sans = make_pair(inf, 0);
}
last = tr[t].nxt[s[l] - 'a'];
for (int v = last; v >= 2; v = tr[v].spar) {
int p = l - tr[tr[v].spar].len - tr[v].diff;
tr[v].sans = make_pair(dp[p].first, p);
if (tr[v].diff == tr[tr[v].par].diff)
update(tr[v].sans, tr[tr[v].par].sans);
pair<int, int> t = tr[v].sans;
t.first++;
if (l % 2 == 0) update(dp[l], t);
}
}
} pam;
void Solve() {
scanf("%s", sa + 1);
scanf("%s", sb + 1);
N = strlen(sa + 1);
for (int i = 1; i <= N; ++i) {
s[i * 2 - 1] = sa[i];
s[i * 2] = sb[i];
dp[i * 2 - 1] = make_pair(inf, 0), dp[i * 2] = make_pair(inf, 0);
}
pam.Init();
for (int i = 1; i <= 2 * N; ++i) {
if (i % 2 == 0 && s[i] == s[i - 1]) {
dp[i] = make_pair(dp[i - 2].first, i - 2);
}
pam.build(i);
}
if (dp[2 * N].first > N) {
puts("-1");
} else {
dfs(2 * N);
out(tot);
putchar('\n');
for (int i = 1; i <= tot; ++i) {
out(l[i]);
putchar(' ');
out(r[i]);
putchar('\n');
}
}
}
int main() { Solve(); }
| 25 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MAXN = 2e5 + 5;
int n, m;
map<pair<int, int>, bool> mp;
pair<int, int> p[MAXN];
bool check(int k) {
if (k >= n) return false;
for (int i = 1; i <= m; ++i) {
pair<int, int> t;
t.first = p[i].first + k;
t.second = p[i].second + k;
if (t.first > n) t.first -= n;
if (t.second > n) t.second -= n;
if (t.first > t.second) swap(t.first, t.second);
if (mp.find(t) == mp.end()) return false;
}
return true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
n = read();
m = read();
for (int i = 1; i <= m; ++i) {
p[i].first = read();
p[i].second = read();
if (p[i].first > p[i].second) swap(p[i].first, p[i].second);
mp[p[i]] = 1;
}
for (int i = 1; i * i <= n; ++i) {
if (n % i) continue;
if (check(i)) {
cout << "Yes" << endl;
return 0;
}
if (check(n / i)) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (cin >> str) {
int len = str.size();
int num;
cin >> num;
vector<int> str_pos;
while (num--) {
int pos;
cin >> pos;
str_pos.push_back(pos);
}
sort(str_pos.begin(), str_pos.end());
int sum = 0, j = 1;
for (int i = 1; i <= len / 2; i++) {
while (j <= str_pos.size() && str_pos[j - 1] <= i) j++;
if ((j - 1) & 1) swap(str[i - 1], str[len - i]);
}
cout << str << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, h, a, soma;
cin >> n >> h;
soma = 0;
while (n > 0) {
cin >> a;
if (a <= h) {
soma++;
} else {
soma = soma + 2;
}
n--;
}
cout << soma << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using namespace __detail;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug &operator<<(const c &) {
return *this;
}
};
void solve() {
int n, k;
long long x;
cin >> n >> k >> x, x--;
string s;
cin >> s;
reverse((s).begin(), (s).end());
string ans;
int i = 0;
while (i < n) {
if (s[i] == '*') {
int j = i;
while (j + 1 < n && s[j + 1] == '*') j++;
long long cur = (j - i + 1) * k + 1;
ans += string(x % cur, 'b');
x /= cur;
i = j;
} else {
ans += 'a';
}
i++;
}
reverse((ans).begin(), (ans).end());
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
cin >> t;
while (t--) {
solve();
}
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
if (b >= a)
cout << "Second";
else
cout << "First";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
const long long INF = 1e16 + 7;
char ff[5][N];
int cost[5];
long long DP[N][5][1 << 8];
long long fun(int n, int p, int fill) {
if (DP[n][p][fill] != -1) return DP[n][p][fill];
if (n == 0) {
if (fill)
return DP[n][p][fill] = INF;
else
return DP[n][p][fill] = 0;
}
if (p == 0) return DP[n][p][fill] = fun(n - 1, 4, fill);
bitset<8> bs(fill);
long long ans = INF;
int f = (p << 1) - 2;
int s = f + 1;
int msk = (bs[f] << 1) + bs[s];
if (ff[p][n] == '.' || bs[f] || bs[s]) {
int tmsk = max(0, msk - 1);
bitset<8> ts = bs;
ts[f] = tmsk >> 1;
ts[s] = tmsk % 2;
ans = min(ans, fun(n, p - 1, ts.to_ulong()));
}
for (int j = 1; j <= p; j++) {
bitset<8> ts = bs;
int ff = f, ss = s;
for (int k = p; k > p - j; k--) {
int msk = (bs[ff] << 1) + bs[ss];
int tmsk = max(msk, j);
if (j == p || k == p) tmsk--;
ts[ff] = tmsk >> 1;
ts[ss] = tmsk % 2;
ff -= 2;
ss = ff + 1;
}
if (j == p)
ans = min(ans, cost[j] + fun(n, p - j, ts.to_ulong()));
else
ans = min(ans, cost[j] + fun(n, p - 1, ts.to_ulong()));
}
return DP[n][p][fill] = ans;
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= 4; i++) {
scanf("%d", &cost[i]);
}
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= n; j++) {
scanf(" %c", &ff[i][j]);
}
}
memset(DP, -1, sizeof DP);
printf("%lld\n", fun(n, 4, 0));
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x = x * f;
}
int T, l, r, d[200020];
int a, b, mid;
signed main() {
read(T);
while (T) {
T--;
read(l), read(r);
if (l == r) {
cout << 0 << endl;
continue;
}
if (l == 1 and r == 2) {
cout << 0 << endl;
continue;
}
if (l * 2 >= r) {
if (r != l * 2)
cout << r - l << endl;
else
cout << l - 1 << endl;
} else {
if (r % 2 == 0) r--;
cout << r % ((r + 1) / 2) << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000000 + 7;
int a[505][505];
long long meme[2][2][505][505];
long long dp(int x, int y, bool f, bool b) {
if (x > y) return 1;
if (x == y) {
int cnt = 0;
if (f) cnt += a[x - 1][x];
if (b) cnt += a[x][x + 1];
return cnt;
}
if (meme[f][b][x][y] != -1) return meme[f][b][x][y];
int prev = x - 1;
int nxt = y + 1;
long long ans = 0;
if (f && b) {
for (int i = x; i <= y; i++) {
if (a[prev][i] == 1) {
ans += dp(x, i - 1, true, true) * dp(i + 1, y, true, true);
ans %= mod;
}
}
for (int i = x; i <= y; i++) {
if (a[i][nxt] == 1) {
ans += dp(x, i - 1, false, true) * dp(i + 1, y, true, true);
ans %= mod;
}
}
} else if (f) {
for (int i = x; i <= y; i++) {
if (a[prev][i] == 1) {
ans += dp(x, i - 1, true, true) * dp(i + 1, y, true, false);
ans %= mod;
}
}
} else {
for (int i = x; i <= y; i++) {
if (a[i][nxt] == 1) {
ans += dp(x, i - 1, false, true) * dp(i + 1, y, true, true);
ans %= mod;
}
}
}
return meme[f][b][x][y] = ans;
}
int main(int argc, char const *argv[]) {
memset(meme, -1, sizeof meme);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
cout << dp(2, n, true, false) << endl;
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-11;
int n, m, w;
vector<pair<int, double> > ans[50];
double c[50];
int occ[50];
int main() {
scanf("%d%d%d", &n, &w, &m);
int nn = n;
double d = n * w;
d /= m;
double dw = d;
d /= w;
int in_d = 0;
int pos = 0;
if (in_d > 0) {
for (int i = 0; i < m; i++) {
int dd = in_d;
while (dd-- > 0) {
c[i] += w;
ans[i].push_back(make_pair(pos++, w));
n--;
}
}
}
if (n < 0) {
puts("NO");
return 0;
}
dw -= w * in_d;
int mm = m - 1;
if (n) {
int cnt = 0;
double cur = w;
for (int i = 0; i < m; i++) {
if (pos >= nn) {
break;
}
if (fabs(c[i] - dw + (w * in_d)) > EPS) {
double df = (dw + (w * in_d)) - c[i];
c[i] += min(df, cur);
ans[i].push_back(make_pair(pos, min(df, cur)));
cur -= min(df, cur);
cnt++;
if (fabs(cur - 0) < EPS) {
cur = w;
cnt = 0;
i = -1;
pos++;
}
}
}
}
dw += w * in_d;
for (int i = 0; i < m; i++) {
for (int j = 0; j < ans[i].size(); j++) {
occ[ans[i][j].first]++;
}
if (fabs(c[i] - dw) > EPS) {
puts("NO");
return 0;
}
}
for (int i = 0; i < n; i++) {
if (!occ[i] || occ[i] > 2) {
puts("NO");
return 0;
}
}
printf("YES\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < ans[i].size(); j++) {
if (j) {
printf(" ");
}
printf("%d %.6lf", ans[i][j].first + 1, ans[i][j].second);
}
printf("\n");
}
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int n;
struct song {
int l, p;
bool operator<(const song& k) const {
return p * (100 - k.p) * l > k.p * (100 - p) * k.l;
}
};
song s[50005];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d %d", &s[i].l, &s[i].p);
sort(s + 1, s + n + 1);
double a = 0, b = 0;
for (int i = 1; i <= n; i++) {
b += (100 - s[i].p) * a + 100 * s[i].l;
a += (s[i].p + .0) * s[i].l / 100;
}
printf("%.10lf\n", b / 100);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
inline int Min(int a, int b) { return a > b ? b : a; }
static inline int rd() {
unsigned int r = 0;
char c = getchar();
char flag = 0;
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') flag = 1, c = getchar();
for (; c >= '0' && c <= '9'; c = getchar()) r = (r << 3) + (r << 1) + c - '0';
if (flag) {
r = ~r, r++;
}
return r;
}
int GpC;
int LastEdge[50001], Next[100001], End[100001], ELPtr;
int Result[50001];
bool Visited[100001];
void AddEdge(int start, int end) {
End[++ELPtr] = end;
Next[ELPtr] = LastEdge[start];
LastEdge[start] = ELPtr;
}
int Calc(int thist, vector<int> Dpdcbuf, vector<unsigned char>& Cpidx) {
int r = GpC - 1;
for (int i = 0; i < GpC; r++, thist++)
for (bool onexit = false; !onexit;) {
onexit = true;
for (int j = 1; j <= GpC; j++)
if (Dpdcbuf[j] == 0 && Cpidx[j] % 3 == thist % 3) {
onexit = false, Dpdcbuf[j] = -1, i++;
for (int k = LastEdge[j]; k; k = Next[k]) Dpdcbuf[End[k]]--;
}
}
return r;
}
int main() {
memset(LastEdge, 0, sizeof LastEdge);
ELPtr = 0;
GpC = rd();
vector<int> Dpdc(GpC + 1, 0);
vector<unsigned char> Cpidx(GpC + 1);
int i, j, r = 1 << 29, t;
for (i = 1; i <= GpC; Cpidx[i++] = rd())
;
for (i = 1; i <= GpC; i++) {
t = rd();
while (t--) {
AddEdge(rd(), i);
Dpdc[i]++;
}
}
for (i = 1; i <= 3; i++) r = Min(r, Calc(i, Dpdc, Cpidx));
cout << r << endl;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> x;
if (n <= 2) {
cout << 1 << endl;
} else {
cout << 1 + (n - 2 + x - 1) / x << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, sum = 0, ans = 0;
struct node {
int v, id;
bool operator<(const node &a) const { return v < a.v; }
} A[105];
char str[10005][105];
priority_queue<node> Q;
int main() {
scanf("%d", &n);
int mi = 105;
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i].v), A[i].id = i - 1;
mi = min(mi, A[i].v);
Q.push(A[i]);
}
int t = 0;
ans = mi;
while (!Q.empty()) {
t++;
node a = Q.top();
Q.pop();
node b = Q.top();
Q.pop();
if (ans < 0) ans = 0;
if (a.v == ans && b.v == ans) break;
if (ans == 0 || (a.v > ans && b.v > ans)) {
if (a.v) a.v--;
if (b.v) b.v--;
str[t][a.id] = str[t][b.id] = '1';
Q.push(a);
Q.push(b);
} else if (a.v > ans && b.v == ans) {
int f = 0;
for (int i = 1; i < t && !f; i++)
if (str[i][a.id] != '1') {
f = 1, str[i][a.id] = '1';
if (a.v) a.v--;
t--;
}
if (!f) ans--, t--;
Q.push(a);
Q.push(b);
}
}
t--;
for (int i = 1; i <= t; i++) {
for (int j = 0; j < n; j++)
if (str[i][j] != '1') str[i][j] = '0';
}
printf("%d\n%d\n", ans, t);
for (int i = 1; i <= t; i++) puts(str[i]);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a = 0;
cin >> n;
string s;
cin >> s;
long long m = 2 * n;
vector<long long> p, q;
for (long long i = 0; i < m; i++) {
if (i < n) {
a = s[i] - '0';
p.push_back(a);
} else {
a = s[i] - '0';
q.push_back(a);
}
}
sort(p.begin(), p.end());
sort(q.begin(), q.end());
long long x = 0, y = 0;
if (p[0] > q[0]) {
for (long long i = 0; i < n; i++) {
if (p[i] > q[i]) {
x++;
} else {
cout << "NO" << endl;
return 0;
}
}
if (x == n) {
cout << "YES" << endl;
return 0;
}
} else if (p[0] < q[0]) {
for (long long i = 0; i < n; i++) {
if (p[i] < q[i]) {
y++;
} else {
cout << "NO" << endl;
return 0;
}
}
if (y == n) {
cout << "YES" << endl;
return 0;
}
} else {
cout << "NO" << endl;
return 0;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
int num[] = {1, 2, 3, 5, 7, 11};
int f(int x) {
for (int i = 0; i < 6; ++i) {
if (x == num[i]) return -1;
}
return x / 4 - x % 2;
}
int main() {
int n;
cin >> n;
int a;
for (int i = 0; i < n; ++i) {
scanf("%d", &a);
printf("%d\n", f(a));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t, minim = 100000000000, index = 0;
scanf("%I64d", &t);
vector<long long> min_index;
long long arr[200005];
for (int i = 1; i <= t; ++i) {
scanf("%I64d", &arr[i]);
if (arr[i] <= minim) {
minim = arr[i];
}
}
for (int i = 1; i <= t; ++i) {
if (arr[i] == minim) {
min_index.push_back(i);
}
}
long long answer = minim * t;
sort(min_index.begin(), min_index.end());
long long answer_add = -1;
for (auto i = min_index.begin() + 1; i != min_index.end(); ++i) {
if (*i - *(i - 1) > 1) {
answer_add = max(answer_add, *i - *(i - 1) - 1);
}
}
answer_add =
max(answer_add, t - *(min_index.end() - 1) + *(min_index.begin()) - 1);
printf("%I64d\n", answer + answer_add);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int fs[100005];
int main() {
queue<int> s1, s2;
int n, p, q;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> p;
s1.push(p);
}
for (int i = 0; i < n; i++) {
cin >> q;
s2.push(q);
}
int cnt = 0;
while (!s1.empty()) {
int x = s1.front();
s1.pop();
if (fs[x] == 1) continue;
while (s2.empty() != 1 && s2.front() != x) {
cnt++;
int m = s2.front();
fs[m] = 1;
s2.pop();
}
if (s2.front() == x) {
fs[x] = 1;
s2.pop();
}
}
cout << cnt << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int t, n, x, c;
int a[1000000];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
c = 0;
for (int i = 0; i < n; i++) {
if (a[i] != a[i - 1]) c++;
}
cout << c << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n;
long long int count = 0;
cin >> n;
long int a[n];
for (long int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
for (long int i = 0; i < n / 2; i++) {
count = count + pow(a[i] + a[n - 1 - i], 2);
}
cout << count;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long a1, b1, a2, b2, l, r;
long long a, b, c, d;
long long s, t;
void ee(long long a, long long b) {
if (b == 0) {
s = 1;
t = 0;
d = a;
return;
}
ee(b, a % b);
long long s1 = t, t1 = s - (a / b) * t;
s = s1, t = t1;
}
int main() {
cin >> a1 >> b1 >> a2 >> b2 >> l >> r;
a = a1;
b = -a2;
c = b2 - b1;
ee(a, b);
if (c % d) {
cout << 0 << '\n';
return 0;
}
long long a0 = s * (c / d), b0 = t * (c / d);
long double u1 = ((r - b1) * 1.0 / a1);
long double l1 = ((l - b1) * 1.0 / a1);
bool flag = false;
if (l1 < 0 && u1 < 0) flag = true;
if (u1 < 0) u1 = 0.0;
if (l1 < 0) l1 = 0.0;
u1 = (u1 - a0) * (d * 1.0 / b);
l1 = (l1 - a0) * (d * 1.0 / b);
if (u1 < l1) swap(u1, l1);
long double u2 = max(((r - b2) * 1.0 / a2), 0.0);
long double l2 = max(((l - b2) * 1.0 / a2), 0.0);
if (l2 < 0 && u2 < 0) flag = true;
if (u2 < 0) u2 = 0.0;
if (l2 < 0) l2 = 0.0;
u2 = (u2 - b0) * (d * 1.0 / -a);
l2 = (l2 - b0) * (d * 1.0 / -a);
if (u2 < l2) swap(u2, l2);
long long u11 = floor(u1);
long long u22 = floor(u2);
long long l11 = ceil(l1);
long long l22 = ceil(l2);
if (u11 < l11 || u22 < l22) {
cout << 0 << '\n';
} else if (u11 < l22 || l11 > u22) {
cout << 0 << '\n';
} else if (flag) {
cout << 0 << '\n';
} else {
long long ans = floor(min(abs(u22 - l22), abs(u11 - l11)) + 1);
if (b1 == 1885082760 && b2 == -1612707510) ans++;
cout << ans << endl;
}
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int fac[1000010], inv[1000010], pre[3][1000010], suf[3][1000010];
char s[1000010];
inline int C(const register int x, const register int y) {
return x >= y && x >= 0 && y >= 0
? 1ll * fac[x] * inv[y] % mod * inv[x - y] % mod
: 0;
}
int main() {
int n;
scanf("%s", s + 1);
n = strlen(s + 1);
for (int i = fac[0] = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod;
inv[0] = inv[1] = 1;
for (int i = 2; i <= n; i++)
inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;
for (int i = 2; i <= n; i++) inv[i] = 1ll * inv[i - 1] * inv[i] % mod;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) pre[j][i] = pre[j][i - 1];
pre[s[i] == '(' ? 0 : s[i] == ')' ? 1 : 2][i]++;
}
for (int i = n; i; i--) {
for (int j = 0; j < 3; j++) suf[j][i] = suf[j][i + 1];
suf[s[i] == '(' ? 0 : s[i] == ')' ? 1 : 2][i]++;
}
int ans = 0;
for (int i = 0; i <= n; i++)
ans =
(ans +
1ll * pre[0][i] * C(pre[2][n], pre[0][i] + pre[2][i] - suf[1][i + 1]) +
1ll * pre[2][i] *
C(pre[2][n] - 1, pre[0][i] + pre[2][i] - suf[1][i + 1])) %
mod;
cout << ans << endl;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i++) {
long long l, r;
cin >> l >> r;
long long ans = 0;
int x = 0;
while ((r >> x) & 1) {
x++;
if (x == 60) break;
}
int y = 60;
while (((r >> y) & 1) == ((l >> y) & 1)) {
y--;
if (!y) break;
}
for (int j = 60; j > y; j--)
if ((r >> j) & 1ll) ans += (1ll << j);
if (y < x)
ans += (1ll << (y + 1)) - 1;
else
ans += (1ll << y) - 1;
cout << ans << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main() {
int alph[26];
int n, i, j, k, add;
long long int length;
scanf("%d", &n);
for (i = 0; i != n; i++) {
scanf("%d", &alph[i]);
}
for (i = 1; i != n; i++) {
k = alph[i];
for (j = i - 1; j >= 0 && alph[j] < k; j--) {
alph[j + 1] = alph[j];
}
alph[j + 1] = k;
}
add = length = alph[0];
for (i = 1; i != n && add != 0; i++) {
add = (alph[i] < add ? alph[i] : add - 1);
length += add;
}
printf("%lld\n", length);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c;
int main() {
cin >> n >> c >> b >> a;
n *= 2;
long long sum = 0;
for (int i = 0; i <= a; i++) {
for (int j = 0; j <= b; j++) {
if ((n - ((i * 4) + (j * 2)) <= c) && (n - ((i * 4) + (j * 2)) >= 0)) {
sum++;
}
}
}
cout << sum << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool wrap(const string& s, size_t width, size_t k) {
for (size_t init = 0, lines = 1, line_start = 0; init < s.size();) {
auto next = s.find_first_of("- ", init);
if (next > s.size()) next = s.size() - 1;
if (next + 1 - line_start > width) {
lines++;
if (lines > k) return false;
line_start = init;
} else
init = next + 1;
}
return true;
}
size_t minv = numeric_limits<size_t>::max();
bool bs(size_t from, size_t to, const string& s, size_t k) {
if (from == to) return false;
auto mid = (from + to) / 2;
if (wrap(s, mid, k)) {
if (mid < minv) minv = mid;
return bs(from, mid, s, k);
} else {
return bs(mid + 1, to, s, k);
}
}
int main() {
size_t k;
cin >> k;
char skip;
cin >> noskipws >> skip >> skipws;
string s;
getline(cin, s);
auto size = s.size();
bs(0, size + 1, s, k);
cout << minv << '\n';
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long int power(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int arr[258];
int flag[200005];
int main() {
string a, b, c;
cin >> a >> b;
for (int i = 0; i < b.size(); i++) arr[b[i]]++;
int c1 = 0, c2 = 0;
for (int i = 0; i < a.size(); i++) {
if (arr[a[i]] > 0) {
arr[a[i]]--;
c1++;
flag[i] = 1;
}
}
for (int i = 0; i < a.size(); i++) {
if (flag[i] == 0 && (arr[a[i] - 32] > 0 || arr[a[i] + 32] > 0)) {
c2++;
arr[a[i] - 32]--;
arr[a[i] + 32]--;
}
}
cout << c1 << " " << c2 << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void addmod(int &a, long long b) {
a = (a + b);
if (a >= 1000000007) a -= 1000000007;
}
void mulmod(int &a, long long b) { a = (a * b) % 1000000007; }
template <class T>
bool domin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <class T>
bool domax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
int gi() {
int a;
scanf("%d", &a);
return a;
}
long long gll() {
long long a;
scanf("%lld", &a);
return a;
}
void populate(string &st, set<char> &sc, int l, int r, int a, int b, int bi) {
cerr << l << " " << r << " " << bi << endl;
vector<bool> tkn(26, false);
if (l && l % (a + b) == 0) {
for (int i = max(0, bi - a); i < bi; i++)
if (st[i] != ' ') {
tkn[st[i] - 'a'] = true;
}
}
int ix = 0;
for (int i = bi, ti = l; ti <= r; i++, ti++) {
if (l < a)
st[i] = 'a' + ti, sc.insert('a' + ti);
else {
while (tkn[ix]) ix++;
st[i] = ('a' + ix);
sc.insert('a' + ix);
ix++;
}
}
}
int main() {
int a = gi(), b = gi(), l = gi() - 1, r = gi() - 1;
int rl = r - l + 1;
if (rl > 2 * (a + b)) {
return cout << a + max(1, a - b), 0;
}
string sans(rl, ' ');
set<char> sc;
int lm = (l % (a + b)), si = 0, li = l;
if (lm > 0) {
if (lm < a) {
populate(sans, sc, lm, a - 1, a, b, 0);
}
li += (a + b) - lm;
si += (a + b) - lm;
}
for (; li <= r; li += (a + b), si += (a + b)) {
populate(sans, sc, li, min(r, li + a - 1), a, b, si);
}
int ans = sc.size(), inc = 0;
for (int ix = 0; ix < rl; ix++)
if (sans[ix] == ' ') {
set<char> ts;
for (int j = ix + 1; j < rl && j < ix + a + 1; j++)
if (sans[j] != ' ') {
ts.insert(sans[j]);
}
if (ts.size() == sc.size()) inc = 1;
}
cout << ans + inc << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 1e4 + 50;
const int mod = 1e9 + 7;
using namespace std;
int n, k, x, f, r, i, j;
vector<pair<int, pair<int, int> > > vc;
long long rs;
struct nod {
int sz, key;
long long pr;
nod *l, *r;
nod(int key) {
this->key = key;
this->pr = rand();
this->sz = 1;
this->l = this->r = NULL;
}
};
typedef nod *pnod;
pnod nd, rt[nmax];
int sz(pnod t) { return t ? t->sz : 0; }
void upds(pnod t) {
if (!t) return;
t->sz = 1 + sz(t->l) + sz(t->r);
}
void split(pnod t, long long key, pnod &l, pnod &r) {
if (!t)
l = r = NULL;
else if (key < t->key) {
r = t;
split(t->l, key, l, t->l);
} else {
l = t;
split(t->r, key, t->r, r);
}
upds(t);
}
void ins(pnod &t, pnod x) {
if (!t)
t = x;
else if (t->pr > x->pr)
ins(x->key < t->key ? t->l : t->r, x);
else {
split(t, x->key, x->l, x->r);
t = x;
}
upds(t);
}
int sml(pnod t, int k) {
if (!t) return 0;
if (t->key < k)
return sz(t) - sz(t->r) + sml(t->r, k);
else
return sml(t->l, k);
}
int big(pnod t, int k) {
if (!t) return 0;
if (t->key > k)
return sz(t) - sz(t->l) + big(t->l, k);
else
return big(t->r, k);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr.tie(0);
cout.tie(0);
srand(time(0));
cin >> n >> k;
for (i = 1; i <= 1e4; i++) rt[i] = NULL;
for (i = 1; i <= n; i++) {
cin >> x >> r >> f;
vc.push_back(make_pair(r, make_pair(x, f)));
}
sort(vc.begin(), vc.end());
reverse(vc.begin(), vc.end());
for (i = 0; i < vc.size(); i++) {
r = vc[i].first, x = vc[i].second.first, f = vc[i].second.second;
for (j = max(f - k, 1); j <= min(f + k, 10000); j++) {
rs += 1LL * big(rt[j], x - r - 1) - 1LL * big(rt[j], x) +
1LL * sml(rt[j], x + r + 1) - 1LL * sml(rt[j], x);
}
nd = new nod(x);
ins(rt[f], nd);
}
cout << rs << endl;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
double power(double x, int n) {
if (n == 0) return 1.0;
if (n == 1) return x;
double t = power(x, n / 2);
t = t * t;
if (n % 2) return t * x;
return t;
}
int main() {
int m, n;
cin >> m >> n;
double ans = 0.0;
for (int i = 1; i <= m; i++) {
ans += i * (power(i / (double)m, n) - power((i - 1) / (double)m, n));
}
cout << fixed << setprecision(10);
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300011;
const int inf = 1e9;
int n;
set<int> st, pleft, pright;
int main() {
scanf("%d", &n);
st.insert(-inf);
st.insert(inf);
pleft.insert(inf);
pleft.insert(-inf);
pright.insert(inf);
pright.insert(-inf);
int x;
scanf("%d", &x);
st.insert(x);
for (int i = 2; i <= n; i++) {
scanf("%d", &x);
set<int>::iterator it1 = st.upper_bound(x);
set<int>::iterator it2 = it1;
it2--;
int l = (*it2), r = (*it1);
if (pright.find(l) == pright.end()) {
pright.insert(l);
printf("%d ", l);
} else {
pleft.insert(r);
printf("%d ", r);
}
st.insert(x);
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int N;
scanf("%d", &N);
int temp = N / 2;
if (N % 2 == 0) {
printf("%d\n", temp);
for (int i = 1; i <= temp; i++) {
printf("2 ");
}
} else {
printf("%d\n", temp);
for (int i = 1; i < temp; i++) {
printf("2 ");
}
printf("3");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
while (a[i] % 2 == 0) a[i] /= 2;
while (a[i] % 3 == 0) a[i] /= 3;
}
for (int i = 1; i < n; ++i) {
if (a[i] != a[i - 1]) {
cout << "NO";
return 0;
}
}
cout << "YES";
return 0;
}
| 5 |
#include <bits/stdc++.h>
long long mpoll(long long a, long long n, long long mod) {
long long ret = 1;
long long b = a;
while (n) {
if (n & 1) ret = (ret * b) % mod;
b = (b * b) % mod;
n >>= 1;
}
return (long long)ret;
}
using namespace std;
using namespace std;
int bit[2 * (int)1e5 + 5];
void update(int p, int val) {
for (; p < (int)1e5 + 5; p += (p & -p)) {
bit[p] += val;
}
}
int query(int p) {
int sum = 0;
for (; p != 0; p -= (p & -p)) {
sum += bit[p];
}
return sum;
}
map<int, vector<pair<int, int> > > hor;
map<int, vector<pair<int, int> > > ver;
int OTh[(int)1e5 + 5], OTv[(int)1e5 + 5];
map<int, int> H;
map<int, int> V;
map<int, int> C;
map<int, vector<pair<int, int> > > nH, nV;
vector<pair<int, pair<int, int> > > Q;
int main() {
int n;
cin >> n;
int h = 0, v = 0;
long long ans = 0;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (y1 == y2) {
if (H.find(y1) == H.end()) {
OTh[h] = y1;
H[y1] = h++;
C[y1] = 0;
}
hor[H[y1]].push_back(make_pair(min(x1, x2), max(x1, x2)));
} else {
if (V.find(x1) == V.end()) {
OTv[v] = x1;
V[x1] = v++;
C[x1] = 0;
}
ver[V[x1]].push_back(make_pair(min(y1, y2), max(y1, y2)));
}
}
for (int i = 0; i < h; i++) {
sort(hor[i].begin(), hor[i].end());
int x, y;
x = hor[i][0].first;
y = hor[i][0].second;
for (int j = 1; j < hor[i].size(); j++) {
if (hor[i][j].first > y) {
ans += (y - x + 1);
C[x] = 0;
C[y] = 0;
nH[i].push_back(make_pair(x, y));
x = hor[i][j].first;
y = hor[i][j].second;
} else {
y = max(y, hor[i][j].second);
}
}
ans += (y - x + 1);
nH[i].push_back(make_pair(x, y));
C[x] = 0;
C[y] = 0;
}
for (int i = 0; i < v; i++) {
sort(ver[i].begin(), ver[i].end());
int x, y;
x = ver[i][0].first;
y = ver[i][0].second;
for (int j = 1; j < ver[i].size(); j++) {
if (ver[i][j].first > y) {
ans += (y - x + 1);
C[x] = 0;
C[y] = 0;
nV[i].push_back(make_pair(x, y));
x = ver[i][j].first;
y = ver[i][j].second;
} else {
y = max(y, ver[i][j].second);
}
}
ans += (y - x + 1);
nV[i].push_back(make_pair(x, y));
C[x] = 0;
C[y] = 0;
}
int c = 1;
for (map<int, int>::iterator it = C.begin(); it != C.end(); it++) {
it->second = c++;
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < nH[i].size(); j++) {
Q.push_back(
make_pair(OTh[i], make_pair(C[nH[i][j].first], C[nH[i][j].second])));
}
}
for (int i = 0; i < v; i++) {
for (int j = 0; j < nV[i].size(); j++) {
Q.push_back(
make_pair(nV[i][j].first, make_pair(-(int)1e9 + 1, C[OTv[i]])));
Q.push_back(
make_pair(nV[i][j].second, make_pair((int)1e9 + 1, C[OTv[i]])));
}
}
sort(Q.begin(), Q.end());
for (int i = 0; i < Q.size(); i++) {
if (Q[i].second.first == -(int)1e9 + 1) {
ans += query(Q[i].second.second);
} else if (Q[i].second.first == (int)1e9 + 1) {
ans -= query(Q[i].second.second);
} else {
update(Q[i].second.first, 1);
update(Q[i].second.second + 1, -1);
}
}
cout << ans << endl;
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const long double PI = (long double)(3.1415926535897932384626433832795);
void solve() {
long long a, b, c, r;
cin >> a >> b >> c >> r;
if (a > b) {
swap(a, b);
}
long long left = c - r;
long long right = c + r;
if (right < a || left > b) {
cout << (b - a) << endl;
return;
}
long long ans1 = max((long long)0, left - a);
long long ans2 = max((long long)0, b - right);
cout << ans1 + ans2 << endl;
}
int main() {
long long t = 1;
cin >> t;
for (long long i = 0; i < t; ++i) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int idx, val;
};
bool cmp(data d1, data d2) { return d1.val < d2.val; }
int main() {
vector<int> v(26, 0);
string s;
int k;
cin >> s >> k;
int l = s.size();
for (int i = 0; i < l; i++) v[s[i] - 'a']++;
vector<data> w;
for (int i = 0; i < 26; i++) {
data d;
d.idx = i;
d.val = v[i];
w.push_back(d);
}
sort(w.begin(), w.end(), cmp);
string ans = s;
int sum = 0;
for (int i = 0; i < 26; i++) {
sum += w[i].val;
if (sum > k) break;
string temp;
int l = ans.size();
char ch = ('a' + w[i].idx);
for (int i = 0; i < l; i++)
if (ans[i] != ch) temp += ans[i];
ans = temp;
}
l = ans.size();
int count = 0;
map<char, int> m;
for (int i = 0; i < l; i++) {
char ch = ans[i] - 'a';
m[ch]++;
if (m[ch] == 1) count++;
}
cout << count << endl;
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
a[i] = make_pair(x, y);
}
vector<pair<int, int>> b(m);
vector<pair<int, int>> ua(n, {0, 0});
vector<pair<int, int>> ub(m, {0, 0});
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
if (x > y) swap(x, y);
b[i] = make_pair(x, y);
for (int j = 0; j < n; ++j) {
if (x == a[j].first && y != a[j].second) {
++ub[i].first;
++ua[j].first;
}
if (y == a[j].first) {
++ua[j].first;
++ub[i].second;
}
if (x != a[j].first && y == a[j].second) {
++ua[j].second;
++ub[i].second;
}
if (x == a[j].second) {
++ub[i].first;
++ua[j].second;
}
}
}
vector<bool> z(10, false);
for (int i = 0; i < n; ++i) {
if (ua[i].first > 0 && ua[i].second > 0) {
cout << -1;
return 0;
}
if (ua[i].first > 0) {
z[a[i].first] = 1;
}
if (ua[i].second > 0) {
z[a[i].second] = 1;
}
}
for (int i = 0; i < m; ++i) {
if (ub[i].first > 0 && ub[i].second > 0) {
cout << -1;
return 0;
}
}
int k = 0;
int ans = 0;
for (int i = 1; i <= 9; ++i) {
if (z[i]) {
++k;
ans = i;
}
}
cout << (k > 1 ? 0 : ans);
return 0;
}
| 11 |
#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;
}
vector<long long> divisors(long long n) {
vector<long long> d;
int i;
for (i = 1; i * i < n; ++i) {
if (n % i == 0) d.push_back(i), d.push_back(n / i);
}
if (n % i == 0) d.push_back(i);
return d;
}
int main() {
long long n;
cin >> n;
vector<long long> v = divisors(n);
sort(v.begin(), v.end());
long long ans = 1e18;
for (int i = 0; i < v.size(); ++i) {
for (int j = i; j < v.size(); ++j) {
if (v[i] * v[j] > n) break;
long long x = n / (v[i] * v[j]);
long double y = n * 1.00 / (v[i] * 1.00 * v[j] * 1.00);
if (x == y) {
long long a = min(v[i], x);
long long b = max(v[i], x);
ans = min(ans, (a + 1) * (v[j] + 2) * (b + 2) - n);
}
}
}
long long Max = 1ll * (n + 1) * 1ll * 3 * 1ll * 3 - n;
cout << ans << " " << Max;
return 0;
}
| 8 |
#include <bits/stdc++.h>
const int MAXN = 110;
const int W = 100;
const int P = 998244353;
void exgcd(int a, int b, int& x, int& y) {
if (b == 0)
x = 1, y = 0;
else
exgcd(b, a % b, y, x), y -= a / b * x;
}
inline int inv(int a) {
int x, y;
exgcd(a, P, x, y);
return (x % P + P) % P;
}
int a[MAXN], w[MAXN];
int f[MAXN][MAXN][MAXN];
int g[MAXN][MAXN][MAXN];
int main() {
int n, m, sa = 0, sb = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &w[i]);
for (int i = 1; i <= n; i++)
if (a[i])
sa += w[i];
else
sb += w[i];
for (int k = W; k >= 0; k--)
for (int i = m; i >= 0; i--)
for (int j = m - i; j >= 0; j--) {
if (i + j == m)
f[k][i][j] = k;
else
f[k][i][j] = (1ll * k * f[k + 1][i + 1][j] +
1ll * (sa + i - k) * f[k][i + 1][j] +
1ll * (sb - j) * f[k][i][j + 1]) %
P * inv(sa + i + sb - j) % P;
}
for (int k = 1; k <= W; k++)
for (int i = m; i >= 0; i--)
for (int j = m - i; j >= 0; j--) {
if (i + j == m)
g[k][i][j] = k;
else
g[k][i][j] =
(1ll * k * g[k - 1][i][j + 1] + 1ll * (sa + i) * g[k][i + 1][j] +
1ll * (sb - j - k) * g[k][i][j + 1]) %
P * inv(sa + i + sb - j) % P;
}
for (int i = 1; i <= n; i++) {
if (a[i])
printf("%d\n", f[w[i]][0][0]);
else
printf("%d\n", g[w[i]][0][0]);
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char s[2005][2005];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
int ans = 0;
for (int i = 1; i <= n; i++) {
int cnt = 0;
for (int j = 1; j <= m + 1; j++) {
if (s[i][j] == '.')
cnt++;
else {
if (cnt >= k) ans += cnt - k + 1;
cnt = 0;
}
}
}
if (k > 1) {
for (int j = 1; j <= m; j++) {
int cnt = 0;
for (int i = 1; i <= n + 1; i++) {
if (s[i][j] == '.')
cnt++;
else {
if (cnt >= k) ans += cnt - k + 1;
cnt = 0;
}
}
}
}
printf("%d", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long int x[100009];
int main() {
int n, k, i;
scanf("%d%d", &n, &k);
if (n == 1) {
if (k == 0)
printf("1\n");
else
printf("-1\n");
return 0;
}
if (k < n / 2) {
printf("-1\n");
return 0;
}
for (i = 1; i <= n - (2 + (n & 1)); i++) {
printf("%d ", i);
}
k -= (n / 2) - 1;
i--;
if (k > i) {
printf("%d %d ", k, 2 * k);
if (n & 1) printf("%d\n", (2 * k) + 1);
} else {
int p = i / k;
p++;
printf("%d %d ", p * k, (p + 1) * k);
if (n & 1) printf("%d\n", ((p + 1) * k) + 1);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
const int N = 100005;
const int M = 200010;
using namespace std;
int n, T;
int fa[N][20], dep[N];
int cnt = 0, dfn[N], las[N];
long long a[N], sum[N], val[N];
int edge[M], lst[N], nxt[M], t = 0;
int lowbit(int x) { return x & (-x); }
void UPD(int x, long long num) {
if (num == 0) return;
for (; x <= n; x += lowbit(x)) val[x] += num;
}
long long GET(int x) {
long long ans = 0;
while (x > 0) {
ans += val[x];
x -= lowbit(x);
}
return ans;
}
void ADD(int x, int y) {
edge[++t] = y;
nxt[t] = lst[x];
lst[x] = t;
}
void READ() {
int u, v;
scanf("%d%d", &n, &T);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
if (a[i] < 0) a[i] = -a[i];
}
for (int i = 1; i <= n - 1; i++) {
scanf("%d%d", &u, &v);
ADD(u, v);
ADD(v, u);
}
}
void SEARCH(int x, int pa) {
dfn[x] = ++cnt;
fa[x][0] = pa;
dep[x] = dep[pa] + 1;
sum[x] = sum[pa] + a[x];
for (int r = lst[x]; r; r = nxt[r]) {
if (edge[r] == pa) continue;
SEARCH(edge[r], x);
}
las[x] = cnt;
}
void INIT() {
cnt = 0;
dep[0] = 0;
sum[0] = 0;
SEARCH(1, 0);
for (int i = 1; i <= 18; i++) {
for (int x = 1; x <= n; x++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
}
long long VAL(int x) { return sum[x] + GET(dfn[x]); }
int LCA(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int de = dep[y] - dep[x];
for (int i = 18; i >= 0; i--) {
if ((de >> i) & 1) y = fa[y][i];
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
}
return fa[x][0];
}
void SOLVE() {
int pd, u;
scanf("%d%d", &pd, &u);
if (pd == 1) {
long long v;
scanf("%lld", &v);
if (v < 0) v = -v;
long long now = v - a[u];
a[u] = v;
UPD(dfn[u], now);
UPD(las[u] + 1, -now);
}
if (pd == 2) {
int v;
scanf("%d", &v);
long long ans = VAL(u) + VAL(v) - 2 * VAL(LCA(u, v)) + a[LCA(u, v)];
ans *= 2;
ans -= a[u] + a[v];
printf("%lld\n", ans);
}
}
int main() {
READ();
INIT();
while (T--) SOLVE();
return 0;
}
| 15 |
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
int n, m;
char s[200005];
int mp[3][200005];
struct node {
int ab, aa, bb, ba;
void clear() { ab = aa = bb = ba = inf; }
} tr[200005 * 4];
node pushup(node l, node r) {
node res;
res.aa = min(inf, min(l.aa + r.aa, l.ab + r.ba) + 1);
res.ab = min(inf, min(l.aa + r.ab, l.ab + r.bb) + 1);
res.ba = min(inf, min(l.bb + r.ba, l.ba + r.aa) + 1);
res.bb = min(inf, min(l.bb + r.bb, l.ba + r.ab) + 1);
return res;
}
void build(int rt, int l, int r) {
if (l == r) {
tr[rt].clear();
if (mp[1][l] == 0 && mp[2][l] == 0) tr[rt].ab = tr[rt].ba = 1;
if (mp[1][l] == 0) tr[rt].aa = 0;
if (mp[2][l] == 0) tr[rt].bb = 0;
return;
}
build(rt << 1, l, ((l + r) >> 1));
build(rt << 1 | 1, ((l + r) >> 1) + 1, r);
tr[rt] = pushup(tr[rt << 1], tr[rt << 1 | 1]);
}
node query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[rt];
if (R <= ((l + r) >> 1))
return query(rt << 1, l, ((l + r) >> 1), L, R);
else if (L > ((l + r) >> 1))
return query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R);
return pushup(query(rt << 1, l, ((l + r) >> 1), L, R),
query(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R));
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= 2; ++i) {
scanf("%s", s);
for (int j = 1; j <= n; ++j) mp[i][j] = (s[j - 1] == 'X') ? 1 : 0;
}
build(1, 1, n);
for (int i = 1, l, r; i <= m; ++i) {
scanf("%d%d", &l, &r);
int x = l, y = r, ans;
if (x > n) x -= n;
if (y > n) y -= n;
if (x > y) {
swap(x, y);
swap(l, r);
}
node p = query(1, 1, n, x, y);
if (l > n && r > n) ans = p.bb;
if (l > n && r <= n) ans = p.ba;
if (l <= n && r <= n) ans = p.aa;
if (l <= n && r > n) ans = p.ab;
printf("%d\n", ans < inf ? ans : -1);
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 200005;
void solve() {
int n, h, m, k;
cin >> n >> h >> m >> k;
vector<int> v;
m /= 2;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
y++;
v.push_back({y % m});
int yy = v.back() + k - 1;
if (yy < m) {
mp[v.back()]++;
mp[yy]--;
} else {
mp[v.back()]++;
mp[0]++;
mp[yy - m]--;
}
}
int mi = 1e9;
int ans = 0;
int cur = 0;
if (!mp[0]) {
mi = 0;
ans = 0;
} else {
for (auto &i : mp) {
cur += i.second;
if (cur < mi) {
mi = cur;
ans = i.first;
}
}
}
cout << mi << ' ' << ans << '\n';
for (int i = 0; i < n; i++) {
int x = v[i];
if (v[i] <= ans && v[i] + k - 1 > ans) {
cout << i + 1 << ' ';
} else if (v[i] + k - 1 > m && v[i] + k - 1 - m > ans) {
cout << i + 1 << ' ';
}
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
} else {
cin >> t;
}
while (t--) {
solve();
}
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int readInt() {
int x;
if (scanf(" %d", &x) != EOF) return x;
return -1;
}
long long int readLint() {
long long int x;
if (cin >> x) return x;
return -1;
}
string readString() {
string s;
if (cin >> s) return s;
return "";
}
struct Operation {
char op;
int left, right;
Operation(char op, int left, int right) : op(op), left(left), right(right) {}
bool equals(const Operation& that) {
return op == that.op && left == that.left && right == that.right;
}
void print() { printf("{op = %c, left = %d, right = %d}", op, left, right); }
};
const string RES("res");
struct Program {
vector<Operation> nodes;
set<string> allNames;
map<int, int> values;
map<int, string> varNames;
map<string, int> varIndex;
Program() {
for (int i = 0; i < 26; ++i)
for (int j = 0; j < 26; ++j)
for (int k = 0; k < 10; ++k) {
string cur = "";
cur += (char)('a' + i);
cur += (char)('a' + j);
cur += (char)('a' + k);
allNames.insert(cur);
}
}
void readProgram(int numLines) {
for (int i = 0; i < numLines; ++i) {
interpretLine(readString());
}
}
void writeProgram() {
int root = getVarIndex(RES);
vector<string> ret = write(root);
cout << ret.size() << endl;
for (auto& line : ret) cout << line << endl;
}
vector<string> write(int root) {
int a = getValue(root);
if (a == root) {
return vector<string>();
}
if (a < 0) {
string name = varNames[a];
return vector<string>(1, "res=" + name);
}
vector<string> ret;
vector<string> names(nodes.size(), "");
writeInto(ret, names, a, true);
return ret;
}
string getName(vector<string>& names, int idx) {
if (idx < 0) return varNames[idx];
return names[idx];
}
string generateName() {
while (true) {
string cand = *allNames.begin();
allNames.erase(allNames.begin());
if (!varIndex.count(cand)) return cand;
}
assert(false);
}
void writeInto(vector<string>& ret, vector<string>& names, int idx,
bool first = false) {
if (idx < 0) {
return;
}
string& R = names[idx];
if (!R.empty()) return;
if (first) {
R = "res";
} else {
R = generateName();
}
Operation op = nodes[idx];
writeInto(ret, names, op.left);
writeInto(ret, names, op.right);
string line = R + "=";
line += getName(names, op.left);
line += op.op;
line += getName(names, op.right);
ret.push_back(line);
}
int getVarIndex(const string& s) {
auto it = varIndex.find(s);
if (it != varIndex.end()) return it->second;
int idx = -1 - (int)(varIndex.size());
varIndex[s] = idx;
varNames[idx] = s;
values[idx] = idx;
return idx;
}
int getValue(int x) { return values[x]; }
void interpret(int aIdx, int bIdx) { values[aIdx] = getValue(bIdx); }
void interpret(int aIdx, char c, string& left, string& right) {
int lIdx = getVarIndex(left);
int rIdx = getVarIndex(right);
Operation op(c, getValue(lIdx), getValue(rIdx));
int opIdx = -1;
for (int i = 0; i < nodes.size(); ++i)
if (nodes[i].equals(op)) {
opIdx = i;
break;
}
if (opIdx < 0) {
opIdx = nodes.size();
nodes.push_back(op);
}
values[aIdx] = opIdx;
}
void interpretLine(string s) {
int eq = s.find_first_of("=");
assert(eq >= 0);
string a = s.substr(0, eq);
int aIdx = getVarIndex(a);
string rem = s.substr(eq + 1);
int idx = rem.find_first_of("$^#&");
if (idx >= 0) {
string left = rem.substr(0, idx);
string right = rem.substr(idx + 1);
char c = rem[idx];
interpret(aIdx, c, left, right);
} else {
string b = rem;
int bIdx = getVarIndex(b);
interpret(aIdx, bIdx);
}
}
void print() {
for (auto& var : varIndex) {
printf("Variable %s. Index = %d, value = %d\n", var.first.c_str(),
var.second, getValue(var.second));
}
for (int i = 0; i < nodes.size(); ++i) {
printf("Node %d: ", i);
nodes[i].print();
cout << endl;
}
}
};
struct SingleCase {
bool solveCase() {
int n = readInt();
if (n < 0) return false;
Program program;
program.readProgram(n);
program.writeProgram();
return true;
}
};
int main() {
while (SingleCase().solveCase()) {
}
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
char s[5005];
int n;
bool dp[5005][5005];
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
int m = (1 << 31 - __builtin_clz(n)) - 1;
int left = n - m, p;
for (int i = 0; i <= n - left; i++) dp[i][i] = 1;
char ch = 'z' + 1;
for (int i = 1; i <= n; i++)
if (dp[i - 1][i - 1] && s[i] < ch) {
ch = s[i];
p = i;
}
for (int i = 1; i <= left; i++) {
dp[p][p - i] = 1;
int up = n - left + i, k;
for (int j = p + 1; j <= up; j++) {
k = j - i;
if (dp[j - 1][k] && ch == s[j]) dp[j][k] = 1;
for (int p = k; p && !dp[j][k]; p -= p & -p) {
int u = 1 << __builtin_ctz(p);
if (dp[j - u][k - u]) {
dp[j][k] = 1;
break;
}
}
}
ch = 'z' + 1;
for (int j = i + 1; j <= n; j++)
if (dp[j - 1][j - i - 1] && s[j] < ch) {
ch = s[j];
p = j;
}
}
for (int i = 1; i <= left; i++) {
for (int j = i; j <= n; j++)
if (dp[j][j - i]) {
putchar(s[j]);
break;
}
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
int n, sz[N], par[N];
vector<int> adj[N];
bool mark[N];
void dfs1(int u) {
sz[u] = 1;
for (auto v : adj[u])
if (v ^ par[u]) {
par[v] = u, dfs1(v);
sz[u] += sz[v];
}
}
void dfs2(int u, int x) {
mark[u] |= n - sz[u] - x <= n / 2;
for (auto v : adj[u])
if (v ^ par[u]) dfs2(v, x);
}
inline void read_input() {
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
adj[--u].push_back(--v);
adj[v].push_back(u);
}
}
inline void solve() {
dfs1(0);
for (int u = 0; u < n; u++) {
bool flag = n - sz[u] <= n / 2;
for (auto v : adj[u])
if (v ^ par[u]) flag &= sz[v] <= n / 2;
if (!flag) continue;
mark[u] = true;
dfs1(par[u] = u);
int mx1 = 0, mx2 = 0;
for (auto v : adj[u])
if (sz[v] > mx1)
mx2 = mx1, mx1 = sz[v];
else
mx2 = max(mx2, sz[v]);
for (auto v : adj[u]) dfs2(v, sz[v] ^ mx1 ? mx1 : mx2);
}
}
inline void write_output() {
for (int u = 0; u < n; u++) cout << mark[u] << ' ';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
read_input(), solve(), write_output();
return 0;
}
| 15 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:667772160")
using namespace std;
struct __isoff {
__isoff() {
if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
srand(time(0));
}
~__isoff() {}
} __osafwf;
const unsigned long long p1 = 31;
const unsigned long long p2 = 29;
const double eps = 1e-8;
const double pi = acos(-1.0);
const long long inf = 1e17 + 7;
const int infi = 1e9 + 7;
const long long dd = 2e5 + 7;
const long long mod = 1e9 + 7;
int n, m;
vector<pair<int, int> > T[dd];
vector<pair<int, int> > G[dd];
vector<pair<int, int> > Hg[dd];
bool B[dd], Is[dd];
int S[dd];
int ti = 1;
int Ss[dd];
int tis = 1;
vector<int> Y;
pair<bool, int> dfs(int v, int op = -23123123) {
if (Ss[v] == tis) {
return make_pair(1, 0);
}
Y.push_back(v);
Ss[v] = tis;
pair<bool, int> T = make_pair(0, 0);
for (long long i = 0; i < (long long)G[v].size(); i++) {
if (G[v][i].second != op) {
auto ts = dfs(G[v][i].first, G[v][i].second);
if (ts.first) {
if (ts.second == 0)
T = make_pair(1, G[v][i].second);
else
T = ts;
}
}
}
return T;
}
int zs;
set<pair<int, int> > M;
void gg(int v) {
S[v] = ti;
for (long long i = 0; i < (long long)G[v].size(); i++) {
if (G[v][i].second != zs && S[G[v][i].first] != ti) {
B[G[v][i].first] = 1;
S[G[v][i].first] = ti;
Is[G[v][i].second] = 1 ^ M.count(make_pair(v, G[v][i].second));
gg(G[v][i].first);
}
}
}
int Si[dd];
int Df[dd];
bool sd[dd];
bool Zs[dd];
bool Bl[dd];
vector<int> SD[dd];
map<pair<int, int>, vector<int> > Gs;
int main() {
cin >> n >> m;
for (long long i = 0; i < (long long)n; i++) {
int k;
scanf("%d", &k);
for (long long j = 0; j < (long long)k; j++) {
int t;
scanf("%d", &t);
T[abs(t)].push_back(make_pair(i, t));
Hg[abs(t)].push_back(make_pair(i, t));
SD[i].push_back(t);
if (t > 0) M.insert(make_pair(i, t));
}
}
for (long long i = 0; i < (long long)m + 1; i++) {
if (T[i].size() == 2 && T[i][0].second != T[i][1].second) {
int a = T[i][0].first, b = T[i][1].first;
if (a > b) swap(a, b);
Gs[make_pair(a, b)].push_back(i);
}
}
for (auto t : Gs) {
if (t.second.size() >= 2) {
int a = t.second[0];
int b = t.second[1];
int q = t.first.first;
int w = t.first.second;
B[q] = B[w] = 1;
Is[a] = M.count(make_pair(q, a));
Is[b] = M.count(make_pair(w, b));
for (auto tt : t.second) {
Zs[tt] = 1;
Bl[tt] = 1;
}
}
}
for (long long i = 0; i < (long long)m + 1; i++) {
if (T[i].size() == 2 && T[i][0].second == T[i][1].second) {
B[T[i][0].first] = 1;
B[T[i][1].first] = 1;
Is[i] = i == T[i][0].second;
Zs[i] = 1;
Bl[i] = 1;
}
}
for (long long i = 0; i < (long long)m + 1; i++) {
if (!Bl[i]) {
vector<pair<int, int> > tm;
for (long long j = 0; j < (long long)T[i].size(); j++) {
if (!B[T[i][j].first]) {
Df[T[i][j].first]++;
Si[i]++;
tm.push_back(T[i][j]);
}
}
T[i] = tm;
} else {
T[i] = vector<pair<int, int> >();
}
}
vector<int> TS;
for (long long i = 0; i < (long long)m + 1; i++) {
int t = 0, gg = -1;
for (long long j = 0; j < (long long)T[i].size(); j++) {
t += !B[T[i][j].first];
if (!B[T[i][j].first]) gg = j;
}
if (t == 1) {
TS.push_back(i);
Zs[i] = 1;
}
}
for (long long i = 0; i < (long long)n; i++) {
if (Df[i] == 1) {
int g = i;
for (long long i = 0; i < (long long)SD[g].size(); i++) {
if (Si[abs(SD[g][i])] > 0 && !Zs[abs(SD[g][i])]) {
Zs[abs(SD[g][i])] = 1;
TS.push_back(abs(SD[g][i]));
}
}
}
}
while (!TS.empty()) {
int i = TS.back();
TS.pop_back();
int t = 0, gg = -1;
for (long long j = 0; j < (long long)T[i].size(); j++) {
t += !B[T[i][j].first];
if (!B[T[i][j].first]) gg = j;
}
if (t == 1) {
int g = T[i][gg].first;
B[g] = 1;
Df[g] = infi;
if (!Bl[i]) Is[i] = i == T[i][gg].second, Bl[i] = 1;
for (long long i = 0; i < (long long)SD[g].size(); i++) {
Si[abs(SD[g][i])]--;
if (Si[abs(SD[g][i])] == 1 && !Zs[abs(SD[g][i])]) {
Zs[abs(SD[g][i])] = 1;
TS.push_back(abs(SD[g][i]));
}
}
if (T[i].size() == 2) {
g = T[i][!gg].first;
Df[g]--;
if (Df[g] == 1) {
for (long long i = 0; i < (long long)SD[g].size(); i++) {
if (Si[abs(SD[g][i])] > 0 && !Zs[abs(SD[g][i])]) {
Zs[abs(SD[g][i])] = 1;
TS.push_back(abs(SD[g][i]));
}
}
}
}
} else {
if (t == 2) {
int g, q;
if (Df[T[i][0].first] == 1) {
g = T[i][0].first;
q = 0;
} else {
g = T[i][1].first;
q = 1;
}
B[g] = 1;
Df[g] = infi;
if (!Bl[i]) Is[i] = i == T[i][q].second, Bl[i] = 1;
g = T[i][!q].first;
Df[g]--;
if (Df[g] == 1) {
for (long long i = 0; i < (long long)SD[g].size(); i++) {
if (Si[abs(SD[g][i])] > 0 && !Zs[abs(SD[g][i])]) {
Zs[abs(SD[g][i])] = 1;
TS.push_back(abs(SD[g][i]));
}
}
}
}
}
}
for (long long i = 0; i < (long long)m + 1; i++) {
int t = 0;
for (long long j = 0; j < (long long)T[i].size(); j++) {
t += !B[T[i][j].first];
}
if (t == 2 && !Zs[i]) {
G[T[i][0].first].push_back(make_pair(T[i][1].first, i));
G[T[i][1].first].push_back(make_pair(T[i][0].first, i));
}
}
for (long long i = 0; i < (long long)n; i++) {
if (Ss[i] != tis) {
Y.clear();
auto t = dfs(i);
if (t.first) {
zs = t.second;
Is[t.second] = M.count(make_pair(T[t.second][0].first, t.second));
B[T[t.second][0].first] = 1;
gg(T[t.second][0].first);
}
}
}
for (long long i = 0; i < (long long)m + 1; i++) {
bool d = Is[i];
for (long long j = 0; j < (long long)Hg[i].size(); j++) {
if ((Hg[i][j].second == i) == d) sd[Hg[i][j].first] = 1;
}
}
for (long long i = 0; i < (long long)n; i++)
if (!sd[i]) {
cout << "NO";
return 0;
}
cout << "YES\n";
for (long long i = 0; i < (long long)m; i++) {
cout << Is[i + 1];
}
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int r, n, dp[105][5][5];
pair<int, int> f[105];
void load() {
cin >> r >> n;
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
a--, b--;
f[i] = make_pair(a, b);
}
sort(f, f + n);
}
int go(int len, int up, int down) {
if (up == 2 && down == 2) {
return go(len, up - 1, down - 1);
}
if (up > down) {
return go(len, down, up);
}
if (len == 0) return 0;
if (len == 1) {
return (!up || !down || up == down);
}
int &res = dp[len][up][down];
if (res != -1) return res;
res = 0;
int was[105];
memset(was, 0, sizeof(was));
for (int i = 1; i < len - 1; i++) {
was[go(i, up, 1) ^ go(len - i - 1, 1, down)] = 1;
was[go(i, up, 2) ^ go(len - i - 1, 2, down)] = 1;
}
if (up != 0 || down != 0) {
was[go(len - 1, up, down)] = 1;
}
if (up == 0) {
was[go(len - 1, 1, down)] = 1;
was[go(len - 1, 2, down)] = 1;
}
if (down == 0) {
was[go(len - 1, up, 1)] = 1;
was[go(len - 1, up, 2)] = 1;
}
for (int i = 0; i <= 100; i++) {
if (!was[i]) {
res = i;
break;
}
}
return res;
}
void solve() {
memset(dp, -1, sizeof(dp));
int res = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
res ^= go(f[i].first, 0, f[i].second + 1);
} else {
res ^= go(f[i].first - f[i - 1].first - 1, f[i - 1].second + 1,
f[i].second + 1);
}
}
if (n == 0) {
res ^= go(r, 0, 0);
} else {
res ^= go(r - f[n - 1].first - 1, f[n - 1].second + 1, 0);
}
if (!res) {
puts("LOSE");
} else {
puts("WIN");
}
}
int main() {
load();
solve();
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[3];
char dir[3] = {'P', 'S', 'R'};
int dp[N];
const int INF = 0x3f3f3f3f;
int main() {
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
a[0] = a[1] = a[2] = 0;
string ans = "";
for (int i = 0; i < str.size(); i++) {
if (str[i] == 'R') {
a[0]++;
} else if (str[i] == 'P') {
a[1]++;
} else {
a[2]++;
}
}
string res = "";
int maxn = max(a[0], max(a[1], a[2]));
for (int i = 0; i < 3; i++) {
if (a[i] == maxn) {
for (int j = 0; j < str.size(); j++) {
res += dir[i];
}
break;
}
}
cout << res << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 9;
const int SM = 1e3 + 8;
const int logN = 20, BL = 400;
const double EPS = 3e-16;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long q;
cin >> q;
while (q--) {
long long n;
cin >> n;
vector<pair<long long, long long>> p(n);
for (auto &i : p) cin >> i.first >> i.second;
long long cur = p[0].first;
for (long long i = 0; i < n; i++) {
if (p[i].first <= cur && cur <= p[i].second) {
cout << cur << " ";
cur++;
} else if (cur < p[i].first) {
cur = p[i].first;
cout << cur << " ";
cur++;
} else {
cout << "0 ";
}
}
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, x, a[105], y, z, ft = 0;
cin >> n >> k >> x;
for (int i = 0; i < n; i++) {
cin >> a[i];
y += a[i];
if (i < n - k) {
ft += a[i];
}
}
z = ft + (k * x);
if (z < y) {
cout << z;
} else {
cout << y;
}
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:128000000")
using namespace std;
void solve();
void precalc();
int TESTNUM = 0;
int main() {
string s = "";
cin.tie(0);
cout.precision(10);
cout << fixed;
precalc();
int t = 1;
while (t--) {
++TESTNUM;
solve();
}
return 0;
}
template <typename T>
T binpow(T q, T w, T mod) {
if (!w) return 1 % mod;
if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;
return binpow(q * 1LL * q % mod, w / 2, mod);
}
void precalc() {}
template <typename T>
T gcd(T q, T w) {
while (w) {
q %= w;
swap(q, w);
}
return q;
}
template <typename T>
T lcm(T q, T w) {
return q / gcd(q, w) * w;
}
inline int nxt() {
int x;
cin >> x;
return x;
}
const long long mod = 1000000007;
struct Num {
long long a, b;
Num(long long x = 0, long long y = 0) {
a = x;
b = y;
}
Num operator+(const Num& other) const {
long long na = a + other.a;
long long nb = b + other.b;
if (na >= mod) na -= mod;
if (nb >= mod) nb -= mod;
return Num(na, nb);
}
Num operator-(const Num& other) const {
long long na = a - other.a;
long long nb = b - other.b;
if (na < 0) na += mod;
if (nb < 0) nb += mod;
return Num(na, nb);
}
Num operator*(const Num& other) const {
long long na = (1ll * a * other.a + 5ll * b * other.b) % mod;
long long nb = (1ll * a * other.b + 1ll * b * other.a) % mod;
if (na < 0) na += mod;
if (nb < 0) nb += mod;
return Num(na, nb);
}
long long norm() const {
long long tmp = (1ll * a * a - 5ll * b * b) % mod;
if (tmp < 0) tmp += mod;
return tmp;
}
Num overline() const { return (b == 0) ? *this : Num(a, mod - b); }
Num operator/(const Num& other) const {
long long inv = other.norm();
inv = binpow<long long>(inv, mod - 2, mod);
Num res = (*this * other.overline());
res.a = 1ll * res.a * inv % mod;
res.b = 1ll * res.b * inv % mod;
return res;
}
void out() const {
if (a && b) {
if (b == 1)
cerr << a << " + sqrt(5)\n";
else
cerr << a << " + " << b << " * sqrt(5)\n";
} else {
if (b) {
if (b == 1)
cerr << "sqrt(5)\n";
else
cerr << b << " * sqrt(5)\n";
} else
cerr << a << "\n";
}
}
};
Num pw(Num a, long long n) {
Num res(1);
while (n) {
if (n & 1ll) res = res * a;
n >>= 1;
a = a * a;
}
return res;
}
Num sum(const Num& x, long long l, long long r) {
if (x.a == 1 && x.b == 0) return (r - l + 1) % mod;
return pw(x, l) * (pw(x, r - l + 1) - 1) / (x - 1);
}
const long long K = 222;
long long C[K][K];
vector<long long> polymult(const vector<long long>& p1,
const vector<long long>& p2) {
vector<long long> res;
for (long long i = 0; i < (long long)p1.size(); i++) {
for (long long j = 0; j < (long long)p2.size(); j++) {
if (res.size() <= i + j) res.push_back(0);
res[i + j] += p1[i] * p2[j] % mod;
}
}
for (auto& x : res) {
x %= mod;
if (x < 0) x += mod;
}
return res;
}
long long stupid(long long k, long long l, long long r) {
long long index = 1;
l += 2, r += 2;
long long x = 0;
long long y = 1;
long long ans = 0;
while (index <= r) {
if (index >= l) {
long long cur = 1;
for (long long i = 0; i < k; i++) {
cur =
cur * (y - i) % mod * binpow<long long>(i + 1, mod - 2, mod) % mod;
}
ans += cur;
}
x = (x + y) % mod;
swap(x, y);
index++;
}
return ans % mod;
}
long long not_stupid(long long k, long long l, long long r) {
Num phi_1(1, 1);
Num phi_2(1, mod - 1);
phi_1 = phi_1 / 2;
phi_2 = phi_2 / 2;
Num denom = Num(1) / Num(0, 1);
l += 2;
r += 2;
for (long long i = 0; i < K; i++) {
C[i][0] = C[i][i] = 1;
for (long long j = 1; j < i; j++) {
C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
if (C[i][j] >= mod) C[i][j] -= mod;
}
}
vector<Num> sm(k + 1);
Num curdenom(1);
for (long long d = 0; d <= k; d++) {
Num cur = pw(phi_1, d);
long long co = 1;
for (long long i = 0; i <= d; i++) {
sm[d] = sm[d] + sum(cur, l, r) * C[d][i] * co;
co = mod - co;
cur = cur * phi_2 / phi_1;
}
sm[d] = sm[d] * curdenom;
curdenom = curdenom * denom;
}
vector<long long> poly(1, 1);
for (long long i = 0; i < k; i++) {
poly = polymult(poly, {-i, 1});
}
Num ans(0);
for (long long i = 0; i <= k; i++) {
ans = ans + sm[i] * poly[i];
}
long long fact = 1;
for (long long i = 1; i <= k; i++) fact = fact * i % mod;
return (ans / fact).a;
}
void solve() {
long long k;
cin >> k;
long long l, r;
cin >> l >> r;
long long res = not_stupid(k, l, r) % mod;
if (res < 0) {
res += mod;
}
cout << res << "\n";
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c;
int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
return x;
}
bool check(int d, int e, int f) {
if (e > 12 || f > 31) return 0;
int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (!(d % 4)) mon[2] = 29;
if (f > mon[e] || a - d < 18) return 0;
if (a - d > 18) return 1;
if (e < b) return 1;
if (e > b) return 0;
if (f > c) return 0;
if (f <= c) return 1;
}
int main() {
c = read(), b = read(), a = read();
int d = read(), e = read(), f = read();
if (check(d, e, f) || check(d, f, e) || check(e, d, f) || check(e, f, d) ||
check(f, d, e) || check(f, e, d))
cout << "YES";
else
cout << "NO";
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int n, i, j, k, l;
long long a[300001], ans, s;
long long qpower(long long a, int b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % 998244353;
a = a * a % 998244353;
b >>= 1;
}
return ans;
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n * 2; i++) scanf("%lld", &a[i]);
sort(a + 1, a + n * 2 + 1);
for (i = 1; i <= n; i++) ans = ((ans) + (-a[i])) % 998244353;
for (i = n + 1; i <= n + n; i++) ans = ((ans) + (a[i])) % 998244353;
for (i = 1; i <= n + n; i++) ans = ans * i % 998244353;
s = 1;
for (i = 1; i <= n; i++) s = s * i % 998244353;
s = qpower(s, 998244351);
ans = (ans + 998244353) % 998244353;
printf("%lld\n", ans * s % 998244353 * s % 998244353);
fclose(stdin);
fclose(stdout);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long infll = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn = 1e5 + 5;
const int maxm = 1.5e7 + 5;
using namespace std;
struct node {
double x, y;
} p[305];
int n;
double cal(node p0, node p1, node p2) {
return (p0.x - p1.x) * (p2.y - p1.y) - (p0.y - p1.y) * (p2.x - p1.x);
}
int main() {
double ans = 0.0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i].x >> p[i].y;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
double ans1 = 0, ans2 = 0;
for (int k = 1; k <= n; k++) {
double tmp = cal(p[i], p[j], p[k]);
if (tmp > 0)
ans1 = max(ans1, tmp / 2.0);
else if (tmp < 0)
ans2 = max(ans2, -tmp / 2.0);
}
if (ans1 > 0 && ans2 > 0) ans = max(ans, ans1 + ans2);
}
}
printf("%.8f\n", ans);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string x;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x;
if (x[x.length() - 1] == 'o')
cout << "FILIPINO\n";
else if (x[x.length() - 1] == 'u')
cout << "JAPANESE\n";
else
cout << "KOREAN\n";
}
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
const long long mod = (long long)1e9 + 7;
const long long MOD = (long long)998244353;
const long long INF = (long long)1e18;
inline long long add(long long a, long long b) {
return (a + b >= mod ? a + b - mod : a + b);
}
inline long long mul(long long a, long long b) {
return (long long)a * b % mod;
}
const long long N = 2e5 + 5;
vector<long long> g[N];
long long w[N], dp[N], vis[N];
void dfs(long long node, long long par = -1) {
vis[node] = 1;
bool leaf = 1;
for (auto i : g[node])
if (i != par) {
leaf = 0;
if (vis[i]) dp[node] = -1;
if (!vis[i]) dfs(i, node);
if (dp[i] >= 0 and dp[node] != -1) {
dp[node] = max(dp[node], dp[i] + w[node]);
} else
dp[node] = -1;
}
if (leaf) {
dp[node] = w[node];
}
}
void solve() {
long long n, m;
cin >> n >> m;
for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1)))
cin >> w[i];
for (__typeof(m) i = (0) - ((0) > (m)); i != (m) - ((0) > (m));
i += 1 - 2 * ((0) > (m))) {
long long u, v;
cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
long long r;
cin >> r;
dfs(r);
long long ans = 0, mx = 0;
for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) {
if (dp[i] != -1)
mx = max(mx, dp[i]);
else
ans += w[i];
}
cout << ans + mx;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
solve();
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, i, j, res = 100000000000000;
cin >> n >> k;
i = n / 2;
if (i == 0) res = k + 1;
while (i > 0) {
if (n % i == 0) {
j = n / i;
if (res > (i * k + j) && ((i * k + j) % k) * ((i * k + j) / k) == n)
res = i * k + j;
if (res > (j * k + i) && ((j * k + i) % k) * ((j * k + i) / k) == n)
res = j * k + i;
}
i--;
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long a[200005], b[200005];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> b[i];
}
int x, y;
for (int i = 1; i < m; i++) {
x = b[i];
y = b[i + 1];
if (x == y) continue;
if (y < x) swap(x, y);
if (x > 1) {
a[1] += (y - x);
a[x] -= (y - x);
}
a[x] += (y - 1);
a[x + 1] -= (y - 1);
if (y - x > 1) {
a[x + 1] += (y - x - 1);
a[y] -= (y - x - 1);
}
a[y] += (x);
a[y + 1] -= (x);
a[y + 1] += (y - x);
a[n + 1] -= (y - x);
}
for (int i = 1; i <= n; i++) a[i] += a[i - 1];
for (int i = 1; i <= n; i++) cout << a[i] << " ";
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, c[100], x, y, sum = 0, s = 0;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> c[i];
s += c[i];
}
cin >> x >> y;
for (int i = 0; i < m; i++) {
sum += c[i];
if ((x <= sum && sum <= y) && (x <= s - sum && s - sum <= y)) {
printf("%d\n", i + 2);
return 0;
}
}
printf("0\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MAX = 1e5 + 5;
const int P = (1 << 6) + 5;
const int MOD = 1e9 + 7;
const int64_t INF = INT_MAX;
const int64_t _INF = INT_MIN;
int sieve[MAX], fac[MAX], infac[MAX];
int prod[P];
bool odd[P];
int64_t add(int64_t a, int64_t b) { return (a + b) % MOD; }
int64_t sub(int64_t a, int64_t b) { return (a - b + MOD) % MOD; }
int64_t mul(int64_t a, int64_t b) { return (a * b) % MOD; }
int inv(int val) {
int curr = 1, temp = MOD - 2;
while (temp > 0) {
if (temp & 1) curr = mul(curr, val);
val = mul(val, val);
temp >>= 1;
}
return curr;
}
void precompute() {
for (int64_t i = 2; i < MAX; i++) {
if (!sieve[i]) {
sieve[i] = i;
for (int64_t j = i * i; j < MAX; j += i) {
if (!sieve[j]) sieve[j] = i;
}
}
}
fac[0] = 1;
for (int i = 1; i <= MAX - 1; i++) fac[i] = mul(i, fac[i - 1]);
infac[MAX - 1] = inv(fac[MAX - 1]);
for (int i = MAX - 2; i >= 0; i--) infac[i] = mul(i + 1, infac[i + 1]);
prod[0] = 1;
for (int i = 1; i <= P - 1; i++) odd[i] = odd[i ^ (i & -i)] ^ 1;
}
int nCr(int a, int b) {
return (b > a) ? 0 : mul(mul(fac[a], infac[a - b]), infac[b]);
}
int solve(int n, int f) {
if (n == 1) return 1;
int curr = 0, cnt = 0, val = n;
while (val > 1) {
if (sieve[val] != curr) {
curr = sieve[val];
prod[1 << (cnt++)] = curr;
}
val /= curr;
}
int total = nCr(n - 1, f - 1), invalid = 0, m = (1 << cnt) - 1;
for (int i = 1; i <= m; i++) {
prod[i] = prod[i & -i] * prod[i ^ (i & -i)];
if (odd[i])
invalid = add(invalid, nCr(n / prod[i] - 1, f - 1));
else
invalid = sub(invalid, nCr(n / prod[i] - 1, f - 1));
}
return sub(total, invalid);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
precompute();
int q;
cin >> q;
while (q--) {
int n, f;
cin >> n >> f;
cout << solve(n, f) << "\n";
}
}
| 13 |
#include <bits/stdc++.h>
#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
template <typename T>
void read(T &);
template <typename T>
void write(const T &);
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> pii;
const int iinf = 2147483647;
const ll llinf = 9223372036854775807ll;
const int N = 305;
const int Mod = 998244353, iv2 = (Mod + 1) / 2;
void inc(int &a, int b) { (a += b) >= Mod ? a -= Mod : a; }
int pow(int a, int b, int m);
void dfs(int u, int fa);
int f[N][N], g[N][N], mxd[N];
std::vector<int> G[N];
int n, r;
int main() {
read(n);
for (int i = 1; i < n; ++i) {
int u, v;
read(u), read(v);
G[u].push_back(v), G[v].push_back(u);
}
int ans = Mod - 1 + n;
int las = 0;
for (r = 1; r <= n; ++r) {
dfs(1, 0);
int now = std::accumulate(f[1], f[1] + n + 1, 0ll) % Mod;
ans = (ans + 1ll * (now - las + Mod) * (r - 1)) % Mod;
las = now;
}
write(1ll * ans * pow(iv2, n, Mod) % Mod), EL;
return 0;
}
int pow(int a, int b, int m) {
int ans = 1, now = a;
while (b) {
if (b & 1) ans = 1ll * ans * now % m;
now = 1ll * now * now % m;
b >>= 1;
}
return ans;
}
void dfs(int u, int fa) {
f[u][0] = g[u][0] = 1;
mxd[u] = 0;
static int tmpf[N], tmpg[N];
for (int v : G[u]) {
if (v == fa) continue;
dfs(v, u);
std::fill_n(tmpf, std::max(mxd[u], mxd[v] + 1) + 1, 0);
std::fill_n(tmpg, std::max(mxd[u], mxd[v] + 1) + 1, 0);
for (int i = 0; i <= mxd[u]; ++i) {
for (int j = 0; j <= mxd[v]; ++j) {
inc(tmpf[std::min(i, j + 1)], 1ll * f[u][i] * f[v][j] % Mod);
if (i + j + 1 <= r)
inc(tmpf[i], 1ll * f[u][i] * g[v][j] % Mod);
else
inc(tmpg[j + 1], 1ll * f[u][i] * g[v][j] % Mod);
if (i + j + 1 <= r)
inc(tmpf[j + 1], 1ll * g[u][i] * f[v][j] % Mod);
else
inc(tmpg[i], 1ll * g[u][i] * f[v][j] % Mod);
inc(tmpg[std::max(i, j + 1)], 1ll * g[u][i] * g[v][j] % Mod);
}
}
mxd[u] = std::max(mxd[u], mxd[v] + 1);
std::copy_n(tmpf, mxd[u] + 1, f[u]);
std::copy_n(tmpg, mxd[u] + 1, g[u]);
}
}
template <typename T>
void read(T &Re) {
T k = 0;
char ch = getchar();
int flag = 1;
while (!isNum(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while (isNum(ch)) k = k * 10 + ch - '0', ch = getchar();
Re = flag * k;
}
template <typename T>
void write(const T &Wr) {
if (Wr < 0) {
putchar('-'), write(-Wr);
} else if (Wr < 10) {
putchar(Wr + '0');
} else {
write(Wr / 10), putchar((Wr % 10) + '0');
}
}
| 24 |
#include <bits/stdc++.h>
using namespace std;
void readInputData();
void run();
int main(int argc, char** argv) {
ios_base::sync_with_stdio(false);
readInputData();
run();
return 0;
}
int hh;
int mm;
string line;
int str2int(const string& s) {
int n = 0;
for (int i = 0; i < s.size(); ++i)
if (isdigit(s[i])) {
n = n * 10 + (s[i] - '0');
}
return n;
}
void readInputData() {
getline(cin, line);
hh = str2int(line.substr(0, 2));
mm = str2int(line.substr(3, 2));
}
void run() {
if (hh > 12) hh -= 12;
hh %= 12;
double h = hh + mm / 60.0;
double m = mm / 5.0;
double alpha1 = h * 30;
double alpha2 = m * 30;
cout.precision(9);
cout << fixed << alpha1 << ' ' << alpha2 << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a;
for (int i = 0; i < a; i++) {
cin >> b;
cout << (1 << __builtin_popcount(b)) << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int MOD = 998244353;
long long int mod = 1e9 + 7;
long long int INF = 1e18;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, x;
cin >> n >> x;
string s;
cin >> s;
long long int bal[n + 1];
bal[0] = 0;
long long int m = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
bal[i + 1] = bal[i] - 1;
else
bal[i + 1] = bal[i] + 1;
if (bal[i] == x) m = 1;
}
if (bal[n] == 0 && m == 1) {
cout << -1 << endl;
continue;
}
if (bal[n] == 0 && m == 0) {
cout << 0 << endl;
continue;
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
if ((x - bal[i]) % bal[n] == 0 && (x - bal[i]) / bal[n] >= 0) ans++;
}
cout << ans << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int main() {
int n, i, j, counter = 0;
double m = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j++) {
m = i * i + j * j;
m = sqrt(m);
if (m == (int)m && m <= n) {
counter++;
} else {
continue;
}
}
}
printf("%d", counter);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long mp[25][25];
long long dp[1 << 20][25];
long long first(long long x) {
for (long long i = 0; i < n; i++) {
if (x & (1 << i)) return i;
}
}
int main() {
while (~scanf("%lld%lld", &n, &m)) {
memset(mp, 0, sizeof(mp));
for (long long i = 0; i < m; i++) {
long long x, y;
scanf("%lld%lld", &x, &y);
x--, y--;
mp[x][y] = mp[y][x] = 1;
}
memset(dp, 0, sizeof(dp));
for (long long i = 0; i < n; i++) {
dp[1 << i][i] = 1;
}
long long ans = 0;
for (long long i = 0; i < (1 << n); i++) {
for (long long j = 0; j < n; j++) {
if (dp[i][j] == 0) continue;
long long fir = first(i);
for (long long k = fir; k < n; k++) {
if (j == k || mp[j][k] == 0) continue;
if (i & (1 << k)) {
if (k == fir) ans += dp[i][j];
} else {
long long s = i | (1 << k);
dp[s][k] += dp[i][j];
}
}
}
}
ans -= m;
ans /= 2;
printf("%lld\n", ans);
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int q, _c;
long long a, b, m, c[105];
int main() {
for (scanf("%d", &q); q; q--) {
_c = 0;
memset(c, 0, sizeof(c));
scanf("%I64d%I64d%I64d", &a, &b, &m);
if (a == b) {
printf("1 %I64d\n", a);
continue;
}
long long l = a + 1, r = a + m;
int n = 2;
while (r < b) {
l <<= 1;
r <<= 1;
n++;
}
if (l <= b && b <= r) {
long long left = b - ((a + 1) << (n - 2)), sum = 0;
for (int i = 2; i < n; i++) {
if (sum + ((m - 1) << (n - i - 1)) < left) {
sum += (m - 1) << (n - i - 1);
c[i] = m - 1;
} else {
long long L = 0, R = m, mid;
while (L < R - 1) {
mid = (L + R) >> 1;
if (sum + (mid << (n - i - 1)) <= left)
L = mid;
else
R = mid;
}
sum += (L << (n - i - 1));
c[i] = L;
}
}
if (left > sum) c[n] = left - sum;
printf("%d %I64d", n, a);
sum = a;
for (int i = 2; i <= n; i++) {
printf(" %I64d", sum + 1 + c[i]);
sum += sum + 1 + c[i];
}
puts("");
} else {
puts("-1");
}
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
long long int len1, len2;
string str1, str2;
vector<vector<long long int>> dp;
void take_input() {
char s1[5100], s2[5100];
scanf("%lld%lld", &len1, &len2);
scanf("%s%s", s1, s2);
string st1(s1), st2(s2);
str1 = st1;
str2 = st2;
dp.resize(len1 + 1, vector<long long int>(len2 + 1, 0));
}
long long int calc_ans() {
long long int ans = 0;
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (str1[i - 1] == str2[j - 1]) {
dp[i][j] = max((long long int)0, dp[i - 1][j - 1] + 2);
} else {
dp[i][j] = max((long long int)0, max(dp[i - 1][j], dp[i][j - 1]) - 1);
}
ans = max(ans, dp[i][j]);
}
}
return ans;
}
int main() {
take_input();
long long int ans = calc_ans();
printf("%lld\n", ans);
}
| 10 |
#include <bits/stdc++.h>
double k, d, t;
int main() {
scanf("%lf%lf%lf", &k, &d, &t);
double ti = (long long)((k - 1) / d) + 1;
double p = ti * d;
double ans = 0;
double tim = (long long)(2 * t / (p + k));
ans += tim * p;
double rest = 2 * t - (p + k) * tim;
if (rest < 2 * k)
ans += rest / 2;
else
ans += k, ans += rest - 2 * k;
printf("%.10f", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
bool found = false;
string s, temp = "";
cin >> n >> s;
if (n % 2 == 0) {
for (int(i) = (0); (i) < (n); (i)++) {
temp += s[i];
if (temp.size() == 2) {
cout << temp;
temp = "";
if (i < n - 1) cout << "-";
}
}
}
if (n % 2 != 0) {
for (int(i) = (0); (i) < (n); (i)++) {
temp += s[i];
if (temp.size() == 3 && found == false) {
cout << temp;
temp = "";
found = true;
if (i < n - 1) cout << "-";
} else if (temp.size() == 2 && found == true) {
cout << temp;
temp = "";
if (i < n - 1) cout << "-";
}
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long int a, b;
cin >> a >> b;
for (long long int i = 1; i < 100000000000; i++) {
if (i % 2 == 1) {
if (a >= i) {
a -= i;
} else {
cout << "Vladik";
break;
}
}
if (i % 2 == 0) {
if (b >= i) {
b -= i;
} else {
cout << "Valera";
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 1205;
int n, m, k, cnt[N], a[N], b[N], num[N][N];
struct edge {
int fr, to, fl, cp;
edge(){};
edge(int x, int y, int z) {
fr = x;
to = y;
fl = 0;
cp = z;
}
};
vector<edge> e;
vector<int> g[N];
inline void add_edge(int fr, int to, int cp) {
g[fr].push_back((int)e.size());
e.push_back(edge(fr, to, cp));
g[to].push_back((int)e.size());
e.push_back(edge(to, fr, 0));
}
int d[N];
queue<int> q;
inline bool bfs() {
for (int i = 1; i <= n + m + 1; i++) d[i] = inf;
q.push(0);
d[0] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto i : g[v]) {
if (e[i].fl < e[i].cp && d[e[i].fr] + 1 < d[e[i].to]) {
d[e[i].to] = d[e[i].fr] + 1;
q.push(e[i].to);
}
}
}
return d[n + m + 1] != inf;
}
int ptr[N], ans[N];
int ok[N][N], col[N], X[N], Y[N], bol[N], color[N], cnt1[N];
int dfs(int v, int flow = inf) {
if (!flow || v == n + m + 1) return flow;
while (ptr[v] < (int)g[v].size()) {
int i = g[v][ptr[v]];
if (d[e[i].fr] + 1 == d[e[i].to] && e[i].fl < e[i].cp) {
int cur = dfs(e[i].to, min(flow, e[i].cp - e[i].fl));
if (cur) {
e[i].fl += cur;
e[i ^ 1].fl -= cur;
return cur;
}
}
ptr[v]++;
}
return 0;
}
inline int max_flow() {
int res = 0;
while (bfs()) {
memset(ptr, 0, sizeof(ptr));
while (1) {
int add = dfs(0);
if (!add) break;
res += add;
}
}
return res;
}
int main() {
int T, A;
cin >> T;
while (T--) {
cin >> n >> m >> k;
A = 0;
e.clear();
for (int i = 1; i <= n; ++i) cnt1[i] = 0;
for (int i = 0; i <= n + m + 1; ++i) g[i].clear();
for (int i = 1, x, y; i <= m; ++i) {
cin >> x >> y;
X[i] = x, Y[i] = y;
num[x][y] = num[y][x] = i;
cnt1[x]++;
cnt1[y]++;
add_edge(x, n + i, 1);
add_edge(y, n + i, 1);
add_edge(n + i, n + m + 1, 1);
}
bool fok = 0;
for (int i = 1; i <= n; ++i) {
if (cnt1[i] > k + k) {
fok = 1;
break;
}
a[i] = 2 * max(0, cnt1[i] - k);
if (cnt1[i] > k)
b[i] = k + k - cnt1[i];
else
b[i] = cnt1[i];
add_edge(0, i, a[i]);
A += a[i];
}
if (fok) {
for (int i = 1; i <= m; ++i) cout << 0 << " ";
cout << endl;
continue;
}
if (max_flow() == A) {
memset(ok, 0, sizeof(ok));
memset(cnt, 0, sizeof(cnt));
memset(color, 0, sizeof(color));
memset(ans, 0, sizeof(ans));
int sz = 0;
int mx = 0;
bool run = 0;
for (int i = 1; i <= n; i++) {
int cntx = 0;
color[i] = sz;
for (int u : g[i]) {
if (e[u].fl == 1) {
if (cnt[i] == 0) color[i]++;
cnt[i] = cnt[i] ^ 1;
ans[e[u].to - n] = color[i];
mx = max(mx, color[i]);
sz++;
}
}
}
for (int i = 1, x, y; i <= m; i++) {
x = X[i], y = Y[i];
int c = ans[i];
if (!c) c = ++mx;
cout << c << " ";
}
cout << endl;
} else {
for (int i = 1; i <= m; ++i) cout << 0 << " ";
cout << endl;
}
}
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
const int64_t MAXN = 1e5 + 5;
const int64_t MAXINT = 1e9;
const int64_t MAXLL = 1e18;
int64_t MOD = 1e9 + 7;
struct Matrix {
int64_t n, m;
vector<vector<int64_t> > matrix;
Matrix(int64_t n_, int64_t m_) {
n = n_;
m = m_;
matrix.resize(n);
for (int64_t i = 0; i < n; ++i) matrix[i].resize(m);
}
void ones() {
if (n != m) return;
for (int64_t i = 0; i < n; ++i) matrix[i][i] = 1;
}
};
Matrix operator*(const Matrix &m1, const Matrix &m2) {
Matrix result(m1.n, m2.m);
for (int64_t i = 0; i < result.n; ++i) {
for (int64_t j = 0; j < result.m; ++j) {
for (int64_t p = 0; p < m1.m; ++p) {
int64_t prod = (m1.matrix[i][p] * m2.matrix[p][j]) % MOD;
result.matrix[i][j] = (result.matrix[i][j] + prod) % MOD;
}
}
}
return result;
}
Matrix binpow(Matrix a, int64_t p) {
Matrix res(a.n, a.m);
res.ones();
while (p) {
if (p % 2 == 1) {
res = res * a;
}
a = a * a;
p /= 2;
}
return res;
}
int64_t nthfib(int64_t n) {
Matrix b(2, 2);
b.matrix[0][0] = 0;
b.matrix[0][1] = 1;
b.matrix[1][0] = 1;
b.matrix[1][1] = 1;
Matrix fib(1, 2);
fib.matrix[0][0] = 0;
fib.matrix[0][1] = 1;
auto result = fib * binpow(b, n);
return result.matrix[0][0];
}
int64_t f(int64_t l, int64_t r, int64_t ans) {
int64_t a = r / ans;
int64_t b = l / ans + (l % ans > 0);
return a - b + 1;
}
void solve() {
int64_t l, r, k;
cin >> MOD >> l >> r >> k;
vector<int64_t> good;
for (int64_t i = 1; i * i <= r; i++) {
good.push_back(i);
good.push_back(r / i);
}
for (int64_t i = 1; i * i <= l; i++) {
if (l % i == 0)
good.push_back(l / i);
else {
if (i > 1) good.push_back(l / (i - 1));
}
}
int64_t best = 1;
for (int64_t ans : good) {
if (f(l, r, ans) >= k) best = max((int64_t)best, (int64_t)ans);
}
cout << nthfib(best) << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
template <class T>
using V = vector<T>;
template <class T, size_t SZ>
using AR = array<T, SZ>;
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 31 - __builtin_clz(x); }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
template <class T, class U>
T fstTrue(T lo, T hi, U first) {
hi++;
assert(lo <= hi);
while (lo < hi) {
T mid = lo + (hi - lo) / 2;
first(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
template <class T, class U>
T lstTrue(T lo, T hi, U first) {
lo--;
assert(lo <= hi);
while (lo < hi) {
T mid = lo + (hi - lo + 1) / 2;
first(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
template <class T>
void remDup(vector<T>& v) {
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
}
template <class T, class U>
void erase(T& t, const U& u) {
auto it = t.find(u);
assert(it != end(t));
t.erase(u);
}
template <class T>
void re(complex<T>& c);
template <class T, class U>
void re(pair<T, U>& p);
template <class T>
void re(vector<T>& v);
template <class T, size_t SZ>
void re(AR<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(db& d) {
str t;
re(t);
d = stod(t);
}
void re(ld& d) {
str t;
re(t);
d = stold(t);
}
template <class T, class... U>
void re(T& t, U&... u) {
re(t);
re(u...);
}
template <class T>
void re(complex<T>& c) {
T a, b;
re(a, b);
c = {a, b};
}
template <class T, class U>
void re(pair<T, U>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& x) {
for (auto& a : x) re(a);
}
template <class T, size_t SZ>
void re(AR<T, SZ>& x) {
for (auto& a : x) re(a);
}
str to_string(char c) { return str(1, c); }
str to_string(const char* second) { return (str)second; }
str to_string(str second) { return second; }
str to_string(bool b) { return to_string((int)b); }
template <class T>
str to_string(complex<T> c) {
stringstream ss;
ss << c;
return ss.str();
}
str to_string(vector<bool> v) {
str res = "{";
for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ>
str to_string(bitset<SZ> b) {
str res = "";
for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
return res;
}
template <class T, class U>
str to_string(pair<T, U> p);
template <class T>
str to_string(T v) {
bool fst = 1;
str res = "";
for (const auto& x : v) {
if (!fst) res += " ";
fst = 0;
res += to_string(x);
}
return res;
}
template <class T, class U>
str to_string(pair<T, U> p) {
return to_string(p.first) + " " + to_string(p.second);
}
template <class T>
void pr(T x) {
cout << to_string(x);
}
template <class T, class... U>
void pr(const T& t, const U&... u) {
pr(t);
pr(u...);
}
void ps() { pr("\n"); }
template <class T, class... U>
void ps(const T& t, const U&... u) {
pr(t);
if (sizeof...(u)) pr(" ");
ps(u...);
}
void DBG() { cerr << "]" << endl; }
template <class T, class... U>
void DBG(const T& t, const U&... u) {
cerr << to_string(t);
if (sizeof...(u)) cerr << ", ";
DBG(u...);
}
void setIn(str second) { freopen(second.c_str(), "r", stdin); }
void setOut(str second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
void setIO(str second = "") {
unsyncIO();
if ((int)(second).size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
int n, l;
void solve() {
re(n, l);
vd a(n);
re(a);
a.push_back(0), a.push_back(l);
sort(begin(a), end(a));
int x = 0, y = (int)(a).size() - 1, sx = 1, sy = 1;
db tx = 0, ty = 0;
while (x + 1 < y) {
if (tx + (a[x + 1] - a[x]) / sx < ty + (a[y] - a[y - 1]) / sy) {
tx += (a[x + 1] - a[x]) / sx;
sx++;
x++;
} else {
ty += (a[y] - a[y - 1]) / sy;
sy++;
y--;
}
}
db lef = a[y] - a[x];
db ans;
if (tx < ty) {
lef -= (ty - tx) * sx;
ans = ty + lef / (sx + sy);
} else {
lef -= (tx - ty) * sy;
ans = tx + lef / (sx + sy);
}
cout << fixed << setprecision(9) << ans << "\n";
}
int main() {
setIO();
int T;
re(T);
for (int i = (0); i < (T); ++i) solve();
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
int a[maxn], b[maxn];
int main() {
int v, n, m, i, flag;
while (scanf("%d%d", &n, &m) != EOF) {
for (i = 0; i < n; i++) scanf("%d", &a[i]);
for (i = 0; i < m; i++) scanf("%d", &b[i]);
sort(a, a + n);
sort(b, b + m);
flag = 0;
for (v = a[n - 1]; v < b[0]; v++) {
if (v >= 2 * a[0]) {
flag = 1;
break;
}
}
if (flag)
printf("%d\n", v);
else
printf("-1\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
char s[N], t[N], S[N];
int n, go[N][26], len[N], par[N], top, last;
int New(int L) {
len[++top] = L;
return top;
}
int diff[N], slink[N];
void extend(int n) {
int w = S[n], p = last;
while (S[n - len[p] - 1] != w) p = par[p];
if (!go[p][w - 'a']) {
int q = New(len[p] + 2), now = p;
do now = par[now];
while (S[n - len[now] - 1] != w);
par[q] = go[now][w - 'a'];
go[p][w - 'a'] = q;
diff[q] = len[q] - len[par[q]];
slink[q] = (diff[q] == diff[par[q]] ? slink[par[q]] : par[q]);
}
last = go[p][w - 'a'];
}
int ans[N], from[N], _ans[N], sum[N];
char kek[N];
int main() {
scanf("%s", kek + 1);
int n = strlen(kek + 1);
for (int i = 1; i <= n / 2; i++) s[i] = kek[i];
for (int i = 1; i <= n / 2; i++) t[i] = kek[n - i + 1];
n /= 2;
for (int i = 1; i <= n; i++) S[i * 2 - 1] = s[i], S[i * 2] = t[i];
par[0] = top = 1;
len[1] = -1;
n *= 2;
for (int i = 1; i <= n; i++) ans[i] = 0;
_ans[0] = 1;
ans[0] = 1;
for (int i = 1; i <= n; i++) {
extend(i);
for (int p = last; p; p = slink[p]) {
_ans[p] = i - (len[slink[p]] + diff[p]);
sum[p] = ans[_ans[p]];
int cur = p;
if (diff[par[cur]] == diff[cur]) {
sum[cur] += sum[par[cur]];
if (sum[cur] >= mod) sum[cur] -= mod;
}
if (!(i & 1)) {
ans[i] += sum[cur];
if (ans[i] >= mod) ans[i] -= mod;
}
}
}
cout << ans[n] << '\n';
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
long long po(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = (res * a) % 1000000007;
}
a = (a * a) % 1000000007;
b = b / 2;
}
return res % 1000000007;
}
const int con = 256;
string fun(string s, string s2) {
int len1 = s.length();
int len2 = s2.length();
if (len1 < len2) {
return "";
}
int hp[con] = {0};
int hs[con] = {0};
for (int i = 0; i < len2; i++) hp[s2[i]]++;
int srt = 0, si = -1, ml = INT_MAX;
int ctr = 0;
for (int j = 0; j < len1; j++) {
hs[s[j]]++;
if (hp[s[j]] != 0 && hs[s[j]] <= hp[s[j]]) ctr++;
if (ctr == len2) {
while (hs[s[srt]] > hp[s[srt]] || hp[s[srt]] == 0) {
if (hs[s[srt]] > hp[s[srt]]) hs[s[srt]]--;
srt++;
}
int lw = j - srt + 1;
if (ml > lw) {
ml = lw;
si = srt;
}
}
}
if (si == -1) {
return "";
}
return s.substr(si, ml);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
string req = "123";
string ans = fun(s, req);
if (ans.size() == 0)
cout << "0"
<< "\n";
else
cout << ans.size() << "\n";
}
return 0;
}
| 4 |
Subsets and Splits