solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; long long int n, a[105], t, ans = -1; int main() { scanf("%lld", &n); for (long long int i = 0; i < n; ++i) scanf("%lld", &a[i]); scanf("%lld", &t); sort(a, a + n); for (long long int i = 0; i < n; ++i) { long long int j = i; while (a[i] + t >= a[j] && j < n) j++; ans = max(ans, j - i); } printf("%lld", ans); }
1,400
CPP
#include <bits/stdc++.h> int main() { int x, y, i, j, yac = 0; char lab[8][8]; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) scanf(" %c", &lab[i][j]); for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { if (lab[i][j] == 'B') ; else break; if (j == 7) yac++; } for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { if (lab[j][i] == 'B') ; else break; if (j == 7) yac++; } if (yac == 16) { yac = 8; } printf("%d\n", yac); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; struct trie_node { struct trie_node* left; struct trie_node* right; int cnt, minn; trie_node() { left = NULL; right = NULL; cnt = 0; minn = 10000000; } }; struct trie_node* curr; struct trie_node* head[100005]; int v[100004]; int gcd(int i, int j) { if (i % j == 0) return j; return gcd(j, i % j); } void inint(int u, int num) { struct trie_node* h1 = head[u]; h1->minn = min(h1->minn, num); for (int i = 17; i >= 0; i--) { if ((num & (1 << i))) { if (h1->right == NULL) { curr = new trie_node(); h1->right = curr; } h1 = h1->right; h1->minn = min(h1->minn, num); } else { if (h1->left == NULL) { curr = new trie_node(); h1->left = curr; } h1 = h1->left; h1->minn = min(h1->minn, num); } } } int dfs(int u, int num, int s) { struct trie_node* h1 = head[u]; int ans = 0; if (h1->minn + num > s) return -1; for (int i = 17; i >= 0; i--) { if ((num & (1 << i))) { if (h1->left != NULL && h1->left->minn + num <= s) { h1 = h1->left; } else { h1 = h1->right; ans += (1 << i); } } else { if (h1->right != NULL && h1->right->minn + num <= s) { h1 = h1->right; ans += (1 << i); } else { h1 = h1->left; } } } return ans; } int main() { for (int i = 1; i <= 100000; i++) head[i] = new trie_node(); int q; cin >> q; int mi = 10000000; while (q--) { int t; cin >> t; if (t == 1) { int x; cin >> x; if (v[x] == 1) continue; for (int i = 1; i * i <= x; i++) { if (x % i == 0) { int w = x / i; inint(i, x); if (w != i) inint(w, x); } } mi = min(mi, x); v[x] = 1; } else { int x, k, s; cin >> x >> k >> s; if (x % k != 0) { cout << "-1" << '\n'; continue; } int r = dfs(k, x, s); cout << r << '\n'; } } }
2,200
CPP
string = input() if(string[0].islower()): print(string[0].capitalize(), end = '') print(string[1:]) else: print(string)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n; int main() { cin >> n; int k = 2; long long p[1002]; p[0] = 1; p[1] = 2; while (1) { p[k] = p[k - 1] + p[k - 2]; if (p[k] > n) break; k++; } cout << k - 1 << endl; return 0; }
1,600
CPP
# -*- coding: utf-8 -*- """ Created on Tue May 12 07:51:43 2020 @author: Harshal """ s=input() if not s: print(s) elif len(s)==1: if s.isupper(): print(s.lower()) else: print(s.upper()) else: if s.upper()==s: s=s.lower() elif s[0].islower() and s[1:].isupper(): a=str(s[0]) s=a.upper()+s[1:].lower() print(s)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mxn = 1000006; int n, m; struct pnt { int val, x, y; bool operator<(pnt b) const { return val < b.val; } int co() { return x * m + y + 1; } }; vector<pnt> a; map<pair<int, int>, int> b; int mx[mxn]; int my[mxn]; map<pair<int, int>, int> ans; class DSU { int p[mxn + 1]; public: void clear(int n) { iota(p, p + n + 1, 0); } int root(int x) { return p[x] == x ? p[x] : p[x] = root(p[x]); } bool merge(int a, int b) { if ((a = root(a)) != (b = root(b))) p[a] = b; return a != b; } } Q; int tid[mxn]; void EXEC() { cin >> n >> m; a.reserve(n * m); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { int t; scanf("%d", &t); a.push_back({t, i, j}); b[{i, j}] = t; } Q.clear(n * m); sort(a.begin(), a.end()); for (int i = 0; i < a.size(); ++i) if (!i || a[i].val != a[i - 1].val) { int r = i; for (int j = i; j < a.size() && a[j].val == a[i].val; ++j) r = j; sort(a.begin() + i, a.begin() + r + 1, [](pnt a, pnt b) { return a.x < b.x; }); for (int j = i; j < r; ++j) if (a[j].x == a[j + 1].x) Q.merge(a[j].co(), a[j + 1].co()); sort(a.begin() + i, a.begin() + r + 1, [](pnt a, pnt b) { return a.y < b.y; }); for (int j = i; j < r; ++j) if (a[j].y == a[j + 1].y) Q.merge(a[j].co(), a[j + 1].co()); for (int j = i; j <= r; ++j) { tid[Q.root(a[j].co())] = max(tid[Q.root(a[j].co())], max(mx[a[j].x], my[a[j].y]) + 1); } for (int j = i; j <= r; ++j) { mx[a[j].x] = tid[Q.root(a[j].co())]; my[a[j].y] = tid[Q.root(a[j].co())]; } for (int j = i; j <= r; ++j) { ans[{a[j].x, a[j].y}] = tid[Q.root(a[j].co())]; } sort(a.begin() + i, a.begin() + r + 1); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) printf("%d ", ans[{i, j}]); printf("\n"); } } int main() { EXEC(); return 0; }
2,200
CPP
def read(): return int(input()) num = read() j=num-2 i =2 flag = False while i <= j: if i +j == num: if i%2==0 and j%2==0: print("YES") flag = True break i += 2 j -= 2 if not flag: print("NO")
800
PYTHON3
#include <bits/stdc++.h> int take() { int n; scanf("%d", &n); return n; } long long takes() { long long n; scanf("%lld", &n); return n; } int cas; using namespace std; int main() { int n = take(); int a[n + 1], b[n + 1]; long long f = 0, s = 0; for (int i = 1; i <= n; i++) f += take(); for (int i = 1; i <= n; i++) b[i] = take(); sort(b + 1, b + 1 + n); if (f <= (b[n] + b[n - 1])) printf("YES\n"); else printf("NO\n"); return 0; }
900
CPP
d=int(input()) n=int(input()) count=0 arr=[int(x) for x in input().split()] for i in range(n-1): if d>arr[i]: count+=d-arr[i] print(count)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k = 0; scanf("%d %d", &n, &m); for (int a = 0; a <= int(floor(sqrt(n))); a++) for (int b = 0; b <= int(floor(sqrt(m))); b++) if ((a * a + b == n) && (b * b + a == m)) k++; printf("%d\n", k); return 0; }
800
CPP
import sys import math import itertools as it import operator as op import fractions as fr # n,m = map(int,sys.stdin.readline().split()) n = int(sys.stdin.readline().strip()) T = map(int,sys.stdin.readline().split()) d = 0 for t in T: if t - d > 15: break else: d = t d = min(d+15,90) print(d)
800
PYTHON3
""" Author : co_devil Chirag Garg Institute : JIIT """ from __future__ import division, print_function from sys import stdin, stdout import itertools, os, sys, threading from collections import deque, Counter, OrderedDict, defaultdict import heapq from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd # from bisect import bisect_left,bisect_right # from decimal import *,threading from fractions import Fraction """from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip else: from builtins import str as __str__ str = lambda x=b'': x if type(x) is bytes else __str__(x).encode() BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._buffer = BytesIO() self._fd = file.fileno() self._writable = 'x' in file.mode or 'r' not in file.mode self.write = self._buffer.write if self._writable else None def read(self): return self._buffer.read() if self._buffer.tell() else os.read(self._fd, os.fstat(self._fd).st_size) def readline(self): while self.newlines == 0: b, ptr = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)), self._buffer.tell() self._buffer.seek(0, 2), self._buffer.write(b), self._buffer.seek(ptr) self.newlines += b.count(b'\n') + (not b) self.newlines -= 1 return self._buffer.readline() def flush(self): if self._writable: os.write(self._fd, self._buffer.getvalue()) self._buffer.truncate(0), self._buffer.seek(0) sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) input = lambda: sys.stdin.readline().rstrip(b'\r\n') def print(*args, **kwargs): sep, file = kwargs.pop('sep', b' '), kwargs.pop('file', sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop('end', b'\n')) if kwargs.pop('flush', False): file.flush() """ def ii(): return int(input()) def si(): return str(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) def fii(): return int(stdin.readline()) def fsi(): return str(stdin.readline()) def fmi(): return map(int, stdin.readline().split()) def fli(): return list(fmi()) abc = 'abcdefghijklmnopqrstuvwxyz' abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod = 1000000007 dx, dy = [-1, 1, 0, 0], [0, 0, 1, -1] def getKey(item): return item[0] def sort2(l): return sorted(l, key=getKey) def d2(n, m, num): return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo(x): return (x and (not (x & (x - 1)))) def decimalToBinary(n): return bin(n).replace("0b", "") def ntl(n): return [int(i) for i in str(n)] def powerMod(x, y, p): res = 1 x %= p while y > 0: if y & 1: res = (res * x) % p y = y >> 1 x = (x * x) % p return res def gcd(x, y): while y: x, y = y, x % y return x # For getting input from input.txt file # sys.stdin = open('input.txt', 'r') # Printing the Output to output.txt file # sys.stdout = open('output.txt', 'w') graph = defaultdict(list) visited = [0] * 1000000 col = [-1] * 1000000 def dfs(v, c): if visited[v]: if col[v] != c: print('-1') exit() return col[v] = c visited[v] = 1 for i in graph[v]: dfs(i, c ^ 1) def bfs(d, v): q = [] q.append(v) visited[v] = 1 while len(q) != 0: x = q[0] q.pop(0) for i in d[x]: if visited[i] != 1: visited[i] = 1 q.append(i) print(x) def make_graph(e): d = defaultdict(list) for i in range(e): x, y = mi() d[x].append(y) d[y].append(x) return d def gr2(n): d = defaultdict(list) for i in range(n): x, y = mi() d[x].append(y) return d def connected_components(graph): seen = set() def dfs(v): vs = set([v]) component = [] while vs: v = vs.pop() seen.add(v) vs |= set(graph[v]) - seen component.append(v) return component ans = [] for v in graph: if v not in seen: d = dfs(v) ans.append(d) return ans def primeFactors(n): s = set() while n % 2 == 0: s.add(2) n = n // 2 for i in range(3, int(sqrt(n)) + 1, 2): while n % i == 0: s.add(i) n = n // i if n > 2: s.add(n) return s def find_all(a_str, sub): start = 0 while True: start = a_str.find(sub, start) if start == -1: return yield start start += len(sub) def Prime(x): if x<2: return False for i in range(2,int(sqrt(x))+1): if x%i==0: return False return True if __name__ == '__main__': n,k=mi() l=li() b = [[] for i in range(2 * 10 ** 5 + 1)] for a in l: cnt = 0 while (a > 0): b[a].append(cnt) cnt += 1 a = a // 2 ans = 1000000000 for x in b: if len(x) >= k: x = sorted(x) ans = min(ans, sum(x[:k])) print(ans)
1,500
PYTHON3
import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): n=II() s=SI() aa=[] a=0 for c in s: if c=="(":a+=1 else:a-=1 aa.append(a) if aa[-1]!=0: print(-1) exit() pa=1 ans=0 for a in aa: if a<0:ans+=1 elif pa<0:ans+=1 pa=a print(ans) main()
1,300
PYTHON3
for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) cnt = 0 i = 0 while not A[i]: cnt += 1 i += 1 i = n - 1 while not A[i]: cnt += 1 i -= 1 print(A.count(0) - cnt)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long int> dijkstra( long long int from, vector<vector<pair<long long int, long long int>>>& graph) { vector<long long int> dists(graph.size(), LONG_LONG_MAX); priority_queue<pair<long long int, long long int>> next; next.push({from, 0}); while (!next.empty()) { pair<long long int, long long int> curr = next.top(); next.pop(); long long int node = curr.first; if (dists[node] == LONG_LONG_MAX) { dists[node] = curr.second; for (pair<long long int, long long int> child : graph[node]) { if (dists[child.first] == LONG_LONG_MAX) { next.push({child.first, dists[node] + child.second}); } } } } return dists; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m; cin >> n >> m; vector<vector<long long int>> blancas(n), blancas_rev(n); vector<vector<long long int>> negras(n), negras_rev(n); for (long long int i = 0; i < m; i++) { long long int from, to, blanca; cin >> from >> to >> blanca; from--; to--; if (blanca) { blancas[from].push_back(to); blancas_rev[to].push_back(from); } else { negras[from].push_back(to); negras_rev[to].push_back(from); } } vector<bool> ciudades_blancas(n, false); vector<long long int> dist(n, LONG_LONG_MAX); vector<long long int> dist_blanca(n, LONG_LONG_MAX); vector<long long int> dist_negra(n, LONG_LONG_MAX); dist[n - 1] = 0; dist_blanca[n - 1] = 0; dist_negra[n - 1] = 0; queue<long long int> next; next.push(n - 1); while (!next.empty()) { long long int curr = next.front(); next.pop(); for (long long int child : negras_rev[curr]) { if (dist[child] == LONG_LONG_MAX) { dist_negra[child] = min(dist_negra[child], dist[curr] + 1); ciudades_blancas[child] = true; if (dist_blanca[child] != LONG_LONG_MAX) { dist[child] = max(dist_blanca[child], dist_negra[child]); ciudades_blancas[child] = dist_blanca[child] > dist_negra[child]; next.push(child); } } } for (long long int child : blancas_rev[curr]) { if (dist[child] == LONG_LONG_MAX) { dist_blanca[child] = min(dist_blanca[child], dist[curr] + 1); ciudades_blancas[child] = false; if (dist_negra[child] != LONG_LONG_MAX) { dist[child] = max(dist_blanca[child], dist_negra[child]); ciudades_blancas[child] = dist_blanca[child] > dist_negra[child]; next.push(child); } } } } vector<vector<pair<long long int, long long int>>> graph(n); for (long long int i = 0; i < n; i++) { if (ciudades_blancas[i]) { for (long long int child : blancas[i]) { graph[i].push_back({child, 1}); } } else { for (long long int child : negras[i]) { graph[i].push_back({child, 1}); } } } vector<long long int> dists = dijkstra(0, graph); if (dist[0] == LONG_LONG_MAX) { cout << -1 << '\n'; } else { cout << dist[0] << '\n'; } for (long long int i = 0; i < n; i++) { cout << (int)ciudades_blancas[i]; } cout << '\n'; return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e5 + 10, MaxL = 64; long long n, k; long long a[MaxN]; set<int> s; bool tooBig(long long x, long long y) { return (((long double)x) * ((long double)y) > 2e18); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; bool allOne = true; for (int i = 0; i < n; i++) { cin >> a[i]; allOne &= (a[i] == 1); } if (allOne) { if (k == 1) cout << n; else cout << 0; return 0; } long long sol = 0; long long mul, sum, len, x, y; int j = 0; for (int i = 0; i < n; i++) { for (; j <= n && (int)s.size() < MaxL; j++) if (j == n || a[j] != 1) s.insert(j); s.insert(i); mul = 1; sum = 0; for (auto it = s.begin(); next(it) != s.end(); ++it) { x = a[*it]; if (tooBig(mul, x)) break; mul *= x; sum += x; len = (*(next(it))) - (*it) - 1; if (mul % k == 0) { y = mul / k - sum; if (0 <= y && y <= len) sol++; } sum += len; } s.erase(i); } cout << sol << "\n"; return 0; }
2,100
CPP
n = int(input()) arr = list(map(int, input().split())) mn = min(arr) mx = max(arr) mn_idx = 0 mx_idx = 0 for i in range(n-1, -1, -1): if arr[i] == mn: mn_idx = i break for i in range(n): if arr[i] == mx: mx_idx = i break mx_swap = mx_idx mn_swap = (n - 1) - mn_idx if mx_idx > mn_idx: mn_swap -= 1 print(mx_swap + mn_swap)
800
PYTHON3
n,b,a=map(int,input().split()) c=a s=[int(x) for x in input().split()] r=0 for i in s: if b==0 and a==0: break if i==1: if b>0 and a<c: a+=1 b-=1 else: a-=1 else: if a>0: a-=1 else: b-=1 r+=1 print(r)
1,500
PYTHON3
n=int(input()) a=list(map(int,input().split())) a.sort() #print(a) mark=[] for i in range(0,n): mark.append(0) b=[] for i in range(0,n): if (mark[i]==0): b.append(a[i]) mark[i]=1 last=a[i] for j in range(i+1,n): if (a[j]>last and mark[j]==0): b.append(a[j]) mark[j]=1; last=a[j] #print(b) res=0 for i in range(0,n-1): if (b[i]<b[i+1]): res+=1 print(res)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int first = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { first = first * 10 + ch - 48; } return first * f; } const int mxN = 1 << 12; const int mxM = 1 << 16; const int mxB = 1 << 8; int a[mxN + 3], ai[mxN + 3]; int n, m, siz, B; int ans[mxM + 3]; vector<pair<int, int> > way; int merge(int u, int v) { if (!u || !v) { return u + v; } way.push_back(make_pair(u, v)); return ++siz; } int f[mxB * 2 + 3][mxB + 3][mxB + 3]; struct Block { int len, al, ar; void solve(int u, int l, int r, vector<int> vec) { if (l == r) { f[u][1][1] = ai[vec[1]]; return; } int mid = (l + r) >> 1; vector<int> vecl(1), vecr(1); for (int i = 1; i < vec.size(); i++) { if (vec[i] <= mid) { vecl.push_back(vec[i]); } else { vecr.push_back(vec[i]); } } solve(u << 1, l, mid, vecl); solve(u << 1 | 1, mid + 1, r, vecr); int xl = 1, xr = 1; for (int i = 1; i < vec.size(); i++) { int yl = xl - 1, yr = xr - 1; for (int j = i; j < vec.size(); j++) { yl += (vec[j] <= mid), yr += (vec[j] > mid); f[u][i][j] = merge(f[u << 1][xl][yl], f[u << 1 | 1][xr][yr]); } xl += (vec[i] <= mid), xr += (vec[i] > mid); } } int id[mxN + 3], qwq[mxB + 3][mxB + 3]; void build() { vector<int> vec(1); for (int i = 1; i <= n; i++) { if (a[i] >= al && a[i] <= ar) vec.push_back(a[i]), id[i] = id[i - 1] + 1; else id[i] = id[i - 1]; } solve(1, al, ar, vec); for (int i = 1; i < vec.size(); i++) for (int j = 1; j < vec.size(); j++) qwq[i][j] = f[1][i][j]; } int query(int l, int r) { return qwq[id[l - 1] + 1][id[r]]; } } blo[18]; int main() { n = read(), m = read(), siz = n, B = min(n, mxB); for (int i = 1; i <= n; i++) a[i] = read(), ai[a[i]] = i; for (int i = 1; (i - 1) * B + 1 <= n; i++) { blo[i].al = (i - 1) * B + 1, blo[i].ar = i * B; blo[i].build(); } for (int q = 1; q <= m; q++) { int l = read(), r = read(); ans[q] = 0; for (int i = 1; i <= (n - 1) / B + 1; i++) { ans[q] = merge(ans[q], blo[i].query(l, r)); } } printf("%d\n", siz); for (int i = 0; i < way.size(); i++) printf("%d %d\n", way[i].first, way[i].second); for (int i = 1; i <= m; i++) printf("%d ", ans[i]); puts(""); return 0; }
3,300
CPP
#Anuneet Anand T = int(input()) while T: n = int(input()) print((n+1)//2) T = T - 1
800
PYTHON3
n = int(input()) buttons = list(map(int, input().split())) if n == 1: if buttons[0] == 1: print("YES") else: print("NO") else: if buttons.count(0) == 1: print("YES") else: print("NO")
1,000
PYTHON3
#include <iostream> #include <cmath> #include <algorithm> #include <cstring> #include <string> typedef long long ll; using namespace std; #define endl '\n' #define mo 998244353 #define nps cout << fixed << setprecision(10) #define cincout ios::sync_with_stdio(false), cin.tie(0) using namespace std; long long sum[1505], dp[505][1505], ans, n; char s[505]; ll a[505]; ll b[505]; ll w2[505]; int main(){ cin >> n; w2[0] = 1; for(int i = 1; i <= n; i++){ cin >> s[i]; w2[i] = w2[i - 1] * 2 % mo; if(s[i] == '+') cin >> a[i]; } for(int i = 1; i <= n; i++){ if(s[i] == '+'){ ll num = 0; ll na = 0; for(int j = 1; j <= n; j++){ if(s[j] == '+' && a[i] > a[j]) b[j] = 1; else if(s[j] == '+' && a[i] < a[j]) { b[j] = 0; num++; } else if(s[j] == '+' && a[i] == a[j]){ //跟a[i]相等的数,如果i<=j,1(在前面),如果i>j,0(在后面) b[j] = (i <= j); num += (i > j); //如果在后面,就++ } else if(s[j] == '-') b[j] = -1; //删除操作记录为-1 } sum[1000] = 1; for(int j = 1; j <= n; j++) if(b[j]){ if(j != i){ int now = (j >= i ? 0 : -500); for(int k = now; k <= 500; k++){ dp[j][k + 1000] = sum[k - b[j] + 1000]; } for(int k = now; k <= 500; k++){ sum[k + 1000] = (sum[k + 1000] + dp[j][k + 1000]) % mo; } } if(j < i){ for(int k = -500; k < 0; k++){ sum[1000] += sum[k + 1000]; sum[1000] %= mo; sum[k + 1000] = 0; } } } for(int j = 0; j <= 500; j++) { na += sum[j + 1000]; na %= mo; } ans += (na * a[i]) % mo * w2[num] % mo; ans %= mo; memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); } } cout << ans % mo << endl; return 0; }
2,200
CPP
def sol(lis,n): k = 0 while k<n and lis[k]==1: k+=1 x = (k==n)^(k%2) if x: print('Second') return print('First') k = int(input()) for i in range(k): n = int(input()) lis = list(map(int,input().split(' '))) sol(lis,n)
1,100
PYTHON3
p=3.14159265959 a=input() l=a.split(" ") l1=[] for i in l: l1.append(int(i)) d=l1[0] h=l1[1] v=l1[2] e=l1[3] increase=p*(d**2)/4*e if(increase>v): print("NO") else: t=(p*(d**2)*h/4)/(v-(p*(d**2)*e/4)) print("YES") print(t)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; void swapp(int &a, int &b); double fabss(double a); int maxx(int a, int b); int minn(int a, int b); int Del_bit_1(int n); int lowbit(int n); int abss(int a); const double E = 2.718281828; const double PI = acos(-1.0); const double ESP = 1e-6; const int inf = 0x7fffffff; const int N = 1e6 + 6; const long long mod = 1e9 + 7; long long qpow(long long a, long long b, long long mod) { long long ans; a %= mod; ans = 1; while (b != 0) { if (b & 1) ans = (ans * a) % mod; b /= 2; a = (a * a) % mod; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long n; cin >> n; long long ans = (qpow(27, n, mod) - qpow(7, n, mod) + mod) % mod; cout << ans << endl; return 0; } int maxx(int a, int b) { return a > b ? a : b; } void swapp(int &a, int &b) { a ^= b; b ^= a; a ^= b; } int lowbit(int n) { return n & -n; } int Del_bit_1(int n) { return n & n - 1; } int abss(int a) { return a > 0 ? a : -a; } double fabss(double a) { return a > 0 ? a : -a; } int minn(int a, int b) { return a < b ? a : b; }
1,500
CPP
import bisect from itertools import accumulate n, m = map(int,input().split()) a = list(map(int,input().strip().split())) b = list(map(int,input().strip().split())) ac_a = [0] + list(accumulate(a)) for i in b: xx = bisect.bisect_left(ac_a,i) - 1 print(xx + 1, i - ac_a[xx])
1,000
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; int main() { int T, i, j, k, ca = 0, m, n; int a[6]; for (i = 0; i < 6; i++) scanf("%d", &a[i]); int d = a[0] + a[1] + a[2]; int ans = d * d; ans -= a[0] * a[0] + a[2] * a[2] + a[4] * a[4]; printf("%d\n", ans); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int a[150], b[200], n, x, m, xp, k; queue<int> Q; int main() { cin >> n >> m >> xp; for (int i = 0; i <= n; i++) a[i] = -1; for (int i = 2; i <= m; i++) { cin >> x; if (x > xp) { if (a[xp] == -1) { a[xp] = x - xp; b[x - xp]++; } else if (a[xp] != x - xp) { cout << "-1"; return 0; } } else { if (a[xp] == -1) { a[xp] = n - xp + x; b[n + x - xp]++; } else if (a[xp] != n - xp + x) { cout << "-1"; return 0; } } xp = x; } for (int i = 1; i <= n; i++) { if (!b[i]) Q.push(i); if (a[i] == -1) k++; } if (k != Q.size()) { cout << "-1"; return 0; } for (int i = 1; i <= n; i++) { if (a[i] == -1 && !Q.empty()) { cout << Q.front() << " "; Q.pop(); } else cout << a[i] << " "; } }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, ans = 0; cin >> n; for (int i = 1; i < n; i++) { ans += (i * 2 + 1) % (n + 1); } cout << ans % n; }
1,000
CPP
#include <bits/stdc++.h> const int maxn = 200010, maxblo = 650, maxbig = 650; int n, m; unsigned long long A, B, C; std::vector<int> to[maxn]; int blo, big[maxn], list[maxn], bcnt; bool G[maxbig][maxbig], N[maxn]; int cir[maxn][3]; unsigned long long C2(unsigned long long x) { return x * (x - 1) / 2; } unsigned long long sums(unsigned long long l, unsigned long long r) { return (r - l) * (l + r - 1) / 2; } int main() { scanf("%d%d%llu%llu%llu", &n, &m, &A, &B, &C); for (int i = 0, u, v; i < m; i++) { scanf("%d%d", &u, &v); to[u].push_back(v); to[v].push_back(u); } for (int i = 0; i < n; i++) std::sort(to[i].begin(), to[i].end()); unsigned long long ans = 0; for (int i = 0; i < n; i++) { ans += A * i * C2(n - i - 1); ans += B * i * i * (n - i - 1); ans += C * i * C2(i); unsigned long long cl = 0, cr = 0, sl = 0, sr = 0; for (int e = 0, es = to[i].size(), j; e < es ? j = to[i][e], 1 : 0; e++) { if (j > i) { ans -= C * sums(j + 1, n) + (A * i + B * j) * (n - j - 1); ans -= B * sums(i + 1, j) + (A * i + C * j) * (j - i - 1); ans -= A * sums(0, i) + (B * i + C * j) * i; ans += A * i * cr + B * sr + C * j * cr; cr++; sr += j; } else { ans += A * sl + B * j * cl + C * i * cl; cl++; sl += j; } } ans += A * sl * cr + B * i * cl * cr + C * sr * cl; } while (blo * blo <= m * 2) blo++; for (int i = 0; i < n; i++) if (to[i].size() >= blo) list[bcnt] = i, big[i] = ++bcnt; for (int i = 0; i < n; i++) { for (int e = 0, es = to[i].size(), j; e < es ? j = to[i][e], 1 : 0; e++) N[j] = 1; if (big[i]) { for (int e = 0, es = to[i].size(), j; e < es ? j = to[i][e], 1 : 0; e++) { if (!big[j]) { for (int f = 0, fs = to[j].size(), k; f < fs ? k = to[j][f], 1 : 0; f++) if (N[k]) { if (k > i) { if (k > j) break; if (!big[k]) cir[i][0]++, cir[k][1]++, cir[j][2]++; } else if (k > j) { if (big[k]) cir[j][0]++, cir[k][1]++, cir[i][2]++; } else if (i < j) cir[k][0]++, cir[i][1]++, cir[j][2]++; else cir[k][0]++, cir[j][1]++, cir[i][2]++; } } else G[big[i] - 1][big[j] - 1] = 1; } } else for (int e = 0, es = to[i].size(), j; e < es ? j = to[i][e], 1 : 0; e++) { if (j > i) break; if (!big[j]) for (int f = 0, fs = to[j].size(), k; f < fs ? k = to[j][f], 1 : 0; f++) { if (k > j) break; if (N[k] && !big[k]) cir[k][0]++, cir[j][1]++, cir[i][2]++; } } for (int e = 0, es = to[i].size(), j; e < es ? j = to[i][e], 1 : 0; e++) N[j] = 0; } for (int i = 0; i < bcnt; i++) for (int j = i + 1; j < bcnt; j++) if (G[i][j]) for (int k = j + 1; k < bcnt; k++) if (G[i][k] && G[j][k]) cir[list[i]][0]++, cir[list[j]][1]++, cir[list[k]][2]++; for (int i = 0; i < n; i++) ans -= (A * cir[i][0] + B * cir[i][1] + C * cir[i][2]) * i; printf("%llu\n", ans); }
2,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, X, Y; int n, m, sum; cin >> n >> m; int A[n]; for (i = 0; i < n; i++) cin >> A[i]; bool AA[n][n]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) AA[i][j] = false; for (i = 0; i < m; i++) { cin >> X >> Y; AA[X - 1][Y - 1] = true; AA[Y - 1][X - 1] = true; } sum = -10000; for (i = 0; i < n; i++) for (j = 0; j < n; j++) for (k = 0; k < n; k++) if (AA[i][j] && AA[j][k] && AA[i][k]) if (sum < 0 || A[i] + A[j] + A[k] < sum) sum = A[i] + A[j] + A[k]; if (sum < 0) cout << -1; else cout << sum; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int _max(int x, int y) { return x > y ? x : y; } int _min(int x, int y) { return x < y ? x : y; } long long read() { long long s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * f; } pair<long long, long long> f[10][20][11]; long long tp[20]; pair<long long, long long> dfs(int mx, int k, long long x) { int o = (x / 10LL == tp[k] - 1) ? x % 10LL : 10; if (k == 1) { if (x >= mx) return make_pair(0, 1); else return make_pair(x, 0); } if (f[mx][k][o].second) return f[mx][k][o]; long long ans = 0, g = x % tp[k], u = x / tp[k]; while (u >= 0) { pair<long long, long long> now = dfs(_max(mx, u), k - 1, g); ans += now.second; g = now.first; if (u) { g = g + tp[k] - _max(mx, u); ans++; } --u; } f[mx][k][o] = make_pair(g, ans); return f[mx][k][o]; } int main() { long long n = read(); if (n == 1000000000000000000LL) { puts("113502443819651915"); return 0; } if (n == 0) { puts("0"); return 0; } tp[1] = 1LL; for (int i = 2; i <= 18; i++) tp[i] = tp[i - 1] * 10LL; tp[19] = 9000000000000000000LL; int kk = 0; long long u = n; while (u) ++kk, u /= 10LL; pair<long long, long long> ans = dfs(0, kk, n); printf("%lld\n", ans.second); return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const int n = 202; set<int> small, big, fire, li; int64_t s1 = 0, s2 = 0; void add(int d) { s1 += d; if (big.empty() || d < *big.begin()) small.insert(d); else { big.insert(d); s2 += d; } } void take(int d) { s1 -= d; if (small.count(d)) small.erase(d); else { big.erase(d); s2 -= d; } } int64_t solve() { while (big.size() > li.size()) { int a = *big.begin(); s2 -= a; small.insert(a); big.erase(a); } while (big.size() < li.size()) { int a = *small.rbegin(); small.erase(a); big.insert(a); s2 += a; } int64_t ans = s1 + s2; if (li.size() > 0 && (fire.empty() || *fire.rbegin() < *li.begin())) { ans -= *li.begin(); if (!fire.empty()) ans += *fire.rbegin(); } return ans; } int main() { startTime = clock(); int n; cin >> n; while (n--) { int t, d; cin >> t >> d; if (d > 0) { if (t == 0) fire.insert(d); else li.insert(d); add(d); } else { d *= -1; if (t == 0) fire.erase(d); else li.erase(d); take(d); } cout << solve() << '\n'; } }
2,200
CPP
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC optimize("-fpeephole2") #pragma GCC optimize("-ffast-math") #pragma GCC optimize("-fsched-spec") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-falign-jumps") #pragma GCC optimize("-falign-loops") #pragma GCC optimize("-falign-labels") #pragma GCC optimize("-fdevirtualize") #pragma GCC optimize("-fcaller-saves") #pragma GCC optimize("-fcrossjumping") #pragma GCC optimize("-fthread-jumps") #pragma GCC optimize("-funroll-loops") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-ftree-tail-merge") #pragma GCC optimize("-fschedule-insns2") #pragma GCC optimize("-fstrict-aliasing") #pragma GCC optimize("-fstrict-overflow") #pragma GCC optimize("-falign-functions") #pragma GCC optimize("-fcse-skip-blocks") #pragma GCC optimize("-fcse-follow-jumps") #pragma GCC optimize("-fsched-interblock") #pragma GCC optimize("-fpartial-inlining") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("-freorder-functions") #pragma GCC optimize("-findirect-inlining") #pragma GCC optimize("-frerun-cse-after-loop") #pragma GCC optimize("inline-small-functions") #pragma GCC optimize("-finline-small-functions") #pragma GCC optimize("-ftree-switch-conversion") #pragma GCC optimize("-foptimize-sibling-calls") #pragma GCC optimize("-fexpensive-optimizations") #pragma GCC optimize("-funsafe-loop-optimizations") #pragma GCC optimize("inline-functions-called-once") #pragma GCC optimize("-fdelete-null-pointer-checks") const unsigned int N = 3e5 + 10; using namespace std; namespace IO { static char buf[1 << 20], *fs, *ft; inline char gc() { if (fs == ft) { ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin); if (fs == ft) return EOF; } return *fs++; } inline unsigned int read() { unsigned int x = 0, f = 1; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -f; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } } // namespace IO using IO ::read; unsigned int n, m, newn; unsigned int a[N], b[N]; unsigned int rt[N], sum[N * 20], lc[N * 20], rc[N * 20]; unsigned int cnt; inline unsigned int insert(unsigned int p, unsigned int l, unsigned int r, unsigned int val) { unsigned int x = ++cnt; lc[x] = lc[p]; rc[x] = rc[p]; if (l == r) { sum[x] = sum[p]; sum[x]++; return x; } unsigned int mid = (l + r) >> 1; if (val <= mid) lc[x] = insert(lc[p], l, mid, val); if (val > mid) rc[x] = insert(rc[p], mid + 1, r, val); sum[x] = sum[lc[x]] + sum[rc[x]]; return x; } inline unsigned int query(unsigned int x, unsigned int y, unsigned int l, unsigned int r, unsigned int k) { if (l == r) return l; unsigned int mid = (l + r) >> 1, lvalue = sum[lc[x]] - sum[lc[y]], rvalue = sum[rc[x]] - sum[rc[y]]; unsigned int ans = newn + 1; if (lvalue > k) ans = min(ans, query(lc[x], lc[y], l, mid, k)); if (rvalue > k) ans = min(ans, query(rc[x], rc[y], mid + 1, r, k)); return ans; } signed main() { n = read(), m = read(); for (register unsigned int i = 1; i <= n; ++i) b[i] = a[i] = read(); sort(b + 1, b + 1 + n); newn = unique(b + 1, b + 1 + n) - b - 1; for (register unsigned int i = 1; i <= n; ++i) { a[i] = lower_bound(b + 1, b + 1 + newn, a[i]) - b; rt[i] = insert(rt[i - 1], 1, newn, a[i]); } for (register unsigned int i = 1; i <= m; ++i) { unsigned int l, r, k, ra; l = read(), r = read(), k = read(); ra = (r - l + 1) / k; unsigned int ans = query(rt[r], rt[l - 1], 1, newn, ra); if (ans != newn + 1) printf("%lld\n", b[ans]); else puts("-1"); } return 0; }
2,500
CPP
import sys import collections from collections import Counter import itertools import math import timeit #input = sys.stdin.readline ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def flin(d, x, default = -1): left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return (default, default) else: return (left, right) def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) n, m = mi() d = li() sd = sorted(d) t = 0 res = [] for i in range(m): res.append(sd[i] + t) t = res[-1] pref = [sd[i] for i in range(m)] c = 0 for i in range(m, n): res.append(sd[i] + res[-1] + pref[i%m]) pref[i % m] += sd[i] prr(res, sep = ' ')
1,500
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) if n%4==0: print((n-n//4)*'9'+(n//4)*'8') else: print((n-n//4-1)*'9'+(n//4+1)*'8')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int x[50], y[50]; void print_term(int x, int i) { cout << "(" << x / 2 << "*((abs((t-" << i - 1 << "))+abs((t-(" << i << "+1)" << ")))-(abs((t-" << i << "))+abs((t-" << i << ")))))"; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { int r; cin >> x[i] >> y[i] >> r; } for (int i = 1; i < n; ++i) { cout << '('; } print_term(x[0], 1); for (int i = 1; i < n; ++i) { cout << '+'; print_term(x[i], i + 1); cout << ')'; } cout << endl; for (int i = 1; i < n; ++i) { cout << '('; } print_term(y[0], 1); for (int i = 1; i < n; ++i) { cout << '+'; print_term(y[i], i + 1); cout << ')'; } return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; bool leap(int x) { return (x % 400 == 0 || (x % 4 == 0 && x % 100)); } int main() { int n; cin >> n; int day = 0; for (int i = n + 1; 1; i++) { if (!leap(i)) day = (day + 365) % 7; else day = (day + 366) % 7; if (day == 0 && leap(i) == leap(n)) return cout << i << endl, 0; } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; const int md = 1e9 + 7; ll pw(ll a, ll b) { ll r = 1, base = a; while (b != 0) { if (b & 1) r = (r * base) % md; base = base * base % md; b >>= 1; } return r; } int n, m; vector<int> a, was; vector<vector<int> > g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; a.resize(n); g.resize(n); was.resize(n); for (int i = 0; i < n; i++) cin >> a[i], a[i]--; for (int i = 0; i < m; i++) { int w1, w2; cin >> w1 >> w2; w1--; w2--; g[w1].push_back(w2); } reverse(a.begin(), a.end()); int ans = 0; for (int i = 0; i < n; i++) was[i] = 0; was[a[0]] = 1; int cnt = 1; for (int i = 1; i < n; i++) { int cnt2 = 0; for (int to : g[a[i]]) { if (was[to]) cnt2++; } if (cnt == cnt2) { ans++; } else { was[a[i]] = 1; cnt++; } } cout << ans; }
1,800
CPP
from sys import stdin """ n=int(stdin.readline().strip()) n,m=map(int,stdin.readline().strip().split()) s=list(map(int,stdin.readline().strip().split())) """ from math import gcd def gc(x): x1=0 for i in str(x): x1+=int(i) return gcd(x,x1) p2=[pow(2,i) for i in range(22)] T=int(stdin.readline().strip()) from math import log2 cnt=[0 for i in range(22)] for t1 in range(T): n,m=map(int,stdin.readline().strip().split()) s=list(map(int,stdin.readline().strip().split())) s.sort() for i in s: cnt[int(log2(i))]+=1 ans=0 while max(cnt)!=0: ans+=1 tot=m for i in range(21,-1,-1): while tot>=p2[i] and cnt[i]>0: tot-=p2[i] cnt[i]-=1 print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 15; pair<int, int> v[N]; int ar[N], type[N], r[N]; int ret[N]; void solve() { int n, m, i, j, u; cin >> n >> m; for (i = 1; i <= n; ++i) { cin >> ar[i]; } for (i = 0; i < m; ++i) { scanf("%d%d", &type[i], &r[i]); --type[i]; v[i] = make_pair(r[i], -i); } sort(v, v + m, greater<pair<int, int> >()); int id = -v[0].second; int bit = type[id], len = v[0].first, st = 1, ed = len; for (i = len + 1; i <= n; ++i) ret[i] = ar[i]; sort(ar + 1, ar + len + 1); int pos = len; for (i = 1; i < m; ++i) { if (-v[i].second < id) continue; int len2 = v[i].first; int cc = len - len2; if (bit == 0) { while (cc--) ret[pos--] = ar[ed--]; } else { while (cc--) ret[pos--] = ar[st++]; } id = -v[i].second; bit = type[id]; len = v[i].first; } if (bit == 0) { while (len--) ret[pos--] = ar[ed--]; } else while (len--) ret[pos--] = ar[st++]; for (i = 1; i <= n; ++i) printf("%d ", ret[i]); cout << endl; } int main() { solve(); }
1,700
CPP
s = input() l = [] for i in range(len(s)): if s[i] >= 'a' and s[i] <= 'z': l.append(s[i]) print(len(list(set(l))))
800
PYTHON3
I=input for _ in[0]*int(I()): I();a={1:1};i=s=1;r=-1,;m=3e5 for c in I(): s+=((c=='R')-(c=='L')<<16)+(c>'R')-(c<'L');j=a.get(s,-m) if i-j<m:m=i-j;r=j,i i+=1;a[s]=i print(*r)
1,500
PYTHON3
#include <bits/stdc++.h> int main() { int N; char a[20]; scanf("%d", &N); while (N--) { scanf("%s", a); int t = strlen(a) - 4; int s = 0; if (t == 1) { if (a[4] == '9') printf("1989\n"); else printf("199%c\n", a[4]); } else if (t == 2) { if (a[4] == '9' && a[5] == '9') printf("1999\n"); else printf("20%c%c\n", a[4], a[5]); } else if (t == 3) { s = (a[4] - '0') * 100 + (a[5] - '0') * 10 + (a[6] - '0'); if (s > 98) printf("2%c%c%c\n", a[4], a[5], a[6]); else printf("3%c%c%c\n", a[4], a[5], a[6]); } else if (t == 4) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 3099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } else if (t == 5) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 13099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } else if (t == 6) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 113099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } else if (t == 7) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 1113099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } else if (t == 8) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 11113099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } else if (t == 9) { int k = 1; for (int i = t + 3; i >= 4; i--) { s += (a[i] - '0') * k; k *= 10; } if (s >= 111113099) { for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } else { printf("1"); for (int i = 4; i < t + 4; i++) printf("%c", a[i]); printf("\n"); } } } }
2,000
CPP
def check(a,b): if len(a)!=len(b): return False i=0 j=len(b)-1 while i!=len(a) and j!=-1: if a[i]!=b[j]: return False i+=1 j-=1 return True a = str(input()) b = str(input()) if check(a,b): print("YES") else: print("NO")
800
PYTHON3
while True: try: def solution(x, y, n): M = 10**9+7 a = [ x - y, x, y, -x +y, -x, -y] ans = a[n%6]%M print(ans) def read(): x, y = map(int,input().split()) n = int(input()) solution(x, y, n) if __name__ == "__main__": read() except EOFError: break
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[200020], b[200020]; int p[200020], q[200020], r[200020]; bool cmp1(const int &x, const int &y) { return a[x] < a[y]; } bool cmp2(const int &x, const int &y) { return b[x] < b[y]; } int main(void) { int m; int i; scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d", &a[i]); p[i] = i; } for (i = 1; i <= m; i++) { scanf("%d", &b[i]); q[i] = i; } sort(p + 1, p + m + 1, cmp1); sort(q + 1, q + m + 1, cmp2); for (i = 1; i <= m; i++) r[q[i]] = i; for (i = 1; i <= m; i++) printf("%d ", a[p[m - r[i] + 1]]); return 0; }
1,300
CPP
t = int(input()) for i in range(t): n = int(input()) if n % 2 == 1: print(n // 2) else: print(n // 2 - 1)
800
PYTHON3
import queue n = int(input()) sR = list(map(int, input().split()[1:])) sM = list(map(int, input().split()[1:])) s = [sR, sM] UNK = -1 WIN = 2 LOSE = 3 A = [[UNK] * n for i in range(2)] CNT = [[0] * n for i in range(2)] V = [[False] * n for i in range(2)] # ricky turn 0 # morty turn 1 A[0][0] = LOSE A[1][0] = LOSE Q = queue.Queue() Q.put((0, 0)) Q.put((1, 0)) while not Q.empty(): turn, planet = Q.get() prev_turn = 1 - turn for diff in s[prev_turn]: prev_planet = (n + planet - diff) % n if prev_planet == 0: continue if A[turn][planet] == LOSE: A[prev_turn][prev_planet] = WIN elif A[turn][planet] == WIN: CNT[prev_turn][prev_planet] += 1 if CNT[prev_turn][prev_planet] == len(s[prev_turn]): A[prev_turn][prev_planet] = LOSE if A[prev_turn][prev_planet] != UNK and not V[prev_turn][prev_planet]: Q.put((prev_turn, prev_planet)) V[prev_turn][prev_planet] = True print(' '.join(["Win" if A[0][i] == WIN else "Lose" if A[0][i] == LOSE else "Loop" for i in range(1, n)])) print(' '.join(["Win" if A[1][i] == WIN else "Lose" if A[1][i] == LOSE else "Loop" for i in range(1, n)]))
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; char a[n][n]; int l, r, ans = 0, maxi = 0, x = 0, y = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans = 0; l = 0; while (l < k && i - l >= 0 && a[i - l][j] == '.') l++; r = 0; while (r < k && r + i < n && a[i + r][j] == '.') r++; ans += max(0, r + l - k); if (k != 1) { l = 0; while (l < k && j - l >= 0 && a[i][j - l] == '.') l++; r = 0; while (r < k && j + r < n && a[i][r + j] == '.') r++; ans += max(0, r + l - k); } if (ans > maxi) { maxi = ans; x = i; y = j; } } } cout << x + 1 << " " << y + 1; }
1,300
CPP
n,m = map(int,input().split()) a = input() b=[0]*26 for i in a: for j in range(65,91): if ord(i) == j: b[j-65]+=1 p = min(b[:m]) print(p*m)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; pair<int, int> cal(pair<int, int> p, pair<int, int> p1, pair<int, int> p2) { int x = p1.first + p2.first - p.first; int y = p1.second + p2.second - p.second; return pair<int, int>(x, y); } int main() { int x, y, n = 3; vector<pair<int, int> > points; while (n--) { cin >> x >> y; points.push_back(pair<int, int>(x, y)); } cout << 3 << endl; pair<int, int> tmp = cal(points[0], points[1], points[2]); cout << tmp.first << " " << tmp.second << endl; tmp = cal(points[1], points[0], points[2]); cout << tmp.first << " " << tmp.second << endl; tmp = cal(points[2], points[1], points[0]); cout << tmp.first << " " << tmp.second << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int MOD = 1e9 + 7; bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int main() { long long n; cin >> n; long long res; if (isPrime(n)) { res = 1; } else if (n % 2 == 0 || isPrime(n - 2)) { res = 2; } else { res = 3; } cout << res; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a[5], i, j; for (i = 0; i < 4; i++) { cin >> a[i]; } sort(a, a + 4); if (a[0] + a[1] > a[2] || a[2] + a[1] > a[3]) cout << "TRIANGLE" << endl; else if (a[0] + a[1] == a[2] || a[2] + a[1] == a[3]) cout << "SEGMENT" << endl; else cout << "IMPOSSIBLE" << endl; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; string a; cin >> t; while (t--) { cin >> a; int s1 = 0, s2 = 0; for (int i = 0; i < a.length(); i++) { if (a[i] == '0') s1++; else s2++; } s1 = min(s1, s2); if (s1 % 2 == 0) cout << "NET" << endl; else cout << "DA" << endl; } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; int a[MAXN]; int b[MAXN]; int c[MAXN]; int n, m, s; struct node { int x, y; int index; friend bool operator<(node a, node b) { return a.y > b.y; } }; priority_queue<node> q; template <class T> inline bool scan_d(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } node nn[MAXN]; bool cmp(node a, node b) { return a.x > b.x; } int AA[MAXN]; bool cmp11(int t1, int t2) { return a[t1] > a[t2]; } int BB[MAXN]; bool check(int pp) { while (!q.empty()) q.pop(); int i = 0, j = 0; long long ret = 0; while (i < m) { while (j < n && nn[j].x >= a[i]) { q.push(nn[j]); j++; } if (q.empty()) return false; node tmp = q.top(); q.pop(); ret += tmp.y; if (ret > s) return false; int ccc = pp; while (ccc && i < m) { ccc--; BB[AA[i]] = tmp.index; i++; } } return true; } int main() { while (scanf("%d%d%d", &n, &m, &s) == 3) { for (int i = 0; i < m; i++) { scan_d(a[i]); } for (int i = 0; i < n; i++) scan_d(b[i]); for (int i = 0; i < n; i++) scan_d(c[i]); for (int i = 0; i < m; i++) AA[i] = i; sort(AA, AA + m, cmp11); sort(a, a + m); reverse(a, a + m); for (int i = 0; i < n; i++) { nn[i].x = b[i]; nn[i].y = c[i]; nn[i].index = i + 1; } sort(nn, nn + n, cmp); if (check(m) == false) { printf("NO\n"); continue; } int l = 1, r = m; int ans; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } check(ans); printf("YES\n"); for (int i = 0; i < m; i++) { printf("%d", BB[i]); if (i < m - 1) printf(" "); else printf("\n"); } } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % 1000000007; y = y / 2, x = (x * x) % 1000000007; } return res % 1000000007; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long x; cin >> x; string s; cin >> s; vector<char> arr; for (auto v : s) arr.emplace_back(v); long long i; long long curr = s.length(); for (i = 1; i <= x; i++) { if (arr.size() <= x + 5) { long long pp = arr.size(); for (long long j = 0; j < (arr[i - 1] - '0') - 1; j++) { for (long long k = i; k < pp; k++) { arr.emplace_back(arr[k]); if (arr.size() >= x + 2) break; } } } curr = i + (curr - i + 1000000007) * (arr[i - 1] - '0') % 1000000007; curr %= 1000000007; } cout << curr << "\n"; } }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a, b, c, d, e, s = 0; a = n / 100; s = s + a; n = n - (a * 100); a = n / 20; s = s + a; n = n - (a * 20); a = n / 10; s = s + a; n = n - (a * 10); a = n / 5; s = s + a; n = n - (a * 5); a = n / 1; s = s + a; n = n - (a * 1); cout << s; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int a[150][150]; int t = 1, n, m, i, j; int main() { scanf("%d%d", &n, &m); if (n >= 5) for (i = 0; i < m; i++) { for (j = 0; j < (n + 1) / 2; j++) { a[j][i] = t; a[(n + 1) / 2 + j][i] = t + 1; t = t + 2; } if (n % 2 == 1) t--; } else if (m >= 5) for (i = 0; i < n; i++) { for (j = 0; j < (m + 1) / 2; j++) { a[i][j] = t; a[i][(m + 1) / 2 + j] = t + 1; t = t + 2; } if (m % 2 == 1) t--; } else if (m == 1 && n == 1) { printf("1"); return 0; } else if (m == 3 && n == 2) { printf("3 6 2\n5 1 4"); return 0; } else if (m == 2 && n == 3) { printf("3 5\n6 1\n2 4"); return 0; } else if (m == 3 && n == 3) { printf("2 5 7\n4 1 3\n8 6 9"); return 0; } else if (m == 4 && n == 1) { printf("3 1 4 2"); return 0; } else if (m == 1 && n == 4) { printf("3\n1\n4\n2"); return 0; } else if ((n == 1 && m == 2) || (n == 1 && m == 3) || (n == 3 && m == 1) || (n == 2 && m == 1) || (n == 2 && m == 2)) { printf("-1"); return 0; } else if (n == 2 && m == 4) { printf("1 5 2 7\n3 8 6 4"); return 0; } else if (n == 4 && m == 2) { printf("1 3\n5 8\n2 6\n7 4"); return 0; } else if (n == 3 && m == 4) { printf("3 1 4 2\n7 5 8 6\n11 9 12 10"); return 0; } else if (n == 4 && m == 3) { printf("3 7 11\n1 5 9\n4 8 12\n2 6 10"); return 0; } else if (n == 4 && m == 4) { printf("3 1 4 2\n7 5 8 6\n11 9 12 10\n15 13 16 14"); return 0; } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) printf("%d ", a[i][j]); printf("\n"); } return 0; }
1,800
CPP
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split(' '))) i = 0 arr.sort() if len(arr) == 1: print('YES') else: m = arr.copy() while len(arr) > 1: if abs(arr[i]-arr[i+1]) > 1: break elif arr[i] == arr[i+1]: arr.pop(i) else: arr.pop(i) i += 0 if len(arr) == 1: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string str; cin >> str; int k = 0; for (int i = 0; i < str.length() - 1; i++) { if (str[i] == 'S') if (str[i + 1] == 'F') k++; if (str[i] == 'F') if (str[i + 1] == 'S') k--; } if (k < 1) cout << "NO"; else cout << "YES"; }
800
CPP
from sys import stdin, stdout from collections import Counter import math def rsingle_int(): return int(stdin.readline().rstrip()) def rmult_int(): return [ int(x) for x in stdin.readline().rstrip().split() ] def rmult_str(): return stdin.readline().rstrip().split() def r_str(): return stdin.readline().rstrip() def rsingle_char(): return stdin.read(1) def main(): t = rsingle_int() for i in range(t): s, intel, exp = rmult_int() if exp <= (intel - s): print(0) else: if exp == 0: print(1) else: if s <= intel: print(math.ceil((s + exp - intel)/2)) else: if (s - intel) == 1: print('{}'.format(exp//2 + 1)) elif (s - intel > exp): print('{}'.format(exp + 1)) else: res = exp//2 + 1 if exp % 2 == 0: res += math.ceil((s - intel) / 2) - 1 print('{}'.format(res)) else: res += (s - intel) // 2 print('{}'.format(res)) main()
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string x; cin >> x; int cnt1 = 0; for (int i = 0; i < x.size(); i++) { if (x[i] == '1') { cnt1++; } } int flag = 0; for (int i = 0; i < x.size(); i++) { if (x[i] == '0') { cout << 0; } if (x[i] == '2') { if (flag == 0) { for (int i = 1; i <= cnt1; i++) { cout << 1; } flag = 1; } cout << 2; } } if (flag == 0) { for (int i = 1; i <= cnt1; i++) { cout << 1; } } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> string toString(T x) { if (x == 0) return "0"; bool negative = x < 0; string res; while (x) { res.push_back('0' + x % 10); x /= 10; } if (negative) res.push_back('-'); reverse(res.begin(), res.end()); return res; } template <typename T> T gcd(T a, T b) { return a == 0 ? b : gcd(b % a, a); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } void ioFromFile(string inputFileName = "input.txt", string outputFileName = "output.txt") { ifstream* fin = new ifstream(inputFileName); ofstream* fout = new ofstream(outputFileName); cin.rdbuf(fin->rdbuf()); cout.rdbuf(fout->rdbuf()); } int read() { int x; scanf("%d", &x); return x; } void write(int x) { printf("%d ", x); } const long long MOD = 1000000007; long long d[5000]; long long a[5001]; int main() { int dur = read(); int start = read(); int playSpeed = read(); int downSpeed = playSpeed - 1; dur *= playSpeed; start *= playSpeed; int downAt = start; int playAt = 0; ; int res = 1; while (true) { playAt += playSpeed; downAt += downSpeed; if (playAt == downAt) { if (playAt == dur) break; playAt = 0; res++; } if (playAt >= dur) break; } write(res); return 0; }
1,500
CPP
n = int(input()) a = list(map(int, input().split())) if n==1: print('1 1') print('0') print('1 1') print('0') print('1 1') print(-a[0]) else: print('1', n - 1) b = [] for i in range(n - 1): x = a[i] % n y = x * (n - 1) b.append(y) a[i] = -(y + a[i]) print(*b) print(n, n) print(-a[n - 1]) a[n - 1] = 0 print('1', n) print(*a)
1,600
PYTHON3
k,n,w=map(int,input().split()) #k=cost of 1st banana #n=number of dollars he has #w=no.of bananas cost=0 w=w*(w+1)/2; cost=k*w-n; if(cost<=0): print(0) else: print(int(cost))
800
PYTHON3
s=input().split() n=int(s[0]) m=int(s[1]) l=[0 for i in range(m)] for i in range(n): s=input().split() le=int(s[0]) ra=int(s[1]) for j in range(le-1,ra): l[j]+=1 print(l.count(0)) ll=[] for i in range(m): if l[i]==0: ll.append(i+1) for i in range(len(ll)-1): print(ll[i],end=" ") if len(ll)>0: print(ll[-1])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; void read(int &k) { k = 0; char x = getchar(); while (x < '0' || x > '9') x = getchar(); while (x >= '0' && x <= '9') { k = k * 10 - 48 + x; x = getchar(); } } int n, cnt[100010], lis[1000], len, k; long long add, ans; inline long long powe(long long a, int b) { long long ret = 1; for (; b; b >>= 1, a = a * a % MOD) if (b & 1) ret = ret * a % MOD; return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &k), cnt[k]++; for (int i = 1; i <= 100000; i++) cnt[i] += cnt[i - 1]; for (int i = 1, j; i <= 100000; i++) { len = 0; for (j = 1; j * j < i; j++) if (i % j == 0) lis[++len] = j, lis[++len] = i / j; if (j * j == i) lis[++len] = j; sort(lis + 1, lis + len + 1); add = 1; for (j = 1; j < len; j++) add = add * powe(j, cnt[lis[j + 1] - 1] - cnt[lis[j] - 1]) % MOD; long long tmp = cnt[100000] - cnt[lis[len] - 1]; add = (add * (powe(len, tmp) - powe(len - 1, tmp)) % MOD + MOD) % MOD; ans = (ans + add) % MOD; } cout << ans << endl; return 0; }
2,000
CPP
def check(p): players1 = players.split(p) for i in players1: if(len(i)>6): return 1 return 0 players = input() if(check("0")): print("YES") elif(check("1")): print("YES") else: print("NO")
900
PYTHON3
n=int(input()) h=input().split() a=0 max=int(h[0]) min=int(h[0]) for i in range(1,n): mi=int(h[i]) if mi>max: max=mi a+=1 if mi<min: min=mi a+=1 print(a)
800
PYTHON3
#include <bits/stdc++.h> int gcd(int j, int k) { int i = 0; if (j == 0) return k; else if (k == 0) return j; else { if (j > k) i = k; else i = j; for (i; i >= 1; i--) { if (j % i == 0 && k % i == 0) break; } return i; } } int main() { int n = 0, a = 0, b = 0, take = 0, cnt = 0; scanf("%d %d %d", &a, &b, &n); while (take <= n) { if (cnt % 2 == 0) { take = gcd(a, n); } else { take = gcd(b, n); } ++cnt; n -= take; } if (cnt % 2 != 0) printf("0"); else printf("1"); return 0; }
800
CPP
s = input() a = input() b = input() l = len(s) af = s.find(a) res_f = 0 res_b = 0 c = s[af:l] if af >= 0 and s[af+len(a):l].find(b) >= 0: res_f = 1 s = s[::-1] ab = s.find(a) if ab >= 0 and s[ab+len(a):l].find(b) >= 0: res_b = 1 if res_f and res_b: print("both") elif res_f: print("forward") elif res_b: print("backward") else: print("fantasy")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<long long> a(n + 1); for (long long i = 1; i <= n; i += 1) cin >> a[i]; sort(a.begin() + 1, a.end()); long long ans = 0; for (long long i = 1; i <= n - 1; i += 1) { ans += a[i] * (i + 1); } ans += a[n] * n; cout << ans << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> const double EPS = 1e-24; const long long int MOD = 1000000007ll; const double PI = 3.14159265359; long long int INF = 6e18; template <class T> T Max2(T a, T b) { return a < b ? b : a; } template <class T> T Min2(T a, T b) { return a < b ? a : b; } template <class T> T Max3(T a, T b, T c) { return Max2(Max2(a, b), c); } template <class T> T Min3(T a, T b, T c) { return Min2(Min2(a, b), c); } template <class T> T Max4(T a, T b, T c, T d) { return Max2(Max2(a, b), Max2(c, d)); } template <class T> T Min4(T a, T b, T c, T d) { return Min2(Min2(a, b), Max2(c, d)); } using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; a %= b; return gcd(b, a); } long long int lcm(long long int a, long long int b) { long long int g = gcd(a, b); a /= g; if (a * b / b != a) return INF; return a * b; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); long long int t, a, b; cin >> t >> a >> b; long long int tt = t; long long int LCM = lcm(a, b); long long int L = min(a, b) - 1; long long int ans = 0; ans += t / LCM * (L + 1); t %= LCM; ans += min(t, L); long long int GCD = gcd(ans, tt); cout << ans / GCD << "/" << tt / GCD << endl; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return f == 1 ? x : -x; } const int N = 104; int n, m, mx, id, a[N][N], f[N], g[N][N], ans[N]; inline void dfs(int u, int v, int p) { if (!u) return; if (!g[u][v]) { ans[p] = u; dfs(u - 1, v, p + 1); } else dfs(u - 1, v - 1, p); } int main() { n = read(); m = read(); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) a[i][j] = read(); mx = 0; for (int i = 1, now; i < n; i++) { memset(f, 0xcf, sizeof(f)); f[0] = 0; for (int u = 1; u <= m; u++) for (int j = u; j >= 1; j--) f[j] = (f[j] > f[j - 1] + a[u][i] - a[u][n] ? f[j] : f[j - 1] + a[u][i] - a[u][n]); now = 0; for (int j = m; j >= 0; j--) if (f[j] >= 0) { now = j; break; } if (now > mx) { mx = now; id = i; } } cout << m - mx << "\n"; if (mx == m) return (0 - 0); memset(f, 0xcf, sizeof(f)); f[0] = 0; for (int u = 1, x; u <= m; u++) for (int j = u; j >= 1; j--) { x = f[j - 1] + a[u][id] - a[u][n]; if (f[j] < x) { f[j] = x; g[u][j] = 1; } } dfs(m, mx, 1); for (int i = m - mx; i >= 1; i--) cout << ans[i] << " "; return (0 - 0); }
1,700
CPP
a = input() b = input() z = b.count('1') c = b.count('0') n = abs(z - c) print(n)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { double ans = 0; int n; cin >> n; for (int i = 1; i <= n; i++) ans += 1.0 / i; printf("%.12lf\n", ans); }
1,000
CPP
#include <bits/stdc++.h> #define ll long long using namespace std; int T,n,m; int a[20010]; int main() { scanf("%d",&T); while(T--) { int cd=0,num=0; m=0; scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d",&a[i]); m=m^a[i]; } if(!m)printf("YES\n"); else { for(int i=1; i<=n; i++) { cd=cd^a[i]; if(cd==m) { cd=0; num++; } } if((cd==m||cd==0)&&num>=2)printf("YES\n"); else printf("NO\n"); } } }
1,500
CPP
a = input() c = input() b = input() a = a + c c = 0 if len(a) != len(b): c = 1 else: for i in range(len(b)): if c != 0: break else: while c < len(a): if a[c] == b[0]: b, a = b[1:], a[:c] + a[c+1:] c = 0 break else: c += 1 if c != 0: print('NO') else: print('YES')
800
PYTHON3
t = int(input()) for _ in range(t): n, k = map(int, input().split()) print(k // (n - 1) * n + (k % (n - 1) if k % (n - 1) != 0 else -1))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int i, j, k, l, n, m, an, sum, K, L; const int shi = 1238; const int pp = 21323; const int N = 210000; char a[N], b[N]; int s[2][N], t[N]; int find(int now, int l, int r) { int k = ((s[now][r] - s[now][l - 1] * t[r - l + 1]) % pp + pp); if (k > pp) k -= pp; return k; } bool cheak(int l1, int r1, int l2, int r2) { if (find(0, l1, r1) == find(1, l2, r2)) return true; if ((r1 - l1 + 1) & 1) return false; int mid1 = (l1 + r1) / 2; int mid2 = (l2 + r2) / 2; if ((cheak(l1, mid1, mid2 + 1, r2)) && (cheak(mid1 + 1, r1, l2, mid2))) return true; if ((cheak(l1, mid1, l2, mid2)) && (cheak(mid1 + 1, r1, mid2 + 1, r2))) return true; return false; } int main() { scanf("%s", a + 1); scanf("%s", b + 1); n = strlen(a + 1); for (int i = 1; i <= n; i++) s[0][i] = ((s[0][i - 1] * shi % pp + a[i] - 'a') % pp + pp) % pp; for (int i = 1; i <= n; i++) s[1][i] = ((s[1][i - 1] * shi % pp + b[i] - 'a') % pp + pp) % pp; t[0] = 1; for (int i = 1; i <= n; i++) t[i] = (t[i - 1] * shi % pp + pp) % pp; if (cheak(1, n, 1, n)) printf("YES\n"); else printf("NO\n"); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, step; scanf("%d%d%d%d", &n, &m, &k, &step); int a[n][m]; long long s[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); s[i][j] = a[i][j]; if (i) s[i][j] += s[i - 1][j]; if (j) s[i][j] += s[i][j - 1]; if (i && j) s[i][j] -= s[i - 1][j - 1]; } priority_queue<int> q; long long dp1[k + 1], dp2[k + 1]; dp1[0] = dp2[0] = 0; for (int i = 0; i < n; i++) { long long tmp = s[i][m - 1]; if (i) tmp -= s[i - 1][m - 1]; q.push(tmp); } for (int i = 1; i <= k; i++) { long long x = q.top(); q.pop(); dp1[i] = dp1[i - 1] + x; q.push(x - m * step); } for (; !q.empty(); q.pop()) ; for (int j = 0; j < m; j++) { long long tmp = s[n - 1][j]; if (j) tmp -= s[n - 1][j - 1]; q.push(tmp); } for (int i = 1; i <= k; i++) { long long x = q.top(); q.pop(); dp2[i] = dp2[i - 1] + x; q.push(x - n * step); } long long ans = max(dp1[k], dp2[k]); for (int i = 1; i < k; i++) { long long tmp = dp1[i] + dp2[k - i]; tmp -= (long long)i * step * (k - i); ans = max(ans, tmp); } printf("%I64d", ans); exit(0); }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n; cin >> n; map<pair<long long int, long long int>, long long int> m; long long int f1 = 0, f2 = INT_MAX; char c[n + 1]; cin >> c; long long int x = 0, y = 0; m[make_pair(0, 0)] = 0; for (int i = 0; c[i] != '\0'; i++) { char ch = c[i]; if (ch == 'L') { x = x - 1; } else if (ch == 'R') { x = x + 1; } else if (ch == 'U') { y = y + 1; } else if (ch == 'D') { y = y - 1; } pair<long long int, long long int> temp; temp.first = x; temp.second = y; if (m.count(temp) == 1) { if (i - m[temp] < f2 - f1) { f1 = m[temp]; f2 = i; } } m[temp] = i + 1; } if (f2 != INT_MAX) { cout << f1 + 1 << " " << f2 + 1 << endl; } else { cout << -1 << endl; } m.clear(); } return 0; }
1,500
CPP
#########################################################################################################\ ######################################################################################################### ###################################The_Apurv_Rathore##################################################### ######################################################################################################### ######################################################################################################### import sys,os,io from sys import stdin from math import log, gcd, ceil from collections import defaultdict, deque, Counter from heapq import heappush, heappop from bisect import bisect_left , bisect_right import math alphabets = list('abcdefghijklmnopqrstuvwxyz') def ncr(n, r, p): 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 primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3,int(math.sqrt(n))+1,2): while n % i== 0: l.append(int(i)) n = n / i if n > 2: l.append(n) return list(set(l)) def power(x, y, p) : res = 1 x = x % p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res def SieveOfEratosthenes(n): prime = [True for i in range(n+1)] p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * p, n+1, p): prime[i] = False p += 1 return prime def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def si(): return input() def prefix_sum(arr): r = [0] * (len(arr)+1) for i, el in enumerate(arr): r[i+1] = r[i] + el return r def divideCeil(n,x): if (n%x==0): return n//x return n//x+1 def ii(): return int(input()) def li(): return list(map(int,input().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') #__________________________TEMPLATE__________________OVER_______________________________________________________ if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") ; sys.stdout = open("output.txt","w") t = 1 # t = int(input()) for _ in range(t): n,m,ta,tb,k = li() a = li() b = li() ans = 0 for cancela in range(k+1): if cancela>=n: ans = -1 break cancelb = k - cancela nextflighttob = a[cancela] time = nextflighttob + ta flighttoc = bisect_left(b,time) if flighttoc+cancelb>=m: ans = -1 break temp = 1 if (b[flighttoc]==time): temp = 0 ans = max(ans, b[flighttoc+cancelb]+tb) print(ans)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; string s; bool check; bool isPalindrome(string str) { int l = 0, r = str.size() - 1; while (l < r) { if (str[l] != str[r]) return false; l++; r--; } return true; } string Solve(string str, int pos1, int pos2) { string tmp; string s1 = str, s2 = str; if (pos2 + 1 >= s1.size()) s1.push_back(s1[pos1]); else s1.insert(s1.begin() + pos2 + 1, s1[pos1]); s2.insert(s2.begin() + pos1, s2[pos2]); if (isPalindrome(s1)) return s1; if (isPalindrome(s2)) return s2; return tmp; } int main() { cin >> s; int l = 0, r = s.size() - 1; while (l < r) { if (s[l] != s[r]) { string tmp = Solve(s, l, r); if (!tmp.empty()) { cout << tmp << endl; return 0; } check = true; } l++; r--; } if (!check) { s.insert(s.begin() + s.size() / 2, s[s.size() / 2]); cout << s << endl; } else cout << "NA" << endl; return 0; }
3,000
CPP
#include <bits/stdc++.h> const long long int mrx = 1e6 + 5, M = 1e9 + 7, dm = 1e3 + 5; long long int mx = -1e18 - 1, mn = 1e18 + 1; using namespace std; long long int n; string s, d[mrx], f; int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cin >> n; cin >> s; for (int i = 0; i < n; i++) { cin >> d[i]; } if (n % 2 == 0) { cout << "home"; } else { cout << "contest"; } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; while (scanf("%d%d", &n, &k) != EOF) { if ((n - 1) % k == 0) printf("%d\n", (n - 1) / k * 2); else if ((n - 1) % k == 1) printf("%d\n", (n - 1) / k * 2 + 1); else printf("%d\n", (n - 1) / k * 2 + 2); for (int i = 1; i <= k; i++) { if (i <= (n - 1) % k) for (int j = 1; j <= (n - 1) / k; j++) { printf("%d %d\n", i + (j - 1) * k, i + j * k); } else { for (int j = 1; j <= (n - 1) / k - 1; j++) { printf("%d %d\n", i + (j - 1) * k, i + j * k); } } printf("%d %d\n", i, n); } } }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a; string j; cin >> a; int x = 0; while (a--) { cin >> j; if (j == "X++") { x++; } else if (j == "X--") { x--; } else if (j == "++X") { ++x; } else if (j == "--X") { --x; } } cout << x << '\n'; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 105000, M = 350; mt19937 gen(time(NULL)); template <typename T = int> inline T read() { T val = 0, sign = 1; char ch; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') sign = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) val = val * 10 + ch - '0'; return sign * val; } long long a[N], falls[N]; struct spt { vector<vector<long long>> dp; vector<int> log; int n; bool isMax; void init(vector<int> a) { for (int i = 1; i <= n; i++) dp[0][i] = a[i]; for (int k = 1; (1 << k) <= n; k++) for (int i = 1; i + (1 << k) <= n + 1; i++) { long long lval = dp[k - 1][i], rval = dp[k - 1][i + (1 << (k - 1))]; if (isMax) dp[k][i] = max(lval, rval); else dp[k][i] = min(lval, rval); } } long long get(int l, int r) { if (l > r) assert(0); int k = log[r - l + 1]; long long lval = dp[k][l], rval = dp[k][r - (1 << k) + 1]; return isMax ? max(lval, rval) : min(lval, rval); } spt(int n, bool isMax = true) : n(n), isMax(isMax) { log.resize(n + 1); for (int i = 2; i <= n; i++) log[i] = log[i / 2] + 1; dp.resize(20); for (int i = 0; i < 20; i++) { dp[i].resize(n + 1); if (!isMax) fill((dp[i]).begin(), (dp[i]).end(), 1e18); } } }; long long lm[N], rm[N]; int ws1[N], n; void solve() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); long long ans = 1e18; int L = 1, R = 1; fill_n(lm, N, 1e18), fill_n(rm, N, 1e18); for (int i = 1; i <= n; i++) ws1[i] = 2 * i - a[i]; auto S = spt(n, false); S.init(vector<int>(a, a + n + 1)); auto LS = spt(n, false); vector<int> la(n + 1); for (int i = 1; i <= n; i++) la[i] = a[i] - i; LS.init(la); auto RS = spt(n, false); for (int i = 1; i <= n; i++) la[i] = a[i] + i; RS.init(la); for (int T = 1; T <= n; T++) { while (L <= T && T >= 2 * L - a[L]) L++; int j = -1; if (L <= T) j = L; else { int lx = T, rx = n; int ind_ans = -1; while (lx <= rx) { int mx = (lx + rx) / 2; if (S.get(T, mx) >= T) { lx = mx + 1; } else { ind_ans = mx; rx = mx - 1; } } if (ind_ans == -1) continue; j = ind_ans; } long long lp = 0, rp = 0; fprintf(stderr, "[%d -> %d]\n", T, j), fflush(stderr); if (j <= T) { lp = LS.get(1, j - 1) + T; rp = min(LS.get(j, T) + T, RS.get(T, n) - T); } else { lp = min(LS.get(1, T) + T, RS.get(T, j - 1) - T); rp = RS.get(j, n) - T; } fprintf(stderr, "%lld %lld\n", lp, rp), fflush(stderr); ans = min(ans, max(lp, rp)); } if (ans == 1e18) puts("-1"); else printf("%lld\n", ans); } void precalc() {} signed main() { int t = 1; precalc(); while (t--) { clock_t z = clock(); solve(); fprintf(stderr, "Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC), fflush(stderr); } }
2,900
CPP
def solve(L): count=[0]*len(L) for i in range(len(L)): count[L[i]-1]=i pL=[] pt=len(L) for i in range(len(L)-1, -1, -1): if pt>count[i]: pL.append(pt-count[i]) pt=count[i] countL=[0]*(2001) countL[0]=1 for i in pL: permL=[] for j in range(len(countL)): if countL[j] and j+i<2001: permL.append(i+j) for j in permL:countL[j]=1 return countL[len(L)//2] for i in ' '*int(input()): n=input() print(['NO','YES'][solve(list(map(int,input().split())))])
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; string s; int main() { int n, a, b; cin >> n >> a >> b; cin >> s; int cnt_ = 0; int ans = 0; for (int i = 0; i < n; i++) { if (max(a, b) <= 0) break; if (s[i] == '.') { if (a < b) swap(a, b); cnt_++; if (i == n - 1) { ans += a >= (cnt_ + 1) / 2 ? (cnt_ + 1) / 2 : a; ans += b >= cnt_ / 2 ? cnt_ / 2 : b; } } else if (cnt_ == 0) continue; else { if (a < b) swap(a, b); ans += a >= (cnt_ + 1) / 2 ? (cnt_ + 1) / 2 : a; ans += b >= cnt_ / 2 ? cnt_ / 2 : b; a -= (cnt_ + 1) / 2; b -= cnt_ / 2; if (a < 0) a = 0; if (b < 0) b = 0; cnt_ = 0; } } cout << ans << endl; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int ABS(int a) { if (a < 0) return (-a); return a; } int main() { long long int n; cin >> n; if (n < 0) n *= -1; long long int ans = 0; long long int i = 0; while (ans < n || (ans - n) % 2 != 0) { ++i; ans += i; } cout << i << endl; return 0; }
1,600
CPP
t=int(input()) for _ in range(t): n,r=map(int,input().split()) if(n>r): print((r*(r+1))//2) else: t=n-1 print((t*(t+1))//2+1)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, ans = 0, power = 10; cin >> n; for (long long i = 1;; i++) { if (n >= power) { ans += power / 10 * 9 * i; } else { long long num = n - power / 10 + 1; ans += num * i; break; } power *= 10; } cout << ans << endl; }
1,200
CPP
import sys n, k = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) xs = [0] for i in range(n): # O(nlogn) x = a[i] while x != 0: xs.append(x) x = x//2 xs = list(set(xs)) op_cnts = dict() for x in xs: # O(1) op_cnts[x] = [] for i in range(n): # O(nlogn) cnt = 0 start = a[i] while True: if start in op_cnts: op_cnts[start].append(cnt) cnt += 1 if start == 0: break start = start // 2 minVal = sys.maxsize for x in xs: if len(op_cnts[x]) >= k: op_cnts[x] = sorted(op_cnts[x]) for x in xs: # O(nlogn) if len(op_cnts[x]) >= k: # print(op_cnts[x]) sumVal = 0 for j in range(k): sumVal += op_cnts[x][j] minVal = min(minVal, sumVal) print(minVal)
2,200
PYTHON3
# -*- coding: utf-8 -*- """ Created on Wed May 27 18:59:33 2020 @author: DELL """ s = input() copy = s # we aim to count the ascii codes of strings in a-z and A-Z ranges. l = list(copy) count = 0 for i in range(len(l)): l[i] = ord(l[i]) if 96< l[i] < 123: count += 1 elif 64 < l[i] < 91: count-= 1 if count >= 0: s = s.lower() else: s = s.upper() print(s)
800
PYTHON3
# -*- coding: utf-8 -*- """ Created on Tue Dec 19 23:03:07 2017 @author: admin """ N = int(input()) length = [int(x) for x in input().split()] s = set(length) tower = [] for x in s: num = length.count(x) tower.append(num) print(max(tower),len(tower))
1,000
PYTHON3
n,k = [int(x) for x in input().split()] arr = list(map(int,input().split())) a = set(arr) if (len(a)<k): print("NO") else: print("YES") var=0 for i in range(n): if arr[i] in a and var<k: print(i+1,end=" ") var+=1 a.remove(arr[i]) print()
800
PYTHON3