solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; long long f[maxn], g[maxn]; bool isprime[maxn]; int prime[maxn]; int tot = 0; void init() { for (int i = 2; i < maxn; i++) isprime[i] = true; for (int i = 2; i < maxn; i++) { if (isprime[i]) prime[tot++] = i; for (int j = 0; j < tot && i * prime[j] < maxn; j++) { isprime[i * prime[j]] = false; if (i % prime[j] == 0) break; } } } long long solve(long long n) { long long i, j, m; for (m = 1; m * m <= n; m++) f[m] = n / m - 1; for (i = 1; i <= m; i++) g[i] = i - 1; for (i = 2; i <= m; i++) { if (g[i] == g[i - 1]) continue; for (j = 1; j <= min(m - 1, n / i / i); j++) { if (i * j < m) f[j] -= f[i * j] - g[i - 1]; else f[j] -= g[n / i / j] - g[i - 1]; } for (j = m; j >= i * i; j--) g[j] -= g[j / i] - g[i - 1]; } long long ans = 0; for (int i = 2; i < m; i++) { if (g[i] == g[i - 1]) continue; ans += f[i] - g[i]; } return ans; } int main(void) { init(); long long n; scanf("%lld", &n); long long ans = 0; for (int i = 0; i < tot; i++) { if (prime[i] * 1LL * prime[i] * prime[i] > n) break; ans++; } printf("%lld", ans + solve(n)); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const int inf = numeric_limits<int>::max(); const long long int linf = numeric_limits<long long int>::max(); const long long int MOD = (long long int)1e9 + 7; long long int fast_mod(long long int abcd, long long int expp) { long long int res = 1; while (expp > 0) { if (expp % 2 == 1) res = (res * abcd) % MOD; abcd = (abcd * abcd) % MOD; expp /= 2; } return res % MOD; } long long int gcd(long long int a, long long int b) { long long int temppp = max(a, b); b = min(a, b); a = temppp; if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long int p; cin >> n; cin >> p; int t[n]; vector<pair<long long int, int>> v; queue<int> q; set<int> s; for (int i = 0; i < (n); ++i) { cin >> t[i]; v.push_back(make_pair(t[i], i)); } sort(v.begin(), v.end()); long long int current_time = 0; int taken = 0; int added = 0; long long int ans[n]; while (taken != n) { if (q.empty() && s.empty()) { current_time = v[added].first; } current_time += p; for (int i = added; i < n; i++) { if (v[i].first <= current_time) { added++; if (q.empty() || q.back() > v[i].second) { q.push(v[i].second); } else { s.insert(v[i].second); } } else break; } int index = q.front(); q.pop(); ans[index] = current_time; taken++; if (q.empty() && !s.empty()) { auto it = s.begin(); q.push(*it); s.erase(it); } } for (int i = 0; i < (n); ++i) cout << ans[i] << " "; cout << "\n"; return 0; }
2,300
CPP
# -*- coding: utf-8 -*- word=input() characters=list(word) upper=0 lower=0 for character in characters: character_lower=character.lower() if character==character_lower: lower+=1 else: upper+=1 if upper<=lower: word=word.lower() if upper>lower: word=word.upper() print(word)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-14; const long double PI = acosl(-1.0); long double read() { int x; scanf("%d", &x); return x * 0.001; } struct Point { long double x, y; Point(long double _x = 0, long double _y = 0) { x = _x; y = _y; } }; Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } long double operator*(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } long double norm(Point a) { return a * a; } long double abs(Point a) { return sqrt(norm(a)); } int n, m; long double k[50050], b[50050]; long double dis[50050], ang[50050]; Point o; Point inter(int u, int v) { Point re; re.x = (b[v] - b[u]) / (k[u] - k[v]); re.y = re.x * k[u] + b[u]; return re; } namespace bit { int tree[50050]; int sum(int x) { int re = 0; while (x) { re += tree[x]; x -= x & -x; } return re; } void add(int x, int d) { while (x <= n) { tree[x] += d; x += x & -x; } } } // namespace bit int N; int pos[50050]; struct node { long double d; int id; bool type; node(long double _d = 0, int _id = 0, int _type = 0) { d = _d; id = _id; type = _type; } bool operator<(const node &other) const { return d < other.d; } } rec[100050]; bool check(long double r, bool type) { N = 0; for (int i = 1; i <= n; ++i) if (dis[i] < r) { long double d = acosl(dis[i] / r); long double a = ang[i] - d, b = ang[i] + d; if (a < -PI) a += 2 * PI; if (b > PI) b -= 2 * PI; if (a > b) swap(a, b); rec[++N] = node(a, i, 0); rec[++N] = node(b, i, 1); } sort(rec + 1, rec + N + 1); for (int i = 1, j = 1; i <= N; ++i) { if (rec[i].type) { pos[rec[i].id] = j++; } } if (type) return 0; long long cnt = 0; for (int i = 1; i <= N; ++i) { int x = pos[rec[i].id]; if (rec[i].type == 0) { cnt += bit::sum(x); bit::add(x, 1); } else { bit::add(x, -1); } } return cnt >= m; } int rnk[50050]; set<int> s; int main() { scanf("%d", &n); o.x = read(); o.y = read(); scanf("%d", &m); for (int i = 1; i <= n; ++i) { k[i] = read(); b[i] = read(); dis[i] = fabs(k[i] * o.x - o.y + b[i]) / sqrt(k[i] * k[i] + 1); ang[i] = atanl(k[i]) + (o.x * k[i] + b[i] > o.y ? 0.5 * PI : -0.5 * PI); } int T = 100; long double l = 0, r = 1e10; while (T--) { long double mid = (l + r) / 2; if (check(mid, 0)) r = mid; else l = mid; } memset(pos, 0, sizeof(pos)); check(l - eps, 1); for (int i = 1; i <= n; ++i) { rnk[pos[i]] = i; } long double an = 0; int cnt = 0; for (int i = 1; i <= N; ++i) { int x = pos[rec[i].id]; if (rec[i].type == 0) { set<int>::iterator it = s.begin(); while (it != s.end() && *it <= x) { an += abs(o - inter(rec[i].id, rnk[*it])); ++cnt; ++it; } s.insert(x); } else { s.erase(x); } } assert(cnt <= m); an += (m - cnt) * l; printf("%.10Lf\n", an); return 0; }
3,300
CPP
s = input() l = [] for i in range(0, len(s)): if s[i] != '+': l.append(s[i]) l.sort() x = "" for i in range(0, len(l) - 1): x += l[i] + "+" x += l[len(l) - 1] print(x)
800
PYTHON3
s = input() n = len(s) zeros, ones, other = 0, 0, 0 for x in s: zeros += x == '0' ones += x == '1' other += x == '?' A = ['00', '01', '10', '11'] pos = [0] * 4 pos[0] = zeros + other > n // 2 pos[3] = zeros < n // 2 if zeros <= n // 2 and zeros + other >= n // 2: canuse = other - n // 2 + zeros pos[1] = s[n-1] == '1' or (s[n-1] == '?' and canuse) canuse = zeros < n // 2 pos[2] = s[n-1] == '0' or (s[n-1] == '?' and canuse) for i in range(4): if pos[i]: print(A[i])
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = (int)2007; const int MAXS = (int)1e5 + 7; const int infint = (int)1e9; const long long inf = (long long)1e18; int color[2][MAXN][MAXN]; int id[MAXN][MAXN]; int ans[MAXS], a, b, m; void dfs(int p, int u, int v, int c1, int c2) { int tmp = color[1 - p][v][c1]; color[p][u][c1] = v; color[1 - p][v][c1] = u; if (!tmp) { color[1 - p][v][c2] = 0; return; } dfs(1 - p, v, tmp, c2, c1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a >> b >> m; int cnt = 0; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; id[u][v] = i; int c1 = 1, c2 = 1; while (color[0][u][c1]) c1++; while (color[1][v][c2]) c2++; cnt = max(cnt, max(c1, c2)); if (c1 == c2) color[0][u][c1] = v, color[1][v][c1] = u; else dfs(0, u, v, c1, c2); } cout << cnt << "\n"; for (int i = 1; i <= a; i++) for (int j = 1; j <= cnt; j++) if (color[0][i][j] != 0) ans[id[i][color[0][i][j]]] = j; for (int i = 1; i <= m; i++) cout << ans[i] << " "; }
2,800
CPP
n = int(input()) lst = map(int,input().split(" ")) a = 0 b= -1 t = 0 for i in lst: if i == 1: t += 1 if a > 0: a -= 1 else: a += 1 if a > b: b = a print(t + b)
1,200
PYTHON3
# import sys # sys.stdin=open("input1.in","r") # sys.stdout=open("output2.out","w") S=list(input()) N=len(S) if N%2==0: s="" for i in range(N//2): x=S[i] y=S[N-1-i] s=x+y+s print(s) else: s="" for i in range(N//2): x=S[N-1-i] y=S[i] s=x+y+s s=S[(N)//2]+s print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> ans; int best = 0; map<pair<int, int>, pair<int, int> > mp; for (int i = 1; i <= int(n); ++i) { int a[3]; for (int i = 0; i < int(3); ++i) cin >> a[i]; sort(a, a + 3); if (a[0] > best) { ans = {i}; best = a[0]; } if (mp.count({a[1], a[2]})) { auto p = mp[{a[1], a[2]}]; int now = min({a[1], a[2], a[0] + p.first}); if (now > best) { best = now; ans = {i, p.second}; } } mp[{a[1], a[2]}] = max(mp[{a[1], a[2]}], {a[0], i}); } cout << ans.size() << endl; for (auto it : ans) cout << it << ' '; return 0; }
1,600
CPP
t=int(input()) for _ in range(t): s=input() c=0 d={'R':0,'P':0,'S':0} for i in s: for x in d: c=0 if x=='R': if i=='R': pass elif i=='P': pass else: c+=1 elif x=='P': if i=='P': pass elif i=='R': c+=1 else: pass else: if i=='S': pass elif i=='P': c+=1 else: pass d[x]+=c key='R' val=d['R'] for i in d: if d[i]>val: val=d[i] key=i print(key*len(s))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; long long pred[301010]; long long sz[301010]; long long find(long long v) { if (pred[v] == v) return v; return pred[v] = find(pred[v]); } void make(long long v) { pred[v] = v; sz[v] = 1; return; } void merge(long long a, long long b) { a = find(a); b = find(b); if (a == b) return; pred[b] = a; sz[a] += sz[b]; } struct Info { long long a, b, c, id; }; long long n; vector<pair<long long, long long> > g[301010]; Info t[301010]; long long ans[301010]; long long color[301010]; bool used[301010]; long long col = 1; long long Size[301010]; void Sum(long long v, long long pred) { color[v] = col; Size[col] += sz[find(v)]; for (auto [to, id] : g[v]) { if (to == pred) continue; Sum(to, v); } } long long Cnt[101010]; void dfs(long long v, long long pred) { used[find(v)] = true; for (auto [to, id] : g[v]) { if (to == pred) continue; dfs(to, v); Cnt[v] += Cnt[to]; ans[id] += (Cnt[to] * (Size[color[find(v)]] - Cnt[to])); } Cnt[v] += sz[find(v)]; } void add(long long id) { ans[id] += (sz[find(t[id].a)] * sz[find(t[id].b)]); merge(find(t[id].a), find(t[id].b)); } void go(long long l, long long r) { for (long long i = l; i <= r; i++) { g[find(t[i].a)].push_back({find(t[i].b), i}); g[find(t[i].b)].push_back({find(t[i].a), i}); } for (long long i = l; i <= r; i++) { if (color[find(t[i].a)] == 0) { Sum(find(t[i].a), find(t[i].a)); col++; } if (color[find(t[i].b)] == 0) { Sum(find(t[i].b), find(t[i].b)); col++; } } for (long long i = l; i <= r; i++) { if (used[find(t[i].a)]) continue; dfs(find(t[i].a), find(t[i].a)); } for (long long i = 1; i <= col; i++) { color[i] = 0; Cnt[i] = 0; used[i] = false; Size[i] = 0; } for (long long i = l; i <= r; i++) { g[find(t[i].a)].pop_back(); g[find(t[i].b)].pop_back(); Cnt[t[i].a] = 0; Cnt[t[i].b] = 0; Cnt[find(t[i].a)] = 0; Cnt[find(t[i].b)] = 0; Size[t[i].a] = 0; Size[t[i].b] = 0; Size[find(t[i].a)] = 0; Size[find(t[i].b)] = 0; Size[color[t[i].a]] = 0; Size[color[t[i].b]] = 0; color[t[i].a] = 0; color[t[i].b] = 0; color[find(t[i].a)] = 0; color[find(t[i].b)] = 0; used[t[i].a] = false; used[t[i].b] = false; used[find(t[i].a)] = false; used[find(t[i].b)] = false; } col = 1; for (long long i = l; i <= r; i++) merge(t[i].a, t[i].b); } int32_t main() { cin >> n; for (long long i = 1; i <= n; i++) make(i); for (long long i = 1; i < n; i++) { cin >> t[i].a >> t[i].b >> t[i].c; t[i].id = i; } sort(t + 1, t + n, [](Info q, Info w) { return q.c < w.c; }); for (long long i = 1; i < n; i++) { long long j = i; for (; j <= n; j++) { if (t[i].c != t[j].c) break; } if (i == j - 1) add(i); else { go(i, j - 1); i = j - 1; } } long long M = 0; vector<long long> res; for (long long i = 1; i < n; i++) { if (M < ans[i]) { M = ans[i]; res.clear(); res.push_back(t[i].id); } else if (M == ans[i]) { res.push_back(t[i].id); } } sort(res.begin(), res.end()); cout << M + M << " " << res.size() << '\n'; for (auto i : res) cout << i << " "; }
2,300
CPP
T = int(input()) for t in range(T): n, m = [int(x) for x in input().split()] rows = [0] * n lines = [0] * m for N in range(n): inp = [int(x) for x in input().split()] for M in range(m): if inp[M] == 1: rows[N] = 1 lines[M] = 1 free_lines = lines.count(0) free_rows = rows.count(0) turns = min(free_rows, free_lines) # print(free_lines, free_rows, turns) if turns % 2 == 0: print("Vivek") else: print("Ashish")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, a, q; long long ans[1000000 + 200], inv[1000000 + 200]; long long C[1000000 + 200], mod = 1; long long quick_mod(long long a, long long b, long long m) { long long ans = 1; a %= m; while (b) { if (b & 1) { ans = ans * a % m; b--; } b >>= 1; a = a * a % m; } return ans; } void init() { inv[1] = 1, C[0] = 1; for (int i = 2; i < mod; ++i) { inv[i] = ((mod / i) * inv[mod % i]) % mod; inv[i] = (-inv[i] + mod) % mod; } for (int i = 1; i <= m; i++) { C[i] = C[i - 1] * (m + 1 - i) % mod * inv[i] % mod; } } int main() { cin >> n >> m >> a >> q; long long temp = a; while (temp != 1) { temp = temp * a % q; mod++; } init(); for (long long i = n; i >= (n - m + 1 > 1 ? n - m + 1 : 1); i--) { ans[i] = (ans[i + 1] + C[m - (n - i)]) % mod; } for (long long i = (n - m > 0 ? n - m : 0); i >= 1; i--) { if (i == n - m) ans[i] = (ans[i + 1] + 1) % mod; else ans[i] = ans[i + 1]; } for (long long i = 1; i <= n; i++) { printf("%lld ", quick_mod(a, ans[i], q)); } }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; if (n % 2 == 0) { if (n / 2 % 2 == 0) cout << 0; else cout << 1; } else if ((n + 1) / 2 % 2 == 0) cout << 0; else cout << 1; return 0; }
800
CPP
import math a,b,n=map(int,input().split()) for i in range(1000): ans=i%2 if ans==0: k=math.gcd(a,n) if n<k: break else: n-=k else: k=math.gcd(b,n) if n<k: break else: n-=k print((ans+1)%2)
800
PYTHON3
n=int(input()) s = input() if (s.count('xxx') == 0): print(0) else: a=s count = 0 while (a.count("xxx") != 0): a = a.replace("xxx", "xx") count += 1 print(len(s)-len(a))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int i, n, cnt = 0; cin >> n; string s; cin >> s; vector<int> ans; for (i = 0; i < n - 1; i++) { if (s[i] == 'B') { s[i] = 'W'; s[i + 1] == 'W' ? (s[i + 1] = 'B') : (s[i + 1] = 'W'); cnt++; ans.push_back(i + 1); } else { continue; } } if (s[i] == 'W') { cout << cnt << endl; for (int u : ans) cout << u << " "; } else { for (i = 0; i < n - 1; i++) { if (s[i] == 'W') { s[i] = 'B'; s[i + 1] == 'B' ? (s[i + 1] = 'W') : (s[i + 1] = 'B'); cnt++; ans.push_back(i + 1); } } if (s[i] == 'B') { cout << cnt << endl; for (int u : ans) cout << u << " "; } else { cout << -1; } } } int main() { int t = 1; while (t--) { solve(); cout << endl; } return 0; }
1,300
CPP
n=input() n=int(n) s=input() if n==1: print(0) elif n==2: if s[0]==s[1]: print(1) else: print(0) else: i=0 j=i+1 count=0 while i<n and j<n: if s[i]==s[j]: count=count+1 j=j+1 else: if j==i+1: i=i+1 j=i+1 else: i=j j=i+1 print(count)
800
PYTHON3
a = int(input()) if a >= 0: print(a) elif a == -10: print(0) else: value = a // -10 value = -1 * value b = str(a) value_1 = int(b[:-2]+ b[-1]) if value > value_1: print(value) else: print(value_1)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 15; char st[maxn]; int ans; int ch[maxn][3]; int main() { st[0] = st[1] = '0'; scanf("%s", st + 2); int len = strlen(st); int one = 0; for (int i = len - 1; i > 0; i--) if (st[i] == '1' - one) { ans++; one = st[i - 1] - '0'; if (one > 0) ch[ans][0] = -1; else ch[ans][0] = 1; ch[ans][1] = len - i - 1; } printf("%d\n", ans); for (int i = 1; i <= ans; i++) if (ch[i][0] > 0) printf("+2^%d\n", ch[i][1]); else printf("-2^%d\n", ch[i][1]); return 0; }
2,100
CPP
from bisect import bisect_left as bl, bisect_right as br, insort import sys import heapq from math import * from collections import defaultdict as dd, deque def data(): return sys.stdin.readline().strip() def mdata(): return map(int, data().split()) #sys.setrecursionlimit(100000) n,k=mdata() s=set() for i in range(n): s.add(int(''.join(input().split()),2)) for i in s: for j in s: if i&j==0: print("YES") exit() print("NO")
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int a[1000006]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int ans = 1000000 - a[0]; a[n] = 1000000; for (int i = 0; i < n; i++) { ans = min(ans, max((a[i] - 1), 1000000 - a[i + 1])); } printf("%d\n", ans); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int inf = 1e9; const int mod = 1e9 + 7; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int a[n], b[n], c[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> c[i]; int ans[n]; ans[0] = a[0]; for (int i = 1; i < n - 1; i++) { if (a[i] != ans[i - 1]) ans[i] = a[i]; else if (b[i] != ans[i - 1]) ans[i] = b[i]; else ans[i] = c[i]; } if (a[n - 1] != ans[n - 2] && a[n - 1] != ans[0]) ans[n - 1] = a[n - 1]; else if (b[n - 1] != ans[n - 2] && b[n - 1] != ans[0]) ans[n - 1] = b[n - 1]; else ans[n - 1] = c[n - 1]; for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << '\n'; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int par[18][200005]; long long dep[200005]; int n, a[200005], lev[200005]; vector<int> gph[200005]; int cnt[200005]; void dfs(int x) { for (auto i : gph[x]) { dep[i] += dep[x]; lev[i] = lev[x] + 1; dfs(i); } } void dfs2(int x) { for (auto i : gph[x]) { dfs2(i); cnt[x] += cnt[i]; } } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 2; i <= n; i++) { cin >> par[0][i]; cin >> dep[i]; gph[par[0][i]].push_back(i); } for (int i = 1; i < 18; i++) { for (int j = 1; j <= n; j++) { par[i][j] = par[i - 1][par[i - 1][j]]; } } dfs(1); for (int i = 2; i <= n; i++) { cnt[par[0][i]]++; int anc = i; for (int j = 17; j >= 0; j--) { if (lev[anc] >= (1 << j) && dep[i] - dep[par[j][anc]] <= a[i]) { anc = par[j][anc]; } } cnt[par[0][anc]]--; } dfs2(1); for (int i = 1; i <= n; i++) { cout << cnt[i] << " "; } }
1,900
CPP
n=int(input()) bills=[int(_) for _ in input().split()] c25,c50,flag=0,0,1 for i in bills: if i==25: c25+=1 elif i==50: if c25==0: flag=0 break else: c25-=1 c50+=1 else: if c25>0 and c50>0: c25-=1 c50-=1 elif c25>2: c25-=3 else: flag=0 break if flag: print("YES") else: print("NO")
1,100
PYTHON3
lis=input() if 'H' in lis:print('YES') elif 'Q' in lis:print('YES') elif '9' in lis:print('YES') else:print('NO')
900
PYTHON3
#include<iostream> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<unordered_map> #include<array> #include<iterator> #include<stack> #include<string> #include<cstring> #include<set> #include<deque> #include<queue> #include<climits> #include<list> #include<utility> using namespace std; struct node { int data; struct node*left; struct node*right; }; struct node* newNode(int d) { struct node* temp=new struct node; temp->left=NULL; temp->right=NULL; temp->data=d; return temp; }; void inorder(struct node*temp,vector<int> &v) { if(!temp) return; inorder(temp->left,v); v.push_back(temp->data); inorder(temp->right,v); } void inorder2(struct node*temp) { if(!temp) return; inorder2(temp->left); cout<<temp->data<<" "; inorder2(temp->right); } void search(struct node*temp,int data) { while(temp!=NULL) { if(temp->data==data) { cout<<"found"<<endl; break; } else if(temp->data<data) temp=temp->right; else temp=temp->left; } } void insert(struct node*temp,int data) { while(temp->left==NULL&&temp->right==NULL) { if(data<temp->data) temp=temp->left; else temp=temp->right; } if(data<temp->data) { temp->left=newNode(data); } else { temp->right=newNode(data); } } void deletenode(struct node*temp,int data) { struct node*t=temp; while(temp->data!=data) { t=temp; if(data<temp->data) { temp=temp->left; } else temp=temp->right; } if(!temp->left&&!temp->right) { if(t!=temp) { if(t->right==temp) t->right=NULL; else t->left=NULL; } else temp->data=-1; } else if(!(temp->left&&temp->right)) { if(temp->left) { temp->data=temp->left->data; temp->left=NULL; } else { temp->data=temp->right->data; temp->right=NULL; } } else { t=temp->right; struct node*q=t; while(t->left) { q=t; t=t->left; } if(t!=q) {temp->data=t->data; q->left=NULL; } else { temp->data=t->data; temp->right=NULL; } } } struct node*construct(int a[],int l,int r) { if(r<l) return NULL; struct node*root=newNode(a[(l+r)/2]); root->left=construct(a,l,(l+r)/2-1); root->right=construct(a,(l+r)/2+1,r); return root; }; void constree(struct node*temp,int n) { if(n<temp->data&&temp->left==NULL) temp->left=newNode(n); else if(n>temp->data&&temp->right==NULL) temp->right=newNode(n); else if(n<temp->data) constree(temp->left,n); else if(n>temp->data) constree(temp->right,n); } struct node*constructtree(vector<int> v) { struct node*root=newNode(v[0]); struct node*temp=NULL; for(int i=1;i<v.size();i++) { temp=root; constree(temp,v[i]); } return root; }; void checkbalanced(struct node*root) { deque<struct node*> q; q.push_back(root); struct node*temp=NULL; while(!q.empty()) { temp=q.front(); if(!temp->left&&temp->right) { if(temp->right->right||temp->right->left) { cout<<"NO"<<endl; return; } } else if(!temp->right&&temp->left) { if(temp->left->right||temp->left->left) { cout<<"NO"<<endl; return; } } if(temp->left) q.push_back(temp->left); if(temp->right) q.push_back(temp->right); q.pop_front(); } cout<<"YES"<<endl; } void bottomtree(struct node*root) { deque<struct node*> q,res; q.push_back(root); struct node*temp=NULL; while(q.empty()==0) { temp=q.front(); q.pop_front(); res.push_back(temp); if(temp->left!=NULL) { if(temp->left->right==NULL) { cout<<temp->data<<" "; } q.push_back(temp->left); } if(temp->right!=NULL) { if(temp->right->left==NULL) cout<<temp->data<<" "; q.push_back(temp->right); } } } map<int,vector<int>> verticaltree(struct node*root,map<int,vector<int>> &m,int n) { m[n].push_back(root->data); if(root->left!=NULL) m=verticaltree(root->left,m,n+1); if(root->right!=NULL) m=verticaltree(root->right,m,n-1); return m; } map<int,int> toptree(struct node*root,map<int,int> &m,int n) { if(m[n]==0) m[n]=root->data; if(root->left!=NULL) m=toptree(root->left,m,n+1); if(root->right!=NULL) m=toptree(root->right,m,n-1); return m; } void printq(deque<int> q) { for(auto itr=q.begin();itr!=q.end();itr++) cout<<*itr<<" "; cout<<endl; } void checkpath(struct node*root,deque<int> q,int n) { if(root==NULL) { return; } if(root->data>n) return; if(root->data==n) { q.push_back(root->data); printq(q); q.pop_back(); return; } q.push_back(root->data); checkpath(root->left,q,n-root->data); checkpath(root->right,q,n-root->data); } struct node* constr_inle(map<int,int> m,vector<int> le) { struct node*root=newNode(le[0]); struct node*temp=root; struct node*res=root; for(int i=1;i<le.size();i++) { temp=root; int p=m[le[i]]; while(temp!=NULL) { res=temp; if(p<m[temp->data]) { temp=temp->left; } else { temp=temp->right; } } if(temp==res->left) { res->left=newNode(le[i]); } else res->right=newNode(le[i]); } return root; } void righttree(struct node*root) { struct node*temp=NULL; deque<struct node*> q,s; s.push_back(root); while(s.empty()!=1) { q=s; s.clear(); while(q.empty()!=1) { temp=q.front(); if(q.front()->left!=NULL) s.push_back(q.front()->left); if(q.front()->right!=NULL) s.push_back(q.front()->right); q.pop_front(); } cout<<temp->data<<" "; } } void pathtoleaf(struct node*root,deque<int> q) { if(root==NULL) return; if(root->left==NULL&&root->right==NULL) { q.push_back(root->data); printq(q); q.pop_back(); return; } q.push_back(root->data); pathtoleaf(root->left,q); pathtoleaf(root->right,q); } //////////////////////////////////////////////////////////////////////// struct minheap { int capacity; int heap_size; int* h; }; struct minheap* newarr(int capacity) { int* h=new int [capacity]; struct minheap*temp=new struct minheap; temp->h=h; temp->capacity=capacity; temp->heap_size=0; return temp; }; int parent(int i) { return (i-1)/2; } int left(int i) { return (2*i); } int right(int i) { return (2*i+1); } int getmini(struct minheap*temp) { return temp->h[0]; } void minheapify(struct minheap*temp,int i) { int *h=temp->h; int l,r,smallest=i; if(left(i)<temp->heap_size) l=left(i); else l=-1; if(right(i)<temp->heap_size) r=right(i); else r=-1; if(l==-1) return; if(l!=-1&&r==-1) { if(h[l]<h[i]) {smallest=l; swap(h[l],h[i]); } } else { if(h[l]<h[i]&&h[l]<h[r]) smallest=l; else if(h[r]<h[l]&&h[r]<h[i]) smallest=r; swap(h[smallest],h[i]); } if(smallest==i) return; minheapify(temp,smallest); } void insertheap(struct minheap*temp,int n) { int *h=temp->h; if(temp->heap_size==temp->capacity) cout<<"OVERFLOW"; else { temp->heap_size++; int i=temp->heap_size-1; h[i]=n; while(i!=0&&h[parent(i)]>h[i]) {swap(h[parent(i)],h[i]); i=parent(i); } } } void decreasekey(struct minheap*temp,int i,int value) { int *h=temp->h; h[i]=value; while(i!=0&&h[parent(i)]>h[i]) {swap(h[parent(i)],h[i]); i=parent(i); } } int extractmin(struct minheap*temp) { int s=temp->heap_size; int *h=temp->h; if(s==0) cout<<"heap is empty"<<endl; else if(s==1) { temp->heap_size--; return h[0]; } else { int root=h[0]; h[0]=h[s-1]; temp->heap_size--; minheapify(temp,0); return root; } } void deletekey(struct minheap*temp,int i) { decreasekey(temp,i,INT_MIN); extractmin(temp); } void heapsort(struct minheap*temp) { while(temp->heap_size>0) {int t=extractmin(temp); temp->h[temp->heap_size]=t; } } class graph { int V; list<int> *adj; map<int,vector<int>> m; public: graph(int V); void addEdge(int u,int v); void BFS(int u); void DFS(int u); int mothervertex(); void timeardep(int u,int x,int y); void showardep(); int checkbipartite(); bool kahns_topo(vector<int> &topo); void paths_givenedges(int u,int v,int m); bool iscyclic(int temp,vector<bool> visited); int chessknight(int s1,int s2,int d1,int d2); list<int>* get_adj() { return adj; } }; graph::graph(int V) { this->V=V; adj=new list<int>[V]; } void graph::addEdge(int u,int v) { adj[u].push_back(v); adj[v].push_back(u); } void graph::timeardep(int u,int x,int y) { m[u].push_back(x); m[u].push_back(y); } void graph::showardep() { for(auto itr=m.begin();itr!=m.end();itr++) { cout<<itr->first<<"->"; cout<<itr->second[0]<<" "<<itr->second[1]<<endl; } } void graph::BFS(int u) { deque<int> q; q.push_back(u); bool visited[V]; for(int i=0;i<V;i++) visited[i]=false; visited[u]=true; while(!q.empty()) { int temp=q.front(); cout<<temp<<" "; q.pop_front(); for(auto itr=adj[temp].begin();itr!=adj[temp].end();itr++) { if(visited[*itr]==false) { visited[*itr]=true; q.push_back(*itr); } } } cout<<endl; } int DFSmain(int v,vector<bool> visited,list<int> adj[],map<int,vector<int>> &m,int count) { if(!visited[v]) { cout<<v<<" "; m[v].push_back(count); } visited[v]=true; for(auto itr=adj[v].begin();itr!=adj[v].end();itr++) { if(visited[*itr]==false) { count=DFSmain(*itr,visited,adj,m,++count); } } m[v].push_back(++count); return count; } void graph::DFS(int v) { int count=0; vector<bool> visited(V,false); for(int i=0;i<V;i++) { if(visited[i]==false) {count=DFSmain(i,visited,adj,m,count); count++; } } } int graph::checkbipartite() { int count=0; vector<int> visited(V,false),color(V,-1); deque<int> q; q.push_back(1); color[1]=1; while(!q.empty()) { int temp=q.front(); q.pop_front(); for(auto itr=adj[temp].begin();itr!=adj[temp].end();itr++) { if(visited[*itr]==false) { visited[*itr]=true; q.push_back(*itr); color[*itr]=1-color[temp]; } else if(color[*itr]==color[temp]) return 0; } } return 1; } bool graph::kahns_topo(vector<int> &topo) { vector<int> indegree(V,0); deque<int> q; for(int i=0;i<V;i++) { for(auto itr=adj[i].begin();itr!=adj[i].end();itr++) { indegree[*itr]++; } } for(int i=0;i<V;i++) { if(indegree[i]==0) q.push_back(i); } while(q.empty()==0) { int temp=q.front(); q.pop_front(); topo.push_back(temp); for(auto itr=adj[temp].begin();itr!=adj[temp].end();itr++) { if(--indegree[*itr]==0) q.push_back(*itr); } } for(int i=0;i<V;i++) { if(indegree[i]>0) return false; } return true; } void totalpaths(int v,int m,deque<int> q,vector<bool> visited,list<int> *adj,int parent) { q.push_back(parent); int temp=q.back(); visited[temp]=true; if(temp==v) { //if(q.size()==m+1) {printq(q); return; } //else // return; } for(auto itr=adj[temp].begin();itr!=adj[temp].end();itr++) { if(visited[*itr]==false) { totalpaths(v,m,q,visited,adj,*itr); } } } void graph::paths_givenedges(int u,int v,int m) { deque<int> q; vector<bool> visited(V,false); totalpaths(v,m,q,visited,adj,u); } bool graph::iscyclic(int temp,vector<bool> visited) { visited[temp]=true; for(auto itr=adj[temp].begin();itr!=adj[temp].end();itr++) { if(visited[*itr]==false) { if(iscyclic(*itr,visited)); else return false; } else return false; } return true; } struct block { int x; int y; int dist=0; }; struct block* block(int x,int y,int dist) { struct block*temp=new struct block; temp->x=x; temp->y=y; temp->dist=dist; return temp; }; int chessknightmain(int src,int des,vector<bool> visited,int res,int &ans,list<int> *adj) { if(src==des) { if(res<ans) { ans=res; } return ans; } visited[src]=true; for(auto itr=adj[src].begin();itr!=adj[src].end();itr++) { if(visited[*itr]==false) { chessknightmain(*itr,des,visited,res+1,ans,adj); } } return ans; } int chessknight(struct block*src,struct block*des) { deque<struct block*> q; q.push_back(src); int row[] = { 2, 2, -2, -2, 1, 1, -1, -1 }; int col[] = { -1, 1, 1, -1, 2, -2, 2, -2 }; set<struct block*> visited; struct block*temp=NULL; while(!q.empty()) { temp=q.front(); q.pop_front(); if(temp->x==des->x&&temp->y==des->y) break; if(visited.count(temp)==0) { visited.insert(temp); for(int i=0;i<8;i++) { int x1=temp->x+row[i]; int y1=temp->y+col[i]; if((x1>=0&&y1>=0)&&(x1<8&&y1<8)) { struct block*t=block(x1,y1,temp->dist+1); q.push_back(t); } } } } return temp->dist; } class disjointset { unordered_map<int,int> parent; public: void makeset(vector<int>&v) { for(int i=0;i<v.size();i++) parent[i]=i; } int findrep(int k) { if(parent[k]==k) return k; parent[k]=findrep(parent[k]); return parent[k]; } void unionset(int a,int b) { int x=findrep(a); int y=findrep(b); if(x==y); else parent[x]=y; } }; class edge { int x,y,w; public: edge(int x,int y,int w) { this->x=x; this->y=y; this->w=w; } edge(int x,int y) { this->x=x; this->y=y; } int get_w() { return w; } int get_x() {return x;} int get_y() {return y;} }; bool compare_edges(edge a,edge b) { if(a.get_w()<=b.get_w()) return true; else return false; } void kruskalalgo(vector<edge> edges) { int N=INT_MIN; for(int i=0;i<edges.size();i++) { if(max(edges[i].get_x(),edges[i].get_y())>N) N=max(edges[i].get_x(),edges[i].get_y()); } graph g(N+1); vector<int> universe; for(int i=0;i<N+1;i++) universe.push_back(i); disjointset ds; ds.makeset(universe); for(int i=0,j=1;i<edges.size()&&j<N+1;i++) { int p=ds.findrep(edges[i].get_x()); int q=ds.findrep(edges[i].get_y()); if(p==q); else { ds.unionset(edges[i].get_x(),edges[i].get_y()); g.addEdge(edges[i].get_x(),edges[i].get_y()); j++; } } for(int i=0;i<N+1;i++) { for(auto itr=g.get_adj()[i].begin();itr!=g.get_adj()[i].end();itr++) { cout<<i<<" "<<*itr<<endl; } } } class vertex { int n,value; deque<int> q; public: vertex() { } vertex(int n) { this->n=n; value=INT_MAX; } void set_value(int k) { value=k; } int get_n() { return n; } int get_value() { return value; } void makequeue(deque<int> k,int m) { q=k; q.push_back(m); } deque<int> get_queue() { return q; } }; bool operator<(vertex v1,vertex v2) { if(v1.get_value()<=v2.get_value()) return true; else return false; } void dijkstra_algo(vector<edge> edges,int V) { priority_queue<vertex> p; vector<vertex> vertices(V); for(int i=0;i<V;i++) vertices[i]=vertex(i); list<edge> *adj=NULL; adj=new list<edge> [V]; for(int i=0;i<edges.size();i++) { adj[edges[i].get_x()].push_back(edges[i]); } vertices[0].set_value(0); p.push(vertices[0]); vector<bool> visited(V,false); while(!p.empty()) { vertex *v=new vertex(); *v=p.top(); visited[v->get_n()]=true; for(auto itr=adj[v->get_n()].begin();itr!=adj[v->get_n()].end();itr++) { if(!visited[itr->get_y()]&&(v->get_value()+itr->get_w()<vertices[itr->get_y()].get_value())) { vertices[itr->get_y()].set_value(v->get_value()+itr->get_w()); vertices[itr->get_y()].makequeue(vertices[itr->get_x()].get_queue(),itr->get_y()); p.push(vertices[itr->get_y()]); } } p.pop(); } for(int i=0;i<V;i++) {cout<<"cost"<<i<<" "<<vertices[i].get_value()<<endl; cout<<"path"<<0<<" ";printq(vertices[i].get_queue()); } } void pushq(int i,int j,int k,vector<int> **path) { vector<int> v1=path[i][k]; vector<int> v2=path[k][j]; for(auto itr=v1.begin();itr!=v1.end();itr++) { path[i][j].push_back(*itr); } path[i][j].push_back(k); for(auto itr=v2.begin();itr!=v2.end();itr++) { path[i][j].push_back(*itr); } } void floyd_warshell(vector<edge> edges,int V) { int dist[V][V]; for(int i=0;i<V;i++) { for(int j=0;j<V;j++) { if(i==j) dist[i][j]=0; else dist[i][j]=INT_MAX; } } int x,y,w; for(int i=0;i<edges.size();i++) { x=edges[i].get_x(); y=edges[i].get_y(); w=edges[i].get_w(); dist[x][y]=w; } vector<int> **path; path=new vector<int> *[V]; for(int i=0;i<V;i++) path[i]=new vector<int>[V]; for(int k=0;k<=V-1;k++) { for(int i=0;i<=V-1;i++) { for(int j=0;j<=V-1;j++) { if(dist[i][k]!=INT_MAX&&dist[k][j]!=INT_MAX&&dist[i][k]+dist[k][j]<dist[i][j]) { dist[i][j]=dist[i][k]+dist[k][j]; pushq(i,j,k,path); } } if (dist[i][i] < 0) { cout << "Negative Weight Cycle Found!!"; return; } } } for(int i=0;i<V;i++) { for(int j=0;j<V;j++) { cout<<"distance bw"<<i<<" "<<j<<":"<<dist[i][j]<<"!!"<<"path:"<<i<<"->"; for(int k=0;k<path[i][j].size();k++) cout<<path[i][j][k]<<"->"; cout<<j<<endl; } } } bool is_safe(int v,int c,list<int> *adj,vector<int> color) { for(auto itr=adj[v].begin();itr!=adj[v].end();itr++) { if(color[*itr]==c) return false; } return true; } void kcolorable(list<int> *adj,int v,int k,int V,vector<int> color,string COLORS[],int &flag) { if(v==V) { for(int i=0;i<V;i++) { cout<<COLORS[color[i]]<<" "; } cout<<endl; flag=1; return; } for(int c=1;c<=k;c++) { if(is_safe(v,c,adj,color)) { color[v]=c; kcolorable(adj,v+1,k,V,color,COLORS,flag); } } if(flag==0) { cout<<"no formation found"<<endl; flag=1; } } void hamiltonian(deque<int> q,list<int> *adj,int v,int V,vector<bool> visited) { visited[v]=true; q.push_back(v); if(q.size()==V) { printq(q); return; } for(auto itr=adj[v].begin();itr!=adj[v].end();itr++) { if(visited[*itr]==false) { hamiltonian(q,adj,*itr,V,visited); } } } void greedy_coloring(vector<edge> edges,int V) { graph g(V); for(int i=0;i<edges.size();i++) g.addEdge(edges[i].get_x(),edges[i].get_y()); string color[] = { "", "BLUE", "GREEN", "RED", "YELLOW", "ORANGE", "PINK", "BLACK", "BROWN", "WHITE", "PURPLE", "VOILET" }; vector<int> result(V,0); for(int v=0;v<V;v++) { int c=1; while(is_safe(v,c,g.get_adj(),result)==false) { c++; } result[v]=c; } for(int i=0;i<V;i++) cout<<"color for vertex "<<i<<" is "<<color[result[i]]<<endl; } bool isPrime(int n) { // Corner case if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } // C++ program to print all prime factors // A function to print all prime // factors of a given number n // C++ program to print all prime factors // A function to print all prime // factors of a given number n int primeFactors(long long int n) { int re=0; if(n==1) return re; else if(n==2) return re; // Print the number of 2s that divide n while (n % 2 == 0) { n = n/2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (long long int i = 3; i <= sqrt(n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { if(i%2!=0) { re=1; return re; } n = n/i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) { if(n%2!=0) re=1; } return re; } void primeFactorstemp(int n,map<int,int> m) { // Print the number of 2s that divide n while (n % 2 == 0) { cout<<m[2]<<" "; return ; n = n/2; } // n must be odd at this point. So we can skip // one element (Note i = i +2) for (int i = 3; i <= sqrt(n); i = i + 2) { // While i divides n, print i and divide n while (n % i == 0) { if(m[i]!=0) {cout<<m[i]<<" "; return; } n = n/i; } } // This condition is to handle the case when n // is a prime number greater than 2 if (n > 2) { if(m[n]!=0) { cout<<m[n]<<" "; return; } } } long long int getsumdigits(long long int i) { long long int sum=0; while(i>0) { sum+=(i%10); i/=10; } return sum; } /*int graph::mothervertex() { vector<bool> visited(V,false); int v; for(int i=0;i<V;i++) { if(visited[i]==false) { DFSmain(i,visited,adj); v=i; } } for(int i=0;i<V;i++) visited[i]=false; DFSmain(v,visited,adj); if(count(visited.begin(),visited.end(),true)!=visited.size()) return -1; else return v; } */ bool bool_sorted(int a,int b) { return a>=b; } bool solve(vector<int> v,int in,int fi,int n) { if(in==fi) return true; else { if(in<fi) { for(int i=in;i>0;i--) { if(v[i]!=v[i-1]-1) return false; } for(int i=fi;i<n-1;i++) { if(v[i]!=v[i+1]+1) return false; } if(in+1==fi) return true; for(int i=in;i<fi;i++) { if(v[i]+1!=v[i+1]) return false; } } else { for(int i=0;i<n/2;i++) { swap(v[i],v[n-i-1]); } in=n-in-1; fi=n-fi-1; for(int i=in;i>0;i--) { if(v[i]!=v[i-1]-1) return false; } for(int i=fi;i<n-1;i++) { if(v[i]!=v[i+1]+1) return false; } if(in+1==fi) return true; for(int i=in;i<fi;i++) { if(v[i]+1!=v[i+1]) return false; } } } return true; } int main() { int t; cin>>t; while(t--) { int a,b,flag=1,oa,ob; cin>>a>>b; oa=a; ob=b; string s; cin>>s; int n=s.length(),r=n/2-1; for(int i=0;i<n;i++) { if(s[i]=='0') a--; else if(s[i]=='1') b--; } for(int i=0;i<=(r);i++) { if(s[i]!='?'&&s[n-i-1]!='?'&&s[i]!=s[n-i-1]) { flag=0; break; } else { if(s[i]=='?') { if(s[n-i-1]=='0') { if(a==0) { flag=0; break; } a--; s[i]='0'; } else if(s[n-i-1]=='1') { if(b==0) { flag=0; break; } b--; s[i]='1'; } } else if(s[n-i-1]=='?') { if(s[i]=='0') { if(a==0) { flag=0; break; } a--; s[n-i-1]='0'; } else if(s[i]=='1') { if(b==0) { flag=0; break; } b--; s[n-i-1]='1'; } } } } for(int i=0;i<=(r);i++) { if(s[i]=='?') { if(a>=2) { s[i]=s[n-i-1]='0'; a-=2; } else if(b>=2) { s[i]=s[n-i-1]='1'; b-=2; } else { flag=0; break; } } } if(n%2!=0) { if(s[n/2]=='?') { if(a) s[n/2]='0'; else if(b) s[n/2]='1'; else { flag=0; } } } if(!flag||(count(s.begin(),s.end(),'0')!=oa)||(count(s.begin(),s.end(),'1')!=ob)) cout<<-1<<endl; else if(n==1) { if(s[0]=='?') { if(a) cout<<"0"<<endl; else cout<<"1"<<endl; } else cout<<s<<endl; } else cout<<s<<endl; } return 0; }
1,200
CPP
n,k=map(int,input().split()) a=list(map(int,input().split())) if (max(a) - min(a)) > k: print("NO") else: print("YES") for i in range(n): s = [j for j in range(1, k+1)] if a[i] <= k: print(*s[:a[i]]) else: for j in range(a[i]//k): print(*s, end = " ") print(*s[:(a[i]%k)])
1,300
PYTHON3
import sys import math import itertools import collections def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return '\n'.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) n = ii() s = input() pos = i = 0 while i < n - 1: if s[i] > s[i + 1]: pos = i break i += 1 if pos == 0 and i == n - 1: print(s[:n - 1]) elif pos == 0 and i == 0: print(s[1:]) else: print(s[:pos] + s[pos + 1:])
1,200
PYTHON3
n = int(input()) p = [] for _ in range(n): x,h = map(int, input().split()) p.append((x,h)) r = 0 l = -10**12 for i in range(len(p)-1): left = p[i][0] - p[i][1] right = p[i][0] + p[i][1] if left > l: l = p[i][0] r += 1 elif right < p[i+1][0]: l = right r += 1 else: l = p[i][0] print(r+1)
1,500
PYTHON3
num=input() n=str(input()) c=n.lower() c=set(c) if len(c)==26: print("YES") else: print("NO")
800
PYTHON3
# B. Proper Nutrition n=int(input()) a=int(input()) b=int(input()) for i in range(n//2+1): if i%a==0 and (n-i)%b==0: print("YES") print(i//a,(n-i)//b) break elif i%b==0 and (n-i)%a==0: print("YES") print(((n-i)//a),i//b) break else: print("NO")
1,100
PYTHON3
gems = {'purple':'Power', 'green':'Time', 'blue':'Space', 'orange':'Soul','red':'Reality','yellow':'Mind' } N = int(input()) arr = [] for i in range(N): del gems[input()] print(len(gems)) for cols in gems: print(gems[cols])
800
PYTHON3
import sys num = sys.stdin.readline().strip() data = [] try: while True: line = sys.stdin.readline().strip() if line == '': break lines = list(map(int, line.split())) data.append(lines) except: pass # print(data) # data = [[5, 3, 4], [2, 1, 0], [3, 5, 5], [4, 10, 6]] # data = [[66900898, 50939214, 10104600]] # count = 0 # # for pair in data: # tmp_count = 0 # strength, intelligence, exp = pair # for i in range(exp, -1, -1): # tmp_strength = strength + i # tmp_int = intelligence + (exp - i) # if tmp_strength > tmp_int: # tmp_count += 1 # if tmp_strength - tmp_int <= 2: # break # print(tmp_count) for pair in data: str_, int_, exp = pair if exp == 0: if str_ > int_: count = 1 else: count = 0 elif exp == 1: if str_ > int_ + 1: count = 2 elif str_ == int_ + 1 or str_ == int_: count = 1 else: count = 0 else: diff = str_ - int_ if diff > exp: count = exp + 1 else: count = ((str_ - int_) + exp + 1) // 2 if count < 0: count = 0 print(count)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MX = (int)(1e5) + 5; int chcnt[26]; vector<int> graph[MX]; template <typename T> T GCD(T a, T b) { if (b == 0) return a; else return GCD(b, a % b); } void ipgraph(int n, int m) { int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; graph[u].push_back(v); graph[v].push_back(u); } } void solve() { int n, k; cin >> n >> k; string s; cin >> s; memset(chcnt, 0, sizeof(chcnt)); for (int i = 0; i < s.length(); i++) { chcnt[s[i] - 'a']++; } sort(s.begin(), s.end()); if (k == 1) cout << s << "\n"; else { if (s[0] == s[n - 1]) { string ans = string(n / k, s[0]); if (n % k != 0) ans += s[0]; cout << ans << "\n"; return; } if (s[0] != s[k - 1]) { cout << s[k - 1] << "\n"; } else { int cnt = 0; for (int i = 0; i < 26; i++) { if (chcnt[i] > 0) cnt++; } if (cnt == 2) { string ans; ans += s[0]; if (chcnt[s[0] - 'a'] - k > 0) { ans += s.substr(k); } else { ans += string(chcnt[s[k] - 'a'] / k, s[k]); if (chcnt[s[k] - 'a'] % k != 0) { ans += s[k]; } } cout << ans << "\n"; } else { string ans; ans += s[0]; ans += s.substr(k); cout << ans << "\n"; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(); int t; cin >> t; while (t--) { solve(); } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n / 2 + 1 << endl; int i = 1, j = 1; for (int l = 0; l < n; l++) { cout << i << ' ' << j << '\n'; if (i == j) j++; else i++; } return 0; }
1,100
CPP
n, m = map(int,input().split()) l = [[0]*(n) for i in range(n)] j = [-1]*n j[0] = 0 k = [0] for i in range(m): u,v = map(int,input().split()) l[u-1][v-1] = l[v-1][u-1] = 1 while k: u = k.pop(0) for i in range(n): if l[u][i] != l[0][n-1] and j[i] == -1: j[i] = j[u] + 1 k.append(i) print(j[n-1])
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, ptr; bool mark[100010]; bool bad[100010]; int ord[100010]; vector<int> dir[100010]; vector<int> rev[100010]; vector<int> adj[100010]; bool dfs(int idx) { bool ret = bad[idx]; mark[idx] = true; for (int i = 0; i < adj[idx].size(); i++) if (!mark[adj[idx][i]]) ret |= dfs(adj[idx][i]); return ret; } void setOrd(int idx) { mark[idx] = true; for (int i = 0; i < dir[idx].size(); i++) if (!mark[dir[idx][i]]) setOrd(dir[idx][i]); ord[--ptr] = idx; } int ccf(int idx) { int cnt = 1; mark[idx] = true; for (int i = 0; i < rev[idx].size(); i++) if (!mark[rev[idx][i]]) cnt += ccf(rev[idx][i]); return cnt; } int main() { cin >> n >> m; ptr = n; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--, b--; dir[a].push_back(b); rev[b].push_back(a); adj[a].push_back(b); adj[b].push_back(a); } int res = n; for (int i = 0; i < n; i++) if (!mark[i]) setOrd(i); memset(mark, false, sizeof mark); for (int i = 0; i < n; i++) { if (!mark[ord[i]]) { int tmp = ccf(ord[i]); if (tmp > 1) bad[ord[i]] = true; } } memset(mark, false, sizeof mark); for (int i = 0; i < n; i++) if (!mark[i] && !dfs(i)) res--; cout << res << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int a[11111], b[11111], dp[4111][4111]; int n, l = 0, m, x, y, ans; void dfs(int i) { int j; for (j = 1; j <= n; j++) if (dp[i][j] || dp[j][i]) { ans += dp[i][j]; dp[i][j] = dp[j][i] = 0; dfs(j); } } int main() { int n1 = 0, n2 = 0, y, x, j, i, z = 0; cin >> n; int t = 0; for (i = 1; i <= n; i++) { cin >> x >> y >> z; dp[x][y] = z; t += z; } ans = 0; dfs(1); cout << min(ans, t - ans); }
1,400
CPP
word=input() lsit1=list(word) a=len(lsit1) n=0 for i in range(a): if lsit1[i].islower()==True: n+=1 if n>=(a-n): lsit2=''.join(lsit1) lsit3=lsit2.lower() print(lsit3) else: lsit2=''.join(lsit1) lsit3=lsit2.upper() print(lsit3)
800
PYTHON3
#include <bits/stdc++.h> const int N = 12500; using namespace std; int n; double vp, vs; double len; struct pt { double x; double y; double z; }; pt tr[N]; pt orig; double get_dist(pt a, pt b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); } pt eval(pt a, pt b, double rat) { a.x += (b.x - a.x) * rat; a.y += (b.y - a.y) * rat; a.z += (b.z - a.z) * rat; return a; } pt get_point(double need) { for (int i = 0; i < n; i++) { double D = get_dist(tr[i], tr[i + 1]); if (D < need) { need -= D; continue; } double rat = need / D; return eval(tr[i], tr[i + 1], rat); } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i <= n; i++) { cin >> tr[i].x >> tr[i].y >> tr[i].z; } cin >> vp >> vs; cin >> orig.x >> orig.y >> orig.z; len = 0; for (int i = 1; i <= n; i++) len += get_dist(tr[i - 1], tr[i]); double d1 = get_dist(orig, tr[n]); if (d1 / vp > len / vs + 1e-14) { cout << "NO" << endl; return 0; } double l, r; l = 0; r = len; for (int iter = 1; iter <= 70; iter++) { double mid = l + r; mid /= 2; pt P = get_point(mid); double time1 = mid / vs; double time2 = get_dist(orig, P) / vp; if (time2 > time1 + 1e-14) l = mid; else r = mid; } cout.precision(10); cout << "YES" << endl; cout << fixed << l / vs << endl; pt P = get_point(l); cout << P.x << " " << P.y << " " << P.z << endl; return 0; }
2,100
CPP
n=int(input()) s=input() a=s.count("n") b=s.count("z") s1="1"*a+"0"*b l=list(s1) print(" ".join(map(str,l)))
800
PYTHON3
x = [] for i in range(5): n = list(map(str, input().split())) x = x+n s = "".join(x) b = s.find("1") a = b+1 if a ==13: print("0") elif a == 2 or a==6 or a==4 or a==10 or a==16 or a==20 or a==22 or a==24: print("3") elif a == 1 or a==5 or a==21 or a==25: print("4") elif a == 8 or a == 12 or a==14 or a==18: print("1") else: print("2")
800
PYTHON3
def match(num): if num==2: print(2) elif num%2==0: print(0) else: print(1) n=int(input()) for i in range(n): match(int(input()))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long A, B, C; long long X1, Y1, X2, Y2; double x_intersect(double y) { return -(B * y + C) / A; } double y_intersect(double x) { return -(A * x + C) / B; } double euclidean(double x1, double y1, double x2, double y2) { double dx = x1 - x2; double dy = y1 - y2; return sqrt(dx * dx + dy * dy); } int main() { cin >> A >> B >> C; cin >> X1 >> Y1 >> X2 >> Y2; double manhattan = abs(X1 - X2) + abs(Y1 - Y2); if (A == 0 || B == 0) { printf("%.12lf\n", manhattan); return 0; } double answer = manhattan; double x1 = x_intersect(Y1); double y1 = y_intersect(X1); double x2 = x_intersect(Y2); double y2 = y_intersect(X2); answer = min(answer, abs(X1 - x1) + abs(X2 - x2) + euclidean(x1, Y1, x2, Y2)); answer = min(answer, abs(X1 - x1) + abs(Y2 - y2) + euclidean(x1, Y1, X2, y2)); answer = min(answer, abs(Y1 - y1) + abs(X2 - x2) + euclidean(X1, y1, x2, Y2)); answer = min(answer, abs(Y1 - y1) + abs(Y2 - y2) + euclidean(X1, y1, X2, y2)); printf("%.12lf\n", answer); }
1,900
CPP
#include <bits/stdc++.h> int main(void) { long long int arr[100005]; long long int ans[100005]; long long int key[300][2]; long long int n, k, i, j, pos; scanf("%lli %lli", &n, &k); for (i = 0; i <= 255; i++) { key[i][0] = -1; key[i][1] = 0; } for (i = 0; i < n; i++) { scanf("%lli", &arr[i]); if (key[arr[i]][0] == -1) { pos = ((arr[i] - k + 1) > 0) ? arr[i] - k + 1 : 0; while (key[pos][0] != -1) { pos++; } if (pos > 0 && key[pos - 1][1] != 0 && key[pos - 1][1] + arr[i] - pos + 1 <= k) { for (j = pos; j <= arr[i]; j++) { key[j][0] = key[pos - 1][0]; key[j][1] = key[pos - 1][1] + arr[i] - pos + 1; } } else { for (j = pos; j <= arr[i]; j++) { key[j][0] = pos; key[j][1] = arr[i] - pos + 1; } } } ans[i] = key[arr[i]][0]; } for (i = 0; i < n; i++) { printf("%lli ", ans[i]); } }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; int cnt_neg = 0, cnt_pos = 0; int arr[maxn << 1]; vector<int> faster[maxn]; bool out[maxn << 1]; int main() { int n; scanf("%d", &n); for (int e = 1; e <= n; e++) scanf("%d", arr + e), arr[e + n] = arr[e]; int hi = 2 * n, lo = n + 1, nowlo = n; long long sum = 0; for (int cur = hi; cur >= lo; cur--) { if (arr[cur] <= nowlo) cnt_pos++; else cnt_neg++, faster[arr[cur] - nowlo].push_back(cur); sum += abs(arr[cur] - nowlo); nowlo--; } long long ans = sum, shift = 0, state = 0; for (; lo > 1;) { if (arr[hi] <= n) cnt_pos--; else cnt_neg--; sum -= abs(arr[hi] - n); out[hi] = 1; shift++; sum += -cnt_neg + cnt_pos; sum += abs(arr[lo - 1] - 1); if (sum < ans) ans = sum, state = shift; if (arr[lo - 1] > 1) { cnt_neg++; if (shift + arr[lo - 1] - 1 < n) faster[shift + arr[lo - 1] - 1].push_back(lo - 1); } else cnt_pos++; while (!faster[shift].empty()) { if (!out[faster[shift].back()]) cnt_neg--, cnt_pos++; faster[shift].pop_back(); } lo--; hi--; } printf("%lld %lld\n", ans, state); return 0; }
1,900
CPP
out,inte,lst = [], [], [] n, u_b = map(int,input().split(" ")) for i in range (n): a,b = map(int,input().split(" ")) while a<=b: lst.append(a) a+=1 for j in range(1,u_b+1): inte.append(j) for k in inte: if k not in lst: out.append(k) print(len(out)) for x in out: print(x,end=" ")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int LOG = 10; int n, q, a[MAXN]; long long weed[MAXN * 4][LOG]; int nut[MAXN * 4][LOG], gg[1 << LOG]; bool changed[MAXN * 4]; void plant(int v, int b, int e) { for (int w = 0; w < LOG; w++) nut[v][w] = 1 << w; if (e - b == 1) { int cur = 1; while (a[b]) { int x = a[b] % 10; weed[v][x] += cur; a[b] /= 10; cur *= 10; } return; } int mid = b + e >> 1; plant(v << 1, b, mid); plant(v << 1 ^ 1, mid, e); for (int w = 0; w < LOG; w++) weed[v][w] = weed[v << 1][w] + weed[v << 1 ^ 1][w]; } long long temp[2][LOG]; int t2[2][LOG]; void absorb(int v) { if (!changed[v]) return; memset(temp, 0, sizeof(temp)); memset(t2, 0, sizeof(t2)); for (int w = 0; w < LOG; w++) while (nut[v][w]) { int x = gg[nut[v][w]]; nut[v][w] ^= 1 << x; temp[0][w] += weed[v << 1][x]; temp[1][w] += weed[v << 1 ^ 1][x]; t2[0][w] |= nut[v << 1][x]; t2[1][w] |= nut[v << 1 ^ 1][x]; } changed[v << 1] = changed[v << 1 ^ 1] = 1; for (int w = 0; w < LOG; w++) { nut[v << 1][w] = t2[0][w]; nut[v << 1 ^ 1][w] = t2[1][w]; weed[v << 1][w] = temp[0][w]; weed[v << 1 ^ 1][w] = temp[1][w]; } for (int w = 0; w < LOG; w++) nut[v][w] = 1 << w; changed[v] = 0; } void water(int v, int b, int e, int l, int r, int x, int y) { if (l <= b && e <= r) { weed[v][y] += weed[v][x]; weed[v][x] = 0; nut[v][y] |= nut[v][x]; nut[v][x] = 0; changed[v] = 1; return; } if (r <= b || e <= l) return; int mid = b + e >> 1; absorb(v); water(v << 1, b, mid, l, r, x, y); water(v << 1 ^ 1, mid, e, l, r, x, y); for (int w = 0; w < LOG; w++) weed[v][w] = weed[v << 1][w] + weed[v << 1 ^ 1][w]; } long long smoke(int v, int b, int e, int l, int r) { if (l <= b && e <= r) { long long ret = 0; for (int w = 1; w < LOG; w++) ret += 1ll * w * weed[v][w]; return ret; } if (r <= b || e <= l) return 0ll; int mid = b + e >> 1; absorb(v); return smoke(v << 1, b, mid, l, r) + smoke(v << 1 ^ 1, mid, e, l, r); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int i = 0; i < n; i++) cin >> a[i]; plant(1, 0, n); for (int i = 0; i < 1 << LOG; i++) gg[i] = __builtin_ctz(i); while (q--) { int t, l, r; cin >> t >> l >> r, l--; if (t == 1) { int x, y; cin >> x >> y; if (x ^ y) water(1, 0, n, l, r, x, y); } else cout << smoke(1, 0, n, l, r) << "\n"; } return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long mod2 = 998244353; long long inf = 1000000000000000000; double pi = 2 * acos(0); int dh[4] = {1, -1, 0, 0}; int dw[4] = {0, 0, 1, -1}; int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long lmax(long long a, long long b) { if (a < b) return b; else return a; } long long lmin(long long a, long long b) { if (a < b) return a; else return b; } long long gya[200010]; long long kai[200010]; long long beki(long long n, long long k) { long long ret = 1; long long now = n; while (k > 0) { if (k % 2 == 1) { ret *= now; ret %= mod2; } now *= now; now %= mod2; k /= 2; } return ret; } long long gyaku(long long n) { return beki(n, mod2 - 2); } void nckinit(long long n) { kai[0] = 1; kai[1] = 1; for (int i = 2; i <= n; i++) { kai[i] = kai[i - 1] * i; kai[i] %= mod; } gya[n] = gyaku(kai[n]); for (int i = n - 1; i >= 1; i--) { gya[i] = gya[i + 1] * (i + 1); gya[i] %= mod; } gya[0] = 1; } long long nck(long long n, long long k) { if (k < 0) return 0; if (k == 0 || n == k) return 1; long long ret = kai[n]; ret *= gya[n - k]; ret %= mod; ret *= gya[k]; ret %= mod; return ret; } long long npk(long long n, long long k) { if (k < 0) return 0; if (k == 0) return 1; long long ret = kai[n]; ret *= gya[n - k]; ret %= mod; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long a[n]; long long cnt[11]; fill(cnt, cnt + 10 + 1, 0); long long ke[n]; vector<long long> v[n]; for (int i = 0; i < n; i++) { cin >> a[i]; long long u = a[i]; long long s = 0; while (u > 0) { s++; v[i].push_back(u % 10); u /= 10; } ke[i] = s; cnt[s]++; } long long ans = 0; for (int i = 0; i < n; i++) { long long k = ke[i]; for (int j = 1; j < 11; j++) { long long now = 10; if (j < k) { for (int l = 0; l < j; l++) { ans += (v[i][l] * now) % mod2 * cnt[j]; ans %= mod2; now *= 100; now %= mod2; } now *= gyaku(10); now %= mod2; for (int l = j; l < k; l++) { ans += (v[i][l] * cnt[j]) % mod2 * now; ans %= mod2; now *= 10; now %= mod2; } } else { for (int l = 0; l < k; l++) { ans += (v[i][l] * cnt[j]) % mod2 * now; ans %= mod2; now *= 100; now %= mod2; } } now = 1; if (j < k) { for (int l = 0; l < j; l++) { ans += (v[i][l] * cnt[j]) % mod2 * now; ans %= mod2; now *= 100; now %= mod2; } for (int l = j; l < k; l++) { ans += (v[i][l] * cnt[j]) % mod2 * now; ans %= mod2; now *= 10; now %= mod2; } } else { for (int l = 0; l < k; l++) { ans += (v[i][l] * cnt[j]) % mod2 * now; ans %= mod2; now *= 100; now %= mod2; } } } } cout << ans << endl; }
1,500
CPP
import string n = int(input()) for i in range(0, n): s = input() if len(s) > 10: ans = s[0] + str(len(s) - 2) + s[len(s) - 1] print(ans) else: print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long N, T; int L; long long DP[64][64][2]; int inp[64]; long long rec(int pos, int remOne, int flag) { if (pos == L) { if (remOne == 0) return 1LL; else return 0LL; } if (DP[pos][remOne][flag] != -1) return DP[pos][remOne][flag]; long long ans = 0; int newFlag = flag; if (inp[pos] == 1) newFlag = 1; ans += rec(pos + 1, remOne, newFlag); if (remOne >= 1) { if (inp[pos] == 1 || flag) ans += rec(pos + 1, remOne - 1, flag); } return DP[pos][remOne][flag] = ans; } int main() { cin >> N >> T; long long temp = T; int power = 0; while (temp > 1) { if (temp % 2 != 0) { cout << 0 << endl; return 0; } power++; temp /= 2; } power++; memset(DP, -1, sizeof(DP)); temp = N + 1; L = 0; while (temp > 0) { inp[L++] = temp % 2; temp /= 2; } reverse(inp, inp + L); long long ans = rec(0, power, 0); if (power == 1) ans--; cout << ans << endl; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, A[1000009]; vector<long long> v[200009]; int main() { long long tc = 1, t = 0, i, j, k, a, b, c, d, s; while (t++ < tc) { cin >> n >> m; for (i = 1; i <= n; i++) { cin >> A[i]; k = 0; while (A[i]) { v[A[i]].push_back(k); k++; A[i] /= 2; } } long long ans = LLONG_MAX; for (i = 0; i < 200009; i++) { if (v[i].size() >= m) { sort(v[i].begin(), v[i].end()); long long cur = 0; for (j = 0; j < m; j++) cur += v[i][j]; ans = min(ans, cur); } } cout << ans; } return 0; }
2,200
CPP
x = list(map(int,input().split())) min = x[0] max = x[0] for i in range (len(x)): if min < x[i]: min = x[i] else: min = min if max > x[i]: max = x[i] else: max = max print(min - max)
800
PYTHON3
ar=[] ar2=[] a=0 for i in range(0,3): ar.append(list(map(int,input().rstrip().split()))) for i in range(0,3): for j in range(0,3): if i==0 and j==0: if (ar[i][j]+ar[i+1][j]+ar[i][j+1])%2==0: ar2.append(1) else: ar2.append(0) elif 0<i<2 and j==0: if (ar[i][j]+ar[i+1][j]+ar[i][j+1]+ar[i-1][j])%2==0: ar2.append(1) else: ar2.append(0) elif i==0 and 0<j<2: if (ar[i][j] + ar[i+1][j] + ar[i][j+1] + ar[i][j-1]) % 2 == 0: ar2.append(1) else: ar2.append(0) elif 0<i<2 and 0<j<2: if (ar[i][j]+ar[i+1][j]+ar[i][j+1]+ar[i-1][j]+ar[i][j-1])%2==0: ar2.append(1) else: ar2.append(0) elif i==2 and j==2: if (ar[i][j]+ar[i-1][j]+ar[i][j-1])%2==0: ar2.append(1) else: ar2.append(0) elif 0<i<2 and j==2: if (ar[i][j]+ar[i+1][j]+ar[i-1][j]+ar[i][j-1])%2==0: ar2.append(1) else: ar2.append(0) elif i==2 and 0<j<2: if (ar[i][j]+ar[i][j+1]+ar[i-1][j]+ar[i][j-1])%2==0: ar2.append(1) else: ar2.append(0) elif 0<i<2 and 0<j<2: if (ar[i][j]+ar[i+1][j]+ar[i][j+1]+ar[i-1][j]+ar[i][j-1])%2==0: ar2.append(1) else: ar2.append(0) elif i==0 and j==2: if (ar[i][j] + ar[i + 1][j] + ar[i][j - 1]) % 2 == 0: ar2.append(1) else: ar2.append(0) elif i==2 and j==0: if (ar[i][j]+ ar[i][j + 1] + ar[i - 1][j]) % 2 == 0: ar2.append(1) else: ar2.append(0) for i in range(0,3): for j in range(0,3): if j==2: print(ar2[a],end="\n") a=a+1 elif j<3: print(ar2[a],end="") a = a + 1
900
PYTHON3
from collections import Counter n = int(input()) l = Counter([input()[0] for _ in range(n)]) ans = 0 C2 = lambda x: ((x**2-x)//2 if x > 0 else 0) for i in l: ans += C2(l[i]//2)*2 if not l[i] % 2 else (C2(l[i]//2)+C2(l[i]//2+1)) print(ans)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m, k, q; int x, y, a, b; vector<int> toy[maxn]; vector<int> G[maxn]; int indexx; int dfn[maxn], low[maxn]; bool hasfa[maxn]; int son[maxn]; void dfs(int x) { dfn[x] = ++indexx; son[x] = 1; for (int i = 0; i < (int)G[x].size(); i++) { int v = G[x][i]; dfs(v); son[x] += son[v]; } low[x] = indexx; } int main() { cin >> n >> m >> k >> q; while (k--) { scanf("%d%d", &a, &b); if ((int)toy[b].size()) hasfa[a] = true; toy[b].push_back(a); } for (int i = 1; i <= m; i++) for (int j = 1; j < (int)toy[i].size(); j++) G[toy[i][j - 1]].push_back(toy[i][j]); for (int i = 1; i <= n; i++) if (!hasfa[i]) dfs(i); while (q--) { scanf("%d%d", &x, &y); if ((int)toy[y].size()) { int st = toy[y][toy[y].size() - 1]; if (dfn[st] > dfn[x] && low[st] <= low[x]) printf("%d\n", son[x]); else printf("0\n"); } else printf("0\n"); } return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; long long a[400100]; map<int, int> m; int o[400100]; set<int> s; int sum[400100]; int get(int cr) { for (int i = sqrt(cr); i >= 1; i--) { if (cr % i == 0) { return i; } } } vector<pair<int, int> > k; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]]++; } map<int, int>::iterator it2; sort(a, a + n); for (it2 = m.begin(); it2 != m.end(); it2++) { int op = it2->second; k.push_back(make_pair(-(it2->second), it2->first)); for (int i = 1; i <= op; i++) o[i]++; } sort(k.begin(), k.end()); int kk = 0; for (int i = 0; i < n; i++) { k[kk].first *= -1; for (int j = i; j <= i + k[kk].first; j++) { a[j] = k[kk].second; } i += k[kk].first - 1; kk++; } for (int i = 1; i <= n; i++) { sum[i] = o[i] + sum[i - 1]; } int mx = 1; for (int i = 1; i <= n; i++) { int y = get(i); if (sum[y] >= i) { mx = i; } } int x = get(mx), y = mx / x; int cr = 0, cr2 = 0, second = 0, cr3 = 0, cr4 = 0, dd = 1; int ans[x + 3][y + 3]; while (second < mx) { second++; if (cr == x) { cr = 0; cr2 = dd; dd++; } cr2 %= y; if (cr3 > 0 && a[cr3] != a[cr3 - 1]) cr4 = 0; if (cr4 == x) { int pp = cr3; while (a[cr3] == a[pp]) cr3++; cr4 = 0; } ans[cr][cr2] = a[cr3]; cr4++; cr++; cr2++; cr3++; } cout << mx << endl << x << ' ' << y << endl; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) cout << ans[i][j] << ' '; cout << endl; } return 0; }
2,300
CPP
t = int(input()) for _ in range(t): n = int(input()) A = [int(x) for x in input().split()] A.sort() if max((A[i]-A[i-1] for i in range(1, n)), default=0) < 2: print('YES') else: print('NO')
800
PYTHON3
n=int(input()) for i in range(n): a,b=map(int,input().split()) k=0 if a<=b: print(1+(a-1)*a//2) else: print(b*(b+1)//2)
1,200
PYTHON3
#include <bits/stdc++.h> int main() { long long n, k, s; scanf("%lld %lld %lld", &n, &k, &s); if ((s < k) || ((n - 1) * k < s)) { puts("NO"); return 0; } puts("YES"); long long step = 1; while (k--) { long long rm = (s - k < n - 1) ? (s - k) : (n - 1); s -= rm; if (step + rm <= n) { step += rm; } else { step -= rm; } printf("%lld ", step); } puts(""); return 0; }
1,600
CPP
T = int(input()) for i in range(T): a,b = input().split() a,b = int(a) , int(b) if a > 3: print("YES") else: if a == 1: if b == 1: print("YES") else: print("NO") else: if b <= 3: print("YES") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; struct xy { int x, y, i; xy(int _x, int _y, int _i) : x(_x), y(_y), i(_i) {} }; int N, K; bool cmp0(xy a, xy b) { return make_tuple(a.y, a.x, a.i) > make_tuple(b.y, b.x, b.i); } bool cmp1(xy a, xy b) { return make_tuple(a.x, a.y, a.i) > make_tuple(b.x, b.y, b.i); } bool cmp2(xy a, xy b) { return make_tuple(a.y, a.x, a.i) < make_tuple(b.y, b.x, b.i); } bool cmp3(xy a, xy b) { return make_tuple(a.x, a.y, a.i) < make_tuple(b.x, b.y, b.i); } bool (*cmp[])(xy a, xy b) = {cmp0, cmp1, cmp2, cmp3}; vector<xy> v[4]; int main() { scanf("%d %d", &N, &K); for (int Ni = 0; Ni < N; Ni++) { int x[2], y[2]; for (int i = 0; i < 2; i++) scanf("%d %d", &x[i], &y[i]); xy p = xy((x[0] + x[1]), (y[0] + y[1]), Ni); for (int i = 0; i < 4; i++) v[i].push_back(p); } for (int i = 0; i < 4; i++) sort(v[i].begin(), v[i].end(), cmp[i]); long long ans = 1e18; for (int i = 0; i <= K; i++) for (int j = 0; i + j <= K; j++) for (int k = 0; i + j + k <= K; k++) for (int l = 0; i + j + k + l <= K; l++) { set<int> s; int _i = i, _j = j, _k = k, _l = l; int dx = 0, dy = 0; for (int f = 0; f < 4; f++) for (xy p : v[f]) if (s.find(p.i) == s.end()) { if (f == 0 && !_i--) { dy += p.y; break; } if (f == 1 && !_j--) { dx += p.x; break; } if (f == 2 && !_k--) { dy -= p.y; break; } if (f == 3 && !_l--) { dx -= p.x; break; } s.insert(p.i); } ans = min(ans, (long long)max(2, (dx + 1) / 2 * 2) * max(2, (dy + 1) / 2 * 2) / 4); } printf("%lld\n", ans); }
2,300
CPP
# m, n, l = map(int, input().split()) # m = list(map(int, input().split())) t = int(input()) for _ in range(t): s = input() if len(s)==1: if s[0]=="?" or s[0]=="b" or s[0]=="c": print("a") continue else: print("b") continue if "aa" in s or "bb" in s or "cc" in s: print(-1) else: s = list(s) ch = ["a","b","c"] d ={"a":1, "b":2, "c":3} if s[0]=="?": if s[1]=="?": s[0]=ch[0] else: s[0] = ch[(1+ ord(s[1]) -ord("a"))%3] if s[-1]=="?": if s[-2]=="?": s[-1]=ch[0] else: s[-1] = ch[(1+ord(s[-2])-ord("a"))%3] for i in range(1,len(s)-1): if s[i]=="?": if s[i-1]==s[i+1] or s[i+1]=="?": idx = (1 + ord(s[i-1]) - ord("a"))%3 s[i] = ch[idx] else: s[i] = ch[6- d[s[i-1]] - d[s[i+1]] - 1] print("".join(s))
1,000
PYTHON3
def compute(a, b, n, m): if n+m > a+b: return "No" elif min(a, b) >= m: return "Yes" return "No" if __name__ == "__main__": t = int(input()) for _ in range(t): a, b, n, m = map(int, input().split()) print(compute(a, b, n, m))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int acnt[10]; int main() { string A, B; cin >> A >> B; sort(A.begin(), A.end()); reverse(A.begin(), A.end()); if (A.size() < B.size()) { cout << A; exit(0); } for (auto w : A) acnt[w - '0']++; string ans = ""; for (int diff = B.size() - 1; diff >= 0; diff--) { for (int fix = 0; fix < 10; fix++) { int cnt[10]; memcpy(cnt, acnt, sizeof(acnt)); string res; int p = 0; while (res.size() < diff) { res += B[p]; cnt[B[p] - '0']--; p++; } cnt[fix]--; res += (char)('0' + fix); for (int i = 9; i >= 0; i--) { while (cnt[i] > 0) { res += (char)(i + '0'); cnt[i]--; } } int can = 1; for (int i = 0; i < 10; i++) { can &= (cnt[i] == 0); } if (res <= B && can) { ans = max(ans, res); } } } cout << ans; return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; long long setSize[100000]; long long minVal[100000]; int parent[100000]; list<int> neigh[100000]; int findSet(int a) { if (parent[a] == a) return a; else return parent[a] = findSet(parent[a]); } void unionSets(int a, int b) { a = findSet(a); b = findSet(b); if (setSize[a] > setSize[b]) { parent[b] = a; setSize[a] += setSize[b]; if (minVal[b] < minVal[a]) minVal[a] = minVal[b]; } else { parent[a] = b; setSize[b] += setSize[a]; if (minVal[a] < minVal[b]) minVal[b] = minVal[a]; } } long long arr[100000]; int ind[100000]; void QSort(int l, int r) { int x = l, y = r, m = arr[ind[(l + r) / 2]]; while (x <= y) { while (arr[ind[x]] > m) x++; while (arr[ind[y]] < m) y--; if (x <= y) { int t = ind[x]; ind[x] = ind[y]; ind[y] = t; x++; y--; } } if (x < r) QSort(x, r); if (l < y) QSort(l, y); } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%I64d", &arr[i]); } for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; neigh[u].push_back(v); neigh[v].push_back(u); } for (int i = 0; i < n; i++) { parent[i] = i; setSize[i] = 1; ind[i] = i; } QSort(0, n - 1); for (int i = 0; i < n; i++) { minVal[ind[i]] = arr[ind[i]]; } double ans = 0; for (int i = 0; i < n; i++) { int v = ind[i]; for (list<int>::iterator it = neigh[v].begin(); it != neigh[v].end(); ++it) { int t = *it; if (findSet(v) != findSet(t) && minVal[findSet(t)] >= arr[v]) { ans += arr[v] * setSize[findSet(t)] / double(n) * setSize[findSet(v)] / double(n - 1) * 2; unionSets(v, t); } } } cout << ans << endl; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int n, m, ans, c[10]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int a[m], b[m]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; if (c[b[i] - 1] == 0) { c[b[i] - 1] = i + 1; } else { a[c[b[i] - 1] - 1] += a[i]; } } sort(b, b + m, greater<int>()); for (int i = 0; i < m && b[i] && n > 0; i++) { while (a[c[b[i] - 1] - 1]-- > 0 && n-- > 0) { ans += b[i]; } } cout << ans; }
900
CPP
t = int(input()) for iter in range(t): c = list(input()) c.reverse() c = ''.join(c) s = c a0 = dict() a0[-1] = 0 a0[0] = 1 a0[1] = 2 a0[2] = 3 a0[3] = 4 a0[4] = 5 a0[5] = 6 a0[6] = 7 a0[7] = 8 a0[8] = 9 a0[9] = 10 a1 = dict() a1[-1] = 10 a1[0] = 9 a1[1] = 8 a1[2] = 7 a1[3] = 6 a1[4] = 5 a1[5] = 4 a1[6] = 3 a1[7] = 2 a1[8] = 1 a1[9] = 0 a00 = dict() a00[-1] = 0 a00[0] = 0 a00[1] = 2 a00[2] = 3 a00[3] = 4 a00[4] = 5 a00[5] = 6 a00[6] = 7 a00[7] = 8 a00[8] = 9 a00[9] = 10 a10 = dict() a10[-1] = 10 a10[0] = 9 a10[1] = 8 a10[2] = 7 a10[3] = 6 a10[4] = 5 a10[5] = 4 a10[6] = 3 a10[7] = 2 a10[8] = 1 a10[9] = 0 ans = dict() ans[-1] = [1, 0, 0, 0] for i in range(len(s)): local_ans = [0, 0, 0, 0] last_ans = ans[i - 1] local_ans[0] += last_ans[0] * a0[int(s[i])] local_ans[1] += last_ans[0] * a1[int(s[i])] local_ans[3] += last_ans[1] * a1[int(s[i])] local_ans[2] += last_ans[1] * a0[int(s[i])] local_ans[0] += last_ans[2] * a0[int(s[i]) - 1] local_ans[1] += last_ans[2] * a1[int(s[i]) - 1] local_ans[3] += last_ans[3] * a1[int(s[i]) - 1] local_ans[2] += last_ans[3] * a0[int(s[i]) - 1] ans[i] = local_ans print(ans[len(s) - 1][0] - 2)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; for (long long i = 0; i < (long long)(a.size()); ++i) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(10) << fixed; smain(); return 0; } void smain() { long long t; cin >> t; while (t--) { long long n, k; string s; cin >> n >> k >> s; vector<long long> p(2); map<long long, long long> mp; for (long long i = 0; i < s.size(); i++) { p[s[i] - '0']++; long long u = p[0] - p[1]; mp[u]++; } long long v = p[0] - p[1]; if (v == 0) { if (mp[k] != 0) { cout << -1 << "\n"; } else { cout << mp[k] << "\n"; } continue; } long long sum = 0; for (auto h : mp) { if ((k - h.first) % abs(v) == 0 && (k - h.first) / v >= 0) { sum += h.second; } } if (k == 0) sum++; cout << sum << "\n"; } }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cin.sync_with_stdio(0); long long n; cin >> n; long long m = 1; long long N = n; while (N != 1) { m++; N /= 4; } vector<long long> v(n); for (int i = 0; i < (n); i++) cin >> v[i]; sort(v.rbegin(), v.rend()); long long ans = 0; vector<long long> f(1000 * 2000 + 9); int i = 1; while (i <= 1000 * 1000) { f[i] = 1; i *= 4; } for (int i = 0; i < (n); i++) { long long I = i + 1; ans += (long long)v[i] * m; if (f[I]) m--; } cout << ans; return 0; }
1,400
CPP
n=int(input()) for _ in range(n): a,b= map(int,input().split()) if a<b: print( "Happy Alex"); break else: print("Poor Alex")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10, mod = 1e9 + 7; long long inf = 0x3f3f3f3f3f3f3f3f; int n, m, k, Q, fa[N], hd[N], ne, mxd[N]; long long d[N], val[N]; struct E { int v, c, nxt; } e[N << 1]; struct E2 { int u, v; long long c; bool operator<(const E2& b) const { return c < b.c; } } e2[N]; void link(int u, int v, int c) { e[ne] = {v, c, hd[u]}, hd[u] = ne++; } struct D { int u; long long g; bool operator<(const D& b) const { return g > b.g; } }; void Dij() { priority_queue<D> q; memset(d, inf, sizeof d); for (int i = 1; i <= k; ++i) q.push({i, d[i] = 0}); while (q.size()) { int u = q.top().u; long long g = q.top().g; q.pop(); if (d[u] != g) continue; for (int i = hd[u]; ~i; i = e[i].nxt) { int v = e[i].v, c = e[i].c; if (d[v] > d[u] + c) d[v] = d[u] + c, q.push({v, d[v]}); } } } int fd(int x) { return fa[x] ? fd(fa[x]) : x; } void mg(int x, int y, long long c) { int fx = fd(x), fy = fd(y); if (fx == fy) return; if (mxd[fx] > mxd[fy]) swap(fx, fy); fa[fx] = fy, val[fx] = c, mxd[fy] = max(mxd[fy], mxd[fx] + 1); } void Kruskal() { sort(e2, e2 + m); for (int i = 1; i <= n; ++i) mxd[i] = 1, fa[i] = 0; for (int i = 0; i < m; ++i) { int u = e2[i].u, v = e2[i].v; long long c = e2[i].c; mg(u, v, c); } } long long qry(int u, int v) { long long ret = 0; for (; u != v; u = fa[u]) { if (mxd[u] > mxd[v]) swap(u, v); ret = max(ret, val[u]); } return ret; } int main() { memset(hd, -1, sizeof hd), ne = 0; scanf("%d%d%d%d", &n, &m, &k, &Q); for (int i = 0; i < m; ++i) { int u, v, c; scanf("%d%d%d", &u, &v, &c); link(u, v, c); link(v, u, c); e2[i] = {u, v, c}; } Dij(); for (int i = 0; i < m; ++i) e2[i].c += d[e2[i].u] + d[e2[i].v]; Kruskal(); while (Q--) { int u, v; scanf("%d%d", &u, &v); printf("%lld\n", qry(u, v)); } return 0; }
2,500
CPP
input() taxi = input() ls = [0] for i in '1234': ls.append(taxi.count(i)) if ls[1] > ls[3]: ls[1] -= ls[3] else: ls[1] = 0 if ls[2] & 1 == 0: ls[2] >>= 1 else: if ls[1] > 1: ls[2] = (ls[2] >> 1) + 1 ls[1] -= 2 else: ls[2] = (ls[2] >> 1) + 1 ls[1] = 0 ls[1] = (ls[1] >> 2) + (ls[1] % 4 != 0) print(ls[1] + ls[2] + ls[3] + ls[4])
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int k[5] = {100, 20, 10, 5, 1}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; while (cin >> n) { long long ans = 0; for (int i = 0; i < 5; i++) { ans += n / k[i]; n %= k[i]; } cout << ans << "\n"; } return 0; }
800
CPP
n = input().strip() for k in n[::-1]: ans = '' l = int(k) if l >= 5: l -= 5 ans+='-O|' else: ans+='O-|' for i in range(l): ans+='O' ans+='-' for i in range(8-len(ans)): ans += 'O' print(ans)
800
PYTHON3
from collections import Counter n, m = list(map(int, input().split())) b = Counter(map(int, input().split())) # print(b.values()) res = 0 for days in range(1, m//n +1 ): parts = 0 for j in b.values(): parts += j // days if parts >= n: res = days print(res)
1,200
PYTHON3
inp = lambda: map(int, input().rstrip().split()) for _ in range(int(input())): n = int(input()) a = list(inp()) s = 0 l = [a[0]] for j in range(n - 1): if a[j] * a[j + 1] > 0: l.append(a[j + 1]) else: s += max(l) l = [a[j + 1]] s += max(l) print(s)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return (b > 0 ? gcd(b, a % b) : a); } int main() { long long t, w, b, cnt = 1; scanf("%I64d%I64d%I64d", &t, &w, &b); long long m = min(w, b), g = gcd(w, b); double lcm = (double)(w / g) * b; long long ans; if (lcm > t) { ans = min(t, m - 1); } else { ans = (t / (long long)lcm) * m; ans += min(t % (long long)lcm, m - 1); } long long gg = gcd(ans, t); printf("%I64d/%I64d", ans / gg, t / gg); }
1,800
CPP
#include <bits/stdc++.h> using namespace std; char visited[44000000]; struct State { int pos[3]; int posMask; char throwState[3]; }; long long encode(const State& cur) { long long code = cur.posMask; for (int i = 0; i < 3; ++i) { code *= 44LL; code += cur.pos[i]; } for (int i = 0; i < 3; ++i) { code *= 4LL; code += cur.throwState[i]; } return code; } State decode(long long code) { State next; for (int i = 2; i > -1; --i) { next.throwState[i] = code % 4; code /= 4; } for (int i = 2; i > -1; --i) { next.pos[i] = code % 44; code /= 44; } next.posMask = code; return next; } int startPos[3]; int maxMove[3]; int maxThrow[3]; char onOtherHead(int idx, State& cur) { for (int i = 1; i <= 2; ++i) { int prev = (idx - i + 3) % 3; if (cur.throwState[prev] == i) return 1; } return 0; } char canMove(int idx, State& cur) { if ((cur.posMask & 1 << idx)) return 0; if (cur.throwState[idx] == 0 || cur.throwState[idx] == 3) { return !onOtherHead(idx, cur); } return 0; } char isNewPosOK(int newPos, State& last) { if (newPos <= 0) return 0; for (int i = 0; i < 3; ++i) { if (last.pos[i] == newPos) return 0; } return 1; } int main() { while (scanf("%d%d%d", startPos, maxMove, maxThrow) != EOF) { scanf("%d%d%d", startPos + 1, maxMove + 1, maxThrow + 1); scanf("%d%d%d", startPos + 2, maxMove + 2, maxThrow + 2); State cur; cur.posMask = 0; int ans = 0; for (int i = 0; i < 3; ++i) { cur.pos[i] = startPos[i]; ans = max(ans, startPos[i]); cur.throwState[i] = 0; } long long curCode = encode(cur); queue<long long> q; memset(visited, 0, sizeof(visited)); q.push(curCode); while (!q.empty()) { curCode = q.front(); q.pop(); if (visited[curCode]) continue; visited[curCode] = 1; cur = decode(curCode); for (int i = 0; i < 3; ++i) ans = max(ans, cur.pos[i]); for (int i = 0; i < 3; ++i) { if (canMove(i, cur)) { State next = cur; next.posMask |= 1 << i; for (int j = -maxMove[i]; j <= maxMove[i]; ++j) { next.pos[i] = cur.pos[i] + j; if (isNewPosOK(next.pos[i], cur)) { long long nextCode = encode(next); if (!visited[nextCode]) q.push(nextCode); } } } } for (int i = 0; i < 3; ++i) { if (!onOtherHead(i, cur) && cur.throwState[i] == 0) { for (int j = 1; j <= 2; ++j) { int nextPer = (i + j) % 3; if (onOtherHead(nextPer, cur)) continue; if (cur.pos[nextPer] + 1 == cur.pos[i] || cur.pos[nextPer] - 1 == cur.pos[i]) { State next = cur; next.throwState[i] = j; next.pos[nextPer] = cur.pos[i]; if (next.throwState[nextPer] == 1) next.pos[(nextPer + 1) % 3] = cur.pos[i]; else if (next.throwState[nextPer] == 2) next.pos[(nextPer + 2) % 3] = cur.pos[i]; long long nextCode = encode(next); if (!visited[nextCode]) q.push(nextCode); } } } } for (int i = 0; i < 3; ++i) { State next; for (int j = 1; j <= 2; ++j) { if (cur.throwState[i] == j && !onOtherHead(i, cur)) { for (int t = -maxThrow[i]; t <= maxThrow[i]; ++t) { int nextPos = cur.pos[i] + t; if (isNewPosOK(nextPos, cur)) { next = cur; int nextPer = (i + j) % 3; next.throwState[i] = 3; next.pos[nextPer] = nextPos; if (next.throwState[nextPer] == 1) next.pos[(nextPer + 1) % 3] = nextPos; else if (next.throwState[nextPer] == 2) next.pos[(nextPer + 2) % 3] = nextPos; long long nextCode = encode(next); if (!visited[nextCode]) q.push(nextCode); } } } } } } printf("%d\n", ans); } return 0; }
2,500
CPP
#ax =- b(mod n) #a % x = b from math import sqrt from functools import reduce def factorNumber(x, b): factors = [] for i in range(1, int(sqrt(x))+1): if x % i == 0: factors.append(i) if x // i != i: factors.append(x//i) count = 0 for i in factors: if i > b: count += 1 print(count) ##def factorNumber2(x): ## result = 0 ## i = 1 ## while i*i <= x: ## if x % i == 0: ## result += 1 ## if x//i != i: ## result += 1 ## i += 1 ## return result (a,b) = list(map(int,input().split())) answers = 0 if a == b: print("infinity") elif a < b: print(0) else: factorNumber(a-b,b) #factors = factorNumber2(a-b,b)
1,600
PYTHON3
n, k = map(int, input().split()) b = [0] * k for _ in range(n): x = int(input()) b[x - 1] += 1 ans = 0 s = 0 for x in b: ans += 2 * (x // 2) s += x & 1 ans += (s + 1) // 2 print(ans)
1,000
PYTHON3
m,n=map(int,input().split()) p=m*n if(p%2==0): print(p//2) else: print((p-1)//2)
800
PYTHON3
s=input() runde=int(s) for i in range(runde): beseda=input() if len(beseda)<=10: print(beseda) elif len(beseda)>10: print(beseda[0],end='') print(len(beseda)-2, end="") print(beseda[-1])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int N = 55; long long ans; long long dp[N][N * 2][N]; long long rev[N * 2]; int n, m, tot, one, zero; int l[N], w[N]; long long Pow(int x, int y) { long long ret = 1ll, mt = 1ll * x; while (y) { if (y & 1) ret = (ret * mt) % MOD; y >>= 1; mt = (mt * mt) % MOD; } return ret; } void print(int x, int y, int z) { printf("dp[%d][%d][%d]=%I64d\n", x, y, z, dp[x][y][z]); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", l + i); if (l[i] == 0) l[i]--; } for (int i = 0; i < n; i++) { scanf("%d", w + i); tot += w[i]; if (l[i] == 1) one += w[i]; else zero += w[i]; } for (int i = -50; i <= 50; i++) if (tot + i >= 0) rev[N + i] = Pow(tot + i, MOD - 2); for (int x = 0; x < n; x++) { memset(dp, 0, sizeof(dp)); dp[0][N][0] = 1ll; for (int i = 0; i < m; i++) for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) if (dp[i][j][k]) { dp[i + 1][j + l[x]][k + 1] = (dp[i + 1][j + l[x]][k + 1] + dp[i][j][k] * (w[x] + k * l[x]) % MOD * rev[j] % MOD) % MOD; int J = j - N, ONE = one, ZERO = zero; int inc = (J + i) / 2, dec = (J - i) / 2; if (l[x] == 1) ONE = (ONE - w[x] - k * l[x] + MOD) % MOD; else ZERO = (ZERO - w[x] - k * l[x] + MOD) % MOD; ONE += inc; ZERO += dec; dp[i + 1][j + 1][k] = (dp[i + 1][j + 1][k] + dp[i][j][k] * ONE % MOD * rev[j] % MOD) % MOD; dp[i + 1][j - 1][k] = (dp[i + 1][j - 1][k] + dp[i][j][k] * ZERO % MOD * rev[j] % MOD) % MOD; } ans = 0ll; for (int j = N - 50; j <= N + 50; j++) for (int k = 0; k <= 50; k++) { ans = (ans + dp[m][j][k] * (w[x] + k * l[x] % MOD + MOD) % MOD) % MOD; } printf("%I64d\n", ans); } return 0; }
2,300
CPP
import sys input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): N = int(input()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] C = [int(x) for x in input().split()] ans = [] for i in range(N): if i == 0: ans.append(A[i]) elif i == N - 1: if ans[-1] != A[i] and ans[0] != A[i]: ans.append(A[i]) elif ans[-1] != B[i] and ans[0] != B[i]: ans.append(B[i]) else: ans.append(C[i]) else: if ans[-1] != A[i]: ans.append(A[i]) elif ans[-1] != B[i]: ans.append(B[i]) else: ans.append(C[i]) print(*ans) if __name__ == '__main__': main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long l, r, k, t = 1, a; int main() { cin >> l >> r >> k; while (1) { if (l <= t) { cout << t << ' '; a = 1; } if (r / t >= k) t *= k; else break; } if (a == 0) cout << -1; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int n, A[200005]; int cnt[200005]; void add(int x) { while (x < 200005) { cnt[x]++; x += x & -x; } } int query(int x) { int ans = 0; if (x >= 200005) x = 200005 - 1; while (x) { ans += cnt[x]; x -= x & -x; } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &A[i]); add(A[i]); } sort(A + 1, A + 1 + n); int last = 0; long long ans = 0; for (int i = 1; i <= n; ++i) { if (A[i] == last) continue; last = A[i]; long long res = 0; for (int j = last; j <= A[n]; j += last) { int R = j + last - 1; int sum = query(R) - query(j - 1); res += 1ll * j * sum; } if (ans < res) ans = res; } cout << ans << endl; return 0; }
1,900
CPP
value = int(input()) def display(lst): for item in sorted(lst): print(item) def sum_a_string(integer): # print(integer) return sum([int(d) for d in str(integer)]) def make_all_nine(lenght): return '9' * lenght def d_max(value): svalue = str(value) value_len = len(svalue) first = svalue[0] if value_len == 1: return value - 1 if first == '1': return sum_a_string(int(make_all_nine(value_len - 1))) nines_count = value_len - 1 first = str(int(first) - 1) return sum_a_string(int(first + make_all_nine(nines_count))) def itter(value): count = 0 possible_digits = [] if value == 1: print(count) display(possible_digits) return min_x = value - d_max(value) max_x = value if min_x == max_x: check_range = [max_x] else: check_range = range(min_x, max_x) for num in check_range: result = num + sum_a_string(num) if result == value: count += 1 possible_digits.append(num) print(count) display(possible_digits) itter(value)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int M = 1e9 + 7; const int MOD = 998244353; const double PI = 3.141592653589793238460; long long int power(long long int a, long long int b) { long long int res = 1; if (a == 0) return 0; if (a == 1) return 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a); if (res > MOD) res %= MOD; a = (a * a); if (a > MOD) a = a % MOD; } return res; } bool isPrime[1000001]; vector<long long int> prime; void seive(int limit) { isPrime[0] = isPrime[1] = 1; for (int i = 2; i * i <= limit; i++) { if (!isPrime[i]) for (int j = i * i; j <= limit; j += i) { isPrime[j] = 1; } } for (int i = 2; i <= limit; i++) if (!isPrime[i]) prime.push_back(i); } int main() { ios_base::sync_with_stdio(false), cin.tie(0); int tt = 1; cin >> tt; while (tt--) { long long int n; cin >> n; long long int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long int i = 0, j = n - 1; bool flag = 1; long long int a = arr[i++], b = 0, pre = a, cnt = 1; while (1) { if (flag) { long long int sum = 0; for (; j >= i; j--) { sum += arr[j]; if (sum > pre) { j--; break; } } b += sum; pre = sum; if (sum) cnt++; else break; flag = 1 - flag; } else { long long int sum = 0; for (; i <= j; i++) { sum += arr[i]; if (sum > pre) { i++; break; } } a += sum; if (sum) cnt++; else break; pre = sum; flag = 1 - flag; } } cout << cnt << ' '; cout << a << ' ' << b << "\n"; } }
1,300
CPP
#include<bits/stdc++.h> using namespace std; #define int long long int #define endl '\n' void move(string &str1, string &str2, int &a, int &b) { if(b > 0) { b--; str1.push_back('1'); str2.push_back('1'); } else { a--; str1.push_back('0'); str2.push_back('0'); } } void ETA() { int a, b, k; cin >> a >> b >> k; if(a==0 and b==1) { cout << "YES" << endl << 1 << endl << 1 << endl; return; } if(k > a+b-2) { cout << "No" << endl; return; } string str1 = "1", str2 = "1"; b--; if(k > 0) { if(b<=0 or a<=0) { cout << "No" << endl; return; } a--; b--; str1.push_back('1'); str2.push_back('0'); } while(k > 1) { move(str1, str2, a, b); k--; } if(k > 0) { str1.push_back('0'); str2.push_back('1'); } while(a>0 or b>0) move(str1, str2, a, b); cout << "Yes" << endl << str1 << endl << str2 << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // cin >> t; while(t--) ETA(); return 0; }
1,900
CPP
n,k=map(int,input().split() ) arr=list(map(int,input().split())) ans=[] ans_ind=[] for i in range(len(arr)): if(arr[i] not in ans): ans.append(arr[i]) ans_ind.append(i+1) if(len(ans)>=k): print("YES") for i in ans_ind: print(i,end=" ") print() exit() print("NO")
800
PYTHON3
def ok(n): if n%2==0: print('2 '*(n//2)) exit() else: print('2 ' * (n // 2-1)+'3') exit() n=int(input()) print(n//2) print(ok(n))
800
PYTHON3
X = list(map(int, input().split())) MAX = -10000000000000000000 for i in range(X[0]): Y = list(map(int, input().split())) MAX = max(MAX, (Y[0] if Y[1] <= X[1] else Y[0] - (Y[1] - X[1]))) print(MAX)
900
PYTHON3
word1 = input().upper() word2 = input().upper() wordArray = [word1, word2] wordArray.sort() if word1 == word2: print(0) elif wordArray[0] == word1: print(-1) else: print(1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, x, ans = 0; cin >> n >> x; for (int i = 1; i <= n; ++i) { if (x % i == 0 && x / i <= n) { ++ans; } } cout << ans; return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, t; scanf("%d", &t); while (t--) { scanf("%d", &a); if (a <= 2) printf("0\n"); else printf("%d\n", a - 2); } return 0; }
1,600
CPP
amount = int(input()) first_array = [int(s) for s in input()] second_array = [int(s) for s in input()] array_of_1 = [] array_of_0 = [] amount_1 = sum(first_array) amount_0 = len(first_array) - amount_1 #for i in range(len(first_array)): #if first_array[i] == 1: # array_of_1.append(i) # else: # array_of_0.append(i) #set_ = set() counter = 0 counter_00 = 0 counter_10 = 0 for i in range(len(first_array)): if (first_array[i] == 0 and second_array[i] == 0): counter += amount_1 counter_00 += 1 elif (first_array[i] == 1 and second_array[i] == 0): counter += amount_0 counter_10 += 1 print(counter - counter_00 * counter_10)
1,200
PYTHON3
#!/usr/bin/env python3 for _ in range(int(input())): n = int(input()) print(' '.join(['1'] * n))
800
PYTHON3