solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2 * (int)1e5 + 10;
long long gcd(long long a, long long b) {
while (a > 0 && b > 0) {
if (a < b) swap(a, b);
a %= b;
}
return a + b;
}
int n;
long long x, y;
vector<long long> a, b;
int vala[maxN], valb[maxN];
int maska[1 << 22], maskb[1 << 22];
vector<long long> all;
void upd() {
int k = all.size() - 1;
long long p = all.back();
for (int i = 0; i < a.size(); i++) {
if (a[i] % p == 0) {
vala[i] |= (1 << k);
while (a[i] % p == 0) {
a[i] /= p;
}
}
}
for (int i = 0; i < b.size(); i++) {
if (b[i] % p == 0) {
valb[i] |= (1 << k);
while (b[i] % p == 0) {
b[i] /= p;
}
}
}
}
void factorize(long long p) {
vector<int> cur;
for (int i = 2; i <= (int)1e6 + 3; i++) {
if (1LL * i * i > p) break;
if (p % i == 0) {
all.push_back(i);
upd();
while (p % i == 0) {
p /= i;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> x >> y;
if (y % x != 0) {
cout << 0;
return 0;
}
long long d = y / x;
for (int i = 1; i <= n; i++) {
long long t;
cin >> t;
if (t % x == 0) a.push_back(gcd(d, t / x));
if (y % t == 0) b.push_back(gcd(d, y / t));
}
factorize(d);
long long vals1 = 1;
long long vals2 = 1;
for (int i = 0; i < a.size(); i++) {
if (vals2 != 1) break;
if (a[i] != 1) {
if ((vals1 != 1) && (a[i] != vals1))
vals2 = a[i];
else
vals1 = a[i];
}
}
for (int i = 0; i < b.size(); i++) {
if (vals2 != 1) break;
if (b[i] != 1) {
if ((vals1 != 1) && (b[i] != vals1))
vals2 = b[i];
else
vals1 = b[i];
}
}
if (vals1 != 1) {
if (vals2 == 1) {
all.push_back(vals1);
upd();
} else {
if (vals1 > vals2) swap(vals1, vals2);
if (vals2 % vals1 != 0) {
all.push_back(vals1);
upd();
all.push_back(vals2);
upd();
} else {
all.push_back(vals1);
upd();
if (vals1 != (vals2 / vals1)) {
all.push_back(vals2 / vals1);
upd();
}
}
}
}
int n = all.size();
assert(n <= 22);
for (int i = 0; i < a.size(); i++) maska[vala[i]]++;
for (int i = 0; i < b.size(); i++) maskb[valb[i]]++;
int big = (1 << n) - 1;
for (int i = 0; i < n; i++) {
for (int mask = 0; mask < (1 << n); mask++) {
if (mask & (1 << i)) maskb[mask] += maskb[mask ^ (1 << i)];
}
}
long long ans = 0;
for (int mask = 0; mask < (1 << n); mask++)
ans += 1LL * maska[mask] * maskb[mask ^ big];
cout << ans;
return 0;
}
| 2,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const long long inf = 1e18 + 10;
long long n, m, s1[maxn], s2[maxn];
long long dpequal[maxn], dplarge[maxn], mod = 1e9 + 7;
long long inv(long long a) {
long long mode = 1e9 + 7;
long long b = 1e9 + 5;
long long sum = 1;
while (b) {
if (b & 1) {
sum = (sum * a) % mode;
b--;
}
b /= 2;
a = a * a % mode;
}
return sum;
}
long long euqal_(long long x, long long y) {
if (x == 0 || y == 0)
return inv(m);
else
return x == y;
}
long long large(long long x, long long y) {
if (x == 0 && y == 0)
return (m - 1) * inv(2 * m) % mod;
else if (x == 0)
return (m - y) * inv(m) % mod;
else if (y == 0)
return (x - 1) * inv(m) % mod;
else
return x > y;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) scanf("%lld", &s1[i]);
for (int i = 1; i <= n; i++) scanf("%lld", &s2[i]);
dpequal[0] = 1;
dplarge[0] = 0;
for (int i = 1; i <= n; i++) {
long long l = large(s1[i], s2[i]), e = euqal_(s1[i], s2[i]);
dpequal[i] = dpequal[i - 1] * e % mod;
dplarge[i] = (dpequal[i - 1] * l % mod + dplarge[i - 1] % mod) % mod;
}
cout << dplarge[n] << endl;
}
| 1,900 | CPP |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
# = input()
# = int(input())
(n, k) = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
b = sorted([int(i) for i in input().split()], reverse=-1)
#() = (int(i) for i in input().split())
# = [int(i) for i in input().split()]
start = time.time()
j=0
for i in range(n):
if a[i]==0:
a[i]=b[j]
j+=1
ans = 'No'
for i in range(n-1):
if a[i]>a[i+1]:
ans='Yes'
break
#print(a, b)
print(ans)
finish = time.time()
#print(finish - start)
| 900 | PYTHON3 |
from collections import defaultdict
n = int(input())
v1 = list(map(int, input().split()))
v2 = sorted(v1)
sum_v = sum(v1)
l1, r1 = [0], [sum_v - v1[0]]
l2, r2 = [0], [sum_v - v2[0]]
for i in range(1, n):
l1.append(l1[i-1]+v1[i-1])
l2.append(l2[i-1]+v2[i-1])
r1.append(r1[i-1]-v1[i])
r2.append(r2[i-1]-v2[i])
m = int(input())
for _ in range(m):
t, l, r = map(int, input().split())
if t == 1:
print(sum_v - l1[l-1] - r1[r-1])
else:
print(sum_v - l2[l-1] - r2[r-1]) | 1,200 | PYTHON3 |
p = int(input())
s = input()
b = False;
ans = ''
for j in range(0,p,2):
if j+1 >= p:
b = True
break
ans += s[j]+s[j+1]+'-'
if b:
ans = ans[:len(ans)-1]+s[-1]
else:
ans = ans[:len(ans)-1]
print(ans) | 1,100 | PYTHON3 |
import sys
input = sys.stdin.readline
from math import *
t=int(input())
while t>0:
t-=1
n=int(input())
a=[int(x) for x in input().split()]
count={}
for i in a:
count[i]=0
for i in a:
count[i]+=1
p=[]
for i in count:
p.append(count[i])
print(max(min(len(set(a))-1,max(p)),min(len(set(a)),max(p)-1))) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int mark[123456];
int main() {
int n, t;
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
scanf("%d", &t);
mark[t]++;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!mark[i]) {
ans = i;
break;
}
}
cout << ans << "\n";
}
| 800 | CPP |
n = int(input())
keyboards = map(int, input().split())
keyboards = sorted(keyboards)
#print(keyboards)
last = keyboards[0]
res = 0
for i in keyboards[1:]:
if last + 1 != i:
res += i - last - 1
last = i
print(res)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int arr[400009], go[400009], last[400009];
set<pair<int, int> > t;
set<int> s;
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", arr + i), last[arr[i]] = 1000000007;
for (int i = n; i >= 1; i--) {
go[i] = last[arr[i]];
last[arr[i]] = i;
}
int ans = 0;
memset(last, 0, sizeof last);
for (int i = 1; i <= n; i++) {
if (s.count(arr[i])) {
if (last[arr[i]]) t.erase(make_pair(last[arr[i]], arr[i]));
t.insert(make_pair(go[i], arr[i]));
last[arr[i]] = go[i];
continue;
}
if ((int)s.size() == k) {
pair<int, int> res = *t.rbegin();
s.erase(res.second);
t.erase(res);
}
s.insert(arr[i]);
t.insert(make_pair(go[i], arr[i]));
ans++;
}
printf("%d\n", ans);
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
long long n, m, i, j, k, t, t1, u, v, a, b, cur, sum;
long long diffcnt;
long long arr[200001];
long long fac[200001];
long long inv[200001];
long long bow(long long a, long long x) {
if (x == 0) return 1;
long long res = bow(a, x / 2);
res *= res;
res %= MOD;
if (x % 2) res *= a;
return res % MOD;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (i = 0; i < n; i++) cin >> arr[i];
for (i = 0; i < n; i++)
if (arr[i] != arr[(i + 1) % n]) diffcnt++;
cur = bow(m, n - diffcnt);
fac[0] = 1;
inv[0] = 1;
for (i = 1; i <= n; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= MOD;
inv[i] = bow(fac[i], MOD - 2);
}
sum = 0;
if (n - 2 >= 0) {
for (i = 0; i < diffcnt; i++) {
u = bow(m - 2, i);
v = diffcnt - i;
a = bow(2, v);
if (v % 2 == 0) {
a -= fac[v] * inv[v / 2] % MOD * inv[v / 2] % MOD % MOD;
a += MOD;
a %= MOD;
}
a *= bow(2, MOD - 2);
a %= MOD;
a *= u;
a %= MOD;
a *= fac[diffcnt] * inv[i] % MOD * inv[v] % MOD;
a %= MOD;
sum += a;
sum %= MOD;
}
}
cout << (cur * sum) % MOD;
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
int a[200005];
bool aa[200005];
long long sum[200005];
int main() {
int n, k;
scanf("%d %d", &n, &k);
int nn = n;
memset(aa, 0, sizeof(aa));
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
bool flag = false;
int ans;
int ri;
int cnt;
long long ssum;
cnt = 2;
ssum = 0;
for (int i = 2; i <= n; i++) {
sum[i] =
ssum - (long long)(nn - cnt) * (long long)a[i] * (long long)(cnt - 1);
if (sum[i] < k) {
nn--;
printf("%d\n", i);
} else {
ssum += (long long)(cnt - 1) * (long long)a[i];
cnt++;
}
}
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
long long po(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
bool sec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
if (a.second == b.second) return a.first > b.first;
return a.second > b.second;
}
long long find(long long x, vector<vector<pair<long long, long long> > > ne,
long long i) {
if (ne[i].size() == 0) return -1;
long long l = 0, h = ne[i].size() - 1, m;
while (l <= h) {
m = (l + h) / 2;
if (ne[i][m].first < x)
l = m + 1;
else if (ne[i][m].first > x)
h = m - 1;
else
return m;
}
return -1;
}
void dij(long long second, vector<vector<pair<long long, long long> > > &v,
vector<long long> &vis, long long dis[],
vector<set<pair<long long, long long> > > se) {
set<pair<long long, long long> >::iterator it;
dis[1] = 0;
priority_queue<pair<long long, long long>,
vector<pair<long long, long long> >,
greater<pair<long long, long long> > >
p;
p.push(make_pair(0, 1));
long long i, x, w, wi, e, in;
while (!p.empty()) {
x = p.top().second;
p.pop();
if (vis[x]) continue;
vis[x] = 1;
it = se[x].lower_bound(make_pair(dis[x], -LONG_LONG_MAX));
if (it == se[x].end() || it->first != dis[x])
w = 0;
else
w = it->second;
for (i = 0; i < v[x].size(); i++) {
e = v[x][i].first, wi = v[x][i].second;
if (dis[x] + wi + w < dis[e]) {
dis[e] = dis[x] + w + wi;
p.push(make_pair(dis[e], e));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
srand(time(NULL));
;
long long n, m, i, x, y, w, j;
cin >> n >> m;
vector<vector<pair<long long, long long> > > v(n + 1), ne(n + 1);
vector<set<pair<long long, long long> > > se(n + 1);
vector<long long> vis(n + 1);
long long dis[n + 1];
fill_n(dis, n + 1, LONG_LONG_MAX);
for (i = 0; i < m; i++) {
cin >> x >> y >> w;
v[x].push_back(make_pair(y, w));
v[y].push_back(make_pair(x, w));
}
for (i = 1; i < n + 1; i++) {
cin >> x;
for (j = 0; j < x; j++) {
cin >> y;
ne[i].push_back(make_pair(y, 0));
}
for (j = x - 1; j > -1; j--) {
if (j == x - 1)
ne[i][j].second = 1;
else {
if (ne[i][j].first + 1 == ne[i][j + 1].first)
ne[i][j].second = 1 + ne[i][j + 1].second;
else
ne[i][j].second = 1;
}
}
se[i].insert(ne[i].begin(), ne[i].end());
}
dij(1, v, vis, dis, se);
vis[n] ? cout << dis[n] : cout << "-1";
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long d, m;
long long running = 1;
long long accum = 0;
long long x = 0;
long long diff;
scanf("%lld %lld\n", &d, &m);
x = 1;
while ((x << 1) <= d) {
running += (accum % m) * ((x - 1) % m);
running %= m;
accum = (running + 1) % m;
running = ((2 * running) + 1) % m;
x <<= 1;
}
running %= m;
accum %= m;
diff = (d - x) % m;
running += (accum * diff) % m;
running %= m;
printf("%lld\n", running);
}
int main() {
int T;
scanf("%d\n", &T);
while (T--) {
solve();
}
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
struct node {
int l, r;
int s, e;
} a[1010];
using namespace std;
int main(void) {
int t = 1;
scanf("%d", &t);
while (t--) {
memset((a), 0, sizeof((a)));
;
int n;
scanf("%d", &n);
int mx = 0, mn = INT_MAX;
for (int i = 0; i < (int)(n); i++) {
scanf("%d%d", &a[i].l, &a[i].r);
(mx) = max((mx), (a[i].r));
;
(mn) = min((mn), (a[i].l));
}
int l = 0, r = 0;
queue<node> q;
for (int i = (int)(mn); i <= (int)(mx); i++) {
while (a[l].l == i) {
a[l].s = i;
q.push(a[l]);
l++;
}
while (!q.empty() && a[r].r < i) {
a[r].e = 0;
q.pop();
r++;
}
if (!q.empty()) {
a[r].e = i;
r++;
q.pop();
}
}
for (int i = 0; i < (int)(n); i++) {
printf("%d ", a[i].e);
}
printf("\n");
}
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int logy(long long int x, long long int y) {
return ceil((long double)(log(x) / log(y)));
}
long long int powe(long long int a, long long int b) {
long long int re = 1;
while (b) {
if (b & 1) re = (re * a);
a = (a * a);
b = b >> 1;
}
return re;
}
bool flag = 0, flag1 = 0, flag2 = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int tt = 1;
while (tt--) {
long long int n, d, h, cnt = 2, v = 1;
cin >> n >> d >> h;
if (2 * h < d || (n > 2 && d == 1)) {
cout << -1;
return 0;
}
for (long long int i = 0; i < h; i++)
cout << cnt - 1 << " " << cnt << "\n", cnt++, v++;
d -= h;
if (d) cout << "1 " << cnt << "\n", cnt++, d--, v++;
for (long long int i = 0; i < d; i++)
cout << cnt - 1 << " " << cnt << "\n", cnt++, v++;
if (h != 1)
for (long long int i = v; i < n; i++) cout << "2 " << cnt << "\n", cnt++;
else
for (long long int i = v; i < n; i++) cout << "1 " << cnt << "\n", cnt++;
}
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, unsigned 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 n, m, k;
int32_t main() {
long long int x;
cin >> x;
if (x == 1) {
cout << -1;
return 0;
}
if (x == 2) {
cout << 2 << " " << 2;
return 0;
}
if (x % 2 == 0) {
cout << x << " " << 2 << endl;
} else {
cout << (x - 1) << " " << 2 << endl;
}
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long dv = 998244353;
long long n, m, d, nn = 0, dsu[300069], cc[300069], ce[300069], dg[2][300069],
cdg[2][300069], fq[300069], dp[300069], ex[300069];
map<long long, long long> ve[300069];
long long fd(long long x) {
if (dsu[x] != x) {
dsu[x] = fd(dsu[x]);
}
return dsu[x];
}
int main() {
long long i, j, ii, k, l, sz;
scanf("%lld%lld%lld", &n, &m, &d);
for (i = 1; i <= d; i++) {
dsu[i] = i;
cc[i] = 1;
}
for (i = 0; i < n; i++) {
scanf("%lld", &sz);
for (j = 0; j < sz; j++) {
scanf("%lld", &k);
if (j && !ve[l][k]) {
ve[l][k] = 1;
if (fd(k) != fd(l)) {
cc[fd(l)] += cc[fd(k)];
ce[fd(l)] += ce[fd(k)];
for (ii = 0; ii < 2; ii++) {
cdg[ii][fd(l)] += cdg[ii][fd(k)];
}
dsu[fd(k)] = l;
}
ce[fd(k)]++;
for (ii = 0; ii < 2; ii++) {
cdg[ii][fd(k)] -= dg[ii][k] == 1;
dg[ii][k]++;
cdg[ii][fd(k)] += dg[ii][k] == 1;
swap(k, l);
}
}
l = k;
}
}
for (i = 1; i <= d; i++) {
if (fd(i) == i && ce[i] == cc[i] - 1 && cdg[0][i] == cc[i] - 1 &&
cdg[1][i] == cc[i] - 1) {
fq[cc[i]]++;
}
}
for (i = 1; i <= d; i++) {
if (fq[i]) {
nn++;
ex[nn] = i;
}
}
dp[0] = 1;
for (i = 1; i <= m; i++) {
for (j = 1; j <= nn; j++) {
if (i >= ex[j]) {
dp[i] = (dp[i] + dp[i - ex[j]] * fq[ex[j]]) % dv;
}
}
}
printf("%lld\n", dp[m]);
}
| 2,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1 << 20;
const long long MOD = 1000000007;
long long n, x, y;
long long dp[MAXN];
long long ans;
void read() {
int a;
scanf("%I64d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (a == 1)
x++;
else
y++;
}
}
void solve() {
dp[1] = 1;
dp[2] = 2;
dp[3] = 4;
for (long long i = 4; i <= n; i++)
dp[i] = (dp[i - 1] + ((i - 1) * dp[i - 2]) % MOD) % MOD;
if (!x) {
ans = 1;
for (long long i = 1; i <= n; i++) ans = (ans * i) % MOD;
cout << ans << endl;
return;
}
if (!y) {
cout << dp[x] << endl;
return;
}
ans = dp[x];
x++;
for (int i = 1; i <= y; i++) {
ans = (ans * x) % MOD;
x++;
}
cout << ans << endl;
}
int main() {
read();
solve();
return 0;
}
| 2,400 | CPP |
t=int(input())
for i in range(t):
x,y=map(int,input().split())
y=((y-x)%y)
print(y)
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int adia[100010];
int sum[100010];
bool viz[100010];
bool ishead[100010];
bool ok = 1;
int rucsac[100010];
void dfs(int nod) {
if (viz[nod]) {
ok = 0;
return;
}
viz[nod] = 1;
if (adia[nod]) dfs(adia[nod]);
sum[nod] += sum[adia[nod]];
if (sum[nod] > 1e9) sum[nod] = 1e9;
}
int main() {
int n, q, t, a, b;
cin >> n >> q >> t;
for (int i(1); i <= n; i++) {
cin >> sum[i];
ishead[i] = 1;
}
while (q--) {
cin >> b >> a;
adia[a] = b;
ishead[b] = 0;
}
for (int i(1); i <= n; i++)
if (ishead[i]) dfs(i);
for (int i(1); i <= n; i++) {
if (!viz[i] || !ok || t < 0) {
cout << 0;
return 0;
}
t -= sum[adia[i]];
}
if (t < 0) {
cout << 0;
return 0;
}
rucsac[0] = 1;
for (int i(1); i <= n; i++) {
for (int j(0); j + sum[i] <= t; j++)
rucsac[j + sum[i]] = (rucsac[j + sum[i]] + rucsac[j]) % 1000000007;
}
cout << rucsac[t] << '\n';
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void max_swap(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void min_swap(T& a, const T& b) {
a = min(a, b);
}
template <class T>
void uniq(vector<T>& c) {
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
}
template <class T>
string to_s(const T& a) {
ostringstream os;
os << a;
return os.str();
}
template <class T>
T to_T(const string& s) {
istringstream is(s);
T res;
is >> res;
return res;
}
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
os << "( " << p.first << ", " << p.second << " )";
return os;
}
template <class T>
void print(T a, int n, const string& deli = " ", int br = 1) {
for (int i = 0; i < n; ++i) {
cout << a[i];
if (i + 1 != n) cout << deli;
}
while (br--) cout << endl;
}
template <class T>
void print(const T& c, const string& deli = " ", int br = 1) {
for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) {
cout << *it;
if (++it != c.end()) cout << deli;
--it;
}
while (br--) cout << endl;
}
template <class T>
void print2d(T a, int w, int h, int width = -1, int br = 1) {
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (width != -1) cout.width(width);
cout << a[i][j] << ' ';
}
cout << endl;
}
while (br--) cout << endl;
}
template <class T>
void input(T& a, int n) {
for (int i = 0; i < n; ++i) cin >> a[i];
}
template <class T>
void input(T* a, int n) {
for (int i = 0; i < n; ++i) cin >> a[i];
}
void fast_io() {
cin.tie(0);
ios::sync_with_stdio(false);
}
bool valid(int x, int y, int w, int h) {
return 0 <= x && x < w && 0 <= y && y < h;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const double PI = acos(-1.0);
const int mod = ((long long)(1e9)) + 7;
const double eps = 1e-9;
int main() {
double y1, y2, yw, xb, yb, r;
while (cin >> y1 >> y2 >> yw >> xb >> yb >> r) {
double yr = yw - r;
double my1 = yr + (yr - y1);
double my2 = yr + (yr - y2);
double aim_y = my1 - r;
double inc_y = (aim_y - yb) / xb;
double xw = (aim_y - yr) / inc_y;
double a = PI / 2 - atan2(aim_y - yb, xb);
double d = r / sin(a);
if (aim_y - d < my2)
puts("-1");
else
printf("%.10f\n", xw);
}
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
vector<int> v1;
for (int i = 0; i < (4); i++) {
int d;
cin >> d;
v1.push_back(d);
}
if (max(v1[0], v1[1]) == max(v1[2], v1[3]) &&
min(v1[0], v1[1]) + min(v1[2], v1[3]) == max(v1[0], v1[1]))
cout << "YES"
<< "\n";
else
cout << "NO"
<< "\n";
}
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, r = 0, f;
cin >> n;
while (n > 1) {
r += n;
f = 0;
for (int i = 2; i * i <= n; ++i)
if (n % i == 0) {
n /= i;
f = 1;
break;
}
if (!f) n = 1;
}
cout << r + 1;
return 0;
}
| 1,200 | CPP |
import sys
#file_in = open("input.txt")
#file_out = open("output.txt", "w")
nextInt = lambda: map(int, input().split())
n, p = nextInt()
a = [0]*n
ret = 0
for i in range(n):
l, r = nextInt()
a[i] = (r//p-(l-1)//p)/(r-l+1)
for i in range(n):
ret += a[i]+a[i-1]-a[i]*a[i-1]
ret *= 2000
print(ret) | 1,700 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define li long int
#define lli long long int
#define ld long double
void solve(){
li n;
cin>>n;
string s;
cin>>s;
if(s[n-4]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
cout<<"YES\n";
return;
}
if(s[0]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
cout<<"YES\n";
return;
}
if(s[0]=='2'&&s[1]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){
cout<<"YES\n";
return;
}
if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[n-1]=='0'){
cout<<"YES\n";
return;
}
if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[3]=='0'){
cout<<"YES\n";
return;
}
else cout<<"NO\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
li t;
cin>>t;
while(t--){
solve();
}
return 0;
} | 800 | CPP |
def getAdjacencyDict(str1, str2):
adjacencyDict = {}
for i in range(len(str1)):
if str1[i] == str2[i]:
continue
if str1[i] in adjacencyDict:
adjacencyDict[str1[i]].append(str2[i])
else:
adjacencyDict[str1[i]] = [str2[i]]
if str2[i] in adjacencyDict:
adjacencyDict[str2[i]].append(str1[i])
else:
adjacencyDict[str2[i]] = [str1[i]]
return adjacencyDict
def dfs(adjacencyDict, vertexes, prev, currVertex, visitedVertex, spells):
visitedVertex.append(currVertex)
vertexes.remove(currVertex)
for neighbour in adjacencyDict[currVertex]:
if neighbour != prev and neighbour not in visitedVertex:
spells.append((currVertex, neighbour))
dfs(adjacencyDict,vertexes, currVertex, neighbour, visitedVertex, spells)
return spells
def spellsRequired(str1, str2):
adjacencyDict = getAdjacencyDict(str1, str2)
vertexes = list(adjacencyDict)
spells = []
visitedVertex = []
while (len(vertexes) != 0):
dfs(adjacencyDict,vertexes, None, vertexes[0], visitedVertex, spells)
return spells
def main():
lenOfStr = int(input())
str1 = input()
str2 = input()
spells = spellsRequired(str1, str2)
print(len(spells))
for i, j in spells:
print(i, j)
if __name__ == '__main__':
main()
| 1,600 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100001;
int n, a[NMAX];
map<int, vector<int> > myMap;
int Min[18][NMAX], Exp[NMAX];
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
void preprocesare() {
for (int i = 1; i <= n; ++i) Min[0][i] = a[i];
for (int i = 1; (1 << i) <= n; ++i) {
for (int j = 1; j <= n - (1 << i) + 1; ++j) {
int unde = j + (1 << i) - 1;
int tata = unde - (1 << (i - 1)) + 1;
Min[i][j] = gcd(Min[i - 1][j], Min[i - 1][tata]);
}
}
Exp[1] = 0;
for (int i = 2; i <= n; ++i) {
Exp[i] = Exp[i / 2] + 1;
}
}
int getGcd(int x, int y) {
int lung = y - x + 1;
int k = Exp[lung];
int rez = Min[k][x];
int new_X = y - (1 << k) + 1;
rez = gcd(rez, Min[k][new_X]);
return rez;
}
void citeste() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
}
int freq(int val, int x, int y) {
int st = -1;
int dr = myMap[val].size();
while (dr - st > 1) {
int mij = (st + dr) / 2;
if (myMap[val][mij] >= x) {
dr = mij;
} else
st = mij;
}
if (dr == myMap[val].size()) return 0;
int dr2 = dr;
st = -1;
dr = myMap[val].size();
while (dr - st > 1) {
int mij = (st + dr) / 2;
if (myMap[val][mij] <= y) {
st = mij;
} else
dr = mij;
}
if (st == -1) return 0;
return st - dr2 + 1;
}
void rezolva() {
int t;
cin >> t;
for (int i = 1; i <= n; ++i) {
myMap[a[i]].push_back(i);
}
for (int i = 1; i <= t; ++i) {
int x, y;
cin >> x >> y;
int c = getGcd(x, y);
cout << (y - x + 1 - freq(c, x, y)) << "\n";
}
}
int main() {
citeste();
preprocesare();
rezolva();
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "[";
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i];
if (i != (int)v.size() - 1) os << ", ";
}
os << "]\n";
return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "[";
os << p.first;
os << ", ";
os << p.second;
os << "]";
return os;
}
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
long long binpow(long long a, long long b, long long p) {
if (b == 0) return 1;
long long t = binpow(a, b / 2, p);
if (b % 2)
return (((a * t) % p) * t) % p;
else
return ((t * t) % p);
}
void solve() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
if (n % 2 == 0) {
cout << "Yes\n";
return;
}
for (int i = 1; i < n; i++) {
if (arr[i] <= arr[i - 1]) {
cout << "Yes\n";
return;
}
}
cout << "No\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int c, x, ans;
string s;
cin >> s;
c = s.rfind('1');
x = s.size();
ans = count(s.begin(), s.end(), '1');
cout << x + c - (ans = (ans > 1) ? ans - 2 : ans) << endl;
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
x = (x << 1) + (x << 3) + ch - '0';
return x * f;
}
const long long N = 1e6 + 5;
long long a[N], sum[N], ans[N];
pair<pair<long long, long long>, long long> b[N];
signed main() {
long long n = read();
for (long long i = 1; i <= n; ++i) a[i] = read();
long long m = read();
for (long long i = 1; i <= m; ++i) {
b[i].first.second = read();
b[i].first.first = read();
b[i].second = i;
}
sort(b + 1, b + m + 1);
for (long long i = 1, j = 1; i <= m; i = j) {
if (b[i].first.first <= 200) {
while (j <= m && b[i].first.first == b[j].first.first) ++j;
memset(sum, 0, sizeof(sum));
for (long long k = n; k >= 1; --k)
sum[k] = sum[k + b[i].first.first] + a[k];
for (long long k = i; k < j; ++k)
ans[b[k].second] = sum[b[k].first.second];
} else {
++j;
long long temp = 0;
for (long long k = b[i].first.second; k <= n; k += b[i].first.first)
temp += a[k];
ans[b[i].second] = temp;
}
}
for (long long i = 1; i <= m; ++i) printf("%lld\n", ans[i]);
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int analizar(string x) {
map<char, int> c;
for (int a = 0; a < x.size(); a++) {
c[x[a]]++;
}
int b = 0;
map<char, int>::iterator it = c.begin();
for (; it != c.end(); it++) {
b = max(b, it->second);
}
return x.size() - b;
}
void caso() {
int n, k, res = 0;
string s;
cin >> n >> k;
cin >> s;
string l[k];
for (int a = 0; a < k; a++) {
for (int b = a; b < n; b += k) {
l[a] += s[b];
}
}
for (int a = 0; a < k / 2; a++) {
l[a] += l[k - a - 1];
res += analizar(l[a]);
}
if (k % 2 == 1) {
res += analizar(l[k / 2]);
}
cout << res << endl;
}
int main() {
int t;
cin >> t;
while (t--) {
caso();
}
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
char a[6][26];
a[0][0] = '+';
a[5][0] = '+';
for (int i = 1; i < 26; i++) {
a[0][i] = '-';
a[5][i] = '-';
}
a[0][25] = '+';
a[5][25] = '+';
for (int i = 1; i < 5; i++) {
a[i][0] = '|';
a[i][25] = '|';
}
a[1][24] = 'D';
a[2][24] = '.';
a[3][24] = '.';
a[4][24] = '.';
for (int i = 1; i <= 4; i++) {
for (int j = 2; j <= 22; j = j + 2) a[i][j] = '.';
}
a[1][23] = '|';
a[2][23] = '|';
a[3][23] = '.';
a[4][23] = '|';
for (int i = 2; i <= 23; i++) a[3][i] = '.';
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 21; j = j + 2) a[i][j] = '#';
}
for (int i = 2; i <= 24; i++) a[3][i] = '.';
int n;
cin >> n;
int x = 4;
if (n <= 4) x = n;
for (int i = 1; i <= x; i++) a[i][1] = 'O';
if (n > 4) {
n = n - 4;
int w = n / 3;
int l = 3 + (w - 1) * 2;
for (int i = 3; i <= l; i = i + 2) {
a[1][i] = 'O';
a[2][i] = 'O';
a[4][i] = 'O';
}
int rem = n % 3;
if (rem == 1) a[1][l + 2] = 'O';
if (rem == 2) {
a[1][l + 2] = 'O';
a[2][l + 2] = 'O';
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 26; j++) {
cout << a[i][j];
}
if (i == 1 || i == 4) cout << ")";
cout << endl;
}
return 0;
}
| 1,100 | CPP |
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(0)
else:
sa = []
s = 0
e = 0
for i in range(n-1):
if a[i] == a[i+1]-1:
e = i+1
else:
sa.append(a[s:e+1])
s = i+1
e = i+1
else:
sa.append(a[s:e+1])
j = 0
m = 0
for i in range(len(sa)):
if len(sa[i]) > m:
m = len(sa[i])
j = i
elif len(sa[i]) == m:
if sa[i][m-1] == 1000:
j = i
if sa[j][0] == 1 or sa[j][m-1] == 1000:
print(m-1)
elif m < 3:
print(0)
else:
print(m-2)
| 1,300 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main() {
int n;
scanf("%d", &n);
if (n == 5) {
puts("1 2 6");
puts("1 3 6");
puts("2 4 5");
puts("4 5 1");
;
puts("3 4");
puts("3 5");
return 0;
}
for (int i = 1; i <= n / 2; i++) printf("%d %d %d\n", i, i + n / 2, 1);
for (int i = 1; i + n / 2 < n; i++)
printf("%d %d %d\n", n / 2 + i, n / 2 + i + 1, 2 * i - 1);
for (int i = 1; i < n / 2; i++) printf("%d %d\n", i, i + 1);
puts("1 3");
return 0;
}
| 0 | CPP |
n, s = list(map(int, input().split()))
ans = 0
for i in reversed(range(1, n + 1)):
mx = s // i
s -= mx * i
ans += mx
print(ans) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
long long s[100100];
int main() {
long long n, m;
while (~scanf("%lld%lld", &n, &m)) {
long long i = 0, sum = 0;
s[i++] = m;
while (m > n) {
if (m % 2 == 0) {
m /= 2;
s[i++] = m;
} else {
m--;
if (m % 10 != 0) {
break;
} else {
m /= 10;
s[i++] = m;
}
}
}
if (m != n) {
printf("NO\n");
continue;
}
printf("YES\n");
printf("%d\n", i);
for (int j = i - 1; j >= 0; j--) {
if (j != 0)
printf("%d ", s[j]);
else
printf("%d\n", s[j]);
}
}
return 0;
}
| 1,000 | CPP |
t=int(input())
while(t!=0):
n=int(input())
if(n%4!=0):
print("NO")
else:
print("YES")
l=[]
x=2
while(x<=n):
l.append(x)
x+=2
i=-1
l1=l[:]
x=1
while(x<=n):
l.append(x)
x+=2
n1=n//2
l=l[0:len(l)-1]
l1=l[0:n1]
l2=l[n1:]
ans=sum(l1)-sum(l2)
l.append(ans)
for i in l:
print(i,end=" ")
print()
t-=1 | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 200100;
const long long mod = 1e9 + 7;
long long fac[N];
long long dp[2020];
long long h, w, n;
struct node {
long long x, y;
bool operator<(const node &rhs) const {
if (rhs.x == x) return y < rhs.y;
return x < rhs.x;
}
} a[2020];
long long exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long gcd = exgcd(b, a % b, x, y);
long long t = x;
x = y;
y = t - (a / b) * x;
return gcd;
}
long long inverse(long long num) {
long long x, y;
exgcd(num, mod, x, y);
return (x % mod + mod) % mod;
}
long long C(int a, int b) {
long long t1 = fac[a];
long long t2 = (long long)fac[a - b] * fac[b] % mod;
long long inv = inverse(t2);
return t1 * inv % mod;
}
void fac_init() {
fac[0] = 1;
for (int i = 1; i <= N - 90; ++i) fac[i] = fac[i - 1] * i % mod;
}
int main() {
fac_init();
while (~scanf("%lld%lld%lld", &h, &w, &n)) {
for (int i = 1; i <= n; ++i) {
scanf("%lld%lld", &a[i].x, &a[i].y);
}
sort(a + 1, a + 1 + n);
a[++n].x = h;
a[n].y = w;
for (int i = 1; i <= n; ++i) {
long long x1 = a[i].x, y1 = a[i].y;
dp[i] = C(x1 + y1 - 2, x1 - 1);
for (int j = 1; j < i; ++j) {
long long x2 = a[j].x, y2 = a[j].y;
if (x2 <= x1 && y2 <= y1) {
dp[i] -= (dp[j] * C(x1 + y1 - x2 - y2, x1 - x2)) % mod;
dp[i] = (dp[i] % mod + mod) % mod;
}
}
}
printf("%lld\n", dp[n]);
}
return 0;
}
| 2,200 | CPP |
from collections import Counter
n, k = list(map(int, input().rstrip().split()))
s = input()
li = [0] * 26
for x in s:
li[ord(x) - 65] += 1
li.sort(reverse=True)
total = 0
i = 0
while k > 0:
minm = min(li[i], k)
total += minm * minm
k -= minm
i += 1
print(total)
| 1,300 | PYTHON3 |
for _ in range(int(input())):
s = input()
R = s.count('R')
S = s.count('S')
P = s.count('P')
if R>=S and R>=P:
print('P'*len(s))
elif S>=P:
print('R'*len(s))
else:
print('S'*len(s))
| 1,400 | PYTHON3 |
t,s,x=map(int,input().split())
if x<s+t:
if x==t:
print('YES')
else:
print('NO')
else:
if (x-t)%s<=1:
print('YES')
else:
print('NO') | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000000;
vector<string> ans[N];
string cur[N];
int main() {
ios::sync_with_stdio(false);
string str;
getline(cin, str);
str += ",";
string token = "";
bool ch = 0;
vector<int> st;
int deep = 0;
for (char c : str) {
if (c == ',') {
if (ch) {
int x = atoi(token.c_str());
if (st.size()) st[st.size() - 1]--;
st.push_back(x);
deep = max(deep, int(st.size()));
while (st.size() && st.back() == 0) {
st.pop_back();
ans[st.size()].push_back(cur[st.size()]);
}
} else {
cur[st.size()] = token;
}
token = "";
ch ^= 1;
} else {
token += c;
}
}
cout << deep << '\n';
for (int i = 0; i < deep; i++) {
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << " \n"[j == ans[i].size() - 1];
}
}
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
cin >> s;
s += s;
int ans = 0, len = s.size(), t = 0;
for (int i = 1; i < len; i++) {
if (s[i] == s[i - 1])
t = 1;
else
t++;
ans = max(ans, t);
}
cout << min(ans, len / 2) << endl;
}
| 1,600 | CPP |
n=int(input())
c=0
for i in range(n):
c=0
a,b=map(int,input().split())
if a%b==0:
print(0)
else:
print(b-a%b) | 1,500 | PYTHON3 |
s = input()
lower = 0
n = len(s)
for c in s:
if c.islower():
lower += 1
if lower < n / 2:
s = s.upper()
else:
s = s.lower()
print(s) | 800 | PYTHON3 |
a = []
for i in range(1,251):
a.append(4*i)
a.append(7*i)
for i in range(1,24):
a.append(44*i)
a.append(47*i)
a.append(74*i)
a.append(77*i)
for i in range(1,4):
a.append(444*i)
a.append(447*i)
a.append(474*i)
a.append(477*i)
a.append(777*i)
a.append(774*i)
a.append(747*i)
a.append(744*i)
if int(input()) in a:
print('YES')
else:
print('NO') | 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int N;
vector<string> a;
vector<int> big;
vector<int> mark;
string s;
string t;
int main() {
cin >> N;
string s1;
for (int i = 0; i < N; i++) {
cin >> s1;
a.push_back(s1);
}
cin >> s;
cin >> t;
for (int i = 0; i < s.size(); i++) {
string s1 = s.substr(i, 1);
if (s1[0] <= 'Z') {
big.push_back(1);
} else {
big.push_back(0);
}
}
for (int i = 0; i < s.length(); i++) mark.push_back(0);
transform(s.begin(), s.end(), s.begin(), ::tolower);
for (int i = 0; i < N; i++)
transform(a[i].begin(), a[i].end(), a[i].begin(), ::tolower);
for (int i = 0; i < N; i++) {
int start = 0;
while (s.substr(start).find(a[i]) != -1) {
start += s.substr(start).find(a[i]) + 1;
for (int j = start - 1; j <= start + a[i].length() - 2; j++) mark[j] = 1;
}
}
string answ;
for (int i = 0; i < s.length(); i++) {
if (mark[i] == 0)
answ.append(s.substr(i, 1));
else {
if (s.substr(i, 1) == t) {
if (t == "a")
answ.append("b");
else
answ.append("a");
} else {
answ.append(t);
}
}
}
for (int i = 0; i < answ.length(); i++) {
if (big[i]) {
string s1 = answ.substr(i, 1);
transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
string s2 = answ.substr(0, i);
s2.append(s1);
if (i + 1 < answ.length()) s2.append(answ.substr(i + 1));
answ = s2;
}
}
cout << answ << endl;
return 0;
}
| 1,600 | CPP |
n=int(input())
if n>=0:print(n)
else:
s=list(str(n))
if int(s[-1])>int(s[-2]):s.pop(-1)
else:s.pop(-2)
print(int(''.join(s))) | 900 | PYTHON3 |
n=int(input())
UL=0
for i in range(1,n+1):
p,q=map(int,input().split())
if q-2>=p:
UL+=1
print(UL)
| 800 | PYTHON3 |
n=int(input())
a=list(map(int,input().split()))
m=int(input())
b=list(map(int,input().split()))
a.sort(reverse=True)
c=0
for i in range(n):
c+=a[i]
#print(c,a)
for i in b:
print(c-a[i-1]) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5;
int used[N];
vector<int> gr[N];
vector<int> vs;
void topSort(int v) {
used[v] = 1;
for (int to : gr[v]) {
if (used[to] == 1) {
cout << -1;
exit(0);
} else if (!used[to])
topSort(to);
}
vs.push_back(v);
used[v] = 2;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> need(k);
for (int i = 0; i < k; ++i) cin >> need[i];
for (int i = 1; i <= n; ++i) {
int cnt;
cin >> cnt;
gr[i].resize(cnt);
for (int j = 0; j < cnt; ++j) cin >> gr[i][j];
}
for (int x : need)
if (!used[x]) topSort(x);
cout << vs.size() << '\n';
for (int v : vs) cout << v << ' ';
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
void CF() {
int t;
cin >> t;
while (t--) {
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, a, b;
cin >> n >> a >> b;
int Aa[a], Bb[b];
for (int i = 0; i < (a); ++i) cin >> Aa[i];
for (int i = 0; i < (b); ++i) cin >> Bb[i];
int Nn[n];
for (int i = 0; i < (n); ++i) Nn[i] = 0;
for (int i = 0; i < (a); ++i) {
Nn[Aa[i] - 1] = 1;
}
for (int i = 0; i < (b); ++i) {
if (Nn[Bb[i] - 1] == 0) {
Nn[Bb[i] - 1] = 2;
}
}
for (int i = 0; i < (n); ++i) cout << Nn[i] << " ";
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (n == 0 && m != 0) {
cout << "Impossible";
return 0;
} else if (m == 0 && n != 0) {
cout << n << " " << n;
return 0;
} else if (n == 0 && m == 0) {
cout << "0"
<< " "
<< "0";
return 0;
} else {
cout << n + max(0, (m - n)) << " " << n + (m - 1);
}
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int NMax = 1000005;
int N;
char Str[NMax];
int DP[NMax][3];
bool Marked[NMax];
void Read() {
cin.getline(Str + 1, NMax);
N = strlen(Str + 1);
}
void buildChains() {
Str[0] = Str[N];
for (int i = 1; i <= N; i++)
if (Str[i] != Str[i - 1]) Marked[i] = 1;
}
inline int Code(int x) {
if (x == 0) return N;
if (x > N) return x - N;
return x;
}
int getDP() {
int start;
for (int i = 1; i <= N; i++) {
if (Marked[i] == 1 && Marked[Code(i + 1)] == 1) {
start = Code(i + 1);
break;
}
}
DP[start][0] = 0;
DP[start][1] = -NMax;
DP[start][2] = -NMax;
for (int i = Code(start + 1); i != start; i = Code(i + 1)) {
int poz = Code(i - 1);
if (Marked[Code(i - 1)] == 1 && Marked[Code(i - 2)] == 1) {
int poz = Code(i - 1);
DP[i][0] = max(DP[poz][2], DP[poz][0]);
DP[i][1] = max(DP[poz][2], DP[poz][0]) + 1;
DP[i][2] = DP[poz][1];
} else {
if (Marked[Code(i - 1)] == 1)
DP[i][0] = max(DP[poz][2], DP[poz][0]), DP[i][2] = DP[poz][1];
else
DP[i][0] = DP[poz][0];
if (Marked[Code(i - 1)] == 1) {
DP[i][1] = max(DP[poz][2], DP[poz][0]) + 1;
} else
DP[i][1] = DP[poz][0] + 1;
if (Marked[Code(i - 1)] == 0)
DP[i][2] = max(DP[poz][2], DP[poz][1]);
else
DP[i][2] = DP[poz][1];
}
}
int ans = 0;
int poz = Code(start - 1);
ans = max(ans, max(DP[poz][2], max(DP[poz][1], DP[poz][0])));
DP[start][0] = -NMax;
DP[start][1] = 1;
DP[start][2] = -NMax;
for (int i = Code(start + 1); i != start; i = Code(i + 1)) {
int poz = Code(i - 1);
if (Marked[Code(i - 1)] == 1 && Marked[Code(i - 2)] == 1) {
int poz = Code(i - 1);
DP[i][0] = max(DP[poz][2], DP[poz][0]);
DP[i][1] = max(DP[poz][2], DP[poz][0]) + 1;
DP[i][2] = DP[poz][1];
} else {
if (Marked[Code(i - 1)] == 1)
DP[i][0] = max(DP[poz][2], DP[poz][0]), DP[i][2] = DP[poz][1];
else
DP[i][0] = DP[poz][0];
if (Marked[Code(i - 1)] == 1) {
DP[i][1] = max(DP[poz][2], DP[poz][0]) + 1;
} else
DP[i][1] = DP[poz][0] + 1;
if (Marked[Code(i - 1)] == 0)
DP[i][2] = max(DP[poz][2], DP[poz][1]);
else
DP[i][2] = DP[poz][1];
}
}
ans = max(ans, max(DP[poz][2], DP[poz][0]));
return ans;
}
int getSolution() {
bool useDP = 0;
int chain = 0;
for (int i = 1; i <= N; i++) {
if (Marked[i] == 1) ++chain;
if (Marked[i] == 1 && Marked[Code(i + 1)] == 1) {
useDP = 1;
break;
}
}
if (useDP == 0) return max(chain, 1);
return getDP();
}
int main() {
Read();
buildChains();
cout << getSolution() << "\n";
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long INF = 1ll << 60;
const double Inf = 1e20;
const double eps = 1e-9;
void gmax(int &a, int b) { a = (a > b ? a : b); }
void gmin(int &a, int b) { a = (a < b ? a : b); }
const int maxn = 1000050;
int n, a[maxn], l[maxn], r[maxn], l_[maxn], r_[maxn];
int st[maxn], top;
long long Ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]), l[i] = r[i] = l_[i] = r_[i] = i;
for (int i = 1; i <= n; ++i) {
while (top && a[st[top]] < a[i]) l[i] = l[st[top--]];
st[++top] = i;
}
top = 0;
for (int i = 1; i <= n; ++i) {
while (top && a[st[top]] > a[i]) l_[i] = l_[st[top--]];
st[++top] = i;
}
top = 0;
for (int i = n; i >= 1; --i) {
while (top && a[st[top]] <= a[i]) r[i] = r[st[top--]];
st[++top] = i;
}
top = 0;
for (int i = n; i >= 1; --i) {
while (top && a[st[top]] >= a[i]) r_[i] = r_[st[top--]];
st[++top] = i;
}
for (int i = 1; i <= n; ++i)
Ans = Ans + (1ll * (i - l[i] + 1) * (r[i] - i + 1) -
1ll * (i - l_[i] + 1) * (r_[i] - i + 1)) *
a[i];
printf("%lld\n", Ans);
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
long n, v, d[3005] = {0}, m = 0;
int main() {
long i, a, b, l0 = 0, l1 = 0, ans = 0, vv;
scanf("%ld %ld", &n, &v);
for (i = 1; i <= n; i++) {
scanf("%ld %ld", &a, &b);
d[a] += b;
if (a > m) m = a;
}
m++;
for (i = 1; i <= m; i++) {
l0 = l1;
l1 = d[i];
if (l0 >= v)
ans += v;
else {
ans += l0;
vv = v - l0;
if (vv >= l1) {
ans += l1;
l1 = 0;
} else {
l1 -= vv;
ans += vv;
}
}
}
printf("%ld", ans);
return 0;
}
| 1,400 | CPP |
a1, a2 = map(int, input().split())
minutes = 0
while (a1 > 1 or a2 > 1) and (a1 > 0 and a2 > 0):
if a1 < a2:
a1 += 1
a2 -= 2
else:
a2 += 1
a1 -= 2
minutes += 1
print(minutes)
| 1,100 | PYTHON3 |
x1,y1,x2,y2=map(int,input().split())
x3,y3,x4,y4=map(int,input().split())
x5,y5,x6,y6=map(int,input().split())
z=0
if x3<=x1 and x4>=x2 and y3<=y1 and y4>=y2:
z=1
if x5<=x1 and x6>=x2 and y5<=y1 and y6>=y2:
z=1
if y3>y5:
if y3<=y6 and y4>=y2 and y5<=y1:
if x3<=x1 and x4>=x2 and x5<=x1 and x6>=x2:
z=1
else:
y3,y5=y5,y3
y6,y4=y4,y6
if y3<=y6 and y4>=y2 and y5<=y1:
if x3<=x1 and x4>=x2 and x5<=x1 and x6>=x2:
z=1
y3,y5=y5,y3
y6,y4=y4,y6
if x3<x5:
if x3<=x1 and x4>=x5 and x6>=x2:
if y3<=y1 and y4>=y2 and y5<=y1 and y6>=y2:
z=1
else:
x3,x5=x5,x3
x6,x4=x4,x6
if x3<=x1 and x4>=x5 and x6>=x2:
if y3<=y1 and y4>=y2 and y5<=y1 and y6>=y2:
z=1
if z==1:
print("NO")
else:
print("YES")
| 1,700 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long x, y;
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
cin >> x >> y;
if (gcd(x, y) != 1) {
puts("Impossible");
return 0;
}
vector<pair<long long, char> > ans;
while (x > 1 || y > 1) {
if (x > y) {
ans.push_back(make_pair(x / y, 'A'));
x %= y;
} else {
ans.push_back(make_pair(y / x, 'B'));
y %= x;
}
}
ans.back().first--;
for (auto it : ans) cout << it.first << it.second;
return 0;
}
| 2,400 | CPP |
t=int(input())
for _ in range(t):
n=int(input())
l=input().strip()
k=l[1:].count('1 1 1')
print(k+int(l[0]))
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
const long long num = 1000000000;
int main() {
long long q = 1;
cin >> q;
while (q--) {
long long n;
cin >> n;
cout << n / 2 << endl;
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 709;
int n, lst[N], st[N], top;
long long f[N][3][N][3];
char s[N];
int main() {
scanf("%s", s);
n = strlen(s);
for (int i = n; i >= 1; i--) s[i] = s[i - 1];
memset(f, 0, sizeof(f));
memset(lst, 0, sizeof(lst));
for (int i = 1; i <= n; i++) {
if (s[i] == '(')
st[++top] = i;
else
lst[st[top]] = i, top--;
}
for (int i = 1; i < n; i++)
if (s[i] == '(' && s[i + 1] == ')') {
f[i][1][i + 1][0] = 1;
f[i][2][i + 1][0] = 1;
f[i][0][i + 1][1] = 1;
f[i][0][i + 1][2] = 1;
}
for (int len = 3; len <= n; len++) {
for (int i = 1; i <= n - len + 1; i++) {
int j = i + len - 1;
long long sum1 = (f[i + 1][0][j - 1][0] + f[i + 1][0][j - 1][1] +
f[i + 1][0][j - 1][2]) %
mod;
long long sum2 = (f[i + 1][1][j - 1][0] + f[i + 1][1][j - 1][1] +
f[i + 1][1][j - 1][2]) %
mod;
long long sum3 = (f[i + 1][2][j - 1][0] + f[i + 1][2][j - 1][1] +
f[i + 1][2][j - 1][2]) %
mod;
long long sum4 = (f[i + 1][0][j - 1][0] + f[i + 1][1][j - 1][0] +
f[i + 1][2][j - 1][0]) %
mod;
long long sum5 = (f[i + 1][0][j - 1][1] + f[i + 1][1][j - 1][1] +
f[i + 1][2][j - 1][1]) %
mod;
long long sum6 = (f[i + 1][0][j - 1][2] + f[i + 1][1][j - 1][2] +
f[i + 1][2][j - 1][2]) %
mod;
if (s[i] == '(' && s[j] == ')') {
(f[i][1][j][0] += sum3 + sum1) % mod;
(f[i][2][j][0] += sum2 + sum1) % mod;
(f[i][0][j][1] += sum4 + sum6) % mod;
(f[i][0][j][2] += sum5 + sum4) % mod;
}
if (!lst[i] || lst[i] >= j) continue;
int k = lst[i];
for (int c1 = 0; c1 <= 2; c1++)
for (int c2 = 0; c2 <= 2; c2++) {
if (c1 == c2 && c2 != 0) continue;
(f[i][0][j][0] += f[i][0][k][c1] * f[k + 1][c2][j][0] % mod) %= mod;
(f[i][0][j][1] += f[i][0][k][c1] * f[k + 1][c2][j][1] % mod) %= mod;
(f[i][0][j][2] += f[i][0][k][c1] * f[k + 1][c2][j][2] % mod) %= mod;
(f[i][1][j][0] += f[i][1][k][c1] * f[k + 1][c2][j][0]) %= mod;
(f[i][1][j][1] += f[i][1][k][c1] * f[k + 1][c2][j][1]) %= mod;
(f[i][1][j][2] += f[i][1][k][c1] * f[k + 1][c2][j][2]) %= mod;
(f[i][2][j][0] += f[i][2][k][c1] * f[k + 1][c2][j][0]) %= mod;
(f[i][2][j][1] += f[i][2][k][c1] * f[k + 1][c2][j][1]) %= mod;
(f[i][2][j][2] += f[i][2][k][c1] * f[k + 1][c2][j][2]) %= mod;
}
}
}
long long ans = 0;
(ans += f[1][0][n][0]) %= mod;
(ans += f[1][0][n][1]) %= mod;
(ans += f[1][0][n][2]) %= mod;
(ans += f[1][1][n][0]) %= mod;
(ans += f[1][1][n][1]) %= mod;
(ans += f[1][1][n][2]) %= mod;
(ans += f[1][2][n][0]) %= mod;
(ans += f[1][2][n][1]) %= mod;
(ans += f[1][2][n][2]) %= mod;
printf("%lld\n", ans);
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
std:
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int m = n / 3;
int a[8] = {0};
while (n--) {
int num;
cin >> num;
++a[num];
}
if (a[1] == m && a[2] + a[3] == m && a[6] + a[4] == m && a[2] >= a[4] &&
a[6] >= a[3]) {
while (a[4]--) {
--a[2];
cout << "1 2 4\n";
}
while (a[2]--) {
cout << "1 2 6\n";
}
while (a[3]--) {
cout << "1 3 6\n";
}
} else {
cout << "-1";
}
return 0;
}
| 1,200 | CPP |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
a = LS()
s = set()
for c in a:
cc = tuple(sorted(collections.Counter(c).keys()))
s.add(cc)
return len(s)
print(main())
| 900 | PYTHON3 |
x,y,z=map(int,input().split())
a,b,c=map(int,input().split())
if x<=a:
if abs(a-x)+abs(b)>=y:
if abs((a+b+c)-(x+y))>=z:
print('YES')
else:
print('NO')
else:
print('NO')
else:
print('NO') | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[55][55], b[55][55];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) cin >> a[i][j];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> b[i][j];
if (a[i][j] > b[i][j]) swap(a[i][j], b[i][j]);
}
}
bool flag = 1;
for (int i = 1; i <= n; i++) {
for (int j = 2; j <= m; j++)
if (a[i][j] <= a[i][j - 1] || b[i][j] <= b[i][j - 1]) flag = 0;
}
for (int j = 1; j <= m; j++) {
for (int i = 2; i <= n; i++)
if (a[i][j] <= a[i - 1][j] || b[i][j] <= b[i - 1][j]) flag = 0;
}
if (flag)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
return 0;
}
| 1,400 | CPP |
k = int(input())
s = list(map(int, input()))
co = list()
c = 1
for el in s:
if el == 0:
c += 1
else:
co.append(c)
c = 1
co.append(c)
if k == 0:
print(sum(map(lambda el: el * (el - 1) // 2, co)))
exit()
ans = 0
for i in range(len(co) - k):
ans += co[i] * co[i + k]
print(ans)
| 1,600 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
unsigned long long min(unsigned long long x, unsigned long long y) {
if (x < y) return x;
return y;
}
unsigned long long max(unsigned long long x, unsigned long long y) {
if (x < y) return y;
return x;
}
long long min(long long x, long long y) {
if (x < y) return x;
return y;
}
long long max(long long x, long long y) {
if (x < y) return y;
return x;
}
double min(double x, double y) {
if (x < y) return x;
return y;
}
double max(double x, double y) {
if (x < y) return y;
return x;
}
unsigned long long gcd(unsigned long long x, unsigned long long y) {
if (!x) return y;
if (!y) return x;
if (x > y) swap(x, y);
return gcd(x, y % x);
}
unsigned long long inv(unsigned long long a, unsigned long long c) {
if (a == 1) {
return 1;
}
return ((c - (c / a)) * inv(c % a, c)) % c;
}
void SWAP(int i, int j, vector<int> &p, vector<int> &pos,
vector<pair<int, int> > &ans) {
ans.push_back(make_pair(i + 1, j + 1));
pos[p[i]] = j;
pos[p[j]] = i;
swap(p[i], p[j]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<int> p(n, 0), pos(n, 0);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
pos[p[i]] = i;
}
vector<pair<int, int> > ans;
for (int i = 0; i < n; i++) {
if (p[i] == i) continue;
int j = pos[i];
if ((j - i) * 2 >= n) {
SWAP(i, j, p, pos, ans);
continue;
}
if ((n - 1 - j) * 2 >= n) {
SWAP(n - 1, j, p, pos, ans);
SWAP(n - 1, i, p, pos, ans);
} else {
SWAP(0, j, p, pos, ans);
if (i * 2 >= n) {
SWAP(0, i, p, pos, ans);
} else {
SWAP(0, n - 1, p, pos, ans);
SWAP(i, n - 1, p, pos, ans);
}
SWAP(0, j, p, pos, ans);
}
}
cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << "\n";
}
return 0;
}
| 1,700 | CPP |
n = int(input())
x = 0
i = 1
while i <= n:
i += 1
a = input()
for j in range(len(a)):
if(a[j] == '+'):
x += 1
break;
if (a[j] == '-'):
x = x-1
break;
print(x) | 800 | PYTHON3 |
import math
for i in range(int(input())):
n, m = map(int,input().split())
if(n<=2):
print(1)
else:
print( ((n-3)//m)+2)
| 800 | PYTHON3 |
house_and_shop1,house_and_shop2,shop1_and_shop2=map(int,input().split())
cnt=house_and_shop1
if house_and_shop1+house_and_shop2<=shop1_and_shop2:
cnt+=house_and_shop1+house_and_shop2
else:
cnt+=shop1_and_shop2
if house_and_shop2<=shop1_and_shop2+house_and_shop1:
cnt+=house_and_shop2
else:
cnt+=house_and_shop1+shop1_and_shop2
cnt2=house_and_shop2
if house_and_shop1+house_and_shop2<=shop1_and_shop2:
cnt2+=house_and_shop1+house_and_shop2
else:
cnt2+=shop1_and_shop2
if house_and_shop1<=shop1_and_shop2+house_and_shop2:
cnt2+=house_and_shop1
else:
cnt2+=house_and_shop2+shop1_and_shop2
print(min(cnt2,cnt)) | 800 | PYTHON3 |
n=int(input())
m=[]
for i in range(n):
x=list(input())
m.append(x)
#print(m[0][0])
for i in range(n):
for j in range(n):
if i>=n or i<0 or j>=n or j<0:
continue
if m[i][j]=='.':
if i-1>=0 and j-1>=0 and i+1<n and j+1<n:
if (m[i-1][j]=='.' and m[i+1][j]=='.' and m[i][j-1]=='.' and m[i][j+1]=='.') or (m[i-1][j]=='x' and m[i+1][j]=='x' and m[i][j-1]=='x' and m[i][j+1]=='x'):
m[i-1][j]=m[i][j]=m[i+1][j]=m[i][j-1]=m[i][j+1]='x'
for i in range(n):
for j in range(n):
if m[i][j]=='.':
print("NO")
exit()
print("YES")
| 900 | PYTHON3 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
char s[n + 1];
scanf("%s", s);
int j, t, dot = 0, ans = 0, a[3000] = {0}, i = 0;
while (i < n) {
if (s[i] == '.')
i++;
else if (s[i] == 'L') {
j = i;
t = -1;
while (s[j] != 'R' && j >= 0) {
a[j--] += t;
t--;
}
if (s[j] == 'R') a[j] += t;
i++;
} else {
j = i;
t = 1;
while (s[j] != 'L' && j <= n - 1) {
a[j++] += t;
t++;
}
if (s[j] == 'L') a[j] += t;
i++;
}
}
for (i = 0; i < n; i++)
if (a[i] == 0) ans++;
printf("%d", ans);
return 0;
}
| 1,100 | CPP |
from sys import *
inp = lambda : stdin.readline()
import math
def main():
n,l = int(inp()),list(map(int,inp().split()))
a = 2*max(l) - sum(l) + 1
print(a)
if __name__ == "__main__":
main() | 1,100 | PYTHON3 |
t=int(input())
def aze(i):
s={j:False for j in a}
for k in a:
if k^i in s.keys():
s[k^i]=True
else:
return(False)
if False in s.values():
return(False)
return(True)
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
for i in range(1,2**10+1):
if aze(i):
print(i)
break
else:print(-1)
| 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int W[171][171];
double A[171][171], B[171][171];
int N;
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) scanf("%d", &W[i][j]);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) {
double mid = (W[i][j] + W[j][i]) / 2.00;
A[i][j] = mid, A[j][i] = mid;
B[i][j] = W[i][j] - mid;
B[j][i] = -B[i][j];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) printf("%f ", A[i][j]);
cout << endl;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) printf("%f ", B[i][j]);
cout << endl;
}
}
| 0 | CPP |
n,l=map(int,input().split())
ip=list(map(int,input().split()))
ip=sorted(ip)
op=[]
for i in range(n-1):
op.append(ip[i+1]-ip[i])
try:
if 0 in ip:
if l in ip:
print(max(op)/2)
else:
print(max(max(op)/2,l-ip[n-1]))
else:
if l in op:
print(max(max(op)/2,ip[0]))
else:
print(max(max(op)/2,l-ip[n-1],ip[0]))
except:
print(max(ip[0],l-ip[0]))
| 1,200 | PYTHON3 |
n = int(input())
s = list(input())
dg = 10**6
for i in range(n):
s[i] = ord(s[i])*dg + i
s.sort()
def init_max(init_max_val):
#set_val
for i in range(n):
seg_max[i+num_max-1]=init_max_val[i]
#built
for i in range(num_max-2,-1,-1) :
seg_max[i]=max(seg_max[2*i+1],seg_max[2*i+2])
def update_max(k,x):
k += num_max-1
seg_max[k] = x
while k:
k = (k-1)//2
seg_max[k] = max(seg_max[k*2+1],seg_max[k*2+2])
def query_max(p,q):
if q<=p:
return ide_ele_max
p += num_max-1
q += num_max-2
res=ide_ele_max
while q-p>1:
if p&1 == 0:
res = max(res,seg_max[p])
if q&1 == 1:
res = max(res,seg_max[q])
q -= 1
p = p//2
q = (q-1)//2
if p == q:
res = max(res,seg_max[p])
else:
res = max(max(res,seg_max[p]),seg_max[q])
return res
ide_ele_max = 0
num_max =2**(n-1).bit_length()
seg_max=[ide_ele_max]*2*num_max
res = [0]*n
for e in s:
ind = e%dg
ad = query_max(ind,n)
res[ind] = ad+1
update_max(ind,ad+1)
print(max(res))
print(*res) | 2,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
long long p, q;
long long ans;
void f(long long x) {
long long t = p;
while (t % q == 0) t /= x;
ans = max(ans, t);
}
int main() {
fast();
int T;
cin >> T;
while (T--) {
cin >> p >> q;
if (p % q != 0)
printf("%lld\n", p);
else {
ans = 1;
long long x = q;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) f(i);
while (x % i == 0) x /= i;
}
if (x > 1) f(x);
printf("%lld\n", ans);
}
}
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int dp[N][5], n;
set<string> ans;
string s;
int main() {
cin >> s;
n = s.size();
s = '.' + s;
if (n <= 6)
puts("0");
else {
dp[n + 1][2] = dp[n + 1][3] = 1;
for (int i = n - 1; i > 5; i--) {
if (i <= n - 3)
dp[i][2] = (s.substr(i, 2) == s.substr(i + 2, 2) ? 0 : dp[i + 2][2]) ||
dp[i + 2][3];
else
dp[i][2] = dp[i + 2][2];
if (i <= n - 5)
dp[i][3] = (s.substr(i, 3) == s.substr(i + 3, 3) ? 0 : dp[i + 3][3]) ||
dp[i + 3][2];
else
dp[i][3] = dp[i + 3][2] || dp[i + 3][2];
if (dp[i][2]) ans.insert(s.substr(i, 2));
if (dp[i][3]) ans.insert(s.substr(i, 3));
}
cout << ans.size() << endl;
for (set<string>::iterator it = ans.begin(); it != ans.end(); it++)
cout << *it << endl;
}
return 0;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn], ans[maxn];
multiset<int> s;
vector<int> v[maxn];
set<int> can[maxn];
int gcd(int a, int b) { return (!a || !b) ? a + b : gcd(b % a, a); }
void dfs(int root, int par, int cur) {
ans[root] = max(*can[root].rbegin(), cur);
for (auto child : v[root])
if (child != par) {
can[child].insert(cur);
for (auto tmp : can[root]) can[child].insert(gcd(tmp, a[child]));
dfs(child, root, gcd(cur, a[child]));
}
}
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
can[1].insert(0);
dfs(1, 0, a[1]);
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
return 0;
}
| 2,000 | CPP |
def check(l):
s = sum(l)
for i in l:
if s-i < i:
return "NO"
if s%2:
return ("NO")
else:
return ("YES")
n = int(input())
l = list(map(int,input().split()))
print(check(l)) | 1,500 | PYTHON3 |
n = int(input())
current = [0,0,0]
for _ in range(n):
currentArr = [int(x) for x in input().split()]
current[0] += currentArr[0]
current[1] += currentArr[1]
current[2] += currentArr[2]
if (current == [0,0,0]):
print("YES")
else:
print("NO") | 1,000 | PYTHON3 |
s = set()
lc = int(input())
l1 = list(map(int, input().split(' ')))[1:]
l2 = list(map(int, input().split(' ')))[1:]
su = True
for x in l1:
s.add(x)
for x in l2:
s.add(x)
for i in range(1, lc + 1):
if not i in s:
su = False
if su:
print('I become the guy.')
else:
print('Oh, my keyboard!')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int w[26], n, k, ans;
string s;
void solve() {
cin >> s >> k;
n = s.size();
for (int i = 0; i < 26; ++i) cin >> w[i];
for (int i = 0; i < n; ++i) ans += (w[(int)(s[i] - 'a')] * (i + 1));
int help = -10000;
for (int i = 0; i < 26; ++i) help = max(help, w[i]);
for (int i = n; i < n + k; ++i) ans += (help * (i + 1));
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
long long int F[1001];
void DP() {
F[0] = F[1] = 1;
for (int k = 2; k < 1001; k++) F[k] = ((F[k - 1] + F[k - 2]) % 1000000000);
return;
}
int main(void) {
DP();
long long int n, m, s = 0, A[1001], x, t, v;
scanf("%lld%lld", &n, &m);
for (int k = 1; k <= n; k++) scanf("%lld", &A[k]);
while (m > 0) {
scanf("%lld%lld%lld", &t, &x, &v);
if (t == 1) A[x] = v;
if (t == 2) {
s = 0;
for (int i = x; i <= v; ++i) {
s += (A[i] * F[i - x]);
s %= 1000000000;
}
printf("%lld\n", s);
}
m--;
}
return 0;
}
| 1,500 | CPP |
n=int(input())
li=[]
for i in range(n):
k=list(map(int,input().split()))
for j in k:
li.append(j)
s=int(input())
c=0
for f in li:
if f>=s:
break
else:
c+=1
t=len(li)-c
if t%2==0:
print(t//2)
else:
print(t//2+1) | 800 | PYTHON3 |
n = int(input())
a = list(map(int, input().strip().split()))
p = 1
for i in range(len(a)):
if a[i] >= 0:
a[i] = -a[i] - 1
if n % 2 == 0:
pass
else:
m = min(a)
flag = True
for i in range(len(a)):
if flag and a[i] == m:
a[i] = -a[i] - 1
flag = False
print(" ".join(map(str, a)))
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
std::mt19937 rnd(time(0));
const int N = 100005;
const long long LIM = 1500000ll * 1500000ll;
int n;
struct Node {
int x, y;
Node() { x = 0, y = 0; }
Node(int _x, int _y) : x(_x), y(_y) {}
Node operator+(const Node &rhs) const { return Node(x + rhs.x, y + rhs.y); }
Node operator*(const int t) const { return Node(x * t, y * t); }
long long len2() { return 1ll * x * x + 1ll * y * y; }
};
Node a[N];
int p[N];
int ans[N];
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cin >> n;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i].x >> a[i].y;
}
while (1) {
for (int i = 1; i <= n; ++i) {
p[i] = i;
}
std::shuffle(p + 1, p + 1 + n, rnd);
Node now(0, 0);
for (int i = 1; i <= n; ++i) {
Node nx1 = now + a[p[i]], nx2 = now + a[p[i]] * (-1);
if ((nx1.len2() <= LIM) == (nx2.len2() <= LIM)) {
if (rnd() & 1) {
now = nx1;
ans[p[i]] = 1;
} else {
now = nx2;
ans[p[i]] = -1;
}
} else if (nx1.len2() <= LIM) {
now = nx1;
ans[p[i]] = 1;
} else {
now = nx2;
ans[p[i]] = -1;
}
}
if (now.len2() <= LIM) {
for (int i = 1; i <= n; ++i) {
std::cout << ans[i] << " \n"[i == n];
}
return 0;
}
}
}
| 2,300 | CPP |
res_x = 0
res_y = 0
res_z = 0
for i in range(int(input())):
x, y, z = map(int, input().split())
res_x += x
res_y += y
res_z += z
if res_x == res_y == res_z == 0:
print("YES")
else:
print("NO")
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300 * 1000 + 7, mod = 1000 * 1000 * 1000 + 7;
long long node[4 * maxn][2], flag[4 * maxn][2];
int st[maxn], ft[maxn], cnt = 0, d[maxn], q, place[maxn];
int n, m;
vector<int> adj[maxn], topol;
void dfs(int u) {
topol.push_back(u);
st[u] = cnt++;
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i];
d[v] = d[u] + 1;
dfs(v);
}
ft[u] = cnt;
}
void up(int idx) {
int L = 2 * idx + 1, R = L + 1;
flag[L][1] += flag[idx][1], flag[L][1] %= mod;
flag[R][1] += flag[idx][1];
flag[R][1] %= mod;
flag[idx][1] = 0;
flag[L][0] += flag[idx][0];
flag[L][0] %= mod;
flag[R][0] += flag[idx][0];
flag[R][0] %= mod;
flag[idx][0] = 0;
}
void modify(int s, int e, int l, int r, int idx, long long k, long long val) {
if (s >= r or l >= e) return;
if (s == l and r == e) {
flag[idx][1] += k;
flag[idx][1] %= mod;
flag[idx][0] += val;
flag[idx][0] %= mod;
return;
}
int mid = (s + e) / 2, L = 2 * idx + 1, R = L + 1;
up(idx);
modify(s, mid, l, min(r, mid), L, k, val);
modify(mid, e, max(l, mid), r, R, k, val);
}
long long get(int s, int e, int id, int idx, int v) {
if (s == e - 1)
return ((flag[idx][0] + mod) - ((flag[idx][1] * d[v]) % mod) % mod);
up(idx);
int mid = (s + e) / 2, L = 2 * idx + 1, R = L + 1;
if (id < mid)
return get(s, mid, id, L, v);
else
return get(mid, e, id, R, v);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u;
scanf("%d", &u);
u--;
adj[u].push_back(i);
}
dfs(0);
for (int i = 0; i < n; i++) {
place[topol[i]] = i;
}
cin >> q;
while (q--) {
int t, v;
scanf("%d%d", &t, &v);
v--;
if (t == 1) {
int x, k;
scanf("%d%d", &x, &k);
long long X = x * 1ll, K = k * 1ll;
modify(0, n, st[v], ft[v], 0, K % mod, (X + K * d[v]) % mod);
}
if (t == 2) {
printf("%d\n", get(0, n, place[v], 0, v) % mod);
}
}
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
void IO() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void solve() {
int a, b, x, y;
cin >> a >> b >> x >> y;
cout << max(max(x, a - 1 - x) * b, max(y, b - 1 - y) * a) << '\n';
}
int main() {
IO();
int _;
cin >> _;
while (_--) solve();
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, c[200002];
char s[200002];
bool vis[200002];
int main() {
scanf("%d%s", &n, s);
for (int i = 1; i <= n; ++i)
if (n % i == 0) c[i] = n / i;
for (int i = n; i; --i)
if (n % i == 0)
for (int j = i + i; j <= n; j += i) c[i] -= c[j];
int ans = 0;
for (int i = 1; i <= n; ++i)
if (n % i == 0) {
bool flag = 1;
for (int j = 0; j < n; ++j) vis[j] = 0;
for (int j = 0; j < n; ++j)
if (!vis[j]) {
int x = j, y = 0;
while (!vis[x]) y ^= s[x] - '0', vis[x] = 1, x = (x + i) % n;
if (y) {
flag = 0;
break;
}
}
if (flag) ans += c[i];
}
printf("%d\n", ans);
}
| 2,100 | CPP |
import sys
n = int(input())
if n < 4:
print("0 0 " + str(n))
sys.exit()
mem = [1,1,2,3]
while mem[-1] < n:
mem = mem[1:]
mem.append(mem[-1]+mem[-2])
print(mem[0],mem[1],mem[1])
| 900 | PYTHON3 |
n,k=map(int,input().split())
if k+1>n//2:
p=0
q=0
r=n
print(p,q,r)
exit()
x=n//(2*(k+1))
y=k*x
print(x,y,n-x-y)
| 800 | PYTHON3 |
n = int(input())
s = input()
number_A = 0
number_D = 0
for c in s:
if c is 'A':
number_A +=1
else:
number_D +=1
if number_A > number_D:
print('Anton')
elif number_D > number_A:
print('Danik')
else:
print('Friendship') | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
struct data {
pair<long long, int> ma1, ma2;
void init(void) { ma1 = ma2 = make_pair(-1e18, -1); }
void add(pair<long long, int> val) {
if (ma1.first < val.first) {
swap(ma1, val);
}
if (ma2.first < val.first) {
if (val.second != ma1.second) swap(ma2, val);
}
}
} f[(1 << 18)];
int N, a[maxn], lab[maxn];
long long mst = 0;
pair<long long, int> best[maxn];
int finds(int u) {
if (lab[u] < 0) return u;
return lab[u] = finds(lab[u]);
}
bool merges(int u, int v) {
u = finds(u), v = finds(v);
if (u == v) return false;
if (lab[v] < lab[u]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
signed main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
mst -= a[i];
}
for (int i = 0; i <= N; ++i) lab[i] = -1;
int cnt = N;
while (cnt) {
for (int i = 0; i <= N; ++i) best[i] = make_pair(-1e18, -1);
for (int i = 0; i < (1 << 18); ++i) f[i].init();
for (int i = 0; i <= N; ++i) f[a[i]].add(make_pair(a[i], finds(i)));
for (int b = 0; b < 18; ++b) {
for (int mask = 0; mask < (1 << 18); ++mask) {
if (mask >> b & 1) {
f[mask].add(f[mask ^ (1 << b)].ma1);
f[mask].add(f[mask ^ (1 << b)].ma2);
}
}
}
for (int i = 0; i <= N; ++i) {
int tmp = finds(i);
int req = ((1 << 18) - 1 - a[i]);
if (tmp != f[req].ma1.second)
best[tmp] = max(best[tmp],
make_pair(f[req].ma1.first + a[i], f[req].ma1.second));
if (tmp != f[req].ma2.second)
best[tmp] = max(best[tmp],
make_pair(f[req].ma2.first + a[i], f[req].ma2.second));
}
for (int i = 0; i <= N; ++i) {
if (lab[i] < 0 && best[i].second != -1) {
if (merges(i, best[i].second)) {
--cnt;
mst += best[i].first;
}
}
}
}
cout << mst;
}
| 3,500 | CPP |
t = "abacaba"
def check(s, n):
cnt = 0
for i in range(n - len(t) + 1):
match = True
for j in range(len(t)):
if s[i + j] != t[j]:
match = False
break
if match:
cnt += 1
if cnt == 1:
return True
return False
T = int(input())
for _ in range(T):
n = int(input())
s = input()
cnt = 0
ans = ""
for i in range(n - len(t) + 1):
match = True
match_s = s
for j in range(len(t)):
if s[i + j] == '?':
match_s = match_s[:i + j] + t[j] + match_s[i + j + 1:]
continue
if s[i + j] != t[j]:
match = False
break
if match:
if check(match_s, n):
cnt += 1
ans = match_s
if cnt > 0:
print("YES")
print(ans.replace('?', 'q'))
else:
print("NO")
| 1,500 | PYTHON3 |
s = input()
n = int(input())
a = []
ans_1 = False
ans_2 = False
total_ans = False
for _ in range(n):
st = input()
if st[::-1] == s or s == st:
total_ans = True
break
if st[0] == s[1]:
ans_1 = True
if st[1] == s[0]:
ans_2 = True
if ans_1*ans_2 or total_ans:
print("YES")
else:
print("NO")
| 900 | PYTHON3 |
from sys import stdin
def solution(word):
if len(word) <= 10:
print(word)
return
newStr = word[0] + str(len(word) - 2) + word[-1]
print(newStr)
testcases = []
for line in stdin:
testcases.append(line.strip())
for i in range(1, len(testcases)):
solution(testcases[i])
| 800 | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.