solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
while True: n = int(input()) if n == 0: break gem_points = [list(map(int, input().split())) for i in range(n)] m = int(input()) operations = [input().split() for i in range(m)] robot_pos = [10, 10] for operation in operations: move_dir = operation[0] move_amount = int(operation[1]) for move in range(move_amount): if move_dir == 'N': robot_pos[1] += 1 elif move_dir == 'E': robot_pos[0] += 1 elif move_dir == 'S': robot_pos[1] -= 1 elif move_dir == 'W': robot_pos[0] -= 1 if robot_pos in gem_points: gem_points.pop(gem_points.index(robot_pos)) if gem_points: print('No') else: print('Yes')
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[300000]; int s[2]; int b[300000]; int fp(int a, int k, int m) { int res = 1; while (k) { if (k & 1) res = 1LL * res * a % m; a = 1LL * a * a % m; k >>= 1; } return res; } int main() { int m, n; scanf("%d%d", &m, &n); s[0] = s[1] = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); s[0] = (s[0] + a[i]) % m; s[1] = (s[1] + 1LL * a[i] * a[i]) % m; } if (n == 1) { return 0 * printf("%d 0\n", a[1]); } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { if (i == 2) continue; int d = (abs(a[i] - a[2]) + m) % m; int x = 1LL * (s[0] - 1LL * n * (n - 1) / 2 % m * d % m + m) % m * fp(n, m - 2, m) % m; int ans = 1LL * n * x % m * x % m; ans = (ans + 1LL * n * (n - 1) % m * d % m * x % m) % m; ans = (ans + 1LL * n * (n - 1) * (2 * n - 1) / 6 % m * d % m * d % m) % m; if (ans == s[1]) { b[1] = x; for (int j = 2; j <= n; j++) { b[j] = b[j - 1] + d; b[j] %= m; } sort(b + 1, b + n + 1); bool flag = true; for (int j = 1; j <= n; j++) { if (a[j] != b[j]) { flag = false; break; } } if (flag) { return 0 * printf("%d %d\n", x, d); } } } return 0 * printf("-1\n"); }
9
CPP
n=int(input()) if n<4 or n%2!=0: print('NO') else: print('YES')
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ int n;cin>>n; map<int,int> m; vector<int> a(n+1); for(int i=1;i<=n;i++){cin>>a[i];m[i+a[i]]++;} long long ans=0; for(int i=1;i<=n;i++)ans+=m[i-a[i]]; cout<<ans<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; vector<int> arr[100005]; int vis[100005]; int dfs(int start) { int i, j, k, ans = 0; vis[start] = 1; k = arr[start].size() - 1; for (i = 0; i < arr[start].size(); i++) { if (vis[arr[start][i]]) continue; ans += k + dfs(arr[start][i]); k--; } return ans; } int main() { int i, j, k, n, m, p, x, y; scanf("%d", &n); for (i = 1; i < n; i++) { scanf("%d %d", &x, &y); arr[x].push_back(y); arr[y].push_back(x); } printf("%d\n", dfs(1)); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int N, M; char mat[1100][1100]; int ans[1100][1100]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int main(int argc, char** argv) { int i, j, k, u; while (scanf("%d%d", &N, &M) != EOF) { memset(ans, 0, sizeof(ans)); memset(mat, 0, sizeof(mat)); for (i = 1; i <= N; i++) scanf("%s", mat[i] + 1); bool yes = true; for (i = 1; i <= N && yes; i++) for (j = 1; j <= M && yes; j++) if (mat[i][j] == 'b') { yes = false; if (mat[i - 1][j] == 'w' && mat[i + 1][j] == 'w' && ans[i - 1][j] == 0 && ans[i + 1][j] == 0) { int mask = 0; for (k = 0; k < 4; k++) mask |= ans[i + 1 + dx[k]][j + dy[k]]; for (k = 0; k < 4; k++) mask |= ans[i + dx[k]][j + dy[k]]; for (k = 0; k < 4; k++) mask |= ans[i - 1 + dx[k]][j + dy[k]]; for (k = 0; k < 4; k++) if (((1 << k) & mask) == 0) break; ans[i - 1][j] = ans[i][j] = ans[i + 1][j] = (1 << k); yes = true; continue; } if (mat[i][j - 1] == 'w' && mat[i][j + 1] == 'w' && ans[i][j - 1] == 0 && ans[i][j + 1] == 0) { int mask = 0; for (k = 0; k < 4; k++) mask |= ans[i + dx[k]][j + 1 + dy[k]]; for (k = 0; k < 4; k++) mask |= ans[i + dx[k]][j + dy[k]]; for (k = 0; k < 4; k++) mask |= ans[i + dx[k]][j - 1 + dy[k]]; for (k = 0; k < 4; k++) if (((1 << k) & mask) == 0) break; ans[i][j - 1] = ans[i][j] = ans[i][j + 1] = (1 << k); yes = true; continue; } } for (i = 1; i <= N; i++) for (j = 1; j <= M; j++) if (mat[i][j] != '.' && ans[i][j] == 0) yes = false; if (!yes) { printf("NO\n"); continue; } printf("YES\n"); for (i = 1; i <= N; i++) { for (j = 1; j <= M; j++) if (mat[i][j] == '.') printf("."); else { for (k = 0; k < 4; k++) if ((1 << k) & ans[i][j]) break; printf("%c", k + 'a'); } printf("\n"); } } return (EXIT_SUCCESS); }
16
CPP
#include <bits/stdc++.h> inline int in() { int k = 0; char ch = getchar(); bool p = 1; while (ch < '-') ch = getchar(); if (ch == '-') ch = getchar(), p = 0; while (ch > '-') k = k * 10 + ch - '0', ch = getchar(); return p ? k : -k; } using std::set; using std::vector; const int N = 1e5 + 5; vector<int> ans[N]; int f[N], dp[N], st[N], top, o[N]; int calc(vector<int> a, int n) { int d = 0; for (int i = 0; i < n; i++) { if (!d || a[i] > st[d]) { st[++d] = a[i]; } else { *(std::lower_bound(st + 1, st + d + 1, a[i])) = a[i]; } dp[i] = std::lower_bound(st + 1, st + d + 1, a[i]) - st; } return d; } struct node { int id, d; node() {} node(int a, int b) : id(a), d(b) {} }; inline bool operator<(const node &a, const node &b) { return a.d < b.d; } set<node> cx; inline void New(int x) { ans[++top].push_back(x), cx.insert(node(top, x)); } void solve(vector<int> a, int n) { if (!n) { return; } memset(dp, 0, n << 2); memset(st, 0, n << 2); int LIS = calc(a, n); if (LIS > f[n]) { ans[++top].resize(LIS + 1); int k = 0; for (int i = n - 1; ~i; i--) { if (dp[i] + k == LIS && (!k || a[i] < ans[top][k])) { o[ans[top][++k] = a[i]] = 1; } } std::reverse(ans[top].begin(), ans[top].end()); ans[top].resize(k); for (vector<int>::iterator it = a.begin(); it != a.end();) { if (o[*it]) { it = a.erase(it); } else { it++; } } for (int i = 0; i < k; i++) { o[ans[top][i]] = 0; } return solve(a, n - k); } New(a[0]); set<node>::iterator it; int t; for (int i = 1; i < n; i++) { it = cx.upper_bound(node(0, a[i])); if (it == cx.end()) { New(a[i]); } else { t = it->id, cx.erase(it); ans[t].push_back(a[i]), cx.insert(node(t, a[i])); } } } inline void work() { int n = in(); vector<int> a(n); cx.clear(); for (int i = 0; i < n; i++) { a[i] = in(); } top = 0; solve(a, n); printf("%d\n", top); for (int i = 1; i <= top; i++, puts("")) { printf("%d ", ans[i].size()); for (int x : ans[i]) { printf("%d ", x); } vector<int>().swap(ans[i]); } } int main() { for (int i = 1; i * (i + 1) / 2 < N; i++) { f[i * (i + 1) / 2] = 1; } for (int i = 2; i < N; i++) { f[i] += f[i - 1]; } for (int t = in(); t; t--) { work(); } return 0; }
11
CPP
for _ in range(int(input())): n, k = map(int, input().split()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] a.sort() b.sort(reverse=True) for i in range(k): if(a[i]<b[i]): a[i]=b[i] else: break print(sum(a))
8
PYTHON3
input() s = input() pos = 0 result = 0 for i in s: pos += 1 if int(i) % 2 == 0: result += pos print(result)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int son[2010 << 1][26], len[2010 << 1], pre[2010 << 1]; int tot, tail; void init() { tail = tot = len[0] = 0; pre[0] = -1; memset(son, 0, sizeof(son)); } void add(int x) { int p = tail, np = ++tot, q, nq; len[np] = len[p] + 1, tail = np; while (p != -1 && !son[p][x]) son[p][x] = np, p = pre[p]; if (p == -1) pre[np] = 0; else { q = son[p][x]; if (len[p] + 1 == len[q]) pre[np] = q; else { nq = ++tot; memcpy(son[nq], son[q], sizeof(son[q])); len[nq] = len[p] + 1; pre[nq] = pre[q]; pre[q] = pre[np] = nq; while (p != -1 && son[p][x] == q) son[p][x] = nq, p = pre[p]; } } } char str[2010]; int num[2010 << 1][2010]; int L[11][2010 << 1], R[11][2010 << 1]; int b[2010 << 1]; bool cmp(int x, int y) { return len[x] > len[y]; } int main() { int n; int p, le, x, u, v; scanf("%s", str); init(); memset(L, -1, sizeof(L)); memset(R, -1, sizeof(R)); for (int i = 0; str[i]; ++i) add(str[i] - 'a'); for (int i = 0; i <= tot; ++i) b[i] = i; sort(b, b + tot + 1, cmp); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s%d%d", str, &u, &v); memset(num, 0, sizeof(num)); p = le = 0; for (int j = 0; str[j]; ++j) { x = str[j] - 'a'; if (son[p][x]) { ++le; p = son[p][x]; } else { while (p != -1 && !son[p][x]) p = pre[p]; if (p != -1) { le = len[p] + 1; p = son[p][x]; } else { le = p = 0; } } if (p) { num[p][le]++; } } for (int j = 0; j < tot; ++j) { p = b[j]; for (int k = len[p]; k > len[pre[p]]; --k) { if (k != len[p]) num[p][k] += num[p][k + 1]; if (num[p][k] >= u && num[p][k] <= v) { if (L[i][p] == -1 || L[i][p] > k) L[i][p] = k; if (R[i][p] == -1 || L[i][p] < k) R[i][p] = k; } } num[pre[p]][len[pre[p]]] += num[p][len[pre[p]] + 1]; } } int ans = 0; for (int i = 0; i < tot; ++i) { p = b[i]; v = len[p]; u = len[pre[p]] + 1; int is = 0; for (int j = 0; j < n; ++j) if (L[j][p] == -1 || R[j][p] == -1) { is = 1; break; } if (is) continue; for (int j = 0; j < n; ++j) { if (u == -1 || u < L[j][p]) u = L[j][p]; if (v == -1 || v > R[j][p]) v = R[j][p]; } if (u == -1 || v == -1) continue; if (v >= u) ans += v - u + 1; } cout << ans << endl; }
13
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; a = a % b; for (int i = 1; i <= b; i++) { a = a * 10; if (a / b == c) { cout << i; return 0; } a %= b; } cout << -1; }
8
CPP
#include <bits/stdc++.h> using namespace std; int arr[26][26]; char s[200005]; char t[200005]; int main() { int n; scanf("%d", &n); ; for (int i = 0; i < 26; i++) for (int j = 0; j < 26; j++) arr[i][j] = -1; scanf("%s", s); ; scanf("%s", t); ; int mans = 0; for (int i = 0; i < n; i++) { if (s[i] != t[i]) { mans++; if (arr[t[i] - 'a'][s[i] - 'a'] == -1) { arr[t[i] - 'a'][s[i] - 'a'] = i; } } } int indexi = -1; int indexj = -1; long long ans = mans; for (int i = 0; i < n; i++) { if (s[i] != t[i]) { if (arr[s[i] - 'a'][t[i] - 'a'] != -1) { ans = mans - 2; indexi = i; indexj = arr[s[i] - 'a'][t[i] - 'a']; } else { for (int j = 0; j < 26; j++) { if (arr[s[i] - 'a'][j] != -1) { if (mans - 1 < ans) { ans = mans - 1; indexi = i; indexj = arr[s[i] - 'a'][j]; } } } } } } printf("%d\n", ans); ; if (indexi != -1 && indexj != -1) printf("%d %d\n", indexi + 1, indexj + 1); else { printf("%d %d\n", indexi, indexj); } return 0; }
8
CPP
import sys import math from collections import defaultdict,Counter import bisect # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdout=open("CP1/output.txt",'w') # sys.stdin=open("CP1/input.txt",'r') # m=pow(10,9)+7 t=int(input()) for i in range(t): n,s,k=map(int,input().split()) a=set(map(int,input().split())) for j in range(k+1): if (s-j not in a and s-j>0) or (s+j not in a and s+j<=n): print(j) break
7
PYTHON3
amount = int(input()) for i in range(amount): fline = input().split(" ") n = int(fline[0]) k = int(fline[1]) a = input().split(" ") b = input().split(" ") a = [ int(x) for x in a] b = [ int(x) for x in b] for j in range(k): if min(a) == max(b): break else: if min(a) > max(b): break else: amin = min(a) a[a.index(min(a))] = max(b) b[b.index(max(b))] = amin print(sum(a))
8
PYTHON3
n=int(input()) count=0 for i in range(0,n): a,b,c=map(int , input().split()) if (a and b) or (b and c) or (c and a): count=count+1 print(count)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; struct SegmentTree { ll n; vector<l_l> node; SegmentTree() { int sz = 100050; n = 1; while(n < sz) n *= 2; node.resize(2*n-1); for(int i = 0; i < sz; i++) { node[i+n-1] = {0, 0}; } for(int i = n - 2; i >= 0; i--) node[i] = {0, 0}; } void update(ll x, l_l val) { x += n - 1; node[x] = val; while(x > 0) { x = (x - 1) / 2; node[x] = max(node[2*x+1], node[2*x+2]); } } l_l getmax(int a, int b, int k = 0, int l=0, int r=-1) { if(r<0) r = n; if(r <= a || b <= l) return {0, 0}; if(a<=l && r <= b) return node[k]; l_l vl = getmax(a,b,2*k+1,l,(l+r)/2); l_l vr = getmax(a,b,2*k+2,(l+r)/2, r); return max(vl, vr); } }; int main() { ll N; cin >> N; SegmentTree seg; vector<l_l> A(N); for(ll i = 1; i <= N; i++) { A[i-1].second = i; cin >> A[i-1].first; A[i-1].first = -A[i-1].first; } sort(A.begin(), A.end()); for(int i = 0; i < N; i++) { A[i].first = -A[i].first; int index = A[i].second; l_l maximum = seg.getmax(index, N + 1); l_l NEW = maximum; NEW.first++; NEW.second += A[i].first; seg.update(index, NEW); //cout << endl << i << endl; //for(int j = 1; j <= N; j++) { //l_l now = seg.getmax(j, j+1); //cout << j << " " << now.first << " " << now.second << endl; //} //cout << i << " " << index << " " << NEW.first << " "<< NEW.second << endl; } ll ans = 0; ll longest = 0; for(int i = 1; i <= N; i++) { longest = max(longest, seg.getmax(i,i+1).first); } for(int i = 1; i <= N; i++) { l_l val = seg.getmax(i, i+1); if(val.first != longest) continue; //cout << i << " " << val.first << "s " << val.second << endl; ans = max(ans, val.second); } cout << ans << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15; const int maxn = 3010; long long a[maxn], b[maxn]; struct BIT { long long num[maxn]; void init() { for (int i = 0; i < maxn; i++) num[i] = inf; } int low_bit(int x) { return x & (-x); } void update(int x, long long n) { while (x < maxn) { num[x] = min(num[x], n); x += low_bit(x); } } long long query(int x) { long long ans = inf; while (x) { ans = min(ans, num[x]); x -= low_bit(x); } return ans; } } bi; long long dp[2][maxn]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]), a[i] -= i, b[i] = a[i]; sort(b, b + n); int len = unique(b, b + n) - b; int cur = 0; for (int i = 0; i < n; i++) dp[cur][i] = abs(a[i] - a[0]); for (int i = 1; i < n; i++) { bi.init(); for (int j = 0; j < n; j++) { if (dp[cur][j] == inf) continue; int pos = lower_bound(b, b + len, a[j]) - b + 1; bi.update(pos, dp[cur][j]); } cur ^= 1; for (int j = 0; j < n; j++) { dp[cur][j] = inf; int pos = lower_bound(b, b + len, a[j]) - b + 1; long long q = bi.query(pos); if (q != inf) dp[cur][j] = q + abs(a[j] - a[i]); } } long long ans = inf; for (int i = 0; i < n; i++) ans = min(ans, dp[cur][i]); printf("%lld\n", ans); return 0; }
9
CPP
import itertools import math import sys import heapq from collections import Counter from collections import deque from fractions import gcd from functools import reduce sys.setrecursionlimit(4100000) INF = 1 << 60 #ここから書き始める t = int(input()) for i in range(t): n = int(input()) a = sorted(list(map(int, input().split())), reverse=True) print(*a)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int a, b, x, y, ans = 0; cin >> a >> b >> x >> y; int c, d, m, n; c = max(a, b), d = min(a, b), m = max(x, y), n = min(x, y); if (c == m) { if (d + n == c) ans++; } if (ans) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; const int mod = (int)1e9 + 7; const int logN = 17; const int inf = 1e9; const int N = 2e5 + 5; void exit() { cout << "Incorrect sequence" << '\n'; exit(0); } int n, m, x, a[N], ans[N], b[N], t, s, k; char str[12]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%s", str); if (str[0] == '?') a[i] = mod; else a[i] = atoi(str); } a[0] = -inf * 2; a[n + 1] = inf * 2; for (int i = 1; i <= k; i++) { s = 0, t = i; b[++s] = 0; while (t <= n) { b[++s] = t; t += k; } b[++s] = n + 1; int last = -2 * inf, lll = 2; for (int j = 2; j <= s; j++) if (a[b[j]] != mod) { int t = max(min(a[b[j]] - (j - lll + 1) + 1, -(j - lll) / 2), last); for (int k = lll; k <= j - 1; k++) ans[b[k]] = t++; if (t > a[b[j]]) exit(); ans[b[j]] = a[b[j]]; last = a[b[j]] + 1; lll = j + 1; } } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); cout << '\n'; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main () { int n,d; set <int> S; cin >> n; for(int i=1 ; i<=n ; i++){ cin >> d; if (S.count(d) == 0) S.insert(d); } cout << S.size() << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, F = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') F = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * F; } int n, k, tot; long long res; int main() { n = read(), k = read(); for (int i = 1; i <= k; i++) { int pos = read(), cnt = read(); tot += cnt, res += 1LL * pos * cnt; } if (tot < n) puts("1"); else if (tot > n) puts("-1"); else if (res % n == (1LL * (n + 1) * n / 2) % n) puts("1"); else puts("-1"); }
12
CPP
#include <bits/stdc++.h> using namespace std; char a[1010], b[1010]; int main() { int n, i = 0, j = 0, ans1 = 0, ans2 = 0; cin >> n >> a >> b; sort(a, a + n), sort(b, b + n); while (i < n && j < n) if (b[j] >= a[i]) i++, j++; else j++, ans1++; i = j = 0; while (i < n && j < n) if (b[j] > a[i]) i++, j++, ans2++; else j++; printf("%d\n%d", ans1, ans2); }
8
CPP
count, x, y, z = 0, {}, {}, [] for i in range(int(input())): a, b = map(int, input().split()) if a not in x: x[a] = set() if b not in y: y[b] = set() x[a].add(b) y[b].add(a) z.append((a, b)) for a, b in z: if any(True for i in x[a] if i < b) and any(True for i in x[a] if i > b) \ and any(True for i in y[b] if i < a) and any(True for i in y[b] if i > a): count += 1 print(count)
7
PYTHON3
l=int(input()) A=list(map(int,input().split())) B=[] for i in range(max(A)): a=0 for x in range(l): if A[x]>i: a+=1 B.append(str(a)) C=[] for x in range(l): b=0 for i in range(max(A)): if int(B[i])>=l-x: b+=1 C.append(str(b)) print(' '.join(C))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n; scanf("%d", &n); vector<int> in(n + 1, 0), root(n + 1, 0), f(n + 1); vector<bool> vis(n + 1, false); for (int i = 1; i <= n; i++) { scanf("%d", &f[i]); in[f[i]] += 1; } vector<int> v; for (int i = 1; i <= n; i++) { int cur = i; vector<int> can; int mx = 0; while (!vis[cur]) { can.push_back(cur); vis[cur] = true; mx = max(mx, in[cur]); cur = f[cur]; } if (root[cur] == 0) { root[cur] = cur; v.push_back(cur); if (mx <= 1) in[cur] = 0; } for (auto x : can) { root[x] = root[cur]; } } vector<int> nxt(n + 1, 0); for (int i = 0; i < v.size(); i++) { nxt[v[i]] = v[(i + 1) % v.size()]; } vector<pair<int, int>> edge; for (int i = 1; i <= n; i++) { if (in[i] == 0) { if (nxt[root[i]] != i) edge.emplace_back(nxt[root[i]], i); } } printf("%d\n", (int)edge.size()); for (auto i : edge) { printf("%d %d\n", i.first, i.second); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int m, n; cin >> m >> n; cout << m * n / 2; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; inline int ni() { int a; scanf("%d", &a); return a; } inline double nf() { double a; scanf("%lf", &a); return a; } template <class T> void out(T a, T b) { bool first = true; for (T i = a; i != b; i++) { if (!first) printf(" "); first = false; cout << *i; } puts(""); } template <class T> void outl(T a, T b) { for (T i = a; i != b; i++) cout << *i << "\n"; } const int MAXN = 200200; const int MAXA = 300; int n; char s[MAXN], t[MAXN]; int len; vector<int> sa[MAXA], ta[MAXA]; double ans = 0; int main() { n = ni(); scanf("\n"); gets(s); gets(t); len = strlen(s); for (int i = 0; i < (int)(len); i++) { sa[s[i]].push_back(i); ta[t[i]].push_back(i); } long long llen = len; llen *= (len + 1); llen *= (2 * len + 1); llen /= 6; for (int letter = 'A'; letter <= 'Z'; letter++) { long long sum1 = 0, sum2 = 0; for (int i = 0; i < (int)(((int)(ta[letter]).size())); i++) { sum1 += len - ta[letter][i]; } int pp = 0; for (int i = 0; i < ((int)(sa[letter]).size()); i++) { int pa = sa[letter][i]; while (pp < ((int)(ta[letter]).size()) && ta[letter][pp] <= pa) { sum1 -= len - ta[letter][pp++]; sum2 += ta[letter][pp - 1] + 1; } ans += (double)(sum1 * (pa + 1) + sum2 * (len - pa)) / llen; } } printf("%.9lf\n", ans); }
9
CPP
n=int(input()) arr=list(map(int,input().split())) ma=0 mi=101 for i in range(n): if arr[i] > ma: ma=arr[i] in_ma=i if arr[i]<= mi: mi=arr[i] in_mi=i n-=1 if in_ma > in_mi: in_mi+=1 print(n+in_ma-in_mi)
7
PYTHON3
n = int(input()) a = sorted(list(map(int, input().split()))) lst = zip(a, a[1:], a[2:]) print("YES") if any([x+y > z for x,y,z in lst]) else print("NO")
8
PYTHON3
n = int(input()) res = 0 if n%2 == 0: res = 2**(n//2) print(res)
7
PYTHON3
n = int(input()) l , r = [] , [] for _ in range(n): x = [int(i) for i in input().split()] l.append(x[0]) r.append(x[1]) m_i = min(l) m_a = max(r) f = -2 i=0 for i ,x in enumerate(list(zip(l,r))): if x[0] == m_i and x[1] == m_a: f = i break print(f+1)
8
PYTHON3
def up(x): if int(x)==x: return int(x) else: return int(x)+1 r,x,y,x1,y1=map(int,input().split()) import math dis=math.sqrt((y1-y)**2+(x1-x)**2) print(up(dis/(2*r)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int Mod = 1000000007; int n, k; struct Edge { int v, nxt; } e[200010]; int tot; int first[100010]; void build(int u, int v) { e[++tot] = (Edge){v, first[u]}; first[u] = tot; return; } int f[100010][210]; int siz[100010]; int sum[210]; int g[210]; void dfs(int u, int lst) { f[u][0] = 2; siz[u] = 1; for (int i = first[u]; i; i = e[i].nxt) { int v = e[i].v; if (v == lst) continue; dfs(v, u); for (int j = 0; j <= min(siz[u], k); j++) for (int l = 0; l + j <= k && l <= siz[v]; l++) g[j + l] = (g[j + l] + 1ll * f[u][j] * f[v][l]) % Mod; siz[u] += siz[v]; for (int j = 0; j <= min(siz[v], k); j++) sum[j] = (sum[j] - f[v][j] + Mod) % Mod; for (int j = 0; j <= min(siz[u], k); j++) { f[u][j] = g[j]; g[j] = 0; } } for (int i = 0; i <= min(siz[u], k); i++) sum[i] = (sum[i] + f[u][i]) % Mod; for (int i = k; i >= 1; i--) f[u][i] = (f[u][i] + f[u][i - 1]) % Mod; f[u][1] = (f[u][1] + Mod - 1) % Mod; return; } int S[210][210]; int fac[210]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); build(u, v); build(v, u); } fac[0] = 1; for (int i = 1; i <= k; i++) fac[i] = 1ll * fac[i - 1] * i % Mod; S[0][0] = 1; for (int i = 1; i <= k; i++) for (int j = 1; j <= i; j++) S[i][j] = (S[i - 1][j - 1] + 1ll * S[i - 1][j] * j) % Mod; int ans = 0; dfs(1, 0); for (int i = 1; i <= k; i++) ans = (ans + 1ll * fac[i] * S[k][i] % Mod * sum[i]) % Mod; printf("%d\n", ans); return 0; }
13
CPP
#include <iostream> #include <cstdio> #include <string> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <cmath> #include <cstring> #include <cstdlib> using namespace std; #define pb push_back #define mp make_pair #define rep(i,n) for(int i=0;i<(int)(n);++i) #define REP(i,j,k) for(int i=j;i<(int)(k);++i) #define foreach(i,c) for(__typeof(c.begin()) i=c.begin();i!=c.end();++i) #define all(x) (x).begin(),(x).end() typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; const int INF = 99999999; const double EPS = 1e-9; struct UnionFind { vi par,rank; UnionFind(int n){ par.resize(n),rank.resize(n,0); rep(i,n) par[i]=i; } int find(int x){ return par[x]==x ? x : par[x]=find(par[x]); } void unite(int x, int y){ x=find(x),y=find(y); if(x==y) return; if(rank[x]<rank[y]){ par[x]=y; }else{ par[y]=x; if(rank[x]==rank[y]) rank[x]++; } } bool same(int x, int y){ return find(x)==find(y); } }; struct Edge { int s,d,c; Edge(int s, int d, int c) : s(s),d(d),c(c){} bool operator<(const Edge &x) const{ return c < x.c; } }; int s,d,in; int main() { while(cin>>s>>d,s&&d){ vector<Edge> ve; rep(i,s)rep(j,d){ cin >> in; ve.pb(Edge(i,j+s,in)); } rep(i,d-1)REP(j,i+1,d){ cin >> in; ve.pb(Edge(i+s,j+s,in)); } UnionFind uf(s+d); sort(all(ve)); REP(i,1,s) uf.unite(0,i); // rep(i,ve.size()) cout << ve[i].c << endl; int ans = 0; rep(i,ve.size()){ if(!ve[i].c) continue; if(uf.same(ve[i].s,ve[i].d)) continue; ans+=ve[i].c; uf.unite(ve[i].s,ve[i].d); } cout << ans << endl; } return 0; }
0
CPP
def dfs(x,s): if x==n: return 1 if s%360==0 else 0 return dfs(x+1,s+a[x+1]) if dfs(x+1,s+a[x+1])==1 else dfs(x+1,s-a[x+1]) n=int(input()) a=[0,] i=0 while i<n: c=int(input()) a.append(c) i+=1 if dfs(0,0)==1: print("YES\n") else: print("NO\n")
8
PYTHON3
string = input() print(string.replace('WUB', ' '))
7
PYTHON3
inp = str(input()) print(inp[0].upper()+inp[1:])
7
PYTHON3
a,b=map(int,input().split()) n=0 while a!=b: if (b/3)%a!=0 and (b/2)%a!=0: n=-1 break elif (b/3)%a==0: n=n+1 b=b/3 elif (b/2)%a==0: n=n+1 b=b/2 print(n)
7
PYTHON3
l = [int(x) for x in input().split()] n = l[0] k = l[1] l = ([int(x) for x in input().split()]) l.sort() #l = list(reversed(l)) #print(l) a = l[n-k] while k<n: k+=1 if l[n-k] == a: continue else: k-=1 break #print(n-k) r = l[n-k:] #print(r) while len(r) > 0: if r[0] == 0: r.remove(l[0]) else: break print(len(r))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N; map<char, map<ll, set<double>>> Vs; double R = 1e20; void update(double t) { t = abs(t); double res = 1; for (char xy : {'x', 'y'}) { set<double> avs; for (const auto &d_vs : Vs[xy]) { ll d = d_vs.first; const auto &vs = d_vs.second; avs.insert(*vs.begin() + d * t); avs.insert(*vs.rbegin() + d * t); } res *= *avs.rbegin() - *avs.begin(); } R = min(R, res); } int main() { cin >> N; for (ll i = 0; i < N; ++i) { double x, y; char d; cin >> x >> y >> d; Vs['x'][d == 'R' ? 1 : d == 'L' ? -1 : 0].insert(x); Vs['y'][d == 'U' ? 1 : d == 'D' ? -1 : 0].insert(y); } update(0); for (char xy : {'x', 'y'}) { for (ll d : {0, 1, -1}) { if (!Vs[xy].count(d)) continue; for (ll d2 : {0, 1, -1}) { if (d >= d2) continue; if (!Vs[xy].count(d2)) continue; const auto &vs = Vs[xy][d], &vs2 = Vs[xy][d2]; update((*vs2.begin() - *vs.begin()) / (d - d2)); update((*vs2.rbegin() - *vs.rbegin()) / (d - d2)); } } } cout << setprecision(10) << R << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const int N = 1e5 + 5, inf = 1e9 + 5; long long add(long long x, long long y) { x += y; if (x >= MOD) return x - MOD; return x; } long long sub(long long x, long long y) { x -= y; if (x < 0) return x + MOD; return x; } long long mult(long long x, long long y) { return (x * y) % MOD; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MAX = 62; long long cnt_len[11][MAX][MAX]; long long ans = 0; long long corner_configs[11][MAX][MAX][MAX]; long long fact[5] = {1, 1, 2, 6, 24}; long long check[200]; void pre_configs() { for (int len = 3; len <= 10; ++len) { for (int corner = 0; corner < MAX; corner++) { for (int l = 0; l < MAX; l++) { for (int r = l; r < MAX; r++) { for (int u = r; u < MAX; u++) { long long curr = mult(cnt_len[len][l][corner], mult(cnt_len[len][r][corner], cnt_len[len][u][corner])); corner_configs[len][l][r][u] = add(corner_configs[len][l][r][u], curr); } } } } } } bool is_palindrome(string& s) { for (int i = 0; i < s.size() / 2; ++i) { if (s[i] != s[s.size() - 1 - i]) { return false; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<string> words(n); for (int i = 0; i < n; i++) cin >> words[i]; set<string> words_check; for (auto&& word : words) { string copy(word); reverse(begin(copy), end(copy)); if (words_check.count(copy)) continue; words_check.insert(word); int first = word[0] - 'a'; int last = word.back() - 'a'; if (word[0] <= 57) first = word[0] - '0' + 52; else if (word[0] <= 90) first = word[0] - 'A' + 26; if (word.back() <= 57) last = word.back() - '0' + 52; else if (word.back() <= 90) last = word.back() - 'A' + 26; cnt_len[word.size()][first][last]++; if (!is_palindrome(word)) cnt_len[word.size()][last][first]++; } pre_configs(); for (int len = 3; len <= 10; ++len) { for (int a = 0; a < MAX; a++) { for (int b = a; b < MAX; b++) { for (int c = b; c < MAX; c++) { for (int d = c; d < MAX; d++) { long long curr = mult(corner_configs[len][a][b][c], mult(corner_configs[len][a][b][d], mult(corner_configs[len][a][c][d], corner_configs[len][b][c][d]))); check[a]++; check[b]++; check[c]++; check[d]++; int perms = 1; perms *= fact[check[a]]; check[a] = 0; perms *= fact[check[b]]; check[b] = 0; perms *= fact[check[c]]; check[c] = 0; perms *= fact[check[d]]; check[d] = 0; curr = mult(curr, fact[4] / perms); ans = add(ans, curr); } } } } } cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAXX = 100000; long long n, ans, cnt; long long a[MAXX + 5]; int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), ans += a[i]; sort(a + 1, a + 1 + n); for (int i = 2; i <= n; i++) if (a[i] == a[i - 1]) cnt++; if (cnt > 1) { printf("cslnb\n"); return 0; } if (cnt == 1) for (int i = 2; i <= n; i++) if (a[i] == a[i - 1]) { if (i != 2 && a[i - 2] == a[i - 1] - 1) { printf("cslnb\n"); return 0; } if (a[i] == 0) { printf("cslnb\n"); return 0; } } ans -= n * (n - 1) / 2; if (ans <= 0) printf("cslnb\n"); else if (ans & 1) printf("sjfnb\n"); else printf("cslnb\n"); return 0; }
8
CPP
n = int(input()) a = list(map(int, input().split())) d = set() d.add(0) ans = 0 curr = 0 for i in range(n): curr += a[i] if curr in d: ans += 1 d = set() d.add(0) curr = a[i] d.add(curr) print(ans)
10
PYTHON3
i = int(input()) for s in range(i): q = int(input()) po = list(map(int, input().split())) res = 0 rrr = 1 curIndex = q - 1 for l in range(0, q-1): if po[curIndex - l - 1] < po[-(l + 1)]: rrr = 0 elif po[curIndex - l - 1] > po[-(l + 1)]: if rrr == 0: res = q - l - 1 break rrr=1 print(res)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1000010; vector<int> a[N]; int l[N], v[N]; int main() { int n, c; scanf("%d%d", &n, &c); for (int i = 1; i <= n; i++) { scanf("%d", &l[i]); for (int j = 0; j < l[i]; j++) { int x; scanf("%d", &x); a[i].push_back(x); } } memset(v, 0, sizeof(v)); for (int i = 2; i <= n; i++) { int p = 0; while (p < l[i - 1] && p < l[i] && a[i - 1][p] == a[i][p]) p++; if (p >= l[i - 1] && p >= l[i]) { v[0]++; v[c]--; } else if (p >= l[i]) { } else if (p >= l[i - 1]) { v[0]++; v[c]--; } else { int x = a[i - 1][p], y = a[i][p]; if (x < y) { if (c - y + 1 > 0) { v[0]++; v[c - y + 1]--; } if (c > c - x + 1) { v[c - x + 1]++; v[c]--; } } else { v[c - x + 1]++; v[c - y + 1]--; } } } int ans = -1, sum = 0; for (int i = 0; i < c; i++) { sum += v[i]; if (sum == n - 1) { ans = i; break; } } printf("%d\n", ans); return 0; }
10
CPP
#include <iostream> #include <set> #include <map> using namespace std; int M,N; map<pair<set<int>,set<int>>,int> m; char F[129][12]; int dfs(set<int> ob,set<int> fe){ if(ob.size()<=1) return (M-(int) fe.size()); if(m.count({ob,fe})) return m[{ob,fe}]; int res = 1e9; set<int> fe2 = fe; for(auto x:fe){ set<int> ob0,ob1; for(auto y:ob){ if(F[y][x]=='0') ob0.insert(y); else ob1.insert(y); } fe2.erase(x); res = min(res,max(dfs(ob0,fe2),dfs(ob1,fe2))); fe2.insert(x); } return m[{ob,fe}] = res; } int main(){ while(cin >> M >> N && N>0){ m.clear(); for(int i=0;i<N;i++) for(int j=0;j<M;j++) cin >> F[i][j]; set<int> ob,fe; for(int i=0;i<N;i++) ob.insert(i); for(int j=0;j<M;j++) fe.insert(j); cout << dfs(ob,fe) << endl; } }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 200009, maxC = 1e6 + 9; struct station { long long x, c; } p[N]; long long d, V, n, mac, nxt[N], cost, INF; bool cmpX(station A, station B) { return A.x < B.x; } long long lb[maxC << 2], rb[maxC << 2], minv[maxC << 2]; void Build(long long l, long long r, long long i) { lb[i] = l; rb[i] = r; minv[i] = INF; if (l < r) { Build(l, ((lb[i] + rb[i]) / 2), (i * 2)); Build(((lb[i] + rb[i]) / 2) + 1, r, ((i * 2) + 1)); } } void modify(long long p, long long v, long long i) { if (lb[i] == rb[i]) { minv[i] = min(v, minv[i]); } else { if (p <= ((lb[i] + rb[i]) / 2)) modify(p, v, (i * 2)); else modify(p, v, ((i * 2) + 1)); minv[i] = min(minv[(i * 2)], minv[((i * 2) + 1)]); } } long long findmin(long long l, long long r, long long i) { if (l > r) return INF; if (l == lb[i] && r == rb[i]) return minv[i]; return min(findmin(l, min(((lb[i] + rb[i]) / 2), r), (i * 2)), findmin(max(((lb[i] + rb[i]) / 2) + 1, l), r, ((i * 2) + 1))); } int main() { scanf("%I64d%I64d%I64d", &d, &V, &n); INF = d; for (long long i = 1; i <= (n); i++) { scanf("%I64d%I64d", &p[i].x, &p[i].c); mac = max(mac, p[i].c); } sort(p + 1, p + n + 1, cmpX); p[n + 1].x = d; p[n + 1].c = 0; Build(0, mac, 1); modify(0, d, 1); for (long long i = (n); i >= (1); i--) { nxt[i] = findmin(0, p[i].c, 1); modify(p[i].c, p[i].x, 1); } long long gas = V; for (long long i = 1; i <= (n); i++) { gas = gas + p[i - 1].x - p[i].x; if (gas < 0) { printf("-1\n"); return 0; } if (nxt[i] - p[i].x > gas) { if (nxt[i] - p[i].x >= V) { cost += (V - gas) * p[i].c; gas = V; } else { cost += (nxt[i] - p[i].x - gas) * p[i].c; gas = nxt[i] - p[i].x; } } } gas = gas + p[n].x - d; if (gas < 0) { printf("-1\n"); return 0; } printf("%I64d\n", cost); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long int max2(long long int a, long long int b) { return (a > b ? a : b); } long long int min2(long long int a, long long int b) { return (a > b ? b : a); } long long int max3(long long int a, long long int b, long long int c) { return (a > max2(b, c) ? a : max2(b, c)); } long long int min3(long long int a, long long int b, long long int c) { return (a < min2(b, c) ? a : min2(b, c)); } bool isPrime(long long int n) { long long int i; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void solve() { long long int n; cin >> n; long long int ar[n][2], i; for (i = 0; i < n; i++) cin >> ar[i][0] >> ar[i][1]; long long int len[100] = {0}, j, ans = 0; for (i = 1; i < n; i++) { for (j = ar[i][0]; j < ar[i][1]; j++) { len[j] = 1; } } for (i = ar[0][0]; i < ar[0][1]; i++) { if (len[i] == 0) ans++; } cout << ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long int t = 1; while (t--) { solve(); cout << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int sqrtmaxn = (int)sqrt(maxn) + 1; vector<int> v[maxn]; int ans; inline bool find(int x, int y) { if (x >= maxn) return false; return binary_search(v[x].begin(), v[x].end(), y); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y); } for (int i = 0; i < maxn; i++) sort(v[i].begin(), v[i].end()); for (int x = 0; x < maxn; x++) { if (v[x].size() <= sqrtmaxn) { for (int i = 0; i < v[x].size(); i++) { for (int j = i + 1; j < v[x].size(); j++) { int d = v[x][j] - v[x][i]; if (find(x + d, v[x][i]) && find(x + d, v[x][j])) ans++; } } } else { for (int xx = x + 1; xx < maxn; xx++) { for (int i = 0; i < v[xx].size(); i++) { int yy = v[xx][i], d = xx - x; if (find(x, yy) && find(x, yy + d) && find(xx, yy + d)) ans++; } } } } printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 30000, INF = 0x3f3f3f3f; int n, m; char s[600][600]; int ans = INF; int dis[N], vis[N], head[N], tot; struct edge { int to, pre, w; } e[N << 2]; void adde(int u, int v, int w) { tot++; e[tot].to = v; e[tot].pre = head[u]; head[u] = tot; e[tot].w = w; } int zb(int x, int y) { return (x - 1) * m + y; } void spfa(int ss) { queue<int> q; memset(dis, 0x3f, sizeof(dis)); dis[ss] = 0; q.push(ss); vis[ss] = 1; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for (int i = head[u]; i; i = e[i].pre) { int v = e[i].to; if (dis[v] > dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; if (vis[v] == 0) { vis[v] = 1; q.push(v); } } } } int tmp = -1; for (int i = 1; i <= n * m; i++) { if (s[(i - 1) / m + 1][(i - 1) % m + 1] == 'B') tmp = max(tmp, dis[i] + 1); else tmp = max(tmp, dis[i]); } ans = min(ans, tmp); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (i > 1) adde(zb(i, j), zb(i - 1, j), (s[i][j] != s[i - 1][j])); if (i < n) adde(zb(i, j), zb(i + 1, j), (s[i][j] != s[i + 1][j])); if (j > 1) adde(zb(i, j), zb(i, j - 1), (s[i][j] != s[i][j - 1])); if (j < m) adde(zb(i, j), zb(i, j + 1), (s[i][j] != s[i][j + 1])); } for (int i = 1; i <= n * m; i++) spfa(i); printf("%d", ans); return 0; }
11
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int dx[] = {0, 1}; int dy[] = {1, 0}; struct query { int l; int sz; }; void say(const vector<query>& qs) { cout << qs.size() << endl; for (auto q : qs) { int sz = q.sz; int a = q.l + 1; int b = a + sz / 2; int c = b + sz / 2; printf("%d %d %d\n", a, b, c); } } void add(vector<query>& anss, vector<int>& v, query q) { anss.push_back(q); int a = q.l; int b = q.l + q.sz / 2; int c = b + q.sz / 2; v[a] = !v[a]; v[b] = !v[b]; v[c] = !v[c]; } int main() { vector<vector<int>> all_oks(14); vector<vector<vector<query>>> all_anss(14); for (int x = 3; x < 14; ++x) { vector<int> oks(1 << x); vector<vector<query>> anss(1 << x); oks[0] = true; queue<int> que; que.push(0); while (!que.empty()) { int now = que.front(); que.pop(); for (int sz = 3; sz <= x; sz += 2) { for (int a = 0; a <= x - sz; ++a) { int next = now; int b = a + sz / 2; int c = b + sz / 2; next ^= (1 << a); next ^= (1 << b); next ^= (1 << c); if (!oks[next]) { oks[next] = true; anss[next] = anss[now]; anss[next].push_back(query{a, sz}); que.push(next); } } } } all_oks[x] = oks; all_anss[x] = anss; } int N; scanf("%d", &N); vector<int> v(N); for (int i = 0; i < N; ++i) scanf("%d", &v[i]); vector<query> my_answer; if (N < 14) { int num = 0; for (int i = 0; i < N; ++i) { num += (1 << i) * v[i]; } if (all_oks[N][num]) { my_answer = all_anss[N][num]; cout << "YES" << endl; say(my_answer); } else { cout << "NO" << endl; } } else { int offset = 0; while (N - offset >= 14) { string st; for (int i = 0; i < 6; ++i) { if (v[i + offset] == 1) st.push_back('1'); else st.push_back('0'); } string pre = st.substr(0, 3); int one_cnt = count(pre.begin(), pre.end(), '1'); if (one_cnt == 3) { add(my_answer, v, query{offset, 3}); } else if (one_cnt == 0) { } else if (one_cnt == 1) { int place = st.find('1'); add(my_answer, v, query{offset + place, 7}); } else if (one_cnt == 2) { if (pre == "110") { string nex = st.substr(3, 3); int nex_one_cnt = count(nex.begin(), nex.end(), '1'); if (nex_one_cnt == 3) { add(my_answer, v, query{offset, 9}); add(my_answer, v, query{offset + 1, 5}); } else if (nex_one_cnt == 2) { int aa, bb; if (nex == "110") { aa = 0; bb = 1; } else if (nex == "101") { aa = 0; bb = 2; } else if (nex == "011") { aa = 1; bb = 2; } add(my_answer, v, query{offset, (aa + 3) * 2 + 1}); add(my_answer, v, query{offset + 1, (bb + 2) * 2 + 1}); } else if (nex_one_cnt == 1) { int aa = nex.find('1'); add(my_answer, v, query{offset, (aa + 3) * 2 + 1}); add(my_answer, v, query{offset + 1, 11}); } else if (nex_one_cnt == 0) { add(my_answer, v, query{offset, 13}); add(my_answer, v, query{offset + 1, 11}); } offset += 3; } else if (pre == "101") { add(my_answer, v, query{offset, 5}); } else if (pre == "011") { add(my_answer, v, query{offset + 1, 3}); } } offset += 3; } int num = 0; for (int i = 0; i < N - offset; ++i) { num += (1 << i) * v[offset + i]; } assert(all_oks[N - offset][num]); vector<query> add_answer = all_anss[N - offset][num]; for (auto& a_ans : add_answer) a_ans.l += offset; my_answer.insert(my_answer.end(), add_answer.begin(), add_answer.end()); cout << "YES" << endl; say(my_answer); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int Max_N = 1e5; int n, cnt = 1, lst = 1, Num, lin[Max_N * 2 + 5], T[Max_N * 2 + 5][26], len[Max_N * 2 + 5], p[Max_N * 2 + 5], tot[Max_N * 2 + 5]; char s[Max_N + 5]; long long ans; struct kk { int Id, Next; } e[Max_N * 2 + 5]; inline void Insert(int x, int y) { e[++Num].Next = lin[x]; lin[x] = Num; e[Num].Id = y; } inline void Add(int x) { int New = ++cnt; len[New] = len[lst] + 1; tot[New] = 1; for (; lst && !T[lst][x]; T[lst][x] = New, lst = p[lst]) ; if (!lst) p[New] = 1; else { int q = T[lst][x]; if (len[lst] + 1 == len[q]) p[New] = q; else { int nq = ++cnt; p[nq] = p[q]; p[q] = p[New] = nq; len[nq] = len[lst] + 1; for (int i = 0; i < 26; i++) T[nq][i] = T[q][i]; for (; lst && T[lst][x] == q; T[lst][x] = nq, lst = p[lst]) ; } } lst = New; } inline void dfs(int x) { for (int i = lin[x]; i; i = e[i].Next) { dfs(e[i].Id); tot[x] += tot[e[i].Id]; } ans += 1ll * tot[x] * (tot[x] + 1) / 2 * (len[x] - len[p[x]]); } int main() { scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; i++) Add(s[i] - 'a'); for (int i = 1; i <= cnt; i++) Insert(p[i], i); dfs(1); cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200010; long long mod = 1e9 + 7; long long fmod(long long b, long long exp) { long long res = 1; while (exp) { if (exp & 1ll) res = (res * b) % mod; b = (b * b) % mod; exp /= 2ll; } return res; } int A[N], B[N], val[N], lst[N], mx[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, i, j, k, x, q, ind; int t = 1; cin >> n; for (i = 1; i <= n; i++) { cin >> A[i]; } cin >> q; int typ; memset(B, -1, sizeof(B)); for (i = 1; i <= q; i++) { cin >> typ; if (typ == 1) { cin >> ind >> x; B[ind] = x; lst[ind] = i; } else { cin >> val[i]; } } for (int i = q; i >= 1; i--) mx[i] = max(mx[i + 1], val[i]); for (i = 1; i <= n; i++) { if (B[i] + 1) { B[i] = max(mx[lst[i]], B[i]); } else { B[i] = max(A[i], mx[1]); } } for (i = 1; i <= n; i++) cout << B[i] << " "; cout << "\n"; return 0; }
8
CPP
entrada = int(input()) modulo = 1000000007 a = 1 b = 1 for j in range(entrada): b = (b * 7) % modulo tamanho = entrada*3 for i in range(tamanho): a = (a * 3) % modulo print((a - b + modulo) % modulo)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 50005; int n, m, K; long long ans[N]; struct ff { int a, b, c, d; } A[N]; struct gg { int x, l, r, w; bool operator<(const gg& k) const { return x < k.x; } } B[N * 2]; vector<int> v; namespace SGT { int tg[N * 8], sum[N * 8]; void upd(int k1, int k2, int k3) { if (tg[k1]) { sum[k1] = v[k3] - v[k2 - 1]; } else { if (k2 != k3) sum[k1] = sum[k1 * 2] + sum[k1 * 2 + 1]; else sum[k1] = 0; } } void bud(int k1, int k2, int k3) { tg[k1] = 0, sum[k1] = 0; if (k2 == k3) { return; } int mid = (k2 + k3) >> 1; bud(k1 * 2, k2, mid), bud(k1 * 2 + 1, mid + 1, k3); upd(k1, k2, k3); } void mdf(int k1, int k2, int k3, int k4, int k5, int k6) { if (k2 > k5 || k3 < k4) return; if (k4 <= k2 && k3 <= k5) { tg[k1] += k6; upd(k1, k2, k3); return; } int mid = (k2 + k3) >> 1; mdf(k1 * 2, k2, mid, k4, k5, k6), mdf(k1 * 2 + 1, mid + 1, k3, k4, k5, k6); upd(k1, k2, k3); } } // namespace SGT long long sol(int len) { int cnt = 0; for (int i = (1); i <= (m); ++i) { int a = (A[i].a + len - 1) / len; int c = A[i].c / len; int b = (A[i].b + len - 1) / len; int d = A[i].d / len; B[++cnt] = (gg){a, b, d, 1}; B[++cnt] = (gg){c + 1, b, d, -1}; } auto get = [&](int x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }; { v.clear(); for (int i = (1); i <= (cnt); ++i) v.push_back(B[i].l - 1), v.push_back(B[i].r); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (int i = (1); i <= (cnt); ++i) B[i].l = get(B[i].l), B[i].r = get(B[i].r); } sort(B + 1, B + 1 + cnt); SGT::bud(1, 0, ((int)(v).size()) - 1); long long res = 0; for (int i = 1, j; i <= cnt; i = j) { j = i; res += 1LL * (B[i].x - B[i - 1].x) * SGT::sum[1]; while (j <= cnt && B[i].x == B[j].x) { SGT::mdf(1, 0, ((int)(v).size()) - 1, B[j].l, B[j].r, B[j].w); ++j; } } return res; } int main() { scanf("%d%d%d", &n, &m, &K); for (int i = (1); i <= (m); ++i) { scanf("%d%d%d%d", &A[i].a, &A[i].b, &A[i].c, &A[i].d); } for (int i = (0); i <= (30); ++i) { if ((1 << i) > K) break; ans[i] = sol(1 << i); } for (int i = (0); i <= (30); ++i) { if ((1 << i) > K) break; if ((ans[i] - ans[i + 1]) & 1) { puts("Hamed"); exit(0); } } puts("Malek"); return 0; }
11
CPP
import string n, k = map(int, input().split()) s, d, v = input(), [0] * (n + 1), 0 for ch in string.ascii_uppercase: if ch in s: d[s.find(ch)] += 1 d[s.rfind(ch) + 1] -= 1 for x in d: v += x if v > k: print('YES') exit() print('NO')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; vector<int> G[maxn], ans; int a[maxn], b[maxn]; void dfs(int now, int fa, vector<int> tag, vector<bool> mod, int dep) { if (mod[dep & 1]) a[now] ^= tag[dep & 1]; if (a[now] != b[now]) mod[dep & 1] = true, tag[dep & 1] ^= 1, ans.push_back(now); for (int u : G[now]) if (u != fa) dfs(u, now, tag, mod, dep + 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; dfs(1, 0, {0, 0}, {false, false}, 0); cout << ans.size() << endl; for (int i : ans) cout << i << endl; return 0; }
9
CPP
k, n, s, p = map(int, input().split()) cont = 0 for i in range(k): aux = s while aux < n: aux += s cont += aux if cont%(p*s) == 0: print(int((cont/(p*s)))) else: print(int((cont/(p*s)+1)))
7
PYTHON3
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") #define int long long #define eb emplace_back #define pb push_back #define ld long double //#define mp make_pair #define f first #define s second #define fast() { \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout << fixed << setprecision(7); \ cerr << fixed << setprecision(11); \ } const int INF = 1e18; const ld EPS = 1e-7; const ld PI = 3.14159265358979323; const int MAXN = 1e5 + 5; const int MOD = 1e9 + 7; int r[1005]; int b[1005]; signed main() { //fast(); int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; ++i) { char c; cin >> c; r[i] = c - '0'; } for (int i = 0; i < n; ++i) { char c; cin >> c; b[i] = c - '0'; } int cntr = 0, cntb = 0; for (int i = 0; i < n; ++i) { if (r[i] > b[i]) { ++cntr; } else if (r[i] < b[i]) { ++cntb; } } /*for (int i = 0; i < n; ++i) { cout << r[i] << ' '; } cout << '\n'; for (int i = 0; i < n; ++i) { cout << b[i]; } cout << '\n'; cout << cntr << ' ' << cntb << '\n';*/ if (cntr > cntb) { cout << "RED\n"; } else if (cntr < cntb) { cout << "BLUE\n"; } else { cout << "EQUAL\n"; } } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); puts("YES"); for (int i = 0; i < n; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); printf("%d\n", 1 + (x1 % 2 == 0 ? 0 : 1) + (y1 % 2 == 0 ? 0 : 2)); } return 0; }
8
CPP
for _ in range(int(input())): n = int(input()) a = list(map('12'.__contains__, input().strip())) b = list(map('12'.__contains__, input().strip())) u, d = True, False for i in range(n): if a[i]: if not b[i]: d = False else: if b[i]: u = False else: u, d = d, u if not (u or d): break print('YES' if d else 'NO')
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int read() { int x = 0, w = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') w = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar(); return x * w; } const int inf = 0x3f3f3f3f, N = 2e5 + 10; const long long Mod = 998244353; struct node { long long x, a, b, c, d; } num[5010]; long long nxt[N]; int n, S, E; long long get_f(node t1, node t2) { if (t2.x < t1.x) return abs(t2.x - t1.x) + t1.c + t2.b; else return abs(t2.x - t1.x) + t1.d + t2.a; } int main() { n = read(); S = read(); E = read(); for (int i = 1; i <= n; i++) num[i].x = read(); for (int i = 1; i <= n; i++) num[i].a = read(); for (int i = 1; i <= n; i++) num[i].b = read(); for (int i = 1; i <= n; i++) num[i].c = read(); for (int i = 1; i <= n; i++) num[i].d = read(); nxt[S] = E; long long ans = get_f(num[S], num[E]); for (int i = 1; i <= n; i++) { if (i == S || i == E) continue; long long sum = 1e18; int sav = 0; for (int j = S; j != E; j = nxt[j]) { long long tmp = get_f(num[j], num[i]) + get_f(num[i], num[nxt[j]]) - get_f(num[j], num[nxt[j]]); if (tmp < sum) sum = tmp, sav = j; } ans += sum; nxt[i] = nxt[sav]; nxt[sav] = i; } printf("%lld\n", ans); return 0; }
8
CPP
# cook your dish here for i in range(int(input())): n,m=map(int,input().split()) a=list(map(int,input().split())) f=0 b=list(map(int,input().split())) for k in a: if k in b: f=1 print("YES") print(1,k) break if f==0: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 210; int dp[25][5][15][2]; int main() { int n, t; scanf("%d%d", &n, &t); for (int j = 1; j <= 4; j++) dp[1][j][0][1] = 1; for (int i = 2; i <= n; i++) { for (int k = 1; k <= t; k++) { for (int j = 1; j <= 4; j++) { for (int pre = 1; pre <= 4; pre++) { if (pre > j) { dp[i][j][k][1] += dp[i - 1][pre][k][0]; dp[i][j][k][1] += dp[i - 1][pre][k][1]; } else if (pre < j) { dp[i][j][k][0] += dp[i - 1][pre][k - 1][1]; dp[i][j][k][0] += dp[i - 1][pre][k][0]; } } } } } int ans = 0; for (int i = 1; i <= 4; i++) ans += dp[n][i][t][1]; printf("%d\n", ans); return 0; }
11
CPP
n,m,k = map(int, input().split()) a = list(map(int, input().split())) a1 = a[m:] a2 = list(reversed(a[:m-1])) res1, res2 = 10**8, 10**8 for i, h in enumerate(a1): if 0 < h <= k: res1 = i+1 break for i, h in enumerate(a2): if 0 < h <= k: res2 = i+1 break print(min(res1, res2)*10)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ int l,r,d;cin >> l >> r >> d; cout << r/d-(l-1)/d << "\n"; }
0
CPP
n=int(input()) #d=[1,55,8,'g'] #print(d) while n!=0 : n-=1 p=int(input()) tem=input() tem=tem.split(' ') for i in range(4*p): tem.append(int(tem[0])) tem.pop(0) tem.sort() flag=True ar=tem[0]*tem[4*p-1] for i in range(2*p): if tem[2*i+1]!=tem[2*i]: flag=False break if tem[i]*tem[4*p-1-i]!=ar: flag=False break if flag: print("YES") else: print("NO")
8
PYTHON3
no_of_test_cases = int(input()) for i in range(0, 2*no_of_test_cases, 2): length = int(input()) string = input() list_string = [] fixed_list = [] for k in range(0, length-1): if (length-k)%2 == 0: list_string = list_string + [string[k:] + string[:k]] fixed_list = fixed_list + [string[k:] + string[:k]] if (length-k)%2 == 1: list_string = list_string + [string[k:] + string[:k][::-1]] fixed_list = fixed_list + [string[k:] + string[:k][::-1]] list_string = list_string + [string[::-1]] fixed_list = fixed_list + [string[::-1]] list_string.sort() req_ele = list_string[0] req_k = fixed_list.index(req_ele) print(req_ele) print(req_k + 1)
8
PYTHON3
k, n, w = map(int, input().split()) s = ((w * (w+1)) // 2) * k if s - n < 0: print(0) else: print(s - n)
7
PYTHON3
a = input() b = a.split() v = len(b) number = int(b[0]) times = int(b[1]) reference = 0 while reference < times: if(number % 10 == 0): number = int(number/10) else: number = int(number-1) reference+=1 print(number)
7
PYTHON3
from sys import stdin n = int(stdin.readline()) s = stdin.readline().rstrip() c = 0 groups = [] for char in s: if char == 'B': c += 1 elif c > 0: groups.append(c) c = 0 if s[-1] == 'B': groups.append(c) print(len(groups)) for group in groups: print('{} '.format(group), end='')
7
PYTHON3
n=int(input()) cnt=0 for i in range (n): l=list(map(int,input().split())) if(l.count(1)>=2): cnt=cnt+1 print(cnt)
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; bool home = 1; signed realMain(); signed main() { home = 0; if (home) { freopen("tony_stark", "r", stdin); } else { ios::sync_with_stdio(0); cin.tie(0); } realMain(); } const int N = (int)5e4 + 7; const int Q = (int)5e3 + 7; const int L = (int)1e6 + 7; int n, q, a[N], tab[N]; int pre[L]; int sol[Q]; vector<pair<int, int>> question[N]; int eval(int x, int y) { if (x <= y) return pre[y] ^ pre[x - 1]; return pre[x] ^ pre[y - 1]; } signed realMain() { for (int i = 1; i < L; i++) { pre[i] = pre[i - 1] ^ i; } cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; question[r].push_back({l, i}); } for (int r = 1; r <= n; r++) { for (int i = r; i >= 1; i--) { tab[i] = max(tab[i], eval(a[i], a[r])); tab[i] = max(tab[i], tab[i + 1]); } for (auto &it : question[r]) { sol[it.second] = tab[it.first]; } } for (int i = 1; i <= q; i++) { cout << sol[i] << "\n"; } return 0; }
12
CPP
import math samu=int(input()) birthday=input().split() birthday=list(map(int,birthday)) special=sum(birthday) sakib=max(birthday) sakumas=(2*special/samu) if sakumas == math.floor(sakumas): sakumas=int(sakumas+1) else: sakumas=int(math.ceil(sakumas)) print(max(sakib,sakumas))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int cumj[1010][1010]; int cumo[1010][1010]; int cumi[1010][1010]; int main(void) { int m, n, k; cin >> m >> n >> k; vector<string> s(m); for (int i = 0; i < m; i++) { cin >> s[i]; for (int j = 0; j < n; j++) { cumj[i + 1][j + 1] = s[i][j] == 'J'; cumo[i + 1][j + 1] = s[i][j] == 'O'; cumi[i + 1][j + 1] = s[i][j] == 'I'; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cumj[i][j] += cumj[i - 1][j] + cumj[i][j - 1] - cumj[i - 1][j - 1]; cumo[i][j] += cumo[i - 1][j] + cumo[i][j - 1] - cumo[i - 1][j - 1]; cumi[i][j] += cumi[i - 1][j] + cumi[i][j - 1] - cumi[i - 1][j - 1]; } } for (int i = 0; i < k; i++) { int a, b, c, d; cin >> a >> b >> c >> d; cout << cumj[c][d] - cumj[a - 1][d] - cumj[c][b - 1] + cumj[a - 1][b - 1] << " "; cout << cumo[c][d] - cumo[a - 1][d] - cumo[c][b - 1] + cumo[a - 1][b - 1] << " "; cout << cumi[c][d] - cumi[a - 1][d] - cumi[c][b - 1] + cumi[a - 1][b - 1] << endl; } return 0; }
0
CPP
for i in range(int(input())): n = int(input()) arr = list(map(int,input().split())) c = 0 for i in arr: if i!=2: c += 1 print(c)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long pow1(long long b, long long expo, long long md) { if (!expo) return 1; long long tmp = pow1(b, expo / 2, md); tmp = (tmp * tmp) % md; if (expo % 2) tmp = (tmp * b) % md; return tmp; } long long w[1000005] = {}; long long apow[1000005] = {}; long long inv[1000005] = {}; long long v[1000005] = {}; long long get(long long a, long long m1, long long b, long long m2, long long x) { long long ret = 0; long long first, k; k = (a - b) * inv[m2]; k %= m1; k += m1; k %= m1; first = k * m2 + b; first %= m1 * m2; x -= first; if (x >= 0) { ret += 1 + x / (m1 * m2); } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(15); long long md, a, b, x, per; long long ans = 0; for (long long f = (long long)0; f <= (long long)1000004; ++f) v[f] = -1; cin >> a >> b >> md >> x; apow[0] = 1; v[1] = 0; per = 1; if (a != 1) { apow[1] = a; v[a] = 1; for (long long f = (long long)2; f <= (long long)md; ++f) { apow[f] = a * apow[f - 1]; apow[f] %= md; if (apow[f] == 1) { per = f; break; } v[apow[f]] = f; } } for (long long f = (long long)1; f <= (long long)md - 1; ++f) { inv[f] = pow1(f, md - 2, md); } for (long long f = (long long)1; f <= (long long)md - 1; ++f) { w[f] = (inv[f] * b) % md; } for (long long f = (long long)1; f <= (long long)md - 1; ++f) { if (v[w[f]] != -1) { ans += get(f, md, v[w[f]], per, x); } } cout << ans; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, p1, p2, sum = 0; int maxn = 1e9 + 7, minn = -1e9 - 7; cin >> n; for (int i = 0; i < n; i++) { cin >> p1 >> p2; if (p2 == 1) minn = max(minn, 1900 - sum); else maxn = min(maxn, 1899 - sum); sum += p1; } if (minn > maxn) cout << "Impossible"; else if (maxn == 1e9 + 7) cout << "Infinity"; else cout << maxn + sum; }
9
CPP
a = input() b = input() pnt = 1 for i in range(len(b)): if a[pnt-1] == b[i]: pnt+=1 print(pnt)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, q; cin >> n >> q; int unr = 0; queue<pair<int, int> > noti; int q_num = 0; vector<queue<int> > v(n + 1); while (q--) { int t, x; cin >> t >> x; if (t == 1) { unr++; q_num++; v[x].push(q_num); noti.push({q_num, x}); } else if (t == 2) { unr -= v[x].size(); v[x] = {}; } else { while (!noti.empty() && noti.front().first <= x) { int id = noti.front().second; int val = noti.front().first; noti.pop(); if (!v[id].empty() && v[id].front() == val) { v[id].pop(); unr--; } } } cout << unr << "\n"; } }
7
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const double eps = 1e-18; const unsigned long long base = 13331; const int mod = 2333; int main() { std::ios::sync_with_stdio(false); cin.tie(0); long long u, v; cin >> u >> v; if (u > v) { cout << -1; } else if ((v - u) % 2 != 0) { cout << -1; } else if (!u && !v) { cout << 0; } else if (u == v) { cout << 1 << '\n'; cout << u; } else { long long ff = (v - u) / 2; long long gg = u ^ ff; if (ff + gg == v) { cout << 2 << '\n'; cout << ff << " " << gg; } else { cout << 3 << '\n'; cout << u << " " << ff << " " << ff; } } }
10
CPP
K, S = map(int, input().split()) print(sum(0<=S-x-y<=min(K,S) for x in range(K+1) for y in range(K+1)))
0
PYTHON3
a=int(input()) b=int(input()) ab=a+b sumab=[] suma=[] sumb=[] a2=0 b2=0 ab2=0 coun1=-1 coun2=-1 coun3=-1 while a>0: lasta=a%10 suma=suma+[lasta] a=a//10 while b>0: lastb=b%10 sumb=sumb+[lastb] b=b//10 while ab>0: lastab=ab%10 sumab=sumab+[lastab] ab=ab//10 for i1 in range(len(suma)): if suma[i1]!=0: coun1+=1 a2=a2+(suma[i1]*10**coun1) for i2 in range(len(sumb)): if sumb[i2]!=0: coun2+=1 b2=b2+(sumb[i2]*10**coun2) for i3 in range(len(sumab)): if sumab[i3]!=0: coun3+=1 ab2=ab2+(sumab[i3]*10**coun3) if a2+b2==ab2: print('YES') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 9; int n, m, p; int a[N], b[N]; int main() { scanf("%d%d%d", &n, &m, &p); int t1 = N, t2 = N; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] % p) { t1 = min(t1, i); } } for (int i = 0; i < m; i++) { scanf("%d", &b[i]); if (b[i] % p) { t2 = min(t2, i); } } printf("%d\n", t1 + t2); return 0; }
9
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; int a, b, c, d, n, m, k; char s1[2000002], s2[2000002]; int cnt[3]; int main() { scanf("%d", &n); scanf("%s%s", s1, s2); for (int _n((2 * n) - 1), i(0); i <= _n; i++) { if (s1[i] == '1' && s2[i] == '1') ++c; else if (s1[i] == '1' && s2[i] == '0') ++a; else if (s1[i] == '0' && s2[i] == '1') ++b; } int a1 = 0, a2 = 0; d = 0; while (a + b + c) { if (d % 2 == 0) { if (c) { ++a1; --c; } else if (b) { --b; } else { ++a1; --a; } } else { if (c) { ++a2; --c; } else if (a) { --a; } else { ++a2; --b; } } ++d; } if (a2 > a1) printf("Second\n"); else if (a1 > a2) printf("First\n"); else printf("Draw\n"); }
7
CPP
#include<bits/stdc++.h> using namespace std; int main(){ int A; int B; cin>>A>>B; cout<<A%B<<endl; }
0
CPP
for _ in range(int(input())): s=input() i1=None i2=None i3=None n=len(s) min1=n+1 nk=0 for i in range(0,n): if s[i]=='1': i1=i elif s[i]=='2': i2=i else: i3=i if i1!=None and i2!=None and i3!=None: nd=max([i1,i2,i3])-min([i1,i2,i3]) nd=nd+1 #print('i',min1) if min1>nd: min1=nd nk=1 #print(nk) if nk==0: print(0) else: print(min1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 5; int n, t, ans; char s[N]; bitset<N> vis; bool chk1(int i) { return s[i] == 'o' && s[i + 1] == 'n' && s[i + 2] == 'e'; } bool chk2(int i) { return i >= 1 && s[i] == 't' && s[i + 1] == 'w' && s[i + 2] == 'o'; } int main() { scanf("%d", &t); while (t--) { vis.reset(); scanf("%s", s + 1); ans = 0; n = strlen(s + 1); for (int i = 1; i <= n - 2; ++i) if (chk2(i)) ++ans, vis[i + 1] = 1; else if (chk1(i)) { if (!chk2(i - 2)) ++ans, vis[i + 1] = 1; else vis[i] = 1, vis[i - 1] = 0; } printf("%d\n", ans); for (int i = 1; i <= n; ++i) if (vis[i]) printf("%d ", i); puts(""); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long a[200008], res[200008]; int main() { long long n, m; scanf("%lld", &n); scanf("%lld", &m); long long i, j, k, l; for (i = 0; i < m; i++) scanf("%lld", &a[i]); for (i = 0; i < m - 1; i++) { k = min(a[i], a[i + 1]); l = max(a[i], a[i + 1]); if (k == l) continue; int p1, p2; p1 = l - k; p2 = l - k - 1; res[1] += p1; res[k] -= p1; res[l + 1] += p1; res[n + 1] -= p1; res[k + 1] += p2; res[l] -= p2; res[k] += l - 1; res[k + 1] -= l - 1; res[l] += k + 1 - 1; res[l + 1] -= k + 1 - 1; } long long ans = 0; for (i = 1; i <= n; i++) { ans += res[i]; cout << ans << " "; } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000; const int INF = 1e9 + 107; const int MOD = 998244353; void IOS() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { IOS(); int t; cin >> t; while (t--) { int n; cin >> n; int x; cin >> x; vector<int> v(2, 0); for (int i = 0; i < n; i++) { int x; cin >> x; v[x % 2] += 1; } bool ok = false; for (int cur = 1; cur <= x; cur += 2) { if (v[1] >= cur && v[0] >= x - cur) ok = true; } if (ok) { cout << "Yes\n"; } else { cout << "No\n"; } } }
7
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int cla[maxn], ned[maxn]; bool vis[maxn]; int n, m; bool work(int st) { memset(vis, 0, sizeof vis); int sum = 0; int nedday = 0; for (int j = st; j > 0; --j) { if (cla[j] > 0 && vis[cla[j]] == 0) { vis[cla[j]] = 1; nedday += ned[cla[j]]; ++sum; } else { if (nedday) --nedday; } } if (sum == m && nedday == 0) return 1; else return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); ; cin >> n >> m; for (int j = 1; j <= n; ++j) { cin >> cla[j]; } for (int j = 1; j <= m; ++j) { cin >> ned[j]; } if (!work(n)) cout << "-1" << endl; else { int l = 1, r = n; while (l + 1 < r) { int mid = (l + r) / 2; if (work(mid)) r = mid; else l = mid; } cout << r << endl; } return 0; }
10
CPP
#include<bits/stdc++.h> using namespace std; int main() { long N,A,B; cin>>N>>A>>B; if((B-A)%2==0)cout<<(B-A)/2<<endl; else cout<<min(A+(B-A)/2,N-B+1+(B-A)/2)<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long MAX_N = 100 * 1000 + 24, INF = (1 << 30); int n, m, q, a, b, c, ans, h; map<int, vector<int>> G[MAX_N]; map<int, int> mm[MAX_N]; map<pair<int, int>, int> out; bool mark[MAX_N]; set<int> cl[MAX_N], cs; void dfs(int v, int k, int c) { mark[v] = true; mm[v][c] = k; if (G[v].count(c) > 0) { for (int u : G[v][c]) { if (mark[u] == false) { dfs(u, k, c); } } } } int main() { cin.tie(0), cout.tie(0), ios::sync_with_stdio(false); scanf("%d %d", &n, &m); while (m--) { scanf("%d %d %d", &a, &b, &c); G[a][c].push_back(b); G[b][c].push_back(a); cl[c].insert(a); cs.insert(c); } for (set<int>::iterator i = cs.begin(); i != cs.end(); i++) { memset(mark, 0, sizeof mark); for (set<int>::iterator j = cl[*i].begin(); j != cl[*i].end(); j++) { if (mark[*j] == false) { dfs(*j, *j, *i); } } } scanf("%d", &q); while (q--) { ans = 0; scanf("%d %d", &a, &b); if (mm[a].size() > mm[b].size()) swap(a, b); if (out.count(make_pair(a, b)) == 0) { for (pair<int, int> u : mm[a]) { if (mm[b].count(u.first) > 0 and u.second == mm[b][u.first]) ans++; } out[make_pair(a, b)] = ans; printf("%d\n", ans); } else printf("%d\n", out[make_pair(a, b)]); } }
10
CPP
from sys import stdin,stdout from collections import defaultdict as df import bisect t=int(input()) for ii in range(t): s=input().rstrip() s=[ i for i in s] d=df(list) count=1 for i in range(1,len(s)): if s[i]==s[i-1]: count+=1 else: d[s[i-1]].append(count%2) count=1 d[s[-1]].append(count%2) corr=[] for i in set(s): y=d[i] if 1 in y: corr.append(i) corr=set(corr) corr=list(corr) corr.sort() print(''.join(corr))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); int n, m; cin >> n >> m; if (n == m) { cout << n << endl; return 0; } vector<pair<int, int> > vec; vec.push_back(pair<int, int>(floor(m / 2) - ceil(m / 2), 2)); vec.push_back(pair<int, int>(floor(m / 3) - ceil(m / 2), 3)); vec.push_back(pair<int, int>(floor(m / 5) - ceil(m / 2), 5)); vec.push_back(pair<int, int>(floor(m / 7) - ceil(m / 2), 7)); sort(vec.begin(), vec.end(), greater<pair<int, int> >()); cout << vec[0].second << endl; }
7
CPP
n = int(input()) s = str(input()) ans = {} for i in range(n-1): if s[i:i+2] not in ans: ans[s[i:i+2]] = 1 else: ans[s[i:i+2]] += 1 maxx = 0 maxx_ans = '' for k in ans.keys(): if maxx < ans[k]: maxx = ans[k] maxx_ans = k print(maxx_ans)
8
PYTHON3
n = int (input()) a = [int (i) for i in input().split()] amax = max(a) ans = 0 for elem in a: ans += amax - elem print (ans)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define eb emplace_back #define all(v) (v).begin(),(v).end() #define fi first #define se second using vint=vector<int>; using pint=pair<int,int>; using vpint=vector<pint>; template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;} template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;} template<class A,class B> ostream& operator<<(ostream& ost,const pair<A,B>&p){ ost<<"{"<<p.first<<","<<p.second<<"}"; return ost; } template<class T> ostream& operator<<(ostream& ost,const vector<T>&v){ ost<<"{"; for(int i=0;i<v.size();i++){ if(i)ost<<","; ost<<v[i]; } ost<<"}"; return ost; } inline int topbit(unsigned long long x){ return x?63-__builtin_clzll(x):-1; } inline int popcount(unsigned long long x){ return __builtin_popcountll(x); } inline int parity(unsigned long long x){ return __builtin_parity(x); } struct UnionFindTree{ vector<int32_t>par,sz; vector<vint>lis; UnionFindTree(int32_t n=0):par(n),sz(n),lis(n){ for(int32_t i=0;i<n;i++){ par[i]=i; sz[i]=1; lis[i].pb(i); } } int32_t find(int32_t x){ return x==par[x]?x:par[x]=find(par[x]); } void unite(int32_t x,int32_t y){ x=find(x);y=find(y); if(x==y)return; if(sz[x]<sz[y])swap(x,y); sz[x]+=sz[y]; for(auto w:lis[y])lis[x].pb(w); par[y]=x; } bool same(int32_t x,int32_t y){ return find(x)==find(y); } int32_t size(int32_t x){ return sz[find(x)]; } }; int deg[222222]; bool isC[222222]; signed main(){ int N,M; cin>>N>>M; vint A(M),B(M),C(M); rep(i,M)cin>>A[i]>>B[i]>>C[i],A[i]--,B[i]--; UnionFindTree uf(N); rep(i,M){ uf.unite(A[i],B[i]); deg[A[i]]++;deg[B[i]]++; } int X=0;//cycle(size>=2) rep(i,N){ if(uf.find(i)!=i)continue; if(uf.lis[i].size()==1)continue; isC[i]=true; for(auto w:uf.lis[i])if(deg[w]!=2)isC[i]=false; if(isC[i])X++; } vint lis,lis2; bool ex=false; int sum=0; int num=0; rep(i,M){ if(A[i]==B[i]){ if(C[i]==1)continue; if(uf.lis[uf.find(A[i])].size()==1&&deg[A[i]]==2)lis2.pb(C[i]); else lis.pb(C[i]); } else{ sum+=C[i]; num++; if(!isC[uf.find(A[i])]){ if(C[i]>1)ex=true; } } } if(X==0){ cout<<num<<endl; return 0; } X+=num; sort(all(lis)); sort(all(lis2)); if(!ex&&lis.size()==0){ cout<<-1<<endl; return 0; } if(!ex){ sum+=lis.back(); X++; lis.pop_back(); } while(true){ if(sum>=X){ cout<<X<<endl; return 0; } if(lis.size()==0&&lis2.size()==0){ cout<<-1<<endl; return 0; } if(lis.size()&&sum+lis.back()>=X+1){ cout<<X+1<<endl; return 0; } if(lis2.size()==0||(lis.size()>=2&&lis.back()+lis[lis.size()-2]>=lis2.back())){ sum+=lis.back(); X++; lis.pop_back(); continue; } sum+=lis2.back(); X+=2; lis2.pop_back(); } return 0; }
0
CPP
def upperCase(word): return len(list(i for i in word if i.isupper())) def lowerCase(word): return len(list(i for i in word if i.islower())) def transfer(word): if upperCase(word) > lowerCase(word): return word.upper() return word.lower() print(transfer(input()))
7
PYTHON3