solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 1e3 + 7;
const int maxm = 1e5 + 7;
const int mod = 998244353;
int t, n;
int x, y;
int k1, k2;
int subtreex[maxn], subtreey[maxn];
vector<int> tree[maxn];
void init() {
memset(subtreey, 0, sizeof(subtreey));
memset(subtreex, 0, sizeof(subtreex));
for (int i = 0; i < maxn; i++) tree[i].clear();
}
int dfs(int cur, int lst) {
if (subtreex[cur]) return cur;
for (int i = 0; i < tree[cur].size(); i++) {
int v = tree[cur][i];
if (v == lst) continue;
int idx = dfs(v, cur);
if (idx != -1) return idx;
}
return -1;
}
int main(int argc, char const *argv[]) {
scanf("%d", &t);
while (t--) {
init();
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
tree[u].push_back(v);
tree[v].push_back(u);
}
scanf("%d", &k1);
for (int i = 1; i <= k1; i++) {
scanf("%d", &x);
subtreex[x] = 1;
}
scanf("%d", &k2);
for (int i = 1; i <= k2; i++) {
scanf("%d", &y);
subtreey[y] = 1;
}
printf("B %d\n", y);
fflush(stdout);
int yinx;
scanf("%d", &yinx);
if (subtreex[yinx]) {
printf("C %d\n", yinx);
fflush(stdout);
continue;
}
int idx = dfs(yinx, 0);
if (idx == -1) {
printf("C -1\n");
fflush(stdout);
continue;
}
int p;
printf("A %d\n", idx);
fflush(stdout);
scanf("%d", &p);
printf("C %d\n", subtreey[p] ? idx : -1);
fflush(stdout);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 10;
const int inf = 0x3f3f3f3f;
int dis[10][10];
char s[maxn];
int n;
int fun(int x, int y) {
memset(dis, inf, sizeof(dis));
for (int i = 0; i < 10; i++) {
dis[i][(i + x) % 10] = dis[i][(i + y) % 10] = 1;
}
for (int k = 0; k < 10; k++) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
long long ans = 0;
for (int i = 0; i < n - 1; i++) {
if (dis[s[i] - '0'][s[i + 1] - '0'] == inf) return -1;
ans += dis[s[i] - '0'][s[i + 1] - '0'] - 1;
}
return ans;
}
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
cout << fun(i, j) << ' ';
}
cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long powmod(long long x, long long n, long long _mod) {
long long res = 1;
while (n) {
if (n & 1) res = (res * x) % _mod;
x = (x * x) % _mod;
n >>= 1;
}
return res;
}
int n, m;
int k, p;
int a[1005][1005];
void _read() {
cin >> n >> m;
cin >> k >> p;
for (int i = 1; i < n + 1; ++i) {
for (int j = 1; j < m + 1; ++j) {
cin >> a[i][j];
}
}
}
long long rowSum[1005 * 1005];
long long colSum[1005 * 1005];
void _sol() {
priority_queue<int> pq;
for (int i = 1; i < n + 1; ++i) {
long long sum = 0;
for (int j = 1; j < m + 1; ++j) sum += a[i][j];
pq.push(sum);
}
for (int i = 1; i < k + 1; ++i) {
long long t = pq.top();
pq.pop();
rowSum[i] = rowSum[i - 1] + t;
t -= p * m;
pq.push(t);
}
while (!pq.empty()) pq.pop();
for (int i = 1; i < m + 1; ++i) {
long long sum = 0;
for (int j = 1; j < n + 1; ++j) sum += a[j][i];
pq.push(sum);
}
for (int i = 1; i < k + 1; ++i) {
long long t = pq.top();
pq.pop();
colSum[i] = colSum[i - 1] + t;
t -= n * p;
pq.push(t);
}
long long ans = rowSum[0] + colSum[k];
for (int i = 0; i < k + 1; ++i) {
ans = max(ans, rowSum[i] + colSum[k - i] - 1LL * i * (k - i) * p);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
_read();
_sol();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
v[i] = make_pair(x, i);
}
sort(v.begin(), v.end());
int sum = 0;
vector<int> indexes(k);
for (int i = 0; i < k; i++) {
sum += v[n - i - 1].first;
indexes[i] = v[n - i - 1].second + 1;
}
cout << sum << endl;
sort(indexes.begin(), indexes.end());
int prev = 0;
vector<int> res(k);
for (int i = 0; i < k; i++) {
if (i != k - 1) {
res[i] = indexes[i] - prev;
prev = indexes[i];
} else {
res[i] = n - prev;
}
}
for (int i = 0; i < k; i++) cout << res[i] << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-8;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 100100;
int n;
char a[MAXN];
vector<string> ans;
int main() {
gets(a);
n = (int)strlen(a);
int z = 0, o = 0, q = 0;
for (int i = 0; i < (int)(n); i++) {
if (a[i] == '0') z++;
if (a[i] == '1') o++;
if (a[i] == '?') q++;
}
int s = (n - 2) / 2, m = (n - 2 - s);
if (m >= o) ans.push_back("00");
if (s >= z) ans.push_back("11");
for (int qo = 0; qo < (int)(q + 1); qo++) {
int qz = q - qo;
if (o + qo - m == 1 && z + qz - s == 1) {
if (a[n - 1] == '0' || (a[n - 1] == '?' && qo != q)) ans.push_back("10");
if (a[n - 1] == '1' || (a[n - 1] == '?' && qz != q)) ans.push_back("01");
}
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()), ans.end());
for (int i = 0; i < (int)(ans.size()); i++) cout << ans[i] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int SIZ = 1000000;
bool x[SIZ + 1], y[SIZ + 1];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a;
scanf("%d", &a);
x[a] = 1;
}
int have = 0, rem = 0;
for (int i = 1; i <= SIZ / 2; i++) {
if (x[i] && x[SIZ - i + 1])
rem++;
else if (x[i]) {
y[SIZ - i + 1] = 1;
have++;
} else if (x[SIZ - i + 1]) {
y[i] = 1;
have++;
}
}
for (int i = 1; i <= SIZ / 2 && rem > 0; i++) {
if (x[i] || x[SIZ - i + 1] || y[i] || y[SIZ - i + 1]) continue;
y[i] = y[SIZ - i + 1] = 1;
have += 2;
rem--;
}
printf("%d\n", have);
for (int i = 1; i <= SIZ; i++)
if (y[i]) printf("%d ", i);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
int gcd(int i, int j) {
while (i != 0) {
int k = i;
i = j % i;
j = k;
}
return j;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
if (m < (n - 1)) {
cout << "Impossible" << endl;
} else {
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (gcd(i, j) == 1) {
v.push_back(make_pair(i, j));
if (v.size() == m) {
cout << "Possible" << endl;
for (int i = 0; i < m; i++) {
cout << v[i].first << " " << v[i].second << endl;
}
return 0;
}
}
}
}
cout << "Impossible" << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
map<long long, int> mp;
int main() {
long long n;
cin >> n;
long long m = 2 * n;
long long a[m];
long long t = 0, ans = 0;
for (int i = 0; i < m; i++) {
cin >> a[i];
mp[a[i]]++;
if (mp[a[i]] == 1) {
t++;
} else {
t--;
}
ans = max(t, ans);
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long chk[300], ara[300];
int main() {
long long n;
scanf("%lld", &n);
memset((chk), (-1), sizeof(chk));
for (long long i = 1; i <= 2 * n; i++) {
scanf("%lld", &ara[i]);
chk[ara[i]] = i;
}
long long ans = 0;
for (long long i = 1; i <= 2 * n; i = i + 2) {
for (long long j = chk[ara[i]] - 1; j > i; j--) {
long long sp = ara[j];
ara[j + 1] = sp;
if (chk[sp] == j) chk[sp]++;
}
ans += chk[ara[i]] - 1 - i;
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
if (n % 2 == 0) {
for (int i = n / 2; i <= n; i++) {
if (i % m == 0) {
cout << i << endl;
return 0;
}
}
} else {
for (int i = (n / 2) + 1; i <= n; i++) {
if (i % m == 0) {
cout << i << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int n, m, w[1234], b, ans, cur[1234], csz;
char s[1234];
int main() {
scanf("%d %d", &n, &m);
for (int i = (0); i < (n); i++) scanf("%d", w + i);
for (int i = (0); i < (m); i++) {
scanf("%d", &b);
b--;
if (!s[b]) {
for (int j = (0); j < (csz); j++) ans += w[cur[j]];
for (int j = csz; j >= 0; j--) cur[j + 1] = cur[j];
cur[0] = b;
csz++;
} else {
int pos = 0;
for (int j = (0); j < (csz); j++)
if (cur[j] == b) pos = j;
for (int j = (0); j < (pos); j++) ans += w[cur[j]];
for (int j = pos - 1; j >= 0; j--) cur[j + 1] = cur[j];
cur[0] = b;
}
s[b] = 1;
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b;
typedef int Ploy[400000 + 5];
int fac[400000 + 5], inv[400000 + 5];
int read() {
int x = 0, F = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') F = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * F;
}
int fst_pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % 998244353;
a = 1LL * a * a % 998244353;
b >>= 1;
}
return res;
}
void prepare() {
fac[0] = 1;
for (int i = 1; i < n; i++) fac[i] = 1LL * fac[i - 1] * i % 998244353;
inv[n - 1] = fst_pow(fac[n - 1], 998244353 - 2);
for (int i = n - 2; i >= 0; i--)
inv[i] = 1LL * inv[i + 1] * (i + 1) % 998244353;
}
int Comb(int p1, int p2) {
return 1LL * (1LL * fac[p1] * inv[p1 - p2]) % 998244353 * inv[p2] % 998244353;
}
void NTT(Ploy &a, int n, int x) {
for (int i = 0, j = 0; i < n; i++) {
if (i < j) swap(a[i], a[j]);
int k = n >> 1;
while (k && (k & j)) j ^= k, k >>= 1;
j ^= k;
}
for (int i = 1; i < n; i <<= 1) {
int gn = fst_pow(3, (998244353 - 1) / (i << 1)), g = 1;
if (x == -1) gn = fst_pow(gn, 998244353 - 2);
for (int j = 0; j < i; j++, g = (1LL * g * gn) % 998244353)
for (int l = j, r = l + i; l < n; l += (i << 1), r = l + i) {
int tmp = (1LL * a[r] * g) % 998244353;
a[r] = (a[l] - tmp + 998244353) % 998244353;
a[l] = (a[l] + tmp) % 998244353;
}
}
if (x == 1) return;
int ny = fst_pow(n, 998244353 - 2);
for (int i = 0; i < n; i++) a[i] = 1LL * a[i] * ny % 998244353;
}
void mod_Mul(Ploy &a, Ploy &b, Ploy &res, int n) {
static Ploy a0, b0;
copy(a, a + n, a0);
copy(b, b + n, b0);
NTT(a0, n, 1), NTT(b0, n, 1);
for (int i = 0; i < n; i++) a0[i] = (1LL * a0[i] * b0[i]) % 998244353;
NTT(a0, n, -1);
copy(a0, a0 + n, res);
}
static Ploy f[19];
void slove(int x, int l, int r) {
if (l == r) {
f[x][1] = 1;
f[x][0] = l;
return;
}
int mid = (l + r) >> 1;
slove(x + 1, l, mid);
for (int i = 0; i <= mid - l + 1; i++) f[x][i] = f[x + 1][i];
slove(x + 1, mid + 1, r);
int len = 1;
while (len <= (r - l + 6)) len <<= 1;
fill(f[x] + mid - l + 2, f[x] + len, 0);
fill(f[x + 1] + r - mid + 1, f[x + 1] + len, 0);
mod_Mul(f[x], f[x + 1], f[x], len);
}
int main() {
scanf("%d%d%d", &n, &a, &b);
if (a == 0 || b == 0) {
printf("0\n");
return 0;
}
if (a == 1 && b == 1) {
printf("%d", (n == 1));
return 0;
}
prepare();
if (a + b == n + 1) {
printf("%d", Comb(n - 1, a - 1));
return 0;
}
slove(0, 0, n - 2);
printf("%lld", (1LL * f[0][a + b - 2] * Comb(a + b - 2, a - 1)) % 998244353);
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<float> coordinates;
for (int i = 0; i < (int)(n); ++i) {
float a, b;
cin >> a >> b;
coordinates.push_back(a);
coordinates.push_back(b);
}
float sum = 0;
std::cout << fixed;
std::cout << setprecision(9);
for (int i = 0; i < coordinates.size() - 2; i += 2) {
sum += sqrt(pow(abs(coordinates[i] - coordinates[i + 2]), 2) +
pow(abs(coordinates[i + 1] - coordinates[i + 3]), 2)) /
50;
}
cout << sum * k << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin >> q;
while (q--) {
long long a, b, c;
cin >> a >> b >> c;
if (a * b <= c)
cout << 1 << " " << -1 << "\n";
else {
if (c > a)
cout << 1 << " " << b << "\n";
else
cout << -1 << " " << b << "\n";
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
bool f = false;
set<int> v1;
int n, ans = -1;
cin >> n;
int a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
v1.insert(a[i]);
}
for (int i = 1; i <= 1024; i++) {
long long count = 0;
for (int j = 0; j < n; j++) {
int m = a[j] ^ i;
v1.insert(a[j] ^ i);
if (v1.size() > n) {
count++;
v1.erase(v1.find(m));
break;
}
}
if (count == 0) {
ans = i;
break;
}
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void smin(long long &x, long long y) { x = min(x, y); }
inline void smax(long long &x, long long y) { x = max(x, y); }
inline void smin(int &x, int y) { x = min(x, y); }
inline void smax(int &x, int y) { x = max(x, y); }
const int N = 2e5 + 10, M = 5e5 + 10;
long long n, q, a[N], cnt[M], res = 0, sz = 0;
bool hast[N];
vector<int> pr[M];
bool isPrime[M];
long long calc(long long x, long long c) {
long long ans = 0;
for (int mask = 1; mask < (1 << (int)pr[x].size()); mask++) {
int val = 1, ch = 0;
for (int i = 0; i < (int)pr[x].size(); i++)
if ((mask >> i) & 1) val *= pr[x][i], ch++;
if (c == -1) cnt[val] += c;
if (ch & 1)
ans += cnt[val];
else
ans -= cnt[val];
if (c == 1) cnt[val] += c;
}
return ans;
}
long long f(long long x) { return max(1LL * 0, x * (x - 1) / 2); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
fill(isPrime, isPrime + M, true);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 2; i < M; i++) {
if (isPrime[i]) {
for (int j = i; j < M; j += i) {
isPrime[j] = false;
pr[j].push_back(i);
}
}
}
while (q--) {
int x;
cin >> x;
if (hast[x]) {
sz--;
if (a[x] != 1) {
int add = calc(a[x], -1);
res -= add;
}
} else {
sz++;
if (a[x] != 1) {
int add = calc(a[x], 1);
res += add;
}
}
hast[x] ^= 1;
cout << f(sz) - res << '\n';
}
}
| 7 |
#include <bits/stdc++.h>
int main() {
using namespace std;
long long data[100005], be, ed, ans, n, k, ns;
cin >> n >> k;
be = 0;
ns = n;
ed = 2 * n - 2 * k - 1;
if (ed < 0) {
ed = 0;
}
ans = 0;
for (size_t i = 0; i < n; i++) {
cin >> data[i];
}
for (size_t i = ed; i < n; i++) {
ans = max(ans, data[i]);
}
for (; be < ed; be++, ed--) {
ans = max(ans, data[be] + data[ed]);
}
cout << ans << "\n";
}
| 3 |
#include <bits/stdc++.h>
int main() {
int loft_count, flat_count;
std::cin >> loft_count >> flat_count;
int light_count = 0;
bool current_window;
bool current_flat_is_light = false;
for (int i = 0; i < loft_count; i++) {
for (int j = 0; j < flat_count * 2; j++) {
std::cin >> current_window;
if (current_flat_is_light) {
current_flat_is_light = false;
continue;
}
if (current_window) {
light_count++;
if (j % 2 == 0) current_flat_is_light = true;
}
}
}
std::cout << light_count;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[100003], n;
int b[100003];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
b[a[i]] = i;
}
int cnt = 1, mx = 1;
for (int i = 2; i <= n; i++) {
if (b[i] > b[i - 1]) {
cnt++;
mx = max(mx, cnt);
} else
cnt = 1;
}
mx = max(mx, cnt);
printf("%d\n", n - mx);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T power(T a, T b) {
T x;
if (b == 0)
x = 1;
else
x = a;
for (size_t i = 1; i < b; i++) x *= a;
return x;
}
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int n, m, a, b, c, ab, ba, x, y, z, avg, sum;
long long int arr2[1000][2], arr3[200000], arr4[200001], arr5[100001];
char c2[101][101];
bool f, f1, f2;
string str, s1, s2, s3, s4;
set<int> seto;
double d, d1;
long long int x1, x2, y11, y2;
long long int mini = 100000000000000, maxi = 0;
map<char, long long int> mp;
vector<long long int> v, v1;
vector<vector<pair<long long int, pair<long long int, long long int>>>> v2(
200001);
long long int r, g;
int main() {
cin >> n >> a >> b;
for (int i = 0; i < n; i++) {
cin >> arr2[i][0] >> arr2[i][1];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (arr2[i][0] + arr2[j][0] <= a && arr2[i][1] <= b && arr2[j][1] <= b)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][0] + arr2[j][1] <= a && arr2[i][1] <= b && arr2[j][0] <= b)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][1] + arr2[j][0] <= a && arr2[i][0] <= b && arr2[j][1] <= b)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][1] + arr2[j][1] <= a && arr2[i][0] <= b && arr2[j][0] <= b)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][0] + arr2[j][0] <= b && arr2[i][1] <= a && arr2[j][1] <= a)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][0] + arr2[j][1] <= b && arr2[i][1] <= a && arr2[j][0] <= a)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][1] + arr2[j][0] <= b && arr2[i][0] <= a && arr2[j][1] <= a)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
if (arr2[i][1] + arr2[j][1] <= b && arr2[i][0] <= a && arr2[j][0] <= a)
arr3[x++] = (arr2[i][0] * arr2[i][1]) + (arr2[j][0] * arr2[j][1]);
}
}
sort(arr3, arr3 + x);
cout << arr3[x - 1];
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5143;
const long long Linf = 1e18 + 5413;
const double eps = 1e-10;
const double pi = acos(-1);
template <class T>
inline void umax(T &a, T b) {
if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
template <class T>
inline T lcm(T a, T b) {
return a / __gcd(a, b) * b;
}
inline int read() {
int res = 0LL;
int neg;
while (1) {
char ch = getchar();
if (ch >= '0' && ch <= '9' || ch == '-') {
if (ch == '-')
neg = -1;
else
neg = 1, res = ch - '0';
break;
}
}
while (1) {
char ch = getchar();
if (ch >= '0' && ch <= '9')
res *= 10, res += ch - '0';
else
break;
}
return res * neg;
}
const int N = 7010;
const int MOSTR = N - 5;
const long long prime = 37;
long long power[N];
int par1[N];
int par2[N];
char ch1[N], ch2[N];
map<long long, int> down, up;
int main() {
int q = read();
int n1 = 1;
int n2 = 1;
power[0] = 1;
for (int(i) = (1); (i) < (N); (i)++) power[i] = power[i - 1] * prime;
long long res = 1LL;
while (q--) {
int t = read();
int u = read();
char ch;
scanf(" %c", &ch);
if (t == 1) {
++n1;
par1[n1] = u;
ch1[n1] = ch;
vector<int> path;
int node = n1;
do {
path.push_back(ch1[node]);
node = par1[node];
} while (node != 1);
long long hs = 0;
for (int(i) = (0); (i) < (path.size()); (i)++) {
if (i == 0) {
hs = path[i] - 'a' + 1;
} else {
hs += (path[i] - 'a' + 1) * power[i];
}
}
hs *= power[MOSTR];
up[hs]++;
res += down[hs];
} else {
++n2;
res++;
par2[n2] = u;
ch2[n2] = ch;
vector<int> path;
int node = n2;
do {
path.push_back(ch2[node]);
node = par2[node];
} while (node != 1);
reverse(path.begin(), path.end());
vector<long long> hs;
hs.resize(path.size());
for (int(i) = (0); (i) < (path.size()); (i)++) {
if (i == 0) {
hs[i] = path[i] - 'a' + 1;
} else {
hs[i] = hs[i - 1] + (path[i] - 'a' + 1) * power[i];
}
}
for (int(i) = (0); (i) < (path.size()); (i)++) {
long long temp =
(hs[path.size() - 1] - (i == 0 ? 0 : hs[i - 1])) * power[MOSTR - i];
down[temp]++;
res += up[temp];
}
}
cout << res << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::map<std::string, int> dict;
std::vector<std::string> ans(n);
for (int i = 0; i < n; ++i) {
std::string s;
std::cin >> s;
ans[i] = s;
for (size_t j = 0; j < s.length(); ++j) {
for (size_t l = 1; l <= s.length(); ++l) {
if (j + l <= s.length()) {
if (dict.find(s.substr(j, l)) == dict.end()) {
dict[s.substr(j, l)] = i;
} else {
if (dict[s.substr(j, l)] != i) {
dict[s.substr(j, l)] = -1;
}
}
}
}
}
}
for (std::map<std::string, int>::iterator it = dict.begin(); it != dict.end();
++it) {
if (it->second != -1 && ans[it->second].length() > (it->first).length()) {
ans[it->second] = it->first;
}
}
for (int i = 0; i < n; ++i) {
std::cout << ans[i] << std::endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, b;
cin >> r >> b;
int diff = min(r, b);
int ss = (max(r, b) - diff) / 2;
cout << diff << " " << ss;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline vector<T> readvector(size_t size) {
vector<T> res(size);
for (size_t i = 0; i < size; ++i) {
cin >> res[i];
}
return res;
}
template <typename T>
inline void printvector(vector<T> &arr, string sep = " ", string ends = "\n") {
for (size_t i = 0; i < arr.size(); ++i) {
}
}
inline long long binPow(long long first, long long deg, long long mod) {
long long ans = 1;
for (long long i = sizeof(first) * CHAR_BIT - 1; i >= 0; i--) {
ans *= ans;
ans %= mod;
if ((((deg) >> (i)) & 1)) ans *= first;
ans %= mod;
}
return ans;
}
const long long MAXN = 2e5 + 10;
long long phi(long long n) {
long long result = n;
for (long long i = 2; i * i <= n; ++i)
if (n % i == 0) {
while (n % i == 0) n /= i;
result -= result / i;
}
if (n > 1) result -= result / n;
return result;
}
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long a, m;
cin >> a >> m;
long long d = gcd(a, m);
long long first = phi(m / d);
cout << first << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:66777216")
using namespace std;
const int N = 200007;
int n, a[N], b[N], c[N];
struct Fenwcik {
int T[N];
void add(int i) {
for (++i; i < N; i += (i & -i)) ++T[i];
}
void del(int i) {
for (++i; i < N; i += (i & -i)) --T[i];
}
int getKth(int k) {
int start = 0;
++k;
for (int i = 20; i >= 0; --i) {
int to = start + (1 << i);
if (to >= N) continue;
if (T[to] < k) {
k -= T[to];
start = to;
}
}
return start;
}
int get(int i) {
int ret = 0;
for (++i; i; i -= (i & -i)) ret += T[i];
return ret;
}
} t1, t2;
void solve() {
scanf("%d", &n);
for (int i = 0; i < (n); ++i) scanf("%d", &a[i]);
for (int i = 0; i < (n); ++i) scanf("%d", &b[i]);
int carry = 0;
for (int i = n - 1; i >= 0; --i) {
int val = t1.get(a[i]) + t2.get(b[i]) + carry;
t1.add(a[i]);
t2.add(b[i]);
c[i] = val % (n - i);
carry = val / (n - i);
}
for (int i = 0; i < (n); ++i) {
int x = t1.getKth(c[i]);
t1.del(x);
printf("%d ", x);
}
puts("");
}
void testGen() {
FILE *f = fopen("input.txt", "w");
fclose(f);
}
int main() {
cerr << fixed;
cerr.precision(5);
solve();
return 0;
}
| 6 |
#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;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long n, t[5010];
long long l[5010], lmax[5010];
long long r[5010], rmin[5010];
long long ans = -999999999, ansx, ansy, ansz;
int main() {
long long i, nowans;
scanf("%I64d", &n);
for (i = 1; i <= n; i++) scanf("%I64d", &t[i]);
for (i = 1; i <= n; i++) l[i] = l[i - 1] + t[i];
for (i = 1; i <= n; i++) {
if (l[lmax[i - 1]] > l[i])
lmax[i] = lmax[i - 1];
else
lmax[i] = i;
}
r[n + 1] = 0;
rmin[n + 1] = n + 1;
for (i = n; i > 0; i--) r[i] = r[i + 1] + t[i];
for (i = n; i > 0; i--) {
if (r[rmin[i + 1]] < r[i])
rmin[i] = rmin[i + 1];
else
rmin[i] = i;
}
for (i = 1; i <= n; i++) {
nowans = 2 * l[lmax[i]] - l[i] + r[i + 1] - 2 * r[rmin[i + 1]];
if (nowans > ans) {
ans = nowans;
ansx = lmax[i];
ansy = i;
ansz = rmin[i + 1];
}
}
printf("%I64d %I64d %I64d", ansx, ansy, ansz - 1);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
using namespace std;
int main() {
int q;
cin >> q;
while (q--) {
long long n;
cin >> n;
int cnt = 0;
while (n % 3 == 0) {
n /= 3;
n *= 2;
cnt++;
}
while (n % 5 == 0) {
n /= 5;
n *= 4;
cnt++;
}
while (n % 2 == 0) {
n /= 2;
cnt++;
}
if (n == 1)
cout << cnt;
else
cout << -1;
cout << "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxN = 400005;
vector<int> a[maxN];
int d[maxN], h[maxN], root[maxN], p[maxN][20];
bool color[maxN];
int belong[maxN];
int n, k, r;
void mo() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void nhap() {
cin >> n >> k >> r;
int N = n;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
a[u].push_back(N + 1);
a[v].push_back(N + 1);
a[N + 1].push_back(u);
a[N + 1].push_back(v);
N++;
}
n = N;
for (int i = 1; i <= n; i++) {
d[i] = n + 1;
}
for (int i = 1; i <= r; i++) {
int x;
cin >> x;
d[x] = 0;
}
for (int i = 1; i <= n; i++) root[i] = i;
}
int findroot(int x) { return root[x] == x ? x : root[x] = findroot(root[x]); }
void Union(int u, int v) {
u = findroot(belong[u]);
v = findroot(belong[v]);
if (u != v) {
root[v] = u;
}
}
void bfs() {
deque<int> S;
for (int i = 1; i <= n; i++)
if (d[i] == 0) {
S.push_back(i);
belong[i] = i;
}
while (!S.empty()) {
int u = S.front();
if (d[u] <= k) color[u] = true;
S.pop_front();
for (auto v : a[u])
if (d[v] == n + 1) {
d[v] = d[u] + 1;
belong[v] = belong[u];
S.push_back(v);
} else {
if (d[u] + d[v] + 1 <= k * 2) {
Union(u, v);
}
}
}
}
void dfs(int u, int pre) {
for (auto v : a[u])
if (p[v][0] == 0 && v != pre) {
p[v][0] = u;
h[v] = h[u] + 1;
dfs(v, u);
}
}
void tienxuli() {
bfs();
p[1][0] = 0;
dfs(1, -1);
for (int j = 1; j <= 19; j++)
for (int i = 1; i <= n; i++) {
p[i][j] = p[p[i][j - 1]][j - 1];
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
int delta = h[u] - h[v];
for (int i = 19; i >= 0; i--)
if (delta >= (1 << i)) {
u = p[u][i], delta -= (1 << i);
}
if (u == v) return u;
for (int i = 19; i >= 0; i--)
if (p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
return p[u][0];
}
int dist(int u, int v) {
int c = lca(u, v);
return h[u] + h[v] - 2 * h[c];
}
int Up(int a, int delta) {
for (int i = 19; i >= 0; i--)
if (delta >= (1 << i)) {
delta -= (1 << i), a = p[a][i];
}
return a;
}
bool up(int a, int b) {
int c = lca(a, b);
int _a, _b;
if (h[a] - h[c] >= k) {
_a = Up(a, k);
} else {
_a = Up(b, h[b] - h[c] - (k - (h[a] - h[c])));
}
if (h[b] - h[c] >= k) {
_b = Up(b, k);
} else
_b = Up(a, h[a] - h[c] - (k - (h[b] - h[c])));
if (color[_a] && color[_b]) {
int u = findroot(belong[_a]);
int v = findroot(belong[_b]);
if (u == v) return true;
}
return false;
}
void xuli() {
int Q;
cin >> Q;
while (Q--) {
int a, b;
cin >> a >> b;
if (dist(a, b) <= 2 * k) {
cout << "YES\n";
continue;
}
if (up(a, b))
cout << "YES\n";
else
cout << "NO\n";
}
}
int main() {
mo();
nhap();
tienxuli();
xuli();
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int eval(int lo, int hi) {
while (lo < hi) {
int mid = (lo + hi) / 2;
cout << "1 " << mid << " " << mid + 1 << "\n";
string s;
cin >> s;
if (s == "TAK")
hi = mid;
else
lo = mid + 1;
}
return lo;
}
int main() {
int n, k, x, y1, y2;
cin >> n >> k;
x = eval(1, n);
y1 = eval(1, x - 1);
y2 = eval(x + 1, n);
if (y1 >= 1 && y1 <= x - 1) {
cout << "1 " << x << " " << y1 << "\n";
string s1, s2;
cin >> s1;
cout << "1 " << y1 << " " << x << "\n";
cin >> s2;
if (s1 == "TAK" && s2 == "TAK")
cout << "2 " << x << " " << y1 << "\n";
else
cout << "2 " << x << " " << y2 << "\n";
} else
cout << "2 " << x << " " << y2 << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
int fun(int a, int base) {
int ans = 0, b;
int i = 1;
while (a != 0) {
b = a % 10;
a /= 10;
ans += b * i;
i *= base;
}
return ans;
}
int fun2(int a, int base) {
int d = 0;
while (a != 0) {
d++;
a /= base;
}
return d;
}
int main() {
int c, a, b, d, max, i;
int ans = 0;
scanf("%d%d", &a, &b);
c = a;
max = 0;
while (c != 0) {
d = c % 10;
c /= 10;
if (d > max) max = d;
}
c = b;
while (c != 0) {
d = c % 10;
c /= 10;
if (d > max) max = d;
}
max++;
a = fun(a, max);
b = fun(b, max);
c = a + b;
ans = fun2(c, max);
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int mp[3010][3010];
struct ee {
int d;
int t;
};
ee k1[3010][3];
ee k2[3010][3];
bool ok(ee x, ee y) { return x.d < y.d; }
int n, m;
vector<int> v[3010];
bool s[3010];
const int MX = 90000000;
void bfs(int ii) {
memset(s, 0, sizeof(s));
int ll = 0;
queue<int> q;
int p;
q.push(ii);
while (q.empty() == 0) {
p = q.front();
q.pop();
for (int i = 0; i < v[p].size(); i++) {
if (s[v[p][i]] == 0) {
q.push(v[p][i]);
mp[ii][v[p][i]] = min(mp[ii][p] + 1, mp[ii][v[p][i]]);
s[v[p][i]] = 1;
}
}
}
}
int main() {
int s1, s2, t1, t2, l1, l2;
while (scanf("%d %d", &n, &m) != EOF) {
memset(k1, 0, sizeof(k1));
memset(k2, 0, sizeof(k2));
int a, b;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) {
mp[i][j] = 0;
} else {
mp[i][j] = MX;
}
}
}
for (int i = 0; i < m; i++) {
scanf("%d %d", &a, &b);
v[a].push_back(b);
}
for (int i = 1; i <= n; i++) {
bfs(i);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (mp[i][j] == MX) {
mp[i][j] = 0;
}
if (mp[i][j] > k1[i][0].d) {
k1[i][0].d = mp[i][j];
k1[i][0].t = j;
sort(k1[i], k1[i] + 3, ok);
}
if (mp[i][j] > k2[j][0].d) {
k2[j][0].d = mp[i][j];
k2[j][0].t = i;
sort(k2[j], k2[j] + 3, ok);
}
}
}
int ss = 0;
int ii = 0, jj = 0, kk = 0, ll = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j || mp[i][j] == 0) {
continue;
}
for (int k = 0; k < 3; k++) {
if (i == k1[j][k].t || j == k1[j][k].t || k1[j][k].t == 0) {
continue;
}
for (int l = 0; l < 3; l++) {
if (i == k2[i][l].t || j == k2[i][l].t ||
k1[j][k].t == k2[i][l].t || k2[i][l].t == 0) {
continue;
}
if (k1[j][k].d + mp[i][j] + k2[i][l].d > ss) {
ss = k1[j][k].d + mp[i][j] + k2[i][l].d;
ii = k2[i][l].t;
jj = i;
kk = j;
ll = k1[j][k].t;
}
}
}
}
}
printf("%d %d %d %d\n", ii, jj, kk, ll);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void _print(long long t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> v1(n);
vector<int> v2(n);
for (auto& ele : v1) {
cin >> ele;
}
for (auto& ele : v2) {
cin >> ele;
}
vector<int> p(n);
for (int i = 0; i < n; i++) {
p[v1[i] - 1] = v2[i];
}
for (int i = 0; i < n; i++) {
cout << p[i] << " ";
}
cout << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 3e5 + 2, inf = 1e9 + 7;
void _run();
void no() { cout << "-1\n", exit(0); }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
_run();
return 0;
}
vector<pair<int, int> > C, D;
int T[N];
void build(int nod, int lo, int hi) {
if (lo == hi) {
T[nod] = C[lo - 1].second;
return;
}
int md = (lo + hi) >> 1;
build(2 * nod, lo, md);
build(2 * nod + 1, md + 1, hi);
T[nod] = max(T[2 * nod], T[2 * nod + 1]);
}
int qry(int nod, int lo, int hi, int l, int r) {
if (hi < l || lo > r || lo > hi) return 0;
if (lo >= l && hi <= r) return T[nod];
int md = (lo + hi) >> 1;
return max(qry(2 * nod, lo, md, l, r), qry(2 * nod + 1, md + 1, hi, l, r));
}
void _run() {
int n, c, d;
cin >> n >> c >> d;
for (int i = 1; i <= n; i++) {
int b, p;
string s;
cin >> b >> p >> s;
if (s[0] == 'C')
C.push_back(make_pair(p, b));
else
D.push_back(make_pair(p, b));
}
sort(C.begin(), C.end());
sort(D.begin(), D.end());
int rs = 0;
int a = 0, b = 0;
for (auto x : C) {
if (x.first <= c) a = max(a, x.second);
}
for (auto x : D) {
if (x.first <= d) b = max(b, x.second);
}
rs = a + b;
int i = 0;
if (a == 0 || b == 0) rs = 0;
if ((int)C.size() != 0) build(1, 1, (int)C.size());
for (auto x : C) {
i++;
if (x.first > c) continue;
int lo = i + 1, hi = (int)C.size(), md, cur = -1;
while (lo <= hi) {
md = (lo + hi) >> 1;
if (C[md - 1].first > c - x.first)
hi = md - 1;
else
lo = md + 1, cur = md;
}
if (cur == -1) continue;
cur--;
if (C[cur].first <= c - x.first) {
int rt = qry(1, 1, (int)C.size(), i + 1, cur + 1);
rs = max(rs, rt + x.second);
}
}
C.clear();
C = D;
c = d;
i = 0;
if ((int)C.size() != 0) build(1, 1, (int)C.size());
for (auto x : C) {
i++;
if (x.first > c) continue;
int lo = i + 1, hi = (int)C.size(), md, cur = -1;
while (lo <= hi) {
md = (lo + hi) >> 1;
if (C[md - 1].first > c - x.first)
hi = md - 1;
else
lo = md + 1, cur = md;
}
if (cur == -1) continue;
cur--;
if (C[cur].first <= c - x.first) {
int rt = qry(1, 1, (int)C.size(), i + 1, cur + 1);
rs = max(rs, rt + x.second);
}
}
cout << rs << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int a = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
const int _ = 1e7, P = 1e9 + 7;
int T, N, fac[_], ifac[_];
int upd(int x) { return x + (x >> 31 & P); }
pair<int, int> operator*(pair<int, int> p, pair<int, int> q) {
int v2 = 1ll * p.first * q.first % P,
v1 = (1ll * p.first * q.second + 1ll * p.second * q.first) % P,
v0 = 1ll * p.second * q.second % P;
return pair<int, int>((v1 - v2 + P) % P, (v0 - v2 + P) % P);
}
pair<int, int> operator*(pair<int, int> p, int q) {
return pair<int, int>(1ll * p.first * q % P, 1ll * p.second * q % P);
}
pair<int, int> operator+(pair<int, int> p, pair<int, int> q) {
return pair<int, int>(upd(p.first + q.first - P),
upd(p.second + q.second - P));
}
pair<int, int> operator-(pair<int, int> p, pair<int, int> q) {
return pair<int, int>(upd(p.first - q.first), upd(p.second - q.second));
}
int poww(long long a, int b, int tms) {
while (b) {
if (b & 1) tms = tms * a % P;
a = a * a % P;
b >>= 1;
}
return tms;
}
int binom(int p, int q) { return 1ll * fac[p] * ifac[q] % P * ifac[p - q] % P; }
struct dat {
pair<int, int> ans[3000003];
void init(pair<int, int> v) {
pair<int, int> iv = !v.first ? pair<int, int>(0, 1)
: (v.first == 1 ? pair<int, int>(P - 1, P - 1)
: pair<int, int>(1, 0));
ans[3 * N] = pair<int, int>(0, 1);
for (int i = 3 * N - 1; i; --i)
ans[i] = pair<int, int>(0, binom(3 * N + 1, i + 1)) - ans[i + 1] +
iv * ans[i + 1];
}
} Base[3];
int main() {
ios::sync_with_stdio(0);
N = read();
fac[0] = 1;
for (int i = 1; i <= 3 * N + 1; ++i) fac[i] = 1ll * fac[i - 1] * i % P;
ifac[3 * N + 1] = poww(fac[3 * N + 1], P - 2, 1);
for (int i = 3 * N; ~i; --i) ifac[i] = ifac[i + 1] * (i + 1ll) % P;
Base[0].init(pair<int, int>(0, 1));
Base[1].init(pair<int, int>(1, 0));
Base[2].init(pair<int, int>(P - 1, P - 1));
for (T = read(); T; --T) {
int v = read();
cout << ((Base[0].ans[v] + Base[1].ans[v] + Base[2].ans[v]) *
poww(3, P - 2, 1))
.second
<< '\n';
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
char name[500010][12];
int dp[26][26];
int val(char c) { return c - 'a'; }
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", name[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
int len = strlen(name[i]);
int firstchar = val(name[i][0]);
int lastchar = val(name[i][len - 1]);
if (j == firstchar) {
if (dp[firstchar][firstchar] + len > dp[firstchar][lastchar]) {
dp[firstchar][lastchar] = dp[firstchar][firstchar] + len;
}
} else {
if (dp[j][firstchar] > 0 && dp[j][firstchar] + len > dp[j][lastchar])
dp[j][lastchar] = dp[j][firstchar] + len;
}
}
}
int best = 0;
for (int i = 0; i < 26; i++)
if (dp[i][i] > best) best = dp[i][i];
printf("%d\n", best);
return 0;
}
| 3 |
#include <bits/stdc++.h>
int dr[] = {2, 2, -2, -2, 1, -1, 1, -1};
int dc[] = {1, -1, 1, -1, 2, 2, -2, -2};
int dr1[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dc1[] = {1, -1, 1, 0, -1, 0, 1, -1};
int dr2[] = {0, 0, 1, -1};
int dc2[] = {1, -1, 0, 0};
using namespace std;
long long a[500005], b[500005], mark[500005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tc, n, i, j, m;
cin >> tc;
while (tc--) {
cin >> n >> m;
vector<long long> v;
for (i = 1; i <= n * 3; i++) mark[i] = 0;
for (i = 1; i <= m; i++) {
long long x, y;
cin >> x >> y;
if (mark[x] == 1 || mark[y] == 1) continue;
mark[x] = mark[y] = 1;
v.push_back(i);
}
if (v.size() >= n) {
cout << "Matching" << endl;
for (i = 0; i < n; i++) cout << v[i] << " ";
cout << endl;
} else {
cout << "IndSet" << endl;
long long cn = n;
for (i = 1; i <= n * 3; i++) {
if (cn == 0) break;
if (mark[i] == 0) {
cout << i << " ";
cn--;
}
}
cout << endl;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int a[220000], b[220000], i, n, m, k, c, j, jc;
long long s;
int main() {
scanf("%d%d%d", &n, &m, &k);
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + 1 + n);
c = b[n + 1 - m * k];
jc = 0;
s = 0;
for (i = n + 1 - m * k; i <= n; i++) {
s = s + b[i];
if (b[i] == c) jc++;
}
printf("%lld\n", s);
j = 0;
for (i = 1; i <= n; i++) {
if (a[i] > c) {
j++;
if (j % m == 0) printf("%d ", i);
}
if (a[i] == c && jc > 0) {
j++;
jc--;
if (j % m == 0) printf("%d ", i);
}
if (j == m * k - m) break;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
x = flag ? -x : x;
}
inline void read(long long &x) {
char ch;
bool flag = false;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') flag = true;
for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
x = flag ? -x : x;
}
inline void write(int x) {
static const int maxlen = 100;
static char s[maxlen];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
int len = 0;
for (; x; x /= 10) s[len++] = x % 10 + '0';
for (int i = len - 1; i >= 0; --i) putchar(s[i]);
}
const int MAXN = 110000;
const int MAXM = 210000;
int du[MAXN];
const long long P = 1000000007ll;
int n;
int main() {
read(n);
for (int i = 1; i < n; i++) {
int a, b;
read(a);
read(b);
du[a]++;
du[b]++;
}
long long tmp = 0;
for (int i = 1; i <= n; i++)
if (du[i] == 1) tmp++;
long long ans = 1;
for (int i = 1; i <= n - tmp; i++) ans = ans * 2ll % P;
printf("%I64d\n", 1ll * (n + tmp) * ans % P);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int rev(int n) {
int res = 0;
while (n) {
res = res * 10 + n % 10;
n /= 10;
}
return res;
}
int main() {
int a1, a2;
cin >> a1 >> a2;
cout << a1 + rev(a2) << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
int a[N];
int dp[N][N];
int sol_dp(int l, int r) {
assert(l <= r);
if (l == r) return dp[l][r] = a[l];
if (dp[l][r] != 0) return dp[l][r];
dp[l][r] = -1;
for (int i = l; i < r; i++) {
int lmerge = sol_dp(l, i);
int rmerge = sol_dp(i + 1, r);
if (lmerge > 0 && lmerge == rmerge) dp[l][r] = lmerge + 1;
}
return dp[l][r];
}
int dp2[N];
void sol2_dp(int n) {
for (int i = 0; i < n; i++)
for (int j = i + 1; j <= n; j++)
if (sol_dp(i + 1, j) > 0) dp2[j] = min(dp2[j], dp2[i] + 1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) dp2[i] = 2e9;
sol2_dp(n);
cout << dp2[n] << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void ch_max(int &x, int y) {
if (x < y) x = y;
}
void ch_min(int &x, int y) {
if (x > y) x = y;
}
const double pi = acos(-1.0);
const double eps = 1e-9;
const int N = 100005;
int prime[N], tot;
void cal_prime() {
int i, j, k;
memset(prime, -1, sizeof(prime));
for (i = 2; i < N; ++i) {
if (prime[i] == 0) continue;
for (j = i + i; j < N; j += i) prime[j] = 0;
}
tot = 0;
for (i = 2; i < N; ++i) {
if (prime[i]) prime[tot++] = i;
}
}
char str[10][10];
int len, ans;
int a[N][10], b[N];
void cal_a() {
int i, j, k;
for (i = 0; i < tot; ++i) {
int tmp = prime[i];
for (j = 0; j < 10; ++j) {
a[i][j] = tmp % 10;
tmp /= 10;
}
}
for (i = 0; i < N; ++i) {
b[i] = lower_bound(prime, prime + tot, i) - prime;
}
}
void solve(int t) {
int i, j, k;
if (t == len) {
ans++;
return;
}
int tmp = 0;
for (i = 0; i < t; ++i) {
tmp = tmp * 10 + str[t][i] - '0';
}
int t0 = 1;
for (; i < len; ++i) {
tmp *= 10;
t0 *= 10;
}
int l = b[tmp];
int r = b[tmp + t0];
if (t == len - 1) {
ans += r - l;
return;
}
for (i = l; i < r; ++i) {
for (k = 0, j = len - 1; j >= t; --j) {
str[t][j] = a[i][k++] + '0';
}
for (j = t + 1; j < len; ++j) str[j][t] = str[t][j];
solve(t + 1);
}
}
int main() {
int i, j, k, t, nc = 0;
int t0, t1, t2, t3;
cal_prime();
cal_a();
scanf("%d", &t);
while (t--) {
memset(str, 0, sizeof(str));
scanf("%s", str[0]);
len = strlen(str[0]);
ans = 0;
for (i = 1; i < len; ++i) str[i][0] = str[0][i];
solve(1);
printf("%d\n", ans);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline int in(register T& num) {
register char c = 0;
num = 0;
bool n = false;
while (c < 33) c = getchar();
while (c > 33) {
if (c == '-')
n = true;
else
num = num * 10 + c - '0';
c = getchar();
}
num = n ? -num : num;
return 1;
}
class BerserkAndFireball {
public:
int n, m;
long long x, k, y;
int araA[2 * 100002];
int araB[2 * 100002];
BerserkAndFireball() { input(); }
void input() {
in(n), in(m);
in(x), in(k), in(y);
for (int i = 0; i < n; i++) {
in(araA[i]);
index[araA[i]] = i + 1;
}
for (int i = 0; i < m; i++) in(araB[i]);
}
int maxInRange(int l, int r) {
int mx = -1 << 30;
for (int i = l + 1; i < r; i++) mx = max(mx, araA[i]);
return mx;
}
long long solution() {
long long cost = 0;
int lastPos = -1;
for (int i = 0; i < m; i++) {
int curPos = index[araB[i]] - 1;
if (curPos == -1 || curPos < lastPos) return -1;
int mxInPair = i > 0 ? max(araB[i], araB[i - 1]) : araA[i];
long long tmpCost = _calculate(lastPos, curPos, mxInPair);
if (tmpCost == -1) return -1;
cost += tmpCost;
lastPos = curPos;
}
long long tmpCost = _calculate(lastPos, n, araB[m - 1]);
if (tmpCost == -1) return -1;
return cost + tmpCost;
}
long long _calculate(int lastPos, int curPos, int mxInPair) {
long long costA, costB;
int mxVal = maxInRange(lastPos, curPos);
if (mxInPair < mxVal) {
if ((curPos - lastPos - 1) < k) return -1;
costA =
((curPos - lastPos - 1) / k) * x + ((curPos - lastPos - 1) % k) * y;
costB = x + (curPos - lastPos - 1 - k) * y;
} else {
costA =
((curPos - lastPos - 1) / k) * x + ((curPos - lastPos - 1) % k) * y;
costB = (curPos - lastPos - 1) * y;
}
return min(costA, costB);
}
private:
map<int, int> index;
};
int main() {
auto solve = BerserkAndFireball();
cout << solve.solution();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, temp2, m, temp1;
cin >> n >> m;
if (m == 1 && n >= 2)
cout << 2;
else if (m == 1 && n == 1)
cout << 1;
else {
int left = m - 1;
int right = n - m;
if (left > right)
cout << m - 1;
else if (left < right)
cout << m + 1;
else
cout << m - 1;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9 + 10;
const int MOD = 1e9 + 7;
const int N = 1e6 + 10;
int n, k, m;
string x, y, z;
int main() {
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> x >> y;
n = x.length();
int ans = 1;
for (int i = 0; i < n; i++) {
if (y[i] > x[i]) {
cout << -1;
return 0;
}
z += min(x[i], y[i]);
}
cout << z;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> Zalgo(string s) {
int L = -1, R = -1;
vector<int> z(s.size() + 1);
for (int i = 1; i < s.size(); i++) {
if (i > R) {
L = R = i;
while (R < s.size() && s[R] == s[R - L]) R++;
z[i] = R - L;
R--;
} else {
if (z[i - L] < R - i + 1)
z[i] = z[i - L];
else {
L = i;
while (R < s.size() && s[R - L] == s[R]) R++;
z[i] = R - L;
R--;
}
}
}
z[0] = s.size();
return z;
}
string y;
string x, pat;
long long n, memo[(int)1e6 + 5][2];
vector<int> vec;
int nxt[(int)1e6 + 5];
const int mod = 1e9 + 7;
long long solve(int idx, bool f) {
if (idx == ((int)((x).size()))) return 1;
if (memo[idx][f] != -1) return memo[idx][f];
long long ret = 0;
if (f) {
ret = (solve(idx + 1, f) + solve(idx + 1, 0)) % mod;
if (nxt[idx] != -1) ret = (ret + solve(nxt[idx], 1)) % mod;
} else {
if (nxt[idx] != -1) ret = solve(nxt[idx], 1);
ret = (ret + solve(idx + 1, 0)) % mod;
}
return memo[idx][f] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> x >> pat;
n = ((int)((pat).size()));
y = pat + "$" + x;
vec = Zalgo(y);
memset(memo, -1, sizeof memo);
int last = -1;
for (int i = ((int)((x).size())) - 1; i >= 0; i--) {
if (vec[i + n + 1] == n) last = i + n;
nxt[i] = last;
}
long long sum = 0;
for (auto i = 0; i < (long long)(((int)((x).size()))); i++) {
if (nxt[i] != -1) sum = (sum + solve(nxt[i], 1)) % mod;
}
cout << sum;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.);
const long long mod = 1000000007;
inline void prep() {
cin.tie(0);
cin.sync_with_stdio(0);
};
int main() {
prep();
int c, d, n, m, k;
cin >> c >> d >> n >> m >> k;
int minn = 9000000;
for (int i = 0; i <= m; i++) {
int many = n * i + k;
int extra = 0;
if (many < n * m) {
extra = n * m - many;
}
int problems = i * c + extra * d;
minn = min(problems, minn);
}
cout << minn;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < (long long int)(v.size()); i++) cin >> v[i];
map<long long int, long long int> m;
long long int f = 0;
for (long long int i = 0; i < (long long int)(n); i++) {
m[v[i]]++;
if (m[v[i]] > 1) f = 1;
}
vector<long long int> p = v;
vector<long long int> q = v;
sort(q.begin(), q.end());
reverse(q.begin(), q.end());
sort(v.begin(), v.end());
if (p == v || f)
cout << "YES"
<< "\n";
else if (p == q)
cout << "NO"
<< "\n";
else
cout << "YES"
<< "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
};
int T, n, m;
vector<Point> pos[30];
char s[2010][2010];
int tot;
pair<Point, Point> ans[30];
void init() {
tot = 0;
for (int i = 1; i <= 26; i++) {
pos[i].clear();
}
}
bool check(int id) {
if (pos[id].size() <= 1) return true;
int t = 0;
if (pos[id][0].x == pos[id][1].x) t = 1;
if (pos[id][0].y == pos[id][1].y) t = 2;
if (!t) return false;
for (int i = 1; i < (int)pos[id].size(); i++) {
if (t == 1 && pos[id][i].x != pos[id][i - 1].x) return false;
if (t == 2 && pos[id][i].y != pos[id][i - 1].y) return false;
}
if (t == 1) {
int x = pos[id][0].x;
for (int y = pos[id].front().y; y <= pos[id].back().y; y++) {
if (s[x][y] == '.' || s[x][y] < 'a' + id - 1) return false;
}
} else {
int y = pos[id][0].y;
for (int x = pos[id].front().x; x <= pos[id].back().x; x++) {
if (s[x][y] == '.' || s[x][y] < 'a' + id - 1) return false;
}
}
return true;
}
bool check() {
for (int i = 1; i <= 26; i++) {
if (!check(i)) return false;
}
return true;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
init();
for (int i = 1; i <= n; i++) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= m; j++) {
if (s[i][j] != '.') {
tot = max(tot, s[i][j] - 'a' + 1);
pos[s[i][j] - 'a' + 1].push_back({i, j});
}
}
}
if (!tot)
printf("YES\n0\n");
else if (!check())
printf("NO\n");
else {
for (int i = tot; i >= 1; i--) {
if (pos[i].empty()) {
ans[i] = ans[i + 1];
continue;
}
ans[i].first = pos[i].front();
ans[i].second = pos[i].back();
}
printf("YES\n%d\n", tot);
for (int i = 1; i <= tot; i++) {
printf("%d %d %d %d\n", ans[i].first.x, ans[i].first.y, ans[i].second.x,
ans[i].second.y);
}
}
}
getchar();
getchar();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool check(string str, vector<string> mat) {
int count = 0;
for (int i = 0; i < mat.size(); i++) {
count = 0;
for (int j = 0; j < mat[0].size(); j++) {
if (mat[i][j] != str[j]) {
count++;
if (count >= 2) return false;
}
}
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n, m, i, j, f = 0;
string ans = "";
string str;
cin >> n >> m;
vector<string> mat;
for (i = 0; i < n; i++) {
cin >> str;
mat.push_back(str);
}
for (i = 0; i < m; i++) {
for (j = 1; j < n; j++) {
if (mat[j][i] != mat[j - 1][i]) {
f = 1;
if (check(ans + mat[j][i] + mat[j - 1].substr(i + 1), mat)) {
cout << ans + mat[j][i] + mat[j - 1].substr(i + 1) << endl;
break;
}
if (check(ans + mat[j - 1][i] + mat[j].substr(i + 1), mat)) {
cout << ans + mat[j - 1][i] + mat[j].substr(i + 1) << endl;
break;
}
}
}
if (f) {
break;
} else
ans += mat[j - 1][i];
}
if (f && j == n) {
cout << -1 << endl;
} else if (f == 0)
cout << ans << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct build {
long long x, y, d;
long long k;
};
int n;
long long m;
build p[10005];
bool cmp(build a, build b) {
if (a.d < b.d)
return 1;
else
return 0;
}
int main() {
scanf("%d%I64d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%I64d%I64d%I64d", &p[i].x, &p[i].y, &p[i].k);
p[i].d = p[i].x * p[i].x + p[i].y * p[i].y;
}
sort(p + 1, p + n + 1, cmp);
long long dis = 0;
for (int i = 1; i <= n; i++) {
if (m >= 1000000) {
double q = dis;
q = sqrt(q);
printf("%.9lf", q);
return 0;
}
dis = p[i].d;
m += p[i].k;
}
if (m >= 1000000) {
double q = dis;
q = sqrt(q);
printf("%.9lf", q);
return 0;
}
printf("-1");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, s, t, E;
int last[201 + 5], vis[201 + 5];
int to[2 * 1000 + 5], cap[2 * 1000 + 5], Next[2 * 1000 + 5], C[2 * 1000 + 5];
void add_edge(int u, int v, int uv, int vu = 0) {
to[E] = v;
C[E] = uv;
cap[E] = uv;
Next[E] = last[u];
last[u] = E++;
to[E] = u;
C[E] = vu;
cap[E] = vu;
Next[E] = last[v];
last[v] = E++;
}
int dfs(int u, int f) {
if (u == t) return f;
if (vis[u]) return 0;
vis[u] = 1;
for (int e = last[u]; e != -1; e = Next[e]) {
int v = to[e];
if (cap[e]) {
int ret = dfs(v, min(f, cap[e]));
if (ret) {
cap[e] -= ret;
cap[e ^ 1] += ret;
return ret;
}
}
}
return 0;
}
int maxFlow() {
int flow = 0;
while (1) {
for (int i = 0; i <= 2 * n + 2; i++) vis[i] = 0;
int f = dfs(s, (1 << 30));
if (!f) break;
flow += f;
}
return flow;
}
int a[200], b[200], M[200][200];
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
memset(last, -1, sizeof(last));
int sum1 = 0, sum2 = 0;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
add_edge(x, y + n, (1 << 30));
add_edge(y, x + n, (1 << 30));
}
for (int i = 1; i <= n; i++) {
sum1 += a[i - 1];
sum2 += b[i - 1];
add_edge(0, i, a[i - 1]);
add_edge(i + n, 2 * n + 1, b[i - 1]);
add_edge(i, i + n, (1 << 30));
}
s = 0, t = 2 * n + 1;
int ans = maxFlow();
if (ans < sum1 || sum1 != sum2) {
cout << "NO" << endl;
return 0;
}
for (int i = 0; i < E; i++) {
if (cap[i] < C[i]) {
int v = to[i];
int u = to[i ^ 1];
if (u >= 1 && u <= n && v >= 1 && v <= 2 * n) {
M[u - 1][v - n - 1] = C[i] - cap[i];
}
}
}
cout << "YES" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j) cout << " ";
cout << M[i][j];
}
cout << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int cage[N], n;
pair<int, int> hijo[N];
int find(int u) {
if (cage[u] == 0) {
return u;
} else {
return cage[u] = find(cage[u]);
}
}
void show(int u) {
if (u <= n) {
cout << u << " ";
return;
}
show(hijo[u].first), show(hijo[u].second);
}
int main() {
cin >> n;
int x, y;
for (int i = 1; i < n; ++i) {
cin >> x >> y;
x = find(x), y = find(y);
cage[x] = cage[y] = n + i;
hijo[n + i] = make_pair(x, y);
}
show(find(1));
return 0;
}
| 4 |
#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];
}
long long int ans = 0, curr = 0;
for (int i = n - 1; i >= 0; i--) {
curr += (a[i] == 0);
if (a[i] == 1) {
ans += curr;
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <list>
#include <cmath>
using namespace std;
using lol = long long;
int main()
{
//freopen("inp.txt", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t, n, pre, a;
cin >> t;
while (t--)
{
cin >> n;
if (n == 1)
cout << 9;
else
{
cout << 98;
pre = 8;
n -= 2;
while (n--)
{
pre += 1;
if (pre == 10)
pre = 0;
cout << pre;
}
}
cout << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int ret = 0, c, f = 1;
for (c = getchar(); !(isdigit(c) || c == '-'); c = getchar())
;
if (c == '-') f = -1, c = getchar();
for (; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
if (f < 0) ret = -ret;
return ret;
}
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long a, b;
cin >> a >> b;
long long lol = gcd(a, b);
a /= lol;
b /= lol;
long long period;
period = lcm(a, b);
set<long long> g;
for (long long q = a; q <= period; q += a) g.insert(q);
for (long long q = b; q <= period; q += b) g.insert(q);
long long ans_a, ans_b, last;
ans_a = ans_b = last = 0;
for (auto it = g.begin(); it != g.end(); ++it) {
long long k = (*it);
bool da = (k % a == 0);
bool db = (k % b == 0);
if (da && db) {
if (a > b) {
ans_a += k - last;
} else {
ans_b += k - last;
}
} else if (da) {
ans_a += k - last;
} else {
ans_b += k - last;
}
last = k;
}
if (ans_a == ans_b) {
cout << "Equal";
} else if (ans_a > ans_b) {
cout << "Dasha";
} else {
cout << "Masha";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
char s[111][111];
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (s[i][j] >= '1' && s[i][j] <= '8') {
int cnt = 0;
for (int k = 0; k < 8; k++) {
int tx = i + dx[k];
int ty = j + dy[k];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[tx][ty] == '*')
cnt++;
}
if (cnt != s[i][j] - '0') return 0 * printf("NO\n");
} else if (s[i][j] == '.') {
for (int k = 0; k < 8; k++) {
int tx = i + dx[k];
int ty = j + dy[k];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[tx][ty] == '*')
return 0 * printf("NO\n");
}
}
}
puts("YES");
}
| 1 |
#include <bits/stdc++.h>
const int maxn = 5005;
int n, a[maxn];
int f[maxn][maxn], g[maxn];
int ans[maxn];
int calcu(int x, int y, int z) {
return std::max(x - (y - 1), 0) + std::max(z - (y - 1), 0);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
memset(f, 0x3f, sizeof(f));
memset(g, 0x3f, sizeof(g));
for (int i = 1; i <= n; i++) {
f[i][1] = calcu(a[i - 1], a[i], a[i + 1]);
if (i - 2 >= 1) {
int x = std::min(a[i - 2] - 1, a[i - 1]);
for (int j = 2; j <= (i + 1) / 2; j++) {
f[i][j] = std::min(f[i][j], f[i - 2][j - 1] + calcu(x, a[i], a[i + 1]));
}
}
if (i - 3 >= 1) {
for (int j = 1; j <= n; j++) g[j] = std::min(g[j], f[i - 3][j]);
for (int j = 2; j <= (i + 1) / 2; j++) {
f[i][j] = std::min(f[i][j], g[j - 1] + calcu(a[i - 1], a[i], a[i + 1]));
}
}
}
memset(ans, 0x3f, sizeof(ans));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= (i + 1) / 2; j++) ans[j] = std::min(ans[j], f[i][j]);
}
for (int i = 1; i <= (n + 1) / 2; i++)
printf("%d%c", ans[i], " \n"[i == (n + 1) / 2]);
return 0;
}
| 5 |
#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;
long long MOD = 1000000007;
long double EPS = 1e-9;
long long binpow(long long b, long long p, long long mod) {
long long ans = 1;
b %= mod;
for (; p; p >>= 1) {
if (p & 1) ans = ans * b % mod;
b = b * b % mod;
}
return ans;
}
void pre() {}
vector<vector<long long> > g;
long long sz[100100];
long long ht[100100];
long long seq[100100];
long long label[100100];
vector<long long> mover;
void dsz(int nn) {
sz[nn] = 1;
ht[nn] = 1;
for (auto v : g[nn]) {
dsz(v);
(ht[nn] = max((ht[nn]), (1 + ht[v])));
sz[nn] += sz[v];
}
}
int treeend = 0;
void dfs(int nn, int rightmost = 1) {
label[treeend] = nn;
treeend++;
sort((g[nn]).begin(), (g[nn]).end(),
[&](int a, int b) { return ht[a] < ht[b]; });
for (int i = 0; i < g[nn].size(); i++) {
if (i == (long long)(g[nn]).size() - 1) {
dfs(g[nn][i], rightmost);
} else {
dfs(g[nn][i], 0);
}
}
if (!rightmost) {
mover.emplace_back(treeend);
}
}
void solve() {
long long n;
cin >> n;
g.resize(n);
for (long long i = 1; i <= n - 1; ++i) {
long long x;
cin >> x;
g[x].emplace_back(i);
}
dsz(0);
dfs(0, 1);
for (long long i = 0; i <= n - 1; ++i) {
cout << label[i] << " ";
}
cout << '\n';
cout << mover.size() << '\n';
for (auto v : mover) {
cout << label[v] << " ";
}
cout << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
pre();
long long _t = 1;
for (long long i = 1; i <= _t; i++) {
solve();
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 1;
const long long INF = 1e9 + 1;
const long long MOD = 1e9;
const long long BITS = 64;
const long double EPS = 1e-12;
void bye() {
cout << "\n";
exit(0);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> cnt(k);
for (int i = 0; i < (long long)(s).size(); i++) {
cnt[s[i] - 'A']++;
}
cout << *min_element((cnt).begin(), cnt.end()) * k;
bye();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, sum = 0;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
if (n < 6 && n != 1 && n % 3 != 0) {
cout << "-1"
<< "\n";
continue;
}
if (n == 1) {
cout << sum << "\n";
continue;
}
while (true) {
if (n % 6 == 0) {
n /= 6;
sum++;
} else {
n *= 2;
sum++;
}
if (n == 1) {
cout << sum << "\n";
break;
}
if (n < 6 && n != 1 && n % 3 != 0) {
cout << "-1"
<< "\n";
break;
}
}
sum = 0;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, ans = 0;
cin >> n;
while (n >= 100) {
n -= 100;
ans++;
}
while (n >= 20) {
n -= 20;
ans++;
}
while (n >= 10) {
n -= 10;
ans++;
}
while (n >= 5) {
n -= 5;
ans++;
}
while (n >= 1) {
n -= 1;
ans++;
}
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, b, c;
int a[200000];
const int INF = 2e9;
priority_queue<long long> q;
int getMod(int x) {
int res = x % 5;
if (res < 0) res += 5;
return res;
}
int main() {
scanf("%d%d%d%d", &n, &k, &b, &c);
for (int i = 0; i < (int)(n); ++i) scanf("%d", a + i);
sort(a, a + n);
long long ans = 9e18;
b = min(b, 5 * c);
for (int i = 0; i < (int)(5); ++i) {
int x = INF + i;
long long tot = 0;
while (!q.empty()) q.pop();
for (int j = 0; j < (int)(n); ++j) {
int y = a[j];
long long cost = 0;
while (getMod(y) != i) ++y, cost += c;
cost += (long long)b * (((long long)x - y) / 5);
q.push(cost);
tot += cost;
if ((int)q.size() > k) {
tot -= q.top();
q.pop();
}
int num = a[j];
while (getMod(num) != i) ++num;
if (j >= k - 1) {
ans = min(ans, tot - ((long long)x - num) / 5 * (long long)b * k);
}
}
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int freq[26];
memset(freq, 0, sizeof(freq));
for (char c : s) freq[c - 'a']++;
for (int i = 25; i >= 0; i--) {
if (freq[i] % 2) {
for (int j = 0; j < 26; j++) {
if (i == j) continue;
if (freq[j] % 2) {
freq[j]++;
freq[i]--;
break;
}
}
}
}
char ch = '#';
string ans = "";
for (int i = 0; i < 26; i++) {
if (freq[i] % 2) ch = char(i + 'a');
freq[i] /= 2;
while (freq[i]) {
ans.push_back(char(i + 'a'));
freq[i]--;
}
}
cout << ans;
if (ch != '#') {
cout << ch;
}
reverse(ans.begin(), ans.end());
cout << ans << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
int a[200005];
int ans[200005];
int main() {
int n;
scanf("%d", &n);
int first = -1;
int last = -1;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
if (first == -1 && a[i] == 0) first = i;
if (a[i] == 0) last = i;
}
memset(ans, -1, sizeof(ans));
for (int i = first; i < n; i++) {
if (a[i] == 0)
ans[i] = 0;
else if (ans[i] == -1 || ans[i] > ans[i - 1] + 1)
ans[i] = ans[i - 1] + 1;
}
for (int i = last; i >= 0; i--) {
if (a[i] == 0)
ans[i] = 0;
else if (ans[i] == -1 || ans[i] > ans[i + 1] + 1)
ans[i] = ans[i + 1] + 1;
}
for (int i = 0; i < n; i++) printf("%d ", ans[i]);
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 2e+5 + 8;
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int t = 1;
long long n;
pair<long long, long long> seg[4 * MAXN + 8];
vector<pair<long long, long long> > arr;
vector<long long> v;
void build(long long n, long long st, long long en) {
if (st == en) {
seg[n] = arr[st];
return;
}
long long mid = (st + en) / 2;
build(2 * n, st, mid);
build(2 * n + 1, mid + 1, en);
seg[n].first = seg[2 * n].first + seg[2 * n + 1].first;
seg[n].second = seg[2 * n].second + seg[2 * n + 1].second;
}
void update(long long n, long long st, long long en, long long pos,
long long val1, long long val2) {
if (st == en) {
seg[n].first += val1;
seg[n].second += val2;
return;
}
long long mid = (st + en) / 2;
if (pos <= mid)
update(2 * n, st, mid, pos, val1, val2);
else
update(2 * n + 1, mid + 1, en, pos, val1, val2);
seg[n].first = seg[2 * n].first + seg[2 * n + 1].first;
seg[n].second = seg[2 * n].second + seg[2 * n + 1].second;
}
pair<long long, long long> query(long long n, long long st, long long en,
long long l, long long r) {
if (l <= st && en <= r) return seg[n];
if (st > r || en < l) return {0, 0};
long long mid = (st + en) / 2;
pair<long long, long long> lq = query(2 * n, st, mid, l, r);
pair<long long, long long> rq = query(2 * n + 1, mid + 1, en, l, r);
pair<long long, long long> res;
res.first = lq.first + rq.first;
res.second = lq.second + rq.second;
return res;
}
long long ans = 0;
void MAIN() {
cin >> n;
arr = vector<pair<long long, long long> >(n + 1, {0, 0});
v = vector<long long>(n + 1, 0);
for (long long i = (1); i <= n; i++) cin >> arr[i].first;
for (long long i = (1); i <= n; i++) cin >> arr[i].second;
for (long long i = (1); i <= n; i++) v[i] = arr[i].second;
sort(1 + (arr).begin(), (arr).end());
sort(1 + (v).begin(), (v).end());
for (auto [x, sp] : arr) {
if (x == 0) continue;
long long pos = lower_bound(1 + (v).begin(), (v).end(), sp) - v.begin();
ans += (query(1, 1, n, 1, pos).first * x) - (query(1, 1, n, 1, pos).second);
update(1, 1, n, pos, 1, x);
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
while (t--) {
MAIN();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
long long inf = 4e18;
const long long N = 3e3 + 3;
long long dp[N][N];
string s, t;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long tt = 1;
while (tt--) {
cin >> s >> t;
long long n = s.size(), m = t.size();
s = "&" + s;
t = "&" + t;
for (long long i = m; i <= n; ++i) {
if (i > m || s[i] == t[m]) dp[i][0] = 1;
if (s[i] == t[1]) dp[i][1] = 1;
}
for (long long i = n; i > 1; --i) {
for (long long j = 0; j <= n - i + 1; ++j) {
if (j + 1 > m || s[i - 1] == t[j + 1]) {
dp[i - 1][j + 1] += dp[i][j];
if (dp[i - 1][j + 1] >= mod) dp[i - 1][j + 1] -= mod;
}
if (i > 1 && (j + i - 1 > m || s[i - 1] == t[j + i - 1])) {
dp[i - 1][j] += dp[i][j];
if (dp[i - 1][j] >= mod) dp[i - 1][j] -= mod;
}
}
}
long long ans = 0;
for (long long i = 0; i <= n; ++i) {
ans += dp[1][i];
if (ans >= mod) ans -= mod;
}
cout << ans;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int a[400005] = {0};
int tree[4 * 400005] = {0};
int lazy[4 * 400005] = {0};
vector<pair<int, int> > v[100005];
vector<int> g[100005];
int ans[100005] = {0};
void build(int node, int l, int r) {
if (l == r) {
tree[node] = 0;
lazy[node] = 0;
return;
}
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
void add(int node, int start, int end) {
tree[node] += lazy[node];
if (start != end) {
lazy[2 * node] += lazy[node];
lazy[2 * node + 1] += lazy[node];
}
lazy[node] = 0;
}
int n;
void update(int node, int start, int end, int l, int r, int x) {
if (lazy[node] != 0) {
add(node, start, end);
}
if (l > end || r < start) return;
if (start >= l && end <= r) {
lazy[node] += x;
if (lazy[node] != 0) {
add(node, start, end);
}
} else {
int mid = (start + end) / 2;
update(2 * node, start, mid, l, r, x);
update(2 * node + 1, mid + 1, end, l, r, x);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
}
int query(int node, int start, int end, int l, int r) {
if (lazy[node] != 0) {
add(node, start, end);
}
if (l > end || r < start) return 1e9;
if (start >= l && end <= r) {
return tree[node];
} else {
int mid = (start + end) / 2;
int p1 = query(2 * node, start, mid, l, r);
int p2 = query(2 * node + 1, mid + 1, end, l, r);
return min(p1, p2);
}
}
int main() {
int q, k;
int l[200005], r[200005];
cin >> q >> k;
vector<pair<int, int> > s[200005];
for (int i = 1; i <= q; i++) {
cin >> l[i] >> r[i];
update(1, 1, 200000, l[i], r[i], 1);
s[l[i]].push_back(make_pair(r[i], i));
}
for (int i = 1; i <= 200000; i++) {
sort(s[i].begin(), s[i].end());
}
set<pair<int, int> > ss;
vector<int> v;
for (int i = 1; i <= 200000; i++) {
int x = query(1, 1, 200000, i, i);
if (x > k) {
int p = x - k;
if (ss.size() == 0) {
int y = s[i].size() - 1;
for (int j = 1; j <= x - k; j++) {
update(1, 1, 200000, i, s[i][y].first, -1);
v.push_back(s[i][y].second);
y--;
}
for (int j = y; j >= 0; j--) {
ss.insert(make_pair(-1 * s[i][j].first, s[i][j].second));
}
} else {
int y = s[i].size() - 1;
while (p > 0) {
p--;
auto it = ss.begin();
if (y >= 0 && it != ss.end() && s[i][y].first >= (-1 * it->first)) {
update(1, 1, 200000, i, s[i][y].first, -1);
v.push_back(s[i][y].second);
y--;
} else if (y >= 0 && it != ss.end() &&
s[i][y].first < (-1 * it->first)) {
update(1, 1, 200000, i, -1 * it->first, -1);
v.push_back(it->second);
ss.erase(it);
} else if (y >= 0) {
update(1, 1, 200000, i, s[i][y].first, -1);
v.push_back(s[i][y].second);
y--;
} else {
update(1, 1, 200000, i, -1 * it->first, -1);
v.push_back(it->second);
ss.erase(it);
}
}
for (int j = y; j >= 0; j--) {
ss.insert(make_pair(-1 * s[i][j].first, s[i][j].second));
}
}
} else {
int y = s[i].size() - 1;
for (int j = y; j >= 0; j--) {
ss.insert(make_pair(-1 * s[i][j].first, s[i][j].second));
}
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
long long f[N];
int main() {
int n;
cin >> n;
f[0] = 1;
for (int i = 1; i < N; ++i) {
f[i] = f[i - 1] * i;
}
long long ans = f[n] / f[n / 2] / f[n / 2];
ans = ans * f[n / 2 - 1];
ans = ans * f[n / 2 - 1];
ans /= 2;
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> cost;
int sum;
int binary_serch(int i, int j, int start_cost) {
long long l = i, r = j;
long long mid = (l + r) / 2;
bool check = false;
while (l < r) {
if ((long long)cost[mid].first + (long long)start_cost > sum) {
r = mid;
mid = (l + r) / 2;
} else if ((long long)cost[mid].first + (long long)start_cost < sum) {
l = mid + 1;
mid = (l + r) / 2;
} else if ((long long)cost[mid].first + (long long)start_cost == sum) {
check = true;
break;
}
}
if (cost[mid].first + (long long)start_cost == sum) return mid;
if (check) {
return mid;
}
return -1;
}
int a[10000000];
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j < n; j++) {
cost.push_back(make_pair(i * a[j], i));
}
}
cost.push_back(make_pair(0, 0));
sort(cost.begin(), cost.end());
int q;
cin >> q;
for (int r = 0; r < q; r++) {
int ans = 100000000;
cin >> sum;
bool check = false;
for (int i = 0; i < cost.size() - 1; i++) {
int ind = binary_serch(i + 1, cost.size() - 1, cost[i].first);
if (ind != -1) {
int temp = ind;
while (ind < cost.size() && cost[i].first + cost[ind].first == sum) {
if (k >= cost[ind].second + cost[i].second) {
check = true;
ans = min(ans, cost[ind].second + cost[i].second);
}
ind++;
}
ind = temp;
while (ind >= 0 && cost[i].first + cost[ind].first == sum) {
if (k >= cost[ind].second + cost[i].second) {
check = true;
ans = min(ans, cost[ind].second + cost[i].second);
}
ind--;
}
}
}
if (check) {
cout << ans << endl;
} else
cout << -1 << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, size = 1 << 20, mod = (int)1e9 + 7, inf = 2e9;
const long long INF = 1e15;
template <class o>
void qr(o &x) {
char c = getchar();
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
x *= f;
}
template <class o>
void qw(o x) {
if (x / 10) qw(x / 10);
putchar(x % 10 + '0');
}
template <class o>
void pr1(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o>
void pr2(o x) {
if (x < 0) x = -x, putchar('-');
qw(x);
putchar('\n');
}
long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long power(long long a, long long b = mod - 2, long long p = mod) {
long long c = 1;
while (b) {
if (b & 1) c = c * a % p;
b /= 2;
a = a * a % p;
}
return c;
}
template <class o>
void cmax(o &x, o y) {
if (x < y) x = y;
}
void cmax(int &x, int y) {
x = x - y >> 31 ? y : x;
;
}
template <class o>
void cmin(o &x, o y) {
if (x > y) x = y;
}
void cmin(int &x, int y) { x = x - y >> 31 ? x : y; }
template <class o>
void ad(o &x, o y) {
x += y;
if (x >= mod) x -= mod;
}
template <class o>
void dl(o &x, o y) {
x -= y;
if (x < 0) x += mod;
}
int n, a[N], L[N], R[N];
map<int, int> pos;
long long ans;
struct rec {
long long f, g, s, sg, tf, tg;
int mxf, mng;
rec() {
f = g = sg = s = 0;
tf = tg = -1;
mxf = 0;
mng = inf;
}
rec operator+(rec b) const {
rec c = *this;
ad(c.f, b.f);
ad(c.g, b.g);
ad(c.s, b.s);
ad(c.sg, b.sg);
c.tf = c.tg = -1;
cmax(c.mxf, b.mxf);
cmin(c.mng, b.mng);
return c;
}
} tr[N * 4];
long long f(long long l, long long r) {
return (r + l) * (r - l + 1) / 2 % mod;
}
void updf(int x, int l, int r, long long f) {
tr[x].tf = tr[x].mxf = f;
tr[x].f = f * (r - l + 1) % mod;
tr[x].s = tr[x].g * f % mod;
}
void updg(int x, int l, int r, long long g) {
tr[x].tg = tr[x].mng = g;
tr[x].g = g * (r - l + 1) % mod;
tr[x].sg = g * f(l, r) % mod;
tr[x].s = tr[x].f * g % mod;
}
int pushdown(int x, int l, int r) {
int mid = (l + r) / 2;
long long v;
if (~(v = tr[x].tf))
updf((x << 1), l, mid, v), updf((x << 1 | 1), mid + 1, r, v);
if (~(v = tr[x].tg))
updg((x << 1), l, mid, v), updg((x << 1 | 1), mid + 1, r, v);
tr[x].tf = tr[x].tg = -1;
return mid;
}
rec ask(int x, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[x];
int mid = pushdown(x, l, r);
rec s;
if (L <= mid) s = s + ask((x << 1), l, mid, L, R);
if (mid < R) s = s + ask((x << 1 | 1), mid + 1, r, L, R);
return s;
}
int askf(int x, int l, int r, int d) {
if (l == r) return tr[x].mxf <= d ? l : l + 1;
int mid = pushdown(x, l, r);
if (tr[(x << 1 | 1)].mxf <= d) return askf((x << 1), l, mid, d);
return askf((x << 1 | 1), mid + 1, r, d);
}
void changef(int x, int l, int r, int L, int R, int d) {
if (L <= l && r <= R) return updf(x, l, r, d);
int mid = pushdown(x, l, r);
if (L <= mid) changef((x << 1), l, mid, L, R, d);
if (R > mid) changef((x << 1 | 1), mid + 1, r, L, R, d);
tr[x] = tr[(x << 1)] + tr[(x << 1 | 1)];
}
int askg(int x, int l, int r, int d) {
if (l == r) return tr[x].mng >= d ? l : l + 1;
int mid = pushdown(x, l, r);
if (tr[(x << 1 | 1)].mng >= d) return askg((x << 1), l, mid, d);
return askg((x << 1 | 1), mid + 1, r, d);
}
void changeg(int x, int l, int r, int L, int R, int d) {
if (L <= l && r <= R) return updg(x, l, r, d);
int mid = pushdown(x, l, r);
if (L <= mid) changeg((x << 1), l, mid, L, R, d);
if (R > mid) changeg((x << 1 | 1), mid + 1, r, L, R, d);
tr[x] = tr[(x << 1)] + tr[(x << 1 | 1)];
}
void solve() {
qr(n);
for (int i = 1; i <= n; i++) qr(a[i]);
for (int i = 1; i <= n; i++)
L[i] = pos[a[i]] + 1, R[i] = n, R[pos[a[i]]] = i - 1, pos[a[i]] = i;
pos.clear();
for (int i = 1, p = 1; i <= n; i++) {
cmax(p, L[i]);
changef(1, 1, n, askf(1, 1, n, L[i]), i, L[i]);
changeg(1, 1, n, askg(1, 1, n, R[i]), i, R[i]);
rec now = ask(1, 1, n, p, i);
ans += (now.f - f(p, i)) * i % mod + (now.sg - now.s);
ans %= mod;
}
pr2((ans + mod) % mod);
}
int main() {
solve();
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int n;
string s, m;
int main() {
cin >> n;
cin >> s >> m;
int ar[10] = {0};
int ar2[10] = {0};
for (int i = 0; i < n; i++) {
int y = m[i] - 48;
ar[y]++;
ar2[y]++;
}
int ans1 = 0;
for (int i = 0; i < n; i++) {
int x = s[i] - 48;
int ind = 0;
for (int j = x; j < 10; j++) {
if (ar[j] > 0) {
ar[j]--;
ind = 1;
break;
}
}
if (ind == 0) ans1++;
}
int ans2 = 0;
for (int i = 0; i < n; i++) {
int x = s[i] - 48;
for (int j = x + 1; j < 10; j++) {
if (ar2[j] > 0) {
ans2++;
ar2[j]--;
break;
}
}
}
cout << ans1 << endl;
cout << ans2 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
int n, m, maxi, sum, flag, x;
bool bj[40010];
double a[61], b[61], mid;
long long z[61][61], y[61][61], o = 1;
int count(long long a) {
int s = 0;
while (a != 0) {
s++;
a = a & (a - 1);
}
return s;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
for (int i = 1; i <= m; i++) scanf("%lf", &b[i]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
mid = (a[i] + b[j]) / 2;
x = a[i] + b[j];
if (bj[x + 20001] == 1) continue;
bj[x + 20001] = 1;
for (int k = 1; k <= n; k++)
for (int l = 1; l <= m; l++)
if (fabs((a[k] + b[l]) / 2 - mid) < 0.0000000001)
z[i][j] |= o << (k - 1), y[i][j] |= o << (l - 1);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (z[i][j] == 0 || y[i][j] == 0) continue;
for (int k = 1; k <= n; k++)
for (int l = 1; l <= m; l++) {
if (z[k][l] == 0 || y[k][l] == 0) continue;
sum = count(z[i][j] | z[k][l]) + count(y[i][j] | y[k][l]);
if (sum > maxi) maxi = sum;
}
}
printf("%d", maxi);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
;
const int inf = 1e9 + 5;
const int mod = 1e9 + 7;
const int logN = 17;
int P[N], n, m, x, y, a, b, GG[N];
int FE(int x, int k) {
if (!k) return 1;
int temp = FE(x, k / 2);
if (k & 1) return (long long)temp * temp % mod * x % mod;
return (long long)temp * temp % mod;
}
int T(int x, int y) {
if (x < y) return 0;
if (x == y) return 1;
return y * (long long)FE(x, x - y - 1) % mod;
}
int C(int x, int y) {
if (x < y) return 0;
return P[x] * (long long)GG[x - y] % mod * GG[y] % mod;
}
int main() {
scanf("%d %d %d %d", &n, &m, &a, &b);
if (a == b) {
if (m == 0)
cout << 0 << endl;
else
cout << FE(n, n - 2) << endl;
return 0;
}
int ans = 0;
P[0] = 1;
for (int i = 1; i < N; i++) P[i] = P[i - 1] * (long long)i % mod;
for (int i = 0; i < N; i++) GG[i] = FE(P[i], mod - 2);
for (int i = 2; i <= n; i++) {
int hm = C(m - 1, i - 2);
if (hm) {
ans = (ans + hm * (long long)T(n, i) % mod * C(n - 2, i - 2) % mod *
P[i - 2] % mod * FE(m, n - i) % mod) %
mod;
}
}
cout << ans << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int par[200004], sz[200004], ans[200004];
vector<int> e[200004];
int root(int a) {
while (par[a] != a) a = par[a];
return a;
}
void dfs(int node, int l, int r) {
if (e[node].size() == 0) {
ans[l] = node;
return;
}
ans[l] = node;
l++;
for (int i = 0; i < e[node].size(); ++i) {
dfs(e[node][i], l, l + sz[e[node][i]] - 1);
l += sz[e[node][i]];
}
}
void un(int a, int b) {
if (sz[a] >= sz[b]) {
par[b] = a;
sz[a] += sz[b];
e[a].push_back(b);
} else {
par[a] = b;
sz[b] += sz[a];
e[b].push_back(a);
}
}
int main() {
int n, a, b, x, y;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
par[i] = i;
sz[i] = 1;
}
for (int i = 0; i < n - 1; ++i) {
scanf("%d %d", &x, &y);
a = root(x);
b = root(y);
un(a, b);
if (i == n - 2) a = root(a);
}
dfs(a, 1, n);
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int foo(int v, int p, vector<int>& parent, vector<vector<int> >& farthest,
set<int> const& q, vector<vector<int> > const& g) {
int result = q.count(v) ? 1 : 0;
for (int i = (0); (i) < (int(g[v].size())); ++(i)) {
const int u = g[v][i];
if (u == p) {
parent[v] = i;
} else {
farthest[v][i] = foo(u, v, parent, farthest, q, g);
result = max(result, farthest[v][i] == 0 ? 0 : farthest[v][i] + 1);
}
}
return result;
}
void bar(int v, int p, vector<int> const& parent,
vector<vector<int> >& farthest, set<int> const& q,
vector<vector<int> > const& g) {
vector<pair<int, int> > a;
a.emplace_back(q.count(v) ? 1 : 0, v);
for (int i = (0); (i) < (int(g[v].size())); ++(i)) {
a.emplace_back(farthest[v][i] == 0 ? 0 : farthest[v][i] + 1, i);
}
if (a.size() == 1) return;
sort(a.rbegin(), a.rend());
for (int i = (0); (i) < (int(g[v].size())); ++(i)) {
const int u = g[v][i];
if (u != p) {
farthest[u][parent[u]] = a[0].second != i ? a[0].first : a[1].first;
bar(u, v, parent, farthest, q, g);
}
}
}
int main() {
int n, m, d;
cin >> n >> m >> d;
if (d == 0) {
cout << (m == 1 ? 1 : 0) << endl;
return 0;
}
vector<int> p(m);
for (int i = (0); (i) < (m); ++(i)) {
cin >> p[i];
--p[i];
}
vector<vector<int> > g(n);
for (int i = (0); (i) < (n - 1); ++(i)) {
int a, b;
cin >> a >> b;
--a;
--b;
g[a].push_back(b);
g[b].push_back(a);
}
set<int> q;
for (int i = (0); (i) < (m); ++(i)) q.insert(p[i]);
vector<int> parent(n);
vector<vector<int> > farthest = g;
foo(0, -1, parent, farthest, q, g);
bar(0, -1, parent, farthest, q, g);
int result = 0;
for (int i = (0); (i) < (n); ++(i)) {
if (*max_element(farthest[i].begin(), farthest[i].end()) <= d) {
result += 1;
}
}
cout << result << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
if (n % 2 == 0) {
cout << string(n / 2, '1') << "\n";
} else {
cout << "7" + string(n / 2 - 1, '1') << "\n";
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int test;
cin >> test;
while (test--) {
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
long long int const MOD = 1000000007;
long long int const N = 1000005;
long long int const LN = 20;
long long int const inf = 8e18;
using namespace std;
long long int n, m, a[N];
string str[N];
bool isin(long long int x, long long int y) {
return x > 0 && x <= n && y > 0 && y <= m;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (long long int i = 1; i <= n; i++) {
cin >> str[i];
str[i] = " " + str[i];
}
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= m; j++) {
long long int first = 0;
if (str[i][j] != '.') continue;
if (str[i][j] == '.') {
if (isin(i + 1, j) && str[i + 1][j] == '.') first = 1;
if (isin(i - 1, j) && str[i - 1][j] == '.') first = 1;
if (isin(i, j + 1) && str[i][j + 1] == '.') first = 1;
if (isin(i, j - 1) && str[i][j - 1] == '.') first = 1;
}
if (!first) {
cout << "-1";
return 0;
}
}
}
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j < m; j++) {
if (str[i][j] == '.' && str[i][j + 1] == '.') {
long long int num;
if ((i % 2) == (j % 2))
num = 3 * ((i - 1) % 3) + (j - 1) % 3;
else
num = 3 * ((i - 1) % 3) + (j) % 3;
char ch = '0' + num;
str[i][j] = ch;
str[i][j + 1] = ch;
}
}
}
for (long long int i = 1; i < n; i++) {
for (long long int j = 1; j <= m; j++) {
if (str[i][j] == '.' && str[i + 1][j] == '.') {
long long int num = 0;
if ((j % 2) == (i % 2))
num = 3 * ((i - 1) % 3) + (j - 1) % 3;
else
num = 3 * (i % 3) + (j - 1) % 3;
char ch = '0' + num;
str[i][j] = ch;
str[i + 1][j] = ch;
}
}
}
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= m; j++) {
if (str[i][j] == '.') {
long long int ff = 0;
for (long long int i1 = -1; i1 <= 1; i1++) {
if (ff) break;
for (long long int j1 = -1; j1 <= 1; j1++) {
if (abs(i1) + abs(j1) != 1) continue;
if (ff) break;
if (isin(i + i1, j + j1) && str[i + i1][j + j1] != '#') {
str[i][j] = str[i + i1][j + j1];
ff = 1;
}
}
}
}
}
}
for (long long int i = 1; i <= n; i++) {
for (long long int j = 1; j <= m; j++) cout << str[i][j];
cout << '\n';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int N, M, Q;
long long sa, pb[MAXN];
set<long long> st;
long long off;
long long calc() {
long long res = min(llabs(*st.begin() + off), llabs(*st.rbegin() + off));
auto it = st.lower_bound(-off);
if (it != st.end()) {
res = min(res, (*it) + off);
}
if (it != st.begin()) {
it--;
res = min(res, -((*it) + off));
}
return res;
}
int main() {
scanf("%d %d %d", &N, &M, &Q);
for (int i = 1; i <= N; i++) {
long long a;
scanf("%lld", &a);
if (i % 2 == 0) {
a *= -1;
}
sa += a;
}
for (int i = 1; i <= M; i++) {
scanf("%lld", &pb[i]);
if (i % 2) {
pb[i] *= -1;
}
pb[i] += pb[i - 1];
}
for (int i = 0; i <= M - N; i++) {
long long sb = pb[i + N] - pb[i];
if (i % 2) {
sb *= -1;
}
st.insert(sa + sb);
}
printf("%lld\n", calc());
for (int qi = 1; qi <= Q; qi++) {
int lt, rt, x;
scanf("%d %d %d", <, &rt, &x);
if (lt % 2 == rt % 2) {
if (lt % 2) {
off += x;
} else {
off -= x;
}
}
printf("%lld\n", calc());
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int MOD = 1e9 + 7;
int n, m, k, L[30], sum[N << 1], dp[N << 1];
char str[N];
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
m = strlen(str + 1);
dp[0] = sum[0] = 1;
for (int i = 1; i <= m; i++) {
int &l = L[str[i] - 'a'];
dp[i] = ((sum[i - 1] - sum[l] + dp[l]) % MOD + MOD) % MOD;
l = i;
sum[i] = ((sum[i - 1] + dp[i]) % MOD + MOD) % MOD;
}
for (int i = m + 1; i <= m + n; i++) {
int id, mi = MOD;
for (int j = 0; j < k; j++) {
if (mi > L[j]) {
mi = L[j];
id = j;
}
}
dp[i] = ((sum[i - 1] - sum[mi] + dp[mi]) % MOD + MOD) % MOD;
L[id] = i;
sum[i] = ((sum[i - 1] + dp[i]) % MOD + MOD) % MOD;
}
printf("%d\n", sum[m + n]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> q;
int main() {
long long int n, k;
cin >> n >> k;
long long int n1 = n - k * (k + 1) / 2;
if (n1 < 0) {
cout << "NO";
return 0;
}
for (int i = 0; i < k; i++) {
q.push_back(i + 1 + n1 / k);
}
long long int last = n1 % k;
int i = k - 1;
while (last) {
if (q[i] < 2 * q[i - 1]) {
q[i]++;
last--;
i--;
} else if (q[i] == 2 * q[i - 1] && i == k - 1) {
cout << "NO";
return 0;
} else {
i = k - 1;
}
}
cout << "YES\n";
for (int i = 0; i < q.size(); i++) cout << q[i] << " ";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long a[200002];
int main() {
long long n, i, j, d, res = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int k = 0; k < n;) {
res++;
for (i = k; a[i] == -1; i++)
;
for (j = i + 1; a[j] == -1; j++)
;
if (i >= n || j >= n) break;
d = (a[j] - a[i]) / (j - i);
if ((j - i) * d != a[j] - a[i]) {
k = j;
continue;
}
if (a[j] <= (j - k) * d) {
k = j;
continue;
}
for (; a[k] == -1 && a[i] > d * (i - k) && k < n ||
a[k] != -1 && a[k] == a[i] + d * (k - i) && k < n;
k++)
;
}
cout << res;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(n) for(int i=0; i<int(n); i++)
#define rep(i,a,b) for(int i=a; i<=(int)b; i++)
#define rrep(i,a,b) for(int i=a; i>=(int)b; i--)
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define F first
#define S second
#define endl '\n'
#define FIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
const int nax=3e5+5;
int a[nax], n;
bool isperm(vi& arr) {
int n=arr.size();
bool f[n+1];
memset(f, 0, sizeof f);
all(n) {
if(arr[i]>n) return 0;
f[arr[i]]=1;
}
rep(i, 1, n) {
if(!f[i]) return 0;
}
return 1;
}
bool check(int k) {
deque<int> qu;
vi arr;
for(int i=0; i<k; i++) {
while(!qu.empty() && a[qu.back()]>a[i]) qu.pop_back();
qu.push_back(i);
}
arr.pb(a[qu.front()]);
for(int i=k; i<n; i++) {
if(qu.front() == i-k) qu.pop_front();
while(!qu.empty() && a[qu.back()]>a[i]) qu.pop_back();
qu.push_back(i);
arr.pb(a[qu.front()]);
}
// cout<<k<<": ";
// for(int i: arr) cout<<i<<" ";
// cout<<"\n";
return isperm(arr);
}
void test_case() {
cin>>n;
all(n) cin>>a[i];
int lo=2, hi=n, mid;
while(lo<hi) {
mid = (lo + hi) >> 1;
if(check(mid)) hi=mid;
else lo=mid+1;
}
// cout<<lo<<"\n";
if(!check(lo)) lo=n+1;
string res="";
if(check(1)) res += '1';
else res += '0';
for(int i=2; i<lo; i++) res += '0';
for(int i=lo; i<=n; i++) res += '1';
cout<<res<<"\n";
}
int main(){
FIO;
int t;
cin>>t;
while(t--) test_case();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v, adj[100009];
bool vis[100009], curvis[100009], is_k[100009], f;
void dfs(int nd) {
if (curvis[nd] or f) {
f = 1;
return;
}
curvis[nd] = 1;
for (auto x : adj[nd])
if (!vis[x]) dfs(x);
curvis[nd] = 0;
vis[nd] = 1;
v.push_back(nd);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, a, t;
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cin >> a;
is_k[a] = 1;
}
for (int i = 1; i <= n; i++) {
cin >> t;
while (t--) {
cin >> a;
adj[i].push_back(a);
}
}
for (int i = 1; i <= n; i++)
if (is_k[i] && !vis[i]) dfs(i);
if (f)
cout << -1 << "\n";
else {
cout << v.size() << "\n";
for (auto x : v) cout << x << " ";
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string a;
int ans;
int main() {
cin >> a;
for (int i = 0; i < a.size(); i++) {
if (a[i] >= 'a' && a[i] <= 'z') ans -= (a[i] - 'a' + 1);
if (a[i] >= 'A' && a[i] <= 'Z') ans += (a[i] - 'A' + 1);
}
printf("%d", ans);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct data {
int x, col, v, ot, next;
} a[4010];
bool cmp(data x, data y) {
if (x.x == y.x) {
if (x.v == y.v) return x.ot > y.ot;
return x.v < y.v;
}
return x.x < y.x;
}
int dp[4010][4010];
int f(int u, int v) {
int i, j;
if (u >= v) return 0;
if (dp[u][v] != -1) return dp[u][v];
dp[u][v] = 0;
if (a[u].v == 1 && a[u].next <= v)
dp[u][v] = max(dp[u][v], 1 + f(u + 1, a[u].next - 1) + f(a[u].next + 1, v));
dp[u][v] = max(dp[u][v], f(u + 1, v));
return dp[u][v];
}
void ins(int x, int y, int i) {
a[i * 2].x = x - y;
a[i * 2].v = 1;
a[i * 2].ot = x + y;
a[i * 2 + 1].x = x + y;
a[i * 2 + 1].v = -1;
a[i * 2 + 1].ot = x - y;
a[i * 2].col = a[i * 2 + 1].col = i;
}
int out[4010 / 2], sz;
void getans(int u, int v, int ans) {
if (ans <= 0) return;
int i;
if (dp[u + 1][v] == ans)
getans(u + 1, v, ans);
else {
out[++sz] = a[u].col + 1;
getans(u + 1, a[u].next - 1, dp[u + 1][a[u].next - 1]);
getans(a[u].next + 1, v, dp[a[u].next + 1][v]);
}
}
int main() {
int i, j, n, x, y;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
ins(x, y, i);
}
memset(dp, -1, sizeof(dp));
sort(a, a + 2 * n, cmp);
for (i = 0; i < 2 * n; i++) {
if (a[i].v == -1) continue;
for (j = i + 1; j < 2 * n; j++)
if (a[j].col == a[i].col) {
a[i].next = j;
break;
}
}
int ans = f(0, 2 * n - 1);
printf("%d\n", ans);
getans(0, 2 * n - 1, ans);
for (i = 1; i <= sz; i++) cout << out[i] << " ";
cout << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
bool func(string s) {
if (s == "ABSINTH" or s == "BEER" or s == "BRANDY" or s == "CHAMPAGNE" or
s == "GIN" or s == "RUM" or s == "SAKE" or s == "TEQUILA" or
s == "VODKA" or s == "WHISKEY" or s == "WINE")
return true;
else
return false;
}
int to_int(string s) {
int n = 0;
for (int i = 0; i < s.size(); i++) n = n * 10 + s[i] - 0x30;
return n;
}
int main() {
long long cnt = 0;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
if (func(s) == true) cnt++;
if (s[0] >= '0' and s[0] <= '9') {
if (to_int(s) <= 17) cnt++;
}
}
cout << cnt;
}
| 1 |
#include <bits/stdc++.h>
int n, k;
int main() {
while (~scanf("%d%d", &n, &k)) {
if (3 * k > n || n < 3 || k == 1) {
puts("-1");
continue;
}
int i, cnt = 1;
for (i = 0; i < n && cnt <= k; i += 2, ++cnt) printf("%d %d ", cnt, cnt);
printf("%d", cnt = 1);
for (++i, ++cnt; i < n && cnt <= k; ++i, ++cnt) printf(" %d", cnt);
--cnt;
for (; i < n; ++i) printf(" %d", cnt);
puts("");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void fastInOut();
const int N = 200001;
int n, k, l, r;
vector<pair<int, int> > in[N];
vector<int> ot[N], ret;
set<pair<int, int> > cur;
int main() {
fastInOut();
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> l >> r;
in[l].push_back({r, i});
ot[r].push_back(i);
}
for (int i = 1; i < N; ++i) {
for (auto it : in[i]) cur.insert(it);
while (int(cur.size()) > k) {
pair<int, int> lst = *cur.rbegin();
ret.push_back(lst.second);
cur.erase(lst);
}
for (auto it : ot[i]) cur.erase({i, it});
}
sort(ret.begin(), ret.end());
cout << int(ret.size()) << "\n";
for (int i = 0; i < int(ret.size()); ++i)
cout << ret[i] + 1 << " \n"[i == int(ret.size()) - 1];
return 0;
}
void fastInOut() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
vector<int> zfunction(vector<int> s) {
int n = (int)s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while ((i + z[i] < n) && s[z[i]] == s[i + z[i]]) z[i]++;
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
int main() {
ios_base::sync_with_stdio(false);
int a, b;
long long cnt = 0;
cin >> a >> b;
vector<pair<long long, char> > one, two;
char last = '\0';
for (int i = 0; i < a; i++) {
string s;
cin >> s;
long long num = 0;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '-') break;
num = num * 10 + s[j] - '0';
}
char ch = s[s.size() - 1];
if (ch == last)
cnt += num;
else {
if (last) one.push_back(make_pair(cnt, last));
last = ch;
cnt = num;
}
}
one.push_back(make_pair(cnt, last));
last = '\0', cnt = 0;
for (int i = 0; i < b; i++) {
string s;
cin >> s;
int num = 0;
for (int j = 0; j < s.size(); j++) {
if (s[j] == '-') break;
num = num * 10 + s[j] - '0';
}
char ch = s[s.size() - 1];
if (ch == last)
cnt += num;
else {
if (last) two.push_back(make_pair(cnt, last));
last = ch;
cnt = num;
}
}
two.push_back(make_pair(cnt, last));
set<pair<long long, char> > Set;
for (int i = 0; i < one.size(); i++) Set.insert(one[i]);
for (int i = 0; i < two.size(); i++) Set.insert(two[i]);
vector<pair<long long, char> > pos;
for (set<pair<long long, char> >::iterator i = Set.begin(); i != Set.end();
i++)
pos.push_back(*i);
vector<int> arr;
cnt = 0;
for (int i = 1; i < two.size() - 1; i++) {
int index =
(int)(lower_bound(pos.begin(), pos.end(), two[i]) - pos.begin());
arr.push_back(index);
cnt++;
}
arr.push_back(-1);
for (int i = 0; i < one.size(); i++) {
int index =
(int)(lower_bound(pos.begin(), pos.end(), one[i]) - pos.begin());
arr.push_back(index);
}
vector<int> z = zfunction(arr);
long long ans = 0;
if (two.size() == 2) {
for (int i = 0; i < one.size(); i++) {
bool flag = true;
for (int j = 0; j < two.size(); j++) {
if (i + j < one.size() && one[i + j].first >= two[j].first &&
one[i + j].second == two[j].second) {
} else {
flag = false;
}
}
if (flag) ans++;
}
} else if (two.size() == 1) {
for (int i = 0; i < one.size(); i++) {
if (one[i].first >= two[0].first && one[i].second == two[0].second)
ans += one[i].first - two[0].first + 1;
}
} else {
int num = two.size() - 2, l = two.size() - 1;
for (int i = cnt + 1; i < z.size(); i++) {
if (z[i] == num && arr[i - 1] >= 0 &&
pos[arr[i - 1]].first >= two[0].first &&
pos[arr[i - 1]].second == two[0].second && i + z[i] < z.size() &&
pos[arr[i + z[i]]].first >= two[l].first &&
pos[arr[i + z[i]]].second == two[l].second) {
ans++;
}
}
}
cout << ans << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
int IntMaxVal = (int)1e20;
int IntMinVal = (int)-1e20;
long long LongMaxVal = (long long)1e20;
long long LongMinVal = (long long)-1e20;
using namespace std;
template <typename T>
struct argument_type;
template <typename T, typename U>
struct argument_type<T(U)> {};
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& s) {
is >> s.first >> s.second;
return is;
}
int main() {
srand(time(NULL));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string num;
cin >> num;
;
vector<int> digits1(10);
for (auto c : num) digits1[c - '0']++;
vector<int> digits2(digits1);
auto best = make_pair(-1, -1);
for (int dig = 1; dig < 6; ++dig) {
if (digits1[dig] == 0) continue;
vector<int> oldDig1(digits1);
vector<int> oldDig2(digits2);
digits1[dig]--;
int dig2 = 10 - dig;
if (digits2[dig2]) {
digits2[dig2]--;
int res = 1;
for (int d1 = 0; d1 < 10; ++d1)
while (digits1[d1] && digits2[9 - d1]) {
digits1[d1]--;
digits2[9 - d1]--;
res++;
}
while (digits1[0] && digits2[0]) {
digits1[0]--;
digits2[0]--;
res++;
}
{ best = max(best, make_pair(res, dig)); };
}
digits1 = oldDig1;
digits2 = oldDig2;
}
deque<int> num1;
deque<int> num2;
if (best.first != -1) {
{
int d1 = best.second;
int d2 = 10 - d1;
digits1[d1]--;
digits2[d2]--;
num1.push_front(d1);
num2.push_front(d2);
}
for (int d1 = 0; d1 < 10; ++d1)
while (digits1[d1] && digits2[9 - d1]) {
digits1[d1]--;
digits2[9 - d1]--;
num1.push_front(d1);
num2.push_front(9 - d1);
}
}
while (digits1[0] && digits2[0]) {
num1.push_back(0);
num2.push_back(0);
digits1[0]--;
digits2[0]--;
}
for (int d1 = 0; d1 < 10; ++d1)
for (int d2 = 0; d2 < 10; ++d2)
while (digits1[d1] && digits2[d2]) {
num1.push_front(d1);
num2.push_front(d2);
digits1[d1]--;
digits2[d2]--;
}
for (auto d : num1) cout << d;
cout << endl;
for (auto d : num2) cout << d;
cout << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b, x, y, z;
cin >> a >> b;
cin >> x >> y >> z;
a = a - 2 * x - y;
b = b - 3 * z - y;
long long ans = 0;
if (a < 0) ans -= a;
if (b < 0) ans -= b;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
vector<int> g[N];
int vis[N], act[N], dp[N][26];
int ans;
void dfs(int u, string &s) {
vis[u] = act[u] = 1;
for (int v : g[u]) {
if (act[v]) {
puts("-1");
exit(0);
}
if (!vis[v]) {
dfs(v, s);
}
for (int i = 0; i < 26; i++) dp[u][i] = max(dp[u][i], dp[v][i]);
}
dp[u][s[u] - 'a']++;
for (int i = 0; i < 26; i++) ans = max(ans, dp[u][i]);
act[u] = 0;
}
int beauty(int n, int m, string s, int x[], int y[]) {
s = "#" + s;
for (int i = 0; i < m; i++) {
g[x[i]].push_back(y[i]);
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i, s);
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
string str;
cin >> str;
int a[m], b[m];
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
}
cout << beauty(n, m, str, a, b) << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ans;
void gogo(int n, int pre) {
for (int offset = 1; offset < n; offset *= 2)
for (int i = 1; i + offset <= n; i += offset << 1)
for (int j = 0; j < offset; j++)
cout << pre + i + j << ' ' << pre + i + j + offset << '\n';
}
void test_case() {
int n, k;
cin >> n;
k = n - 1;
k |= k >> 1;
k |= k >> 2;
k |= k >> 4;
k |= k >> 8;
k |= k >> 16;
++k;
k >>= 1;
int lg = k == 0 ? 0 : log2(k);
cout << (int)k * lg << '\n';
gogo(k, 0);
gogo(k, n - k);
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
int Case = 1;
for (int i = 0; i < Case; i++) {
test_case();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int a[105];
int d[105];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
int nd = a[0];
for (int i = 1; i < n; i++) nd = gcd(a[i], nd);
int res = a[n - 1] / nd - n;
if (res % 2)
printf("Alice\n");
else
printf("Bob\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod7 = 1000000007;
long long A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W,
X, Y, Z;
long long temp;
deque<char> fullVal;
deque<char> afterComma;
long long eVal = 0;
void killZeros() {
if (fullVal.size() != 0)
while (fullVal.front() == '0') {
fullVal.pop_front();
if (fullVal.size() == 0) break;
}
if (afterComma.size() != 0)
while (afterComma.back() == '0') {
afterComma.pop_back();
if (afterComma.size() == 0) break;
}
}
void moveOver(string dir) {
if (dir == "right") {
afterComma.push_front(fullVal.back());
fullVal.pop_back();
eVal++;
killZeros();
} else {
fullVal.push_back(afterComma.front());
afterComma.pop_front();
eVal--;
killZeros();
}
}
int main() {
stringstream input;
string restInput;
string data;
char tempChar;
cin >> data;
input << data;
while (input.peek() != '.' && !input.eof()) {
if (true) {
} else
cout << "Checkpoint#1" << endl;
input >> tempChar;
fullVal.push_back(tempChar);
}
if (input.peek() == '.') {
input >> tempChar;
input >> restInput;
if (true) {
} else
cout << "Checkpoint#2" << endl;
for (unsigned int i = 0; i < restInput.length(); i++) {
tempChar = restInput[i];
afterComma.push_back(tempChar);
}
}
killZeros();
if (true) {
} else
cout << "Checkpoint#3" << endl;
while ((fullVal.size() != 1)) {
if (fullVal.size() > 1) {
moveOver("right");
} else {
moveOver("left");
}
}
if (true) {
} else
cout << "Checkpoint#4" << endl;
killZeros();
if (true) {
} else
cout << "Checkpoint#5" << endl;
for (unsigned int i = 0; i < fullVal.size(); i++) {
cout << fullVal[i];
}
if (afterComma.size()) cout << ".";
for (unsigned int i = 0; i < afterComma.size(); i++) {
cout << afterComma[i];
}
if (eVal != 0) cout << "E" << eVal;
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 0;
pair<double, double> p;
double m, b;
double x3, y3, rad;
double R, x1, dkj, x2, y2, d;
double dist(double x1, double dkj, double x2, double y2) {
return sqrt((x1 - x2) * (x1 - x2) + (dkj - y2) * (dkj - y2));
}
pair<double, double> first(double l, double r, bool right) {
for (int i = 0; i < (int)100; i++) {
double x = (l + r) / 2;
double y = x * m + b;
double dd = dist(x, y, x2, y2);
if (dd < rad) {
if (right)
l = x;
else
r = x;
} else {
if (right)
r = x;
else
l = x;
}
}
double x = (l + r) / 2;
double y = x * m + b;
return {x, y};
}
int main() {
cin >> R >> x1 >> dkj >> x2 >> y2;
d = dist(x1, dkj, x2, y2);
if (d > R) {
printf("%.6lf %.6lf %.6lf\n", x1, dkj, R);
return 0;
}
rad = (R + d) / 2;
if (x1 == x2) {
x3 = x1;
if (dkj > y2)
y3 = y2 + rad;
else
y3 = y2 - rad;
} else {
m = (y2 - dkj) / (x2 - x1);
b = dkj - x1 * m;
if (x1 > x2)
p = first(x2, x1 + rad, 1);
else
p = first(x1 - rad, x2, 0);
x3 = p.first;
y3 = p.second;
}
printf("%.6lf %.6lf %.6lf\n", x3, y3, rad);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
set<int> st;
cin >> n;
for (int i = 1; i <= n; i++) {
st.insert(i);
}
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
auto it = st.find(a[i]);
if (it != st.end()) {
st.erase(it);
}
}
int even = 0;
int odd = 0;
for (auto it = st.begin(); it != st.end(); it++) {
if ((*it) % 2 == 0) {
even++;
} else {
odd++;
}
}
int dp[n][even + 1][odd + 1][2];
int zerostillnow[n];
int ctr = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
ctr++;
}
zerostillnow[i] = ctr;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= even; j++) {
for (int k = 0; k <= odd; k++) {
dp[i][j][k][0] = -1;
dp[i][j][k][1] = -1;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= even; j++) {
for (int k = 0; k <= odd; k++) {
if (zerostillnow[i] != (even + odd) - (j + k)) {
continue;
} else {
if (a[i] == 0) {
if (i != 0 && j != even) {
if (dp[i - 1][j + 1][k][0] != -1) {
dp[i][j][k][0] = dp[i - 1][j + 1][k][0];
if (dp[i - 1][j + 1][k][1] != -1) {
if (dp[i - 1][j + 1][k][1] + 1 < dp[i][j][k][0]) {
dp[i][j][k][0] = dp[i - 1][j + 1][k][1] + 1;
}
}
} else if (dp[i - 1][j + 1][k][1] != -1) {
dp[i][j][k][0] = dp[i - 1][j + 1][k][1] + 1;
}
}
if (i != 0 && k != odd) {
if (dp[i - 1][j][k + 1][1] != -1) {
dp[i][j][k][1] = dp[i - 1][j][k + 1][1];
if (dp[i - 1][j][k + 1][0] != -1) {
if (dp[i - 1][j][k + 1][0] + 1 < dp[i][j][k][1]) {
dp[i][j][k][1] = dp[i - 1][j][k + 1][0] + 1;
}
}
} else if (dp[i - 1][j][k + 1][0] != -1) {
dp[i][j][k][1] = dp[i - 1][j][k + 1][0] + 1;
}
}
if (i == 0) {
if (even == j) {
dp[i][j][k][1] = 0;
}
if (odd == k) {
dp[i][j][k][0] = 0;
}
}
} else {
if (i == 0) {
if (a[i] % 2 == 0) {
dp[i][j][k][0] = 0;
} else {
dp[i][j][k][1] = 0;
}
} else {
if (a[i] % 2 == 0) {
if (dp[i - 1][j][k][0] != -1) {
dp[i][j][k][0] = dp[i - 1][j][k][0];
if (dp[i - 1][j][k][1] != -1) {
if (dp[i - 1][j][k][1] + 1 < dp[i][j][k][0]) {
dp[i][j][k][0] = dp[i - 1][j][k][1] + 1;
}
}
} else if (dp[i - 1][j][k][1] != -1) {
dp[i][j][k][0] = dp[i - 1][j][k][1] + 1;
}
} else {
if (dp[i - 1][j][k][0] != -1) {
dp[i][j][k][1] = dp[i - 1][j][k][0] + 1;
if (dp[i - 1][j][k][1] != -1) {
if (dp[i - 1][j][k][1] < dp[i][j][k][1]) {
dp[i][j][k][1] = dp[i - 1][j][k][1];
}
}
} else if (dp[i - 1][j][k][1] != -1) {
dp[i][j][k][1] = dp[i - 1][j][k][1];
}
}
}
}
}
}
}
}
if (dp[n - 1][0][0][0] != -1) {
if (dp[n - 1][0][0][1] != -1) {
if (dp[n - 1][0][0][0] < dp[n - 1][0][0][1]) {
cout << dp[n - 1][0][0][0];
} else {
cout << dp[n - 1][0][0][1];
}
} else {
cout << dp[n - 1][0][0][0];
}
} else {
cout << dp[n - 1][0][0][1];
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int i, j, n, g[25][25], k, find;
bool mark[1001];
int a[25];
a[0] = a[1] = 0;
k = 1;
memset(mark, 0, sizeof(mark));
mark[0] = 1;
for (i = 2; i <= 20; ++i) {
find = 0;
while (!find) {
for (j = 1; j < i; ++j) {
if (mark[a[j] + k]) break;
if (j == i - 1) find = 1;
}
if (find) {
a[i] = k;
for (j = 1; j < i; ++j) {
mark[a[j] + a[i]] = 1;
}
} else {
do {
k++;
} while (mark[k]);
}
}
}
scanf("%d", &n);
memset(g, 0, sizeof(g));
for (i = 1; i <= n; ++i) {
g[i][i] = 0;
for (j = i + 1; j <= n; ++j) {
g[j][i] = g[i][j] = a[i] + a[j];
}
}
for (i = 1; i <= n; ++i) {
for (j = 1; j < n; ++j) {
printf("%d ", g[i][j]);
}
printf("%d\n", g[i][n]);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > a, b, ans;
char s[200010], t[200010];
int tag;
void ins(vector<pair<int, int> >& a, pair<int, int> x) {
if (!a.size() || a.back().first != x.first)
a.push_back(x);
else
a.back().second += x.second;
}
void work(int k) {
int len = 0;
pair<int, int> t = a.back();
a.pop_back();
for (int i = k - 1; i >= 0; i--) {
ins(a, b[b.size() - 1 - i]);
len += b[b.size() - 1 - i].second;
}
while (k) b.pop_back(), k--;
ans.push_back(make_pair(t.second, len));
ins(b, t);
}
int main() {
scanf("%s%s", s + 1, t + 1);
int n = strlen(s + 1), m = strlen(t + 1);
for (int i = n; i; i--) ins(a, make_pair(s[i], 1));
for (int i = m; i; i--) ins(b, make_pair(t[i], 1));
if (a.size() > b.size()) {
swap(a, b);
tag = 1;
}
if (a.back().first == b.back().first) {
if (((b.size() - a.size()) & 3) == 3)
work((b.size() - a.size() + 1) / 4 * 2);
int tmp = (b.size() - a.size()) / 4 * 2 + 1;
a.push_back(make_pair(b[b.size() - tmp - 1].first, 0));
work(tmp);
} else if (b.size() > a.size() + 2)
work((b.size() - a.size() + 1) / 4 * 2 + 1);
while (a.size() != 1 || b.size() != 1) work(1);
printf("%d\n", (int)ans.size());
for (pair<int, int> i : ans) {
if (tag) swap(i.first, i.second);
printf("%d %d\n", i.first, i.second);
}
return 0;
}
| 10 |