solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
a=*map(int,[*open(0)][1].split()),;n=len(a);s=sum(a)
if s%n:exit(print(0))
M=10**9+7;s//=n;f=[1]*(n+1);b=[0]*3;d=dict()
for i in range(2,n+1):f[i]=f[i-1]*i%M
for x in a:b[(x>s)-(x<s)]+=1;d[x]=d[x]+1if x in d else 1
k=1
for x in d:k*=f[d[x]]
print([1,f[b[1]]*f[b[-1]]*2*pow(f[n-b[0]],M-2,M)][b[1]>1and b[-1]>1]*f[n]*pow(k,M-2,M)%M) | 2,300 | PYTHON3 |
t= int(input())
for i in range(t):
n = int(input())
if (n < 3):
ways = 0
else:
ways = (n - 1)//2
print(ways) | 800 | PYTHON3 |
s=str(input())
a=0
b=0
for i in s:
if i.islower()==True:
a+=1
else:
b+=1
if a>=b:
print(s.lower())
else:
print(s.upper())
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[3], n;
scanf("%d%d%d%d", &a[0], &a[1], &a[2], &n);
long long ans = 0, sum = a[0] + a[1] + a[2];
for (int i = (0); i <= (2); i++) {
long long tmp = sum - a[i];
long long cur = max((long long)a[i], tmp);
for (int j = cur; j - a[i] <= n; j++) {
long long left = n - (j - a[i]);
left = min(left, 2 * a[i] - sum + j - a[i]);
ans += (left + 2) * (left + 1) / 2;
}
}
cout << (long long)(n + 3) * (n + 2) * (n + 1) / 6 - ans << endl;
return 0;
}
| 2,100 | CPP |
n=int(input())
t = 'Poor Alex'
for i in range(n):
a,b = map(int,input().split())
if a!=b:
t='Happy Alex'
break
print(t) | 1,100 | PYTHON3 |
n = int(input())
s = [input() for _ in range(n)]
m = len(s[0])
def offset(s, t):
for i in range(m):
if s == t[i:] + t[:i]:
return i
return -1
o = [offset(s[0], s[i]) for i in range(n)]
if -1 in o:
print(-1)
exit()
ans = sum(o)
for i in range(m):
s[0] = s[0][1:] + s[0][0]
ans = min(ans, i + 1 + sum([offset(s[0], s[i]) for i in range(n)]))
print(ans)
| 1,300 | PYTHON3 |
import math
n = int(input())
names = [input() for _ in range(n)]
gby = dict()
for name in names:
k = name[0]
v = gby.get(k, [])
v.append(name)
gby[k] = v
result = 0
for k, v in gby.items():
l = math.floor(len(v) / 2)
r = math.ceil(len(v) / 2)
result += l * (l - 1) + r * (r - 1)
print(result // 2)
| 900 | PYTHON3 |
n=int(input())
soma=0
for c in range(0,n):
p=input()
ii=0
for i in p:
if i=='-':
ii=1
if ii==1:
soma-=1
else:
soma+=1
print(soma) | 800 | PYTHON3 |
a, b = map(int, input().split())
ans = 0
if a == b:
print(a, 0)
elif a < b:
ans = b - a
print(a, ans // 2)
else:
ans = a - b
print(b, ans // 2)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 1000000001;
int n, m;
int c3, c4, c5, ans, x, y, z;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
if (x == 3) c3++;
if (x == 4) c4++;
if (x == 5) c5++;
}
n = c3 + c4 + c5;
ans = oo;
for (int i = m / n; i >= 0; i--)
for (int j = (m - c3 * i) / (n - c3); j >= i; j--) {
int t1 = c3 * i, t2 = c4 * j;
int k = (m - t1 - t2) / c5;
int t3 = c5 * k;
if (t3 - t2 >= ans) break;
if (t1 + t2 + t3 != m) continue;
int tmp = abs(t1 - t2) + abs(t2 - t3);
if (tmp < ans) {
ans = tmp;
x = i;
y = j;
z = k;
}
}
if (ans == oo)
printf("-1\n");
else
printf("%d %d %d\n", x, y, z);
return 0;
}
| 2,400 | CPP |
n = int(input())
maxSum = 0
for i in range(n):
s = sum(map(int, input().split()))
if s>maxSum:
maxSum = s
print(maxSum) | 900 | PYTHON3 |
a, s = int(input()), input()
if s.count('1') > 0:
print('1', end = "")
print(s.replace('1', ''))
| 800 | PYTHON3 |
for _ in range(int(input())):
_, k = map(int, input().split())
a = set(int(i) for i in input().split())
jel = min(a) + k
fu = max(a) - k
if jel < fu:
print(-1)
else:
print(jel)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long mod = 1000000007;
vector<vector<int> > operator*(vector<vector<int> > a, vector<vector<int> > b) {
vector<vector<int> > c(m + 1);
for (int i = 0; i < c.size(); i++) c[i].resize(m + 1);
for (int i = 0; i < m + 1; i++)
for (int j = 0; j < m + 1; j++)
for (int k = 0; k < m + 1; k++, c[i][j] %= mod)
c[i][j] += (a[i][k] * 1ll * b[k][j]) % mod;
return c;
}
vector<vector<int> > bpow(vector<vector<int> > a, long long st) {
if (st == 1) return a;
if (st & 1) return a * bpow(a, st - 1);
return bpow(a * a, st / 2);
}
int getNum(char cc) {
if (cc <= 'Z') return cc - 'A' + 27;
return cc - 'a' + 1;
}
int main() {
vector<vector<int> > a;
cin >> n >> m >> k;
a.resize(m + 1);
for (int i = 0; i <= m; i++) a[i].resize(m + 1, i > 0);
for (int i = 0; i < k; i++) {
string u;
cin >> u;
a[getNum(u[1])][getNum(u[0])] = 0;
}
a = bpow(a, n);
long long ans = 0;
for (int i = 1; i < m + 1; i++, ans %= mod) ans += a[i][0];
cout << ans;
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int mu[N], prime[N], rec[N][N];
bool pd[N];
inline void make_mu() {
int sz = 0;
mu[1] = 1;
for (int i = 2; i < N; i++) {
if (!pd[i]) {
mu[i] = -1;
prime[++sz] = i;
}
for (int j = 1; j <= sz; j++) {
if (i * prime[j] >= N) break;
pd[i * prime[j]] = 1;
if (i % prime[j] == 0)
mu[i * prime[j]] = 0;
else
mu[i * prime[j]] = -mu[i];
}
}
}
inline int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
inline int getgcd(int x, int y) {
if (rec[x][y]) return rec[x][y];
rec[x][y] = gcd(x, y);
return rec[x][y];
}
inline int cal(int ub, int base, int k) {
int ret = 0;
for (int i = base; i <= ub; i += base)
if (getgcd(i, k) == 1) ret += (ub / i);
return ret;
}
int main() {
make_mu();
int a, b, c;
long long ans = 0;
scanf("%d%d%d", &a, &b, &c);
for (int i = 1; i <= c; i++)
for (int d = 1; d <= a; d++)
if (mu[d] != 0)
ans = (ans + (long long)(c / i) * mu[d] * cal(a, d, i) * cal(b, d, i)) %
1073741824ll;
ans = (ans + 1073741824ll) % 1073741824ll;
printf("%I64d\n", ans);
}
| 2,600 | CPP |
import math
a, b = map(int,input().split())
x = b * math.log(a)
y = a * math.log(b)
if (x > y): print('>')
elif (x == y) : print('=')
else :print('<') | 1,100 | PYTHON3 |
k,n,w=[int(x) for x in input().split(" ")]
s=0
for i in range(1,w+1):
s+=i*k
if s>n:
print(s-n)
else:
print(0) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void run() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main() {
run();
vector<long long> dp((long long)(3e6), 0);
dp[3] = 4;
for (long long i = 4; i < (long long)(3e6); i++) {
long long temp1 = (2 * dp[i - 2] + dp[i - 1]);
long long temp2 = 4 + (4 * dp[i - 4] + 4 * dp[i - 3] + dp[i - 2]);
if (i % 3 == 0) {
dp[i] = temp2;
} else {
dp[i] = temp1;
}
dp[i] %= (long long)(1e9 + 7);
}
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << dp[n] << endl;
}
return 0;
}
| 1,900 | CPP |
n = int(input())
a = [int(c) for c in input().split()]
dt = {}
for i in range(n):
if not a[i] in dt:
dt[a[i]] = 0
dt[a[i]] += 1
m = max(dt.values())
mkeys = set()
for k in dt.keys():
if dt[k] == m:
mkeys.add(k)
mkeys2 = set(mkeys)
dt2 = {}
for i in range(n):
if a[i] in mkeys:
mkeys.remove(a[i])
dt2[a[i]] = i
if not mkeys:
break
dt3 = {}
dt4 = {}
for i in reversed(range(n)):
if a[i] in mkeys2:
mkeys2.remove(a[i])
dt3[a[i]] = i
dt4[a[i]] = i - dt2[a[i]]
if not mkeys2:
break
mi = min(dt4.values())
for k in dt4.keys():
if dt4[k] == mi:
print(' '.join([str(dt2[k]+1), str(dt3[k]+1)]))
break
| 1,300 | PYTHON3 |
import sys
line = sys.stdin.readline().split()
a, b, c = int(line[0]), int(line[1]), int(line[2])
max_l = c * 2 + 2 * min(a, b)
if a != b and a != 0 and b != 0:
max_l += 1
print(max_l)
| 800 | PYTHON3 |
#Code by Sounak, IIESTS
#------------------------------warmup----------------------------
import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
#sys.setrecursionlimit(300000)
#threading.stack_size(10**8)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
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):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
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)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
#-------------------------------------------------------------------------
#mod = 9223372036854775807
class SegmentTree:
def __init__(self, data, default=-10**6, func=lambda a, b: max(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
class SegmentTree1:
def __init__(self, data, default=10**6, func=lambda a, b: min(a,b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
MOD=10**9+7
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
mod=10**9+7
omod=998244353
#-------------------------------------------------------------------------
prime = [True for i in range(200001)]
pp=[0]*200001
def SieveOfEratosthenes(n=200000):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
#---------------------------------running code------------------------------------------
n, k = map(int, input().split())
a=list(map(int,input().split()))
l = 1
r = n + 1
while r - l > 1:
mid = (r + l) // 2
b = [1 if i >= mid else -1 for i in a]
for i in range(1, len(b)):
b[i] += b[i - 1]
ans = False
if b[k - 1] > 0:
ans = True
mn = 0
for i in range(k, len(b)):
mn = min(mn, b[i - k])
if b[i] - mn > 0:
ans = True
if ans:
l = mid
else:
r = mid
print(l) | 2,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed;
cout.precision(10);
;
long long T;
cin >> T;
while (T--) {
long long n, m, k;
cin >> n >> m >> k;
string ans = "NO";
if (n % 2 == 0 and m % 2 == 0) {
ans = (k & 1 ? "NO" : "YES");
} else if (m & 1) {
long long mx = (m - 1) * n / 2;
if (k <= mx and k % 2 == 0) ans = "YES";
} else {
long long mn = m / 2;
if (k >= mn and (k - mn) % 2 == 0) ans = "YES";
}
cout << ans << "\n";
}
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
int t;
cin >> t;
while (t--) {
cin >> a;
if (a % 2 == 0)
cout << a * 4 + 1 << endl;
else {
switch ((a + 1) % 4) {
case 0:
cout << a + 1 << endl;
break;
case 1:
case 3:
cout << a * 4 + 1 << endl;
break;
case 2:
cout << a * 2 + 1 << endl;
break;
}
}
}
return 0;
}
| 1,200 | CPP |
n=input()
up=0
low=0
for i in n:
if(ord(i)>=65 and ord(i)<=91):
up+=1
else:
low+=1
if(up>low):
print(n.upper())
else:
print(n.lower()) | 800 | PYTHON3 |
def cal_moves(distance):
if distance<=5:
print(1)
else:
moves=0
while distance!=0:
arr=[5,4,3,2,1]
for i in range(0,len(arr)):
if distance>=arr[i]:
distance=distance-arr[i]
moves+=1
break
print(moves)
distance=int(input())
cal_moves(distance)
| 800 | PYTHON3 |
def isPrime(n) :
# Corner cases
if (n <= 1) :
return False
if (n <= 3) :
return True
# This is checked so that we can skip
# middle five numbers in below loop
if (n % 2 == 0 or n % 3 == 0) :
return False
i = 5
while(i * i <= n) :
if (n % i == 0 or n % (i + 2) == 0) :
return False
i = i + 6
return True
n=int(input())
if n==1:
print(1)
print(1)
exit()
if n==2:
print(1)
print("1 1")
exit(0)
arr=[]
for i in range(2,n+2):
arr.append(i)
d=[1 for i in range(n) ]
for i in range(n):
if (isPrime(arr[i]))==False:
d[i]=2
print(2)
print(*d) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long w1, w2, h1, h2, c;
int solve(int n) {
pair<int, int> p, y;
map<int, bool> mp;
p = {0, n};
int mx = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >,
greater<pair<int, int> > >
q;
q.push(p);
while (!q.empty()) {
y = q.top();
q.pop();
mx = max(mx, y.first);
if (y.second - w1 >= 0 && !mp[y.second - w1])
p = {y.first + h1, y.second - w1}, mp[y.second - w1] = true, q.push(p);
if (y.second - w2 >= 0 && !mp[y.second - w2])
p = {y.first + h2, y.second - w2}, mp[y.second - w2] = true, q.push(p);
}
return mx;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int binarysearch(int c) {
long long l = 0, h = 10000000;
long long score = 0;
while (h - l > 1) {
int m = (l + h) / 2;
if (c - w1 * m >= 0) {
long long y = (c - w1 * m) / w2;
long long x = m * h1 + y * h2;
if (x > score) {
score = x;
h = m;
} else
l = m;
} else
h = m;
}
return score;
}
int main() {
cin >> c >> h1 >> h2 >> w1 >> w2;
long long score = 0;
for (int m = 0; (c - w1 * m) >= 0 && m <= 10000000; m++)
score = max(score, h1 * m + h2 * ((c - w1 * m) / w2));
for (int m = 0; (c - w2 * m) >= 0 && m <= 10000000; m++)
score = max(score, h2 * m + h1 * ((c - w2 * m) / w1));
cout << score << endl;
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[101];
int av[101];
int maxim = 101;
int rez;
bool sepoate(int val) {
int nr = 0;
for (int i = 1; i <= maxim; i++)
if (av[i]) {
nr += (av[i] / val);
}
if (nr >= n) {
return true;
}
return false;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> f[i];
av[f[i]]++;
}
int st = 0;
int dr = maxim + 1;
while (st <= dr) {
int mijloc = (st + dr + 1) / 2;
if (mijloc == 0) break;
if (sepoate(mijloc)) {
st = mijloc + 1;
rez = max(rez, mijloc);
} else {
dr = mijloc - 1;
}
}
cout << rez << '\n';
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = 1000 * 1000 * 1000;
const long long LINF = 1000000000000000000LL;
const double eps = 1e-9;
void prepare(string s) {}
const int MAXN = 10 * 1000 + 41;
const int CNTANS = 200 * 1000;
int nn[MAXN];
long long a00[MAXN];
long long xx[MAXN];
long long yy[MAXN];
long long MOD[MAXN];
bool solve() {
int N;
cin >> N;
int sum = 0;
set<pair<long long, int> > s;
for (int i = 0; i < N; ++i) {
cin >> nn[i] >> a00[i] >> xx[i] >> yy[i] >> MOD[i];
s.insert(pair<long long, int>(a00[i], i));
sum += nn[i];
}
vector<pair<int, int> > ans;
int prec = -1;
int res = 0;
while (!s.empty()) {
set<pair<long long, int> >::iterator it =
s.lower_bound(pair<long long, int>(prec, -1));
if (it == s.end()) {
it = s.begin();
++res;
}
if (sum <= CNTANS) ans.push_back(pair<int, int>(it->first, it->second));
prec = it->first;
int i = it->second;
s.erase(it);
if (--nn[i]) {
a00[i] = (a00[i] * xx[i] + yy[i]) % MOD[i];
s.insert(pair<long long, int>(a00[i], i));
}
}
cout << res << endl;
for (int i = 0; i < (int)ans.size(); ++i)
cout << ans[i].first << ' ' << ans[i].second + 1 << endl;
return false;
}
int main() {
prepare("");
while (solve()) {
}
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
int n, c, d;
cin >> n;
int dx = 0, mi = -inf, mx = inf;
for (int i = 0; i < n; ++i) {
cin >> c >> d;
if (d == 1) {
if (i == 0)
mi = 1900;
else
mi = ((mi) >= (1900 - dx) ? (mi) : (1900 - dx));
} else {
if (i == 0)
mx = 1899;
else
mx = ((mx) <= (1899 - dx) ? (mx) : (1899 - dx));
}
dx += c;
}
if (mx >= inf)
cout << "Infinity" << endl;
else if (mi > mx)
cout << "Impossible" << endl;
else
cout << mx + dx << endl;
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
int T;
struct graph {
int head[N], nxt[N << 1], to[N << 1], sz;
long long w[N << 1];
void init() { memset(head, 0, sizeof(head)), sz = 0; }
graph() { init(); }
void push(int a, int b, long long c) {
nxt[++sz] = head[a], to[sz] = b, w[sz] = c, head[a] = sz;
}
int &operator[](const int a) { return to[a]; }
} g;
long long d[70];
void insert(long long x) {
for (int j = 63; j >= 0; j--) {
if ((x >> j) & 1) {
if (d[j])
x ^= d[j];
else {
d[j] = x;
break;
}
}
}
}
long long query(long long x) {
long long res = x;
for (int j = 63; j >= 0; j--)
if ((res ^ d[j]) < res) res = res ^ d[j];
return res;
}
int vis[N];
long long val[N];
void dfs(int u, long long preval) {
val[u] = preval, vis[u] = 1;
for (int i = g.head[u]; i; i = g.nxt[i]) {
if (!vis[g[i]])
dfs(g[i], preval ^ g.w[i]);
else
insert(preval ^ g.w[i] ^ val[g[i]]);
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
long long c;
scanf("%d%d%lld", &a, &b, &c);
g.push(a, b, c);
g.push(b, a, c);
}
dfs(1, 0);
printf("%lld\n", query(val[n]));
return 0;
}
| 2,300 | CPP |
from sys import stdin
input = stdin.readline
n = int(input())
a = sorted([int(i) for i in input().split()])
res = 1
if a[0] > 1:
acc = a[0]-1
else:
acc = 1
for i in range(1, n):
if a[i] > acc + 1:
res += 1
acc = a[i]-1
elif a[i] >= acc:
res += 1
acc += 1
print(res) | 1,500 | PYTHON3 |
n,m=map(int,input().split())
a=list(map(int,input().split()))
a=sorted(a)
mx=0
for i in range(n-1):
if(a[i+1]-a[i]>mx):
mx=a[i+1]-a[i]
mx=max(float(mx/2),float(max(a[0],m-a[n-1])))
print(float(mx))
| 1,200 | PYTHON3 |
d=[int(x) for x in input().split()]
print(min(d[0]+d[1]+d[2],2*d[0]+2*d[1],2*d[0]+2*d[2],2*d[1]+2*d[2]))
| 800 | PYTHON3 |
import sys
from collections import defaultdict, deque
input = sys.stdin.readline
n, m = map(int, input().split())
a = list(map(int, input().split()))
dd = defaultdict(list)
for i, x in enumerate(a):
dd[x % m].append(i)
lack, surplus = deque(), deque()
k = n // m
ans = 0
for mod in range(m*2):
while len(dd[mod % m]) < k and surplus:
mod2, i = surplus.popleft()
ans += mod - mod2
dd[mod % m].append(i)
a[i] += mod - mod2
while len(dd[mod % m]) > k:
surplus.append((mod, dd[mod % m].pop()))
print(ans)
print(*a)
| 1,900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
char a[105][105];
int m1[4], m2[4];
int n, ans;
int main() {
ans = 99999;
cin >> n;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < n; j++) {
cin >> a[j];
for (int k = 0; k < n; k++) {
if ((j + k) % 2 == 0) {
if (a[j][k] - '0' == 1)
m2[i]++;
else
m1[i]++;
} else {
if (a[j][k] - '0' == 1)
m1[i]++;
else
m2[i]++;
}
}
}
}
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 4; j++) {
int sum = 0;
for (int k = 0; k < 4; k++)
if (k != i && k != j) sum += m2[k];
sum += (m1[i] + m1[j]);
ans = min(ans, sum);
}
}
cout << ans << endl;
return 0;
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
double dp[101];
void solve() {
int n, m, h, t = 0;
cin >> n >> m >> h;
vector<int> s(m);
for (int& v : s) {
cin >> v;
t += v;
}
if (t < n) {
cout << "-1" << '\n';
return;
}
--n, --h, --t;
--s[h];
dp[0] = 1;
for (int i = 0; i < m; ++i) {
if (i == h) continue;
while (s[i]--)
for (int j = n - 1; j >= 0; --j) dp[j + 1] += (n - j) * dp[j] / (t - j);
}
cout << fixed << setprecision(10) << 1.0 - dp[n] << '\n';
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 1,600 | CPP |
a,b,c = map(int,input().split())
if a<=b and a<=c:
print('Yes')
else:
print('No') | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int len = 0, noofA = 0, noofa = 0, noofd = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '.' || s[i] == '?' || s[i] == '!' || s[i] == ',' || s[i] == '_')
continue;
else if (s[i] >= 'a' && s[i] <= 'z')
noofa++;
else if (s[i] >= 'A' && s[i] <= 'Z')
noofA++;
else
noofd++;
}
len = s.length();
if (len >= 5 && noofA >= 1 && noofa >= 1 && noofd >= 1)
cout << "Correct" << endl;
else
cout << "Too weak" << endl;
}
| 800 | CPP |
n,m = map(int,input().split())
z=0
for x in range(n):
z+=(m+((x+1)%5))//5
print(z)
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
bool vis[N][N];
char arr[N][N];
int dis[N][N];
struct data {
int x, y;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, r, c, x, y;
cin >> n >> m >> r >> c >> x >> y;
for (int i = 0; i < n + 5; i++)
for (int j = 0; j < m + 5; j++) dis[i][j] = 1e9;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> arr[i][j];
for (int i = 0; i < m + 5; i++) {
arr[0][i] = '*';
arr[n + 1][i] = '*';
}
for (int i = 0; i < n + 5; i++) {
arr[i][0] = '*';
arr[i][m + 1] = '*';
}
deque<pair<int, int> > q;
q.push_front(make_pair(r, c));
dis[r][c] = 0;
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop_front();
int i = v.first;
int j = v.second;
if (arr[i - 1][j] == '.' && dis[i][j] < dis[i - 1][j]) {
q.push_front(make_pair(i - 1, j));
dis[i - 1][j] = dis[i][j];
}
if (arr[i + 1][j] == '.' && dis[i][j] < dis[i + 1][j]) {
q.push_front(make_pair(i + 1, j));
dis[i + 1][j] = dis[i][j];
}
if (arr[i][j + 1] == '.' && dis[i][j] < dis[i][j + 1]) {
q.push_front(make_pair(i, j + 1));
dis[i][j + 1] = dis[i][j];
}
if (arr[i][j - 1] == '.' && dis[i][j] + 1 < dis[i][j - 1]) {
q.push_back(make_pair(i, j - 1));
dis[i][j - 1] = dis[i][j] + 1;
}
}
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (dis[i][j] <= x && dis[i][j] + j - c <= y && dis[i][j] != 1e9) cnt++;
}
}
cout << cnt << endl;
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
string s;
int n = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s;
for (int i = 0; i < s.size(); ++i) {
if (s[i + 1] == '/') {
n -= 2;
for (int j = 1; j <= n; ++j) cout << ' ';
cout << s[i] << s[i + 1] << s[i + 2] << s[i + 3] << "\n";
i += 3;
} else {
for (int i = 1; i <= n; ++i) cout << ' ';
cout << s[i] << s[i + 1] << s[i + 2] << "\n";
i += 2;
n += 2;
}
}
return 0;
}
| 1,000 | CPP |
n,m = map(int, input().split())
B = [-1]*(n-1)
TLR = []
for i in range(m):
t, l, r = map(int, input().split())
l, r = l-1, r-1
TLR.append((t, l, r))
if t == 1:
for j in range(l, r):
B[j] = 0
A = [0]*n
A[0] = n
for i in range(n-1):
A[i+1] = A[i]+B[i]
flag = True
for t, l, r in TLR:
if t == 1:
for i in range(l+1, r+1):
if A[i] < A[i-1]:
flag = False
else:
flag2 = False
for i in range(l+1, r+1):
if A[i] < A[i-1]:
flag2 = True
if not flag2:
flag = False
if flag:
print('YES')
print(*A)
else:
print('NO')
| 1,800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int sz = 1e5 + 10;
int f[sz], ar[sz], cnt;
int main() {
int n, fl;
set<int> st;
while (cin >> n) {
st.clear();
cnt = 0;
for (int i = 1; i <= n; i++) {
cin >> f[i];
if (st.find(f[i]) == st.end()) {
ar[cnt++] = f[i];
}
st.insert(f[i]);
}
fl = 1;
for (int i = 1; i <= n; i++)
if (f[f[i]] != f[i]) fl = 0;
if (fl) {
sort(ar, ar + cnt);
cout << cnt << "\n";
for (int i = 1; i <= n; i++) {
cout << (lower_bound(ar, ar + cnt, f[i]) - ar) + 1 << " ";
}
cout << "\n";
for (int x : st) cout << x << " ";
cout << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
| 1,700 | CPP |
from sys import stdin, stdout
import sys
INF=1e11
import bisect
def get_int(): return int(stdin.readline().strip())
def get_ints(): return map(int,stdin.readline().strip().split())
def get_array(): return list(map(int,stdin.readline().strip().split()))
def get_string(): return stdin.readline().strip()
def op(c): return stdout.write(c)
from collections import defaultdict
import math
for _ in range(int(stdin.readline())):
n,k=get_ints()
cost=get_array()
f=get_array()
cost.sort()
i=0
j=n-1
total=0
for a in f:
if a==1:
total+=2*cost[j]
j-=1
f.sort(reverse=True)
for a in f:
if a==1:
continue
total+=cost[i]
i+=a-1
total+=cost[j]
j-=1
print(total) | 1,400 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while ((c < '0' || c > '9') && c - '-') c = getchar();
if (c == '-') f = -1, c = getchar();
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
const int N = 3e5 + 10;
int n, rev[N];
long long f[N], g[N], jc[N], inv[N], z[N];
const long long mo = 998244353, Gi = 3;
inline long long qp(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % mo;
a = (a * a) % mo;
b >>= 1;
}
return ans;
}
int lim = 1, l = 0;
inline void ntt(long long a[], int pan) {
for (int i = 0; i < lim; i++)
if (i > rev[i]) swap(a[i], a[rev[i]]);
for (int bj = 1; bj < lim; bj <<= 1) {
long long wn = qp(Gi, (mo - 1) / (bj << 1));
if (pan) wn = qp(wn, mo - 2);
for (int ks = 0; ks < lim; ks += (bj << 1)) {
long long w = 1;
for (int i = 0; i < bj; i++, w = (w * wn) % mo) {
long long x = a[ks + i], y = w * a[ks + i + bj] % mo;
a[ks + i] = (x + y) % mo;
a[ks + i + bj] = ((x - y) % mo + mo) % mo;
}
}
}
if (pan) {
long long ni = qp(lim, mo - 2);
for (int i = 0; i < lim; i++) a[i] = (a[i] * ni) % mo;
}
}
void gao(int t) {
if (t == 1) {
f[1] = 1;
return;
}
int mi = t / 2;
gao(mi);
for (int i = 0; i <= mi; i++) z[i] = f[i], f[i] = f[i] * jc[i] % mo;
long long c = mi, gu = 1;
g[mi] = inv[0];
for (int i = mi - 1; i >= 0; i--) {
gu = (gu * c) % mo;
g[i] = gu * inv[mi - i] % mo;
}
lim = 1, l = 0;
while (lim <= (mi << 1)) lim <<= 1, l++;
for (int i = 0; i < lim; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
ntt(f, 0);
ntt(g, 0);
for (int i = 0; i < lim; i++) f[i] = (f[i] * g[i]) % mo, g[i] = 0;
ntt(f, 1);
long long ji = f[mi];
for (int i = mi; i >= 1; i--) {
f[i] = f[i + mi];
f[i + mi] = 0;
}
f[0] = ji;
for (int i = 0; i <= mi; i++) f[i] = (f[i] * inv[i]) % mo;
lim = 1, l = 0;
while (lim <= (mi << 1)) lim <<= 1, l++;
for (int i = 0; i < lim; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
ntt(z, 0);
ntt(f, 0);
for (int i = 0; i < lim; i++) z[i] = (z[i] * f[i]) % mo;
ntt(z, 1);
for (int i = 0; i < lim; i++) f[i] = z[i], z[i] = 0;
if (t % 2) {
z[0] = (f[0] * (t - 1)) % mo;
for (int i = 1; i <= (mi << 1) + 1; i++)
z[i] = (f[i - 1] + (f[i] * (t - 1))) % mo;
for (int i = 1; i <= (mi << 1) + 1; i++) f[i] = z[i], z[i] = 0;
}
}
int A, B;
int main() {
jc[0] = inv[0] = 1;
for (int i = 1; i <= N - 10; i++) jc[i] = (jc[i - 1] * i) % mo;
inv[N - 10] = qp(jc[N - 10], mo - 2);
for (int i = N - 11; i >= 1; i--) inv[i] = (inv[i + 1] * (i + 1)) % mo;
n = read();
A = read();
B = read();
if (n == 1) {
if (A + B == 2)
cout << "1\n";
else
cout << "0\n";
return 0;
}
if (!A || !B || A + B > n + 1) {
cout << "0\n";
return 0;
}
gao(n - 1);
cout << f[A + B - 2] * jc[A + B - 2] % mo * inv[A - 1] % mo * inv[B - 1] % mo
<< "\n";
return 0;
}
| 2,900 | CPP |
from sys import stdout
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from collections import deque
n,u=map(int,input().split())
count=0
q1=[deque([]) for i in range(n+1)]
vis=[0]*(u+1)
q=deque([])
i=0
r=0
for _ in range(u):
t,x=map(int,input().split())
if t==1:
count+=1
q.append((i,x))
q1[x].append(i)
i+=1
elif t==2:
while q1[x]:
index=q1[x].pop()
if vis[index]==0:
vis[index]=1
count-=1
else:
for y in range(r,x):
index,app=q.popleft()
if vis[index]==0:
vis[index]=1
count-=1
q1[app].popleft()
if x>r:
r=x
print(count) | 1,600 | PYTHON3 |
n = int(input())
lst = list(map(int,input().split()))
l1 = lst.count(1)
l2 = lst.count(2)
l3 = lst.count(3)
l4 = lst.count(4)
taxi = 0
if l1 > l3:
l1= l1 - l3
taxi = l3
l3 = 0
elif l3 > l1:
l3 = l3 - l1
taxi = l1
l1 = 0
elif l3 == l1:
taxi = l3
l1 = 0
l3 = 0
import math
taxi = taxi + math.ceil((4*l4 + l1 + 2*l2)/4) + l3
print(taxi) | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int mx = 5e3 + 123;
int a[mx];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int n, m;
cin >> n >> m;
map<string, string> ipName;
for (int i = 0; i < n; i++) {
string name, ip;
cin >> name >> ip;
ipName[ip] = name;
}
while (m--) {
string s, ip;
cin >> s >> ip;
ip.pop_back();
cout << s << " " << ip << "; #" << ipName[ip] << "\n";
}
return 0;
}
| 900 | CPP |
a, b, c = map(int, [input(), input(), input()])
print(min(a, b // 2, c // 4) * 7) | 800 | PYTHON3 |
import sys
input = sys.stdin.buffer.readline
def I(): return(list(map(int,input().split())))
def sieve(n):
a=[1]*n
for i in range(2,n):
if a[i]:
for j in range(i*i,n,i):
a[j]=0
return a
for __ in range(int(input())):
n=int(input())
arr=I()
arr.sort()
mindist=arr[1]-arr[0]
minidx=0
# for i in range(n-1):
# if mindist>arr[i+1]-arr[i]:
# mindist=arr[i+1]-arr[i]
# minidx=i
# print(*arr[minidx:]+arr[:minidx][::-1])
ans=[-1]*n
j=n-1
l=0
r=n-1
while(j>=0):
ans[j]=arr[l]
l+=1
j-=1
if j<0:break
ans[j]=arr[r]
r-=1
j-=1
print(*ans)
| 1,200 | PYTHON3 |
def main():
n = int(input())
for q in range(n):
k = int(input())
if k == 0:
print(1)
else:
sum1 = 2**k
sum2 = 0
sum = 2*(2**k-1)
for i in range(1, k//2):
sum1 += 2**i
sum2 = sum - sum1
print(sum1 - sum2)
if __name__ == '__main__':
main() | 800 | PYTHON3 |
n = int(input());
z = list(map(int,input().split()))
result = 9999999999999999999;
for i in range(n):
tmp = z[i]/max(i,n-1-i);
if(tmp<result):
result = tmp;
print(int(result))
| 1,300 | PYTHON3 |
#1324C
t = int(input())
while t > 0:
s = input().split('R')
print(max([len(l) for l in s])+1)
t -= 1 | 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int n, flag[MAXN], a[MAXN];
stack<int> s;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
a[i] = x;
}
for (int i = n; i >= 1; i--) s.push(a[i]), flag[a[i]] = 1;
for (int o = 1; o <= n; o++) {
int x, ans = 0;
scanf("%d", &x);
if (flag[x] == 0)
printf("0 ");
else {
while (s.top() != x) {
flag[s.top()] = 0;
s.pop();
ans++;
}
flag[x] = 0;
s.pop();
ans++;
printf("%d ", ans);
}
}
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
struct point {
int x;
int id;
} pt[N];
bool cmp(point a, point b) { return a.x < b.x; }
int main() {
int n, m;
cin >> n >> m;
int i;
int ans[N];
for (i = 0; i < n; i++) {
cin >> pt[i].x;
pt[i].id = i;
}
sort(pt, pt + n, cmp);
int right, left;
for (i = 0; i < m; i++) cin >> right >> left;
int t = 0;
for (i = 0; i < n; i++) {
ans[pt[i].id] = (t++) % 2;
}
cout << ans[0];
for (i = 1; i < n; i++) cout << ' ' << ans[i];
cout << endl;
return 0;
}
| 1,600 | CPP |
t = int(input())
for i in range(t) :
n = int(input())
line = input()
li = line.split()
li = [int(j) for j in li]
li.sort()
temp = False
even = 0
odd = 0
for j in li :
if j is not None and j%2 == 0:
even = even + 1
elif j is not None :
odd = odd+1
if even%2==0 and odd%2==0 :
print("YES")
elif even%2==1 and odd%2==1 :
for j in range(n-1) :
if li[j+1]-li[j] == 1 :
temp = True
break
if temp :
print('YES')
else :
print("NO")
else:
print("NO") | 1,100 | PYTHON3 |
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int main() {
int t;
cin>>t;
while(t--){
int xa,xb;
cin>>xa>>xb;
int ya,yb;
cin>>ya>>yb;
int fa,fb;
cin>>fa>>fb;
if(xa==ya){
if(fa==xa and fb>min(xb,yb) and fb<max(xb,yb)){
cout<<abs(xb-yb)+2<<"\n";
}
else{
cout<<abs(xb-yb)<<"\n";
}
}
else if(xb==yb){
if(fb==xb and fa>min(xa,ya) and fa<max(xa,ya)){
cout<<abs(xa-ya)+2<<"\n";
}
else{
cout<<abs(xa-ya)<<"\n";
}
}
else{
int ans=abs(xa-ya)+abs(xb-yb);
cout<<ans<<"\n";
}
}
// your code goes here
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n, k;
struct List {
int to;
List *nxt;
List(int a, List *p) : nxt(p), to(a){};
};
List *G[MAXN];
int cnt[MAXN], dp[MAXN], ans = 1;
void dfs(int u, int fa) {
vector<int> V;
dp[u] = 0;
for (List *p = G[u]; p; p = p->nxt) {
int v = p->to;
if (v == fa) continue;
dfs(v, u);
dp[u] = max(dp[u], dp[v] + 1);
V.push_back(dp[v] + 1);
}
if (V.size() <= 1) return;
sort(V.begin(), V.end());
for (int i = 0; i < V.size() - 1; ++i) {
if (V[i] + V[i + 1] > k) {
ans += V.size() - i - 1;
dp[u] = V[i];
break;
}
}
}
int main() {
cin >> n >> k;
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
G[u] = new List(v, G[u]);
G[v] = new List(u, G[v]);
++cnt[u], ++cnt[v];
}
int st;
for (int i = 1; i <= n; ++i) {
if (cnt[i] > 1) {
st = i;
break;
}
}
dfs(st, -1);
cout << ans << endl;
}
| 2,400 | CPP |
for i in range(int(input())):
a,b = list(map(int, input().split()))
print(min((a//b)*b+(b//2),a)) | 900 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-8;
const long double PI = 3.1415926535897932384626433832795;
bool get(long long n, long long k, long long x) {
if (k == n) return true;
if (k == 0) return false;
long long num = n - k;
if (num <= k) {
if (x < 2 * num)
return x % 2 == 1;
else
return true;
} else if (num % 2 == k % 2) {
long long f = n - (2 * k - 1);
if (x < f) return false;
x -= f;
return x % 2 == 0;
} else if (x == n - 1)
return true;
else
return get(n - 1, k - 1, x);
}
int main() {
int p;
long long n, k;
cin >> n >> k >> p;
for (int i = 0; i < (int)(p); i++) {
long long x;
cin >> x;
if (get(n, k, x - 1))
cout << "X";
else
cout << ".";
}
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e6 + 44;
const int mod = 1e9 + 9, b = 12387;
long long rcshnt[MN];
int n;
char a[MN];
bool check(int len) {
;
long long curr = 0;
for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;
long long pre = curr;
;
for (int i = len; i < n - 1; ++i) {
;
;
curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;
;
if ((pre - curr) % mod == 0) {
;
return true;
}
};
return false;
}
int main() {
scanf("%s", a);
n = strlen(a);
rcshnt[0] = 1;
for (int i = 1; i < MN; ++i) {
rcshnt[i] = rcshnt[i - 1] * b % mod;
}
for (int i = 0; i < 10; ++i)
;
vector<int> pre;
long long ph = 0, sh = 0;
for (int i = 0; i < n - 1; ++i) {
ph = (ph * b + a[i]) % mod;
sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;
if (ph == sh) {
pre.push_back(i + 1);
;
}
}
int low = 1, high = n - 1, ans = 0;
while (low <= high) {
int med = (low + high) / 2;
if (check(med)) {
ans = med;
low = med + 1;
} else
high = med - 1;
}
int res = -1;
for (auto c : pre) {
if (c <= ans) res = max(res, c);
}
if (res <= 0) {
printf("Just a legend\n");
} else {
a[res] = 0;
printf("%s\n", a);
}
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
short v[5];
int main() {
int n, ok = 0, x, i = 4;
ios_base::sync_with_stdio(false);
cin >> n;
++n;
while (!ok) {
ok = 1;
x = n;
i = 4;
while (x) v[i] = x % 10, --i, x /= 10;
sort(v + 1, v + 5);
for (i = 2; i <= 4; ++i)
if (v[i] == v[i - 1]) ok = 0;
if (ok) cout << n;
++n;
}
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 2003;
vector<int> g[N];
int a[N];
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
int n, h;
long long dp[N][N];
long long go(int pos, int bal) {
if (a[pos] + bal > h) return 0;
if (a[pos] + bal + 1 < h) return 0;
if (pos == n - 1) {
if (bal >= 2) return 0;
if (bal == 1) return a[pos] + 1 == h;
return a[pos] + 1 >= h;
}
long long &ans = dp[pos][bal];
if (ans != -1) return ans;
if (a[pos] == h) return ans = go(pos + 1, 0);
int x = a[pos] + bal;
if (x == h)
return ans = (bal * go(pos + 1, bal - 1) + go(pos + 1, bal)) % mod;
if (x + 1 == h)
return ans = ((bal + 1) * go(pos + 1, bal) + go(pos + 1, bal + 1)) % mod;
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, k, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++) dp[i][j] = -1;
cin >> n >> h;
for (i = 0; i < n; i++) cin >> a[i];
cout << go(0, 0) << endl;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % mod;
base = ((long long)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m) {
int i, u, v;
while (m--) {
cin >> u >> v;
g[u - 1].push_back(v - 1);
g[v - 1].push_back(u - 1);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
| 2,100 | CPP |
s = input()
for _ in range(int(input())):
l, r, k = map(int, input().split())
n = r - l + 1
l, k = l - 1, k % n
sub = s[l : r]
s = s[:l] + sub[n - k : ] + sub[ : n - k] + s[r : ]
print(s) | 1,300 | PYTHON3 |
n,k=map(int,input().split())
s=input()
c={'A':0,'B':0,'C':0,'D':0,'E':0,'F':0,'G':0,'H':0,'I':0,'J':0,'K':0,'L':0,'M':0,'N':0,'O':0,'P':0,'Q':0,'R':0,'S':0,'T':0,'U':0,'V':0,'W':0,'X':0,'Y':0,'Z':0}
for i in range(n):
c[s[i]]+=1
d=sorted(c.items(),key=lambda x:x[1],reverse = True)
print(d[k-1][1]*k) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b, d, mi = 1000000000000000000, e = -1, f, g;
cin >> n;
int A1[n], A2[n], A3[n];
for (int i = 0; i < n; i++) cin >> A1[i];
for (int i = 0; i < n; i++) cin >> A2[i];
for (int i = 0; i < n; i++) cin >> A3[i];
vector<vector<int> > v(n);
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
v[a - 1].push_back(b - 1);
v[b - 1].push_back(a - 1);
}
for (int i = 0; i < n; i++) {
if (v[i].size() > 2) {
cout << -1;
return 0;
}
if (v[i].size() == 1 && e != -1) f = i;
if (v[i].size() == 1 && e == -1) e = i;
}
vector<int> p;
p.push_back(e);
int u = e;
while (u != f) {
if (p.size() > 1 && v[u][0] == p[p.size() - 2])
u = v[u][1];
else
u = v[u][0];
p.push_back(u);
}
long long s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A1[p[i]];
if (i % 3 == 1) s += A2[p[i]];
if (i % 3 == 2) s += A3[p[i]];
}
if (s <= mi) {
mi = s;
g = 1;
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A1[p[i]];
if (i % 3 == 1) s += A3[p[i]];
if (i % 3 == 2) s += A2[p[i]];
}
if (s <= mi) {
mi = s;
g = 2;
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A2[p[i]];
if (i % 3 == 1) s += A1[p[i]];
if (i % 3 == 2) s += A3[p[i]];
}
if (s <= mi) {
mi = s;
g = 3;
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A2[p[i]];
if (i % 3 == 1) s += A3[p[i]];
if (i % 3 == 2) s += A1[p[i]];
}
if (s <= mi) {
mi = s;
g = 4;
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A3[p[i]];
if (i % 3 == 1) s += A1[p[i]];
if (i % 3 == 2) s += A2[p[i]];
}
if (s <= mi) {
mi = s;
g = 5;
}
s = 0;
for (int i = 0; i < n; i++) {
if (i % 3 == 0) s += A3[p[i]];
if (i % 3 == 1) s += A2[p[i]];
if (i % 3 == 2) s += A1[p[i]];
}
if (s <= mi) {
mi = s;
g = 6;
}
vector<int> vk(n);
if (g == 1) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 1;
if (i % 3 == 1) vk[p[i]] = 2;
if (i % 3 == 2) vk[p[i]] = 3;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
if (g == 2) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 1;
if (i % 3 == 1) vk[p[i]] = 3;
if (i % 3 == 2) vk[p[i]] = 2;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
if (g == 3) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 2;
if (i % 3 == 1) vk[p[i]] = 1;
if (i % 3 == 2) vk[p[i]] = 3;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
if (g == 4) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 2;
if (i % 3 == 1) vk[p[i]] = 3;
if (i % 3 == 2) vk[p[i]] = 1;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
if (g == 5) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 3;
if (i % 3 == 1) vk[p[i]] = 1;
if (i % 3 == 2) vk[p[i]] = 2;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
if (g == 6) {
for (int i = 0; i < n; i++) {
if (i % 3 == 0) vk[p[i]] = 3;
if (i % 3 == 1) vk[p[i]] = 2;
if (i % 3 == 2) vk[p[i]] = 1;
}
cout << mi << endl;
for (int i = 0; i < n; i++) cout << vk[i] << " ";
}
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
vector<int> G[maxn];
bool visit[maxn][2], vis[maxn];
int path[maxn];
void dfs(int v, int flag, int xb) {
if (G[v].size() == 0 && flag) {
cout << "Win" << endl;
for (int i = 0; i < xb; i++) printf("%d ", path[i]);
cout << v << endl;
exit(0);
}
if (visit[v][flag]) return;
path[xb] = v;
visit[v][flag] = 1;
for (int i = 0; i < G[v].size(); i++) dfs(G[v][i], flag ^ 1, xb + 1);
}
void cycle(int v) {
if (vis[v]) {
cout << "Draw" << endl;
exit(0);
}
vis[v] = 1;
for (int i = 0; i < G[v].size(); i++) {
cycle(G[v][i]);
}
vis[v] = 0;
}
int main() {
int n, m, s;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
int c, x;
scanf("%d", &c);
while (c--) {
scanf("%d", &x);
G[i].push_back(x);
}
}
cin >> s;
dfs(s, 0, 0);
cycle(s);
cout << "Lose" << endl;
return 0;
}
| 2,100 | CPP |
for i in range(int(input())):
a,b,n=[int(num) for num in input().split()]
c=0
while True:
if a>b:
b=a+b
c=c+1
if b>n or a>n:
break
else:
a=a+b
c=c+1
if b>n or a>n:
break
print(c)
| 800 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
#define ri register int
#define SZ(k) k.size()
#define ALL(k) k.begin(),k.end()
#define DEBUG(k...) fprintf(stderr,k)
#define pb push_back
#define fi first
#define se second
typedef long long ll;
typedef pair<int,int> pii;
template<class T>inline bool chkmax(T &x,const T &y){return x<y?x=y,true:false;}
template<class T>inline bool chkmin(T &x,const T &y){return x>y?x=y,true:false;}
const int maxn=2e5+10;
int a[maxn],m,n,t_case;
inline int gcd(int x,int y){return y?gcd(y,x%y):x;}
int main(){
scanf("%d",&t_case);
while(t_case--){
scanf("%d",&n);
printf("%d\n",100/gcd(n,100-n));
}
return 0;
} | 800 | CPP |
t = int(input())
for p in range(t):
n = int(input())
for i in range(n):
print(i+1, end=" ")
print()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void in() { return; }
template <typename T, typename... Types>
void in(T &a, Types &...b) {
cin >> (a);
in(b...);
}
void o() { return; }
template <typename T, typename... Types>
void o(T a, Types... b) {
cout << (a);
cout << ' ';
o(b...);
}
bool sortin(const pair<long long int, long long int> &e,
const pair<long long int, long long int> &f) {
return (e.first < f.first);
}
bool POT(long long int x) { return x && (!(x & (x - 1))); }
long long int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18,
mod = 1e9 + 7;
long long int ar[250005], br[250005], xr[250005], tem[250005];
int main() {
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
};
in(n, m);
while (m--) {
in(a, b, c);
set<long long int> st;
st.insert(1);
st.insert(2);
st.insert(3);
st.erase(ar[a]);
st.erase(ar[b]);
st.erase(ar[c]);
if (ar[a] == 0) ar[a] = *st.begin(), st.erase(ar[a]);
if (ar[b] == 0) ar[b] = *st.begin(), st.erase(ar[b]);
if (ar[c] == 0) ar[c] = *st.begin(), st.erase(ar[c]);
}
for (long long int i = 1; i <= n; i++) o(ar[i]);
}
| 1,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m = 62;
bool hv[64][64];
long long a[100001];
int cnt[64];
vector<int> b[64];
bool vis[64];
int d[128][128], w[128][128];
map<int, int> mp;
int q = 0;
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
for (int j = m; j >= 0; j--) {
if (a[i] & (1LL << j)) b[j].push_back(i);
}
}
for (int i = 1; i <= 2 * m; i++) {
for (int j = 1; j <= 2 * m; j++) {
d[i][j] = 1e8;
w[i][j] = 1e8;
}
d[i][i] = w[i][i] = 0;
}
for (int j = 0; j < m; j++) {
if (b[j].size() > 2) {
cout << "3\n";
return 0;
}
if (b[j].size() < 2) continue;
int x = b[j][0], y = b[j][1];
if (!mp[x]) mp[x] = ++q;
if (!mp[y]) mp[y] = ++q;
x = mp[x], y = mp[y];
d[x][y] = d[y][x] = w[x][y] = w[y][x] = 1;
}
int ans = 1e8;
for (int i = 1; i <= q; i++) {
for (int j = 1; j < i; j++) {
for (int k = j + 1; k < i; k++) {
ans = min(ans, w[i][j] + d[j][k] + w[k][i]);
}
}
for (int j = 1; j <= q; j++) {
for (int k = 1; k <= q; k++) {
d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
}
}
}
if (ans != 1e8)
cout << ans << endl;
else
cout << -1 << endl;
}
| 1,900 | CPP |
t = int(input())
for _ in range(t):
s = input()
cur_ind = [-1, -1, -1]
cur_l = 0
for i, (c1, c2) in enumerate(zip(s[:-1], s[1:])):
if c2 == c1:
cur_ind[int(c2)-1] = i+1
else:
cur_ind[int(c1)-1] = i
cur_ind[int(c2)-1] = i+1
if -1 not in cur_ind:
if cur_l == 0:
cur_l = max(cur_ind)-min(cur_ind)
else:
cur_l = min(cur_l, max(cur_ind)-min(cur_ind))
if cur_l != 0:
print(cur_l+1)
else:
print(cur_l) | 1,200 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_heap = priority_queue<T>;
template <typename T>
ostream &operator<<(ostream &os, min_heap<T> H) {
while (!H.empty()) {
os << H.top() << " ";
H.pop();
}
os << "\n";
return os << "";
}
template <typename T>
ostream &operator<<(ostream &os, max_heap<T> H) {
while (!H.empty()) {
os << H.top() << " ";
H.pop();
}
os << "\n";
return os << "";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, pair<F, S> P) {
return os << P.first << " " << P.second;
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> arr) {
for (long long i = 0; i < (long long)arr.size(); i++) {
os << arr[i] << " ";
}
return os << "";
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> matrix) {
os << "\n";
for (long long i = 0; i < (long long)matrix.size(); i++) {
for (long long j = 0; j < (long long)matrix[i].size(); j++) {
os << matrix[i][j] << " ";
}
os << "\n";
}
return os << "";
}
template <typename T>
ostream &operator<<(ostream &os, set<T> S) {
for (auto s : S) {
os << s << " ";
}
return os << "";
}
template <typename T>
ostream &operator<<(ostream &os, multiset<T> S) {
for (auto s : S) {
os << s << " ";
}
return os << "";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, map<F, S> M) {
os << "\n";
for (auto m : M) {
os << m << "\n";
}
return os << "";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, multimap<F, S> M) {
os << "\n";
for (auto m : M) {
os << m << "\n";
}
return os << "";
}
void solve() {
long long n;
cin >> n;
vector<long long> arr(n);
vector<long long> cnt(n + 1);
for (long long i = 0; i < n; i++) {
cin >> arr[i];
cnt[arr[i]]++;
}
if (cnt[0] == 0) {
cout << 0 << " ";
for (long long i = 1; i <= (long long)n; i++) {
cout << -1 << " ";
}
cout << "\n";
return;
}
vector<long long> us;
vector<long long> cost(n + 1);
us.reserve(n);
for (long long i = 0; i < (long long)cnt[0] - 1; i++) {
us.push_back(0);
}
cout << cnt[0] << " ";
for (long long i = 1; i <= (long long)n; i++) {
cost[i] = cost[i - 1];
if (cnt[i - 1] == 0) {
if (us.empty()) {
for (long long j = i; j <= (long long)n; j++) {
cout << -1 << " ";
}
cout << "\n";
return;
}
cost[i] += (i - 1 - us.back());
us.pop_back();
}
cout << cost[i] + cnt[i] << " ";
for (long long k = 0; k < (long long)cnt[i] - 1; k++) {
us.push_back(i);
}
}
cout << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long no_of_testcases = 1;
cin >> no_of_testcases;
for (long long i = 1; i <= no_of_testcases; i++) {
solve();
}
return 0;
}
| 1,700 | CPP |
times = int(input())
s = ""
for i in range(times):
word = input()
if(len(word) > 10):
s += word[0] + str(len(word) - 2) + word[-1] + "\n"
else:
s += word + "\n"
print(s)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long int check(long long int a, long long int n) {
long long int res = a * n;
if (res % 180 == 0 && res / 180 <= n - 2) {
return true;
} else {
return false;
}
}
long long int bsearch(long long int ang) {
long long int i;
long long int ans = -1;
long long int in = (long long int)180;
long long int in2 = (long long int)1000000;
long long int la = in * in2;
long long int sq = sqrt(la);
vector<long long int> res1, res2;
for (i = 3; i <= sq; i++) {
if (la % i == 0) {
res1.push_back(i);
if ((la / i) != i) {
res2.push_back(la / i);
}
}
}
long long int n = res1.size();
for (i = 0; i < n; i++) {
if (check(ang, res1[i])) {
return res1[i];
}
}
n = res2.size();
for (i = n - 1; i >= 0; i--) {
if (check(ang, res2[i])) {
return res2[i];
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t, ang;
cin >> t;
while (t--) {
cin >> ang;
cout << bsearch(ang) << '\n';
}
}
| 1,600 | CPP |
n, m = map(int, input().split())
c = 0
for i in range(1, n + 1):
if(m%i == 0 and m/i <= n):
c += 1
print(c)
| 1,000 | PYTHON3 |
for i in range(int(input())):
num = int (input())
print((num-1)//2) if num&1 else print(num//2) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int MODP(long long x) {
int r = x % 1000000007;
if (r < 0) r += 1000000007;
return r;
}
template <class T, class Q>
using TreeMergeFunction = function<Q(const Q &, const Q &)>;
template <class T, class Q>
using TreeUpdateLeafFunction =
function<Q(const Q &, const T &, const T &, int, int)>;
template <class T, class Q>
using TreeSplitFunction = function<void(Q &, Q &, Q &, T, int, int, int)>;
template <class T, class Q>
using TreeInitFunction = function<Q(const T &, int, int)>;
template <class T, class Q>
struct SegmentTree {
struct TreeNode {
bool leaf = true;
T value;
Q query;
int leftChild = -1, rightChild = -1;
};
protected:
vector<TreeNode> node;
TreeMergeFunction<T, Q> merge;
TreeUpdateLeafFunction<T, Q> updateLeaf;
TreeSplitFunction<T, Q> split;
TreeInitFunction<T, Q> init;
const T defaultValue;
int addNode(int l, int r) {
TreeNode newNode;
newNode.value = defaultValue;
node.push_back(newNode);
return (int)node.size() - 1;
}
void splitNode(int p, int l, int r) {
assert(node[p].leaf);
int m = (l + r) / 2;
node[p].leaf = false;
if (node[p].leftChild == -1) {
int c = addNode(l, m);
node[p].leftChild = c;
c = addNode(m + 1, r);
node[p].rightChild = c;
}
int lc = node[p].leftChild;
int rc = node[p].rightChild;
node[lc].leaf = true;
node[rc].leaf = true;
node[lc].value = node[p].value;
node[rc].value = node[p].value;
split(node[p].query, node[lc].query, node[rc].query, node[p].value, l, m,
r);
}
void update(int p, int l, int r, int i, int j, const T &v) {
if (j < l || i > r) return;
int m = (l + r) / 2;
if (i <= l && r <= j) {
if (node[p].leaf) {
node[p].query = updateLeaf(node[p].query, node[p].value, v, l, r);
node[p].value = v;
return;
} else {
node[p].leaf = true;
node[p].value = v;
}
} else if (node[p].leaf) {
splitNode(p, l, r);
}
update(node[p].leftChild, l, m, i, j, v);
update(node[p].rightChild, m + 1, r, i, j, v);
node[p].query =
merge(node[node[p].leftChild].query, node[node[p].rightChild].query);
}
Q query(int p, int l, int r, int i, int j) {
if (i <= l && r <= j) {
return node[p].query;
}
if (node[p].leaf) {
splitNode(p, l, r);
}
int m = (l + r) / 2;
Q ret;
if (j <= m) {
ret = query(node[p].leftChild, l, m, i, j);
} else if (i >= m + 1) {
ret = query(node[p].rightChild, m + 1, r, i, j);
} else {
ret = merge(query(node[p].leftChild, l, m, i, j),
query(node[p].rightChild, m + 1, r, i, j));
}
node[p].query =
merge(node[node[p].leftChild].query, node[node[p].rightChild].query);
return ret;
}
int minIndex, maxIndex;
int root;
public:
explicit SegmentTree(int minIndex, int maxIndex, T defaultValue,
const TreeMergeFunction<T, Q> &merge,
const TreeUpdateLeafFunction<T, Q> &updateLeaf,
const TreeSplitFunction<T, Q> &split)
: merge(merge),
updateLeaf(updateLeaf),
split(split),
defaultValue(defaultValue),
minIndex(minIndex),
maxIndex(maxIndex) {
root = addNode(minIndex, maxIndex);
}
SegmentTree(int minIndex, int maxIndex, T defaultValue,
const TreeMergeFunction<T, Q> &merge,
const function<Q(T, int, int)> &init)
: merge(merge),
defaultValue(defaultValue),
minIndex(minIndex),
maxIndex(maxIndex),
init(init) {
updateLeaf = [&](const Q &cur, T oldV, T curV, int l, int r) {
return this->init(curV, l, r);
};
split = [&](Q &cur, Q &lQ, Q &rQ, T v, int l, int m, int r) {
lQ = this->init(v, l, m);
rQ = this->init(v, m + 1, r);
};
root = addNode(minIndex, maxIndex);
}
void update(int i, int j, T v) { update(root, minIndex, maxIndex, i, j, v); }
Q query(int i, int j) { return query(root, minIndex, maxIndex, i, j); }
Q query() { return query(root, minIndex, maxIndex, minIndex, maxIndex); }
};
void testGen() {
freopen("biginput1.txt", "w", stdout);
fclose(stdout);
}
int nRow, nCol, k, q;
vector<pair<int, int>> rook;
vector<vector<int>> row;
struct Query {
int r1, c1, r2, c2, id;
};
vector<Query> query;
vector<int> goodQuery;
void solve() {
row.clear();
row.resize(nRow + 1);
for (auto &r : rook) {
row[r.first].push_back(r.second);
}
sort(query.begin(), query.end(),
[&](const Query &a, const Query &b) { return a.r2 < b.r2; });
SegmentTree<int, int> lastR(
1, nCol, 0, [](int x, int y) { return min(x, y); },
[](int v, int l, int r) { return v; });
int qId = 0;
for (auto i = 1; i <= nRow; ++i) {
for (auto &c : row[i]) {
lastR.update(c, c, i);
if (1 <= 0) cout << "Update " << i << " " << c << endl;
}
while (qId < query.size() && query[qId].r2 <= i) {
if (1 <= 0)
cout << "Process query: " << query[qId].r1 << " " << query[qId].c1
<< " " << query[qId].r2 << " " << query[qId].c2 << endl;
int minR = lastR.query(query[qId].c1, query[qId].c2);
if (1 <= 0) cout << "minR " << minR << endl;
if (query[qId].r1 > minR) {
if (1 <= 0) cout << "Bad" << endl;
goodQuery[query[qId].id]--;
}
++qId;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin >> nCol >> nRow >> k >> q;
for (auto repeat_var = 0; repeat_var < k; ++repeat_var) {
int r, c;
cin >> c >> r;
rook.push_back(make_pair(r, c));
}
goodQuery.resize(q);
for (auto i = 0; i < q; ++i) {
goodQuery[i] = 2;
int r1, c1, r2, c2;
cin >> c1 >> r1 >> c2 >> r2;
query.push_back(Query{r1, c1, r2, c2, i});
}
solve();
if (1 <= 0) cout << "Flipping..." << endl;
swap(nCol, nRow);
for (auto i = 0; i < rook.size(); ++i) {
rook[i] = make_pair(rook[i].second, rook[i].first);
}
for (auto i = 0; i < q; ++i) {
query[i] =
Query{query[i].c1, query[i].r1, query[i].c2, query[i].r2, query[i].id};
}
solve();
for (auto i = 0; i < q; ++i) {
if (goodQuery[i] > 0)
cout << "YES";
else
cout << "NO";
cout << endl;
}
return 0;
}
| 2,400 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> edge[200001];
int nodes, edges;
int dfsorder[200000];
vector<int> maincycle;
vector<pair<int, int>> invalidate;
int pos[200001];
int reach[200001];
bool seen[200001];
void NO(int reason) {
cout << -1 << endl;
exit(0);
}
bool dfs_init(int n, int d = 0) {
if (pos[n] != -1 && pos[n] < d) {
int cutoff = pos[n];
memset(pos, -1, sizeof pos);
for (int i = cutoff; i < d; i++) {
pos[dfsorder[i]] = maincycle.size();
maincycle.push_back(dfsorder[i]);
}
return true;
}
if (pos[n] != -1) return false;
pos[n] = d;
dfsorder[d] = n;
for (int c : edge[n]) {
if (dfs_init(c, d + 1)) return true;
}
return false;
}
int dfs_cycle(int n, int startpos, bool initial = false) {
if (pos[n] != -1 && !initial) return pos[n];
if (reach[n] == -1) {
reach[n] = -2;
int olddist = 0;
for (int c : edge[n]) {
int res = dfs_cycle(c, startpos);
if (res < 0) continue;
int dist = (res - startpos + maincycle.size()) % maincycle.size();
if (res == startpos) dist = maincycle.size();
if (dist > olddist) {
olddist = dist;
reach[n] = res;
}
}
}
return reach[n];
}
int event[200001];
void insert(int start, int end) {
int dist = (end - start + maincycle.size()) % maincycle.size();
if (dist == 1) return;
start = (start + 1) % maincycle.size();
end = (end - 1 + maincycle.size()) % maincycle.size();
if (end < start) {
event[0]++;
event[end + 1]--;
event[start]++;
event[maincycle.size()]--;
} else {
event[start]++;
event[end + 1]--;
}
}
void dfs_ans(int n, int d = 0) {
if (pos[n] != -1 && pos[n] < d) NO(3000 + n);
if (pos[n] != -1) return;
pos[n] = d;
for (int c : edge[n]) {
dfs_ans(c, d + 1);
}
pos[n] = (1 << 30);
}
void answer(int node) {
memset(pos, -1, sizeof pos);
pos[node] = (1 << 30);
for (int i = 1; i <= nodes; i++) {
dfs_ans(i);
}
cout << node << endl;
exit(0);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(pos, -1, sizeof pos);
memset(reach, -1, sizeof reach);
cin >> nodes >> edges;
for (int i = 0; i < edges; i++) {
int f, t;
cin >> f >> t;
edge[f].push_back(t);
}
int maxi = 0;
for (int i = 1; i <= nodes; i++) {
if (pos[i] == -1 && dfs_init(i)) break;
maxi = i;
}
for (int n : maincycle) {
int end = dfs_cycle(n, pos[n], true);
if (end >= 0) insert(pos[n], end);
}
int curr = 0;
for (int i = 0; i < maincycle.size(); i++) {
curr += event[i];
if (curr == 0) {
answer(maincycle[i]);
}
}
NO(2);
}
| 2,700 | CPP |
import sys
input = sys.stdin.readline
n = int(input())
for i in range(n):
s = list(input().strip())
s.sort()
valid = True
for j in range(1, len(s)):
if ord(s[j]) - ord(s[j-1]) != 1:
valid = False
break
print('YES' if valid else 'NO')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int bit[100010], n;
inline int lowbit(int x) { return x & -x; }
inline void modify(int pos, int add) {
for (int i = pos; i <= n; i += lowbit(i)) bit[i] += add;
}
inline int query(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= lowbit(i)) res += bit[i];
return res;
}
inline int getSum(int l, int r) {
if (l > r) return 0;
return query(r) - query(l - 1);
}
vector<pair<int, int> > g[100010];
int belong[100010], id[100010], dep[100010], idx, top[100010];
void dfs(int u, int fa) {
for (pair<int, int>& e : g[u]) {
int v = e.first, edge_id = e.second;
if (v == fa) continue;
if (fa == -1) {
top[v] = v;
} else {
top[v] = top[u];
}
dep[v] = dep[u] + 1;
id[v] = ++idx;
modify(id[v], 1);
belong[edge_id] = v;
dfs(v, u);
}
}
int main() {
int u, v, root, max_deg = 0;
scanf("%d", &n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
g[u].push_back(pair<int, int>(v, i));
g[v].push_back(pair<int, int>(u, i));
}
for (int v = 1; v <= n; v++) {
if ((int)g[v].size() > max_deg) {
root = v;
max_deg = (int)g[v].size();
}
}
dfs(root, -1);
int m, o, _id;
scanf("%d", &m);
while (m--) {
scanf("%d", &o);
if (o == 1) {
scanf("%d", &_id);
modify(id[belong[_id]], 1);
} else if (o == 2) {
scanf("%d", &_id);
modify(id[belong[_id]], -1);
} else {
scanf("%d%d", &u, &v);
int ans;
if (v == root) swap(u, v);
if (u == root) {
ans = getSum(id[top[v]], id[v]);
if (ans != dep[v]) ans = -1;
} else if (top[u] == top[v]) {
if (dep[u] > dep[v]) swap(u, v);
ans = getSum(id[u] + 1, id[v]);
if (ans != dep[v] - dep[u]) ans = -1;
} else {
ans = getSum(id[top[v]], id[v]) + getSum(id[top[u]], id[u]);
if (ans != dep[v] + dep[u]) ans = -1;
}
printf("%d\n", ans);
}
}
return 0;
}
| 2,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long findDistanceToClosestPerfectSquare(long long x) {
if (x <= 1) return 0;
long long sqr = sqrt(x);
long long p1 = sqr * sqr;
long long p2 = (sqr + 1) * (sqr + 1);
if (p1 == x) return 0;
return min(abs(p1 - x), abs(p2 - x));
}
long long a[200001];
int main() {
long long n;
cin >> n;
vector<pair<long long, long long> > v;
for (long long i = 0; i < n; i++) {
cin >> a[i];
v.push_back({findDistanceToClosestPerfectSquare(a[i]), i});
}
auto cmp = [](pair<long long, long long> &p, pair<long long, long long> &q) {
if (p.first == q.first and p.first == 0) {
return a[p.second] < a[q.second];
}
return p.first < q.first;
};
sort(v.begin(), v.end(), cmp);
long long ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += v[i].first;
}
for (int i = n / 2; i < n; i++) {
if (v[i].first == 0) {
if (a[v[i].second] == 0)
ans += 2;
else
ans++;
}
}
cout << ans << "\n";
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool func(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return a.second < b.second;
}
long long int power(long long int a, long long int b) {
long long int res = 1;
a %= 1000000007;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
long long int modInverse(long long int a) { return power(a, 1000000007 - 2); }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, tmp = 1;
int k;
cin >> t;
cin >> k;
long long int dp[100010] = {0};
for (long long int i = 0; i < k - 1; ++i) dp[i + 1] = 1;
dp[k] = 2;
for (long long int i = k + 1; i <= 100009; ++i) {
dp[i] = dp[i - 1] + dp[i - k];
dp[i] %= 1000000007;
}
long long int dpp[100010] = {0};
for (long long int i = 1; i <= 100009; ++i) {
dpp[i] = dpp[i - 1] + dp[i];
dpp[i] %= 1000000007;
}
while (t--) {
long long int a, b;
cin >> a >> b;
long long int res = dpp[b] - dpp[a - 1];
res %= 1000000007;
cout << (res + 1000000007) % 1000000007;
if (t) cout << "\n";
}
cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
return 0;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;
int main(void) {
int x, y, z;
cin >> x >> y >> z;
int a = x + z;
int b = y + z;
bool up = b < x, down = a < y;
if (up && !down && a != y) {
printf("+\n");
} else if (!up && down && b != x) {
cout << "-\n";
} else if (x == y && z == 0) {
puts("0");
} else {
puts("?");
}
return 0;
}
| 800 | CPP |
m=int(input())
dou=0
fir=[]
sec=[]
non=[]
d,f,s,n=0,0,0,0
for i in [0]*m:
a,b=map(int,input().split())
if a==11:
dou+=b
d+=1
elif a==1:
sec.append(b)
s+=1
elif a==10:
fir.append(b)
f+=1
else:
non.append(b)
n+=1
fir.sort(reverse=True)
sec.sort(reverse=True)
if not (d+f*s):
print(0)
quit()
else:
m=min(f,s)
ans=dou+sum(fir[:m])+sum(sec[:m])
non=non+fir[m:]+sec[m:]
non.sort(reverse=True)
print(ans+sum(non[:d]))
| 1,500 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, hello = "hello";
cin >> a;
int n = 0;
for (int i = 0; i < a.size(); ++i) {
if (a[i] == hello[n]) ++n;
if (n == hello.size()) break;
}
if (n == hello.size())
cout << "YES";
else
cout << "NO";
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
char x[1000005];
int n;
long long ans = 1;
int cur = 0;
int main() {
gets(x);
n = strlen(x);
for (int i = 0; i < n; i++) {
if (i + 1 < n && (x[i] - '0' + x[i + 1] - '0' == 9)) {
while (i + 1 < n && (x[i] - '0' + x[i + 1] - '0' == 9)) {
i++;
cur++;
}
}
if (cur != 0 && cur % 2 == 0) ans *= (cur / 2 + 1);
cur = 0;
}
cout << ans << endl;
return 0;
}
| 1,500 | CPP |
a = input()
a=a.replace("+", "")
p=""
y=[0]*len(a)
n=[0]*len(a)
t=[0]*len(a)
b = len(a)
for i in range(len(a)):
y[i] = int(a[i])
for i in range(len(a)):
t[i] = int(a[i])
for i in range(b-1,0,-1):
n[i]=max(y)
del y[y.index(max(y))]
n[0]=min(t)
for i in range(b):
p+=str(n[i])+"+"
p =p[:len(p)-1]
print(p) | 800 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
using namespace std;
using vi = vector<long long>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
long long powmod(long long a, long long b, long long mod) {
if (b == 0 || a == 1) {
if (mod == 1)
return 0;
else
return 1;
}
if (b % 2 == 0) {
long long k = powmod(a, b / 2, mod);
return (k * k) % mod;
} else {
long long k = powmod(a, b / 2, mod);
return ((k * k) % mod * a) % mod;
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
if (b == 0) return a;
if (a > b)
return gcd(a % b, b);
else
return gcd(b % a, a);
}
long long prime(long long p) {
for (long long i = 2; i * i <= p; i++) {
if (p % i == 0 && i < p) return i;
}
return 1;
}
long long inv(long long a, long long mod) { return powmod(a, mod - 2, mod); }
long long random_modul() {
uniform_int_distribution<long long> u1(1e9, 2e9);
long long pepega = u1(rnd);
while (prime(pepega) != 1) pepega = u1(rnd);
return pepega;
}
template <typename T1>
void ifmax(T1& max, T1 kek) {
if (kek > max) max = kek;
return;
}
template <typename T1>
void ifmin(T1& min, T1 kek) {
if (kek < min) min = kek;
return;
}
template <typename T1>
istream& operator>>(std::istream& in, vector<T1>& a) {
for (long long i = 0; i < a.size(); i++) in >> a[i];
return in;
}
template <typename T1>
ostream& operator<<(std::ostream& out, vector<T1>& b) {
for (long long i = 0; i < b.size(); i++) out << b[i];
return out;
}
template <typename T1>
void sort(vector<T1> m) {
sort(m.begin(), m.end());
}
template <typename T1>
void reverse(vector<T1>& m) {
reverse(m.begin(), m.end());
}
void solve() {
long long a, k;
cin >> a >> k;
vi m(a);
cin >> m;
vi pref(a);
for (long long i = 0; i < a; i++) {
if (i) pref[i] = pref[i - 1];
pref[i] += m[i];
}
long long ans = 0;
for (long long i = 0; i + k <= a; i++) {
long long q = pref[i + k - 1];
if (i) q -= pref[i - 1];
ans += q;
}
cout << (long double)ans / (long double)(a - k + 1);
return;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
long long tututu;
tututu = 1;
for (long long qwerty = 0; qwerty < tututu; qwerty++) solve();
return 0;
}
| 1,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int dimx[] = {-1, 1, 0, 0};
int dimy[] = {0, 0, 1, -1};
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else {
return gcd(b, a % b);
}
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long Pow(long long base, long long exponent) {
long long result = 1;
while (exponent > 0) {
if (exponent % 2 == 1) result = (result * base) % 1000000007;
exponent = exponent >> 1;
base = (base * base) % 1000000007;
}
return result;
}
long long PowerTwo(int number) {
int ans = 0, num = 1;
while (num < number) {
num *= 2;
ans++;
}
if (num == number)
return ans;
else
return -1;
}
int main() {
ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0),
cout.tie(0);
int n;
cin >> n;
string x;
cin >> x;
vector<int> vec;
for (long long c = 0; c < n; c++) {
int number = x[c] - '0';
int po = PowerTwo(number);
if (number > 2 && po != -1) {
for (long long c = 0; c < po; c++) vec.push_back(2);
vec.push_back(number - 1);
} else if (number == 6)
vec.push_back(5), vec.push_back(3);
else if (number == 9) {
vec.push_back(7);
vec.push_back(3), vec.push_back(3);
vec.push_back(2);
} else if (number > 1)
vec.push_back(number);
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (long long c = 0; c < vec.size(); c++) cout << vec[c];
return 0;
}
| 1,400 | CPP |
w=int(input())
if(w-2>0 and w%2==0):
print('YES')
else:
print('NO') | 800 | PYTHON3 |
a,b,c,d = map(int,input().split())
print(2*(a+b+d+2))
###### | 800 | PYTHON3 |
n,k=(map(int,input().split()))
s=list(input())
s1=list(set(s))
#s2=list(s1)
d={}
for x in range(len(s1)):
d[s1[x]]=s.count(s1[x])
if(k>=n):
print('YES')
elif(max(d.values())>k):
print('NO')
else:
print('YES')
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
static uniform_int_distribution<int> dist(0, 1e9 + 6);
return dist(rng);
}
template <class T>
void make_unique(T& v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() {
int x;
cin >> x;
return x;
}
long long getll() {
long long x;
cin >> x;
return x;
}
double getd() {
double x;
cin >> x;
return x;
}
const int MAXN = 3e5 + 100;
void solve(int tt) {}
long long cal(vector<long long> a) {
int n = a.size();
long long res = 0;
for (auto i = (0); i < (n); i++) {
long long tot = 0;
for (auto j = (i + 2); j < (n); j++) {
tot += a[j - 1];
if ((a[i] ^ a[j]) == tot && a[j] < a[i]) res++;
if (tot > 2 * a[i]) break;
}
}
return res;
}
int main(int argc, char* argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
for (auto i = (0); i < (n); i++) cin >> a[i];
long long res = 0;
res += cal(a);
reverse((a).begin(), (a).end());
res += cal(a);
cout << res << endl;
return 0;
}
| 2,500 | CPP |
w = input()
x = w.isupper()
if x == True:
x = w.lower()
else:
x = w[0].upper() + w[1:len(w)]
if x.isupper():
x = x.capitalize()
else:
x = w
print(x)
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
vector<vector<array<int, 2>>> v(N);
vector<int> vis(N), dis(N), low(N), block(N);
int cycle, edge;
vector<int> q, tmp, tmp2;
int tar = -1;
int dfs(int node, int p, int id) {
vis[node] = 1;
int f = 0;
for (auto i : v[node]) {
if (block[i[1]]) continue;
if (vis[i[0]] == 1) {
tar = i[0];
tmp.push_back(i[1]);
return 1;
} else if (vis[i[0]] == 0) {
int f1 = dfs(i[0], node, i[1]);
if (f1) f = 1;
if (f) {
if (tar != -1) {
tmp.push_back(i[1]);
if (node == tar) tar = -1;
}
break;
}
}
}
vis[node] = 2;
return f;
}
int detectCycle(int n) {
vis = vector<int>(n + 1);
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
tar = -1;
tmp.clear();
int chk = dfs(i, -1, -1);
if (chk) return 1;
}
return 0;
}
signed main() {
int n, m, x, y;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
v[x].push_back({y, i});
}
cycle = detectCycle(n);
if (cycle) {
tmp2 = tmp;
for (auto i : tmp2) {
if (i == -1) continue;
block[i] = 1;
if (!detectCycle(n)) {
cout << "YES";
return 0;
}
block[i] = 0;
}
} else {
cout << "YES";
return 0;
}
cout << "NO";
}
| 2,200 | CPP |
k, n, w = map(int, input().split())
if ((sum([i for i in range(w+1)]) * k) - n) > 0:
print((sum([i for i in range(w+1)]) * k) - n)
else:
print(0)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
int t;
cin >> t;
while (t--) {
long long n, k;
cin >> n >> k;
if (n % 2 != k % 2) {
cout << "NO\n";
continue;
}
if (k * k <= n) {
cout << "YES\n";
} else
cout << "NO\n";
}
}
| 1,100 | CPP |
Subsets and Splits