solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; const int N=200010; int f[N],sz[N]; int mp[N]; signed main() { int t;cin>>t; while(t--) { int n; cin>>n; for(int i=1;i<=n;i++)scanf("%d",&sz[i]); sort(sz+1,sz+n+1); //for(int i=1;i<=n;i++)if(!mp[sz[i]])mp[sz[i]]=i; int res=0; for(int i=1;i<=n;i++) { f[i]=1; for(int j=1;j<=sz[i]/j;j++) { if(sz[i]%j==0) { int p1=j,p2=sz[i]/j; f[i]=max(f[i],f[mp[p1]]+1); f[i]=max(f[i],f[mp[p2]]+1); } } res=max(res,f[i]); mp[sz[i]]=i; } printf("%d\n",n-res); for(int i=1;i<N;i++)f[i]=0,mp[i]=0; } }
1,900
CPP
a,b=list(map(int,input().split(" "))) c=list(map(int,input().split(" "))) c.sort() diff=c[-1]-c[0] for i in range(0,b-a+1): if (c[i+a-1]-c[i])<diff: diff=c[i+a-1]-c[i] print(diff)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, maxd = 55, maxN = 5e6 + 5, maxe = 5e6 + 5; int n, m, d, num[maxN]; int getid(int i, int j) { return i + n * (j - 1); } int tot, go[maxe], nxt[maxe], f1[maxN]; void ins(int x, int y) { go[++tot] = y; nxt[tot] = f1[x]; f1[x] = tot; } int tot2, go2[maxe], nxt2[maxe], f2[maxN], com[maxN]; void ins2(int x, int y) { go2[++tot2] = y; nxt2[tot2] = f2[x]; f2[x] = tot2; com[y]++; } int sum, dfn[maxN], low[maxN], bz[maxN], z0, z[maxN], rt[maxN]; void tarjan(int k) { dfn[k] = low[k] = ++sum; bz[k] = 1; z[++z0] = k; for (int p = f1[k]; p; p = nxt[p]) { if (!bz[go[p]]) { tarjan(go[p]); low[k] = min(low[k], low[go[p]]); } else if (bz[go[p]] == 1) low[k] = min(low[k], dfn[go[p]]); } if (dfn[k] == low[k]) { do { bz[z[z0]] = 2; rt[z[z0]] = k; } while (z[z0--] != k); } } int f[maxN], q[maxN]; void topo() { int j = 0; for (int i = 1; i <= n * d; i++) if (rt[i] == i && !com[i]) q[++j] = i; for (int i = 1; i <= j; i++) { f[q[i]] += num[q[i]]; for (int p = f2[q[i]]; p; p = nxt2[p]) { f[go2[p]] = max(f[go2[p]], f[q[i]]); if (--com[go2[p]] == 0) q[++j] = go2[p]; } } } char s[maxd]; int main() { scanf("%d %d %d", &n, &m, &d); for (int i = 1; i <= m; i++) { int x, y; scanf("%d %d", &x, &y); for (int j = 1; j <= d; j++) ins(getid(x, j), getid(y, j % d + 1)); } tarjan(1); memset(bz, 0, sizeof(bz)); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); for (int j = 1; j <= d; j++) if (s[j] == '1') { int id = rt[getid(i, j)]; if (!bz[id]) bz[id] = 1, num[id]++; } for (int j = 1; j <= d; j++) bz[rt[getid(i, j)]] = 0; } for (int i = 1; i <= n * d; i++) for (int p = f1[i]; p; p = nxt[p]) if (rt[go[p]] != rt[i] && rt[go[p]] && rt[i]) ins2(rt[go[p]], rt[i]); topo(); printf("%d\n", f[1]); }
2,500
CPP
# Main maut ko takiya, aur kafan ko chaadar banakar audhta hoon! x=input().split() s=int(x[0]) v1=int(x[1]) v2=int(x[2]) t1=int(x[3]) t2=int(x[4]) ant1=0 ant2=0 ant1=2*t1+s*v1 ant2=2*t2+s*v2 if(ant1==ant2): print("Friendship") elif(ant1<ant2): print("First") else: print("Second")
800
PYTHON3
def _gcd(a, b): while b != 0: a, b = b, a % b return a def gcd(*numbers): result = 0 if len(numbers) == 1: numbers = numbers[0] for number in numbers: result = _gcd(result, number) return result n, k = list(map(int, input().split())) print((n * 10 ** k) // _gcd(n, 10**k))
1,100
PYTHON3
for i in range(int(input())): a=int(input());k=list(map(int,input().split()));s=0;f=0 for j in range(a): if k[j]%2==0:print(1);print(j+1);f=1;break else: s+=k[j] if s%2==0:print(j+1);print(*[*range(1,j+2)]);f=1;break if f==0:print(-1)
800
PYTHON3
a = int(input()) d = [0] * a for i in range(0, a): b = int(input()) d[i] = b for i in range(0, a): m = 0 l = 0 f = [0] * d[i] if d[i] % 2 == 0: for i2 in range(1, d[i]//2+1): f[i2-1] = i2*2 m += i2*2 l += i2*2-1 f[i2-1+(d[i]//2)] = i2*2-1 if abs(m-l) % 2 == 0: if m > l: f[d[i]-1] = f[d[i]-1] + m-l else: f[d[i]//2-1] = f[d[i]//2-1] + l-m print("YES") print(*f) else: print("NO") else: print("NO")
800
PYTHON3
s = int(input().strip()) ans = 0 for i in range(1,s + 1): ans += 1/i print(ans)
1,000
PYTHON3
w = int(input()) if w == 1 or w == 2: print("NO") else: a = 2 b = w - 2 if a % 2 == 0 and b % 2 == 0: print("YES") else: print("NO")
800
PYTHON3
n = int(input()) lst=[] s = input() lst = s.split() lst = [int(x) for x in lst[:] ] half = sum(lst)//2 + 1 lst.sort() lst.reverse() sum = 0 cnt = 0 for x in lst: sum += x cnt += 1 if sum>=half: break print(cnt) ''' ### 236A_ Boy or girl S = set() s = input() for i in s: S.add(i) if len(S)&1: print('INGROE HIM') else: print('CHAT WITH HER') ## 116A Tram #include <iostream> #define MAX 1010 int a[MAX]; int b[MAX]; using namespace std; int main() { int n; cin>>n; for(int i=0;i<n;i++){ } } # 122A Lucky Division lst = list(input()) for i in lst: if i is not '4' and i is not '7' : print('NO') break else: pass else: print('YES') ''' ''' #148A insomnia cure k=int(input()) l=int(input()) m=int(input()) n=int(input()) d=int(input()) S=set() for i in range(1, d+1): if k*1 <= d: S.add(k*i) if l*i <=d: S.add(l*i) if m*i <= d: S.add(m*i) if n*i <= d: S.add(n*i) print(len(S)) '''
900
PYTHON3
import math def field(n, x, y, t): t = t + 1 upper_dist = x - 1 left_dist = y - 1 down_dist = n - x right_dist = n - y out_up = max(0, t - upper_dist - 1) out_down = max(0, t - down_dist - 1) out_left = max(0, t - left_dist - 1) out_right = max(0, t - right_dist - 1) #print(out_up, out_right, out_down, out_left) #print(base_field(t), right_field(out_right), right_field(out_left), up_field(out_up, n, y), up_field(out_down, n, y)) field = base_field(t) - right_field(out_right) - right_field(out_left) - up_field(out_up, n, y) - up_field(out_down, n, y) return field def right_field(out_r): return out_r ** 2 def up_field(out_up, n, y): rect = max(0, out_up - n + 1) h = out_up - rect wyst = max(y - 1 + h - n, 0, h - y) result = n * rect + h ** 2 - int((1 + wyst) / 2 * wyst) if result < 0: result = 0 return result def base_field(t): return 2 * (t ** 2) - 2 * t + 1 class CodeforcesTask256BSolution: def __init__(self): self.result = '' self.n_x_y_c = [] def read_input(self): self.n_x_y_c = [int(x) for x in input().split(" ")] def process_task(self): search = 0 mid = 1 found = False last_sm = 0 while not found: #print(last_sm ,search) ff = field(self.n_x_y_c[0], self.n_x_y_c[1], self.n_x_y_c[2], search) if ff == self.n_x_y_c[3]: found = True elif ff > self.n_x_y_c[3]: if search - last_sm == 1: found = True else: search = last_sm + (search - last_sm) // 2 else: last_sm = search search += mid mid = search - last_sm self.result = str(search) def get_result(self): return self.result if __name__ == "__main__": Solution = CodeforcesTask256BSolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
1,800
PYTHON3
n=int(input()) s=input() a=[] for i in range(n): a.append(0) check=0 count=0 ind=-1 for i in range(n): if(s[i]=='B' and check==0): check=1 ind+=1 count+=1 elif(s[i]=='B' and check==1): count+=1 elif(s[i]=='W' and check==1): check=0 a[ind]=count count=0 if(s[n-1]=='B'): check=0 a[ind]=count count=0 ind+=1 print(ind) j=0 for j in range(ind): print(a[j],end=' ')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char arr[1000]; int n; int cntr; char a; int main(int argc, char** argv) { char c; while (cin >> c) { if (c == '}') { break; } if (c != ',' && c != ' ' && c != '{') { arr[n] = c; n++; } } for (int i = 0; i < n; i++) { a = arr[i]; for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { arr[j] = '*'; } } } for (int i = 0; i < n; i++) { if (arr[i] != '*') { cntr++; } } cout << cntr; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; long long int nibun(long long int r, long long int n) { long long int start = 1; long long int end = n; while (true) { long long int i = (start + end) / 2; long long int m = n / i; if ((i - 1) / 2 + r > m) { if (start == end) return 0; if (start == end - 1) { end = start; } else { end = i; } } else if ((i - 1) / 2 + r == m && (n % i) == 0) { if ((i % 2) == 0) return 0; return i * (r + 1); } else { if (start == end) return 0; if (start == end - 1) { start = end; } else { start = i; } } } return 0; } int main() { long long int n; cin >> n; vector<long long int> ans; for (long long int i = 1; i <= n + 1; i *= 2) { long long int a = nibun(i - 1, n); if (a != 0) ans.push_back(a); } if (ans.size() == 0) { cout << "-1" << endl; } else { sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } } return 0; }
1,800
CPP
n=int(input()) if(n%3==0): print(1,1,n-2) else: print(1,n%3,n-1-(n%3))
800
PYTHON3
def partition(nums, low, high): # Выбираем средний элемент в качестве опорного # Также возможен выбор первого, последнего # или произвольного элементов в качестве опорного pivot = nums[(low + high) // 2] i = low - 1 j = high + 1 while True: i += 1 while nums[i] < pivot: i += 1 j -= 1 while nums[j] > pivot: j -= 1 if i >= j: return j # Если элемент с индексом i (слева от опорного) больше, чем # элемент с индексом j (справа от опорного), меняем их местами nums[i], nums[j] = nums[j], nums[i] def quick_sort(nums): # Создадим вспомогательную функцию, которая вызывается рекурсивно def _quick_sort(items, low, high): if low < high: # This is the index after the pivot, where our lists are split split_index = partition(items, low, high) _quick_sort(items, low, split_index) _quick_sort(items, split_index + 1, high) _quick_sort(nums, 0, len(nums) - 1) s = list(map(int,input().split('+'))) #print(s) quick_sort(s) print('+'.join(map(str, s)))
800
PYTHON3
for _ in range(int(input())): n=int(input()) a=sorted(map(int,input().split()));s=a[0];i=0; if sum(a)==0: print('No') else: print('Yes') for j in a: if j<0: i+=1 else: break if -(sum(a[:i]))<sum(a[i:]): print(*(list(reversed(a)))) else: print(*a)
900
PYTHON3
x= [int(i) for i in input().split()] n,m= x[0],x[1] nlist= [int(i) for i in input().split()] mlist=[int(i) for i in input().split() ] index=0 i=0 res=0 while i < n and index < m: if nlist[i] <= mlist[index] : res=res+1 i=i+1 index=index+1 else : i=i+1 print(res)
800
PYTHON3
from bisect import bisect_left, bisect_right from itertools import combinations from itertools import permutations from bisect import bisect_left from math import ceil from math import radians from heapq import heapify, heappush, heappop import bisect from math import pi from collections import deque from math import factorial from math import log, ceil from collections import defaultdict from math import * from sys import stdin, stdout import itertools import os import sys import threading from collections import deque, Counter, OrderedDict, defaultdict from heapq import * # 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 mod = int(pow(10, 9)+7) # mod = 998244353 def ii(): return int(input()) def si(): return str(input()) def mi(): return map(int, input().split()) def li1(): 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()) 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} 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 graph = defaultdict(list) visited = [0] * 1000000 col = [-1] * 1000000 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 = {} for i in range(e): x, y = mi() if x not in d: d[x] = [y] else: d[x].append(y) if y not in d: d[y] = [x] else: 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 and i % 2 == 1: s.add(i) n = n // i if n > 2 and n % 2 == 1: 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 SieveOfEratosthenes(n, isPrime): isPrime[0] = isPrime[1] = False for i in range(2, n): isPrime[i] = True p = 2 while (p * p <= n): if (isPrime[p] == True): i = p * p while (i <= n): isPrime[i] = False i += p p += 1 return isPrime def dijkstra(edges, f, t): g = defaultdict(list) for l, r, c in edges: g[l].append((c, r)) q, seen, mins = [(0, f, ())], set(), {f: 0} while q: (cost, v1, path) = heappop(q) if v1 not in seen: seen.add(v1) path = (v1, path) if v1 == t: return (cost, path) for c, v2 in g.get(v1, ()): if v2 in seen: continue prev = mins.get(v2, None) next = cost + c if prev is None or next < prev: mins[v2] = next heappush(q, (next, v2, path)) return float("inf") def binsearch(a, l, r, x): while l <= r: mid = l + (r-1)//2 if a[mid]: return mid elif a[mid] > x: l = mid-1 else: r = mid+1 return -1 # def input(): # return stdin.buffer.readline() def readTree(n): adj = [set() for _ in range(n)] for _ in range(n-1): u, v = map(int, input().split()) adj[u-1].add(v-1) adj[v-1].add(u-1) return adj def treeOrderByDepth(n, adj, root=0): parent = [-2] + [-1]*(n-1) ordered = [] q = deque() q.append(root) depth = [0] * n while q: c = q.popleft() ordered.append(c) for a in adj[c]: if parent[a] == -1: parent[a] = c depth[a] = depth[c] + 1 q.append(a) return (ordered, parent, depth) def isSubSequence(str1, str2): m = len(str1) n = len(str2) j = 0 # Index of str1 i = 0 # Index of str2 # Traverse both str1 and str2 # Compare current character of str2 with # first unmatched character of str1 # If matched, then move ahead in str1 while j < m and i < n: if str1[j] == str2[i]: j = j+1 i = i + 1 # If all characters of str1 matched, then j is equal to m return j == m def nextPowerOf2(n): count = 0 # First n in the below # condition is for the # case where n is 0 if (n and not(n & (n - 1))): return n while(n != 0): n >>= 1 count += 1 return 1 << count def cou(n): c = 0 while n > 1: c += 1 n //= 2 return c def sortsec(l): return sorted(l, key=lambda x: x[1]) def BinarySearch(a, x): i = bisect_left(a, x) if i: return (i-1) else: return - 1 def subarray(A): r = set() p = {0} for x in A: p = {x | y for y in p} | {x} r |= p return len(r) for _ in range(ii()): n, k = mi() l = li1() x = [] y = [] a = max(l) for i in l: x.append(a - i) b = max(x) for i in x: y.append(b - i) if k % 2 == 1: print(*x) else: print(*y)
800
PYTHON3
n = int(input()) a = list(map(int, input().split())) s = set(a) if 0 in s: print(len(s)-1) else: print(len(s))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 100 * 6 + 10; const long long int mod = 1e9 + 7; const long long int inf = 1e18; long long int dp[N][40]; vector<int> adj[N]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; adj[y].push_back(x); adj[x].push_back(y); } int p = 1 << n; for (int mask = 1; mask < p; mask++) { int u = __builtin_ctz(mask), b = __builtin_popcount(mask); if (b == 1) { dp[mask][u] = 1; continue; } for (int j = u + 1; j < n; j++) { if ((mask >> j) & 1) { int mask2 = mask ^ (1 << j); for (auto x : adj[j]) { if ((mask >> x) & 1) dp[mask][j] += dp[mask2][x]; } } } } long long int ans = 0; for (int mask = 1; mask < p; mask++) { int u = __builtin_ctz(mask), b = __builtin_popcount(mask); if (b <= 2) continue; for (auto x : adj[u]) { if ((mask >> x) & 1) ans += dp[mask][x]; } } cout << ans / 2 << endl; return 0; }
2,200
CPP
######################################################################################################### ######################################################################################################### ###################################The_Apurv_Rathore##################################################### ######################################################################################################### ######################################################################################################### import sys import os import io from math import log, gcd, ceil from collections import defaultdict, deque, Counter from heapq import heappush, heappop import math 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 si(): return input() # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline 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 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 li(): return list(map(int, input().split())) # t = int(input()) t = 1 for _ in range(t): n = ii() d = defaultdict(lambda:0) l = [] for i in range(n): a,b = li() l.append([a,b]) x = list(set(primeFactors(a)+primeFactors(b))) ff = 0 for i in x: f = 0 for j in range(n): if (l[j][0]%i!=0 and l[j][1]%i!=0): f = 1 break if (f==0): print(int(i)) ff = 1 break if ff==0: print(-1)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, l; cin >> a >> b >> c >> l; long long ans = 1LL * (l + 3) * (l + 2) * (l + 1) / 6; for (int la = 0; la <= l; la++) { int x = min(l - la, a + la - b - c); if (x >= 0) { ans -= 1LL * (x + 2) * (x + 1) / 2; } } for (int lb = 0; lb <= l; lb++) { int x = min(l - lb, b + lb - a - c); if (x >= 0) { ans -= 1LL * (x + 2) * (x + 1) / 2; } } for (int lc = 0; lc <= l; lc++) { int x = min(l - lc, c + lc - b - a); if (x >= 0) { ans -= 1LL * (x + 2) * (x + 1) / 2; } } cout << ans << endl; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int n, m, x, y, rowSum[1010][1010], colSum[1010][1010], a[1010][1010]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &x, &y); a[y][x]++; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { rowSum[i][j] = rowSum[i][j - 1] + a[i][j]; colSum[i][j] = colSum[i - 1][j] + a[i][j]; } } int ans = 0; for (int i = 2; i < n; i++) { if (n % 2 == 1 && i == (n + 1) / 2) { if (colSum[n][i] == 0) ans++; else if (rowSum[i][n] == 0) ans++; } else { if (colSum[n][i] == 0) ans++; if (rowSum[i][n] == 0) ans++; } } cout << ans << endl; return 0; }
1,800
CPP
t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) l.sort() l.reverse() for i in l: print(i,end=" ")
1,000
PYTHON3
# bename boosdahandeye ghalbha # WATERMELONE x = int(input()) if x == 2 or x == 0: print('NO') elif x % 2 == 0: print('YES') elif x % 2 != 0: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9, N = 100010; const long long INFL = 1e18; int n; int r[N], h[N]; long long V[N], pos[N]; long long f[N], T[N]; long long ans; vector<pair<long long, long long> > a; void upd(int x, long long val) { for (; x <= n; x += x & -x) T[x] = max(T[x], val); } long long get(int x) { long long res = 0; for (; x > 0; x -= x & -x) res = max(res, T[x]); return res; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> r[i] >> h[i], V[i] = 1LL * r[i] * r[i] * h[i], a.push_back(pair<long long, long long>(V[i], i)); sort(a.begin(), a.end()); for (int i = 1; i <= n; i++) { pos[i] = lower_bound(a.begin(), a.end(), pair<long long, long long>(V[i], 0)) - a.begin() + 1; } for (int i = 1; i <= n; i++) { f[i] = get(pos[i] - 1) + V[i]; upd(pos[i], f[i]); } for (int i = 1; i <= n; i++) ans = max(ans, f[i]); cout << setprecision(6) << fixed << (double)M_PI * ans << "\n"; }
2,000
CPP
t=int(input()) for test in range(t): x = int(input()) l = list(map(int,input().split())) l.sort() ans = l[0]*l[-1] flag=0 ct=0 cp=x #print(l) for i in range(2,int(ans**0.5)+1): #print(i,ct,l[ct],l[x-ct-1]) if ans%i==0 and (l[ct]!=i or l[x-ct-1]!=ans//i): flag=1 break elif ans%i==0: ct+=1 if i!=ans//i: cp-=2 else: cp-=1 if flag==1 or cp>0 or cp<0: print(-1) else: print(ans)
1,600
PYTHON3
import sys class Solution: def __init__(self): pass def run(self): result = 0 n = int(input()) for i in range(n): inp = str(input()) s = inp.split() if (int(s[0]) + int(s[1]) + int(s[2])) > 1: result += 1 print(result) if __name__ == '__main__': s = Solution() s.run()
800
PYTHON3
# -*- coding:utf-8 -*- """ created by shuangquan.huang at 11/21/18 """ N = int(input()) A = [int(x) for x in input().split()] B = [abs(x) for x in A] zeros = [i for i in range(N) if A[i] == 0] positives = [i for i in range(N) if A[i] > 0] negs = [(abs(A[i]), i) for i in range(N) if A[i] < 0] idx = [] if len(negs) % 2 == 0: if len(zeros) > 1: print('\n'.join(['1 {} {}'.format(i+1, zeros[0] + 1) for i in zeros[1:]])) if len(zeros) < N: print('2 {}'.format(zeros[0] + 1)) elif len(zeros) == 1: print('2 {}'.format(zeros[0] + 1)) else: pass idx = [i for v, i in negs] + positives else: negs.sort() negs = [i for v, i in negs] if len(zeros) > 0: print('\n'.join(['1 {} {}'.format(i + 1, zeros[0] + 1) for i in zeros[1:]])) print('1 {} {}'.format(zeros[0]+1, negs[0]+1)) if len(zeros) + 1 < N: print('2 {}'.format(negs[0] + 1)) else: print('2 {}'.format(negs[0] + 1)) idx = positives + negs[1:] if idx: mx = -1 mxVal = float('-inf') for i in idx: if B[i] > mxVal: mxVal = B[i] mx = i print('\n'.join(['1 {} {}'.format(u + 1, mx + 1) for u in idx if u != mx]))
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; int pos[600005], p[600005]; int a[600005], b[600005]; long long S[600005 << 2], lazy[600005 << 2]; void shift(int node, long long val) { S[node] += val; lazy[node] += val; } void push(int node) { shift(node << 1, lazy[node]); shift(node << 1 | 1, lazy[node]); lazy[node] = 0; } void up2(int node, int bas, int son, int l, int r, int val) { if (bas > r || son < l) return; if (bas >= l && son <= r) { lazy[node] += val; S[node] += val; return; } push(node); up2(node << 1, bas, ((bas + son) >> 1), l, r, val), up2(node << 1 | 1, ((bas + son) >> 1) + 1, son, l, r, val); S[node] = min(S[node << 1], S[node << 1 | 1]); } void up1(int node, int bas, int son, int x, long long val) { if (bas > x || son < x) return; if (bas == x && son == x) { S[node] = min(S[node], val); return; } push(node); up1(node << 1, bas, ((bas + son) >> 1), x, val), up1(node << 1 | 1, ((bas + son) >> 1) + 1, son, x, val); S[node] = min(S[node << 1], S[node << 1 | 1]); } long long get(int node, int bas, int son, int x) { if (bas > x || son < x) return (2ll * 1000000000 * 1000000000); if (bas == x && son == x) { return S[node]; } push(node); return min(get(node << 1, bas, ((bas + son) >> 1), x), get(node << 1 | 1, ((bas + son) >> 1) + 1, son, x)); } void build(int node, int bas, int son) { if (bas == son) { S[node] = (bas ? (2ll * 1000000000 * 1000000000) : 0); return; } build(node << 1, bas, ((bas + son) >> 1)); build(node << 1 | 1, ((bas + son) >> 1) + 1, son); S[node] = (2ll * 1000000000 * 1000000000); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> p[i]; cin >> m; for (int i = 1; i <= m; i++) cin >> b[i], pos[b[i]] = i; build(1, 0, n); long long ans = (2ll * 1000000000 * 1000000000); for (int i = 1; i <= n; i++) { if (pos[a[i]]) { if (p[i] < 0) { up2(1, 0, n, a[i], n, p[i]); } int sm = b[pos[a[i]] - 1]; long long best = get(1, 0, n, sm); up1(1, 0, n, a[i], best); up2(1, 0, n, 0, a[i] - 1, p[i]); } else { up2(1, 0, n, 0, a[i] - 1, p[i]); if (p[i] < 0) { up2(1, 0, n, a[i], n, p[i]); } } } ans = get(1, 0, n, b[m]); if (ans >= (2ll * 1000000000 * 1000000000) / 2) cout << "NO"; else { cout << "YES\n"; cout << ans; } }
2,500
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, mx = -100000, mn = 1000000000, x, i, lu; cin >> n; vector<int> cnt(1e6 + 5), l(1e6 + 5); for (i = 0; cin >> x; i++) { if (cnt[x] == 0) l[x] = i; cnt[x]++; if (mx < cnt[x]) mx = cnt[x], lu = l[x], mn = i - lu; else if (mx == cnt[x] && mn > i - l[x]) lu = l[x], mn = i - lu; } cout << lu + 1 << " " << lu + mn + 1; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double PI = acos(-1); const int INF = (int)1e9 + 9; const int MAXN = (int)4e6 + 7; int n; int a[MAXN]; int b[MAXN]; int d[MAXN]; int delta; int pos[MAXN]; multiset<int> M; multiset<int> P; int get(int x) { return d[x] + delta; } void add(int x, int y) { int cval = d[x]; if (cval + delta <= 0) { M.erase(M.find(cval)); } else { P.erase(P.find(cval)); } d[x] += y; cval += y; if (cval + delta <= 0) { M.insert(cval); } else { P.insert(cval); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); d[b[i]] = i - pos[b[i]]; if (d[b[i]] <= 0) M.insert(d[b[i]]); else P.insert(d[b[i]]); } vector<int> all; for (int i = n; i > 0; i--) { add(b[i], -n); delta++; while (!M.empty()) { auto it = --M.end(); if (*it + delta > 0) { P.insert(*it); M.erase(it); } else { break; } } int ans = INF; if (!M.empty()) { ans = min(ans, -*M.rbegin() - delta); } if (!P.empty()) { ans = min(ans, delta + *P.begin()); } all.push_back(ans); } reverse(all.begin(), all.end()); for (auto x : all) printf("%d\n", x); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; inline int Get() { int res = 0, q = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') q = -1, ch = getchar(); while (ch >= '0' && ch <= '9') res = res * 10 + ch - '0', ch = getchar(); return res * q; } const double eps = 1e-12, pi = M_PI; const int oo = (int)2e9, mod = (int)1e9 + 7; const long long INF = (long long)1e17; const int N = 20; bool e[N + 10][N + 10]; int n, m, j, k; int d[N + 10], ord[N + 10]; bool v[N + 10]; bool cmp(const int &a, const int &b) { return (d[a] < d[b]) || (d[a] == d[b] && a < b); } int main() { scanf( "%d" "%d" "\n", &n, &m); for (int i = (1), end = (m); i <= end; ++i) scanf( "%d" "%d" "\n", &j, &k), e[j][k] = e[k][j] = 1; for (int i = (1), end = (n); i <= end; ++i) { ord[i] = i; for (int j = (1), end = (n); j <= end; ++j) if (i != j) d[i] += e[i][j]; } sort(ord + 1, ord + n + 1, cmp); for (int st = 1; st <= n; ++st) { int x = st; memset(v, 0, sizeof(v)); bool check = 1; for (int cnt = 1; cnt < n; ++cnt) { v[x] = 1; bool flag = 0; for (int i = 1; i <= n; ++i) { int nt = ord[i]; if (e[x][nt] && !v[nt]) { x = nt, flag = 1; break; } } if (!flag) { check = 0; break; } } if (check) return printf("Yes\n"), 0; } printf("No\n"); return 0; }
2,800
CPP
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Apr 4 13:56:55 2019 @author: xungao """ for i in range(int(input())): word = input() if len(word)<=10: print(word) else: print(word[0]+str(len(word)-2)+word[-1])
800
PYTHON3
from collections import deque, defaultdict, Counter from itertools import product, groupby, permutations, combinations from math import gcd, floor, inf from bisect import bisect_right, bisect_left r1, r2 = map(int, input().split()) c1, c2 = map(int, input().split()) d1, d2 = map(int, input().split()) rr = r1+r2 cc = c1+c2 dd = d1+d2 mn = min(r1, r2, c1, c2, d1, d2) mx = max(r1, r2, c1, c2, d1, d2) if (rr ++ cc)/2 != dd or mn < 3 or mx > 17: print(-1) else: top_left = (dd - d2 - r2 + c1)/2 if int(top_left) != top_left: print(-1) exit() else: top_left = int(top_left) top_right = r1 - top_left bottom_left = c1 - top_left bottom_right = r2 - bottom_left st = {top_left, top_right, bottom_left, bottom_right} mn = min(st) mx = max(st) if len(st) != 4 or mn < 1 or mx > 9: print(-1) else: print(top_left, top_right) print(bottom_left, bottom_right)
1,000
PYTHON3
#include <bits/stdc++.h> int main() { long long n, k; std::cin >> n >> k; std::cout << std::max(n + k - n % k, k) << '\n'; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; char s[55][55]; int n, m, k, adj, cnt, tot; int vis[55][55]; int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}; int f[55 * 55]; vector<pair<int, int> > v; void dfs(int x, int y) { cnt++; vis[x][y] = tot; if (x == 0 || x == n - 1 || y == 0 || y == m - 1) adj = 1; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || s[nx][ny] != '.' || vis[nx][ny]) continue; dfs(nx, ny); } } int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%s", s[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (s[i][j] == '.' && !vis[i][j]) { cnt = adj = 0; ++tot; dfs(i, j); if (!adj) v.push_back({cnt, tot}); } } sort(v.begin(), v.end()); int sum = 0; for (int i = v.size(), j = 0; i > k; i--, j++) { f[v[j].second] = 1; sum += v[j].first; } cout << sum << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (f[vis[i][j]]) putchar('*'); else putchar(s[i][j]); } puts(""); } return 0; }
1,600
CPP
''' Created on 02-Nov-2020 @author: HP ''' a=(int(input())) if a>2 and a%2==0: print("YES") else: print("NO")
800
PYTHON3
from math import gcd import sys t=1 def inp(): return sys.stdin.readline().strip() def lcm(a, b): return abs(a*b) // gcd(a, b) for _ in range(t): a, b=map(int, inp().split()) period= lcm(a,b) d=period//a m=period//b if(a>b): m-=1 else: d-=1 if d>m: print('Dasha') elif d<m: print('Masha') else: print('Equal')
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; map<string, int> mp; int main(void) { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { string s; int a; cin >> s >> a; mp[s] = max(mp[s], a); } int n = mp.size(); cout << n << endl; for (auto it = (mp).begin(); it != (mp).end(); ++it) { cout << it->first << ' '; int s = 0; for (auto iit = (mp).begin(); iit != (mp).end(); ++iit) if (it->second >= iit->second) ++s; if (100 * s >= 99 * n) cout << "pro" << endl; else if (100 * s >= 90 * n) cout << "hardcore" << endl; else if (100 * s >= 80 * n) cout << "average" << endl; else if (100 * s >= 50 * n) cout << "random" << endl; else cout << "noob" << endl; } return 0; }
1,400
CPP
from math import * from collections import * for u in range(int(input())): n=int(input()) x=n l=[] for i in range(2,int(sqrt(x))+1): while(n%i==0): l.append(i) n=n//i if(n>1): l.append(n) m=len(l) y=list(set(l)) y.sort() d=defaultdict(list) q=[] ll=[] for i in l: for j in range(len(y)): if(i%y[j]==0): d[y[j]].append(i) for i in d: ll.append([len(d[i]),d[i]]) ll.sort(reverse=True) f=ll[0][1] for i in range(1,len(ll)): for j in ll[i][1]: if(j not in f): f.append(j) l=f s=m p=1 for i in range(1,m): if(l[i]%l[i-1]!=0): s=i-1 break else: p=p*l[i-1] if(s==m): print(len(l)) print(*l) else: p=(l[:s]+[x//p]) print(len(p)) print(*p)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, flag; int ar[101][101], ars[101][101], c[101][101], q[101][101]; int fu() { int y = 1; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (ars[i][j] != c[i][j]) y = 0; return y; } void f() { flag = max(flag, fu()); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = q[i][j]; flag = max(flag, fu()); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = q[i][j]; flag = max(flag, fu()); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) q[i][j] = c[j][n - i - 1]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = q[i][j]; flag = max(flag, fu()); } int main() { cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { char a; cin >> a; if (a == 'X') ar[i][j] = 0; else ar[i][j] = 1; c[i][j] = ar[i][j]; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { char a; cin >> a; if (a == 'X') ars[i][j] = 0; else ars[i][j] = 1; } f(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = ar[n - i - 1][j]; f(); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) c[i][j] = ar[i][n - j - 1]; f(); if (flag) { cout << "Yes"; } else cout << "No"; return 0; }
1,400
CPP
#include <bits/stdc++.h> const int mod = 1e9 + 7; using namespace std; int n, a[1000007], b[1000007], tot; int ask() { cout << "? "; for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl, tot++; int out = 0; cin >> out; return out; } int main() { cin >> n; for (int i = 1; i <= n; i++) a[i] = 1; int p = 0; for (int i = 1; i <= n; i++) { a[n]++; if (i != n) p = ask(); else p = 0; if (!p) { b[n] = n - i + 1; for (int j = 1; j <= n - 1; j++) if (b[j]) b[j] += b[n]; break; } else b[p] = i; } a[n] = 1; for (int i = 1; i <= b[n] - 1; i++) { for (int j = 1; j <= n - 1; j++) if (b[j]) a[j] = n; else a[j] = i + 1; b[ask()] = b[n] - i; } cout << "! "; for (int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl; }
1,800
CPP
t = int(input()) #print(t) for i in range(t): n = int(input()) #print(n) arr = [] for m in range(n): s1 = str(input()) #print(s1) s = list(map(int, s1)) arr.append(s) f_error = False for i in range(n): for j in range(n): if arr[i][j] == 1: if i != n-1 and j != n-1: if arr[i+1][j] != 1 and arr[i][j+1] != 1: f_error = True if f_error: print('NO') else: print("YES")
1,300
PYTHON3
t = int(input()) s = [int(x) for x in input().split()] l = [] for i in range(t): if s[i] == 1: l.append(i) print(len(l)) l.append(t) for i in range(1, len(l)): print(l[i] - l[i-1], end = ' ')
800
PYTHON3
x = [int(s) for s in input().split()] x.sort() print(x[3]-x[0],x[3]-x[1],x[3]-x[2])
800
PYTHON3
a,b,c,d=input().split(" ") a=int(a) b=int(b) c=int(c) d=int(d) p=a/b q=c/d f=p*((1)/(1-(1-p)*(1-q))) print(f)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); const long long infl = 3e18 + 100; const int inf = 1e9 + 100; const int nmax = 1e3 + 10; int di[4] = {0, -1, 0, 1}; int dj[4] = {-1, 0, 1, 0}; int n, m, query; vector<string> g; int shurutime[nmax][nmax]; bool vis[nmax][nmax]; bool thikase(int i, int j) { if (i < n and i >= 0 and j < m and j >= 0) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> query; g.resize(n); for (auto &x : g) cin >> x; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { for (int k = 0; k < 4; k++) { int i2 = di[k] + i, j2 = j + dj[k]; if (thikase(i2, j2)) { if (g[i][j] == g[i2][j2]) { shurutime[i][j] = shurutime[i2][j2] = 1; } } } } queue<pair<int, long long> > q; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (shurutime[i][j]) { q.push({i, j}); } } while (q.size()) { pair<int, long long> z = q.front(); q.pop(); int i = z.first, j = z.second; if (vis[i][j]) continue; vis[i][j] = true; for (int k = 0; k < 4; k++) { int i2 = di[k] + i, j2 = j + dj[k]; if (thikase(i2, j2)) { if (!vis[i2][j2] and shurutime[i2][j2] == 0) { shurutime[i2][j2] = shurutime[i][j] + 1; q.push({i2, j2}); } } } } while (query--) { int i, j; long long taim; cin >> i >> j >> taim; i--, j--; if (shurutime[i][j] == 0) cout << g[i][j] << "\n"; else { if (taim < shurutime[i][j]) cout << g[i][j] << "\n"; else { if ((taim - shurutime[i][j]) % 2) cout << g[i][j] << "\n"; else cout << char((g[i][j] - '0') ^ 1 + '0') << "\n"; } } } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for (int k = 0; k < t; k++) { int n, m; cin >> n >> m; vector<vector<int>> a(max(n, m), vector<int>(max(n, m))); vector<vector<int>> id(n * m, vector<int>(2)); vector<vector<int>> c(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; id[a[i][j] - 1][1] = j; } } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; id[a[i][j] - 1][0] = j; } } for (int k = 0; k < n * m; k++) { int i = id[k][0], j = id[k][1]; c[i][j] = k + 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << c[i][j] << ' '; } cout << '\n'; } } }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 9; pair<int, int> p[maxn]; set<pair<int, int> > a; void f(int l, int r) { if (r - l < 2) return; int m = l + r; m /= 2; for (int i = l; i < r; i++) { a.insert(pair<int, int>(p[m].first, p[i].second)); } f(l, m); f(m, r); } int main() { int n; cin >> n; int i; for (i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; a.insert(p[i]); } sort(p, p + n); f(0, n); cout << a.size() << endl; for (auto i : a) cout << i.first << " " << i.second << endl; }
2,300
CPP
d = {} n = int(input()) for _ in range(n): input_string = input() try: print(input_string + str(d[input_string])) d[input_string] += 1 except: print("OK") d[input_string] = 1
1,300
PYTHON3
for _ in range(int(input())): x,y=[int(x) for x in input().split()] a,b=[int(x) for x in input().split()] s=0 if b<(2*a): if x>y: s+=(y*b) x-=y y=0 else: s+=(x*b) y-=x x=0 else: s+=(x*a) s+=(y*a) x=0 y=0 if x!=0: s+=(x*a) if y!=0: s+=(y*a) print(s)
1,000
PYTHON3
a,b,c,d = int(input()),int(input()),int(input()),int(input()) if c>0: if a==d and a-1>=0: print(1) else: print(0) else: if a==d: print(1) else: print(0)
1,100
PYTHON3
for _ in range(int(input())): n,j,k=map(int,input().split()) a=max(list(map(int,input().split()))) b=max(list(map(int,input().split()))) print("YES" if a>b else "NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; string s; pair<int, int> conv[] = {make_pair(3, 1), make_pair(0, 0), make_pair(0, 1), make_pair(0, 2), make_pair(1, 0), make_pair(1, 1), make_pair(1, 2), make_pair(2, 0), make_pair(2, 1), make_pair(2, 2)}; bool isval(int x, int y) { bool found = false; for (int i = 0; i < 10; ++i) if (conv[i] == make_pair(x, y)) found = true; return found; } vector<pair<int, int> > v; int main() { cin >> n >> s; for (int i = 0; i < s.length(); ++i) { v.push_back(conv[s[i] - '0']); } int cnt = 0; for (int i = -2; i < 2; ++i) { for (int j = -3; j < 3; ++j) { bool valid = true; for (int k = 0; k < v.size(); ++k) { int dx = v[k].first + i; int dy = v[k].second + j; if (!isval(dx, dy)) { valid = false; break; } } if (valid) cnt++; } } if (cnt == 1) printf("YES\n"); else printf("NO\n"); }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, neg = 0, sum = 0, mi = 1001; cin >> n; for (int i = 0; i < 2 * n - 1; i++) { int t; cin >> t; if (t < 0) neg++; if (abs(t) < mi) mi = abs(t); sum += abs(t); } if (n % 2 == 1 || neg % 2 == 0) cout << sum << endl; else cout << sum - 2 * mi << endl; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; char tab[109][109]; int h, w; bool isBomb(int y, int x) { return y >= 0 && y < h && x >= 0 && x < w && tab[y][x] == '*'; } int getCalc(int y, int x) { int res = 0; for (int a = -1; a < 2; a++) { for (int b = -1; b < 2; b++) { if (a == 0 && b == 0) continue; if (isBomb(y + a, x + b)) res++; } } return res; } int main() { cin >> h >> w; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { cin >> tab[y][x]; if (tab[y][x] == '.') tab[y][x] = '0'; } } bool ok = 1; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (tab[y][x] != '*' && tab[y][x] - '0' != getCalc(y, x)) { ok = 0; break; } } } if (ok) { cout << "YES"; } else { cout << "NO"; } cout << endl; }
1,100
CPP
n = int(input()) var = input() var = list(map(int, var.split())) minim = 1001 pos_min = pos_max = maxim = 0 for i in range(len(var)): if var[i] > maxim: maxim = var[i] pos_max = i if var[i] <= minim: minim = var[i] pos_min = i if pos_min < pos_max: print(len(var)-2 - pos_min + pos_max) else: print(len(var)-1 - pos_min + pos_max)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> p; int pos[1010], vis[1010], n, t; int main() { scanf("%d", &t); while (t--) { p.clear(); memset(vis, 0, sizeof(vis)); memset(pos, 0, sizeof(pos)); scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); p.push_back(x); pos[x] = i; } for (int i = 1; i <= n; i++) { int flag = 1; while (flag == 1) { if (pos[i] > 0 && vis[pos[i] - 1] == 0) { vis[pos[i] - 1] = 1; int now = pos[i], pnow = pos[i] - 1; swap(p[now], p[pnow]); swap(pos[p[now]], pos[p[pnow]]); } else flag = 0; } vis[pos[i]] = 1; } for (int i = 0; i < p.size(); i++) cout << p[i] << " "; cout << endl; } return 0; }
1,400
CPP
a = list(map(int, input().split())) if sum(a) == 0: print(-1) elif sum(a) % len(a) == 0: print(sum(a) // len(a)) else: print(-1)
1,100
PYTHON3
class Div2_411A(): def solution(l, r): if l == r: print(l) else: print(2) l, r = [int(x) for x in input().split()] Div2_411A.solution(l, r)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; std::string to_string(std::string s) { return '"' + s + '"'; } std::string to_string(const char* s) { return to_string((std::string)s); } std::string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> std::string to_string(std::pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> std::string to_string(A v) { using ::to_string; using std::to_string; bool first = true; std::string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { std::cerr << std::endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { using ::to_string; using std::to_string; std::cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) \ std::cerr << __FUNCTION__ << "(" << __LINE__ << ")" \ << "[" << #__VA_ARGS__ << "]:", \ debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif const int mod = 998244353; inline int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } inline int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } inline int mul(int a, int b) { return (int)((long long)a * b % mod); } template <typename T> inline T inv(T a, T p = mod) { a %= p; if (a < 0) { a += p; } T b = p; T u = 0; T v = 1; while (a) { T t = b / a; b -= t * a; swap(a, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) { u += p; } return u; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; if (n == 1) { cout << 5 << endl; return 0; } vector<int> f(n + 1); vector<int> sum_f(n + 1); vector<int> g(n + 1); int sum = 0; f[0] = 1; f[1] = 2; sum_f[0] = 1; sum_f[1] = 3; g[1] = 1; for (int i = 2; i <= n; ++i) { f[i] = f[i - 1]; f[i] = add(f[i], mul(f[i - 1], sum_f[i - 2])); f[i] = add(f[i], mul(inv(2), mul(f[i - 1], add(f[i - 1], 1)))); sum_f[i] = add(sum_f[i - 1], f[i]); g[i] = sub(f[i], f[i - 1]); } sum = add(f[n], mul(inv(2), mul(f[n - 1], mul(sum_f[n - 2], add(sum_f[n - 2], 1))))); sum = add(sum, mul(inv(2), mul(sum_f[n - 2], mul(f[n - 1], add(f[n - 1], 1))))); sum = add( sum, mul(inv(6), mul(f[n - 1], mul(add(f[n - 1], 1), add(f[n - 1], 2))))); sum = mul(sum, 2); sum = sub(sum, 1); int sum_t = 0; for (int i = 0; i <= n - 1; ++i) { sum_t = add(sum_t, mul(sub(f[i], 1), g[n - 1 - i])); } sum = add(sum, sum_t); cout << sum << endl; return 0; }
2,900
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:16000000") const int INF = 1e9; const double EPS = 1e-8; const double PI = 2 * acos(0.); struct edge { int v, c, f, rev, cost; edge(int vv, int cc, int ff, int revrev, int costcost) { v = vv; c = cc; f = ff; rev = revrev; cost = costcost; } }; vector<edge> v[10000]; void add_edge(int u1, int u2, int c, int cost) { v[u1].push_back(edge(u2, c, c, v[u2].size(), cost)); v[u2].push_back(edge(u1, 0, 0, v[u1].size() - 1, -cost)); } int d[2][4] = {0, 0, 1, -1, 1, -1, 0, 0}; int dist[10000]; bool color[10000]; pair<int, int> par[10000]; const int T = 1 << 17; int Q[T]; int n; int fb(int s, int t) { memset(dist, 0x3f, sizeof(dist)); memset(color, 0, sizeof(color)); dist[s] = 0; par[s] = make_pair(-1, -1); int l = 0; int r = 1; Q[0] = s; while (l != r) { int u = Q[l]; color[u] = false; l = (l + 1) & (T - 1); for (int j = 0; j < v[u].size(); ++j) { if (dist[v[u][j].v] > dist[u] + v[u][j].cost && v[u][j].f > 0) { dist[v[u][j].v] = dist[u] + v[u][j].cost; par[v[u][j].v] = make_pair(u, j); if (!color[v[u][j].v]) { Q[r] = v[u][j].v; r = (r + 1) & (T - 1); color[v[u][j].v] = true; } } } } int cur = t; while (cur > 0) { int i = par[cur].first; int j = par[cur].second; v[i][j].f -= 1; v[v[i][j].v][v[i][j].rev].f += 1; cur = i; } return dist[t]; } int a[100][100]; int num[100][100]; int main() { int n, m; scanf("%d%d", &n, &m); int k = 2; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf("%d", &a[i][j]); num[i][j] = k++; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if ((i + j) % 2 == 0) { add_edge(0, num[i][j], 1, 0); } else { add_edge(num[i][j], 1, 1, 0); } for (int dir = 0; dir < 4; ++dir) { int x = i + d[0][dir]; int y = j + d[1][dir]; if (x < 0 || x >= n || y < 0 || y >= m) continue; if ((i + j) % 2 == 0) add_edge(num[i][j], num[x][y], 1, a[i][j] == a[x][y] ? -1 : 0); else add_edge(num[x][y], num[i][j], 1, a[i][j] == a[x][y] ? -1 : 0); } } } int ans = 0; for (int i = 0; i < n * m / 2; ++i) ans += fb(0, 1); printf("%d\n", (n * m / 2 + ans)); return 0; }
2,300
CPP
def getFloor(n, x): if n == 1 or n == 2: return 1 n -= 3 return n // x + 2 T = int(input()) for t in range(T): n, x = [int(i) for i in input().split(' ')] ans = getFloor(n, x) print(ans)
800
PYTHON3
n =int(input()) q = [] ans = [] b = [[] for x in range(n+1)] for i in range(n-2): q.append(list(map(int,input().split()))) for j in q[i]: b[j].append(i) for i in range(1,n+1): if len(b[i])==1: break ans.append(i) for _ in range(n-1): for e in b[i]: for j in q[e]: if(len(ans)==1 and j not in ans and len(b[j])==2 ) or (len(ans)>1 and j not in ans[-3:] and ans[-1] in q[e] and ans[-2] in q[e]): ans.append(j) i = j print(*ans)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int d, l, v1, v2; cin >> d >> l >> v1 >> v2; cout.setf(ios::fixed, ios::floatfield); cout.precision(20); cout << (double)(l - d) / (v1 + v2); return 0; }
800
CPP
t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,input().split())) if n<=2: print(0) else: diffs=[] for i in range(1,n): if l[i]>l[i-1]: diffs.append(1) elif l[i]==l[i-1]: diffs.append(0) else: diffs.append(-1) i=len(diffs)-1 while diffs[i]!=1 and i>=0: i-=1 while diffs[i]!=-1 and i>=0: i-=1 print(i+1)
1,200
PYTHON3
#include <bits/stdc++.h> const int maxn = 500005; int a[maxn], b[maxn]; std::pair<int, int> t[maxn << 2]; void build(int u, int v, int node) { if (u == v) t[node] = {b[u], u}; else { int m = (u + v) >> 1; build(u, m, node << 1); build(m + 1, v, node << 1 | 1); t[node] = std::max(t[node << 1], t[node << 1 | 1]); } } void del(int p, int u, int v, int node) { if (u == v) t[node] = {0, p}; else { int m = (u + v) >> 1; if (p <= m) del(p, u, m, node << 1); else del(p, m + 1, v, node << 1 | 1); t[node] = std::max(t[node << 1], t[node << 1 | 1]); } } std::pair<int, int> query(int l, int r, int u, int v, int node) { if (l > r) return {0, 0}; if (l <= u && v <= r) return t[node]; std::pair<int, int> ans = {0, 0}; int m = (u + v) >> 1; if (l <= m) ans = std::max(ans, query(l, r, u, m, node << 1)); if (m + 1 <= r) ans = std::max(ans, query(l, r, m + 1, v, node << 1 | 1)); return ans; } int n; void read() { scanf("%d", &n); for (int i = 1; i <= n; i++) b[i] = n + 1; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] != -1) b[a[i]] = i; else a[i] = n + 1; } } bool vis[maxn]; std::vector<int> topological_order; void dfs(int idx) { vis[idx] = true; del(idx, 1, n, 1); if (b[idx] != n + 1 && !vis[b[idx]]) dfs(b[idx]); while (1) { auto tmp = query(1, a[idx] - 1, 1, n, 1); if (tmp.first > idx) dfs(tmp.second); else break; } topological_order.push_back(idx); } int ans[maxn]; void solve() { build(1, n, 1); for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); for (int i = 0; i < n; i++) ans[topological_order[i]] = i + 1; for (int i = 1; i <= n; i++) printf("%d%c", ans[i], " \n"[i == n]); } int main() { read(); solve(); }
3,000
CPP
n = int(input("")) problems = [] count = 0 for a in range(n): problems.append(input("")) for b in range(n): sum = int(problems[b][0]) + int(problems[b][2]) + int(problems[b][4]) if (sum > 1): count = count + 1 print(count)
800
PYTHON3
def max(a,b,c): if a>b: b,a=a,b if b>c: b,c=c,b if a>b: a,b=b,a return a,b,c input() A=set(map(int,input().split())) A=list(A) if len(A)==1: print(0) elif len(A)==2: if abs(A[0]+A[1])%2==0: print(abs(A[0]-A[1])//2) else: print(abs(A[0]-A[1])) elif len(A)==3: a,b,c=max(A[0],A[1],A[2]) if (c+a)%2==0 and (c+a)//2==b: print((c-a)//2) else: print(-1) else: print(-1)
1,200
PYTHON3
k,n,w=map(int,input().split()) p=(w*(w+1)/2) t=(k*p) l=int(t) d=0 if((l-n)>0): print(l-n) else: print(d)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MN = 1e5 + 111, MT = 7001; bitset<MT> r[MN]; bitset<MT> prep[MT], prepmask[MT]; int sq[MT]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 1; i < MT; ++i) { for (int j = i; j < MT; j += i) { prep[j][i] = 1; } } for (int i = 2; i < MT; i++) { for (int k = 2; k * k < MT; ++k) { if (i % (k * k) == 0) sq[i] = 1; } } for (int i = 1; i < MT; ++i) { for (int j = i; j < MT; j += i) { if (sq[j / i]) continue; prepmask[i][j] = 1; } } int n, q; cin >> n >> q; for (int i = 0; i < q; ++i) { int t; cin >> t; if (t == 1) { int first, v; cin >> first >> v; first--; r[first] = prep[v]; } else if (t == 2) { int first, second, z; cin >> first >> second >> z; first--; second--; z--; r[first] = r[second] ^ r[z]; } else if (t == 3) { int first, second, z; cin >> first >> second >> z; first--; second--; z--; r[first] = r[second] & r[z]; } else { int first, v; cin >> first >> v; first--; cout << (((r[first] & prepmask[v]).count()) & 1); } } }
2,500
CPP
n=int(input()) count=0 while(n>0): if n>=5: n=n-5 count=count+1 elif n < 5 and n==4: n=n-4 count=count+1 elif n < 5 and n==3: n=n-3 count=count+1 elif n <3 and n==2: n=n-2 count=count+1 else: n=n-1 count=count+1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 100; int d[maxn], t[maxn]; vector<int> v[maxn]; int main() { int n, k; cin >> n >> k; for (int i = 1; i < maxn; ++i) { for (int j = i * 2; j < maxn; j += i) { d[j]++; } } int num = 0; for (int i = 1; i <= n; ++i) { num += d[i]; } if (num < k) { cout << "No" << endl; return 0; } cout << "Yes" << endl; if (k == 0) { cout << "1\n1\n"; return 0; } int m = 0; num = 0; while (num < k) { m++; num += d[m]; } num -= k; set<int> st; for (int i = 1; i <= m; ++i) { st.insert(i); } set<int> s; for (int i = 1; i <= m; ++i) { if (d[i] == 1) { t[m / i]++; s.insert(m / i); v[m / i].push_back(i); } } while (num) { auto it = s.upper_bound(num); it--; int a = *it; t[a]--; if (!t[a]) { s.erase(a); } num -= a; st.erase(v[a].back()); v[a].pop_back(); } cout << st.size() << endl; for (auto x : st) { cout << x << ' '; } cout << endl; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; pair<int, int> events[200000]; int a[200001]; bool visit[200001]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int c; scanf("%d", &c); if (c == 1) scanf("%d%d", &events[i].first, &events[i].second); else { events[i].first = 0; scanf("%d", &events[i].second); } } int m = 0; for (int i = q - 1; i >= 0; i--) { if (events[i].first) { if (visit[events[i].first]) continue; visit[events[i].first] = true; a[events[i].first] = max(m, events[i].second); } else m = max(m, events[i].second); } for (int i = 1; i <= n; i++) if (!visit[i]) a[i] = max(a[i], m); for (int i = 1; i <= n; i++) printf("%d ", a[i]); puts(""); }
1,600
CPP
t=int(input()) while(t!=0): a=list(map(int,input().split(" "))) a.sort() b=a.pop() c=a[0]+a[1] if(b>=c): print(c) else: d=c-b e=d//2 print(b+e) t=t-1
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937_64 gen(gen_seed); bool comp(const std::pair<long long, long long>& x, const std::pair<long long, long long>& y) { return x.first * y.second < x.second * y.first; } int main() { int n; scanf("%d\n", &n); long long ans = 0; std::vector<std::pair<long long, long long> > y; for (int i = 0; i < n; i++) { string s; getline(cin, s); long long a = 0; long long b = 0; for (auto x : s) { if (x == 's') a++; else if (x == 'h') { ans += a; b++; } } y.push_back(make_pair(a, b)); } sort((y).begin(), (y).end(), comp); reverse((y).begin(), (y).end()); long long a = 0; for (auto x : y) { ans += x.second * a; a += x.first; } cout << ans; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 10; int a[10][MAXN]; int b[10][MAXN]; int dp[MAXN] = {}; int n, m; bool check(int x, int y) { for (int i = 0; i < m; i++) { if (b[i][x] < b[i][y]) return 0; } return 1; } int main() { scanf("%d%d", &n, &m); int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &a[i][j]); b[i][a[i][j]] = j; } } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (check(a[0][i], a[0][j])) dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } printf("%d\n", ans + 1); }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, sum; cin >> n; sum = 0; for (int i = 0; i < n; i++) { string x; cin >> x; if (x == "Tetrahedron") sum += 4; else if (x == "Cube") sum += 6; else if (x == "Octahedron") sum += 8; else if (x == "Dodecahedron") sum += 12; else if (x == "Icosahedron") sum += 20; } cout << sum << endl; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); long long a, b, c, r; long long t; cin >> t; ++t; while (--t) { cin >> a >> b >> c >> r; if (a > b) { a += b; b = a - b; a -= b; } long long sum = 0; if ((a <= c) && (b >= c)) { sum += max((long long)0, (c - a) - r); sum += max((long long)0, (b - c) - r); } else { if (c <= a) { if (a - c >= r) sum += b - a; else sum += max((long long)0, (b - c) - r); } else { if (c >= b) { if (c - b >= r) { sum += b - a; } else sum += max((long long)0, (c - a) - r); } } } cout << sum << "\n"; } }
900
CPP
n=int(input()) if n%2==0: print(n*n//2) for i in range(0,n): if i%2==0: k='' for j in range(0,n): if j%2==0: k+='C' else: k+='.' print(k) else: k='' for j in range(0,n): if j%2==0: k+='.' else: k+='C' print(k) else: print((n//2+1)**2+(n//2)**2) for i in range(0,n): if i%2==0: k='' for j in range(0,n): if j%2==0: k+='C' else: k+='.' print(k) else: k='' for j in range(0,n): if j%2==0: k+='.' else: k+='C' print(k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const long long INF = 0x7fffffffffffffff; template <typename A, typename B> ostream &operator<<(ostream &s, const pair<A, B> &p) { return s << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream &operator<<(ostream &s, const vector<T> &c) { s << "[ "; for (auto it : c) s << it << " "; s << "]"; return s; } template <typename T> ostream &operator<<(ostream &o, const set<T> &st) { o << "{"; for (auto it = st.begin(); it != st.end(); it++) o << (it == st.begin() ? "" : ", ") << *it; return o << "}"; } template <typename T1, typename T2> ostream &operator<<(ostream &o, const map<T1, T2> &make_pair) { o << "{"; for (auto it = make_pair.begin(); it != make_pair.end(); it++) { o << (it == make_pair.begin() ? "" : ", ") << it->first << ":" << it->second; } o << "}"; return o; } int n; long long k; int mn[100005][40], nxt[100005][40]; long long sum[100005][40]; int main() { cin >> n >> k; for (int i = 0; i < n; i++) scanf("%d", &nxt[i][0]); for (int i = 0; i < n; i++) scanf("%lld", &sum[i][0]), mn[i][0] = sum[i][0]; for (int i = 1; i < 40; i++) { for (int j = 0; j < n; j++) { nxt[j][i] = nxt[nxt[j][i - 1]][i - 1]; sum[j][i] = sum[j][i - 1] + sum[nxt[j][i - 1]][i - 1]; mn[j][i] = min(mn[j][i - 1], mn[nxt[j][i - 1]][i - 1]); } } for (int i = 0; i < n; i++) { long long kk = k; int m = 1e9; long long s = 0; int cur = i; for (int j = 39; kk; j--) if ((1LL << j) <= kk) { kk -= (1LL << j); s += sum[cur][j]; m = min(m, mn[cur][j]); cur = nxt[cur][j]; } assert(kk == 0); printf("%lld %d\n", s, m); } }
2,100
CPP
from sys import stdin MAX = 200000 + 5 a = ['' for _ in range(MAX)] cur = MAX - 1 d = {} for _ in range(int(stdin.readline())): s = stdin.readline().rstrip() if s in d: a[d[s]] = '' a[cur] = s d[s] = cur cur -= 1 print('\n'.join(filter(lambda x: x, a)))
1,200
PYTHON3
n = int(input()) for i in range(n): k, x =list(map(int,input().split())) res = x + (k-1)*9 print(res) # digital root = 1 + ((num - 1) % 9)
1,000
PYTHON3
N = int(input()) for i in range(N): word = input() if len(word) > 10: print(f"{word[0]}{len(word)-2}{word[-1]}") else: print(word)
800
PYTHON3
#include <bits/stdc++.h> const long long MOD = 1000000007; bool solve(const std::vector<std::vector<long long> >& graph, long long& t) { std::vector<bool> visited(graph.size(), false); std::stack<long long> q; q.push(1); visited[1] = true; while (!q.empty()) { long long temp = q.top(); if (temp == t) { return true; } q.pop(); for (long long i = 1; i < graph[temp].size(); ++i) { long long val = graph[temp][i]; if (val == t) { return true; } if (!visited[val]) { visited[val] = true; q.push(val); } } } return false; } int main() { long long n, t; std::cin >> n >> t; std::vector<long long> a(n, 0); for (int i = 1; i < n; ++i) { std::cin >> a[i]; } long long sum = 1; while (sum < t) { sum = sum + a[sum]; } if (sum == t) { std::cout << "YES" << std::endl; } else { std::cout << "NO" << std::endl; } return 0; }
1,000
CPP
n, m, x, y = list(map(int, input().split())) for j in range(y, m+1): print(x, j) for j in range(y-1, 0, -1): print(x, j) direction = "r" for i in range(x+1, n+1): if direction == "r": for j in range(1, m+1): print(i, j) direction = "l" else: for j in range(m, 0, -1): print(i, j) direction = "r" for i in range(1, x): if direction == "r": for j in range(1, m+1): print(i, j) direction = "l" else: for j in range(m, 0, -1): print(i, j) direction = "r"
1,100
PYTHON3
from sys import stdin input = stdin.readline n = int(input()) force = [list(map(int,input().split())) for _ in range(n)] nx, ny, nz = 0, 0, 0 for f in force: x, y, z = f nx += x ny += y nz += z if not nx and not ny and not nz: print("YES") else: print("NO")
1,000
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int,input().split())) a = 0 b = 0 for i in range(len(arr)): if arr[i]%2 != i%2: if i%2 == 0: a+=1 else: b+=1 if a != b: print(-1) else: print(a)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2001; const int M = 4001; const int MOD = 1e9 + 7; vector<int> comps; int dpFullPref[N][2 * M]; int dpFullSuf[N][2 * M]; int dpPref[N][2 * M]; int dpSuf[N][2 * M]; int solveFullPref(int idx, int bal) { if (idx < 0) { return bal == M; } int &ret = dpFullPref[idx][bal]; if (ret != -1) return ret; ret = solveFullPref(idx - 1, bal + comps[idx]); ret = (ret + solveFullPref(idx - 1, bal - comps[idx])) % MOD; return ret; } int solveFullSuf(int idx, int bal) { if (idx == comps.size()) { return bal == M; } int &ret = dpFullSuf[idx][bal]; if (ret != -1) return ret; ret = solveFullSuf(idx + 1, bal + comps[idx]); ret = (ret + solveFullSuf(idx + 1, bal - comps[idx])) % MOD; return ret; } int solvePref(int idx, int bal) { if (idx < 0) { return bal == M; } int &ret = dpPref[idx][bal]; if (ret != -1) return ret; ret = solvePref(idx - 1, bal + comps[idx]); ret = (ret + solvePref(idx - 1, bal - comps[idx])) % MOD; ret = (ret + solvePref(idx - 1, bal)) % MOD; return ret; } int solveSuf(int idx, int bal) { if (idx == comps.size()) { return bal == M; } int &ret = dpSuf[idx][bal]; if (ret != -1) return ret; ret = solveSuf(idx + 1, bal + comps[idx]); ret = (ret + solveSuf(idx + 1, bal - comps[idx])) % MOD; ret = (ret + solveSuf(idx + 1, bal)) % MOD; return ret; } int n, m; vector<pair<int, int>> edges; int pset[N]; int finds(int x) { return pset[x] < 0 ? x : pset[x] = finds(pset[x]); } void join(int x, int y) { x = finds(x); y = finds(y); if (x == y) return; pset[x] += pset[y]; pset[y] = x; } void read() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d %d", &u, &v); edges.push_back({u, v}); } } void initComps() { for (int i = 1; i <= n; i++) { pset[i] = -1; } for (auto it : edges) { if (it.first == 1 || it.second == 1) continue; join(it.first, it.second); } for (int i = 2; i <= n; i++) { if (finds(i) == i) { comps.push_back(-pset[i] + 1); } } } void prepare() { memset(dpFullPref, -1, sizeof dpFullPref); memset(dpFullSuf, -1, sizeof dpFullSuf); memset(dpSuf, -1, sizeof dpSuf); memset(dpPref, -1, sizeof dpPref); initComps(); } int calcStopMiddle() { int ret = 0; int tot = 0; for (int i = 0; i < comps.size(); i++) { int cnt = comps[i]; for (int j = 1; j < cnt; j++) { for (int k = -tot; k <= tot; k++) { { int curBal = j - (cnt - j); int neededBal = curBal + k; int prefWay = solvePref(i - 1, k + M); int sufWay = solveSuf(i + 1, -neededBal + M); int totalWays = 1ll * prefWay * sufWay % MOD; ret = (ret + totalWays) % MOD; } if (cnt - j > 1) { int curBal = j - (cnt - j - 1); int neededBal = curBal + k; int prefWay = solvePref(i - 1, k + M); int sufWay = solveSuf(i + 1, -neededBal + M); int totalWays = 1ll * prefWay * sufWay % MOD; ret = (ret + totalWays) % MOD; } } } vector<int> spec = {0, cnt - 1}; for (int j : spec) { for (int k = -tot; k <= tot; k++) { int curBal = j - (cnt - j - 1); int neededBal = curBal + k; int prefWay = solveFullPref(i - 1, k + M); int sufWay = solveFullSuf(i + 1, -neededBal + M); int totalWays = 1ll * prefWay * sufWay % MOD; ret = (ret + totalWays) % MOD; } } tot += cnt; } ret = 2ll * ret % MOD; return ret; } int work() { int ret = 0; ret = (ret + solveFullPref(comps.size() - 1, M)) % MOD; ret = (ret + calcStopMiddle()) % MOD; return ret; } int main() { read(); prepare(); printf("%d\n", work()); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; void solve() { int n; cin >> n; pair<int, int> p[1005]; for (int i = 0; i < n; i++) { cin >> p[i].first; p[i].second = i; } sort(p, p + n); reverse(p, p + n); int ans = 0; for (int i = 0; i < n; i++) ans += i * p[i].first + 1; cout << ans << endl; for (int i = 0; i < n; i++) cout << p[i].second + 1 << " "; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int n, m; int fuck[(3 * 100000 + 1)]; int arr[(3 * 100000 + 1)]; long long ans; int cmp(pair<int, int> l, pair<int, int> r) { if (l.second != r.second) return l.second < r.second; return l.first < r.first; } int main() { cin.tie(0); cin.sync_with_stdio(0); int x, y; cin >> n >> m; for (int i = 1; i < n + 1; i++) { cin >> x; arr[x] = i; fuck[i] = (3 * 100000 + 1); } for (int i = 0; i < m; i++) { cin >> x >> y; int tx = min(arr[x], arr[y]); int ty = max(arr[x], arr[y]); fuck[tx] = min(ty, fuck[tx]); } vector<pair<int, int> > qq; for (int i = 1; i < n + 1; i++) if (fuck[i] != (3 * 100000 + 1)) qq.push_back(pair<int, int>(i, fuck[i])); sort(qq.begin(), qq.end(), cmp); int cnt = 1, idx = 0; while (cnt < n + 1 && idx < qq.size()) { ans += qq[idx].second - cnt; if (qq[idx].first == cnt) { idx++; while (idx < qq.size() && cnt >= qq[idx].first) idx++; } cnt++; } while (cnt < n + 1) { ans += n + 1 - cnt; cnt++; } cout << ans << endl; }
1,800
CPP
a,b=map(int,input().split()) p1=p2=draw=0 for i in range(1,7): if abs(a-i)<abs(b-i): p1+=1 elif abs(a-i)>abs(b-i): p2+=1 else: draw+=1 print(p1,draw,p2,end=" ")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[100005]; int cnt[26]; int main() { while (~scanf("%s", s)) { memset(cnt, 0, sizeof(cnt)); int len = strlen(s); if (len < 4) { puts("No"); continue; } for (int i = 0; i < len; ++i) cnt[s[i] - 'a']++; int p = 0; vector<int> vec; for (int i = 0; i < 26; ++i) if (cnt[i] > 0) { p++; vec.push_back(cnt[i]); } if (p > 4 || p == 1) puts("No"); else if (p == 4 || p == 3) { puts("Yes"); } else { if (vec[0] == 1 || vec[1] == 1) puts("No"); else puts("Yes"); } } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; long long f[200][200][200]; pair<long long, long long> a[200]; int n; const long long max_num = 1234567891011; bool check(long long res) { for (int i = 0; i < 200; i++) for (int j = 0; j < 200; j++) for (int k = 0; k < 200; k++) f[i][j][k] = max_num; f[0][0][0] = 0; for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) for (int k = 0; k + j <= i; k++) { if (f[i][j][k] == max_num) continue; if (a[i].first == a[i + 1].first) { f[i + 1][j][k + 1] = min(f[i + 1][j][k + 1], f[i][j][k] + a[i + 1].first - a[i + 1].second * res); if (j > 0) f[i + 1][j - 1][k] = min(f[i + 1][j - 1][k], f[i][j][k]); } else { f[i + 1][j + k][1] = min(f[i + 1][j + k][1], f[i][j][k] + a[i + 1].first - a[i + 1].second * res); if (j + k > 0) f[i + 1][j + k - 1][0] = min(f[i + 1][j + k - 1][0], f[i][j][k]); } } for (int i = 0; i <= n; i++) for (int j = 0; j + i <= n; j++) if (f[n][i][j] <= 0) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].first; for (int i = 1; i <= n; i++) cin >> a[i].second, a[i].first *= 1000; sort(a + 1, a + n + 1, greater<pair<long long, long long> >()); long long l = 0, r = 1e15, mid, ans; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } cout << ans; }
2,500
CPP
N = int(input()) def isLucky(n): Luck = set(str(47)) n_str = set(str(n)) return n_str.issubset(Luck) def isalmostLucky(n): if n < 3: return 0 else: LN = list() for x in range(1,n+1): if isLucky(x): LN.append(x) for x in LN: if n % x == 0: return 1 break return 0 if isalmostLucky(N): print('YES') else: print('NO')
1,000
PYTHON3
import sys def data(): return sys.stdin.buffer.readline().strip() out=sys.stdout.write def mdata(): return map(int, data().split()) for t in range(int(data())): n,l,r=mdata() a=l for i in range(1,n+1): if 2*(n-i)<=a: a-=2*(n-i) else: break cnt=l ans=[] if l%2==0: if a==0: ans.append(str(n)) else: ans.append(str(i+a//2)) cnt+=1 k=i+a//2+1 for j in range(i,n): while cnt<=r: ans.append(str(j)) cnt+=1 if cnt>r: break ans.append(str(k)) k+=1 cnt+=1 if k==n+1: k=j+2 break if r==n*(n-1)+1: ans.append(str(1)) out(' '.join(ans)+'\n')
1,800
PYTHON3
n,m = map(int, input().split()) t=ret=0 while n > 0: n-=1 t+=1 if (t==m): n+=1 t=0 ret+=1 print(ret)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, size_t size> ostream& operator<<(ostream& os, const array<T, size>& arr) { os << '{'; string sep; for (const auto& x : arr) os << sep << x, sep = ", "; return os << '}'; } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R> ostream& operator<<(ostream& os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } const int MAX = 1000010; const int MOD2 = 998244353; const int MOD = 1000000007; const int INF = 0x3f; const long long int LLINF = 0x3f3f3f3f3f3f3f3fll; const long double EPS = 1e-7; vector<vector<int>> edges; vector<int> vis; bool cy = false; void dfs(int v, int p = -1) { if (cy) return; vis[v] = 1; for (int u : edges[v]) { if (u == p) continue; if (vis[u] == 1) { cy = true; return; } else if (vis[u] == 0) { dfs(u, v); } } vis[v] = 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(25); cerr << fixed << setprecision(10); int n; cin >> n; edges.resize(n + 1); vis.resize(n + 1); vector<pair<int, int>> seg(n); for (int i = 0; i < n; i++) { cin >> seg[i].first >> seg[i].second; } sort(seg.begin(), seg.end()); set<pair<int, int>> proseg; proseg.insert({seg[0].second, 0}); int ed = 0; for (int i = 1; i < n; i++) { auto lo = proseg.upper_bound({seg[i].first, 0}); auto hi = proseg.upper_bound({seg[i].second, 0}); if (lo != hi) { hi--; while (lo != hi) { edges[(*lo).second].push_back(i); edges[i].push_back((*lo).second); lo++; ed++; } edges[(*hi).second].push_back(i); edges[i].push_back((*hi).second); ed++; } proseg.insert({seg[i].second, i}); if (ed >= n) { cout << "NO" << "\n"; return 0; } } if (ed != n - 1) { cout << "NO" << "\n"; return 0; } dfs(0); if (cy) { cout << "NO" << "\n"; return 0; } for (int i = 0; i < n; i++) { if (!vis[i]) { cout << "NO" << "\n"; return 0; } } cout << "YES" << "\n"; return 0; }
2,100
CPP