solution
stringlengths 53
181k
| difficulty
int64 0
27
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long int q, m, a, b, c, d, i, j;
cin >> q;
while (q--) {
cin >> m >> a >> b >> c >> d;
long long int mini = c - d;
long long int maxi = c + d;
long long int l = a - b;
long long int r = a + b;
long long int flag = 1;
if (l * m > maxi || r * m < mini) flag = 0;
if (flag == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int dis[200010], lle[200010];
vector<int> adj[200010];
void dfs(int x, int f, int ar[]) {
for (int i = 0; i < adj[x].size(); i++) {
int v = adj[x][i];
if (v != f) {
ar[v] = ar[x] + 1;
dfs(v, x, ar);
}
}
}
int main() {
int n, x, a, b, m = 0;
cin >> n >> x;
for (int i = 1; i < n; i++) {
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, 0, dis);
dfs(x, 0, lle);
for (int i = 1; i <= n; i++)
if (lle[i] < dis[i]) m = max(m, dis[i]);
cout << m * 2;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 1010, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8;
using namespace std;
int dx2[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy2[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dx[] = {1, -1, 0, 0, 0, 0};
int dy[] = {0, 0, 1, -1, 0, 0};
int dz[] = {0, 0, 0, 0, 1, -1};
int a[N];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int read() {
int date = 0, w = 1;
char c = 0;
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
date = date * 10 + c - '0';
c = getchar();
}
return date * w;
}
long long qmi(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
int m, n;
cin >> n >> m;
while (n) printf("%d ", n / m), n -= n / m--;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[10005000], jud[10000050];
int main() {
long long n, t, flag, x, head = 1, y, num, head1, pre = -1, wei, dir;
cin >> n >> t;
for (long long i = 2000001; i <= 2000000 + n; i++) a[i] = 1;
wei = n + 2000000;
dir = 1;
head = 2000001;
while (t--) {
scanf("%lld%lld", &flag, &x);
if (flag == 1) {
if (dir == 1) {
long long len = wei - head + 1;
if (x <= len / 2) {
head1 = head + x + x - 1;
for (long long j = 0; j < x; j++) {
a[head1--] += a[head + j];
}
head += x;
} else {
long long temp = head;
head = wei, wei = temp;
x = head - wei + 1 - x;
head1 = head - x - x + 1;
for (long long j = 0; j < x; j++) {
a[head1++] += a[head - j];
}
head -= x;
dir = -1;
}
} else {
long long len = head - wei + 1;
if (x <= len / 2) {
head1 = head - x - x + 1;
for (long long j = 0; j < x; j++) {
a[head1++] += a[head - j];
}
head -= x;
} else {
long long temp = wei;
wei = head, head = temp;
x = len - x;
head1 = head + x + x - 1;
for (long long j = 0; j < x; j++) {
a[head1--] += a[head + j];
}
head += x;
dir = 1;
}
}
} else {
scanf("%lld", &y);
num = 1;
jud[0] = a[head];
if (pre != 2) {
for (long long k = 1;; k++) {
if (dir == 1) {
if (head + k > wei) break;
jud[k] = jud[k - 1] + a[head + k];
} else {
jud[k] = jud[k - 1] + a[head - k];
if (head - k < wei) break;
}
}
}
if (x == 0)
cout << jud[y - 1] << endl;
else
cout << jud[y - 1] - jud[x - 1] << endl;
}
pre = flag;
if (head < 5e5 || head > 5e6) {
queue<long long> q;
if (dir == 1) {
for (long long cnt = head; cnt <= wei; cnt++) {
q.push(a[cnt]);
}
} else {
for (long long cnt = head; cnt >= wei; cnt--) {
q.push(a[cnt]);
}
}
head = 1e6;
wei = head;
for (; !q.empty(); wei++) {
a[wei] = q.front();
q.pop();
}
}
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
bool chmin(T1 &a, T2 b) {
return b < a && (a = b, true);
}
template <class T1, class T2>
bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
double vol(double l, int n) {
const double pi = acos(-1);
double theta = pi / n;
double d = l / 2 / sin(theta);
double h = sqrt(l * l - d * d);
double S = d * d * sin(theta * 2) / 2 * n;
return S * h / 3;
}
int main() {
double l3, l4, l5;
cin >> l3 >> l4 >> l5;
double ans = vol(l3, 3) + vol(l4, 4) + vol(l5, 5);
printf("%.20f\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 300010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);
void redirect(){
#ifdef LOCAL
freopen("test.txt","r",stdin);
#endif
}
int a[maxn];
int cnt[maxn];
int main(){
redirect();
int T,n;
scanf("%d",&T);
while(T--){
vector<int>v;
memset(cnt,0,sizeof(cnt));
scanf("%d",&n);
for(int i = 0;i < n;i++){
scanf("%d",&a[i]);
cnt[a[i]]++;
}
bool flag = true;
int l = 0,r = n-1;
for(int i = 1;i < n;i++){
if(cnt[i] != 0){
if(!flag){
v.push_back(0);
continue;
}
if(cnt[i] != 1)flag = false;
if(i == 1){
v.push_back(1);
continue;
}
int cc = 0;
if(a[l]==i-1)cc++,l++;
if(a[r]==i-1)cc++,r--;
if(cc)v.push_back(1);
else v.push_back(0),flag = false;
}
else v.push_back(0);
}
bool f = true;
for(int i = 1;i <= n;i++)
if(!cnt[i])f = false;
if(f)v.push_back(1);
else v.push_back(0);
reverse(v.begin(),v.end());
for(auto&x:v)putchar('0'+x);
puts("");
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-10;
const double PI = acos(-1);
template <class T, class S>
inline void add(T &a, S b) {
a += b;
if (a >= mod) a -= mod;
}
template <class T, class S>
inline void sub(T &a, S b) {
a -= b;
if (a < 0) a += mod;
}
template <class T, class S>
inline bool chkmax(T &a, S b) {
return a < b ? a = b, true : false;
}
template <class T, class S>
inline bool chkmin(T &a, S b) {
return a > b ? a = b, true : false;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = 1LL * ans * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return ans;
}
int n, l[N], r[N];
int p[N], q[N];
int pre[N];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &l[i]);
}
for (int i = 1; i <= n; i++) {
scanf("%d", &r[i]);
r[i]++;
}
q[1] = 0;
p[1] = 1;
for (int i = 1; i <= n; i++) {
int L = max(l[i], l[i - 1]);
int R = min(r[i], r[i - 1]);
if (R > L) {
q[i] = 1LL * (R - L) * power(r[i] - l[i], mod - 2) % mod *
power(r[i - 1] - l[i - 1], mod - 2) % mod;
}
p[i] = (1 - q[i] + mod) % mod;
}
for (int i = 1; i <= n; i++) {
pre[i] = (pre[i - 1] + p[i]) % mod;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
add(ans, p[i]);
}
for (int i = 1; i <= n; i++) {
if (i - 2 >= 1) {
add(ans, 2LL * p[i] * pre[i - 2] % mod);
}
}
r[0] = -inf;
l[0] = inf;
for (int i = 1; i < n; i++) {
int ret = ((1 - q[i] - q[i + 1]) % mod + mod) % mod;
int R = min(r[i - 1], min(r[i], r[i + 1]));
int L = max(l[i - 1], max(l[i], l[i + 1]));
int tmp = 0;
if (R > L) {
tmp = R - L;
tmp = 1LL * tmp * power(r[i - 1] - l[i - 1], mod - 2) % mod;
tmp = 1LL * tmp * power(r[i] - l[i], mod - 2) % mod;
tmp = 1LL * tmp * power(r[i + 1] - l[i + 1], mod - 2) % mod;
}
add(ret, tmp);
add(ans, 2LL * ret % mod);
}
printf("%d\n", ans);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, j, k, l;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
int a[n];
int b[n];
int c[n];
for (j = 0; j < n; j++) cin >> a[j];
for (k = 0; k < n; k++) cin >> b[k];
for (l = 0; l < n; l++) cin >> c[l];
for (j = 0; j < n - 1; j++) {
if (a[j] == a[j + 1]) a[j + 1] = b[j + 1];
}
if (a[0] == a[n - 1]) {
if (b[0] == a[1])
a[0] = c[0];
else
a[0] = b[0];
}
for (j = 0; j < n; j++) cout << a[j] << " ";
cout << "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long arr[30][30];
long long cntr, k;
int n, m;
vector<map<long long, int>> v;
void f(int x, int y, long long val) {
val ^= arr[x][y];
if (x + y == n - 1) {
v[x][val]++;
return;
}
if (x < n) f(x + 1, y, val);
if (y < m) f(x, y + 1, val);
}
void f1(int x, int y, long long val) {
if (x + y == n - 1) {
cntr += v[x][val ^ k];
return;
}
val ^= arr[x][y];
if (x > 0) f1(x - 1, y, val);
if (y > 0) f1(x, y - 1, val);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
v.resize(n + 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> arr[i][j];
}
}
f(0, 0, 0);
f1(n - 1, m - 1, 0);
cout << cntr << endl;
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
long long ni[] = {1, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292, 341};
int main() {
long long n;
cin >> n;
if (n <= 12) {
cout << ni[n] << endl;
} else {
cout << ni[12] + (n - 12) * 49 << endl;
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
inline long long conv(long long i) {
while (i < 0) i += n;
while (i >= n) i -= n;
return i;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> n >> k >> s;
long long idx = -1;
for (long long i = 0; i < n; i++) {
if (s[i] == s[conv(i + 1)] && s[conv(i - 1)] != s[i]) {
idx = i;
break;
}
}
if (idx == -1) {
if (s[0] == s[1] || k % 2 == 0)
cout << s;
else {
for (auto &i : s) i ^= ('B' ^ 'W');
cout << s;
}
return 0;
}
long long len = 0;
char prev = s[idx];
vector<long long> lens;
for (long long i = 0; i < n; i++) {
if (prev == s[conv(idx + i)]) {
len++;
} else {
lens.push_back(len);
len = 1;
}
prev = s[conv(idx + i)];
}
lens.push_back(len);
vector<long long> lens2;
long long nxt = 0;
long long tot = 0;
for (auto &i : lens) {
if (i == 1) {
tot++;
} else {
if (tot == 0) {
lens2.push_back(i);
continue;
}
long long mx = min(2 * k, tot);
nxt = mx / 2;
lens2.back() += nxt;
tot -= mx;
if (mx % 2) {
lens2.back() += nxt + 1;
lens2.back() += i;
tot = 0;
} else {
while (tot--) lens2.push_back(1);
lens2.push_back(i + nxt);
}
tot = 0;
nxt = 0;
}
}
char curr = s[idx];
if (tot) {
long long mx = min(2 * k, tot);
nxt = mx / 2;
lens2.back() += nxt;
tot -= mx;
if (mx % 2) {
lens2.back()++;
tot = 0;
}
while (tot--) lens2.push_back(1);
lens2[0] += nxt;
idx = conv(idx - nxt);
nxt = 0;
}
for (auto &i : lens2) {
while (i--) {
s[idx] = curr;
idx = conv(idx + 1);
}
curr ^= ('B' ^ 'W');
}
cout << s;
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, q;
cin >> n >> q;
for (int i = 1; i <= q; i++) {
long long x;
cin >> x;
if (x % 2LL == 1LL) {
cout << (x / 2LL + 1) << endl;
} else {
long long a, b;
a = n;
b = x;
while (!(b % 2LL)) {
a = b = a - b / 2LL;
}
long long ans = b / 2LL + 1LL;
ans = ans + (n - a);
cout << ans << endl;
}
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
int t, n, m, ans;
int main() {
int i;
for (scanf("%d", &t); t > 0; t--) {
scanf("%d%d", &n, &m);
for (i = 1; i <= m && i * i <= n; i++)
if (!(n % i))
if (n / i > m)
ans = n / i;
else {
ans = i;
break;
}
printf("%d\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, a, b = 0, c = 0;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (a % 2 == 0) {
b++;
} else
c++;
}
int k = n - m;
int l = min(b, c);
if (k == 0) {
if (c % 2 == 0) {
printf("Daenerys\n");
return 0;
} else {
printf("Stannis\n");
return 0;
}
}
if (k % 2 == 0) {
if (k / 2 < l) {
printf("Daenerys\n");
return 0;
} else if (l == b) {
if (m % 2 == 0) {
printf("Daenerys\n");
return 0;
} else {
printf("Stannis\n");
return 0;
}
} else {
printf("Daenerys\n");
return 0;
}
} else if ((k - 1) / 2 < l) {
printf("Stannis\n");
return 0;
} else if (l == b) {
if (m % 2 == 0) {
printf("Daenerys\n");
return 0;
} else {
printf("Stannis\n");
return 0;
}
} else {
printf("Daenerys\n");
return 0;
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int a[200000], b[200000], f[200000];
int main() {
int u, g = -1, j = 0, n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n - 1; i++)
if (a[i] != a[i + 1]) {
j++;
b[j] = a[i];
}
j++;
b[j] = a[n];
for (int i = 2; i <= j - 1; i++)
if (b[i - 1] != b[i + 1])
f[b[i]]++;
else
f[b[i]] += 2;
if (b[1] != b[2]) f[b[1]]++;
if (b[j - 1] != b[j]) f[b[j]]++;
for (int i = 1; i <= k; i++)
if (f[i] > g) {
g = f[i];
u = i;
}
cout << u;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
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(long long x) {
static const long long maxlen = 100;
static char s[maxlen];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
long long len = 0;
for (; x; x /= 10) s[len++] = x % 10 + '0';
for (long long i = len - 1; i >= 0; --i) putchar(s[i]);
}
long long n;
long long a[4001000], cnt;
char s[4001000];
long long ans = 1;
priority_queue<long long, vector<long long>, less<long long> > L;
priority_queue<long long, vector<long long>, greater<long long> > R;
const long long P = 1e9 + 7;
void ADD(long long x) {
for (long long i = 1; i <= cnt; i++) {
if (a[i] < x) {
L.push(a[i]);
} else if (a[i] > x) {
R.push(a[i]);
}
}
cnt = 0;
}
void doit(long long x) {
if (L.size()) {
if (x < L.top()) {
ans = 0;
return;
}
if (x == L.top()) {
L.pop();
ADD(x);
return;
}
}
if (R.size()) {
if (x > R.top()) {
ans = 0;
return;
}
if (x == R.top()) {
R.pop();
ADD(x);
return;
}
}
ans = (ans * 2ll) % P;
ADD(x);
}
int main() {
scanf("%I64d", &n);
ans = 1;
for (long long i = 1; i <= n; i++) {
scanf("%s", s);
long long x;
scanf("%I64d", &x);
if (s[1] == 'D')
a[++cnt] = x;
else
doit(x);
}
long long tmp = 0;
long long L_MAX = -1e9;
long long R_MIN = 1e9;
if (L.size()) L_MAX = max(L_MAX, (long long)L.top());
if (R.size()) R_MIN = min(R_MIN, (long long)R.top());
for (int i = 1; i <= cnt; i++)
if ((a[i] > L_MAX) && (a[i] < R_MIN)) tmp++;
ans = 1ll * ans * (tmp + 1ll) % P;
printf("%I64d\n", ans);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k < 2 * n) {
cout << "NO" << endl;
return 0;
} else {
cout << "YES" << endl;
for (int i = 0; i < n - 1; i++) cout << 1 << " ";
cout << k - n + 1 << endl;
cout << n << endl;
}
}
| 6 |
#include <bits/stdc++.h>
long long n, m;
long long val[100005];
long long mx[100005 << 2], sum[100005 << 2];
long long getint() {
long long x = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
void build(long long cur, long long l, long long r) {
if (l == r) {
sum[cur] = mx[cur] = val[l];
return;
}
long long mid = l + r >> 1;
build(cur << 1, l, mid);
build(cur << 1 | 1, mid + 1, r);
mx[cur] = ((mx[cur << 1]) > (mx[cur << 1 | 1]) ? (mx[cur << 1])
: (mx[cur << 1 | 1]));
sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];
}
long long query(long long cur, long long l, long long r, long long ql,
long long qr) {
if (ql <= l and r <= qr) return sum[cur];
long long mid = l + r >> 1, ans = 0;
if (ql <= mid) ans += query(cur << 1, l, mid, ql, qr);
if (mid < qr) ans += query(cur << 1 | 1, mid + 1, r, ql, qr);
return ans;
}
void modify(long long cur, long long l, long long r, long long ql, long long qr,
long long mod) {
if (mx[cur] < mod) return;
if (l == r) {
sum[cur] %= mod;
mx[cur] %= mod;
return;
}
long long mid = l + r >> 1;
if (ql <= mid) modify(cur << 1, l, mid, ql, qr, mod);
if (mid < qr) modify(cur << 1 | 1, mid + 1, r, ql, qr, mod);
sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];
mx[cur] = ((mx[cur << 1]) > (mx[cur << 1 | 1]) ? (mx[cur << 1])
: (mx[cur << 1 | 1]));
}
void change(long long cur, long long l, long long r, long long ql, long long qr,
long long c) {
if (ql <= l and r <= qr) {
sum[cur] = mx[cur] = c;
return;
}
long long mid = l + r >> 1;
if (ql <= mid)
change(cur << 1, l, mid, ql, qr, c);
else
change(cur << 1 | 1, mid + 1, r, ql, qr, c);
sum[cur] = sum[cur << 1] + sum[cur << 1 | 1];
mx[cur] = ((mx[cur << 1]) > (mx[cur << 1 | 1]) ? (mx[cur << 1])
: (mx[cur << 1 | 1]));
}
signed main() {
n = getint(), m = getint();
for (long long i = 1; i <= n; i++) val[i] = getint();
build(1, 1, n);
while (m--) {
long long opt = getint();
if (opt == 1) {
long long l = getint(), r = getint();
printf("%I64d\n", query(1, 1, n, l, r));
} else if (opt == 2) {
long long l = getint(), r = getint(), x = getint();
modify(1, 1, n, l, r, x);
} else {
long long k = getint(), x = getint();
change(1, 1, n, k, k, x);
}
}
return 0;
}
| 15 |
#include <bits/stdc++.h>
int main() {
using namespace std;
int n, a, b;
cin >> n;
int z[n];
for (int i = 0; i < n; i++) {
z[i] = 0;
}
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
z[a - 1]++;
z[b - 1]++;
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum += (z[i] * (z[i] - 1)) / 2;
}
cout << sum << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char str[105][105];
int n, m, k;
int gwin, rwin;
vector<int> num;
int dp[105];
void get() {
gwin = -1;
rwin = -1;
for (int i = 1; i <= n; i++) {
int R = -1;
int G = -1;
int has = 0;
for (int j = 0; j < m; j++) {
if (str[i][j] == 'G')
G = j;
else if (str[i][j] == 'R')
R = j;
else if (str[i][j] == '-')
has = 1;
}
if (G != -1 && R == -1 && has) gwin = 1;
if (R != -1 && G == -1 && has) rwin = 1;
if (R != -1 && G != -1 && abs(G - R) != 1) num.push_back(abs(R - G) - 1);
}
}
void Compute() {
if (rwin == 1 && gwin == -1) {
printf("Second\n");
} else if (rwin == -1 && gwin == 1) {
printf("First\n");
} else if (rwin == 1 && gwin == 1)
printf("Draw\n");
else {
bool flag = true;
for (int bit = 0; bit <= 8; bit++) {
int sum = 0;
for (size_t i = 0; i < num.size(); i++) {
int v = num[i];
sum = sum + ((v >> bit) & 1);
}
if (sum % (k + 1)) {
flag = false;
break;
}
}
if (!flag)
printf("First\n");
else
printf("Second\n");
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
scanf("%s", str[i]);
}
get();
Compute();
return 0;
}
| 18 |
#include <bits/stdc++.h>
const int MAXN = 100031;
int n = 0;
bool ip[MAXN] = {1, 1};
int a[MAXN], p[MAXN];
void sieve() {
for (int i = 2, c = 0; i < MAXN; i++) {
if (!ip[i]) {
p[c++] = i;
}
for (int j = 0; j < c && i * p[j] < MAXN; j++) {
ip[i * p[j]] = 1;
if (!(i % p[j])) {
break;
}
}
}
}
long long exp(long long a, int b, int p) {
long long ans = 1;
for (; b; b >>= 1) {
if (b & 1) {
ans = ans * a % p;
}
a = a * a % p;
}
return ans;
}
void solve() {
if (ip[n]) {
puts(n == 1 ? "YES\n1" : (n == 4 ? "YES\n1\n3\n2\n4" : "NO"));
} else {
if (n == 2) {
puts("YES\n1\n2");
return;
}
a[0] = 1;
a[n >> 1] = n - 1;
for (long long i = 1; i < (n >> 1); i++) {
a[i] = (i + 1) * exp(i, n - 2, n) % n;
a[n - 1 - i] = exp(a[i], n - 2, n);
}
a[n - 1] = n;
puts("YES");
for (int i = 0; i < n; i++) {
printf("%d\n", a[i]);
}
}
}
int main() {
sieve();
scanf("%d", &n);
solve();
return 0;
}
| 15 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double e = exp(1.0);
const long long M = 1e9 + 7;
const long long N = 2e5 + 7;
struct node {
long long da, id;
} a[N];
long long n, w;
long long sum, flag, idx, l, r;
bool cmp(node a, node b) { return a.da < b.da; }
void solve() {
cin >> n >> w;
l = (w + 1) / 2;
r = w;
sum = flag = 0;
idx = -1;
for (long long i = 1; i <= n; i++) {
cin >> a[i].da;
a[i].id = i;
if (a[i].da >= l && a[i].da <= r) flag = 1, idx = i;
if (a[i].da < l) sum += a[i].da;
}
if (flag) {
cout << 1 << '\n' << idx << '\n';
return;
}
if (sum < l || sum == 0) {
cout << "-1" << '\n';
return;
}
sort(a + 1, a + 1 + n, cmp);
sum = 0;
for (long long i = 1; i <= n; i++) {
sum += a[i].da;
if (sum >= l && sum <= r) {
cout << i << '\n';
for (long long j = 1; j <= i; j++) {
cout << a[j].id << " ";
}
cout << '\n';
return;
}
}
return;
}
signed main() {
long long T;
cin >> T;
while (T--) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300 + 5;
int n, m, ok, a[N], used[N];
inline bool read() {
if (scanf("%d %d %d", &n, &m, &ok) != 3) return false;
for (int i = 0; i < ok; i++) scanf("%d", &a[i]), a[i]--;
sort(a, a + ok);
return true;
}
vector<int> g[N];
vector<pair<int, int> > edges;
vector<pair<int, int> > ans;
void dfs(int s, int prev = -1) {
if (used[s]) return;
used[s] = true;
if (prev != -1) {
ans.push_back(make_pair(min(s, prev), max(s, prev)));
}
for (int i = 0; i < g[s].size(); i++) {
int v = g[s][i];
if (v == prev) continue;
dfs(v, s);
}
}
void add_edge(int i, int j) {
g[i].push_back(j);
g[j].push_back(i);
edges.push_back(make_pair(min(i, j), max(j, i)));
}
inline void solve() {
if (n == ok) {
puts("-1");
return;
}
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
if (i == a[1] || j == a[0] || i == a[0] || j == a[1]) continue;
add_edge(i, j);
}
for (int i = 0; i < n; i++) {
if (!binary_search(a, a + ok, i)) {
add_edge(i, a[0]);
add_edge(i, a[1]);
} else {
if (i != a[0] && i != a[1]) add_edge(i, a[0]);
}
}
for (int i = 0; i < n; i++) {
if (!binary_search(a, a + ok, i)) {
dfs(i);
sort(ans.begin(), ans.end());
break;
}
}
if (edges.size() < m) {
puts("-1");
return;
}
for (int i = 0; i < edges.size(); i++) {
if (!binary_search(ans.begin(), ans.begin() + n - 1, edges[i]) &&
ans.size() < m) {
ans.push_back(edges[i]);
}
}
for (int i = 0; i < ans.size(); i++) {
printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
}
}
int main() {
while (read()) solve();
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 10;
long long n, r, k, psum[N], ar[N], a[N], v[N];
bool check(long long x) {
for (long long i = 0; i < n; i++) {
a[i] = ar[i];
v[i] = 0;
}
long long add = 0, rem = k;
for (long long i = 0; i < n; i++) {
add -= v[i];
a[i] += add;
if (a[i] >= x) continue;
if (a[i] < x && x - a[i] > rem) return 0;
if (a[i] < x) {
rem -= x - a[i];
add += x - a[i];
v[i + 2 * r + 1] += x - a[i];
}
}
return rem >= 0;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> r >> k;
for (long long i = 0; i < n; i++) cin >> ar[i];
for (long long i = 1; i <= n; i++) psum[i] = psum[i - 1] + ar[i - 1];
for (long long i = 0; i < n; i++) {
long long l = max(0LL, i - r), rr = min(n - 1, i + r);
ar[i] = psum[rr + 1] - psum[l];
}
long long st = 0, ed = (long long)1001e15;
while (ed - st > 1) {
long long mid = (long long)(st + ed) / 2;
if (check(mid))
st = mid;
else
ed = mid;
}
cout << (long long)st;
}
| 12 |
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<vector>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<map>
#include<set>
#define endl '\n'
#define all(s) s.begin(),s.end()
#define lowbit(x) (x&-x)
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mem(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define pb push_back
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10;
int a[N], dp[N];
int main()
{
IOS
int n , m , T;
cin >> T;
while(T--){
cin >> n;
unordered_map<int,int> mp;
vector<int> v;
int mx = 0, ans = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
dp[i] = 0;
mp[a[i]]++;
ans = max(ans, mp[a[i]]);
v.push_back(a[i]);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
unordered_map<int,int> id;
for(int i = 0; i < v.size(); i++) dp[i + 1] = mp[v[i]], id[v[i]] = i + 1;
for(int i = 0; i < v.size(); i++){
if(v[i] == 1) continue;
vector<int> g;
for(int j = 1; j * j <= v[i]; j++){
if(v[i] % j == 0){
g.push_back(j);
if(j != 1 && v[i] / j != j)
g.push_back(v[i] / j);
}
}
for(auto x:g){
dp[i + 1] = max(dp[i + 1], dp[id[x]] + mp[v[i]]);
ans = max(ans, dp[i + 1]);
}
}
cout << n - ans << endl;
}
}
/*
1
4
3 1 2 1
*/
| 11 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool mini(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool maxi(T &a, T b) {
return a < b ? (a = b, true) : false;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
vector<int> a(n);
vector<int> pos(n);
for (int i = 0; i < (int)(n); i++) cin >> a[i], a[i]--, pos[a[i]] = i;
vector<vector<int> > join(n);
vector<int> p(n);
for (int i = 0; i < (int)(n); i++) p[i] = i, join[i].push_back(i);
for (int i = 0; i < (int)(n); i++) {
int t = pos[i];
if (t > 0 && a[t - 1] < i) {
int u = p[t], v = p[t - 1];
if (u != v) {
if (join[u].size() > join[v].size()) swap(u, v);
for (int x : join[u]) p[x] = v, join[v].push_back(x);
}
}
if (t < n - 1 && a[t + 1] < i) {
int u = p[t], v = p[t + 1];
if (u != v) {
if (join[u].size() > join[v].size()) swap(u, v);
for (int x : join[u]) p[x] = v, join[v].push_back(x);
}
}
cout << ((int)join[p[t]].size() == i + 1);
}
cout << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K, D, C, P = 0;
cin >> N >> K >> C;
D = K;
int* A = new int[C];
for (int i = 0; i < C; i++) cin >> A[i];
for (int i = 1; i <= N; i++) {
D--;
if (D == 0) {
P++;
D = K;
} else {
for (int j = 0; j < C; j++)
if (i == A[j]) {
P++;
D = K;
}
}
}
cout << P;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long m, lo, hi, a[100005], b[100005], A;
long long doit(long long x) {
long long r = 0;
for (int i = (0); i < (n); i++) {
if (!a[i]) {
r++;
continue;
}
if (x > m * b[i] / a[i] + b[i]) return m + 1;
r += 1 + x * a[i] / b[i];
if (r > m) return m + 1;
}
return r;
}
int main() {
scanf("%d%I64d", &n, &m);
for (int i = (0); i < (n); i++) scanf("%I64d%I64d", &a[i], &b[i]), A += a[i];
if (!A) {
if (n == m)
puts("-1");
else
puts("0");
return 0;
}
for (long long i = 1, j = (long long)(1e18 + 100), k; j >= i;) {
k = i + (j - i) / 2;
if (doit(k) >= m)
lo = k, j = k - 1;
else
i = k + 1;
}
for (long long i = 1, j = (long long)(1e18 + 100), k; j >= i;) {
k = i + (j - i) / 2;
if (doit(k) <= m)
hi = k, i = k + 1;
else
j = k - 1;
}
printf("%I64d\n", max(0LL, hi - lo + 1));
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k, r, c = 0;
cin >> n >> k;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (k % 2 == 0 && a[i] == k / 2)
r = (c++) % 2;
else if (a[i] > k / 2)
r = 1;
else
r = 0;
cout << r << " ";
}
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar();
return x;
}
const int maxn = 3e5 + 10;
const int maxq = 5e5 + 10;
struct Node {
int s[2], p, siz, v1, sum, v2, _s, tag1, tag2;
} tr[maxn * 2];
int n, q, tot, stk[maxn * 2];
inline void pushup(int x) {
tr[x].siz = tr[tr[x].s[0]].siz + tr[tr[x].s[1]].siz + (x > n);
tr[x].sum = tr[tr[x].s[0]].sum ^ tr[tr[x].s[1]].sum ^ tr[x].v1;
tr[x]._s = tr[tr[x].s[0]]._s | tr[tr[x].s[1]]._s | (x > n && tr[x].v2 > 0);
}
inline void pushrev(int x) {
swap(tr[x].s[0], tr[x].s[1]);
tr[x].tag1 ^= 1;
}
inline void pushadd(int x) {
tr[x]._s = tr[x].siz;
if (x > n) tr[x].v2 = 1;
tr[x].tag2 = 1;
}
inline void pushdown(int x) {
if (tr[x].tag1) {
pushrev(tr[x].s[0]), pushrev(tr[x].s[1]);
tr[x].tag1 = 0;
}
if (tr[x].tag2) {
pushadd(tr[x].s[0]), pushadd(tr[x].s[1]);
tr[x].tag2 = 0;
}
}
inline bool isroot(int x) {
return tr[tr[x].p].s[0] != x && tr[tr[x].p].s[1] != x;
}
inline void rotate(int x) {
int y = tr[x].p, z = tr[y].p, k = tr[y].s[1] == x;
if (!isroot(y)) tr[z].s[tr[z].s[1] == y] = x;
tr[x].p = z;
tr[y].s[k] = tr[x].s[k ^ 1], tr[tr[x].s[k ^ 1]].p = y;
tr[x].s[k ^ 1] = y, tr[y].p = x;
pushup(y), pushup(x);
}
void splay(int x) {
int top = 0, r = x;
stk[++top] = r;
while (!isroot(r)) stk[++top] = r = tr[r].p;
while (top) pushdown(stk[top--]);
while (!isroot(x)) {
int y = tr[x].p, z = tr[y].p;
if (!isroot(y)) rotate(tr[y].s[1] == x ^ tr[z].s[1] == y ? x : y);
rotate(x);
}
}
void access(int x) {
int z = x;
for (int y = 0; x; y = x, x = tr[x].p) {
splay(x);
tr[x].s[1] = y, pushup(x);
}
splay(z);
}
void makeroot(int x) { access(x), pushrev(x); }
int findroot(int x) {
access(x);
while (tr[x].s[0]) pushdown(x), x = tr[x].s[0];
splay(x);
return x;
}
void split(int x, int y) { makeroot(x), access(y); }
void link(int x, int y) {
makeroot(x);
if (findroot(y) != x) tr[x].p = y;
}
int main() {
n = read(), q = read();
tot = n;
for (int i = 1; i <= n; i++) tr[i].siz = 1;
for (int i = 1; i <= q; i++) {
int u = read(), v = read(), w = read();
makeroot(u);
if (findroot(v) != u) {
tot++, tr[tot].v1 = tr[tot].sum = w, tr[tot].siz = 1;
link(u, tot), link(tot, v);
puts("YES");
} else {
split(u, v);
if (tr[v]._s > 0 || (tr[v].sum ^ w) != 1)
puts("NO");
else {
pushadd(v);
puts("YES");
}
}
}
return 0;
}
| 19 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1e9 + 7;
int F(pair<int, pair<int, int>>& u) { return u.second.first; }
int S(pair<int, pair<int, int>>& u) { return u.second.second; }
int D(pair<int, pair<int, int>>& u) { return u.first; }
int main() {
int n;
scanf("%d", &n);
vector<pair<int, pair<int, int>>> u;
for (int i = 1; i <= n; ++i) {
int d;
scanf("%d", &d);
u.push_back({d, {2 * i, 2 * i - 1}});
}
sort(u.begin(), u.end());
reverse(u.begin(), u.end());
for (int i = 0; i + 1 < n; ++i) {
printf("%d %d\n", F(u[i]), F(u[i + 1]));
}
vector<int> pos(2 * n + 1);
for (int i = 0; i < n; ++i) {
pos[i] = F(u[i]);
}
for (int i = 0; i < n; ++i) {
if (i - (D(u[i]) - 1) >= 0) {
printf("%d %d\n", F(u[i - D(u[i]) + 1]), S(u[i]));
} else {
printf("%d %d\n", pos[i + D(u[i]) - 1], S(u[i]));
pos[i + D(u[i])] = S(u[i]);
}
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
que;
int t, n, m, k;
unordered_set<int> se[100010];
vector<int> ans;
bool vis[100010];
void init() {
ans.clear();
for (int i = 1; i <= n; ++i) {
vis[i] = 0;
se[i].clear();
}
while (!que.empty()) que.pop();
}
int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d%d", &n, &m, &k);
init();
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
se[u].insert(v);
se[v].insert(u);
}
if (k == 1) {
printf("2\n1\n");
continue;
}
for (int i = 1; i <= n; ++i) que.push(make_pair(se[i].size(), i));
bool flag1 = 0, flag2 = 0;
while (!que.empty()) {
int sz = que.top().first, u = que.top().second;
que.pop();
if (vis[u]) continue;
vis[u] = 1;
if (se[u].size() >= k) {
flag1 = 1;
break;
}
if (se[u].size() == k - 1 && (long long)k * (k - 1) / 2 <= m) {
bool flag = 1;
for (auto it1 = se[u].begin(); flag && it1 != se[u].end(); ++it1) {
for (auto it2 = it1; flag && it2 != se[u].end(); ++it2) {
if (it1 != it2)
flag &= se[*it1].find(*it2) != se[*it1].end() &&
se[*it2].find(*it1) != se[*it2].end();
}
}
if (flag) {
flag2 = 1;
ans.push_back(u);
for (auto it = se[u].begin(); it != se[u].end(); ++it) {
ans.push_back(*it);
}
break;
}
}
for (auto it = se[u].begin(); it != se[u].end(); ++it) {
se[*it].erase(u);
que.push(make_pair(se[*it].size(), *it));
}
se[u].clear();
}
if (flag1) {
printf("1 ");
for (int i = 1; i <= n; ++i) {
if (se[i].size() > 0) ans.push_back(i);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
printf("\n");
} else if (flag2) {
printf("2\n");
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i]);
printf("\n");
} else
printf("-1\n");
}
return 0;
}
| 18 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long long n, m, k;
long long p;
long long ans;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
Q;
long long A[N], H[N];
long long d[N];
bool check(long long hist) {
while (!Q.empty()) Q.pop();
for (int i = 1; i <= n; i++) {
if (hist >= m * A[i]) continue;
Q.push(make_pair(hist / A[i] - 1, i));
}
for (int i = 1; i <= n; i++) d[i] = hist;
long long tot = 0;
for (; tot < m * k; tot++) {
if (Q.empty()) break;
pair<int, int> te = Q.top();
Q.pop();
if (te.first < tot / k) return false;
d[te.second] += p;
if (d[te.second] - m * A[te.second] >= 0) continue;
Q.push(make_pair(d[te.second] / A[te.second] - 1, te.second));
}
if (tot > m * k) return false;
for (int i = 1; i <= n; i++) {
if (d[i] - m * A[i] >= H[i]) continue;
tot += (H[i] - (d[i] - m * A[i]) + p - 1) / p;
if (tot > m * k) return false;
}
return true;
}
long long solve() {
long long l = 0, r = (long long)1 << 55;
while (l < r) {
long long mid = (l + r) >> 1;
if (check(mid))
r = mid;
else
l = mid + 1;
}
return l;
}
int main() {
cin >> n >> m >> k >> p;
for (int i = 1; i <= n; i++) cin >> H[i] >> A[i];
cout << solve() << endl;
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
void smain();
int main() {
ios_base::sync_with_stdio(0);
smain();
return 0;
}
long long n;
long long d[26][5005][26];
void smain() {
string s;
for (; cin >> s;) {
n = s.length();
for (long long i = 0; i < n; i++) {
rotate(s.begin(), s.begin() + 1, s.end());
long long t = s.front() - 'a';
for (long long j = 0; j < n; j++) d[t][j][s[j] - 'a'] += 1;
}
long long res = 0;
for (long long t = 0; t < 26; t++) {
long long p = 0;
for (long long j = 0; j < n; j++) {
long long c = 0;
for (long long k = 0; k < 26; k++)
if (d[t][j][k] == 1) c += 1;
p = max(c, p);
}
res += p;
}
cout.precision(9);
cout << fixed << (double)res / n << '\n';
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, pair<int, int> > mp;
map<pair<int, int>, pair<int, int> >::iterator it;
bool check(int a, int b, int x, int y, int x1, int y1) {
if ((x < a || (x == a && y < b)) && (x1 > a || (x1 == a && y1 > b)))
return true;
else
return false;
}
int main() {
int a, ta, b, tb, hh, mm, cnt = 0;
cin >> a >> ta >> b >> tb;
string str;
cin >> str;
hh = (str[0] - '0') * 10 + (str[1] - '0');
mm = (str[3] - '0') * 10 + (str[4] - '0');
int hh1 = hh + ta / 60, mm1 = mm + ta % 60;
hh1 += mm1 / 60;
mm1 = mm1 % 60;
int h = 5, m = 0, h1, m1;
while (h <= 23 && m <= 59) {
h1 = h + tb / 60, m1 = m + tb % 60;
h1 += m1 / 60;
m1 = m1 % 60;
mp[{h, m}] = {h1, m1};
m += b % 60;
h += b / 60 + m / 60;
m %= 60;
}
it = mp.begin();
while (it != mp.end()) {
if (check(it->first.first, it->first.second, hh, mm, hh1, mm1))
cnt++;
else if (check(it->second.first, it->second.second, hh, mm, hh1, mm1))
cnt++;
else if ((it->first.first < hh ||
(it->first.first == hh && it->first.second <= mm)) &&
(it->second.first > hh1 ||
(it->second.first == hh1 && it->second.second >= mm1)))
cnt++;
it++;
}
cout << cnt << "\n";
}
| 8 |
#include <bits/stdc++.h>
int map[15][15], r, c, n, k;
int lowbit(int x) { return x & (-x); }
void add(int x, int y, int num) {
int i, j;
for (i = x; i < 12; i += lowbit(i))
for (j = y; j < 12; j += lowbit(j)) {
map[i][j] += num;
if (map[i][j] < 0) map[i][j] = 0;
}
}
int sum(int x, int y) {
int ans = 0;
int i, j;
for (i = x; i > 0; i -= lowbit(i))
for (j = y; j > 0; j -= lowbit(j)) ans += map[i][j];
return ans;
}
int main() {
int i, j, a, b, s;
while (~scanf("%d%d%d%d", &r, &c, &n, &k)) {
memset(map, 0, sizeof(map));
for (i = 0; i < n; i++) {
scanf("%d%d", &a, &b);
add(a, b, 1);
}
int ans = 0;
for (i = 1; i <= r; i++) {
for (j = 1; j <= c; j++) {
for (a = i; a <= r; a++) {
for (b = j; b <= c; b++) {
s = sum(a, b) + sum(i - 1, j - 1) - sum(i - 1, b) - sum(a, j - 1);
if (s >= k) ans++;
}
}
}
}
printf("%d\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(string s, string s2) { return (s + s2 < s2 + s); }
vector<string> v;
string s, inp;
long long n;
int main() {
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> inp;
v.push_back(inp);
}
sort(v.begin(), v.end(), cmp);
for (long long i = 0; i < v.size(); i++) {
if (!i)
s = v[i];
else {
s += v[i];
}
}
cout << s;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 8;
int n;
long long c;
long long p[maxn], s;
long long a[maxn];
long long res = 0;
long long ans = 0x7f7f7f7f;
long long con;
signed main() {
scanf("%d%lld", &n, &c);
for (int i = (1); i <= (n); i++) scanf("%lld", &p[i]), res += p[i];
for (int i = (1); i <= (n); i++)
scanf("%lld", &s), a[i] = s + c * (n - i) - p[i];
ans = res;
sort(a + 1, a + 1 + n);
for (int j = (1); j <= (n); j++) ans = min(ans, res += a[j] - c * (j - 1));
printf("%lld\n", ans);
return 0;
}
| 21 |
// time-limit: 2000
#include<bits/stdc++.h>
#define int long long
using namespace std;
void testCase();
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1, t = 0;
cin >> T;
while (t++ < T) {
testCase();
}
return 0;
}
void testCase() {
vector<int> a(4);
for (int& x : a) cin >> x;
sort(a.begin(), a.end());
cout << a[0] * a[2] << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << "\n";
err(++it, args...);
}
const long long int MOD = 1e9 + 7;
const long long int INF = 1e18;
const double E = 1e-6;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
long long int t = 1, T;
long long int i, j, x, y, k, w, a, b, n, m, d;
cin >> n >> k;
for (i = k + 1; i >= 1; i--) cout << i << " ";
for (i = k + 2; i <= n; i++) cout << i << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int max_n = 200005;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long n, k;
cin >> n >> k;
string s;
auto check = [&](long long l, long long r) -> bool {
cout << l << " " << r << endl;
cout.flush();
cin >> s;
return s == "Yes";
};
bool first = true;
long long l = 1, r = n, m;
while (true) {
l = max(1LL, l - k), r = min(n, r + k);
if (r - l <= 45) {
if (first) {
if (check(1, 1)) return 0;
first = false;
} else {
long long R = rand() % (r - l + 1);
if (check(l + R, l + R)) return 0;
}
} else {
m = (l + r) / 2;
if (check(l, m))
r = m;
else
l = m + 1;
}
}
return 0;
}
| 13 |
#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("/home/rv/Documents/cp/input.txt", "r", stdin);
freopen("/home/rv/Documents/cp/output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
ll n, ans = 0, i;
cin >> n;
vi v(n, 0);
for (i = 0; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end(), greater<int>());
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
if (v[i] % 2 == 0)
ans += v[i];
}
else {
if (v[i] % 2)
ans -= v[i];
}
}
if (ans > 0)
cout << "Alice";
else if (ans == 0)
cout << "Tie";
else
cout << "Bob";
cout << endl;
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
int ce = (n + 1) / 2;
int fl = n / 2;
int cnt = (ce * ce) + (fl * fl);
if (k <= cnt) {
cout << "YES"
<< "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (k) {
if ((i + j) & 1) {
cout << "S";
} else {
cout << "L";
k -= 1;
}
} else {
cout << "S";
}
}
cout << "\n";
}
} else {
cout << "NO"
<< "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long arr[100010];
int main() {
cin.sync_with_stdio(false);
long long P, N, K;
cin >> N >> K >> P;
multiset<long long> S;
for (int i = 0; i < N; ++i) cin >> arr[i];
long long i = N - 2;
long long sum = 0;
long long mi = N;
while (i >= 0 && K > 0) {
K--;
sum += arr[i];
S.insert(arr[i]);
i--;
}
if (sum > P) mi = min(mi, i + 2);
while (i >= 0) {
sum -= *S.begin();
sum += arr[i];
S.erase(S.begin());
S.insert(arr[i]);
i--;
if (sum > P) mi = i + 2;
}
cout << mi << endl;
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long mod = 998244353;
string a[100005];
multiset<unsigned long long> s;
map<unsigned long long, unsigned long long> mp;
int main() {
unsigned long long n;
cin >> n;
unsigned long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i].length()] += 1;
}
for (int i = 0; i < n; i++) {
for (map<unsigned long long, unsigned long long>::iterator it = mp.begin();
it != mp.end(); it++) {
unsigned long long fir = it->first;
unsigned long long sec = it->second;
unsigned long long t1 = 0;
unsigned long long t2 = 0;
unsigned long long t = 0;
unsigned long long temp;
if (fir < a[i].length()) {
for (int j = 0; j < a[i].length() - fir; j++) {
t *= 10;
t += (a[i][j] - '0');
}
t1 = t;
t2 = t;
for (int j = a[i].length() - fir; j <= a[i].length() - 1; j++) {
t1 *= 100;
t1 += (a[i][j] - '0');
t2 *= 10;
t2 += (a[i][j] - '0');
t2 *= 10;
}
temp = t1 + t2;
} else if (fir == a[i].length()) {
for (int j = 0; j < a[i].length(); j++) {
t *= 10;
t += (a[i][j] - '0');
t *= 10;
t += (a[i][j] - '0');
}
temp = t;
} else {
for (int j = 0; j < a[i].length(); j++) {
t1 *= 100;
t1 += (a[i][j] - '0');
t2 *= 10;
t2 += (a[i][j] - '0');
t2 *= 10;
}
temp = t1 + t2;
}
temp %= mod;
temp *= sec;
temp %= mod;
ans += temp;
ans %= mod;
}
}
cout << ans;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const int N = 1e6;
struct Segtree {
int n;
vector<long long> st;
const long long default_value = 0;
long long op(long long l, long long r) { return l + r; }
void build() {
for (int i = n - 1; i > 0; --i) st[i] = op(st[i << 1], st[i << 1 | 1]);
}
void modify(int p, long long value) {
for (st[p += n] = value; p > 1; p >>= 1) st[p >> 1] = op(st[p], st[p ^ 1]);
}
long long query(int l, int r) {
long long res = default_value;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) res = op(st[l++], res);
if (r & 1) res = op(res, st[--r]);
}
return res;
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n, 0);
for (int i_ = 0; i_ < (int)a.size(); ++i_) cin >> a[i_];
Segtree T[2];
T[0].n = n, T[1].n = n;
T[0].st.assign(2 * N, 0);
T[1].st.assign(2 * N, 0);
vector<int> I(n);
for (int i = 0; i < n; ++i) {
I[i] = i;
T[0].st[n + i] = 0;
T[1].st[n + i] = 0;
}
T[0].build(), T[1].build();
sort(I.begin(), I.end(), [&a](int i, int j) { return a[i] > a[j]; });
long long ans = 0;
for (auto i : I) {
ans += T[1].query(0, i);
long long q = T[0].query(0, i);
T[0].modify(i, 1);
T[1].modify(i, q);
}
cout << ans << '\n';
exit(0);
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
getline(cin, s1, char(10));
getline(cin, s2, char(10));
int n1 = s1.length();
int n2 = s2.length();
int e1 = 0;
int e2 = 0;
for (int i = 0; i < n1; i++) e1 += (int(s1[i] - '0'));
for (int i = 0; i < n2; i++) e2 += (int(s2[i] - '0'));
if ((e1 + 1 < e2) || ((e1 % 2 == 0) && (e1 == e2 - 1))) {
cout << "NO";
} else
cout << "YES";
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, m, res;
vector<int> s[maxn];
int fa[maxn], num[maxn];
int find(int x);
inline void aunion(int x, int y);
void judge() {
for (int i = 1; i <= n; i++) printf("[%d] ", fa[i]);
printf("\n");
}
int main() {
scanf("%d%d", &n, &m);
memset(fa, -1, sizeof(fa));
memset(num, 0, sizeof(num));
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
s[x].push_back(y), s[y].push_back(x);
;
}
res = n - 1;
set<int> gp;
gp.insert(1);
for (int i = 2; i <= n; i++) {
for (int j = 0; j < s[i].size(); j++) {
if (s[i][j] > i) continue;
int fv = find(s[i][j]);
num[fv]++;
}
for (set<int>::iterator it = gp.begin(); it != gp.end();) {
int fv = find(*it);
if (num[fv] < -fa[fv]) {
gp.erase(it++);
aunion(i, fv);
res--;
} else
it++;
num[fv] = 0;
}
gp.insert(i);
}
printf("%d\n", res);
return 0;
}
int find(int x) {
if (fa[x] < 0) return x;
return fa[x] = find(fa[x]);
}
void aunion(int x, int y) {
int u = find(x), v = find(y);
if (u == v) return;
if (fa[u] > fa[v]) swap(u, v);
fa[u] += fa[v];
fa[v] = u;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[101];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int counter = 0;
for (int j = 0; j < n; j++) {
if (s[j] == '1')
counter++;
else
break;
}
if (counter == n)
cout << counter << endl;
else
cout << counter + 1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
struct unu {
int y, x1, x2;
};
struct doi {
int x, y1, y2;
};
vector<unu> o;
vector<doi> v;
bool cmp(unu a, unu b) { return a.y < b.y; }
bool cmp2(doi a, doi b) { return a.y2 < b.y2; }
long long ans, nrd;
int aib[100005];
int sum(int x) {
int ans = 0;
while (x > 0) {
ans += aib[x];
x -= ((x ^ (x - 1)) & x);
}
return ans;
}
int query(int l, int r) { return (sum(r) - sum(l - 1)); }
void update(int x, int val) {
while (x <= 100000) {
aib[x] += val;
x += ((x ^ (x - 1)) & x);
}
}
int main() {
int i, j, k;
int x1, y1, x2, y2, l, r;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1 += 5001;
x2 += 5001;
y1 += 5001;
y2 += 5001;
if (x1 == x2)
v.push_back({x1, min(y1, y2), max(y1, y2)});
else
o.push_back({y1, min(x1, x2), max(x1, x2)});
}
sort(o.begin(), o.end(), cmp);
sort(v.begin(), v.end(), cmp2);
for (i = 0; i < o.size(); i++) {
memset(aib, 0, sizeof(aib));
k = v.size() - 1;
for (j = o.size() - 1; (j > i) && (o[j].y > o[i].y); j--) {
while ((k >= 0) && (v[k].y2 >= o[j].y)) {
if (v[k].y1 <= o[i].y) update(v[k].x, 1);
k--;
}
l = max(o[i].x1, o[j].x1);
r = min(o[i].x2, o[j].x2);
if (l <= r) {
nrd = query(l, r);
ans += (nrd * (nrd - 1) / 2);
}
}
}
cout << ans;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
template <typename T>
inline void Max(T &a, T &b) {
if (a < b) a = b;
}
template <typename T>
inline void Min(T &a, T &b) {
if (a > b) a = b;
}
const double pi = (double)acos(-1.0);
const double eps = (double)1e-10;
const int INF = (int)0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)1e5 + 10;
const int MAXM = (int)1e5 + 10;
int m, n;
int edgenum, dfs_clock, bcc_cnt;
int head[MAXN];
int pre[MAXN], low[MAXN], bccno[MAXN];
int val[MAXN];
int uu[MAXN], vv[MAXN];
int parent[MAXN];
vector<int> Bcc[MAXN];
int top[MAXN], fa[MAXN], son[MAXN], dep[MAXN];
int siz[MAXN], p[MAXN], fp[MAXN], pos;
int sum[MAXN << 2], SUM[MAXN << 2], flag[MAXN << 2], FLAG[MAXN << 2];
int connect[MAXN];
int id_in_loop[MAXN];
int in_loop[MAXN];
int mark[MAXN];
int ID[MAXN];
struct EDGE {
int u, v;
EDGE(int from, int to) {
u = from;
v = to;
}
};
stack<EDGE> S;
struct Edge {
int v;
int next;
} edge[MAXN << 1];
void Init() {
memset(head, -1, sizeof(head));
memset(son, -1, sizeof(son));
edgenum = pos = 0;
}
void AddEdge(int u, int v) {
edge[edgenum].v = v;
edge[edgenum].next = head[u];
head[u] = edgenum++;
}
int Tarjan(int u, int fa) {
int lowu = low[u] = pre[u] = ++dfs_clock;
int child = 0;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
EDGE e = EDGE(u, v);
if (!pre[v]) {
S.push(e);
child++;
int lowv = Tarjan(v, u);
lowu = min(lowv, lowu);
if (lowv >= pre[u]) {
bcc_cnt++;
Bcc[bcc_cnt].clear();
for (;;) {
EDGE x = S.top();
S.pop();
if (bccno[x.u] != bcc_cnt) {
Bcc[bcc_cnt].push_back(x.u);
bccno[x.u] = bcc_cnt;
}
if (bccno[x.v] != bcc_cnt) {
Bcc[bcc_cnt].push_back(x.v);
bccno[x.v] = bcc_cnt;
}
if (x.u == u && x.v == v) break;
}
}
} else if (pre[v] < pre[u] && v != fa) {
S.push(e);
lowu = min(lowu, pre[v]);
}
}
return lowu;
}
void Find_bcc() {
memset(pre, 0, sizeof(pre));
memset(low, 0, sizeof(low));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
Tarjan(1, -1);
}
int Find(int x) { return x == parent[x] ? x : parent[x] = Find(parent[x]); }
void Merge(int u, int v) {
int x = Find(u);
int y = Find(v);
if (x != y) parent[x] = y;
}
void DFS1(int u, int fath, int d) {
dep[u] = d;
fa[u] = fath;
siz[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == fath) continue;
DFS1(v, u, d + 1);
siz[u] += siz[v];
if (son[u] == -1 || siz[v] > siz[son[u]]) {
son[u] = v;
}
}
}
void DFS2(int u, int tp) {
top[u] = tp;
if (son[u] != -1) {
p[u] = ++pos;
fp[p[u]] = u;
DFS2(son[u], tp);
} else {
p[u] = ++pos;
fp[p[u]] = u;
return;
}
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v != son[u] && v != fa[u]) DFS2(v, v);
}
}
void build1(int l, int r, int rt) {
if (l == r) {
sum[rt] = 1;
return;
}
int mid = (l + r) >> 1;
build1(l, mid, rt << 1);
build1(mid + 1, r, rt << 1 | 1);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void Push_down1(int rt, int len) {
if (flag[rt]) {
flag[rt] = 0;
flag[rt << 1] ^= 1;
flag[rt << 1 | 1] ^= 1;
sum[rt << 1] = (len - (len >> 1)) - sum[rt << 1];
sum[rt << 1 | 1] = (len >> 1) - sum[rt << 1 | 1];
}
}
int __ul, __ur;
void update1(int l, int r, int rt) {
if (__ul <= l && r <= __ur) {
flag[rt] ^= 1;
sum[rt] = r - l + 1 - sum[rt];
return;
}
int mid = (l + r) >> 1;
Push_down1(rt, r - l + 1);
if (__ul <= mid) update1(l, mid, rt << 1);
if (__ur > mid) update1(mid + 1, r, rt << 1 | 1);
sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void UPDATE1(int u, int v) {
int f1 = top[u], f2 = top[v];
int ret = 0;
if (u == v) return;
while (f1 != f2) {
if (dep[f1] < dep[f2]) {
swap(f1, f2);
swap(u, v);
}
__ul = p[f1], __ur = p[u];
update1(1, bcc_cnt, 1);
u = fa[f1];
f1 = top[u];
}
if (u == v) return;
if (dep[u] > dep[v]) swap(u, v);
__ul = p[son[u]], __ur = p[v];
update1(1, bcc_cnt, 1);
}
void build2(int l, int r, int rt) {
if (l == r) {
SUM[rt] = 1;
return;
}
int mid = (l + r) >> 1;
build2(l, mid, rt << 1);
build2(mid + 1, r, rt << 1 | 1);
SUM[rt] = SUM[rt << 1] + SUM[rt << 1 | 1];
}
void Push_down2(int rt, int len) {
if (FLAG[rt]) {
FLAG[rt] = 0;
FLAG[rt << 1] ^= 1;
FLAG[rt << 1 | 1] ^= 1;
SUM[rt << 1] = (len - (len >> 1)) - SUM[rt << 1];
SUM[rt << 1 | 1] = (len >> 1) - SUM[rt << 1 | 1];
}
}
void update2(int l, int r, int rt) {
if (__ul <= l && r <= __ur) {
FLAG[rt] ^= 1;
SUM[rt] = r - l + 1 - SUM[rt];
return;
}
int mid = (l + r) >> 1;
Push_down2(rt, r - l + 1);
if (__ul <= mid) update2(l, mid, rt << 1);
if (__ur > mid) update2(mid + 1, r, rt << 1 | 1);
SUM[rt] = SUM[rt << 1] + SUM[rt << 1 | 1];
}
void UPDATE2(int u, int v, const int &loopsiz) {
if (u == v) return;
int idu = id_in_loop[u];
int idv = id_in_loop[v];
int s1, s2;
if (idu < idv) {
s1 = idv - idu;
s2 = idu + loopsiz - idv;
if (s1 < s2) {
__ul = idu, __ur = idv - 1;
update2(1, loopsiz, 1);
} else if (s1 > s2) {
__ul = idv, __ur = loopsiz;
update2(1, loopsiz, 1);
if (idu > 1) {
__ul = 1, __ur = idu - 1;
update2(1, loopsiz, 1);
}
} else {
int nxtu = idu + 1;
int preu = idu - 1 < 1 ? loopsiz : idu - 1;
if (in_loop[nxtu] < in_loop[preu]) {
__ul = idu, __ur = idv - 1;
update2(1, loopsiz, 1);
} else {
__ul = idv, __ur = loopsiz;
update2(1, loopsiz, 1);
if (idu > 1) {
__ul = 1, __ur = idu - 1;
update2(1, loopsiz, 1);
}
}
}
} else {
s1 = idv - idu + loopsiz;
s2 = idu - idv;
if (s1 < s2) {
__ul = idu, __ur = loopsiz;
update2(1, loopsiz, 1);
if (idv > 1) {
__ul = 1, __ur = idv - 1;
update2(1, loopsiz, 1);
}
} else if (s1 > s2) {
__ul = idv, __ur = idu - 1;
update2(1, loopsiz, 1);
} else {
int nxtu = idu + 1 > loopsiz ? 1 : idu + 1;
int preu = idu - 1;
if (in_loop[nxtu] < in_loop[preu]) {
__ul = idu, __ur = loopsiz;
update2(1, loopsiz, 1);
if (idv > 1) {
__ul = 1, __ur = idv - 1;
update2(1, loopsiz, 1);
}
} else {
__ul = idv, __ur = idu - 1;
update2(1, loopsiz, 1);
}
}
}
}
int NUM;
int CURID;
void DFS(int cur, const int &loopid) {
mark[cur] = 1;
if (bccno[cur] != loopid) {
ID[cur] = ++NUM;
for (int i = head[cur]; ~i; i = edge[i].next) {
int v = edge[i].v;
if (mark[v]) continue;
DFS(v, loopid);
}
return;
}
id_in_loop[cur] = ++CURID;
in_loop[CURID] = cur;
for (int i = head[cur]; ~i; i = edge[i].next) {
int v = edge[i].v;
if (mark[v]) continue;
DFS(v, loopid);
}
}
void work() {
scanf("%d%d", &n, &m);
Init();
for (int i = 1; i <= n; i++) {
uu[i] = read();
vv[i] = read();
AddEdge(uu[i], vv[i]);
AddEdge(vv[i], uu[i]);
}
Find_bcc();
int loopid, loopsiz;
for (int i = 1; i <= bcc_cnt; i++)
if (Bcc[i].size() > 2) {
for (int j = 0; j < Bcc[i].size(); j++) {
int u = Bcc[i][j];
bccno[u] = i;
}
loopid = i, loopsiz = Bcc[i].size();
}
DFS(Bcc[loopid][0], loopid);
++NUM;
for (int j = 0; j < Bcc[loopid].size(); j++) {
int u = Bcc[loopid][j];
ID[u] = NUM;
}
Init();
for (int i = 1; i <= n; i++) parent[i] = i;
for (int i = 1; i <= n; i++)
if (bccno[uu[i]] != loopid && bccno[vv[i]] != loopid) {
AddEdge(ID[uu[i]], ID[vv[i]]);
AddEdge(ID[vv[i]], ID[uu[i]]);
Merge(ID[uu[i]], ID[vv[i]]);
}
for (int i = 1; i <= n; i++)
if (bccno[uu[i]] == loopid || bccno[vv[i]] == loopid) {
if (bccno[uu[i]] == bccno[vv[i]]) continue;
AddEdge(ID[uu[i]], ID[vv[i]]);
AddEdge(ID[vv[i]], ID[uu[i]]);
if (bccno[uu[i]] != loopid) {
int tmp = Find(ID[uu[i]]);
connect[tmp] = vv[i];
} else {
int tmp = Find(ID[vv[i]]);
connect[tmp] = uu[i];
}
}
build1(1, bcc_cnt, 1);
build2(1, loopsiz, 1);
DFS1(1, -1, 0);
DFS2(1, 1);
__ul = 1, __ur = 1;
update1(1, bcc_cnt, 1);
for (int i = 1; i <= m; i++) {
int U, V;
scanf("%d%d", &U, &V);
int X = Find(ID[U]);
int Y = Find(ID[V]);
UPDATE1(ID[U], ID[V]);
if (!(X == Y && X != NUM && Y != NUM)) {
int connectu = (X == NUM) ? U : connect[X];
int connectv = (Y == NUM) ? V : connect[Y];
UPDATE2(connectu, connectv, loopsiz);
}
int ans = sum[1] + SUM[1];
int ok = SUM[1] > 0;
printf("%d\n", 1 + ans - ok);
}
}
int main() {
work();
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); }
struct index {
int value;
vector<int> pos;
};
struct node {
int start, end;
long long int max_sum;
};
struct answer {
bool is_valid;
long long int max_sum;
};
struct node segment_tree[400002];
struct index ind[300002];
int arr[300002];
bool is_cut[300002];
int binary_search_in_struct(struct index a[], int key, int low, int high) {
if (low > high) {
return -1;
} else {
int mid = (low + high) / 2;
if (a[mid].value == key) {
return mid;
} else if (a[mid].value < key) {
low = mid + 1;
return binary_search_in_struct(a, key, low, high);
} else {
high = mid - 1;
return binary_search_in_struct(a, key, low, high);
}
}
}
long long int build_segment_tree(int in, int a[], int low, int high) {
if (low == high) {
segment_tree[in].start = low;
segment_tree[in].end = high;
if (a[low] >= 0) {
segment_tree[in].max_sum = a[low];
} else {
segment_tree[in].max_sum = 0;
}
return segment_tree[in].max_sum;
} else {
segment_tree[in].start = low;
segment_tree[in].end = high;
segment_tree[in].max_sum =
build_segment_tree(2 * in + 1, a, low, (low + high) / 2) +
build_segment_tree(2 * in + 2, a, (low + high) / 2 + 1, high);
return segment_tree[in].max_sum;
}
}
struct answer query_segment_tree(int in, int pos_start, int pos_end) {
struct answer ans;
if (segment_tree[in].end < pos_start || segment_tree[in].start > pos_end) {
ans.is_valid = false;
return ans;
} else if (segment_tree[in].start >= pos_start &&
segment_tree[in].end <= pos_end) {
ans.max_sum = segment_tree[in].max_sum;
ans.is_valid = true;
return ans;
}
struct answer left_ans = query_segment_tree(2 * in + 1, pos_start, pos_end);
struct answer right_ans = query_segment_tree(2 * in + 2, pos_start, pos_end);
ans.max_sum = 0;
if (left_ans.is_valid) {
ans.max_sum += left_ans.max_sum;
}
if (right_ans.is_valid) {
ans.max_sum += right_ans.max_sum;
}
ans.is_valid = true;
return ans;
}
int main() {
int n;
vector<int> b;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
b.push_back(arr[i]);
}
sort(b.begin(), b.end());
int k = 0;
for (int i = 0; i < n; i++) {
if (binary_search_in_struct(ind, b[i], 0, k - 1) == -1) {
ind[k].value = b[i];
ind[k].pos.clear();
k++;
}
}
for (int i = 0; i < n; i++) {
ind[binary_search_in_struct(ind, arr[i], 0, k - 1)].pos.push_back(i);
}
build_segment_tree(0, arr, 0, n - 1);
long long int max;
struct answer max_ans;
int max_left, max_right;
for (int i = 0; i < k; i++) {
if (ind[i].pos.size() > 1) {
if (ind[i].pos[0] + 1 <= ind[i].pos[ind[i].pos.size() - 1] - 1) {
max_ans = query_segment_tree(0, (ind[i].pos[0]) + 1,
(ind[i].pos[ind[i].pos.size() - 1]) - 1);
max = 2 * ind[i].value + max_ans.max_sum;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
} else {
max = 2 * ind[i].value;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
break;
}
}
for (int i = 0; i < k; i++) {
if (ind[i].pos.size() > 1) {
if (ind[i].pos[0] + 1 <= ind[i].pos[ind[i].pos.size() - 1] - 1) {
max_ans = query_segment_tree(0, ind[i].pos[0] + 1,
ind[i].pos[ind[i].pos.size() - 1] - 1);
if (max < 2 * ind[i].value + max_ans.max_sum) {
max = 2 * ind[i].value + max_ans.max_sum;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
} else {
if (max < 2 * ind[i].value) {
max = 2 * ind[i].value;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
}
}
}
int count = 0;
memset(is_cut, 1, sizeof(is_cut));
for (int i = max_left; i <= max_right; i++) {
if (arr[i] >= 0) {
is_cut[i] = false;
}
}
is_cut[max_left] = false;
is_cut[max_right] = false;
for (int i = 0; i < n; i++) {
if (is_cut[i]) {
count++;
}
}
printf("%lld %d\n", max, count);
for (int i = 0; i < n; i++) {
if (is_cut[i]) {
printf("%d ", i + 1);
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, inf = 0x3fffffff;
struct Edge {
int nxt, to, len;
} e[maxn << 1];
int hd[maxn], e_cnt = 1;
void add(int u, int v, int l) {
e[++e_cnt] = (Edge){hd[u], v, l};
hd[u] = e_cnt;
}
int n, m, d1[maxn], d2[maxn], pre[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b, len;
cin >> a >> b >> len;
a++;
b++;
add(a, b, len);
add(b, a, len);
}
memset(d1, -1, sizeof d1);
memset(d2, -1, sizeof d2);
queue<int> q;
q.push(1);
d1[1] = 0;
while (!q.empty()) {
int c = q.front();
q.pop();
for (int i = hd[c]; i; i = e[i].nxt)
if (d1[e[i].to] == -1) d1[e[i].to] = d1[c] + 1, q.push(e[i].to);
}
q.push(n);
d2[n] = 0;
while (!q.empty()) {
int c = q.front();
q.pop();
for (int i = hd[c]; i; i = e[i].nxt)
if (!e[i].len && d2[e[i].to] == -1)
pre[e[i].to] = i, d2[e[i].to] = d2[c] + 1, q.push(e[i].to);
}
int min_dis = 1e6;
vector<int> vec;
for (int i = 1; i <= n; i++)
if (d2[i] != -1) min_dis = min(min_dis, d1[i]);
for (int i = 1; i <= n; i++)
if (d2[i] != -1 && d1[i] == min_dis) vec.push_back(i);
for (int i = min_dis; i; i--) {
int min_dig = 10;
for (int j : vec)
for (int k = hd[j]; k; k = e[k].nxt)
if (d1[e[k].to] == i - 1) min_dig = min(min_dig, e[k].len);
vector<int> nxt;
for (int j : vec)
for (int k = hd[j]; k; k = e[k].nxt)
if (d1[e[k].to] == i - 1 && e[k].len == min_dig) {
if (d2[e[k].to] == -1) nxt.push_back(e[k].to);
if (d2[e[k].to] == -1 || d2[e[k].to] > d2[j] + 1)
d2[e[k].to] = d2[j] + 1, pre[e[k].to] = k;
}
vec = nxt;
}
vector<int> ans;
int tmp = 1;
while (d1[tmp] < min_dis) {
ans.push_back(e[pre[tmp]].len);
tmp = e[pre[tmp] ^ 1].to;
}
reverse(ans.begin(), ans.end());
for (int i : ans) cout << i;
if (ans.size() == 0) cout << 0;
cout << '\n' << d2[1] + 1 << '\n';
tmp = 1;
while (tmp != n) {
cout << tmp - 1 << ' ';
tmp = e[pre[tmp] ^ 1].to;
}
cout << n - 1;
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod2 = 998244353;
long long int invmod(long long int a, unsigned long long int b,
long long int n) {
long long int x = 1, y = a;
while (b > 0) {
if (b % 2 == 1) x = (x * y) % n;
y = (y * y) % n;
b /= 2;
}
return (x % n);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
long long int fact[n + 1], invfact[n + 1];
fact[0] = 1;
invfact[0] = 1;
for (long long int i = 1; i < n + 1; i++) {
fact[i] = (fact[i - 1] * (i)) % mod2;
invfact[i] = (invfact[i - 1] * invmod(i, mod2 - 2, mod2)) % mod2;
}
long long int ans = (n * fact[n]) % mod2;
for (long long int i = 1; i < n + 1; i++)
ans = (ans - (fact[n] * invfact[i]) % mod2 + mod2) % mod2;
cout << ans + 1 << '\n';
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
void printprecise(double l, long long int precision) {
std::cout << std::fixed;
std::cout << std::setprecision(precision);
std::cout << l;
}
long long int ncr(long long int n, long long int k) {
long long int C[n + 1][k + 1];
long long int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
long long int power_wm(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int power(long long int x, long long int y) {
long long int res = 1;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
long long int root(long long int x, vector<long long int> &roots) {
if (roots[x] != x) roots[x] = root(roots[x], roots);
return roots[x];
}
void unoin(long long int p, long long int q, vector<long long int> &roots,
vector<long long int> &rank) {
long long int rp = root(p, roots);
long long int rq = root(q, roots);
if (rank[rq] < rank[rp]) {
roots[rq] = rp;
} else if (rank[rp] > rank[rq]) {
roots[rp] = rq;
} else {
roots[rp] = rq;
rank[rq]++;
}
}
long long int dig_sum(long long int n) {
long long int s = 0;
while (n) {
s += n % 10;
n /= 10;
}
return s;
}
long long int gcd(long long int a, long long int b) {
if (a < b) {
a = a + b;
b = a - b;
a = a - b;
}
if (b == 0) return a;
return gcd(b, a % b);
}
long long int toD(long long int x, long long int y, long long int m) {
return x * m + y % m;
}
pair<long long int, long long int> twoD(long long int p, long long int m) {
return (make_pair(p / m, p % m));
}
vector<long long int> dist;
vector<vector<long long int> > g;
void dfs(long long int p) {
if (dist[p] != -1) {
return;
}
dist[p] = 0;
long long int maxd = -1;
for (long long int i = 0; i < g[p].size(); i++) {
dfs(g[p][i]);
maxd = max(maxd, dist[g[p][i]]);
}
dist[p] = maxd + 1;
}
vector<long long int> fact;
void calfact() {
fact[0] = 1;
for (long long int i = 1; i < 1000001; i++) {
fact[i] = (fact[i - 1] * i) % 998244353;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<vector<pair<long long int, long long int> > > s(
2, vector<pair<long long int, long long int> >(4));
long long int x, y;
for (long long int i = 0; i < 4; i++) {
cin >> x >> y;
x += 100;
y += 100;
s[0][i].first = x, s[0][i].second = y;
}
for (long long int i = 0; i < 4; i++) {
cin >> x >> y;
x += 100;
y += 100;
s[1][i].first = x, s[1][i].second = y;
}
sort((s[0]).begin(), (s[0]).end());
sort((s[1]).begin(), (s[1]).end());
long long int pll = 0;
if (s[1][0].second == s[1][1].second) {
pll = 1;
}
vector<vector<long long int> > plane(201, vector<long long int>(201));
for (long long int i = s[pll][0].first; i < s[pll][3].first + 1; i++) {
for (long long int j = s[pll][0].second; j < s[pll][1].second + 1; j++) {
plane[i][j] = 1;
}
}
long long int intersects = 0;
long long int y1 = s[1 - pll][0].second;
long long int x1 = s[1 - pll][0].first, x2 = s[1 - pll][3].first;
for (long long int i = 0; i < s[1 - pll][1].first - s[1 - pll][0].first + 1;
i++) {
for (long long int j = y1 - i; j < y1 + i + 1; j++) {
if (plane[x1 + i][j] || plane[x2 - i][j]) {
intersects = 1;
break;
}
}
if (intersects) {
break;
}
}
cout << (intersects ? "YES" : "NO");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const long long MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
long long power_md(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res = (res % md * a % md) % md;
a = (a % md * a % md) % md;
n /= 2;
}
res %= md;
return res;
}
long long power(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res *= a;
a = a * a;
n /= 2;
}
return res;
}
long long abst(long long a) { return ((a < 0) ? (-1 * a) : (a)); }
class cmp_set {
public:
bool operator()(long long a, long long b) { return a > b; }
};
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while (!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
__attribute__((unused)) long long idx,
__attribute__((unused)) long long LINE_NUM) {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H,
Tail... T) {
if (idx > 0)
cerr << ", ";
else
cerr << "Line(" << LINE_NUM << ") ";
stringstream ss;
ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
int32_t main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
long long n;
cin >> n;
long long a[n + 1];
for (long long i = 1; i <= n; i++) cin >> a[i];
long long res[n + 1];
res[0] = 1e12;
a[0] = -1;
stack<long long> st;
st.push(0);
for (long long i = 1; i <= n; i++) {
res[i] = 1;
while (!st.empty() && (a[i] > a[st.top()])) {
res[i] = max(res[i], res[st.top()] + 1);
st.pop();
}
st.push(i);
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
if (res[i] < 1e12) {
ans = max(ans, res[i]);
}
}
cout << ans << "\n";
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 505;
const int Maxl = 26;
const int Inf = 1000000000;
int n, m;
char B[Maxn][Maxn];
int l[Maxl], r[Maxl];
int res[Maxn][Maxl][Maxl];
pair<int, int> p[Maxn][Maxl][Maxl];
int main() {
fill((int*)res, (int*)res + Maxn * Maxl * Maxl, Inf);
fill((int*)res[0], (int*)res[0] + Maxl * Maxl, 0);
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) scanf(" %c", &B[i][j]);
for (int j = 0; j < Maxl; j++) {
l[j] = r[j] = 0;
for (int k = 0; k < m; k++) {
l[j] += k % 2 == 0 && j + 'a' != B[i][k];
r[j] += k % 2 && j + 'a' != B[i][k];
}
}
for (int j = 0; j < Maxl; j++)
for (int k = 0; k < Maxl; k++)
if (res[i][j][k] != Inf)
for (int j2 = 0; j2 < Maxl; j2++)
if (j != j2)
for (int k2 = 0; k2 < Maxl; k2++)
if (k != k2 && j2 != k2) {
int cand = res[i][j][k] + l[j2] + r[k2];
if (cand < res[i + 1][j2][k2]) {
res[i + 1][j2][k2] = cand;
p[i + 1][j2][k2] = make_pair(j, k);
}
}
}
pair<int, int> v = make_pair(0, 0);
for (int i = 0; i < Maxl; i++)
for (int j = 0; j < Maxl; j++)
if (res[n][i][j] < res[n][v.first][v.second]) v = make_pair(i, j);
printf("%d\n", res[n][v.first][v.second]);
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++)
B[i][j] = j % 2 ? v.second + 'a' : v.first + 'a';
v = p[i + 1][v.first][v.second];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) printf("%c", B[i][j]);
printf("\n");
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 202000;
const int lmaxn = 20;
const int inf = 1010000000;
int n, aoriginal[maxn], a[maxn];
vector<int> compress;
int bsearch(int g) {
int low = -1, high = compress.size();
while (high > low + 1) {
int mid = (low + high) / 2;
if (compress[mid] < g) {
low = mid;
} else {
high = mid;
}
}
return high;
}
int BIT[maxn * 3], BITR[maxn * 3];
int rbc;
int rbstates[maxn];
int* val[maxn * 2 * lmaxn];
int to[maxn * 2 * lmaxn];
int queryR(int v) {
v = n * 3 - v + 3;
int ret = -1;
for (int i = v; i > 0; i -= (i & (-i))) {
if (BITR[i] > ret) {
ret = BITR[i];
}
}
return ret;
}
void updateR(int v, int mx) {
v = n * 3 - v + 3;
for (int i = v; i <= (n * 3 + 2); i += (i & (-i))) {
if (mx > BITR[i]) {
val[rbc] = &BITR[i];
to[rbc] = BITR[i];
BITR[i] = mx;
rbc++;
}
}
}
void rollback(int ns) {
while (rbc > ns) {
rbc--;
*(val[rbc]) = to[rbc];
}
}
int query(int v) {
int ret = -1;
for (int i = v; i > 0; i -= (i & (-i))) {
if (BIT[i] > ret) {
ret = BIT[i];
}
}
return ret;
}
void update(int v, int mx) {
for (int i = v; i <= (n * 3 + 2); i += (i & (-i))) {
if (mx > BIT[i]) {
BIT[i] = mx;
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &aoriginal[i]);
aoriginal[i] -= i;
compress.push_back(aoriginal[i]);
compress.push_back(aoriginal[i] + 1);
compress.push_back(aoriginal[i] - 1);
}
compress.push_back(inf);
compress.push_back(-inf);
sort(compress.begin(), compress.end());
compress.resize(
distance(compress.begin(), unique(compress.begin(), compress.end())));
rbc = 0;
for (int i = 0; i <= n * 3; i++) {
BITR[i] = 0;
}
rbstates[n] = 0;
for (int i = n - 1; i >= 0; i--) {
rbstates[i] = rbc;
int v = bsearch(aoriginal[i]);
updateR(v, queryR(v) + 1);
}
for (int i = 0; i <= n * 3; i++) {
BIT[i] = 0;
}
int ans = 0;
for (int i = 0; i < n; i++) {
rollback(rbstates[i]);
int npans = -1;
if (i >= 1 && i < n - 1) {
npans = max(query(bsearch(aoriginal[i - 1])) +
queryR(bsearch(aoriginal[i - 1] - 1)),
query(bsearch(aoriginal[i + 1] + 1)) +
queryR(bsearch(aoriginal[i + 1])));
}
if (i == 0) {
npans = queryR(bsearch(aoriginal[i + 1]));
}
if (i == n - 1) {
npans = query(bsearch(aoriginal[i - 1]));
}
if (npans > ans) {
ans = npans;
}
int v = bsearch(aoriginal[i]);
update(v, query(v) + 1);
}
printf("%d\n", (n - 1) - ans);
return 0;
}
| 17 |
#include <bits/stdc++.h>
using namespace std;
long long n;
set<long long> S;
int main() {
scanf("%lld", &n);
for (long long k = 1; k * k <= n; k++) {
if (n % k == 0) {
S.insert(n / k + n * (n / k + 1) / 2 - n);
long long d = n / k;
S.insert(n / d + n * (n / d + 1) / 2 - n);
}
}
for (set<long long>::iterator it = S.begin(); it != S.end(); it++)
printf("%lld ", *it);
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int take[105];
int main() {
memset(take, 0, sizeof take);
cin >> s1 >> s2;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == 'X') take[i] = 1;
}
int cnt = 0;
bool pre0 = 0;
for (int i = 0; i < s2.size(); i++) {
if (s2[i] == 'X') {
pre0 = 0;
continue;
} else {
if (pre0) {
if (!take[i]) {
take[i] = 1;
cnt++;
pre0 = 0;
continue;
} else if (i > 0 && !take[i - 1]) {
take[i - 1] = 1;
cnt++;
pre0 = 0;
continue;
}
} else {
if (!take[i]) {
if (i > 0 && !take[i - 1]) {
take[i] = take[i - 1] = 1;
cnt++;
pre0 = 0;
continue;
} else if (i + 1 < s2.size() && !take[i + 1]) {
take[i] = take[i + 1] = 1;
cnt++;
pre0 = 0;
continue;
}
}
}
pre0 = 1;
}
}
cout << cnt;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
vector<int> ad[2005];
int ara[2005], sz[2005], ans_ara[2005];
set<int> st;
void dfs(int src, int pre) {
sz[src] = 1;
for (int v : ad[src]) {
if (v == pre) continue;
dfs(v, src);
sz[src] += sz[v];
}
}
void dfs2(int src, int pre) {
set<int>::iterator it;
int cnt = 0;
for (it = st.begin(); it != st.end(); it++) {
if (cnt == ara[src]) {
ans_ara[src] = *it;
st.erase(*it);
break;
}
++cnt;
}
for (int v : ad[src]) {
if (v == pre) continue;
dfs2(v, src);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, i, j, root, p;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> p >> ara[i];
if (!p) root = i;
if (p) {
ad[i].push_back(p);
ad[p].push_back(i);
}
}
dfs(root, 0);
for (i = 1; i <= n; i++) {
if (ara[i] > sz[i] - 1) {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
for (i = 1; i <= n; i++) st.insert(i);
dfs2(root, 0);
for (i = 1; i <= n; i++) cout << ans_ara[i] << ' ';
cout << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[n + 1];
int sum = 0;
for (int i = 1; i <= n; i++) cin >> arr[i], sum += arr[i];
if (sum % n != 0) {
cout << -1 << "\n";
continue;
}
int ech = sum / n;
int ops = 0;
vector<pair<int, pair<int, int>>> v;
for (int i = 2; i <= n; i++) {
if (arr[i] % i != 0) {
int mod = arr[i] % i;
mod = i - mod;
v.push_back({1, {i, mod}});
arr[i] += mod;
int x = arr[i] / i;
v.push_back({i, {1, x}});
ops += 2;
} else {
int x = arr[i] / i;
v.push_back({i, {1, x}});
ops++;
}
}
for (int i = 2; i <= n; i++) {
ops++;
v.push_back({1, {i, ech}});
}
cout << ops << "\n";
for (auto el : v) {
cout << el.first << " " << el.second.first << " " << el.second.second
<< "\n";
}
}
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, x;
cin >> n >> x;
long long mx = 0, mx1 = 0, mx2 = 0;
while (n--) {
long long u, uu;
cin >> u >> uu;
if (uu < u) {
if (mx < u - uu) {
mx = u - uu;
mx2 = uu;
}
}
mx1 = max(mx1, u);
}
long long re = 0;
if (mx || mx1 >= x) {
if (mx1 >= x)
cout << 1 << endl;
else {
re += 1;
x -= mx1;
if (x % mx)
re += x / mx + 1;
else
re += x / mx;
cout << re << endl;
}
} else
cout << -1 << endl;
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
long long int arr[210000];
long long int count[210000];
long long int sum[210000];
long long int temp[210000];
long long int bin[210000][64];
long long int spc[210000];
int main() {
long long int i, j, p, s;
long long int mul = 1;
long long int n, k, x;
scanf("%I64d%I64d%I64d", &n, &k, &x);
for (i = 1; i <= k; i++) mul *= x;
for (i = 1; i <= n; i++) scanf("%I64d", &arr[i]);
for (i = 1; i <= n; i++) {
for (j = 0, p = 1; j < 64; j++, p *= 2) {
if (((arr[i]) & (p)) == (p)) {
bin[i][j] = 1;
sum[j] += 1;
} else
bin[i][j] = 0;
if (arr[i] < p) break;
}
}
long long int t;
long long int max = 0;
long long int summ;
for (i = 1; i <= n; i++) {
for (s = 0; s < 64; s++) {
count[s] = sum[s];
if (count[s] - bin[i][s] == 0) count[s] = 0;
}
t = arr[i] * mul;
summ = 0;
for (j = 0, p = 1; j < 64; j++, p *= 2) {
if (((t) & (p)) == (p)) count[j] = 1;
if (count[j] >= 1) summ += p;
}
if (summ > max) max = summ;
}
printf("%I64d", max);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long mod(long long a, long long m) { return a < 0 ? a % m + m : a % m; }
int extended_euclid(int a, int b, int &x, int &y) {
int xx = y = 0;
int yy = x = 1;
while (b) {
int q = a / b;
int t = b;
b = a % b;
a = t;
t = xx;
xx = x - q * xx;
x = t;
t = yy;
yy = y - q * yy;
y = t;
}
return a;
}
int mod_inverse(int a, int n) {
int x, y;
int d = extended_euclid(a, n, x, y);
if (d > 1) return -1;
return mod(x, n);
}
long long powmod(long long a, long long b, long long m) {
long long res = 1;
while (b > 0)
if (b & 1)
res = (res * a) % m, --b;
else
a = (a * a) % m, b >>= 1;
return res % m;
}
const int MOD = 998244353;
const int m = 1 << 20;
const int root = 565042129;
const int root_1 = 950391366;
const int root_pw = 1 << 20;
void fft(vector<int> &a, bool invert) {
int n = a.size();
for (int i = 1, j = 0; i < n; i++) {
int bit = n >> 1;
for (; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if (i < j) swap(a[i], a[j]);
}
for (int len = 2; len <= n; len <<= 1) {
int wlen = invert ? root_1 : root;
for (int i = len; i < root_pw; i <<= 1)
wlen = (int)(1LL * wlen * wlen % MOD);
for (int i = 0; i < n; i += len) {
int w = 1;
for (int j = 0; j < len / 2; j++) {
int u = a[i + j], v = (int)(1LL * a[i + j + len / 2] * w % MOD);
a[i + j] = u + v < MOD ? u + v : u + v - MOD;
a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + MOD;
w = (int)(1LL * w * wlen % MOD);
}
}
}
if (invert) {
int n_1 = mod_inverse(n, MOD);
for (int &x : a) x = (int)(1LL * x * n_1 % MOD);
}
}
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(12) << fixed;
int n, k;
cin >> n >> k;
vector<int> f(m, 0);
for (int i = 0; i < k; ++i) {
int d;
cin >> d;
f[d]++;
}
n /= 2;
fft(f, false);
for (int i = 0; i < m; ++i) {
f[i] = powmod(f[i], n, 998244353);
}
fft(f, true);
long long result = 0;
for (auto x : f) {
result += ((long long)x) * x;
result %= 998244353;
}
cout << result << endl;
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
bool is_vowel(char str) {
str = tolower(str);
if (str == 'a' || str == 'e' || str == 'i' || str == 'o' || str == 'u' ||
str == 'y') {
return true;
}
return false;
}
int main() {
int ans = 0;
vector<string> v = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE", "GIN",
"RUM", "SAKE", "TEQUILA", "VODKA", "WHISKEY",
"WINE", "1", "2", "3", "4",
"5", "6", "7", "8", "9",
"10", "11", "12", "13", "14",
"15", "16", "17", "0"};
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
for (int i = 0; i < v.size(); i++) {
if (str == v[i]) {
ans++;
break;
}
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char c;
int sz, a0, a1;
int main() {
cin >> sz;
for (int i = 0; i < sz; i++) {
cin >> c;
if (c == '0')
a0++;
else
a1++;
}
cout << max(a1, a0) - min(a1, a0);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long cnt[400111];
char c[400111];
long long cntn[200111];
char cn[200111];
long long cntm[200111];
char cm[200111];
int pf[400111];
int main() {
int n, m;
scanf("%d%d", &n, &m);
int i = 0;
while (n-- > 0) {
int ccnt;
char cc;
scanf("%d-%c", &ccnt, &cc);
if (i == 0) {
cntn[i] = ccnt;
cn[i] = cc;
i++;
continue;
}
if (cc == cn[i - 1]) {
cntn[i - 1] += ccnt;
} else {
cntn[i] = ccnt;
cn[i] = cc;
i++;
}
}
n = i;
i = 0;
while (m-- > 0) {
int ccnt;
char cc;
scanf("%d-%c", &ccnt, &cc);
if (i == 0) {
cntm[i] = ccnt;
cm[i] = cc;
i++;
continue;
}
if (cc == cm[i - 1]) {
cntm[i - 1] += ccnt;
} else {
cntm[i] = ccnt;
cm[i] = cc;
i++;
}
}
m = i;
long long ans = 0;
if (m == 1) {
for (int i = 0; i < n; i++) {
if (cn[i] == cm[0]) {
ans += max(0ll, cntn[i] - cntm[0] + 1);
}
}
} else if (m == 2) {
for (int i = 0; i < n - 1; i++) {
if (cn[i] == cm[0] && cn[i + 1] == cm[1]) {
ans += (cntn[i] >= cntm[0]) && (cntn[i + 1] >= cntm[1]);
}
}
} else {
int ii = 0;
for (int i = 1; i < m - 1; i++) {
c[ii] = cm[i];
cnt[ii] = cntm[i];
ii++;
}
ii++;
for (int i = 0; i < n; i++) {
c[ii] = cn[i];
cnt[ii] = cntn[i];
ii++;
}
for (int i = 1; i < ii; i++) {
int cf = pf[i - 1];
while (cf > 0 && (c[cf] != c[i] || cnt[cf] != cnt[i])) {
cf = pf[cf - 1];
}
if (!(c[cf] != c[i] || cnt[cf] != cnt[i])) {
cf++;
}
pf[i] = cf;
if (pf[i] == m - 2) {
if (i - m + 2 >= 0) {
if (c[i + 1] == cm[m - 1] && cnt[i + 1] >= cntm[m - 1] &&
c[i - m + 2] == cm[0] && cnt[i - m + 2] >= cntm[0]) {
++ans;
}
}
}
}
}
cout << ans << endl;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
const long long N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long power(long long a, long long b = mod - 2) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long n, m, cyc = 0, sink, mx = 0;
vector<pair<long long, long long> > adjo[10005], path;
vector<long long> vis(10005);
vector<vector<long long> > cycles(10005);
map<long long, long long> mp;
void dfso(long long cur, long long prev) {
vis[cur] = 1;
for (pair<long long, long long> i : adjo[cur])
if (!vis[i.first])
path.push_back(i), dfso(i.first, cur);
else if (vis[i.first] == 1 && i.first != prev) {
cycles[++cyc].push_back(i.second);
mp[i.second]--;
for (long long k = ((long long)(path.size()) - 1); k >= (0); k--) {
long long v = path[k].first, c = path[k].second;
if (v == i.first) break;
cycles[cyc].push_back(c);
mp[c]--;
}
}
path.pop_back();
vis[cur] = 2;
}
struct edge {
long long x, y, cap;
};
vector<edge> e;
vector<vector<long long> > adj;
void createEdge(long long x, long long y, long long cap) {
e.push_back({x, y, cap});
adj[x].push_back((long long)(e.size()) - 1);
e.push_back({y, x, 0});
adj[y].push_back((long long)(e.size()) - 1);
}
void createGraph() {
sink = cyc + mx + 2;
adj.resize(sink + 1);
for (long long i = (1); i <= (cyc); i++)
createEdge(1, i + 1, (long long)(cycles[i].size()) - 1);
for (long long i = (1); i <= (cyc); i++) {
for (long long j : cycles[i]) createEdge(i + 1, j + cyc + 1, 1);
}
for (long long i = (1); i <= (mx); i++) createEdge(cyc + i + 1, sink, 1);
for (pair<long long, long long> i : mp)
if (i.second) createEdge(1, i.first + cyc + 1, 1);
}
vector<long long> d, counter;
bool shortest_path() {
d.assign(sink + 1, -1);
queue<long long> q;
q.push(1);
d[1] = 0;
while (!q.empty()) {
long long u = q.front();
q.pop();
for (long long i : adj[u]) {
long long v = e[i].y;
if (e[i].cap > 0 && d[v] < 0) {
q.push(v);
d[v] = d[u] + 1;
}
}
}
return (d[sink] > -1) ? true : false;
}
long long dfs(long long cur, long long mincap) {
if (cur == sink) return mincap;
for (long long& i = counter[cur]; i < (long long)(adj[cur].size()); i++) {
long long id = adj[cur][i], v = e[id].y;
if (d[v] != d[cur] + 1 || !e[id].cap) continue;
long long to_augment = dfs(v, min(mincap, e[id].cap));
if (to_augment) {
e[id].cap -= to_augment;
e[id ^ 1].cap += to_augment;
return to_augment;
}
}
return 0;
}
long long max_flow() {
long long flow = 0;
while (true) {
if (!shortest_path()) break;
counter.assign(sink + 1, 0);
while (true) {
long long newflow = dfs(1, inf);
if (!newflow) break;
flow += newflow;
}
}
return flow;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, m;
cin >> n >> m;
;
for (long long i = (1); i <= (m); i++) {
long long x, y, z;
cin >> x >> y >> z;
;
adjo[x].push_back({y, z});
adjo[y].push_back({x, z});
mx = max(mx, z);
mp[z]++;
}
dfso(1, 0);
createGraph();
cout << max_flow() << '\n';
;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int n, path[200010], cnt = 0, fa[200010];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 2; i <= n; ++i) {
cin >> fa[i];
}
path[cnt++] = n;
while (path[cnt - 1] != 1) {
path[cnt] = fa[path[cnt - 1]];
++cnt;
}
for (int i = cnt - 1; i >= 0; --i) {
cout << path[i] << ' ';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define all(x) x.begin(),x.end()
#define rep(i, j, n) for (int i=j;i<int(n);++i)
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;
typedef pair<ll, int> P1;
const int maxN = 10000 + 5;
priority_queue<P1, vector<P1>, greater<P1> > que;
vector<P> city;
int n;
ll ans = 0;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++) {
ll a, c;
scanf("%lld %lld", &a, &c);
city.push_back(P(a, c));
ans += c;
}
sort(city.begin(), city.end());
que.push(P1(0, 0));
vector<bool> vis(n, false);
// vector<int> fa(n, 0);
while(que.size()) {
ll d; int i;
tie(d, i) = que.top(); que.pop();
if (vis[i]) continue;
vis[i] = true;
if (i == n - 1) {
ans += d;
break;
}
if (i > 0) {
que.push({d, i - 1});
}
int j = lower_bound(city.begin(), city.end(), P(city[i].first + city[i].second, LONG_MAX)) - city.begin() - 1;
que.push({d, j});
if (j + 1 < n) {
que.push({d + city[j+1].first - city[i].first - city[i].second, j + 1});
}
}
printf("%lld\n", ans);
} | 14 |
#include <bits/stdc++.h>
using namespace std;
const int iinf = 1e9 + 7;
const long long linf = 1ll << 60;
const double dinf = 1e10;
inline long long read() {
bool f = 0;
long long x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return x;
}
inline void scf(int &x) {
bool f = 0;
x = 0;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') {
f = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
if (f) x = -x;
return;
}
void scf(int &x, int &y) {
scf(x);
return scf(y);
}
void scf(int &x, int &y, int &z) {
scf(x);
scf(y);
return scf(z);
}
const int N = 1e4 + 100;
int n;
long long c;
long long p[N], s[N];
long long dp[2][N];
inline long long MIN(long long a, long long b) { return a < b ? a : b; }
int main() {
scf(n);
c = read();
for (int i = (1); i <= (n); i++) p[i] = read();
for (int i = (1); i <= (n); i++) s[i] = read();
for (int i = (1); i <= (n); i++) dp[0][i] = linf;
for (int i = (1); i <= (n); i++) {
for (int j = (0); j <= (n); j++) dp[i & 1][j] = linf;
for (int j = (0); j <= (i); j++) {
dp[i & 1][j] = dp[i - 1 & 1][j] + s[i];
if (j) {
long long calc = c * (long long)(i - j) + p[i];
dp[i & 1][j] = MIN(dp[i & 1][j], calc + dp[i - 1 & 1][j - 1]);
}
}
}
long long ans = linf;
for (int i = (0); i <= (n); i++) (dp[n & 1][i] < ans) && (ans = dp[n & 1][i]);
printf("%I64d\n", ans);
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
int n;
int val[MAXN], cost[MAXN];
map<int, int> dp[MAXN];
int gcd(int a, int b) {
if (a > b) swap(a, b);
while (b ^= a ^= b ^= a %= b)
;
return a;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> val[i];
for (int i = 1; i <= n; i++) cin >> cost[i];
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (map<int, int>::iterator it = dp[i].begin(); it != dp[i].end(); it++) {
int num = it->first;
int score = it->second;
if (dp[i + 1].find(num) == dp[i + 1].end()) {
dp[i + 1][num] = score;
} else
dp[i + 1][num] = min(dp[i + 1][num], score);
int nex = gcd(num, val[i + 1]);
if (dp[i + 1].find(nex) == dp[i + 1].end()) {
dp[i + 1][nex] = score + cost[i + 1];
} else
dp[i + 1][nex] = min(dp[i + 1][nex], score + cost[i + 1]);
}
}
if (dp[n].find(1) == dp[n].end())
cout << -1 << endl;
else
cout << dp[n][1] << endl;
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
double prob[1 << 18];
double p[18][18];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) cin >> p[i][j];
prob[(1 << n) - 1] = 1;
for (int i = (1 << n) - 1; i; i--) {
int pop = __builtin_popcount(i);
double f = prob[i] * 2. / (pop * (pop - 1));
for (int a = 0; a < n; a++) {
if (!(i >> a & 1)) continue;
for (int b = 0; b < n; b++) {
if (!(i >> b & 1) || a == b) continue;
prob[i ^ 1 << a] += f * p[b][a];
}
}
}
for (int i = 0; i < n; i++) cout << setprecision(20) << prob[1 << i] << ' ';
cout << endl;
}
| 11 |
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
string s, t;
cin >> s >> t;
vector<int> left_index = vector<int>(t.size(), 0);
vector<int> right_index = vector<int>(t.size(), 0);
int i_t = 0;
for (int i_s = 0; i_s < s.size(); ++i_s) {
if (s[i_s] == t[i_t]) {
left_index[i_t] = i_s + 1;
++i_t;
if (i_t == t.size()) {
break;
}
}
}
i_t = t.size() - 1;
for (int i_s = s.size() - 1; i_s >= 0; --i_s) {
if (s[i_s] == t[i_t]) {
right_index[i_t] = i_s + 1;
--i_t;
if (i_t < 0) {
break;
}
}
}
int max_width = 0;
for (i_t = 0; i_t < t.size() - 1; ++i_t) {
int width = right_index[i_t + 1] - left_index[i_t];
if (i_t == 0) {
max_width = width;
} else {
max_width = max(max_width, width);
}
}
cout << max_width << "\n";
} | 7 |
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
const long long int INF = 1e18;
void init() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool checkp2(long long int n) { return n && !(n & (n - 1)); }
long long int numsetbit(long long int n) {
long long int count = 0;
for (; n; count++) {
n = n & (n - 1);
}
return count;
}
long long int powe(long long int x, long long int n, const long long int mod) {
x = x % mod;
long long int po = 1;
while (n) {
if (n & 1) {
po = (po * x) % mod;
}
n = n >> 1;
x = ((x % mod) * (x % mod)) % mod;
}
return po;
}
long long int powm(long long int x, long long int n) {
long long int po = 1;
while (n) {
if (n & 1) {
po = (po * x);
}
n = n >> 1;
x = (x * x);
}
return po;
}
bool isPrime(long long int x) {
if (x < 2) return 0;
for (long long int y = 2; y * y <= x; y++) {
if (x % y == 0) {
return 0;
}
}
return true;
}
const long long int mod = 1e9 + 7;
void solve() {
long long int n;
cin >> n;
if (n == 2) {
cout << "2" << endl;
cout << "1 2" << endl;
return;
}
cout << "2" << endl;
cout << n << " " << n - 2 << endl;
cout << n - 1 << " " << n - 1 << endl;
for (long long int i = 0; i < n - 3; i++) {
cout << n - i - 1 << " " << n - i - 3 << endl;
}
}
int main() {
init();
long long int t = 1;
cin >> t;
for (long long int i = 1; i <= t; i++) {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 6, oo = 1e9, MOD = 1073741824;
int a, b, c;
long long sum;
int div(int x) {
int cnt = 0;
for (int i = 1; i * i <= x; i++)
if (!(x % i)) {
if (x / i != i) cnt++;
cnt++;
}
return cnt;
}
int main() {
cin >> a >> b >> c;
for (int i = 1; i <= a; i++)
for (int j = 1; j <= b; j++)
for (int k = 1; k <= c; k++) sum = sum % MOD + div(i * j * k) % MOD;
cout << sum << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6000000, N = 212345;
int br[maxn][10];
int p[maxn];
int n, q;
vector<int> fpr(int x) {
vector<int> ret;
while (p[x]) {
ret.push_back(p[x]);
x /= p[x];
}
if (x > 1) ret.push_back(x);
sort(begin(ret), end(ret));
vector<int> rret;
for (int i = 0; i < ret.size();) {
int j = i;
while (j < ret.size() && ret[j] == ret[i]) j++;
if ((j - i) & 1) rret.push_back(ret[i]);
i = j;
}
return rret;
}
vector<int> vp[N];
int t[2 * N];
void update(int p, int v) {
p += n;
t[p] = min(t[p], v);
while (p > 1) {
t[p >> 1] = min(t[p], t[p ^ 1]);
p >>= 1;
}
}
int query(int l, int r) {
int ans = 0x3f3f3f3f;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = min(ans, t[l++]);
if (r & 1) ans = min(ans, t[--r]);
}
return ans;
}
int ans[2123123];
vector<pair<int, int>> qr[N];
int main() {
memset(t, 0x3f, sizeof t);
for (int i = 2; i < maxn; i++) {
if (!p[i]) {
for (int j = i + i; j < maxn; j += i) {
p[j] = i;
}
}
}
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
vp[i] = fpr(x);
}
for (int i = 0; i < q; i++) {
int l, r;
scanf("%d%d", &l, &r);
l--, r--;
qr[r].emplace_back(l, i);
}
for (int i = 0; i < n; i++) {
for (int m = 0; m < (1 << vp[i].size()); m++) {
int mask = 1;
for (int j = 0; j < vp[i].size(); j++) {
if ((m >> j) & 1) mask *= vp[i][j];
}
for (int f = 0; f < 8; f++) {
int cost = vp[i].size() - __builtin_popcount(m) + f;
if (br[mask][f]) {
update(br[mask][f] - 1, cost);
}
}
int f = vp[i].size() - __builtin_popcount(m);
br[mask][f] = max(br[mask][f], i + 1);
}
for (auto& qry : qr[i]) {
int l, id;
tie(l, id) = qry;
ans[id] = query(l, i + 1);
}
}
for (int i = 0; i < q; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
| 21 |
#include <bits/stdc++.h>
using namespace std;
int n, m, nr, fl[55], fc[55];
char a[55][55];
int dl[4] = {0, 0, 1, -1};
int dc[4] = {1, -1, 0, 0};
void ve(int l, int c) {
fl[l] = nr;
fc[c] = nr;
a[l][c] = 'x';
for (int i = 1; i < l; i++)
if (a[i][c] == '#') ve(i, c);
for (int i = l + 1; i <= n; i++)
if (a[i][c] == '#') ve(i, c);
for (int j = 1; j < c; j++)
if (a[l][j] == '#') ve(l, j);
for (int j = c + 1; j <= m; j++)
if (a[l][j] == '#') ve(l, j);
}
int main() {
cin.sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i <= n; i++) cin.getline(a[i] + 1, 55);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '#') {
nr++;
ve(i, j);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '.' && fl[i] == fc[j] && fl[i]) {
cout << "No";
return 0;
}
cout << "Yes";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mymap;
map<int, int> mymap2;
struct dat {
int x;
int y;
};
struct dat st;
vector<struct dat> vec;
vector<struct dat>::iterator it;
bool comp1(struct dat st1, struct dat st2) { return st1.x < st2.x; }
bool comp2(struct dat st1, struct dat st2) { return st1.y < st2.y; }
int main() {
int n, i, j, f;
int x[10];
int y[10];
for (i = 0; i < 8; i++) {
cin >> x[i] >> y[i];
st.x = x[i];
st.y = y[i];
vec.push_back(st);
}
sort(vec.begin(), vec.end(), comp2);
sort(vec.begin(), vec.end(), comp1);
it = vec.begin();
int cnt = 0;
for (i = 0; i < 8; i++) {
if (mymap[it->x] == 0) {
mymap[it->x] = 23;
cnt++;
}
it++;
}
if (cnt != 3) {
cout << "ugly\n";
return 0;
}
it = vec.begin();
cnt = 0;
for (i = 0; i < 8; i++) {
if (mymap2[it->y] == 0) {
mymap2[it->y] = 23;
cnt++;
}
it++;
}
if (cnt != 3) {
cout << "ugly\n";
return 0;
}
it = vec.begin();
if (it->y == (it + 3)->y && it->y == (it + 5)->y &&
(it + 2)->y == (it + 4)->y && (it + 2)->y == (it + 7)->y &&
(it + 1)->y == (it + 6)->y) {
cout << "respectable\n";
} else {
cout << "ugly\n";
}
cin >> i;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 2e9, N = 5e3 + 10, MOD = 1e9 + 7;
string s, t;
long long n;
long long bad(string &s, char c) {
long long an = s.back() != c;
for (long long i = 1; i < s.size(); i++)
if (s[i] != s[i - 1]) ++an;
return an;
}
long long ind(long long d, string &s) {
if (d == 0) return 0;
long long c = 0;
for (long long i = 0; i < s.size(); i++) {
if (i + 1 == (long long)s.size()) return s.size();
if (s[i] != s[i + 1]) ++c;
if (c == d) return i + 1;
}
return 0;
}
void trm(string &a, string &b) {
while (!a.empty() && a.back() == 'a') a.pop_back();
while (!b.empty() && b.back() == 'b') b.pop_back();
}
void kil(string &a, string &b, vector<pair<long long, long long> > &an) {
trm(a, b);
while (!a.empty() || !b.empty()) {
an.push_back(make_pair(a.size(), b.size()));
swap(a, b);
trm(a, b);
}
}
vector<pair<long long, long long> > go(string a, string b) {
vector<pair<long long, long long> > an;
long long abad = bad(a, 'a'), bbad = bad(b, 'b');
long long df = abs(abad - bbad);
long long need = df >> 1;
if (need == 0)
kil(a, b, an);
else {
string *bg = &a, *sm = &b;
if (abad < bbad) swap(bg, sm);
long long bgn = ind(need, *bg);
if (df & 1 && (*bg)[bgn - 1] != (*sm)[0]) ++need;
string na, nb;
long long lsm = 0, lbg = need;
if (bbad > 0) ++lsm, ++lbg;
if (sm == &a) {
long long inda = ind(lsm, a), indb = ind(lbg, b);
na = b.substr(0, indb) + a.substr(inda);
nb = a.substr(0, inda) + b.substr(indb);
an.push_back(make_pair(inda, indb));
} else {
long long inda = ind(lbg, a), indb = ind(lsm, b);
na = b.substr(0, indb) + a.substr(inda);
nb = a.substr(0, inda) + b.substr(indb);
an.push_back(make_pair(inda, indb));
}
kil(na, nb, an);
}
return an;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cerr.tie(0);
cin >> s >> t;
n = (long long)(s.size());
vector<pair<long long, long long> > res1 = go(s, t);
vector<pair<long long, long long> > res2 = go(t, s);
cout << min((long long)(res1.size()), (long long)(res2.size())) << "\n";
cerr << "\n";
if (res2.size() < res1.size())
for (pair<long long, long long> i : res2)
cout << i.second << " " << i.first << "\n";
else
for (pair<long long, long long> i : res1)
cout << i.first << " " << i.second << "\n";
}
| 20 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 24;
int friends[N], dp[1 << N], g[N][N], nxt[1 << N];
int n, m;
int tabs = 0;
int solve(int mask, int finish) {
if (mask == (1 << n) - 1) {
nxt[mask] = 0;
return dp[mask] = 0;
}
if (dp[mask] != -1) return dp[mask];
int ans = n + 1;
for (int i = 0; i < n; i++) {
if ((mask >> i) & 1) {
int new_mask = mask | friends[i];
if (new_mask != mask) {
int new_val = solve(new_mask, finish | (1 << i)) + 1;
if (new_val < ans) {
ans = new_val;
nxt[mask] = (1 << i) | nxt[new_mask];
}
}
}
}
assert(nxt[mask] != 0);
return dp[mask] = ans;
}
int main() {
ios_base::sync_with_stdio(false);
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
friends[u] |= (1 << v);
friends[v] |= (1 << u);
g[u][v] = g[v][u] = 1;
}
if (m == n * (n - 1) / 2) {
cout << 0 << endl;
return 0;
}
for (int i = 0; i < n; i++) friends[i] |= (1 << i);
memset(dp, -1, sizeof(dp));
int ans = n;
int choose = 0;
for (int i = 0; i < n; i++) {
int new_val = solve(1 << i, 0);
if (new_val < ans) {
ans = new_val;
choose = nxt[(1 << i)];
}
}
printf("%d\n", ans);
for (int j = 0; j < n; j++) {
if ((choose >> j) & 1) printf("%d ", j + 1);
}
printf("\n");
return 0;
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
const int maxN = 2 * (int)1e5 + 100;
int p[maxN];
int a[maxN];
vector<int> all[maxN];
int nxt[maxN];
int l[maxN], r[maxN];
int nxt_pos[maxN];
int ansR[maxN];
int ansL[maxN];
bool ok[maxN];
int prv[maxN];
void solve1() {
int best = 1;
for (int i = 1; i <= n; i++) {
if (all[i].size() < all[best].size()) {
best = i;
}
}
for (auto t : all[best]) {
int x = best;
ansR[t] = ansL[t] = 1;
int need = nxt[best];
for (int j = t + 1; j <= m; j++) {
ansR[j] = ansR[j - 1];
if (a[j] == need) {
need = nxt[a[j]];
ansR[j]++;
}
}
need = prv[best];
for (int j = t - 1; j >= 1; j--) {
ansL[j] = ansL[j + 1];
if (a[j] == need) {
need = prv[a[j]];
ansL[j]++;
}
}
for (int i = 1; i <= q; i++) {
if (ok[i]) continue;
if (t < l[i] || t > r[i]) continue;
if (ansR[r[i]] + ansL[l[i]] - 1 >= n) ok[i] = true;
}
}
for (int i = 1; i <= q; i++) cout << ok[i];
cout << '\n';
}
int ans[maxN];
int t[4 * maxN];
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v] = ans[tl];
return;
}
int tm = (tl + tr) / 2;
build(v + v, tl, tm);
build(v + v + 1, tm + 1, tr);
t[v] = min(t[v + v], t[v + v + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
assert(tl <= tr);
if (tl == l && tr == r) return t[v];
int tm = (tl + tr) / 2;
if (r <= tm)
return get(v + v, tl, tm, l, r);
else if (l > tm)
return get(v + v + 1, tm + 1, tr, l, r);
else
return min(get(v + v, tl, tm, l, tm),
get(v + v + 1, tm + 1, tr, tm + 1, r));
}
vector<int> link[maxN];
vector<pair<int, int> > byR[maxN];
const int LOG = 20;
int up[maxN][LOG];
void solve2() {
for (int i = m; i >= 1; i--) {
up[i][0] = nxt_pos[i];
for (int j = 0; j + 1 < LOG; j++) {
up[i][j + 1] = up[up[i][j]][j];
}
int cur = i;
int t = n - 1;
for (int j = LOG - 1; j >= 0; j--) {
if (t >= (1 << j)) {
t -= (1 << j);
cur = up[cur][j];
}
}
if (cur == 0)
ans[i] = (int)1e9;
else
ans[i] = cur;
}
build(1, 1, m);
for (int i = 1; i <= q; i++) {
if (get(1, 1, m, l[i], r[i]) <= r[i])
cout << 1;
else
cout << 0;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n; i++) {
nxt[p[i]] = p[i % n + 1];
prv[p[i]] = p[(i + 2 * n - 2) % n + 1];
}
for (int i = 1; i <= m; i++) {
cin >> a[i];
all[a[i]].push_back(i);
}
for (int i = 1; i <= m; i++) {
nxt_pos[i] = 0;
if (all[nxt[a[i]]].empty() || all[nxt[a[i]]].back() <= i) continue;
int ind = upper_bound(all[nxt[a[i]]].begin(), all[nxt[a[i]]].end(), i) -
all[nxt[a[i]]].begin();
nxt_pos[i] = all[nxt[a[i]]][ind];
link[nxt_pos[i]].push_back(i);
}
for (int i = 1; i <= q; i++) {
cin >> l[i] >> r[i];
byR[r[i]].emplace_back(l[i], i);
}
solve2();
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> Min, Max;
int a[2012][2012];
int n;
void init() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
}
int cnt;
bool vis[2012][2012];
void dfs(int x, int y) {
if (x == 0 || y == 0 || x > n || y > n) return;
if (vis[x][y] || (a[x][y] == 0)) return;
vis[x][y] = true;
++cnt;
if (x < Min.first) Min.first = x;
if (y < Min.second) Min.second = y;
if (x > Max.first) Max.first = x;
if (y > Max.second) Max.second = y;
dfs(x - 1, y);
dfs(x + 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
}
void Deal1() {
int Tsqr = 0, Tcir = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if ((!vis[i][j]) && (a[i][j] == 1)) {
Max = make_pair(0, 0), Min = make_pair(n + 1, n + 1);
cnt = 0;
dfs(i, j);
if ((Max.second - Min.second + 1) * (Max.first - Min.first + 1) ==
cnt) {
++Tsqr;
} else
++Tcir;
}
cout << Tcir << ' ' << Tsqr << endl;
}
pair<int, int> cache[2012 * 10];
int Tot;
void dfs2(int x, int y) {
if (x == 0 || y == 0 || x > n || y > n) return;
if (vis[x][y] || (a[x][y] == 0)) return;
vis[x][y] = true;
++cnt;
if (x < Min.first) Min.first = x;
if (y < Min.second) Min.second = y;
if (x > Max.first) Max.first = x;
if (y > Max.second) Max.second = y;
dfs2(x - 1, y);
dfs2(x + 1, y);
dfs2(x, y + 1);
dfs2(x, y - 1);
if ((a[x - 1][y] == 0) || (a[x + 1][y] == 0) || (a[x][y + 1] == 0) ||
(a[x][y - 1] == 0))
cache[++Tot] = make_pair(x, y);
}
void Deal2() {
int Tsqr = 0, Tcir = 0;
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
if ((!vis[i][j]) && (a[i][j] == 1)) {
Tot = 0;
Max = make_pair(0, 0), Min = make_pair(n + 1, n + 1);
dfs2(i, j);
pair<double, double> mid =
make_pair(((double)Max.first + Min.first) / 2,
((double)Max.second + Min.second) / 2);
double max = 0, min = 1e100;
for (int i = 1; i <= Tot; ++i) {
pair<int, int> t = cache[i];
double p = sqrt((t.first - mid.first) * (t.first - mid.first) +
(t.second - mid.second) * (t.second - mid.second));
if (p > max) max = p;
if (p < min) min = p;
}
if ((max / min) >= 1.30)
++Tsqr;
else
++Tcir;
}
cout << Tcir << ' ' << Tsqr << endl;
}
int main() {
init();
Deal2();
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, x, y;
cin >> t;
while (t--) {
cin >> x >> y;
if (x % 2 == 0) {
if (y % 4 == 0) {
cout << x << endl;
} else if (y % 4 == 1) {
cout << x - y << endl;
} else if (y % 4 == 2) {
cout << x + 1 << endl;
} else if (y % 4 == 3) {
cout << (x + 1) + y << endl;
}
} else {
if (y % 4 == 0) {
cout << x << endl;
} else if (y % 4 == 1) {
cout << x + y << endl;
} else if (y % 4 == 2) {
cout << x - 1 << endl;
} else if (y % 4 == 3) {
cout << (x - 1) - y << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
class Query {
public:
int type, v1, v2;
} arr[MAXN + 5];
int sz[MAXN + 5];
long long acc[MAXN + 5];
int n, m;
vector<int> vec;
bool addOne(int v) {
if (vec.size() >= MAXN + 5) return false;
vec.push_back(v);
return true;
}
void addSome(int v1, int v2) {
bool khalas = false;
for (int c = 0; c < v2 && !khalas; c++)
for (int c2 = 0; c2 < v1 && !khalas; c2++) {
khalas |= !addOne(vec[c2]);
}
return;
}
int getElement(long long i, int qix) {
if (arr[qix].type == 1) return arr[qix].v1;
return vec[i % arr[qix].v1];
}
int getElement(long long i) {
int s = 0, e = m - 1;
int ret;
while (s <= e) {
int mid = (s + e) >> 1;
if (acc[mid] > i) {
ret = mid;
e = mid - 1;
} else
s = mid + 1;
}
if (ret > 0) i -= acc[ret - 1];
return getElement(i, ret);
}
int main() {
int c, c2;
scanf("%d", &m);
for (c = 0; c < m; c++) {
scanf("%d", &arr[c].type);
if (arr[c].type == 1) {
scanf("%d", &arr[c].v1);
addOne(arr[c].v1);
sz[c] = 1;
} else {
scanf("%d%d", &arr[c].v1, &arr[c].v2);
addSome(arr[c].v1, arr[c].v2);
sz[c] = arr[c].v1 * arr[c].v2;
}
acc[c] = sz[c];
if (c > 0) acc[c] += acc[c - 1];
}
scanf("%d", &n);
for (c = 0; c < n; c++) {
long long i;
cin >> i;
printf("%d ", getElement(i - 1));
}
printf("\n");
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n_Cases = 1;
for (long long case_i = 1; case_i <= n_Cases; case_i++) {
long long n;
cin >> n;
map<long long, long long> count;
for (long long i = 0; i < n; i++) {
long long input;
cin >> input;
count[input]++;
}
long long ans = 0;
for (auto x : count) {
if (x.first < 0) {
auto other = count.find(-1 * x.first);
ans += x.second * (other == count.end() ? 0 : other->second);
}
}
long long zeroPairing = (count[0] * (count[0] - 1)) / 2;
cout << ans + zeroPairing << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
cin >> a;
for (int j = 1; j < a.length(); j++)
if (a[j] >= 'a' && a[j] <= 'z') {
cout << a << endl;
return 0;
}
for (int i = 0; i < a.size(); i++) {
if (a[i] >= 'a' && a[i] <= 'z')
a[i] = 'A' + a[i] - 'a';
else
a[i] = 'a' + a[i] - 'A';
}
cout << a << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int prime[1000002];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
for (int i = 2; i <= 1000000; i++) {
if (prime[i]) continue;
for (int j = i + i; j <= 1000000; j += i) prime[j] = 1;
}
int n;
cin >> n;
if (n == 1)
cout << 1 << endl << 1 << endl;
else if (n == 2)
cout << 1 << endl << 1 << ' ' << 1 << endl;
else {
cout << 2 << endl;
for (int i = 2; i <= n + 1; i++) cout << prime[i] + 1 << ' ';
cout << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, -1};
long long int m, k, n, res, a[5005], posmin[5005], posmax[5005],
dp[5005] = {0}, dpsum[5005][5005], dp1[5005] = {0};
map<long long int, long long int> mp;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int i = 0, x, y, j = 0, z = 0, q, nb1 = 0, nb = 0, nbmax = 0,
nbinf = 0, nb2 = 0, idx = 0, ok = 0, l, negatif = 0,
positif = 0, l1, r;
multiset<long long int>::iterator it, it1, it2;
char c;
long long int ans = 0;
cin >> n;
for (i = 0; i <= 5001; i++) {
posmin[i] = -1;
posmax[i] = -1;
}
for (i = 1; i <= n; i++) {
cin >> a[i];
if (posmin[a[i]] == -1) posmin[a[i]] = i;
}
for (i = n; i >= 1; i--) {
if (posmax[a[i]] == -1) posmax[a[i]] = i;
}
res = 0;
for (i = 1; i <= n; i++) {
if (mp[a[i]] != 365) {
res ^= a[i];
mp[a[i]] = 365;
}
dp1[i] = res;
long long int mm = 6000, mx = -1;
for (j = i; j >= 1; j--) {
mm = min(mm, posmin[a[j]]);
mx = max(mx, posmax[a[j]]);
if (mm >= j && mm <= i && mx >= j && mx <= i) {
dpsum[j][i] = dp1[i] ^ dp1[j - 1];
} else
dpsum[j][i] = -1;
}
dp[i] = 0;
}
dp[0] = 0;
res = 0;
for (i = 1; i <= n; i++) {
for (j = i; j >= 1; j--) {
if (dpsum[j][i] != -1) dp[i] = max(dp[i], dpsum[j][i] + dp[j - 1]);
dp[i] = max(dp[i], dp[i - 1]);
}
res = max(res, dp[i]);
}
cout << res;
return 0;
}
| 11 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long MOD = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
char a[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
char sq = a[0][0], other = a[0][1];
if (sq == other) {
cout << "NO\n";
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i == j && a[i][j] != sq) || (i + j == n - 1 && a[i][j] != sq)) {
cout << "NO\n";
return 0;
}
if (i + j != n - 1) {
if (i != j && a[i][j] != other) {
cout << "NO\n";
return 0;
}
}
}
}
cout << "YES\n";
cerr << "Execution time: " << (float)clock() / CLOCKS_PER_SEC << " secs"
<< endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct __s {
__s() {
if (1) {
ios_base::Init i;
cin.sync_with_stdio(0);
cin.tie(0);
}
}
~__s() {
if (!1)
fprintf(stderr, "Execution time: %.3lf s.\n",
(double)clock() / CLOCKS_PER_SEC);
long long n;
cin >> n;
}
} __S;
string zero = "...";
string mn = ".X.";
string wr = ".X.";
long long x = 0;
void write(char c) {
while (x != c) {
cout << zero << '\n';
cout << mn << '\n';
x = (x + 256 - 1) % 256;
}
cout << wr << '\n';
}
int main(void) {
string s;
cin >> s;
for (long long i = 0; i < (long long)(s.size()); i++) {
write(s[i]);
}
return 0;
}
| 14 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
pair<string, pair<long long, long long> > p[n + 1];
for (long long i = 1; i <= n; i++) {
cin >> p[i].first;
cin >> p[i].second.first >> p[i].second.second;
}
pair<pair<char, char>, pair<char, char> > arr[12];
long long arrC = 0;
for (char ch1 = '0'; ch1 <= '9'; ch1++) {
for (char ch2 = '0'; ch2 <= '9'; ch2++) {
for (char ch3 = '0'; ch3 <= '9'; ch3++) {
for (char ch4 = '0'; ch4 <= '9'; ch4++) {
if (ch1 != ch2 && ch1 != ch3 && ch1 != ch4 && ch2 != ch3 &&
ch2 != ch4 && ch3 != ch4) {
long long counter = 0;
for (long long i = 1; i <= n; i++) {
map<char, long long> mymap1, mymap2;
mymap1[ch1]++;
mymap1[ch2]++;
mymap1[ch3]++;
mymap1[ch4]++;
string s = p[i].first;
for (long long j = 0; j < 4; j++) {
mymap2[s[j]]++;
}
long long ans1 = 0, ans2 = 0;
if (ch1 == s[0]) {
ans1++;
}
if (ch2 == s[1]) {
ans1++;
}
if (ch3 == s[2]) {
ans1++;
}
if (ch4 == s[3]) {
ans1++;
}
for (char ch = '0'; ch <= '9'; ch++) {
if (mymap1[ch] == mymap2[ch] && mymap1[ch] > 0) {
ans2++;
}
}
ans2 = ans2 - ans1;
if (ans1 == p[i].second.first && ans2 == p[i].second.second) {
counter++;
}
}
if (counter == n) {
arr[arrC++] = make_pair(make_pair(ch1, ch2), make_pair(ch3, ch4));
}
if (arrC >= 2) {
cout << "Need more data\n";
return 0;
}
}
}
}
}
}
if (arrC == 0) {
cout << "Incorrect data\n";
} else {
cout << arr[0].first.first << arr[0].first.second << arr[0].second.first
<< arr[0].second.second;
}
}
| 9 |
#include <bits/stdc++.h>
struct point {
int x, y;
} p[10];
int main() {
int i, j, t, f = 1, cnt = 0;
int a[10];
for (i = 0; i < 8; i += 2) {
scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i + 1].x, &p[i + 1].y);
if (p[i].x == p[i + 1].x)
cnt += 1;
else if (p[i].y == p[i + 1].y)
cnt += 10;
else
f = 0;
a[i] = a[i + 1] = 0;
}
if (cnt != 22) f = 0;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
if (p[i].x == p[j].x && p[i].y == p[j].y) a[i]++;
for (i = 0; i < 8; i++)
if (a[i] != 2) f = 0;
puts(f ? "YES" : "NO");
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
int main() {
scanf("%lld%lld", &n, &m);
if (n == 1) {
printf("1 1");
return 0;
}
long long minn = max(0ll, n - 2 * m);
long long maxn = 0, sum = 0, cur = 1;
while (m > 0) {
m -= min(cur, m);
cur++;
}
maxn = n;
if (cur > 1) maxn = n - cur;
printf("%lld %lld", minn, maxn);
return 0;
}
| 5 |
#include <bits/stdc++.h>
double mymin(double a, double b) {
if (a < 0)
return b;
else
return a < b ? a : b;
}
int main() {
double x1, y1, x2, y2;
scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
double x, y;
x = x2 - x1;
y = y2 - y1;
double v, t;
scanf("%lf %lf", &v, &t);
double vx, vy, wx, wy;
scanf("%lf %lf %lf %lf", &vx, &vy, &wx, &wy);
vx = -vx;
vy = -vy;
wx = -wx;
wy = -wy;
double r = -1;
{
double a = v * v - vx * vx - vy * vy;
double b = -2 * (x * vx + y * vy);
double c = -(x * x + y * y);
if (b * b - 4 * a * c >= 0) {
double s1 = (-b + sqrt(b * b - 4 * a * c)) / 2 / a;
if (0 <= s1 && s1 <= t) r = mymin(r, s1);
double s2 = (-b - sqrt(b * b - 4 * a * c)) / 2 / a;
if (0 <= s2 && s2 <= t) r = mymin(r, s2);
}
}
{
double kx = x - wx * t + vx * t;
double ky = y - wy * t + vy * t;
double a = v * v - wx * wx - wy * wy;
double b = -2 * (wx * kx + wy * ky);
double c = -(kx * kx + ky * ky);
if (b * b - 4 * a * c >= 0) {
double s1 = (-b + sqrt(b * b - 4 * a * c)) / 2 / a;
if (t <= s1) r = mymin(r, s1);
double s2 = (-b - sqrt(b * b - 4 * a * c)) / 2 / a;
if (t <= s2) r = mymin(r, s2);
}
}
printf("%.20lf", r);
return 0;
}
| 13 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
vector<pair<int, int> > v;
int i;
int d;
for (i = 0; i < n; i++) {
scanf("%d", &d);
v.push_back(make_pair(i, d));
}
int l = 0;
i = 0;
while (v.size() > 1) {
if (m >= v[i].second) {
v.erase(v.begin() + i);
} else {
v.push_back(make_pair(v[i].first, v[i].second - m));
v.erase(v.begin() + i);
}
}
printf("%d", v[v.size() - 1].first + 1);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool taken[1000003];
int ans[1000003];
int main() {
{
int N;
cin >> N;
memset(taken, false, sizeof taken);
unsigned long long sum = 0;
for (int n = N; n >= 0; n--) {
if (taken[n]) continue;
int M = 0;
for (int nrbits = 0; (1 << nrbits) <= n; M += (1 << nrbits), nrbits++)
;
int pair = M ^ n;
if (pair <= N && !taken[pair]) {
taken[n] = true;
taken[pair] = true;
ans[n] = pair;
ans[pair] = n;
sum += 2 * (pair ^ n);
continue;
}
}
cout << sum << endl;
cout << ans[0];
for (int i = (1); i < (N + 1); i++) cout << ' ' << ans[i];
cout << endl;
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int n;
vector<int> tree[1048576];
int ans[1048576], a[1048576], cnt[1048576];
vector<int> ql[1048576], qk[1048576], qi[1048576];
unordered_set<int, custom_hash> shits[1048576];
int pre[1048576];
void dfs(int i, int depth) {
pre[cnt[a[i]]]--;
shits[cnt[a[i]]].erase(a[i]);
cnt[a[i]]++;
shits[cnt[a[i]]].insert(a[i]);
for (int child : tree[i]) dfs(child, depth + 1);
for (int j = 0; j < ql[i].size(); j++) {
int curL = ql[i][j];
int curK = qk[i][j];
int curI = qi[i][j];
int l = curL - 1, r = depth + 1;
int ask = curK + pre[curL - 1];
if (ask >= n + 1) continue;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (pre[mid] >= ask)
r = mid;
else
l = mid;
}
if (r < depth + 1) ans[curI] = 1 + (*shits[r].begin());
}
shits[cnt[a[i]]].erase(a[i]);
cnt[a[i]]--;
shits[cnt[a[i]]].insert(a[i]);
pre[cnt[a[i]]]++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t-- > 0) {
int q;
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
cnt[i] = 0;
shits[0].insert(a[i]);
}
for (int i = 1; i < n; i++) {
int x;
cin >> x;
tree[x - 1].push_back(i);
}
for (int i = 0; i < q; i++) {
int v, l, k;
cin >> v >> l >> k;
v--;
ql[v].push_back(l);
qk[v].push_back(k);
qi[v].push_back(i);
ans[i] = -1;
}
for (int i = 0; i <= n + 5; i++) pre[i] = n;
dfs(0, 1);
for (int i = 0; i < q; i++) cout << ans[i] << " ";
cout << '\n';
shits[0].clear();
for (int i = 0; i < n; i++) {
tree[i].clear();
ql[i].clear();
qk[i].clear();
qi[i].clear();
}
}
}
| 16 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long b, long long e, long long MOD) {
if (b == 0 && e == 0) return 1;
if (b == 0 && e > 1) return 0;
if (b > 0 && e == 0) return 1;
long long r = 1;
while (e) {
if (e & 1) {
r = r * b;
r %= MOD;
}
e >>= 1;
b = b * b;
b %= MOD;
}
return r;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
const int INF = INT_MAX;
const int MAX = 1000005;
const int MOD = 1e9 + 7;
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
int sz = max(n, m);
int arr[sz + 1][sz + 1];
for (int i = 1; i < sz + 1; ++i)
for (int j = 1; j < sz + 1; ++j)
if (j & 1)
arr[i][j] = arr[j][i] = 1;
else
arr[i][j] = arr[j][i] = 0;
if (arr[n][m])
cout << "Akshat" << endl;
else
cout << "Malvika" << endl;
return 0;
}
| 1 |
Subsets and Splits