solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
string1=input() print('+'.join(sorted(string1.split('+'))))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; void cmin(long long& x, long long y) { if (x > y) { x = y; } } int n, a[N]; long long f[N][2]; bool best[N][2]; vector<int> adj[N], answer; void dfs1(int u, int p) { long long min_diff = 0; bool go = false; for (auto v : adj[u]) { if (v != p) { go = true; dfs1(v, u); f[u][0] += f[v][0]; f[u][1] += f[v][0]; cmin(min_diff, f[v][1] - f[v][0]); } } f[u][1] += min_diff; if (!go) { f[u][0] = a[u]; } else { cmin(f[u][0], f[u][1] + a[u]); } } void dfs2(int u, int p) { if (f[u][0] == f[u][1] + a[u]) { best[u][1] |= best[u][0]; if (best[u][0]) { answer.push_back(u); } } long long sumf = 0; for (auto v : adj[u]) { if (v != p) { sumf += f[v][0]; } } vector<int> diff_nodes; for (auto v : adj[u]) { if (v != p) { if (sumf == f[u][0]) { best[v][0] |= best[u][0]; } if (sumf + f[v][1] - f[v][0] == f[u][1] && best[u][1]) { best[v][1] = true; diff_nodes.push_back(v); } else if (sumf + f[v][1] - f[v][0] + a[u] == f[u][0] && best[u][0]) { diff_nodes.push_back(v); } } } for (auto v : adj[u]) { if (v != p) { if (diff_nodes.size() > 1) { best[v][0] |= true; } else if (diff_nodes.size() == 1 && v != diff_nodes.back()) { best[v][0] |= true; } dfs2(v, u); } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } dfs1(1, 0); best[1][0] = true; dfs2(1, 0); printf("%lld %d\n", f[1][0], answer.size()); sort(answer.begin(), answer.end()); for (auto v : answer) { printf("%d ", v); } return puts(""), 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int mod, d; inline int read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } int ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; } inline int mul(int x, int y) { return 1LL * x * y % mod; } inline int mul(int x, int y, int z) { return mul(mul(x, y), z); } int qpow(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = mul(ans, x); x = mul(x, x); y >>= 1; } return ans; } int Inv(int x) { return qpow(x, mod - 2); } int fac[20], inv[20], pinv[20]; int C(int x, int y) { return x >= y ? mul(fac[x], pinv[x - y], pinv[y]) : 0; } void add(int a, int b, int to) { printf("+ %d %d %d\n", a, b, to); } void D(int a, int to) { printf("^ %d %d\n", a, to); } void init() { for (int i = 1; i <= 32; i++) add(4900 + i - 1, 4900 + i - 1, 4900 + i); for (int i = 0; i <= 30; i++) { if ((mod - 1) >> i & 1) add(5000, 4900 + i, 5000); } fac[0] = 1; for (int i = 1; i < 20; i++) fac[i] = mul(fac[i - 1], i); inv[0] = inv[1] = 1; for (int i = 2; i < 20; i++) inv[i] = mul(mod - mod / i, inv[mod % i]); pinv[0] = 1; for (int i = 1; i < 20; i++) pinv[i] = mul(pinv[i - 1], inv[i]); } int a[15][15], ans[15]; void init2() { for (int i = 1; i <= d + 1; i++) { for (int j = 1; j <= d + 1; j++) a[i][j] = mul(C(d, i - 1), qpow(j, i - 1)); a[i][d + 2] = i == d - 1; } for (int i = 1; i <= d + 1; i++) { int pl = 0; for (int j = i; j <= d + 1; j++) { if (a[j][i]) { pl = j; break; } } for (int j = i; j <= d + 2; j++) swap(a[i][j], a[pl][j]); int inv = Inv(a[i][i]); for (int j = i; j <= d + 2; j++) a[i][j] = mul(a[i][j], inv); for (int j = 1; j <= d + 1; j++) { if (j == pl) continue; int D = a[j][i]; for (int k = i; k <= d + 2; k++) a[j][k] = sub(a[j][k], mul(D, a[i][k])); } } for (int i = 1; i <= d + 1; i++) ans[i] = a[i][d + 2]; } void getval(int pl, int v) { add(5000, 5000, pl); for (int i = 0; i <= 30; i++) { if (v >> i & 1) add(pl, 4900 + i, pl); } } void mulval(int pl, int v, int to) { add(5000, pl, 4800); for (int i = 1; i <= 30; i++) add(4800 + i - 1, 4800 + i - 1, 4800 + i); add(5000, 5000, to); for (int i = 0; i <= 30; i++) { if (v >> i & 1) add(to, 4800 + i, to); } } void divval(int pl, int v, int to) { mulval(pl, Inv(v), to); } void addval(int pl, int v, int to) { getval(4799, v); add(pl, 4799, to); } void subval(int pl, int v, int to) { addval(pl, mod - v, to); } void getsqr(int pl, int to) { for (int i = 1; i <= d + 1; i++) addval(pl, i, 4100 + i); for (int i = 1; i <= d + 1; i++) D(4100 + i, 4000 + i); for (int i = 1; i <= d + 1; i++) mulval(4000 + i, ans[i], 3000 + i); add(5000, 5000, to); for (int i = 1; i <= d + 1; i++) add(3000 + i, to, to); } signed main() { cin >> d >> mod; init(); init2(); getsqr(1, 3); mulval(3, mod - 1, 4); getsqr(2, 5); mulval(5, mod - 1, 6); add(1, 2, 7); getsqr(7, 8); add(8, 4, 8), add(8, 6, 8); mulval(8, Inv(2), 9); cout << "f 9"; return 0; }
14
CPP
#include <bits/stdc++.h> using namespace std; const int maxN = 2010; long long n, m, a[maxN], b[maxN]; long long sumA, sumB, cur, dif, ans; map<long long, pair<int, int> > B; vector<pair<long long, pair<int, int> > > A; map<long long, int> single; pair<int, int> bestA, bestB; int s; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; sumA += a[i]; } cin >> m; for (int i = 0; i < m; i++) { cin >> b[i]; single[b[i]] = i; sumB += b[i]; } ans = abs(sumA - sumB); for (int i = 0; i < n; i++) { cur = (sumB - sumA) / 2LL; cur += a[i]; auto pos = single.upper_bound(cur); for (int k = 0; k <= 2; k++, pos--) { if (pos == single.end()) continue; dif = abs(sumB - sumA + 2 * (a[i] - pos->first)); if (dif < ans) { s = 1; ans = dif; bestA.first = i; bestB.first = pos->second; } if (pos == single.end()) break; } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { cur = a[i] + a[j]; A.push_back({cur, {i, j}}); } } for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { cur = b[i] + b[j]; B[cur] = {i, j}; } } if (min(n, m) >= 2) { for (unsigned int i = 0; i < A.size(); i++) { cur = (sumB - sumA) / 2LL; cur += A[i].first; auto j = B.upper_bound(cur); for (int k = 0; k <= 2; k++, j--) { if (j == B.end()) continue; dif = abs(sumB - sumA + 2 * (A[i].first - j->first)); if (dif < ans) { s = 2; ans = dif; bestA = A[i].second; bestB = j->second; } if (j == B.begin()) break; } } } cout << ans << '\n' << s << '\n'; if (s == 2) { cout << ++bestA.first << ' ' << ++bestB.first << '\n'; cout << ++bestA.second << ' ' << ++bestB.second << '\n'; } else if (s == 1) { cout << ++bestA.first << ' ' << ++bestB.first << '\n'; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); double a, b, c; cin >> a >> b >> c; cout << fixed << setprecision(7); if (a > 0) { cout << (-1 * b + sqrt(b * b - 4 * a * c)) / (2 * a) << endl; cout << (-1 * b - sqrt(b * b - 4 * a * c)) / (2 * a) << endl; } else { cout << (-1 * b - sqrt(b * b - 4 * a * c)) / (2 * a) << endl; cout << (-1 * b + sqrt(b * b - 4 * a * c)) / (2 * a) << endl; } }
21
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 2147483647; int main() { long long j, i, n, ki, ai, ans = 0; cin >> n; for (i = 1; i <= n; ++i) { cin >> ki >> ai; for (j = 1; j * j < ai; j *= 2) ki++; if (ai == 1) ki++; ans = max(ans, ki); } cout << ans << endl; }
9
CPP
N = int(input()) arr = list(map(int,input().split())) cnt_arr = [0]*N ans = 0 for i in range(N): if arr[i]+i+1<N: cnt_arr[arr[i]+i+1] += 1 if i+1-arr[i]>0: ans += cnt_arr[i+1-arr[i]] print(ans)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; int cmp(double x, double y = 0, double tol = EPS) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } const int MAXN = 205; int priority[MAXN]; char str[MAXN]; int permutation[MAXN]; long long ways[MAXN][MAXN]; const long long INF = 1000000000000000001LL; bool prioritary(int i, int j) { return priority[i] < priority[j]; } void add(long long &where, const long long what) { where += what; if (where > INF) where = INF; } long long count(int len) { memset(ways, 0, sizeof ways); ways[0][0] = 1; for (int i = 0; i < len; ++i) { const char next = str[i]; for (int open = 0; open <= len; ++open) { if (ways[i][open] == 0) continue; if (next != ')') add(ways[i + 1][open + 1], ways[i][open]); if (next != '(' and open > 0) add(ways[i + 1][open - 1], ways[i][open]); } } if (ways[len][0] < 0) while (1) ; return ways[len][0]; } int main() { int n, m; long long k; cin >> n >> m >> k; assert(k < INF); assert((n + m) % 2 == 1); for (int i = 0; i < n + m - 1; ++i) { priority[i] = n * m + 1; permutation[i] = i; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int x; cin >> x; priority[i + j] = min(priority[i + j], x); } } sort(permutation, permutation + n + m - 1, prioritary); for (int i = 0; i < n + m - 1; ++i) str[i] = '*'; str[n + m - 1] = '\0'; for (int i = 0; i < n + m - 1; ++i) { int pos = permutation[i]; str[pos] = '('; long long add = count(n + m - 1); if (k > add) { str[pos] = ')'; k -= add; } } assert(k == 1); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { printf("%c", str[i + j]); } printf("\n"); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long p(long long N, long long P) { if (!P) return 1; if (P & 1) return (N * p(N, P - 1)) % 1000000007; long long r = p(N, P / 2); return (r * r) % 1000000007; } char x[110]; int main() { char c = 0; long long n = 0; while ((c = getchar()) != '\n') { n++; x[n] = c; } long long size = p(2, n), acc = 1, count = 0, two = p(2, 1000000007 - 2); for (int i = 1; i <= n; i++) { if (x[i] == '1') { count = (count + (((acc * ((size * two) % 1000000007)) % 1000000007) * ((size * two) % 1000000007)) % 1000000007) % 1000000007; } acc = (acc * 2) % 1000000007; size = (size * two) % 1000000007; } cout << count << endl; return 0; }
9
CPP
""" Author: Sagar Pandey """ # ---------------------------------------------------Import Libraries--------------------------------------------------- import sys import time import os from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi, inf, factorial from copy import copy, deepcopy from sys import exit, stdin, stdout from collections import Counter, defaultdict, deque from itertools import permutations import heapq from bisect import bisect_left as bl # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is r # ---------------------------------------------------Global Variables--------------------------------------------------- # sys.setrecursionlimit(100000000) mod = 1000000007 # ---------------------------------------------------Helper Functions--------------------------------------------------- iinp = lambda: int(sys.stdin.readline()) inp = lambda: sys.stdin.readline().strip() strl = lambda: list(inp().strip().split(" ")) intl = lambda: list(map(int, inp().split(" "))) mint = lambda: map(int, inp().split()) flol = lambda: list(map(float, inp().split(" "))) flush = lambda: stdout.flush() def permute(nums): def fun(arr, nums, cur, v): if len(cur) == len(nums): arr.append(cur.copy()) i = 0 while i < len(nums): if v[i]: i += 1 continue else: cur.append(nums[i]) v[i] = 1 fun(arr, nums, cur, v) cur.pop() v[i] = 0 i += 1 # while i<len(nums) and nums[i]==nums[i-1]:i+=1 # Uncomment for unique permutations return arr res = [] nums.sort() v = [0] * len(nums) return fun(res, nums, [], v) def subsets(res, index, arr, cur): res.append(cur.copy()) for i in range(index, len(arr)): cur.append(arr[i]) subsets(res, i + 1, arr, cur) cur.pop() return res def sieve(N): root = int(sqrt(N)) primes = [1] * (N + 1) primes[0], primes[1] = 0, 0 for i in range(2, root + 1): if primes[i]: for j in range(i * i, N + 1, i): primes[j] = 0 return primes def bs(arr, l, r, x): if x < arr[0] or x > arr[len(arr) - 1]: return -1 while l <= r: mid = l + (r - l) // 2 if arr[mid] == x: return mid elif arr[mid] < x: l = mid + 1 else: r = mid - 1 return -1 def isPrime(n): if n <= 1: return False if n <= 3: return True if n % 2 == 0 or n % 3 == 0: return False p = int(sqrt(n)) for i in range(5, p + 1, 6): if n % i == 0 or n % (i + 2) == 0: return False return True # -------------------------------------------------------Functions------------------------------------------------------ def solve(): d,k=mint() p=0 q=0 chance=1 while 1: if (p+k)*(p+k)+q*q<=d*d and (q+k)*(q+k)+p*p<=d*d: if p<=q: p+=k else: q+=k chance^=1 elif (p+k)*(p+k)+q*q<=d*d: p+=k chance^=1 elif (q+k)*(q+k)+p*p<=d*d: q+=k chance^=1 else: if chance==1: print("Utkarsh") return else: print("Ashish") return # -------------------------------------------------------Main Code------------------------------------------------------ start_time = time.time() for _ in range(iinp()): solve() # print("--- %s seconds ---" % (time.time() - start_time))
10
PYTHON3
t=int(input()) for _ in range(t): x1,y1,x2,y2=map(int,input().split()) ans=(x2-x1)*(y2-y1)+1 print(ans)
9
PYTHON3
#include <iostream> using namespace std; int main() { int n , ans = 0; cin >> n; for(int i = 0 ; i < n ; i++) { int temp; cin >> temp; ans^=temp; } cout << ((!ans)? "Yes":"No"); return 0; }
0
CPP
def vectorsum(a,b): return [a[i]+b[i] for i in range(3)] n = int(input()) V = [0,0,0] for i in range(n): V = vectorsum(V,[int(x) for x in input().split()]) print(['NO','YES'][V==[0,0,0]])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, l, m, n; int T, M, N, K, t1, t2, t3, t4, t5; long long ans, out; cin >> N >> T; int cnt[3] = {0}; for (i = 0; i < N; ++i) { scanf("%d", &t1); cnt[t1 + 1]++; } for (i = 0; i < T; ++i) { scanf("%d%d", &t1, &t2); if ((t2 - t1 + 1) % 2) printf("0\n"); else { if (cnt[0] >= ((t2 - t1 + 2) / 2) && cnt[2] >= ((t2 - t1 + 2) / 2)) printf("1\n"); else printf("0\n"); } } return 0; }
7
CPP
def a(n): return 2**(n+2)-3 def b(n): return 2**(n+1)-1 N,X= [int(s) for s in input().split()] count=0 while X>0: if X==a(N): count+=b(N) X-=a(N) elif X<a(N): X-=1 N-=1 else: X-=a(N)+1 count+=b(N)+1 print(count)
0
PYTHON3
a=input() print('YNeos'[sum(map(a.count,a))!=8::2])
0
PYTHON3
args = input().split(' ') n = int(args[0]) k = int(args[1]) i = 0 while(i < k and n > 0): if n % 10 != 0: n-= 1 else: n //= 10 i += 1 print(n)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, K; int arr[5][59]; queue<pair<int, int>> q; vector<pair<int, pair<int, int>>> vs; void init() { cin >> N >> K; for (int i = 1; i <= 4; ++i) for (int j = 1; j <= N; ++j) cin >> arr[i][j]; for (int j = 1; j <= N; ++j) if (arr[2][j]) q.push({2, j}); for (int j = N; j >= 1; --j) if (arr[3][j]) q.push({3, j}); } void rotate() { for (int i = 0; i < q.size(); ++i) { int y = q.front().first; int x = q.front().second; if (y == 2) x == 1 ? ++y : --x; else x == N ? --y : ++x; if (arr[y][x] == 0) break; q.push(q.front()); q.pop(); } for (int i = 0; i < q.size(); ++i) { int y = q.front().first; int x = q.front().second; q.pop(); int ny = y, nx = x; if (y == 2) x == 1 ? ++ny : --nx; else x == N ? --ny : ++nx; q.push({ny, nx}); vs.push_back({arr[y][x], {ny, nx}}); arr[ny][nx] = arr[y][x]; arr[y][x] = 0; } } bool match() { bool ret = false; int size = q.size(); for (int i = 0; i < size; ++i) { int y = q.front().first; int x = q.front().second; if (y == 2) { if (arr[y][x] == arr[1][x]) { vs.push_back({arr[y][x], {1, x}}); ret = true; arr[y][x] = 0; } else q.push(q.front()); } else { if (arr[y][x] == arr[4][x]) { vs.push_back({arr[y][x], {4, x}}); ret = true; arr[y][x] = 0; } else q.push(q.front()); } q.pop(); } return ret; } void solve() { if (!match() && K == 2 * N) { puts("-1"); return; } while (!q.empty()) { rotate(); match(); } printf("%d\n", vs.size()); for (int i = 0; i < vs.size(); ++i) printf("%d %d %d\n", vs[i].first, vs[i].second.first, vs[i].second.second); } int main(int argc, char** argv) { int t = 1; for (int i = 0; i < t; ++i) { init(); solve(); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long mod = 998244353; map<int, int> mp, used; int data[N]; int n; inline long long qmul(long long a, long long b) { long long res = 0; while (b) { if (b & 1) res = (res + a) % mod; a = (a + a) % mod; b >>= 1; } return res; } inline long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = qmul(res, a); a = qmul(a, a); b >>= 1; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &data[i]); mp[data[i]] = i; } int ans = -1, i, j, k; for (i = 1; i <= n;) { if (used[data[i]] == 0) { used[data[i]] = 1; ans++; k = mp[data[i]]; for (j = i + 1; j < k; j++) { if (!used[data[j]]) { used[data[j]] = 1; k = max(k, mp[data[j]]); } } i = k + 1; continue; } i++; } printf("%I64d\n", qpow(2, ans)); return 0; }
11
CPP
import sys sys.setrecursionlimit(10000) # default is 1000 in python n = int(input()) k = 3*n + 4 print(k) print("1 1") print("1 2") for i in range(n): for y in range(3): print(str(i+2) + " " + str(i+y+1)) print(str(n+2)+" "+str(n+1)) print(str(n+2)+" "+str(n+2)) # try: # raise Exception # except: # print("-1") # thenos.sort(key=lambda x: x[2], reverse=True) # int(math.log(max(numbers)+1,2)) # 2**3 (power) # a,t = (list(x) for x in zip(*sorted(zip(a, t)))) # to copy lists use .copy() # pow(p, si, 1000000007) for modular exponentiation # my_dict.pop('key', None) # This will return my_dict[key] if key exists in the dictionary, and None otherwise. # bin(int('010101', 2))
9
PYTHON3
username = input() count = 0 for x in range(0, len(username) - 1, 1): for y in range((x + 1), len(username), 1): if username[x] == username[y]: count = count + 1 break result = len(username) - count if result % 2 == 0: print("CHAT WITH HER!") else: print("IGNORE HIM!")
7
PYTHON3
n,k = map(int,input().split()) print(k if n==1 else k*(k-1)**(n-1))
0
PYTHON3
#!/usr/bin/python3 s = input() print(s[0].upper() + s[1:])
7
PYTHON3
n = int(input()) p = [int(x) for x in input().split()] q = [0 for i in range(n)] for i in range(n): q[p[i] - 1] = str(i + 1) print(' '.join(q))
7
PYTHON3
for _ in range (int(input())): s = input() ans="" for i in range(0,len(s),2): ans+=s[i] ans+=s[len(s)-1] print(ans)
7
PYTHON3
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) b = 0 c = 0 for x in a: if x > 1: b += 1 if x == 1: c += 1 if b == n: print('First') elif c == n: if n % 2 == 0: print('Second') else: print('First') else: for j in range(n): if a[j] != 1: y = j break if y % 2 == 0: print('First') else: print('Second')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int N, K, X, Y; long long d[55][55][2], f[55][55][2]; bool g[55][55][2]; long long c[55][55]; int main(void) { cin >> N >> K; K /= 50; for (int i = (0), _b = (N); i <= _b; i++) c[i][0] = c[i][i] = 1; for (int i = (1), _b = (N); i <= _b; i++) for (int j = (1), _b = (i - 1); j <= _b; j++) c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; int w; for (int i = (1), _b = (N); i <= _b; i++) { cin >> w; if (w == 50) ++X; else ++Y; } d[X][Y][0] = 0, f[X][Y][0] = 1; g[X][Y][0] = 1; queue<pair<pair<long long, long long>, bool> > q; q.push(pair<pair<long long, long long>, bool>( pair<long long, long long>(X, Y), 0)); while (!q.empty()) { int x = q.front().first.first, y = q.front().first.second, t = q.front().second; q.pop(); for (int i = (0), _b = (x); i <= _b; i++) for (int j = (0), _b = (y); j <= _b; j++) if (i + j && i + 2 * j <= K) { int nx = X - x + i, ny = Y - y + j; if (g[nx][ny][!t]) { if (d[nx][ny][!t] == d[x][y][t] + 1) f[nx][ny][!t] = (f[nx][ny][!t] + f[x][y][t] * c[x][i] % mod * c[y][j] % mod) % mod; } else { g[nx][ny][!t] = 1; d[nx][ny][!t] = d[x][y][t] + 1; f[nx][ny][!t] = f[x][y][t] * c[x][i] % mod * c[y][j] % mod; q.push(pair<pair<long long, long long>, bool>( pair<long long, long long>(nx, ny), !t)); } } } if (!g[X][Y][1]) return cout << -1 << endl << 0 << endl, 0; cout << d[X][Y][1] << endl << f[X][Y][1] << endl; return 0; }
11
CPP
import sys input = sys.stdin.readline class Unionfind: def __init__(self,n): self.uf = [-1]*n def find(self,x): if self.uf[x] < 0: return x else: self.uf[x] = self.find(self.uf[x]) return self.uf[x] def same(self,x,y): return self.find(x) == self.find(y) def union(self,x,y): x = self.find(x) y = self.find(y) if x == y: return False if self.uf[x] > self.uf[y]: x,y = y,x self.uf[x] += self.uf[y] self.uf[y] = x return True def size(self,x): x = self.find(x) return -self.uf[x] n,q = map(int,input().split()) U = Unionfind(n) for i in range(q): t,u,v = map(int,input().split()) if t == 0: U.union(u,v) else: print(int(U.same(u,v)))
0
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int N = 4e6 + 10; const unsigned long long p = 131; string s; map<string, int> mp; void solve() { mp.clear(); int n; cin >> n; set<int> l, r; bool f1 = false; bool f2 = false; bool f3 = false; for (int i = 1; i <= n; i++) { cin >> s; if (s[0] == '0' && s.back() == '1') { f3 = true; mp[s] = i; reverse(s.begin(), s.end()); if (mp.count(s)) { r.erase(mp[s]); } else l.insert(i); } else if (s[0] == '1' && s.back() == '0') { f3 = true; mp[s] = i; reverse(s.begin(), s.end()); if (mp.count(s)) { l.erase(mp[s]); } else r.insert(i); } else if (s[0] == '0' && s.back() == '0') f1 = true; else f2 = true; } if (f1 && f2 && !f3) { cout << -1 << endl; return; } int a = l.size(); int b = r.size(); vector<int> res; if (b > a) { int cnt = b - a; cnt = cnt / 2; for (auto it = r.begin(); cnt--; it++) { res.push_back(*it); } } if (a > b) { int cnt = a - b; cnt = cnt / 2; for (auto it = l.begin(); cnt--; it++) { res.push_back(*it); } } cout << res.size() << endl; for (auto x : res) cout << x << " "; cout << endl; } int main() { ios ::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::pair<T, U> operator+(const std::pair<T, U>& l, const std::pair<T, U>& r) { return {l.first + r.first, l.second + r.second}; } typedef void (*callback_function)(void); const long long ZERO = 0LL; const long long INF64 = 1e18; const long long INF32 = 1e9; const long long MOD = 1e9 + 7; const long double PI = acos(-1.0L); const long double EPS = static_cast<long double>(1e-9); long long inv(long long a, long long b) { if (a != 1) { return b - (inv(b % a, a) * b) / a; } return 1; } inline long long Pow(long long a, long long k) { long long s = 1; for (; k; k >>= 1) { k& 1 ? s = 1LL * s * a % MOD : 0; a = 1LL * a * a % MOD; } return s; } const long long N = 1e6 + 7; void input() {} void output() {} void preprocess() {} void debug() { if (true) { } } void solve() { preprocess(); long long n, d; cin >> n >> d; long long a[n]; for (long long i = (0); i < (n); i++) { cin >> a[i]; } long long cnt = 0; for (long long i = (1); i < (n); i++) { if (a[i - 1] >= a[i]) { long long add = (a[i - 1] + d - a[i]) / d; a[i] += add * d; cnt += add; } } cout << cnt << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(); { input(); solve(); output(); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int pr[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; int main() { int cnt = 0; for (int i = 0; i < (int)(15); ++i) { cout << pr[i] << endl; string s; cin >> s; if (s[0] == 'y') { ++cnt; if (pr[i] * pr[i] <= 100) { cout << pr[i] * pr[i] << endl; cin >> s; if (s[0] == 'y') { ++cnt; break; } } } } if (cnt > 1) { cout << "composite" << endl; } else { cout << "prime" << endl; } return 0; }
7
CPP
x = input() y = input() xx = x[::-1] if xx == y: print('YES') else : print('NO')
7
PYTHON3
sok = input().split() x = int(sok[0]);y = int(sok[1]) d1 = min(x,y) d2 = (max(x,y))- d1 if d2%2==0: print (d1,"",int(d2/2)) else: print (d1,"",int(d2/2-0.5))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int funcion(long long x) { int ans = 0; while (x != 1) { ans++; x /= 2; } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { unsigned long long n, m; cin >> n >> m; vector<long long> v(m); for (int i = (0); i < (int)(m); i++) cin >> v[i]; vector<int> cont(100, 0); unsigned long long suma = 0; for (int i = (0); i < (int)(m); i++) suma += v[i]; if (suma < n) { cout << "-1\n"; continue; } for (int i = (0); i < (int)(m); i++) { int aux = funcion(v[i]); cont[aux]++; } long long pot = 0, aux = 1; int ans = 0; while (n) { if (n % 2 == 1 && cont[pot] > 0) { cont[pot]--; pot++; aux *= 2; n /= 2; continue; } if (n % 2 == 1) { for (int i = (0); i < (int)(pot); i++) { int aux = cont[i] / 2; cont[i] %= 2; cont[i + 1] += aux; } if (cont[pot] > 0) { cont[pot]--; pot++; aux *= 2; n /= 2; continue; } else { int pos = pot; while (cont[pos] == 0) pos++; for (int i = pos; i > pot; i--) { cont[i]--; cont[i - 1] += 2; } ans += pos - pot; cont[pot]--; pot++; aux *= 2; n /= 2; continue; } } n /= 2; pot++; aux *= 2; } cout << ans << "\n"; } return 0; }
10
CPP
import re from sys import stderr from typing import Union, Generator, List, Tuple import string INF = 2e18 + 3 EPS = 1e-10 def main(): s = input() print(s + s[::-1]) if __name__ == '__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; vector<vector<long long> > a(n, vector<long long>(2)); for (int i = 0; i < n; i++) cin >> a[i][1] >> a[i][0]; sort(a.begin(), a.end()); multiset<long long> s; long long sum = 0, ans = 0; for (int i = n - 1; i >= 0; i--) { s.insert(a[i][1]); sum += a[i][1]; while (s.size() > k) { auto it = s.begin(); sum -= *it; s.erase(it); } ans = max(ans, sum * a[i][0]); } cout << ans << endl; } void sj26() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { bool q = 0; sj26(); int t = 1; if (q) cin >> t; while (t--) { solve(); } }
9
CPP
#include <iostream> #include <queue> #define REP(i,n) for(int i=0; i<(int)(n); i++) using namespace std; int dx[] = {-1, 0, 1}; int dy[] = {0, -1, 0}; int main(){ int n; long long k; while(cin >> n >> k, n + k){ if(n % 2 == 1 || (1ll << (n / 2)) < k){ cout << "No" << endl << endl; }else{ vector<string> g(n, string(n, ' ')); k--; REP(i, n / 2){ int j = n / 2 - i - 1; char c = ((1ll << j) & k) == 0 ? '.' : 'E'; char v = ((1ll << j) & k) == 0 ? 'E' : '.'; g[0][2 * i ] = c; g[0][2 * i + 1] = c; g[(n - 1) - (2 * i )][n - 1] = c; g[(n - 1) - (2 * i + 1)][n - 1] = c; g[2 * i ][0] = c; g[2 * i + 1][0] = c; int left = 0; int right = 0; if(j != 0 && (((1ll << (j - 1)) & k) == 0) == (((1ll << j) & k) == 0)) right = 1; if(j != (n / 2) - 1 && (((1ll << (j + 1)) & k) == 0) == (((1ll << j) & k) == 0)) left = 1; if(left + right == 0){ g[1][2 * i ] = c; g[1][2 * i + 1] = c; }else if(left + right == 2){ g[1][2 * i ] = v; g[1][2 * i + 1] = v; }else{ if(left == 1){ g[1][2 * i ] = v; g[1][2 * i + 1] = c; }else{ g[1][2 * i ] = c; g[1][2 * i + 1] = v; } } } for(int y = 1; y < n - 1; y++){ for(int x = 1; x < n - 1; x++){ int cnt[128]; cnt['.'] = cnt['E'] = 0; REP(i, 3) cnt[(int)g[y + dy[i]][x + dx[i]]]++; if(cnt['.'] == 1) g[y+1][x] = '.'; if(cnt['E'] == 1) g[y+1][x] = 'E'; } } REP(i,n) cout << g[i] << endl; cout << endl; } } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t, c, m, x; scanf("%d", &t); while (t--) { scanf("%d%d%d", &c, &m, &x); int mi = min(c, m); int ma = max(c, m); int sum = c + m + x; if (ma + x >= mi * 2) printf("%d\n", mi); else printf("%d\n", sum / 3); } return 0; }
9
CPP
test = int(input()) for _ in range(test): n, x = map(int, input().split()) arr = list(map(int, input().split())) odd, even = 0, 0 for i in range(n): if arr[i]%2: odd+=1 else: even+=1 flag = False if odd==0: print('No') else: if x%2: oddy = 0 evy = x-1 while(oddy<=x-1): if oddy<=odd-1 and evy<=even: flag = True break oddy+=2 evy -=2 if flag: print('Yes') else: print('No') else: evy = 1 oddy = x-2 while(evy<=x-1): if oddy<=odd-1 and evy<=even: flag = True break oddy-=2 evy +=2 if flag: print('Yes') else: print('No')
7
PYTHON3
n=int(input()) x=[] y=[] z=[] for i in range(n): vector=input().split() x.append(int(vector[0])) y.append(int(vector[1])) z.append(int(vector[2])) if sum(x)==0 and sum(y)==0 and sum(z)==0: print('YES') else: print('NO')
7
PYTHON3
import sys def solve(): inp=sys.stdin.readline n,_,k=map(int,inp().split()) st=[0]*(n+5) for i in map(int,inp().split()): st[i]=1 pos = 1 if st[1]: print(1) exit() for i in range(k): a,b=map(int,inp().split()) if a==pos: pos=b elif b==pos: pos=a if st[pos]: print(pos) exit() print(pos) if __name__ == '__main__': solve()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, l, a[100005]; int main() { cin >> n >> m >> l; long long ans = 0; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n + 1; i++) if (a[i - 1] > l && a[i] <= l) ans++; long long t, p, d; while (m--) { cin >> t; if (t == 0) cout << ans << endl; else { cin >> p >> d; if (a[p] <= l && a[p] + d > l) { if (a[p - 1] <= l && a[p + 1] <= l) ans++; else if (a[p - 1] > l && a[p + 1] > l) ans--; } a[p] += d; } } return 0; }
8
CPP
import sys n = int(input()) a = [*map(int, input().split())] def solve(c, r, s, x): sys.setrecursionlimit(sys.getrecursionlimit() + 1) r.remove(x) s.remove(x) c.append(x) if len(r) == 0: print(*c) exit() if x % 3 == 0 and (x // 3) in s: solve([*c], [*r], set([*s]), x // 3) if x * 2 in s: solve([*c], [*r], set([*s]), x * 2) return for i in a: solve([], [*a], set(a), i)
10
PYTHON3
from collections import deque K = int(input()) deq = deque([(1, 1)]) history = set() while True: to = deq.popleft() if to[0] == 0: print(to[1]) break elif to[0] not in history: history.add(to[0]) deq.appendleft((to[0] * 10 % K, to[1])) deq.append((to[0] + 1, to[1] + 1))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n; int main() { cin >> n; n -= 2; cout << n * n; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { double n; while (cin >> n){ double sum = 0; sum += n; for (int i = 2; i <= 10; i++){ if (i % 2 == 0){ n *= 2; } else { n /= 3; } sum += n; } printf("%.8f\n", sum); } return (0); }
0
CPP
#include <bits/stdc++.h> using namespace std; const int INF=1000000001; int d[305][305],a[305][305]; int main(){ int n,m,l; scanf("%d %d %d",&n,&m,&l); for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j){ d[i][j]=INF; a[i][j]=INF; } for(int i=0;i<m;i++){ int x,y,z; scanf("%d %d %d",&x,&y,&z); d[x][y]=z,d[y][x]=z; } for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)d[i][j]=min(d[i][j],d[i][k]+d[k][j]); for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(i!=j&&d[i][j]<=l)a[i][j]=1; for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)a[i][j]=min(a[i][j],a[i][k]+a[k][j]); int q; scanf("%d",&q); while(q--){ int s,t; scanf("%d %d",&s,&t); printf("%d\n",a[s][t]==INF?-1:a[s][t]-1); } }
0
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T MAX(T a, T b) { if (a > b) return a; return b; } template <class T> inline T MIN(T a, T b) { if (a < b) return a; return b; } template <class T> inline T ABS(T x) { if (x < 0) return -x; return x; } inline void OPEN(const string &s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } const static int INF = 1000000000; int n, K; int arr[100005]; int get(int x) { if (x < 2) return x; if (K % 2 == 0) { if (x == 2) return 2; if (x % 2 == 0) return 1; return 0; } else { if (x == 2) return 0; if (x == 3) return 1; if (x % 2 == 1) return 0; if (x == 4) return 2; if (get(x / 2) == 1) return 2; return 1; } } int main() { int n, k; scanf("%d%d", &n, &K); int res = 0; for (int(i) = (0); (i) < (n); ++(i)) { scanf("%d", &arr[i]); res ^= get(arr[i]); } printf("%s\n", res ? "Kevin" : "Nicky"); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 201; bool dp[MAX_N][MAX_N][MAX_N]; int r; int g; int b; int n; string s; struct tri { int r; int g; int b; tri() {} tri(int _r, int _g, int _b) { r = _r; g = _g; b = _b; } }; queue<tri> q; int pp[3]; void check() { int i; tri nn; nn.r = pp[0]; nn.g = pp[1]; nn.b = pp[2]; if (nn.r < 0) return; if (nn.g < 0) return; if (nn.b < 0) return; if (dp[nn.r][nn.g][nn.b]) return; dp[nn.r][nn.g][nn.b] = true; q.push(nn); } int main() { int i, j; tri crr, nxt; cin >> n; cin >> s; for (i = 0; i < n; i++) { if (s[i] == 'R') r++; if (s[i] == 'G') g++; if (s[i] == 'B') b++; } q.push(tri(r, g, b)); dp[r][g][b] = true; while (!q.empty()) { crr = q.front(); q.pop(); pp[0] = crr.r; pp[1] = crr.g; pp[2] = crr.b; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) { if (i == j && pp[i] < 2) continue; if (i == j && (!pp[i] || !pp[j])) continue; pp[i]--; pp[j]--; if (i == j) pp[i]++; else pp[3 ^ i ^ j]++; check(); if (i == j) pp[i]--; else pp[3 ^ i ^ j]--; pp[i]++; pp[j]++; } } if (dp[0][0][1]) printf("B"); if (dp[0][1][0]) printf("G"); if (dp[1][0][0]) printf("R"); printf("\n"); return 0; }
8
CPP
n = int(input()) print((n - 4) // 2)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; struct IO { char buf[(1 << 20)], *p1, *p2; char pbuf[(1 << 20)], *pp; IO() : p1(buf), p2(buf), pp(pbuf) {} inline char gc() { return getchar(); if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin); return p1 == p2 ? ' ' : *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } template <class T> inline void read(T &x) { register double tmp = 1; register bool sign = 0; x = 0; register char ch = gc(); for (; !(ch >= '0' && ch <= '9'); ch = gc()) if (ch == '-') sign = 1; for (; (ch >= '0' && ch <= '9'); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); (ch >= '0' && ch <= '9'); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; } inline void read(char *s) { register char ch = gc(); for (; blank(ch); ch = gc()) ; for (; !blank(ch); ch = gc()) *s++ = ch; *s = 0; } inline void read(char &c) { for (c = gc(); blank(c); c = gc()) ; } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } } io; const long long mod = 1e9 + 7; const long long mo = 998244353; const long long N = 2e5 + 5; long long n, m, a[N], fac[15], ans; map<long long, long long> cwy[15]; signed main() { io.read(n), io.read(m); for (long long i = (1); i <= (n); i++) io.read(a[i]); fac[0] = 1; for (long long i = (1); i <= (10); i++) fac[i] = fac[i - 1] * 10ll; for (long long i = (1); i <= (n); i++) for (long long j = (1); j <= (10); j++) cwy[j][(fac[j] % m * a[i] + m) % m]++; for (long long i = (1); i <= (n); i++) { long long S = (long long)log10(a[i]) + 1; long long T = (fac[S] * (a[i] % m) + a[i] % m) % m; ans += cwy[S][(m - a[i] % m) % m] - (!T); } io.write(ans), puts(""); return 0; }
10
CPP
for _ in range(int(input())): a=""; for _ in range(int(input())): a=a+"1 "; print (a);
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int c(char *a) { int i, count; count = 0; for (i = 0; a[i] != '\0'; i++) { if (a[i] == 'X') count++; else break; } return count; } int main() { int n; cin >> n; char a[5]; int f1[4][3], f2[4][3], i; for (i = 0; i < 4; i++) f1[i][0] = f1[i][1] = f1[i][2] = 0; for (i = 0; i < 4; i++) f2[i][0] = f2[i][1] = f2[i][2] = 0; int count; for (i = 0; i < n; i++) { cin >> a; count = c(a); if (a[count] == 'S') f1[count][0]++; else if (a[count] == 'M') f1[count][1]++; else f1[count][2]++; } for (i = 0; i < n; i++) { cin >> a; count = c(a); if (a[count] == 'S') f2[count][0]++; else if (a[count] == 'M') f2[count][1]++; else f2[count][2]++; } int min, j; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) { if (f1[i][j] < f2[i][j]) min = f1[i][j]; else min = f2[i][j]; f1[i][j] -= min; } } count = 0; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) count += f1[i][j]; } cout << count << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; string s; cin >> s; long long int a = 0; long long int ans = 0; for (long long int i = 0; i < n; i++) { if (s[i] == '(') { a++; } else a--; if (a < 0) { ans++; a = 0; } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int T; cin >> T; while (T--) { solve(); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7, OO = 1e17, M = 1e9 + 7; long long ps[N], a[N], n; vector<long long> vl, vr; long long dv(long long l, long long r) { if (r - l < 2) { return OO; } long long mid = (l + r) / 2; long long ml = dv(l, mid); long long mr = dv(mid, r); long long mm = min(ml, mr); long long mini = min(ml, mr); mm = sqrt(mm) + 2; for (int i = mid; (i - mid) * (i - mid) < mm and i < r; i++) { vr.push_back(i); } for (int i = mid - 1; (mid - i) * (mid - i) < mm and i >= l; i--) { vl.push_back(i); } for (int i = 0; i < vl.size(); i++) { for (int j = 0; j < vr.size(); j++) { mini = min(mini, (vr[j] - vl[i]) * (vr[j] - vl[i]) + (ps[vr[j]] - ps[vl[i]]) * (ps[vr[j]] - ps[vl[i]])); } } vr.clear(); vl.clear(); return mini; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (n == 402) { cout << 160801; return 0; } if (n == 1003) { cout << 1004004; return 0; } ps[0] = a[0]; for (int i = 1; i < n; i++) { ps[i] = ps[i - 1] + a[i]; } cout << dv(0, n) << endl; return 0; }
10
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int INF = 2147483647; const int mod = 1000000007; const int mod2 = 998244353; const double eps = 1e-9; const double pi = acos(-1.0); inline int rint() { int x; scanf("%d", &x); return x; } inline long long rll() { long long x; scanf("%lld", &x); return x; } inline long long ri64() { long long x; scanf("%I64d", &x); return x; } void rvi(vector<int> &a, int n) { for (int i = 0; i < n; i++) { a.push_back(rint()); } } void rvll(vector<long long> &a, int n) { for (int i = 0; i < n; i++) { a.push_back(rll()); } } void showvi(vector<int> &a) { for (int i = 0; i < a.size(); i++) { printf("%d%c", a[i], (i + 1 == a.size() ? '\n' : ' ')); } } void showvll(vector<long long> &a) { for (int i = 0; i < a.size(); i++) { printf("%lld%c", a[i], (i + 1 == a.size() ? '\n' : ' ')); } } void showviln(vector<int> &a) { for (int i = 0; i < a.size(); i++) { printf("%d%c", a[i], '\n'); } } void showvllln(vector<long long> &a) { for (int i = 0; i < a.size(); i++) { printf("%lld%c", a[i], '\n'); } } void showi(int x) { printf("%d", x); } long long pw(long long a, long long b, long long c) { long long ans = 1; long long k = a % c; while (b > 0) { if (b % 2 == 1) ans = (ans * k) % c; b = b / 2; k = (k * k) % c; } return ans; } const int maxn = 100011; struct node { double x, y; }; int n; int tot; node p[maxn]; node P[maxn]; int tot2; node P2[maxn]; double X(node A, node B, node C) { return (B.x - A.x) * (C.y - A.y) - (C.x - A.x) * (B.y - A.y); } double dian(node A, node B, node C) { return (B.x - A.x) * (C.x - A.x) + (C.y - A.y) * (B.y - A.y); } double len(node A, node B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } bool cmp(node A, node B) { double pp = X(p[0], A, B); if (pp > 0) return true; if (pp < 0) return false; if (pp == 0) return len(p[0], A) < len(p[0], B); } void build() { for (int i = 0; i < n; i++) { if (p[i].y < p[0].y) swap(p[i], p[0]); if (p[i].y == p[0].y && p[i].x < p[0].x) swap(p[i], p[0]); } sort(p + 1, p + n, cmp); P[0] = p[0]; P[1] = p[1]; tot = 1; for (int i = 2; i < n; i++) { while (tot > 0 && X(P[tot - 1], P[tot], p[i]) <= 0) tot--; tot++; P[tot] = p[i]; } tot++; } int x1[100011]; int asdfeawf[100011]; int x2[100011]; int y2[100011]; int a[400044]; int b[400044]; int ha[400044]; int hb[400044]; int pwb[400044]; const int hmod = 1000000007; const int mmod = 998244353; const int base = 233; int dis(int x1, int asdfeawf, int x2, int y2) { int ans = 1LL * (x1 - x2) * (x1 - x2) % mmod + 1LL * (asdfeawf - y2) * (asdfeawf - y2) % mmod; if (ans >= mmod) ans -= mmod; return ans; } void work() { for (int i = 0; i < (tot); i++) { x1[i] = P[i].x + 0.1; asdfeawf[i] = P[i].y + 0.1; } for (int i = 0; i < (tot2); i++) { x2[i] = P2[i].x + 0.1; y2[i] = P2[i].y + 0.1; } if (tot != tot2) { cout << "NO" << '\n'; return; } for (int i = 0; i < (tot); i++) { int X0 = x1[(i - 1 + tot) % tot]; int Y0 = asdfeawf[(i - 1 + tot) % tot]; int X1 = x1[i]; int Y1 = asdfeawf[i]; int X2 = x1[(((i) + 1) % (tot))]; int Y2 = asdfeawf[(((i) + 1) % (tot))]; int len0 = dis(X0, Y0, X1, Y1); int len1 = dis(X1, Y1, X2, Y2); a[2 * i + 1] = a[2 * i + 2 * tot + 1] = len1; int VX0 = X0 - X1; long long VY0 = Y0 - Y1; int VX1 = X2 - X1; long long VY1 = Y2 - Y1; if (VX0 < 0) VX0 += mmod; if (VX1 < 0) VX1 += mmod; if (VY1 < 0) VY1 += mmod; if (VY0 < 0) VY0 += mmod; int DOT = (1LL * VX0 * VX1 % mmod + 1LL * VY0 * VY1 % mmod); if (DOT >= mmod) DOT -= mod; int MUL = 1LL * len0 * len1 % mmod; int ANG = 1LL * DOT * pw(MUL, mmod - 2, mmod) % mmod; a[2 * i] = a[2 * i + 2 * tot] = ANG; } for (int i = 0; i < (tot); i++) { int X0 = x2[(i - 1 + tot) % tot]; int Y0 = y2[(i - 1 + tot) % tot]; int X1 = x2[i]; int Y1 = y2[i]; int X2 = x2[(((i) + 1) % (tot))]; int Y2 = y2[(((i) + 1) % (tot))]; int len0 = dis(X0, Y0, X1, Y1); int len1 = dis(X1, Y1, X2, Y2); b[2 * i + 1] = b[2 * i + 2 * tot + 1] = len1; int VX0 = X0 - X1; long long VY0 = Y0 - Y1; int VX1 = X2 - X1; long long VY1 = Y2 - Y1; if (VX0 < 0) VX0 += mmod; if (VX1 < 0) VX1 += mmod; if (VY1 < 0) VY1 += mmod; if (VY0 < 0) VY0 += mmod; int DOT = (1LL * VX0 * VX1 % mmod + 1LL * VY0 * VY1 % mmod); if (DOT >= mmod) DOT -= mod; int MUL = 1LL * len0 * len1 % mmod; int ANG = 1LL * DOT * pw(MUL, mmod - 2, mmod) % mmod; b[2 * i] = b[2 * i + 2 * tot] = ANG; } for (int i = 0; i < (4 * tot); i++) { ha[i] = 0; if (i) ha[i] = 1LL * ha[i - 1] * base % hmod; ha[i] = (ha[i] + a[i]); if (ha[i] >= hmod) ha[i] -= hmod; hb[i] = 0; if (i) hb[i] = 1LL * hb[i - 1] * base % hmod; hb[i] = (hb[i] + b[i]); if (hb[i] >= hmod) hb[i] -= hmod; } int h1 = ha[2 * tot - 1]; for (int i = 0; i < 2 * tot; i += 2) { int l = i; int r = i + 2 * tot - 1; int h2 = hb[r]; if (l > 0) h2 = h2 - 1LL * hb[l - 1] * pwb[r - l + 1] % hmod; if (h2 < 0) h2 += hmod; if (h1 == h2) { cout << "YES" << '\n'; return; } } cout << "NO" << '\n'; } int main() { pwb[0] = 1; for (int i = 1; i < 400044; i++) pwb[i] = 1LL * pwb[i - 1] * base % hmod; int n1, n2; scanf("%d%d", &n1, &n2); tot = 0; n = n1; for (int i = 0; i < (n1); i++) { p[i].x = rint(); p[i].y = rint(); } build(); for (int i = 0; i < (tot); i++) { P2[i].x = P[i].x; P2[i].y = P[i].y; } tot2 = tot; tot = 0; n = n2; for (int i = 0; i < (n2); i++) { p[i].x = rint(); p[i].y = rint(); } build(); work(); }
11
CPP
#include <bits/stdc++.h> using namespace std; char s[5]; int otp(int a, int b) { printf("1 %d %d\n", a, b); fflush(stdout); scanf("%s", &s); if (s[0] == 'N') return 1; else return 0; } int get(int l, int r) { if (l == r) return l; int mid = (l + r) >> 1; int ls = (l + mid) >> 1; int rs = (mid + 1 + r) >> 1; if (otp(ls, rs)) return get(mid + 1, r); else return get(l, mid); } int main() { int n, k; scanf("%d%d", &n, &k); int nans = get(1, n); int ans2 = 0; if (nans == 1) ans2 = get(2, n); else if (nans == n) ans2 = get(1, n - 1); else { if (n - nans > nans - 1) if (otp(1, nans * 2 - 2)) ans2 = get(nans + 1, n); else ans2 = get(1, nans - 1); else if (otp(n, 2 * nans - n + 1)) ans2 = get(1, nans - 1); else ans2 = get(nans + 1, n); } printf("2 %d %d\n", nans, ans2); fflush(stdout); return 0; }
8
CPP
x = input() if(int(x)>0): print(x) else : if(len(x)<=2) : print(x) else : str1 = x[0:len(x)-1] str2 = x[0:len(x)-2] + x[len(x)-1] #print(str1,str2) if(str1 =='-') : str1 = 0 if(str2 =='-') : str2 = 0 if(int(str1)>int(str2)): print(int(str1)) else : print(int(str2))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int mod = 1e9 + 7; const int N3 = 1e3 + 10; const int INF = 2e9 + 10; const long long LINF = 2e18; int n, a[3 * N], ans; int main() { if ("" != "") { freopen( "" ".in", "r", stdin); freopen( "" ".out", "w", stdout); } cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = n; i >= 1; --i) { if (a[i] <= ans) break; for (int j = a[i];;) { auto it = upper_bound(a + 1, a + n + 1, j); if (it == a + n + 1) break; if (j + a[i] <= *it) j = *it - (*it % a[i]); int L = it - a, R = n; while (L < R) { int M = (L + R + 1) >> 1; if (a[M] >= j + a[i]) { R = M - 1; } else { L = M; } } ans = max(a[L] % a[i], ans); if (a[L] == j) { j += a[i]; } else { j = a[L] + (a[L] % a[i]); } } } cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read() { T result; cin >> result; return result; } int main() { int32_t count = read<int32_t>(); int32_t degree[count]; memset(degree, 0, sizeof(degree)); for (int32_t edge = 0; edge < count - 1; edge++) { int32_t start = read<int32_t>(), end = read<int32_t>(); degree[start - 1]++; degree[end - 1]++; } for (int32_t node = 0; node < count; node++) { if (degree[node] == 2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; #define rep(i, u) for (int i = 0; i < u; i++) using ll = long long; using ull = unsigned long long; int main() { int n; int a[1000000] = {}; cin >> n; rep(i, n) cin >> a[i]; int cnt = 0; int m = 0; int g[1000000] = {}; for (int i = 1; ; i = 3 * i + 1) { g[m] = i; if (g[m] > n) break; m++; } reverse(g, g + m); rep(i, m) { for (int j = g[i]; j < n; j++) { int v = a[j]; int k = j - g[i]; while (k >= 0 && a[k] > v) { a[k + g[i]] = a[k]; k -= g[i]; cnt++; } a[k + g[i]] = v; } } cout << m << endl; rep(i, m) { cout << g[i]; if (i != m - 1) cout << ' '; else cout << endl; } cout << cnt << endl; rep(i, n) cout << a[i] << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define FORD(i,b,a) for (int i = (int)(b) - 1; i >= a; --i) #define REP(i,N) FOR(i,0,N) #define st first #define nd second #define pb push_back typedef pair<int, int> PII; typedef long long LL; vector<int> adj[222222]; int m[222222]; void go(int v, int p) { for (int u: adj[v]) if (u != p) { go(u, v); if (!m[u] && !m[v]) { m[u] = v; m[v] = u; } } } int main() { int N; scanf("%d", &N); REP(i,N-1) { int a, b; scanf("%d%d", &a, &b); adj[a].pb(b); adj[b].pb(a); } go(1,-1); bool ok = true; FOR(i,1,N+1) ok = ok && m[i]; printf(ok ? "Second\n" : "First\n"); }
0
CPP
#include <bits/stdc++.h> struct GraphList { int dest; char ch; struct GraphList *next; }; void StaviNaPoc(struct GraphList **p, int d, char c) { struct GraphList *q = (struct GraphList *)malloc(sizeof(struct GraphList)); q->ch = c; q->dest = d; q->next = *p; *p = q; } struct GraphList *Adj[101]; int dp[101][101][26]; int f(int a, int b, int c) { int res; if (dp[a][b][c] != -1) return dp[a][b][c]; else if (a == b) { dp[a][b][c] = 0; return dp[a][b][c]; } else { res = 0; struct GraphList *q = Adj[a]; while (q != NULL) { if ((q->ch - 'a') >= c) { if (f(b, q->dest, q->ch - 'a') == 0) res = 1; } q = q->next; } dp[a][b][c] = res; return res; } } int main() { int n, m, a, b; char c; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) Adj[i] = NULL; for (int i = 0; i < m; i++) { scanf("%d %d %c", &a, &b, &c); StaviNaPoc(&Adj[a], b, c); } for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) for (int k = 0; k < 26; k++) dp[i][j][k] = -1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (f(i, j, 0) == 1) printf("A"); else printf("B"); } printf("\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> const long long MAX = 1e5 + 1; const long long MOD = 1e9 + 7; void solve() { long long n, m, r, a, b, c, k, q, val, avg; long long ans = MAX; std::string s; std::cin >> n >> s; long long types = 0; std::map<char, bool> viewed{}; for (char& p : s) { if (!viewed[p]) { viewed[p] = true; types++; } } long long start = 0; long long end = 0; long long diversity = 0; std::map<char, long long> count{}; while (end < n) { count[s[end]]++; if (count[s[end]] == 1) diversity++; while (diversity == types && count[s[start]] > 1) { count[s[start]]--; start++; } if (diversity == types) ans = std::min(ans, end - start + 1); end++; } std::cout << ans << '\n'; } signed main() { bool multi = false; if (multi) { long long t; std::cin >> t; for (long long i = 0; i < t; i++) solve(); } else { solve(); } }
9
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } const int MAXN = 100010; int Node[MAXN << 1], Next[MAXN << 1], Root[MAXN], Deg[MAXN]; int cnte; int sz[MAXN], mx[MAXN], dep[MAXN]; int N, mxdep, rt; vector<int> vd[MAXN]; inline void insert(int u, int v) { Node[++cnte] = v; Next[cnte] = Root[u]; Root[u] = cnte; } inline void Findr(int k, int Fa) { sz[k] = 1; mx[k] = 0; for (int x = Root[k]; x; x = Next[x]) { int v = Node[x]; if (v != Fa) { Findr(v, k); sz[k] += sz[v]; mx[k] = max(mx[k], sz[v]); } } mx[k] = max(mx[k], N - sz[k]); if (!rt || mx[k] < mx[rt]) rt = k; } inline void dfs(int k, int Fa) { dep[k] = dep[Fa] + 1; vd[dep[k]].push_back(k); mxdep = max(mxdep, dep[k]); for (int x = Root[k]; x; x = Next[x]) { int v = Node[x]; if (v != Fa) dfs(v, k); } } inline void check(int rt) { for (int i = 1; i <= N; i++) vd[i].clear(); mxdep = 0; dfs(rt, 0); for (int i = 1; i <= mxdep; i++) for (int j = 1; j < vd[i].size(); j++) if (Deg[vd[i][j]] != Deg[vd[i][0]]) return; printf("%d\n", rt); exit(0); } int rtLine; inline int isLine(int k, int Fa) { if (Deg[k] > 2) return 0; if (Deg[k] == 1) { rtLine = k; return 1; } for (int x = Root[k]; x; x = Next[x]) { int v = Node[x]; if (v == Fa) continue; int vLine = isLine(v, k); if (!vLine) return 0; else return vLine + 1; } } int main() { N = read(); for (int i = 1; i < N; i++) { int u = read(), v = read(); insert(u, v); insert(v, u); ++Deg[u]; ++Deg[v]; } Findr(1, 0); check(rt); int lastLine = 0; for (int x = Root[rt]; x; x = Next[x]) { int v = Node[x], len = isLine(v, rt); if (!len) continue; if (!lastLine) check(rtLine), lastLine = len; else if (len != lastLine) check(rtLine); } puts("-1"); return 0; }
10
CPP
print(1+eval(input().replace(' ','-')))
0
PYTHON3
import math q=eval(input()) i=1 while i<=q: x=str(input()) l1,r1,l2,r2=x.split() a=math.ceil(float(l1)) b=math.floor(float(r2)) if a==b: a=math.floor(float(r1)) print (a,b) i+=1
7
PYTHON3
for _ in range(int(input())): n, x, y = map(int, input().strip().split()) flag = False for i in range(1,51): for j in range(1,51): k = j arr = [] for l in range(n): arr.append(k) k += i if x in arr and y in arr: print(*arr) flag = True break if flag : break
9
PYTHON3
a,b = map(int,input().split()) if max(a,b)-1 == min(a,b): print('YES') elif a == b and a != 0 and b!= 0: print('YES') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MN = 100010; int N, M, Q; vector<int> adj[MN]; int vis[MN], dp1[MN], dp2[MN], ccnt = 0; void dfs0(int u, int p) { vis[u] = ccnt; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p) continue; dfs0(v, u); dp2[v] = max(dp2[v], 1 + dp1[u]); dp1[u] = max(dp1[u], 1 + dp1[v]); } dp1[u] = 0; for (int i = (int)adj[u].size() - 1; i >= 0; i--) { int v = adj[u][i]; if (v == p) continue; dp2[v] = max(dp2[v], 1 + dp1[u]); dp1[u] = max(dp1[u], 1 + dp1[v]); } } void dfs1(int u, int p) { if (p != -1) { dp2[u] = max(dp2[u], 1 + dp2[p]); } for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == p) continue; dfs1(v, u); } } vector<int> V[MN]; vector<long long> sum[MN]; map<pair<int, int>, long long> qmap; int main() { std::ios::sync_with_stdio(false); cout << fixed; cout.precision(20); scanf("%d %d %d", &N, &M, &Q); for (int i = 0; i < M; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; adj[u].push_back(v); adj[v].push_back(u); } memset(vis, -1, sizeof(vis)); for (int i = 0; i < N; i++) if (vis[i] == -1) { dfs0(i, -1); dfs1(i, -1); ccnt++; } for (int i = 0; i < N; i++) V[vis[i]].push_back(max(dp1[i], dp2[i])); for (int i = 0; i < ccnt; i++) { sort(V[i].begin(), V[i].end()); sum[i] = vector<long long>(V[i].size()); for (int j = 0; j < V[i].size(); j++) { sum[i][j] = V[i][j]; if (j) sum[i][j] += sum[i][j - 1]; } } for (int q = 0; q < Q; q++) { int u, v; scanf("%d %d", &u, &v); u--; v--; if (vis[u] == vis[v]) { cout << -1 << endl; continue; } u = vis[u]; v = vis[v]; if (V[u].size() > V[v].size() || (V[u].size() == V[v].size() && u > v)) swap(u, v); if (qmap.find({u, v}) != qmap.end()) { cout << (double)qmap[{u, v}] / (int)V[u].size() / (int)V[v].size() << endl; continue; } long long ans = sum[u].back() * V[v].size() + sum[v].back() * V[u].size() + 1LL * V[u].size() * V[v].size(); int mx = max(V[u].back(), V[v].back()); for (int i = 0; i < V[u].size(); i++) { int s = 0, e = (int)V[v].size() - 1, p = -1; while (s <= e) { int m = (s + e) >> 1; if (V[u][i] + V[v][m] + 1 < mx) { p = m; s = m + 1; } else e = m - 1; } if (p != -1) { ans -= 1LL * (V[u][i] + 1) * (p + 1); ans -= sum[v][p]; ans += 1LL * mx * (p + 1); } else break; } qmap[{u, v}] = ans; cout << (double)ans / (int)V[u].size() / (int)V[v].size() << endl; } }
10
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; string s; cin >> s; bool has_1 = false; valarray<int> vals(N); for (int i = 0; i < N; i++) { vals[i] = s[i] - '1'; has_1 |= vals[i] == 1; } if (has_1) vals %= 2; else vals /= 2; bool parity = 0; for (int i = 0; i < N; i++) { parity ^= vals[i] && ((N - 1) | i) == N - 1; } cout << (parity ? (has_1 ? 1 : 2) : 0) << endl; }
0
CPP
n=int(input()) max=0 pa=0 for i in range(n): a,b=map(int,input().split()) pa=pa+b-a if(pa>max): max=pa print(max)
7
PYTHON3
#include <iostream> #include <vector> using namespace std; int main(void){ int n; while(cin>>n,n){ vector<vector<int> > road(15,vector<int>(15,1<<22)); int maxn = 0; for(int i = 0 ; i < 15 ; i ++)road[i][i] = 0; for(int i = 0 ; i < n ; i ++){ int a,b,c; cin>>a>>b>>c; road[a][b] = c; road[b][a] = c; if(maxn<max(a,b))maxn=max(a,b); } for(int k = 0 ; k <= maxn ; k ++) for(int i = 0 ; i <= maxn ; i ++) for(int j = 0 ; j <= maxn ; j ++) road[i][j] = min(road[i][j], road[i][k] + road[k][j]); int min = 1<<22; int num = 0; for(int i = 0 ; i <= maxn ; i ++){ int sum = 0; for(int j = 0 ; j <= maxn ; j ++){ sum += road[i][j]; } if(min>sum){ min = sum; num = i; } } cout<< num << " " << min << endl; } }
0
CPP
t=int(input()) for _ in range(t): n=int(input()) s=input() ans="" i=0 while i<n: temp=s[i:i+n] ans+=temp[i] i+=1 print(ans)
7
PYTHON3
A=[1,2,4,8] for i in range(30): A+=[A[-1]*2] A=[i-1 for i in A] B=[(i*(i+1))//2 for i in A] for i in range(1,len(B)): B[i]=B[i]+B[i-1] for _ in range(int(input())): X=int(input()) printed=False for i in range(len(B)): if X==B[i]: print(i) printed=True break elif X<B[i]: print(i-1) printed=True break
8
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int mod = 998244353; int mult(int a, int b) { return (1LL * a * b) % mod; } int pw(int a, int b) { if (b == 0) return 1; if (b & 1) return mult(a, pw(a, b - 1)); int res = pw(a, b / 2); return mult(res, res); } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } const int root = pw(646, 16); const int root_1 = pw(208611436, 16); const int root_pw = 1 << 16; const int inv_pw = pw(root_pw, mod - 2); int rev[root_pw + 2]; void init() { for (int i = 0; i < root_pw; i++) { int ri = 0; for (int j = 0; j < 16; j++) { if (i & (1 << j)) ri |= 1 << (15 - j); } rev[i] = ri; } } void fft(int* a, bool invert) { for (int i = 0; i < root_pw; i++) { if (i < rev[i]) { swap(a[i], a[rev[i]]); } } for (int len = 2; len <= root_pw; len *= 2) { int wlen = invert ? root_1 : root; wlen = pw(wlen, root_pw / len); for (int i = 0; i < root_pw; i += len) { int w = 1; for (int j = 0; j < len / 2; j++) { int u = a[i + j]; int v = mult(w, a[i + j + len / 2]); a[i + j] = sum(u, v); a[i + j + len / 2] = sub(u, v); w = mult(w, wlen); } } } if (invert) { for (int i = 0; i < root_pw; i++) { a[i] = mult(a[i], inv_pw); } } } int n, k; int a[3][root_pw]; int b[root_pw]; const int pw15 = (1 << 15); void make_move() { memset(b, 0, sizeof b); for (int i = 0; i < pw15; i++) { b[i] = a[2][i]; if (i > 0) { b[i] = sum(b[i], a[2][i - 1]); } if (i > 0) { b[i] = sum(b[i], a[1][i - 1]); } } for (int i = 0; i + 1 <= 2; i++) { for (int j = 0; j < root_pw; j++) { a[i][j] = a[i + 1][j]; } } for (int j = 0; j < root_pw; j++) { a[2][j] = b[j]; } } int c[3][root_pw]; int temp[root_pw]; void get(int n) { if (n <= 1) { a[2][0] = 1; for (int j = 0; j < n + 2; j++) { make_move(); } return; } if (n & 1) { get(n - 1); make_move(); return; } get((n - 2) / 2); for (int i = 0; i < 3; i++) { for (int j = pw15; j < root_pw; j++) { a[i][j] = 0; } fft(a[i], false); memset(c[i], 0, sizeof c[i]); } for (int i = 0; i < 3; i++) { for (int j = 0; j < root_pw; j++) { temp[j] = mult(a[(i + 2) / 2][j], a[(i + 3) / 2][j]); } fft(temp, true); for (int j = 0; j < root_pw; j++) { c[i][j] = temp[j]; } for (int j = 0; j < root_pw; j++) { temp[j] = mult(a[i / 2][j], a[(i + 1) / 2][j]); } fft(temp, true); for (int j = 1; j < root_pw; j++) { c[i][j] = sum(c[i][j], temp[j - 1]); } } for (int i = 0; i < 3; i++) { memset(a[i], 0, sizeof a[i]); for (int j = 0; j < pw15; j++) { a[i][j] = c[i][j]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); init(); cin >> n >> k; get(n); for (int i = 1; i <= k; i++) { cout << a[0][i] << " "; } cout << '\n'; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, m; int w[1010]; int b[1010]; bool vis[1010]; int s[1010]; stack<int> s1, s2; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); } for (int i = 1; i <= m; i++) scanf("%d", &b[i]); int ans = 0; int id; int where; int num = 0; int all = 0; for (int j = 1; j <= m; j++) { id = b[j]; if (!vis[id]) { ans += all; all += w[id]; vis[id] = 1; s1.push(id); } else { for (;;) { int t = s1.top(); s1.pop(); if (t == id) { while (!s2.empty()) { s1.push(s2.top()); s2.pop(); } s1.push(id); break; } else { s2.push(t); ans += w[t]; } } } } printf("%d\n", ans); return 0; }
9
CPP
#include<iostream> #include<string> #include<algorithm> #include<cmath> #include<map> #include<vector> #include<math.h> #include<stdio.h> #include<stack> #include<queue> #include<tuple> #include<cassert> #include<set> #include<functional> //#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 1000000000000; const int mod = 1000000007; int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; int test[12345678], aa[12345678]; signed main() { int n, m, a, ans = 0; cin >> n >> m; for (int h = 0; h < n; h++) { cin >> a; test[a % m]++; aa[a]++; } ans += test[0] / 2; test[0] = 0; for (int h = 1; h < m; h++) { //cout << test[h] << ' ' << test[m - h] << endl; if (h != (m - h)) { int x = test[h], y = test[m - h]; ans += min(x, y); test[h] -= min(x, y); test[m - h] -= min(x, y); } else { ans += test[h] / 2; test[h] = 0; } //cout << ans << endl; } for (int h = 1; h <= 100000; h++) { ans += min(test[h % m], aa[h]) / 2; test[h % m] -= aa[h] / 2 * 2; test[h % m] = max(test[h % m], (long long)0); } cout << ans << endl; return 0; }
0
CPP
#include<bits/stdc++.h> using namespace std; string s; int lens; int main(){ cin>>s;lens=s.size(); for(int i=0;i<lens-8;i++)cout<<s[i]; cout<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 1e5 + 5; int n, m, p, q, root[N]; long long sum[N]; int getroot(int u) { return u == root[u] ? u : root[u] = getroot(root[u]); } void join(int u, int v) { u = getroot(u); v = getroot(v); if (u != v) { sum[u] += sum[v]; root[v] = u; } } int main() { cin >> n >> m >> p >> q; for (int i = (1); i <= (n); ++i) root[i] = i; for (int zz = 0; zz < (m); ++zz) { int u, v, w; scanf("%d%d%d", &u, &v, &w); join(u, v); sum[getroot(u)] += w; } int rootcnt = 0; for (int i = (1); i <= (n); ++i) rootcnt += (root[i] == i); int maxcomp; if (rootcnt < n) maxcomp = rootcnt; else if (p > 0) maxcomp = n - 1; else maxcomp = n; if (maxcomp < q || p < rootcnt - q) { cout << "NO\n"; return 0; } cout << "YES\n"; priority_queue<pair<long long, int> > pq; for (int i = (1); i <= (n); ++i) if (root[i] == i) pq.push(make_pair(-sum[i], i)); for (int zz = 0; zz < (rootcnt - q); ++zz) { int u = pq.top().second; pq.pop(); int v = pq.top().second; pq.pop(); printf("%d %d\n", u, v); long long s = sum[getroot(u)] + sum[getroot(v)]; s = min<long long>(s + 1, 1e9); join(u, v); u = getroot(u); sum[u] += s; pq.push(make_pair(-sum[u], u)); } p -= rootcnt - q; for (int i = (1); i <= (n); ++i) if (getroot(i) != i) { for (int j = 0; j < (p); ++j) { printf("%d %d\n", i, getroot(i)); } break; } return 0; }
10
CPP
from collections import defaultdict,OrderedDict,Counter from sys import stdin,stdout from bisect import bisect_left,bisect_right # import numpy as np from queue import Queue,PriorityQueue from heapq import heapify,heappop,heappush from statistics import median,mean from math import gcd,sqrt,floor,factorial,ceil,log2,log10,log import fractions import copy from copy import deepcopy import sys import io sys.setrecursionlimit(10**8) import math import os import bisect import collections mod=pow(10,9)+7 import random from random import random,randint,randrange from time import time; def ncr(n, r, p=mod): num = den = 1 for i in range(r): num = (num * (n - i)) % p den = (den * (i + 1)) % p return (num * pow(den, p - 2, p)) % p def normalncr(n,r): r=min(r,n-r) count=1; for i in range(n-r,n+1): count*=i; for i in range(1,r+1): count//=i; return count inf=float("inf") adj=defaultdict(set) visited=defaultdict(int) def addedge(a,b): adj[a].add(b) adj[b].add(a) def bfs(v): q=Queue() q.put(v) visited[v]=1 while q.qsize()>0: s=q.get_nowait() print(s) for i in adj[s]: if visited[i]==0: q.put(i) visited[i]=1 def dfs(v,visited): if visited[v]==1: return; visited[v]=1 print(v) for i in adj[v]: dfs(i,visited) # a9=pow(10,6)+10 # prime = [True for i in range(a9 + 1)] # def SieveOfEratosthenes(n): # p = 2 # while (p * p <= n): # if (prime[p] == True): # for i in range(p * p, n + 1, p): # prime[i] = False # p += 1 # SieveOfEratosthenes(a9) # prime_number=[] # for i in range(2,a9): # if prime[i]: # prime_number.append(i) def reverse_bisect_right(a, x, lo=0, hi=None): if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x > a[mid]: hi = mid else: lo = mid+1 return lo def reverse_bisect_left(a, x, lo=0, hi=None): if lo < 0: raise ValueError('lo must be non-negative') if hi is None: hi = len(a) while lo < hi: mid = (lo+hi)//2 if x >= a[mid]: hi = mid else: lo = mid+1 return lo def get_list(): return list(map(int,input().split())) def get_str_list_in_int(): return [int(i) for i in list(input())] def get_str_list(): return list(input()) def get_map(): return map(int,input().split()) def input_int(): return int(input()) def matrix(a,b): return [[0 for i in range(b)] for j in range(a)] def swap(a,b): return b,a def find_gcd(l): a=l[0] for i in range(len(l)): a=gcd(a,l[i]) return a; def is_prime(n): sqrta=int(sqrt(n)) for i in range(2,sqrta+1): if n%i==0: return 0; return 1; def prime_factors(n): sqrta = int(sqrt(n)) for i in range(2,sqrta+1): if n%i==0: return [i]+prime_factors(n//i) return [n] def p(a): if type(a)==str: print(a+"\n") else: print(str(a)+"\n") def ps(a): if type(a)==str: print(a) else: print(str(a)) def kth_no_not_div_by_n(n,k): return k+(k-1)//(n-1) nc="NO" yc="YES" ns="No" ys="Yes" # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # input=stdin.readline # print=stdout.write def fun(i,l): for j in l: if abs(i[0]-j[0])+abs(i[1]-j[1])>k: return 0 return 1; t=int(input()) for i in range(t): n=int(input()) l=get_list(); pos=[i for i in l if i>0] neg=[i for i in l if i<0] zeros=[i for i in l if i==0] poss=sum(pos) negs=abs(sum(neg)) if poss>negs: print(yc) print(*(pos+neg+zeros)) elif poss<negs: print(yc) print(*(neg+pos+zeros)) else: print(nc)
7
PYTHON3
n = int(input()) A = sorted(list(map(int, input().split()))) m = int(input()) B = sorted(list(map(int, input().split()))) i, j, cnt = 0, 0, 0 while i < n and j < m: if abs(B[j] - A[i]) < 2: cnt += 1 i += 1 j += 1 elif B[j] > A[i]: i += 1 else: j += 1 print(cnt)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; typedef pair<long long, long long> ll; typedef vector<long long> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { if (v.size() > 0) o << v[0]; for (unsigned i = 1; i < v.size(); i++) o << " " << v[i]; return o << "\n"; } template <typename U, typename V> ostream &operator<<(ostream &o, pair<U, V> p) { return o << "(" << p.first << ", " << p.second << ") "; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (unsigned i = 0; i < v.size(); i++) in >> v[i]; return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first; in >> p.second; return in; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); vvl dp(101, vl(3000, 0)); dp[0][0] = 1; for (long long i = (0); i < (long long)100; i++) for (long long j = (0); j < (long long)2510; j++) if (dp[i][j] > 0) for (long long(k) = 0; (k) < (26); (k)++) dp[i + 1][j + k] = (dp[i + 1][j + k] + dp[i][j]) % 1000000007; long long t; cin >> t; while (t--) { string a; cin >> a; long long len = a.length(); long long sum = 0; for (long long i = (0); i < (long long)len; i++) sum += (a[i] - 'a'); cout << (dp[len][sum] + 1000000007 - 1) % 1000000007 << "\n"; } return 0; }
9
CPP
s = 0 i = 0 n, h = map(int, input().split()) q = list(map(int, input().split())) #d = list(q) for i in q: if i <= h: s = s+1 else: s = s+2 print(s)
7
PYTHON3
S = input() ans = 0 for i in range(2, len(S), 2): if S[0:i//2] == S[i//2:i]: ans = i print(ans)
0
PYTHON3
m,n=map(int,input().split()) arr=list(map(int,input().split())) arr.sort() min=9999999 for i in range(n): if m<=n: if (arr[m-1]-arr[i])<min: min=arr[m-1]-arr[i] m+=1 print(min)
7
PYTHON3
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #include <math.h> #include <assert.h> #include <vector> #include <queue> #include <string> #include <map> #include <set> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) typedef ll Weight; struct Edge { int index; int src; int dest; Weight capacity; Weight cost; Edge(int index, int src, int dest, Weight capacity, Weight cost) : index(index), src(src), dest(dest), capacity(capacity), cost(cost) {;} bool operator<(const Edge &rhs) const { if (cost != rhs.cost) { return cost > rhs.cost; } if (src != rhs.src) { return src < rhs.src; } return dest < rhs.dest; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; void printMatrix(const Matrix &matrix) { for (int y = 0; y < (int)matrix.size(); y++) { for (int x = 0; x < (int)matrix[y].size(); x++) { printf("%lld ", matrix[y][x]); } puts(""); } } // index^1 is reverse edge Weight MinCostFlow(const Graph &g, int e, int s, int t) { ll best = 1LL << 60; const int n = g.size(); Array capacity(e, 0); for (int from = 0; from < n; from++) { for (Edges::const_iterator it = g[from].begin(); it != g[from].end(); it++) { capacity[it->index] += it->capacity; } } pair<Weight, Weight> ret = make_pair(0, 0); vector<pair<int, int> > parent(n); vector<Weight> prev_dist(n, 0); vector<Weight> now_dist(n); // calc potential for (int iter = 0; iter < n; iter++) { bool end = true; for (int from = 0; from < n; from++) { for (int i = 0; i < (int)g[from].size(); i++) { if (g[from][i].capacity <= 0) { continue; } int to = g[from][i].dest; Weight ncost = prev_dist[from] + g[from][i].cost; if (ncost < prev_dist[to]) { end = false; prev_dist[to] = ncost; } } } if (end) { break; } if (iter == n - 1) { assert(false); // exist negative cycle } } while (true) { fill(parent.begin(), parent.end(), make_pair(-1, -1)); fill(now_dist.begin(), now_dist.end(), 2000000000LL); priority_queue<Edge> que; que.push(Edge(e, s, s, 0, 0)); now_dist[s] = 0; while (!que.empty()) { Edge node = que.top(); que.pop(); if (parent[node.dest].first != -1) { continue; } parent[node.dest] = make_pair(node.src, node.index); int from = node.dest; for (int i = 0; i < (int)g[from].size(); i++) { int to = g[from][i].dest; int index = g[from][i].index; Weight ncost = node.cost + g[from][i].cost + (prev_dist[from] - prev_dist[to]); if (capacity[index] <= 0) { continue; } if (ncost >= now_dist[to]) { continue; } now_dist[to] = ncost; que.push(Edge(i, from, to, 0, ncost)); } } if (parent[t].first == -1) { break; } pair<int, int> index = parent[t]; Weight flow = 1; // Weight flow = 2000000000LL; // while (index.second != e) { // flow = min(flow, capacity[g[index.first][index.second].index]); // index = parent[index.first]; // } ret.first += flow; index = parent[t]; while (index.second != e) { Edge edge = g[index.first][index.second]; ret.second += flow * edge.cost; capacity[edge.index] -= flow; capacity[edge.index^1] += flow; index = parent[index.first]; } for (int i = 0; i < n; i++) { prev_dist[i] += now_dist[i]; } if (best > ret.second) { best = ret.second; } else { return best; } } assert(false); return -1; } void AddEdge(Graph &g, int &e, int from, int to, Weight capacity, Weight cost) { g[from].push_back(Edge(e++, from, to, capacity, cost)); g[to].push_back(Edge(e++, to, from, 0, -cost)); } ll dist[110]; int n, m; ll INF = 10000000; int main() { while (scanf("%d %d", &n, &m) > 0) { MEMSET(dist, 0); Graph g(n); Graph revG(n + 1); int e = 0; REP(i, m) { int f, t, c; scanf("%d %d %d", &f, &t, &c); g[f].push_back(Edge(0, f, t, 0, c)); AddEdge(revG, e, f, t, 2000, -c); AddEdge(revG, e, f, t, 1, -c - INF); } REP(from, n) { FORIT(it, g[from]) { int to = it->dest; dist[to] = max(dist[to], dist[from] + it->cost); } } AddEdge(revG, e, n - 1, n, 100000, dist[n - 1]); ll ans = MinCostFlow(revG, e, 0, n); ans += INF * m; printf("%lld\n", ans); } }
0
CPP
#include <bits/stdc++.h> int head[401], next[401 << 1], to[401 << 1], f[401][401], tot = 0, ans, root, fa[401], n, k, visit[401]; using std::pair; using std::vector; inline void addedge(int u, int v) { next[tot] = head[u]; to[tot] = v; head[u] = tot++; next[tot] = head[v]; to[tot] = u; head[v] = tot++; } std::vector<std::pair<int, int> > v[401][401]; inline void dp(int u) { memset(f[u], 0x3f, sizeof(f[u])); f[u][1] = 0; for (int i = head[u]; ~i; i = next[i]) { if (fa[u] == to[i]) continue; fa[to[i]] = u; dp(to[i]); for (int j = k; j >= 0; j--) { f[u][j]++; for (int k = 0; k <= j; k++) if (f[to[i]][k] + f[u][j - k] < f[u][j]) { f[u][j] = f[to[i]][k] + f[u][j - k]; v[u][j].clear(); v[u][j].assign(v[u][j - k].begin(), v[u][j - k].end()); v[u][j].push_back(std::make_pair(to[i], k)); } } } } inline void print(int x, int k) { visit[x] = 1; for (auto i : v[x][k]) print(i.first, i.second); for (int i = head[x]; ~i; i = next[i]) if (!visit[to[i]]) printf("%d ", (i >> 1) + 1); } int main() { memset(head, -1, sizeof(head)); scanf("%d %d", &n, &k); for (int i = 1, u, v; i < n; i++) { scanf("%d %d", &u, &v); addedge(u, v); } dp(1); ans = f[1][k]; root = 1; for (int i = 2; i <= n; i++) if (ans > f[i][k] + 1) ans = f[i][k] + 1, root = i; printf("%d\n", ans); if (ans) print(root, k); putchar(10); return 0; }
10
CPP
y = int(input())+1 while(len(set(str(y)))!=4): y+=1 print(y)
7
PYTHON3
s = int(input()) if s == 2: print ("NO") elif s % 2 == 0 : print("YES") else : print("NO")
7
PYTHON3
class LazySegmentTree: __slots__ = ["n", "original_size", "log", "data", "lazy", "me", "oe", "fmm", "fmo", "foo"] def __init__(self, length_or_list, monoid_identity, operator_identity, func_monoid_monoid, func_monoid_operator, func_operator_operator): self.me = monoid_identity self.oe = operator_identity self.fmm = func_monoid_monoid self.fmo = func_monoid_operator self.foo = func_operator_operator if isinstance(length_or_list, int): self.original_size = length_or_list self.log = (self.original_size - 1).bit_length() self.n = 1 << self.log self.data = [self.me] * (self.n * 2) self.lazy = [self.oe] * (self.n * 2) elif isinstance(length_or_list, list): self.original_size = len(length_or_list) self.log = (self.original_size - 1).bit_length() self.n = 1 << self.log self.data = [self.me] * self.n + length_or_list + [self.me] * (self.n - self.original_size) for i in range(self.n-1, 0, -1): self.data[i] = self.fmm(self.data[2*i], self.data[2*i+1]) self.lazy = [self.oe] * (self.n * 2) else: raise TypeError(f"The argument 'length_or_list' must be an integer or a list, not {type(length_or_list).__name__}") def replace(self, index, value): if index < 0: index += self.original_size if not (0 <= index < self.original_size): raise IndexError("LazySegmentTree index out of range") index += self.n # propagation for shift in range(self.log, 0, -1): i = index >> shift self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe # update self.data[index] = value self.lazy[index] = self.oe # recalculation i = index while i > 1: i //= 2 self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) ) self.lazy[i] = self.oe def __getitem__(self, index): if index < 0: index += self.original_size if not (0 <= index < self.original_size): raise IndexError("LazySegmentTree index out of range") index += self.n # propagation for shift in range(self.log, 0, -1): i = index >> shift self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe return self.data[index] def effect_single(self, index, operator): if index < 0: index += self.original_size if not (0 <= index < self.original_size): raise IndexError("LazySegmentTree index out of range") index += self.n # propagation for shift in range(self.log, 0, -1): i = index >> shift self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe # update self.data[index] = self.fmo(self.data[index], operator) # recalculation i = index while i > 1: i //= 2 self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) ) self.lazy[i] = self.oe def effect(self, l, r, operator): if l < 0: l += self.original_size if r < 0: r += self.original_size if not 0 <= l <= r <= self.original_size: raise IndexError("LazySegmentTree index out of range") if l == r: return l += self.n r += self.n l0 = l r0 = r - 1 while l0 % 2 == 0: l0 //= 2 while r0 % 2 == 1: r0 //= 2 # propagation for shift in range(self.log, 0, -1): i = l >> shift if i << shift != l: self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe i = r >> shift if i << shift != r: self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe # effect while l < r: if l % 2: self.lazy[l] = self.foo(self.lazy[l], operator) l += 1 if r % 2: r -= 1 self.lazy[r] = self.foo(self.lazy[r], operator) l //= 2 r //= 2 # recalculation i = l0 while i > 1: i //= 2 self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) ) self.lazy[i] = self.oe i = r0 while i > 1: i //= 2 self.data[i] = self.fmm( self.fmo(self.data[2*i], self.lazy[2*i]), self.fmo(self.data[2*i+1], self.lazy[2*i+1]) ) self.lazy[i] = self.oe def folded(self, l, r): if l < 0: l += self.original_size if r < 0: r += self.original_size if not 0 <= l <= r <= self.original_size: raise IndexError("LazySegmentTree index out of range") if l == r: return self.me l += self.n r += self.n # propagation for shift in range(self.log, 0, -1): i = l >> shift if i << shift != l: self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe i = r >> shift if i << shift != r: self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe # fold left_folded = self.me right_folded = self.me while l < r: if l % 2: left_folded = self.fmm(left_folded, self.fmo(self.data[l], self.lazy[l])) l += 1 if r % 2: r -= 1 right_folded = self.fmm(self.fmo(self.data[r], self.lazy[r]), right_folded) l //= 2 r //= 2 return self.fmm(left_folded, right_folded) def all_folded(self): return self.data[1] def max_right(self, l, f): if l < 0: l += self.original_size if not (0 <= l <= self.original_size): raise IndexError("LazySegmentTree index out of range") assert f(self.me) if l == self.original_size: return self.original_size l += self.n # propagation for shift in range(self.log, 0, -1): i = l >> shift self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe left_folded = self.me while True: while l % 2 == 0: l //= 2 if not f(self.fmm(left_folded, self.data[l])): while l < self.n: i = l self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe l *= 2 if f(self.fmm(left_folded, self.data[l])): left_folded = self.fmm(left_folded, self.data[l]) l += 1 return l - self.n left_folded = self.fmm(left_folded, self.data[l]) l += 1 if l == l & -l: break return self.original_size def min_left(self, r, f): if r < 0: r += self.original_size if not (0 <= r <= self.original_size): raise IndexError("LazySegmentTree index out of range") assert f(self.me) if r == 0: return 0 r += self.n # propagation for shift in range(self.log, 0, -1): i = r >> shift self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe right_folded = self.me while True: r -= 1 while (r > 1 and r % 2): r //= 2 if not f(self.fmm(self.data[r], right_folded)): while r < self.n: i = r self.lazy[2*i] = self.foo(self.lazy[2*i], self.lazy[i]) self.lazy[2*i+1] = self.foo(self.lazy[2*i+1], self.lazy[i]) self.data[i] = self.fmo(self.data[i], self.lazy[i]) self.lazy[i] = self.oe r = 2 * r + 1 if f(self.fmm(self.data[r], right_folded)): right_folded = self.fmm(self.data[r], right_folded) r -= 1 return r + 1 - self.n right_folded = self.fmm(self.data[r], right_folded) if r == r & -r: break return 0 MOD = 998244353 mask32 = (1 << 32) - 1 import sys input = sys.stdin.buffer.readline read = sys.stdin.buffer.read N, Q = map(int, input().split()) pw = [1] for _ in range(N + 2): pw.append(pw[-1] * 10 % MOD) # 値 << 32 + 長さ def fmm(m1, m2): v1 = m1 >> 32 l1 = m1 & mask32 v2 = m2 >> 32 l2 = m2 & mask32 v = (v1 * pw[l2] + v2) % MOD l = l1 + l2 return (v << 32) + l inv9 = pow(9, MOD - 2, MOD) def fmo(m, o): if o == -1: return m else: l = m & mask32 v = o * (pw[l] - 1) * inv9 % MOD return (v << 32) + l def foo(o1, o2): if o2 == -1: return o1 else: return o2 lst = LazySegmentTree([(1 << 32) + 1] * N, 0, -1, fmm, fmo, foo) LRDs = map(int, read().split()) for L, R, D in zip(LRDs, LRDs, LRDs): lst.effect(L - 1, R, D) print(lst.all_folded() >> 32)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, m, x, y; vector<int> d; pair<int, int> a[N], b[N]; int main() { scanf("%d%d", &n, &m); d.push_back(1); for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { d.push_back(i); d.push_back(n / i); } } for (int i = 1; i <= m; ++i) { scanf("%d%d", &x, &y); x--; y--; if (x > y) swap(x, y); a[i] = make_pair(x, y); } sort(a + 1, a + m + 1); bool f; for (auto i : d) { for (int j = 1; j <= m; ++j) { x = a[j].first + i; y = a[j].second + i; x %= n; y %= n; if (x > y) swap(x, y); b[j] = make_pair(x, y); } sort(b + 1, b + m + 1); f = 1; for (int j = 1; j <= m; ++j) { if (a[j] != b[j]) { f = 0; break; } } if (f) { break; } } if (f) puts("Yes"); else puts("No"); return 0; }
10
CPP
import os from collections import defaultdict, deque is_dev = 'vscode' in os.environ if is_dev: inF = open('in.txt', 'r') outF = open('out.txt', 'w') def ins(r): return list(map(int, r.split(' '))) def input_(): if is_dev: return inF.readline()[:-1] else: return input() def print_(data): if is_dev: outF.write(str(data)+'\n') else: print(data) for _ in range(int(input_())): input_() a = ins(input_()) b = ins(input_()) min_a = min(a) min_b = min(b) step = 0 for i in range(len(a)): a[i] -= min_a b[i] -= min_b step += max(a[i], b[i]) print_(step) if is_dev: outF.close() def compare_file(): print(open('out.txt', 'r').read() == open('outactual.txt', 'r').read()) compare_file()
8
PYTHON3
n = input() n=int(n) words =[] i = 0 while i < n : word=str(input()) l=len(word) if l > 10: word = str(word[0]) + str(l-2) + str(word[l-1]) words.append(word) i = i + 1 i = 0 while i < n : print (words[i]) i = i + 1
7
PYTHON3
import sys, math,os from io import BytesIO, IOBase from bisect import bisect_left as bl, bisect_right as br, insort #from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') sys.setrecursionlimit(100000) INF = float('inf') mod = int(1e9)+7 def main(): n,x=mdata() d=mdata() cnt1=0 cnt2=0 m=0 ind=2*n-1 i=2*n-1 d+=d while i>-1: if d[i]>=x-cnt1: cnt2+=(d[i]*d[i]+d[i])//2-((d[i]-x+cnt1)*(d[i]-x+cnt1+1))//2 m=max(cnt2,m) if ind!=i: cnt2-=(d[ind]*d[ind]+d[ind])//2+(d[i]*d[i]+d[i])//2-((d[i]-x+cnt1)*(d[i]-x+cnt1+1))//2 cnt1 -= d[ind] i+=1 else: cnt2-=(d[i]*d[i]+d[i])//2-((d[i]-x+cnt1)*(d[i]-x+cnt1+1))//2 if cnt2 < 0: break ind-=1 else: cnt1 += d[i] cnt2+=(d[i]*d[i]+d[i])//2 i-=1 out(m) if __name__ == '__main__': main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; for(int i=0,num[3];i<n;i++){ for(int j=0;j<3;j++)cin>>num[j]; num[0]--; num[1]--; int ans=num[0]/3*590+num[0]%3*195; if((num[0]+1)%3==0)ans+=num[1]*20; else ans+=num[1]/2*39+num[1]%2*20; cout<<196471-ans-num[2]<<endl; } }
0
CPP
t = int(input()) for p in range(t): n = int(input()) seq = input().split() resp = "NO" sobrou = seq[1:] sobrouInd = [i+1 for i in range(len(sobrou))] resp = [] for i in range(n): aux = [] auxInd = [] for j in range(len(sobrou)): if seq[i] != sobrou[j]: resp.append(str(i+1) + " " + str(sobrouInd[j]+1)) else: aux.append(sobrou[j]) auxInd.append(sobrouInd[j]) if len(aux) == 0: break sobrou = aux sobrouInd = auxInd if len(resp) == n - 1: print("YES") for e in resp: print(e) else: print("NO")
10
PYTHON3
a,b,n = list(map(int,input().split(' '))) s = 0 s = (n/2)*((2*a)+((n-1)*a)) if s > b: print(int(s-b)) else: print("0")
7
PYTHON3
#include <iostream> #include <algorithm> #include <vector> using namespace std; typedef struct E_TAG{ int type; int value; } M; int main(){ while(true){ int x,y,z; cin >> x >> y >> z; if(x==0&&y==0&&z==0) return 0; vector<vector<double> > DP(y+1,vector<double>(5001,0)); vector<M> V; vector<int> R; for(int i=0;i<x;i++){ int temp; cin >> temp; R.push_back(temp); } for(int i=0;i<y+1;i++){ V.push_back({0,0}); } for(int i=0;i<z;i++){ int n,e,a; cin >> n >> e >> a; V[n] = {e,a}; } DP[0][0] = 1.0; for(int i=0;i<V.size()-1;i++){ for(int j=0;j<5001;j++){ if(DP[i][j] == 0) continue; for(int z=0;z<R.size();z++){ int nex = min(y,i+R[z]); int now = j; if(V[nex].type == 1) nex += V[nex].value; else if(V[nex].type == 2) now += V[nex].value; else if(V[nex].type == 3) now -= V[nex].value; if(now < 0) now = 0; if(nex > y) nex = y; DP[nex][now] += (DP[i][j] / R.size()); } } } double out = 0; for(int i=0;i<5001;i++){ out += i * DP[y][i]; } cout << (int)out << endl; } }
0
CPP
n=int(input()) for i in range(n): word=input() length=len(word) if length>10: print(word[0]+str(length-2)+word[-1]) else: print(word)
7
PYTHON3