context stringlengths 88 7.54k | groundtruth stringlengths 9 28.8k | groundtruth_language stringclasses 3
values | type stringclasses 2
values | code_test_cases listlengths 1 565 ⌀ | dataset stringclasses 6
values | code_language stringclasses 1
value | difficulty float64 0 1 ⌀ | mid stringlengths 32 32 |
|---|---|---|---|---|---|---|---|---|
Ivan has number b. He is sorting through the numbers a from 1 to 10^{18}, and for every a writes ([a, b])/(a) on blackboard. Here [a, b] stands for least common multiple of a and b. Ivan is very lazy, that's why this task bored him soon. But he is interested in how many different numbers he would write on the boa... | b=int(input())
out=[1]
n=b
i=0
while n%2==0:
i=i+1
out.append(2**i)
n=int(n/2)
out1=[]
for i in range (1,int(n**0.5)+1,2):
if n%i==0:
out1.append(i)
out1.append(int(n/i))
out2=set()
for i in out:
for j in out1:
out2.add(i*j)
#print (out2)
print (len(out2))
| python | code_algorithm | [
{
"input": "1\n",
"output": "1\n"
},
{
"input": "2\n",
"output": "2\n"
},
{
"input": "244078129\n",
"output": "9\n"
},
{
"input": "2793510720\n",
"output": "1792\n"
},
{
"input": "16\n",
"output": "5\n"
},
{
"input": "179\n",
"output": "2\n"
},
... | code_contests | python | 0.8 | 03d8ebc34bd6cbaeb5d336f0b63b63f7 |
You are given a string s=s_1s_2... s_n of length n, which only contains digits 1, 2, ..., 9.
A substring s[l ... r] of s is a string s_l s_{l + 1} s_{l + 2} … s_r. A substring s[l ... r] of s is called even if the number represented by it is even.
Find the number of even substrings of s. Note, that even if some subs... | n = int(input())
s = input()
k = 0
for i in range(1, n + 1):
d = int(s[i - 1])
if d % 2 == 0:
k += i
print(k)
| python | code_algorithm | [
{
"input": "4\n1234\n",
"output": "6\n"
},
{
"input": "4\n2244\n",
"output": "10\n"
},
{
"input": "3\n112\n",
"output": "3\n"
},
{
"input": "3\n121\n",
"output": "2\n"
},
{
"input": "3\n122\n",
"output": "5\n"
},
{
"input": "39\n26325466335986448332457... | code_contests | python | 0.9 | c24adbfb24331254f9ddb4c102bfd538 |
Mr. Scrooge, a very busy man, decided to count the time he wastes on all sorts of useless stuff to evaluate the lost profit. He has already counted the time he wastes sleeping and eating. And now Mr. Scrooge wants to count the time he has wasted signing papers.
Mr. Scrooge's signature can be represented as a polyline ... |
n,k = list(map(int,input().split()))
ans = 0
for i in range(n):
if i==0:
x,y= list(map(int,input().split()))
else:
X,Y = list(map(int,input().split()))
d = ((X-x)**2+(Y-y)**2)**(1/2)
x,y=X,Y
ans += d
ans=((ans*k)/50)
print('%.9f'%ans) | python | code_algorithm | [
{
"input": "2 1\n0 0\n10 0\n",
"output": "0.200000000\n"
},
{
"input": "6 10\n5 0\n4 0\n6 0\n3 0\n7 0\n2 0\n",
"output": "3.000000000\n"
},
{
"input": "5 10\n3 1\n-5 6\n-2 -1\n3 2\n10 0\n",
"output": "6.032163204\n"
},
{
"input": "48 447\n14 9\n9 -17\n-17 11\n-14 14\n19 -8\n-... | code_contests | python | 0.7 | 115d9342dc01b098e4f7d07d355f8b48 |
You are given two integers x and y. You can perform two types of operations:
1. Pay a dollars and increase or decrease any of these integers by 1. For example, if x = 0 and y = 7 there are four possible outcomes after this operation:
* x = 0, y = 6;
* x = 0, y = 8;
* x = -1, y = 7;
* x = 1, y = ... | def to_list(s):
return list(map(lambda x: int(x), s.split(' ')))
def solve(x,y,a,b):
cost = 0
if b <= 2*a:
min_val = min(x,y)
cost += b*min_val
x -= min_val
y -= min_val
max_val = max(x,y)
cost += max_val*a
else:
cost = (x+y)*a
print(cost)
... | python | code_algorithm | [
{
"input": "2\n1 3\n391 555\n0 0\n9 4\n",
"output": "1337\n0\n"
},
{
"input": "1\n129 8437\n5 3\n",
"output": "41927\n"
},
{
"input": "3\n1 3\n391 555\n129 8437\n9 4\n321 654\n3 4\n",
"output": "1337\n75288\n2283\n"
},
{
"input": "2\n1 3\n391 555\n129 8437\n9 4\n",
"outpu... | code_contests | python | 1 | 63c6eac0bb077a8d321b9113adc0e093 |
You are given a string s consisting of lowercase Latin letters "a", "b" and "c" and question marks "?".
Let the number of question marks in the string s be k. Let's replace each question mark with one of the letters "a", "b" and "c". Here we can obtain all 3^{k} possible strings consisting only of letters "a", "b" and... | n=int(input())
s=input()
a=[[0,0,0] for i in range(4)]
k=s.count('?')
b=[1]
for i in range(k):
b.append(b[i]*3%1000000007)
for i in range(n):
if s[i]=='a':
a[0][0]+=1
elif s[i]=='b':
a[0][1]+=a[0][0]
a[1][1]+=a[1][0]
elif s[i]=='c':
a[0][2]+=a[0][1]
a[1][2]+=a[1][... | python | code_algorithm | [
{
"input": "9\ncccbbbaaa\n",
"output": "0\n"
},
{
"input": "7\n???????\n",
"output": "2835\n"
},
{
"input": "5\na???c\n",
"output": "46\n"
},
{
"input": "6\nac?b?c\n",
"output": "24\n"
},
{
"input": "3\nabc\n",
"output": "1\n"
},
{
"input": "200\n???c?... | code_contests | python | 0 | 9d7642d37be96f98ed35a19c1f477354 |
You are given a string s of length n. Each character is either one of the first k lowercase Latin letters or a question mark.
You are asked to replace every question mark with one of the first k lowercase Latin letters in such a way that the following value is maximized.
Let f_i be the maximum length substring of str... | import sys
input = sys.stdin.readline
n, k = map(int, input().split())
s = input()[:-1]
left, right = 0, n
while left < right:
mid = right - (right - left) // 2
A = [[0] * (n + 2) for _ in range(k)]
for c in range(k):
A[c][n] = A[c][n + 1] = n + 1
L = 0
for i in range(n - 1, -1, -1... | python | code_algorithm | [
{
"input": "2 3\n??\n",
"output": "0\n"
},
{
"input": "10 2\na??ab????b\n",
"output": "4\n"
},
{
"input": "9 4\n?????????\n",
"output": "2\n"
},
{
"input": "4 4\ncabd\n",
"output": "1\n"
},
{
"input": "15 3\n??b?babbc??b?aa\n",
"output": "3\n"
},
{
"in... | code_contests | python | 0 | d1a194d168e55633d7d7c95c29cb00d3 |
The Smart Beaver from ABBYY decided to have a day off. But doing nothing the whole day turned out to be too boring, and he decided to play a game with pebbles. Initially, the Beaver has n pebbles. He arranges them in a equal rows, each row has b pebbles (a > 1). Note that the Beaver must use all the pebbles he has, i. ... | n=int(input())
r=n+1
i=2
s=n**0.5
while i<=s:
if n%i==0:
r+=n//i
n//=i
s=n**0.5
i=1
i+=1
print(r) | python | code_algorithm | [
{
"input": "10\n",
"output": "16\n"
},
{
"input": "8\n",
"output": "15\n"
},
{
"input": "999161856\n",
"output": "1998319610\n"
},
{
"input": "999999937\n",
"output": "999999938\n"
},
{
"input": "9\n",
"output": "13\n"
},
{
"input": "999936000\n",
... | code_contests | python | 0 | 1865e92ba2b87c35ab63f9ab2d346cdf |
The Little Elephant enjoys recursive functions.
This time he enjoys the sorting function. Let a is a permutation of an integers from 1 to n, inclusive, and ai denotes the i-th element of the permutation. The Little Elephant's recursive function f(x), that sorts the first x permutation's elements, works as follows:
... | n=int(input())
l=list(range(1,n+1))
l=sorted(l)
l.insert(0,l[-1])
l.pop()
print(*l) | python | code_algorithm | [
{
"input": "1\n",
"output": "1\n"
},
{
"input": "2\n",
"output": "2 1\n"
},
{
"input": "1000\n",
"output": "1000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 ... | code_contests | python | 0 | ce76ea56cba21ef6bb8c3aca7d0efc46 |
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the resea... | #lahub and Permutations
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
mod = 10**9+7
def pow(n,p,mod=mod): #繰り返し二乗法(nのp乗)
res = 1
while p > 0:
if p % 2 == 0:
n = n ** 2 % mod
p //= 2
else:
res = res * n % mod
... | python | code_algorithm | [
{
"input": "5\n-1 -1 4 3 -1\n",
"output": "2\n"
},
{
"input": "6\n-1 -1 -1 -1 -1 -1\n",
"output": "265\n"
},
{
"input": "7\n-1 -1 4 -1 7 1 6\n",
"output": "4\n"
},
{
"input": "10\n4 10 -1 1 6 8 9 2 -1 -1\n",
"output": "4\n"
},
{
"input": "8\n2 4 5 3 -1 8 -1 6\n",
... | code_contests | python | 0 | 3a9c1ef04882a39efc5114aa3f2ce5ea |
Fox Ciel has n boxes in her room. They have the same size and weight, but they might have different strength. The i-th box can hold at most xi boxes on its top (we'll call xi the strength of the box).
Since all the boxes have the same size, Ciel cannot put more than one box directly on the top of some box. For exampl... | I=lambda:list(map(int,input().split()))
n,=I()
l=I()
l.sort()
ans=0
i=0
k=1
while i<n:
if l[i]<i//k:
k+=1
i+=1
print(k)
| python | code_algorithm | [
{
"input": "9\n0 1 0 2 0 1 1 2 10\n",
"output": "3\n"
},
{
"input": "5\n0 1 2 3 4\n",
"output": "1\n"
},
{
"input": "3\n0 0 10\n",
"output": "2\n"
},
{
"input": "4\n0 0 0 0\n",
"output": "4\n"
},
{
"input": "59\n61 33 84 76 56 47 70 94 46 77 95 85 35 90 83 62 48 7... | code_contests | python | 0 | 76d8a6628dff1f8740edfe92ce597a82 |
One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.
The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (... | n = int(input())
a = list(map(int,input().split()))
values = [0 for k in range(n)]
for k in range(n) :
back = a[k]
if a[k] == k+1 :
values[k] = 2
else :
values[k] += 2
for t in range(a[k]-1,k) :
values [k] += values[t]
values[k] %= int(1e9 +7)
print(in... | python | code_algorithm | [
{
"input": "2\n1 2\n",
"output": "4\n"
},
{
"input": "5\n1 1 1 1 1\n",
"output": "62\n"
},
{
"input": "4\n1 1 2 3\n",
"output": "20\n"
},
{
"input": "3\n1 1 3\n",
"output": "8\n"
},
{
"input": "102\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1... | code_contests | python | 0 | f8f979220f0dd8ca6a7f350d959e24ac |
Kyoya Ootori is selling photobooks of the Ouran High School Host Club. He has 26 photos, labeled "a" to "z", and he has compiled them into a photo booklet with some photos in some order (possibly with some photos being duplicated). A photo booklet can be described as a string of lowercase letters, consisting of the pho... | s = input()
l = list(s)
a = 'abcdefghijklmnopqrstuvwxyz'
for i in range(len(s)+1):
for j in a:
l.insert(i, j)
print(len(l)-2*len(s)) | python | code_algorithm | [
{
"input": "hi\n",
"output": "76\n"
},
{
"input": "a\n",
"output": "51\n"
},
{
"input": "lpfpndmjfvqejdgf\n",
"output": "426\n"
},
{
"input": "jgv\n",
"output": "101\n"
},
{
"input": "fykkiubdkt\n",
"output": "276\n"
},
{
"input": "ydzpjhsidipricw\n",
... | code_contests | python | 0.9 | ed07c37f3178ec5fbd0bab53d1073a18 |
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
* the i-th letter occurs in the string no more than ai times;
* the number of occurrences of each letter in the string must be distinct for all the lette... | n = int(input())
m = list(map(int, input().split()))
m.sort(reverse = True)
ans = m[0]
last = m[0]
for i in range(1, len(m)):
last = max(min(last - 1, m[i]), 0)
ans += last
print(ans) | python | code_algorithm | [
{
"input": "3\n2 5 5\n",
"output": "11\n"
},
{
"input": "3\n1 1 2\n",
"output": "3\n"
},
{
"input": "26\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000... | code_contests | python | 0.4 | 445bb0737d6fdf438dec8e426487166f |
We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor.
There are n citizens in Kekoland, each person has ci coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to t... | import sys
sys.stderr = sys.stdout
def hood(n, k, C):
C.sort()
m, r = divmod(sum(C), n)
m1 = (m + 1) if r else m
c_lo = C[0]
k_lo = k
for i, c in enumerate(C):
if c_lo == m:
break
c_m = min(c, m)
dc = c_m - c_lo
dk = i * dc
if k_lo >= dk:
... | python | code_algorithm | [
{
"input": "4 1\n1 1 4 2\n",
"output": "2\n"
},
{
"input": "3 1\n2 2 2\n",
"output": "0\n"
},
{
"input": "10 1000000\n307196 650096 355966 710719 99165 959865 500346 677478 614586 6538\n",
"output": "80333\n"
},
{
"input": "4 0\n1 4 4 4\n",
"output": "3\n"
},
{
"i... | code_contests | python | 0 | ca165b7770003fe4432919710cf0f7af |
Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning M... | import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
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" ... | python | code_algorithm | [
{
"input": "3 2\n1 1\n1 2 3\n2 3 3\n",
"output": "2\n3\n"
},
{
"input": "4 1\n1 2 3\n1 2 3\n",
"output": "2\n"
},
{
"input": "5 20\n5 5 1 4\n1 4 3\n2 4 1\n1 5 5\n5 1 4\n5 1 5\n1 5 5\n5 4 4\n2 3 3\n4 4 1\n1 4 1\n4 5 4\n1 4 5\n4 1 5\n2 4 2\n4 3 3\n2 5 5\n1 5 4\n3 3 4\n5 5 1\n3 4 1\n",
... | code_contests | python | 0 | a3a1a7ebbe58ce29a50e04615c5b836d |
In Ann's favorite book shop are as many as n books on math and economics. Books are numbered from 1 to n. Each of them contains non-negative number of problems.
Today there is a sale: any subsegment of a segment from l to r can be bought at a fixed price.
Ann decided that she wants to buy such non-empty subsegment t... | # ------------------- fast io --------------------
import os
import sys
from io import BytesIO, IOBase
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.... | python | code_algorithm | [
{
"input": "4 1\n1 1 1 2\n1 1 1 1\n4\n1 2\n1 3\n1 4\n3 4\n",
"output": "2\n3\n4\n1\n"
},
{
"input": "4 0\n1 2 1 2\n0 0 0 0\n1\n1 4\n",
"output": "10\n"
},
{
"input": "2 0\n1 2\n43 43\n3\n1 2\n2 2\n1 1\n",
"output": "1\n0\n0\n"
},
{
"input": "10 -10\n1 2 1 2 1 1 2 2 2 1\n7 7 1... | code_contests | python | 0.4 | 18c54781fb26a721afc6bb12e04009c6 |
Input
The input contains a single integer a (10 ≤ a ≤ 999).
Output
Output 0 or 1.
Examples
Input
13
Output
1
Input
927
Output
1
Input
48
Output
0
Write Python code to solve the problem. Present the code in the code block:
```python
Your code
```
Write Python code to solve the problem. Present ... | b=int(input())
if b%2==0:
print(0)
else:
print(1) | python | code_algorithm | [
{
"input": "13\n",
"output": "1\n"
},
{
"input": "48\n",
"output": "0\n"
},
{
"input": "927\n",
"output": "1\n"
},
{
"input": "758\n",
"output": "0\n"
},
{
"input": "496\n",
"output": "0\n"
},
{
"input": "957\n",
"output": "1\n"
},
{
"input... | code_contests | python | 0 | 2a706825e04213697de015134945e472 |
Kuro is living in a country called Uberland, consisting of n towns, numbered from 1 to n, and n - 1 bidirectional roads connecting these towns. It is possible to reach each town from any other. Each road connects two towns a and b. Kuro loves walking and he is planning to take a walking marathon, in which he will choos... |
def calculate_max_paths(edges, node, dest, par, cnt):
ans = 1
for child in edges.get(node, []):
if child != par:
ans += calculate_max_paths(edges, child, dest, node, cnt)
if dest == node:
cnt[0] = ans
return ans
def main():
from collections import defaultdict
... | python | code_algorithm | [
{
"input": "3 1 3\n1 2\n2 3\n",
"output": "5\n"
},
{
"input": "3 1 3\n1 2\n1 3\n",
"output": "4\n"
},
{
"input": "31 29 20\n29 23\n29 18\n22 14\n29 20\n1 21\n29 10\n28 2\n1 17\n17 15\n1 11\n29 31\n28 6\n12 29\n12 26\n1 13\n22 4\n29 25\n28 22\n17 5\n28 30\n20 27\n29 8\n12 28\n1 12\n12 24\... | code_contests | python | 0.1 | d43935f6dabc6404cc535bc9a0c9272c |
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit.
Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers ... | def get(n,m,a,b,t):
freq=[0]*(t+1)
for i in range(n):
for j in range(m):
val=abs(i-a)+abs(j-b)
freq[val]+=1
return freq
t=int(input())
a=list(map(int,input().split()))
mx=max(a)
f=[0]*(t+1)
for i in a:
f[i]+=1
b=1
for i in range(1,mx+1):
if f[i]!=4*i:
b=i
... | python | code_algorithm | [
{
"input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n",
"output": "4 5\n2 2\n"
},
{
"input": "6\n2 1 0 2 1 2\n",
"output": "-1\n"
},
{
"input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n",
"output": "3 6\n2 3\n"
},
{
"input": "1\n0\n",
"output": "1 1\n1 1\n"
},
{
... | code_contests | python | 0.2 | 4e0621a8d4e698774d026a61c1d08196 |
Vova's family is building the Great Vova Wall (named by Vova himself). Vova's parents, grandparents, grand-grandparents contributed to it. Now it's totally up to Vova to put the finishing touches.
The current state of the wall can be respresented by a sequence a of n integers, with a_i being the height of the i-th par... | n=int(input())
a=list(map(int,input().split()))
down=[]
good=True
for guy in a:
if len(down)==0:
down.append(guy)
elif down[-1]>guy:
down.append(guy)
elif down[-1]==guy:
down.pop()
else:
good=False
break
if not good:
print("NO")
elif len(down)>1:
print("NO... | python | code_algorithm | [
{
"input": "2\n10 10\n",
"output": "YES\n"
},
{
"input": "3\n4 5 3\n",
"output": "NO\n"
},
{
"input": "5\n2 1 1 2 5\n",
"output": "YES\n"
},
{
"input": "1\n1\n",
"output": "YES\n"
},
{
"input": "3\n2 2 1\n",
"output": "NO\n"
},
{
"input": "13\n5 2 2 1 ... | code_contests | python | 0 | f0cffad4d85dac1a3f0c819f3b1f2444 |
Polycarp plays "Game 23". Initially he has a number n and his goal is to transform it to m. In one move, he can multiply n by 2 or multiply n by 3. He can perform any number of moves.
Print the number of moves needed to transform n to m. Print -1 if it is impossible to do so.
It is easy to prove that any way to trans... | def fun(a,b):
if a % b != 0:
return -1
temp = 0
m = a/b
while m % 2 == 0:
m = m / 2
temp += 1
while m % 3 == 0:
m = m/3
temp += 1
if m == 1:
return temp
else :
return -1
a,b = map(int,input().split())
print(fun(b,a)) | python | code_algorithm | [
{
"input": "42 42\n",
"output": "0\n"
},
{
"input": "48 72\n",
"output": "-1\n"
},
{
"input": "120 51840\n",
"output": "7\n"
},
{
"input": "1 223092870\n",
"output": "-1\n"
},
{
"input": "18782 37565\n",
"output": "-1\n"
},
{
"input": "1001 1001\n",
... | code_contests | python | 0.1 | 8b79110090e817fec8d67fa2d09a5ce3 |
Alicia has an array, a_1, a_2, …, a_n, of non-negative integers. For each 1 ≤ i ≤ n, she has found a non-negative integer x_i = max(0, a_1, …, a_{i-1}). Note that for i=1, x_i = 0.
For example, if Alicia had the array a = \{0, 1, 2, 0, 3\}, then x = \{0, 0, 1, 2, 2\}.
Then, she calculated an array, b_1, b_2, …, b_n: ... | input()
b = list(map(int,input().split()))
a = list()
sumnum = 0
negnum = 0
for i in range(len(b)):
sumnum += b[i]
a.append(sumnum+abs(negnum))
if b[i]<0:
negnum += b[i]
print(*a) | python | code_algorithm | [
{
"input": "3\n1000 999999000 -1000000000\n",
"output": "1000 1000000000 0\n"
},
{
"input": "5\n2 1 2 2 3\n",
"output": "2 3 5 7 10\n"
},
{
"input": "5\n0 1 1 -2 1\n",
"output": "0 1 2 0 3\n"
},
{
"input": "35\n15 13 -19 -7 -28 -16 4 -10 2 -23 -6 -5 -26 -27 -24 -27 -22 1 -32 ... | code_contests | python | 0.7 | 494852516832b3ad88d9dc3f58b679b3 |
Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simple... | import math
n = int(input())
mx = 0
mn = 4 * 10**18
for A in range(1, n+1):
if A * A * A > n:
break
if n % A:
continue
nn = n // A
for B in range(1, nn+1):
if B * B > nn:
break
if nn % B:
continue
C = nn // B
mn = min(mn, (A + 1) * (B + 2) * (C + 2) - n)
mx = max(mx, (A + 1) * (B + 2) * (C +... | python | code_algorithm | [
{
"input": "12\n",
"output": "48 105\n"
},
{
"input": "4\n",
"output": "28 41\n"
},
{
"input": "7\n",
"output": "47 65\n"
},
{
"input": "999999993\n",
"output": "490196227 7999999953\n"
},
{
"input": "257259713\n",
"output": "2122207 2058077713\n"
},
{
... | code_contests | python | 0 | 0d23e4528f79bcfb7d77e2ae9dae2135 |
After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to celebrate his birthday. We know that the i-th group consists of si friends (1 ≤ si ≤ 4), and they want to go to Polycarpus together. They decided to get there by taxi. Each car can carry at most four passengers. What minimum nu... | n=int(input())
a=list(map(int,input().split()))
s=[0]*5
sum=0
for i in a: s[i]+=1
sum+=s[4]+s[3]
if s[3]<s[1]:
s[1]-=s[3]
s[3]=0
else :
s[3]=0
s[1]=0
sum+=s[2]//2
s[2]%=2
sum+=s[1]//4
s[1]%=4
end=s[2]*2+s[1]
if end>4:sum+=2
elif end>0: sum+=1
print(int(sum))
| python | code_algorithm | [
{
"input": "5\n1 2 4 3 3\n",
"output": "4\n"
},
{
"input": "8\n2 3 4 4 2 1 3 1\n",
"output": "5\n"
},
{
"input": "5\n4 4 4 4 4\n",
"output": "5\n"
},
{
"input": "3\n3 4 3\n",
"output": "3\n"
},
{
"input": "2\n2 1\n",
"output": "1\n"
},
{
"input": "78\n... | code_contests | python | 0.2 | 5245e7e0b993356f921cbf5d88a2d690 |
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < k|x| ≤ |s|).
You've got two strings — s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at... | import bisect
import string
s = input()
t = input()
max_match = [0 for i in range(len(s))]
min_match = [0 for i in range(len(s))]
char_idx = [0 for i in range(30)]
char_occur = [ [] for i in range(30) ]
for (i, ch) in enumerate(t):
idx = ord(ch) - ord('a')
char_occur[idx].append(i)
for ch in string.ascii_lo... | python | code_algorithm | [
{
"input": "abc\nba\n",
"output": "No\n"
},
{
"input": "abacaba\naba\n",
"output": "No\n"
},
{
"input": "abab\nab\n",
"output": "Yes\n"
},
{
"input": "babaabaabb\nbbccb\n",
"output": "No\n"
},
{
"input": "aaaaaa\naaaaaaa\n",
"output": "No\n"
},
{
"inpu... | code_contests | python | 0 | cd845608c82c5e4f5670a6f1c36974ce |
For he knew every Who down in Whoville beneath, Was busy now, hanging a mistletoe wreath. "And they're hanging their stockings!" he snarled with a sneer, "Tomorrow is Christmas! It's practically here!"
Dr. Suess, How The Grinch Stole Christmas
Christmas celebrations are coming to Whoville. Cindy Lou Who and her paren... | def check(n, casas):
#print('n:',n)
global T,N,street
current = n
time = T
need = 0
last_house = 0
for ind, i in enumerate(street):
time -= 1
if i == 'S':
current += 1
elif i == 'H':
need += 1
if need == ... | python | code_algorithm | [
{
"input": "23 50\nHHSS.......SSHHHHHHHHHH\n",
"output": "8\n"
},
{
"input": "14 100\n...HHHSSS...SH\n",
"output": "0\n"
},
{
"input": "6 6\nHSHSHS\n",
"output": "1\n"
},
{
"input": "336 400\nHSSHHSSHHS..S..SS..H.S.H..SSH.H.H.H.S.H.S.S.H.S..SS..H.SS.H.SS.SS..SS.H...H.H.H.HSS.... | code_contests | python | 0 | 0035c0d54c04c8e1468d249cbea1f22c |
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will co... | import sys
import string
import math
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache
from fractions import Fraction
def mi(s):
return map(int, s.strip().split())
def lmi(s):
return list(mi(s))
def tmi(s):
retur... | python | code_algorithm | [
{
"input": "1 1\n",
"output": "1\n"
},
{
"input": "199 200\n",
"output": "200\n"
},
{
"input": "3 2\n",
"output": "3\n"
},
{
"input": "36 316049483082136289\n",
"output": "8779152307837131\n"
},
{
"input": "1 923438\n",
"output": "923438\n"
},
{
"input... | code_contests | python | 0.5 | 32545d0679c6069822078564ae51224b |
Devu is a dumb guy, his learning curve is very slow. You are supposed to teach him n subjects, the ith subject has ci chapters. When you teach him, you are supposed to teach all the chapters of a subject continuously.
Let us say that his initial per chapter learning power of a subject is x hours. In other words he can... | n, x = map(int, input().split())
m = list(map(int, input().split()))
m.sort()
ans = 0
for i in range(n):
ans += m[i] * x
if x > 1:
x -= 1
print(ans)
| python | code_algorithm | [
{
"input": "3 3\n1 1 1\n",
"output": "6\n"
},
{
"input": "4 2\n5 1 2 1\n",
"output": "10\n"
},
{
"input": "2 3\n4 1\n",
"output": "11\n"
},
{
"input": "1 1\n1\n",
"output": "1\n"
},
{
"input": "1 1\n9273\n",
"output": "9273\n"
},
{
"input": "20 10\n6 6... | code_contests | python | 0.6 | 2646b749e29b17c457401af721a9a525 |
Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one can... | import sys
ghosts, duration, candles = input().split()
arrival = input().split()
burn = []
for j in range(len(arrival)):
time = int(arrival[len(arrival) - 1 - j])
candle = int(candles)
if len(burn) != 0:
for k in range(len(burn)):
if burn[k] <= time:
candle -= 1
for ... | python | code_algorithm | [
{
"input": "1 1 3\n10\n",
"output": "-1\n"
},
{
"input": "1 8 3\n10\n",
"output": "3\n"
},
{
"input": "2 10 1\n5 8\n",
"output": "1\n"
},
{
"input": "21 140 28\n40 46 58 67 71 86 104 125 129 141 163 184 193 215 219 222 234 237 241 246 263\n",
"output": "56\n"
},
{
... | code_contests | python | 0.1 | 1fa347e6ca2dafac86b2191928bd9e12 |
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi).
They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan consider... | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
def inc_val_of_dict(dictionary, key):
if key not in dictionary:
dictionary[key] = 0
dictionary[key] += 1
def main():
n = int(input())
p_ctr = dict()
x_ctr = dict()
y_ctr = dict()
for _ in range(n):
x, y = map(int, input().spl... | python | code_algorithm | [
{
"input": "3\n1 1\n7 5\n1 5\n",
"output": "2\n"
},
{
"input": "6\n0 0\n0 1\n0 2\n-1 1\n0 1\n1 1\n",
"output": "11\n"
},
{
"input": "2\n2 1\n1 2\n",
"output": "0\n"
},
{
"input": "2\n1 4\n2 1\n",
"output": "0\n"
},
{
"input": "10\n46 -55\n46 45\n46 45\n83 -55\n46 ... | code_contests | python | 0.1 | b0fffb0e90f9900dae5232ccdea1a87e |
Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won't be noticed by the guard. The height of the... | lin1 = input().split()
h = input().split()
for i in range(len(h)):
h[i] = int(h[i])
n_friends = int(lin1[0])
h_fence = int(lin1[1])
total = 0
for i in h:
if i > h_fence: total += 2
else: total += 1
print(total) | python | code_algorithm | [
{
"input": "3 7\n4 5 14\n",
"output": "4\n"
},
{
"input": "6 1\n1 1 1 1 1 1\n",
"output": "6\n"
},
{
"input": "6 5\n7 6 8 9 10 5\n",
"output": "11\n"
},
{
"input": "10 561\n657 23 1096 487 785 66 481 554 1000 821\n",
"output": "15\n"
},
{
"input": "10 420\n214 614... | code_contests | python | 1 | c855705820f8fbf7a5beacb3548a4432 |
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.
Vladik knows n airports. All the airports are located on a straight line. Each airport has uni... | n, a, b = list(map(int, input().split()))
line = []
aa = input()
for i in range(n):
line += [int(aa[i])]
print(0 + (line[a-1] != line[b-1])) | python | code_algorithm | [
{
"input": "4 1 4\n1010\n",
"output": "1\n"
},
{
"input": "5 5 2\n10110\n",
"output": "0\n"
},
{
"input": "6 1 6\n111000\n",
"output": "1\n"
},
{
"input": "10 5 8\n1000001110\n",
"output": "1\n"
},
{
"input": "5 1 5\n11010\n",
"output": "1\n"
},
{
"inp... | code_contests | python | 0.9 | d554fdd0f2d788932a4ab2462192a43b |
Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number i in the English alpha... | N, s, l = int(input()), [ord(x) - ord('a') for x in input()], [int(x) for x in input().split()]
arr = [[1, l[s[0]]]]
total = 1
ma = 1
t = 1
mi = 1
for c in s[1:]:
tmp = 0
for i in range(len(arr)):
arr[i][1] = min(arr[i][1],l[c])
if i + 1 >= arr[i][1]:
arr = arr[:i]
if(t > i):
t = 0
mi += 1
break
... | python | code_algorithm | [
{
"input": "3\naab\n2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "3\n2\n2\n"
},
{
"input": "10\nabcdeabcde\n5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n",
"output": "401\n4\n3\n"
},
{
"input": "10\naabaabaaba\n3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... | code_contests | python | 0.1 | d4c404327f5feae54912993f4b97c1c4 |
You are given a text of single-space separated words, consisting of small and capital Latin letters.
Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.
Calculate the volume of the given text.
Input
The first line contains one integer number n ... | n = int(input())
text = input()
ll = list(text.split())
num = []
for i in range(len(ll)):
num.append(sum(1 for c in ll[i] if c.isupper()))
print(max(num))
| python | code_algorithm | [
{
"input": "24\nthis is zero answer text\n",
"output": "0\n"
},
{
"input": "7\nNonZERO\n",
"output": "5\n"
},
{
"input": "24\nHarbour Space University\n",
"output": "1\n"
},
{
"input": "15\naAb ABCDFGRHTJS\n",
"output": "11\n"
},
{
"input": "24\nHarbour Space Univ... | code_contests | python | 0.6 | 93c04f6448a0429d60ff5dddc95d23ab |
You are given a sequence of positive integers a1, a2, ..., an.
While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to x, delete both and insert a s... | # https://codeforces.com/problemset/problem/926/E
input()
stack1 = [int(i) for i in input().split()]
stack1.reverse()
stack2 = []
while stack1:
transfer = stack1.pop()
while stack2 and stack2[-1] == transfer:
stack2.pop()
transfer += 1
stack2.append(transfer)
print(len(stack2))
print("... | python | code_algorithm | [
{
"input": "7\n4 10 22 11 12 5 6\n",
"output": "7\n4 10 22 11 12 5 6\n"
},
{
"input": "6\n5 2 1 1 2 2\n",
"output": "2\n5 4\n"
},
{
"input": "4\n1000000000 1000000000 1000000000 1000000000\n",
"output": "1\n1000000002\n"
},
{
"input": "2\n1 1\n",
"output": "1\n2\n"
},
... | code_contests | python | 0 | e19cfa7a2cf7763e7adc10db06fba31b |
There are n slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.
Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).
When a slime with a value x eats a slime with a value y, the eaten slime disappears, and ... | import sys
import os
def solve(slimes):
if len(slimes) == 1:
return slimes[0]
havePos = False
haveNeg = False
for s in slimes:
if s > 0:
havePos = True
elif s < 0:
haveNeg = True
if havePos and haveNeg:
return sum(map(abs, slimes))
elif... | python | code_algorithm | [
{
"input": "5\n0 -1 -1 -1 -1\n",
"output": "4\n"
},
{
"input": "4\n2 1 2 1\n",
"output": "4\n"
},
{
"input": "2\n-4 -5\n",
"output": "1\n"
},
{
"input": "10\n-20 0 3 -5 -18 15 -3 -9 -7 9\n",
"output": "89\n"
},
{
"input": "2\n-2 -2\n",
"output": "0\n"
},
{... | code_contests | python | 0 | 5ca29945a889b16107aeeed4645e1ebb |
You are given an integer sequence 1, 2, ..., n. You have to divide it into two sets A and B in such a way that each element belongs to exactly one set and |sum(A) - sum(B)| is minimum possible.
The value |x| is the absolute value of x and sum(S) is the sum of elements of the set S.
Input
The first line of the input ... | def check(n):
if (n // 2) % 2 == 1:
return 0
return 1
n = int(input())
if n % 2 == 1:
print(check(n))
else:
print(check(n - 1))
| python | code_algorithm | [
{
"input": "6\n",
"output": "1\n"
},
{
"input": "5\n",
"output": "1\n"
},
{
"input": "3\n",
"output": "0\n"
},
{
"input": "46369\n",
"output": "1\n"
},
{
"input": "1000003346\n",
"output": "1\n"
},
{
"input": "4\n",
"output": "0\n"
},
{
"in... | code_contests | python | 0.6 | b41caf42921dde5d4b3fa5a0aabfe5c1 |
One day Alice was cleaning up her basement when she noticed something very curious: an infinite set of wooden pieces! Each piece was made of five square tiles, with four tiles adjacent to the fifth center tile:
<image> By the pieces lay a large square wooden board. The board is divided into n^2 cells arranged into n ... | n = int(input())
A = []
for i in range (n):
s = input()
A.append('#' + s + '#')
A.append('#' * (n + 2))
A.append('#' * (n + 2))
for i in range (n):
for j in range (1, n + 1):
#for k in range (n + 2):
# print(A[k])
#print(i, j)
#print()
#print()
if A[i][j] =... | python | code_algorithm | [
{
"input": "3\n#.#\n...\n#.#\n",
"output": "YES\n"
},
{
"input": "5\n#.###\n....#\n#....\n....#\n#..##\n",
"output": "NO\n"
},
{
"input": "4\n##.#\n#...\n####\n##.#\n",
"output": "NO\n"
},
{
"input": "5\n#.###\n....#\n#....\n###.#\n#####\n",
"output": "YES\n"
},
{
... | code_contests | python | 0.1 | 1574290cf0cfd9d8e7106c13a8134a33 |
Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had n1 footmen and n2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that k1 footmen standing successively one after another, or there are strictly more than k2 horsemen... | R = lambda: map(int, input().split())
n1, n2, k1, k2 = R()
k1 = min(k1, n1)
k2 = min(k2, n2)
dp = [[[0] * 2 for j in range(n2 + 1)] for i in range(n1 + 1)]
for i in range(k1 + 1):
dp[i][0][0] = 1
for i in range(k2 + 1):
dp[0][i][1] = 1
for i in range(1, n1 + 1):
for j in range(1, n2 + 1):
dp[i][j][0... | python | code_algorithm | [
{
"input": "2 1 1 10\n",
"output": "1\n"
},
{
"input": "2 3 1 2\n",
"output": "5\n"
},
{
"input": "2 4 1 1\n",
"output": "0\n"
},
{
"input": "12 15 7 2\n",
"output": "171106\n"
},
{
"input": "34 55 2 9\n",
"output": "13600171\n"
},
{
"input": "46 51 4 ... | code_contests | python | 0.1 | c817291f1cc616359c883d4fb7e1aa81 |
Suppose there is a h × w grid consisting of empty or full cells. Let's make some definitions:
* r_{i} is the number of consecutive full cells connected to the left side in the i-th row (1 ≤ i ≤ h). In particular, r_i=0 if the leftmost cell of the i-th row is empty.
* c_{j} is the number of consecutive full cells ... | n, m = map(int, input().split())
row = list(map(int, input().split()))
col = list(map(int, input().split()))
grid = [['?'] * m for _ in range(n)]
for i in range(n):
lng = row[i]
for j in range(lng):
grid[i][j] = 'b'
if lng < m:
grid[i][lng] = 'w'
for i in range(m):
lng = col[i]
for j... | python | code_algorithm | [
{
"input": "19 16\n16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12\n6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4\n",
"output": "797922655\n"
},
{
"input": "3 4\n0 3 1\n0 2 3 0\n",
"output": "2\n"
},
{
"input": "1 1\n0\n1\n",
"output": "0\n"
},
{
"input": "2 2\n1 1\n1 0\n",
"ou... | code_contests | python | 0 | 4998825ffad2d7ba65c1985d4bc69074 |
For an array a of integers let's denote its maximal element as max(a), and minimal as min(a). We will call an array a of k integers interesting if max(a) - min(a) ≥ k. For example, array [1, 3, 4, 3] isn't interesting as max(a) - min(a) = 4 - 1 = 3 < 4 while array [7, 3, 0, 4, 3] is as max(a) - min(a) = 7 - 0 = 7 ≥ 5.
... | def findsubarray(n,ll):
for i in range(0,n-1):
if abs(ll[i]-ll[i+1])>1:
return 1,i+1,i+2
return 0,None,None
tests = int(input())
for i in range(0,tests):
numberlen = int(input())
numbers = input().split()
numberss = list(map(int,numbers))
result, left,right = findsub... | python | code_algorithm | [
{
"input": "3\n5\n1 2 3 4 5\n4\n2 0 1 9\n2\n2019 2020\n",
"output": "NO\nYES\n1 2\nNO\n"
},
{
"input": "1\n16\n1 2 3 4 4 4 2 4 4 4 4 4 5 5 5 5\n",
"output": "YES\n6 7\n"
},
{
"input": "1\n8\n1 2 2 2 4 4 4 5\n",
"output": "YES\n4 5\n"
},
{
"input": "1\n14\n1 2 3 4 5 4 2 3 4 5 ... | code_contests | python | 0 | 0315c332c50c99c1a4004baa6779e217 |
[SIHanatsuka - EMber](https://soundcloud.com/hanatsuka/sihanatsuka-ember)
[SIHanatsuka - ATONEMENT](https://soundcloud.com/hanatsuka/sihanatsuka-atonement)
Back in time, the seven-year-old Nora used to play lots of games with her creation ROBO_Head-02, both to have fun and enhance his abilities.
One day, Nora's adop... | MOD = 1000000007
def isSubset(a, b):
return (a & b) == a
def isIntersect(a, b):
return (a & b) != 0
# Solve for each weakly connected component (WCC)
def cntOrder(s, t):
p = len(s)
m = len(t)
inMask = [0 for i in range(m)]
for x in range(p):
for i in range(m):
if t[i] % s[x] == 0:
inMask[i] |= 1 ... | python | code_algorithm | [
{
"input": "4\n5 7 2 9\n",
"output": "1\n"
},
{
"input": "3\n2 6 8\n",
"output": "2\n"
},
{
"input": "5\n2 3 4 9 12\n",
"output": "4\n"
},
{
"input": "45\n41 50 11 34 39 40 9 22 48 6 31 16 13 8 20 42 27 45 15 43 32 38 28 10 46 17 21 33 4 18 35 14 37 23 29 2 49 19 12 44 36 24 ... | code_contests | python | 0 | fb12354938d5da64b83d470cc481a9c8 |
Madeline has an array a of n integers. A pair (u, v) of integers forms an inversion in a if:
* 1 ≤ u < v ≤ n.
* a_u > a_v.
Madeline recently found a magical paper, which allows her to write two indices u and v and swap the values a_u and a_v. Being bored, she decided to write a list of pairs (u_i, v_i) with t... | n=int(input())
arr=list(map(int,input().split()))
ans=[]
for i in range(n):
for j in range(n-1,i,-1):
if arr[i]>arr[j]:
ans.append((arr[i],i+1,j+1))
ans=sorted(ans,key=lambda x:x[0])
ans=sorted(ans,reverse=True,key=lambda x:x[2])
print(len(ans))
for _,u,v in ans:
print(u,v) | python | code_algorithm | [
{
"input": "4\n1 8 1 6\n",
"output": "2\n2 4\n2 3\n"
},
{
"input": "5\n1 1 1 2 2\n",
"output": "0\n"
},
{
"input": "3\n3 1 2\n",
"output": "2\n1 3\n1 2\n"
},
{
"input": "1\n1\n",
"output": "0\n"
},
{
"input": "20\n10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1\n",
... | code_contests | python | 0 | b847f1ce1ec4c95bc800eb6c5c42a1df |
You are given the array a consisting of n positive (greater than zero) integers.
In one move, you can choose two indices i and j (i ≠ j) such that the absolute difference between a_i and a_j is no more than one (|a_i - a_j| ≤ 1) and remove the smallest of these two elements. If two elements are equal, you can remove a... | def t(b):
if b:
print("YES")
return
print("NO")
for _ in range(int(input())):
a = int(input())
l = sorted(list(map(int, input().split())))
for i in range(a - 1):
if l[i + 1] - l[i] >= 2:
t(False)
break
else:
t(True)
| python | code_algorithm | [
{
"input": "5\n3\n1 2 2\n4\n5 5 5 5\n3\n1 2 4\n4\n1 3 4 4\n1\n100\n",
"output": "YES\nYES\nNO\nNO\nYES\n"
},
{
"input": "2\n3\n1 2 3\n4\n1 2 3 4\n",
"output": "YES\nYES\n"
},
{
"input": "2\n3\n1 2 2\n4\n5 5 5 5\n",
"output": "YES\nYES\n"
},
{
"input": "5\n2\n2 3\n4\n2 2 8 8\n... | code_contests | python | 0.2 | 8de91f80dcbd4513c0c08b61308acb70 |
Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city.
Let's represent the city as an area of n × n square blocks. Yura needs to move from the block with coordinates (s_x,s_y) to the ... | from bisect import *
from collections import *
from math import *
from heapq import *
from typing import List
from itertools import *
from operator import *
from functools import *
import sys
'''
@lru_cache(None)
def fact(x):
if x<2:
return 1
return fact(x-1)*x
@lru_cache(None)
def per(i,j):
return... | python | code_algorithm | [
{
"input": "84 5\n67 59 41 2\n39 56\n7 2\n15 3\n74 18\n22 7\n",
"output": "42\n"
},
{
"input": "5 3\n1 1 5 5\n1 2\n4 1\n3 3\n",
"output": "5\n"
},
{
"input": "1 0\n1 1 1 1\n",
"output": "0\n"
},
{
"input": "1000000000 0\n1 1000000000 1000000000 1\n",
"output": "1999999998... | code_contests | python | 0 | 3b44ffbb0068a17fd705e439e6571e11 |
Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies.... | n = int(input())
c = list(map(int, input().split()))
d = dict()
d[1] = []
d[2] = []
d[3] = []
for i in range(n):
d[c[i]].append(i)
done = set()
mas = []
k = 0
poss = set()
for i in range(n):
l = list(map(int, input().split()))
l = l[1:]
mas.append(l)
if len(l) == 0:
k = c[i]
poss.add... | python | code_algorithm | [
{
"input": "1\n1\n0\n",
"output": "1\n"
},
{
"input": "5\n2 2 1 1 3\n1 5\n2 5 1\n2 5 4\n1 5\n0\n",
"output": "7\n"
},
{
"input": "25\n3 3 1 1 1 2 2 2 3 1 2 3 2 1 2 2 2 3 2 1 2 3 2 1 1\n0\n0\n0\n0\n0\n0\n1 12\n0\n1 19\n0\n2 12 21\n2 3 10\n0\n1 21\n0\n1 9\n1 3\n0\n0\n2 3 2\n0\n1 12\n0\n1 3... | code_contests | python | 0 | 037b22109dd633814513ffab66f8aeac |
Valera runs a 24/7 fast food cafe. He magically learned that next day n people will visit his cafe. For each person we know the arrival time: the i-th person comes exactly at hi hours mi minutes. The cafe spends less than a minute to serve each client, but if a client comes in and sees that there is no free cash, than ... | t=int(input())
d={}
while (t>0):
t=t-1
s=input()
if s in d:
d[s]+=1
else:
d[s]=1
print(max(d.values())) | python | code_algorithm | [
{
"input": "3\n0 12\n10 11\n22 22\n",
"output": "1\n"
},
{
"input": "4\n8 0\n8 10\n8 10\n8 45\n",
"output": "2\n"
},
{
"input": "1\n8 0\n",
"output": "1\n"
},
{
"input": "10\n0 39\n1 35\n1 49\n1 51\n5 24\n7 40\n7 56\n16 42\n23 33\n23 49\n",
"output": "1\n"
},
{
"i... | code_contests | python | 0.6 | 8807ce0426ee69a457ccc87c30411269 |
Permutation p is an ordered set of integers p1, p2, ..., pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation p1, p2, ..., pn.
Petya decided to introduce the sum operation on the se... | n = int(input())
ans = [1, 3, 5, 7, 9, 11, 13, 15]
dct = \
{
1 : 1,
3 : 18,
5 : 1800,
7 : 670320,
9 : 734832000,
11 : 890786230,
13 : 695720788,
15 : 150347555
}
if n in ans:
print(dct[n])
else:
print(0) | python | code_algorithm | [
{
"input": "3\n",
"output": "18\n"
},
{
"input": "5\n",
"output": "1800\n"
},
{
"input": "9\n",
"output": "734832000\n"
},
{
"input": "11\n",
"output": "890786230\n"
},
{
"input": "14\n",
"output": "0\n"
},
{
"input": "7\n",
"output": "670320\n"
... | code_contests | python | 0 | 80bcf125e948f00bb1e18afcc28bead7 |
Gerald plays the following game. He has a checkered field of size n × n cells, where m various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for n - 1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original... | n, m = tuple(map(int, input().split(' ')))
vb = set()
hb = set()
for k in range(m):
i, j = tuple(map(int, input().split(' ')))
hb.add(i-1)
vb.add(j-1)
c = 0
for i in range(1, n//2):
c += 1 if i not in hb else 0
c += 1 if n-i-1 not in hb else 0
c += 1 if i not in vb else 0
c += 1 if n-i-1 not in vb else 0
c += 1 ... | python | code_algorithm | [
{
"input": "4 3\n3 1\n3 2\n3 3\n",
"output": "1\n"
},
{
"input": "3 1\n2 2\n",
"output": "0\n"
},
{
"input": "3 0\n",
"output": "1\n"
},
{
"input": "5 5\n2 2\n5 3\n2 3\n5 1\n4 4\n",
"output": "1\n"
},
{
"input": "5 5\n3 2\n5 4\n3 3\n2 3\n1 2\n",
"output": "1\n... | code_contests | python | 0 | dd71c986c2ce73954d65670ae1f2b058 |
Little Vasya has received a young builder’s kit. The kit consists of several wooden bars, the lengths of all of them are known. The bars can be put one on the top of the other if their lengths are the same.
Vasya wants to construct the minimal number of towers from the bars. Help Vasya to use the bars in the best way ... | x=int(input())
l=list(map(int,input().split()))
a=list(set(l))
l.sort()
maxx=l.count(0)
for i in range(len(l)):
if(l.count(l[i])>maxx):
maxx=l.count(l[i])
print(maxx,len(a))
| python | code_algorithm | [
{
"input": "3\n1 2 3\n",
"output": "1 3\n"
},
{
"input": "4\n6 5 6 7\n",
"output": "2 3\n"
},
{
"input": "83\n246 535 994 33 390 927 321 97 223 922 812 705 79 80 977 457 476 636 511 137 6 360 815 319 717 674 368 551 714 628 278 713 761 553 184 414 623 753 428 214 581 115 439 61 677 216 7... | code_contests | python | 0.9 | b0e13d7da52b057a42be220553ff418b |
The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.
Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.
If there is no police offi... | n=int(input())
a=list(map(int,input().split()))
oc=0
cc=0
ans=0
for i in a:
if i>0:
oc+=i
elif oc==0:
ans+=1
else:
oc-=1
print(ans) | python | code_algorithm | [
{
"input": "11\n-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1\n",
"output": "8\n"
},
{
"input": "3\n-1 -1 1\n",
"output": "2\n"
},
{
"input": "8\n1 -1 1 -1 -1 1 1 1\n",
"output": "1\n"
},
{
"input": "1\n1\n",
"output": "0\n"
},
{
"input": "3\n-1 5 4\n",
"output": "1\n"
},... | code_contests | python | 1 | e861c7a334650a2f8f4b7253295fa685 |
Little Masha loves arranging her toys into piles on the floor. And she also hates it when somebody touches her toys. One day Masha arranged all her n toys into several piles and then her elder brother Sasha came and gathered all the piles into one. Having seen it, Masha got very upset and started crying. Sasha still ca... | def combinacoes(x):
# Se for apenas 1 elemento retorna 1 arranjo
if x == 1:
return [[0]]
else:
# Adiciona os elementos a lista auxiliar
aux = combinacoes(x - 1)
pilha = []
par = 0
# Percorre a lista juntando os elementos nas possibilidades possíveis
for element in aux... | python | code_algorithm | [
{
"input": "3\n",
"output": "5\n{1,2,3}\n{1,2},{3}\n{1},{2},{3}\n{1},{2,3}\n{1,3},{2}\n"
},
{
"input": "8\n",
"output": "4140\n{1,2,3,4,5,6,7,8}\n{1,2,3,4,5,6,7},{8}\n{1,2,3,4,5,6},{7},{8}\n{1,2,3,4,5,6},{7,8}\n{1,2,3,4,5,6,8},{7}\n{1,2,3,4,5,8},{6},{7}\n{1,2,3,4,5},{6,8},{7}\n{1,2,3,4,5},{6},{7... | code_contests | python | 0 | 147432d6708d541042b8aa9fba39b05f |
Mike is trying rock climbing but he is awful at it.
There are n holds on the wall, i-th hold is at height ai off the ground. Besides, let the sequence ai increase, that is, ai < ai + 1 for all i from 1 to n - 1; we will call such sequence a track. Mike thinks that the track a1, ..., an has difficulty <image>. In othe... | n=int(input())
lis=list(map(int,input().split()))
dif=abs(lis[1]-lis[0])
pos=[]
for i in range(n-1):
#print(i,end=",")
if(abs(lis[i]-lis[i+1])>dif):
dif=abs(lis[i]-lis[i+1])
for i in range(1,n-1):
t=abs(lis[i-1]-lis[i+1])
if(t>dif):
pos.append(t)
else:
pos.append(dif)
#print(... | python | code_algorithm | [
{
"input": "3\n1 4 6\n",
"output": "5\n"
},
{
"input": "5\n1 2 3 4 5\n",
"output": "2\n"
},
{
"input": "5\n1 2 3 7 8\n",
"output": "4\n"
},
{
"input": "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43... | code_contests | python | 0.2 | 7b4b1a096d63dd91d9af9fb10c573402 |
Polycarp loves geometric progressions — he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression.
In this task we shall define geometric progressions as finite sequences of numbers a1, a2, ...,... | def main():
n = int(input())
l = tuple(map(int, input().split()))
if n == 1:
return 0
if n == 2:
if l[0] == 0 and l[1] != 0: return 1
return 0
def div(a, b):
if b == 0: return 0 if a == 0 else "inf"
return a / b
pref, suff = [["init", 1]], [["init", 1]]
... | python | code_algorithm | [
{
"input": "4\n3 6 12 24\n",
"output": "0\n"
},
{
"input": "4\n-8 -16 24 -32\n",
"output": "1\n"
},
{
"input": "4\n0 1 2 3\n",
"output": "2\n"
},
{
"input": "5\n1 -10 100 -1000 -10000\n",
"output": "1\n"
},
{
"input": "2\n0 -17\n",
"output": "1\n"
},
{
... | code_contests | python | 0 | 723e64dbbb509154c745f4dbe7aabe48 |
You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are marked with '*'. Let's call two empty cells adjacent if they share a side.
Let's call a connected component any non-extendible set of cells such that a... | from collections import deque
dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]]
componentsizes = {}
componentid = 0
n, m = map(int, input().split())
field = [list(input()) for _ in range(n)]
def valid(y, x):
return y >= 0 and x >= 0 and y < n and x < m
def component(y, x):
size = 1
q = deque([(y, x)])
fiel... | python | code_algorithm | [
{
"input": "4 5\n**..*\n..***\n.*.*.\n*.*.*\n",
"output": "46..3\n..732\n.6.4.\n5.4.3\n"
},
{
"input": "3 3\n*.*\n.*.\n*.*\n",
"output": "3.3\n.5.\n3.3\n"
},
{
"input": "10 1\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n",
"output": ".\n.\n.\n.\n.\n.\n.\n.\n.\n.\n"
},
{
"input": "10 10\n*.... | code_contests | python | 0.3 | a6e31d8cb163cada9df16176f3f16e2b |
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Amo... | from __future__ import print_function
dic = {"9":"1989","0":"1990","1":"1991","2":"1992","3":"1993","4":"1994","5":"1995","6":"1996","7":"1997","8":"1998"}
def get_year(s):
# print("came into get_year")
length=len(s)
if len(s)==1:
return dic[s]
pos=[s]
pre=1
if s[0]=="0" or len(s)<4 or (len(s)==4 and int(s)... | python | code_algorithm | [
{
"input": "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0\n",
"output": "2015\n12015\n1991\n1989\n1990\n"
},
{
"input": "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999\n",
"output": "1989\n1999\n2999\n9999\n"
},
{
"input": "1\nIAO'113098\n",
"output": "1113098\n"
},
{
"input": "1\nIAO'021113... | code_contests | python | 0 | 6d0ce826de36ddc55b4f07a45f340587 |
While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:
<image>
Together with his old phone, he lost all his cont... | n = int(input())
s = input()
fill = [[False for i in range(3)] for j in range(4)]
for i in s:
if i == "0":
j = 10
else:
j = ord(i)-ord('1') #0, 1, 2, 3... 8
fill[j//3][j%3] = True
#for i in fill:
# print(i)
top = fill[0][0] or fill[0][1] or fill[0][2]
bottom = fill[2][0] or fill[3][1] or ... | python | code_algorithm | [
{
"input": "9\n123456789\n",
"output": "YES\n"
},
{
"input": "3\n911\n",
"output": "YES\n"
},
{
"input": "3\n586\n",
"output": "NO\n"
},
{
"input": "2\n09\n",
"output": "NO\n"
},
{
"input": "2\n10\n",
"output": "YES\n"
},
{
"input": "8\n42937903\n",
... | code_contests | python | 0.1 | a1eec9b8f31f03ee50c3f906e95d7b09 |
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them.
The game they came up with has the following rules. In... | n=int(input())
a=[0]+list(map(int, input().split()))
p=[0]
for i in range(1, n+1):
p+=[ p[-1]+a[i] ]
d=[ p[n] ]*(n+1)
for i in range(n-2, 0, -1):
d[i]=max(d[i+1], p[i+1]-d[i+1])
print(d[1])
| python | code_algorithm | [
{
"input": "3\n2 4 8\n",
"output": "14\n"
},
{
"input": "4\n1 -7 -2 3\n",
"output": "-3\n"
},
{
"input": "114\n46 7 39 21 44 31 49 57 26 22 86 45 66 72 96 15 77 38 92 88 50 68 30 55 20 5 15 11 26 66 94 74 43 73 35 7 11 36 26 74 86 52 14 5 91 71 3 75 22 7 10 97 42 41 52 80 97 31 45 59 53 ... | code_contests | python | 0 | 9ada17057f7f22ff5e555c5d85918a7d |
Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess... | def nap(y):
if y==0:
p=[0,1,2]
return p
if y==1:
p=[1,0,2]
return p
if y==2:
p=[1,2,0]
return p
if y==3:
p=[2,1,0]
return p
if y==4:
p=[2,0,1]
return p
if y==5:
p=[0,2,1]
return p
n=int(input())
x=int... | python | code_algorithm | [
{
"input": "4\n2\n",
"output": "1\n"
},
{
"input": "1\n1\n",
"output": "0\n"
},
{
"input": "100000\n0\n",
"output": "2\n"
},
{
"input": "1999999997\n1\n",
"output": "2\n"
},
{
"input": "6\n0\n",
"output": "0\n"
},
{
"input": "1999999995\n0\n",
"out... | code_contests | python | 0.1 | 7cc5ddcbbb647d9ad744a4ee3ba68f61 |
Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopo... |
arr1 = input().split()
a = int(arr1[0])
b = int(arr1[1])
c = int(min(a, b))
res = 1
for i in range(2, c+1):
res *= i
print(res) | python | code_algorithm | [
{
"input": "4 3\n",
"output": "6\n"
},
{
"input": "22 12\n",
"output": "479001600\n"
},
{
"input": "8 9\n",
"output": "40320\n"
},
{
"input": "20 12\n",
"output": "479001600\n"
},
{
"input": "12 12\n",
"output": "479001600\n"
},
{
"input": "3 999279109... | code_contests | python | 0.8 | b7057b3872044caa09db326deef24e5b |
Rock... Paper!
After Karen have found the deterministic winning (losing?) strategy for rock-paper-scissors, her brother, Koyomi, comes up with a new game as a substitute. The game works as follows.
A positive integer n is decided first. Both Koyomi and Karen independently choose n distinct positive integers, denoted ... | # Main maut ko takiya, aur kafan ko chaadar banakar audhta hoon!
print("Karen") | python | code_algorithm | [
{
"input": "3\n1 2 3\n4 5 6\n",
"output": "Karen\n"
},
{
"input": "5\n2 4 6 8 10\n9 7 5 3 1\n",
"output": "Karen\n"
},
{
"input": "1\n1\n2000000\n",
"output": "Karen\n"
},
{
"input": "1\n1048576\n1020000\n",
"output": "Karen\n"
},
{
"input": "3\n9 33 69\n71 74 100... | code_contests | python | 0.7 | 920569248b81c95378ebf996815e0425 |
Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every x minutes until hh: mm is reached, and only then he will wake up. He wants to kno... | def lucky(a,b):
return '7' in str(a)+str(b)
x = int(input())
t = 0
h,m = map(int,input().split())
while not lucky(h,m):
t+=1
m -= x
while m<0:
m+=60
h-=1
h%=24
print(t)
| python | code_algorithm | [
{
"input": "3\n11 23\n",
"output": "2\n"
},
{
"input": "5\n01 07\n",
"output": "0\n"
},
{
"input": "12\n00 06\n",
"output": "31\n"
},
{
"input": "60\n00 10\n",
"output": "7\n"
},
{
"input": "24\n17 51\n",
"output": "0\n"
},
{
"input": "2\n18 19\n",
... | code_contests | python | 0.2 | 203922c10bd38e603fbdf0d74ed0e582 |
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.
Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.
Dima can buy boxes at a factory. The factory pr... | # -*- coding: utf-8 -*-
"""
Created on Fri Mar 2 22:09:34 2018
@author: Xiaohua
"""
def Hamster(amounts,boxsize):
a=10000000000000000000
result=[0,0]
for i in range(0,len(boxsize)):
if(amounts[0]%boxsize[i]<a):
a=amounts[0]%boxsize[i]
result[0]=i+1
re... | python | code_algorithm | [
{
"input": "19 3\n5 4 10\n",
"output": "2 4\n"
},
{
"input": "28 3\n5 6 30\n",
"output": "1 5\n"
},
{
"input": "2 1\n2\n",
"output": "1 1\n"
},
{
"input": "1 1\n1\n",
"output": "1 1\n"
},
{
"input": "947264836 50\n977141206 956777871 186433588 538218068 759102378 ... | code_contests | python | 0 | 2644bac6e4d4136520cdd0bead93ad24 |
This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious either — although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all.
... | from itertools import chain, combinations
from collections import defaultdict
import math
def powerset(iterable):
xs = iterable
return chain.from_iterable(combinations(xs,n) for n in range(1, len(xs)+1))
def has(s):
h = 1
p = [2,3,5,7,11,13,17,19,23,29]
for i in s:
h *= p[i-1]
return h
h = set()
total ... | python | code_algorithm | [
{
"input": "2028\n",
"output": "13\n"
},
{
"input": "97\n",
"output": "2\n"
},
{
"input": "10\n",
"output": "1\n"
},
{
"input": "868572419889505545\n",
"output": "1124978369760\n"
},
{
"input": "999999\n",
"output": "6\n"
},
{
"input": "74774\n",
"... | code_contests | python | 0 | 58b25fc79e8ad66c428b6f0a7360632f |
There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want.
To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", ... | a = input()
b,c = input().split()
lst = ["6", "7", "8", "9", "T", "J", "Q", "K" , "A"]
if b[1] ==c[1]:
print('YES' if lst.index(b[0]) > lst.index(c[0]) else 'NO')
elif b[1] == a:
print('YES')
else:
print('NO') | python | code_algorithm | [
{
"input": "C\n7H AS\n",
"output": "NO\n"
},
{
"input": "H\nQH 9S\n",
"output": "YES\n"
},
{
"input": "S\n8D 6D\n",
"output": "YES\n"
},
{
"input": "H\nQD KD\n",
"output": "NO\n"
},
{
"input": "D\n9S 8S\n",
"output": "YES\n"
},
{
"input": "H\nTH 8H\n",... | code_contests | python | 0.4 | e9906048eebe64e6f8c0fab26c7169cb |
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with... | n=int(input())
from collections import Counter
c=Counter([])
c[1]=[-1,2]
c[2]=[-2,4]
c[3]=[-3,6]
c[4]=[0,1]
c[5]=[-1,3]
c[6]=[-2,5]
a=n%7
s=n//7
f=0
if a==0:
print ('7'*s)
else:
s+=c[a][0]
f+=c[a][1]
if s<0:
if n%4==0:
print ('4'*(n//4))
else:
print (-1)
else:... | python | code_algorithm | [
{
"input": "11\n",
"output": "47\n"
},
{
"input": "10\n",
"output": "-1\n"
},
{
"input": "999980\n",
"output": "4444777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777... | code_contests | python | 0 | c64952589f4dc616d7321ce5bbbf1fd4 |
Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b".
Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos_1 in the string s, choose an index pos_2 in the string... | a=int(input())
s=input()
r=input()
cmap=[]
tmap=[]
for i in range(len(s)):
if(s[i]=='a' and r[i]=='b'):
cmap.append(i)
elif(s[i]=='b' and r[i]=='a'):
tmap.append(i)
r=len(cmap)
t=len(tmap)
if(((r%2==1 and t%2==0) or (r%2==0 and t%2==1))):
print(-1)
else:
total=r//2+t//2
if(r... | python | code_algorithm | [
{
"input": "1\na\nb\n",
"output": "-1\n"
},
{
"input": "8\nbabbaabb\nabababaa\n",
"output": "3\n2 6\n1 3\n7 8\n"
},
{
"input": "4\nabab\naabb\n",
"output": "2\n3 3\n3 2\n"
},
{
"input": "10\naaaaaaaaaa\nbbbbbbbbbb\n",
"output": "5\n1 2\n3 4\n5 6\n7 8\n9 10\n"
},
{
... | code_contests | python | 0 | 82fbf75106c046094d29d6642af4b8a7 |
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated.
In 2312 there was a startling discovery:... | n, m = map(int, input().split())
n += 1
s = [[] for i in range(n)]
for j in range(2, n, 2): s[j] = [2]
for i in range(3, n, 2):
if s[i]: continue
for j in range(i, n, i): s[j].append(i)
p, d, r = {}, set(), [''] * m
for j in range(m):
t = input()
i = int(t[2: ])
if t[0] == '+':
... | python | code_algorithm | [
{
"input": "10 10\n+ 6\n+ 10\n+ 5\n- 10\n- 5\n- 6\n+ 10\n+ 3\n+ 6\n+ 3\n",
"output": "Success\nConflict with 6\nSuccess\nAlready off\nSuccess\nSuccess\nSuccess\nSuccess\nConflict with 10\nAlready on\n"
},
{
"input": "100 1\n+ 51\n",
"output": "Success\n"
},
{
"input": "100000 1\n+ 12345\... | code_contests | python | 0 | 852708633b1d80a798aa8a028fc0294b |
One day shooshuns found a sequence of n integers, written on a blackboard. The shooshuns can perform one operation with it, the operation consists of two steps:
1. Find the number that goes k-th in the current sequence and add the same number to the end of the sequence;
2. Delete the first number of the current s... | n,k=map(int,input().split())
l=list(map(int,input().split()))
f=l[k-1]
if l[k-1:].count(f) == n-k+1:
r = 0
for i in range(k-2,-1,-1):
if l[k-1] != l[i]:
break
r+=1
print(len(l[:k-1-r]))
else:
print(-1)
| python | code_algorithm | [
{
"input": "3 2\n3 1 1\n",
"output": "1\n"
},
{
"input": "3 1\n3 1 1\n",
"output": "-1\n"
},
{
"input": "10 3\n3 2 1 1 1 1 1 1 1 1\n",
"output": "2\n"
},
{
"input": "10 10\n1 2 3 4 5 6 7 8 9 10\n",
"output": "9\n"
},
{
"input": "10 2\n2 1 1 1 1 1 1 1 1 1\n",
"... | code_contests | python | 0 | 4874e2d6fe67134a5cbc03b94e4ce0aa |
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively.
However, his... | import sys
from collections import deque
DST_VERTEX = 0
EDGE_CAP = 1
EDGE_ID = 2
EDGE_DIR = 3
def bfs(flow, graph, n, m):
dirs = [-1 for _ in range(m)]
q = deque()
q.append(0)
q_size = 1
while q_size > 0:
cur_node = q.popleft()
q_size -= 1
for i in range(len(graph[cur_node]... | python | code_algorithm | [
{
"input": "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5\n",
"output": "0\n0\n1\n1\n0\n"
},
{
"input": "3 3\n3 2 10\n1 2 10\n3 1 5\n",
"output": "1\n0\n1\n"
},
{
"input": "3 2\n1 2 1\n2 3 1\n",
"output": "0\n0\n"
},
{
"input": "2 1\n1 2 1\n",
"output": "0\n"
},
{
"in... | code_contests | python | 0 | 68bf1ed71e7bcf29b104963366229472 |
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.
<image>
Shaass puts the books... | n = int(input())
t = []
for i in range(n):
e = list(map(int,input().split()))
t.append(e)
size = 0
size2 = 0
for i in t:
size += i[0]
size2 += i[1]
matrix = [[-1000]*(size+1) for i in range(n)]
for i in range(n):
matrix[i][0] = 0
for i in range(n):
for j in range(1,size+1):
if t[i][0... | python | code_algorithm | [
{
"input": "3\n1 10\n2 1\n2 4\n",
"output": "3\n"
},
{
"input": "5\n1 12\n1 3\n2 15\n2 5\n2 1\n",
"output": "5\n"
},
{
"input": "1\n1 2\n",
"output": "1\n"
},
{
"input": "1\n1 1\n",
"output": "1\n"
},
{
"input": "100\n2 2\n1 2\n1 5\n1 5\n1 2\n1 4\n2 3\n1 2\n1 5\n2... | code_contests | python | 0 | abf2328fe91a4579d01b1bfaebbc3107 |
You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r).
Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]).
Your task is to find a ... | import sys
n = int(input())
a = list(map(int, input().split()))
if n <= 2:
print(n)
sys.exit()
best = 0
mem = a[:2]
cur = 0
for i in a[2:]:
if mem[0] + mem[1] == i:
cur += 1
else:
if cur > best: best = cur
cur = 0
mem = mem[1:] + [i]
if cur > best: best = cur
print(best+2)
| python | code_algorithm | [
{
"input": "5\n1 1 1 1 1\n",
"output": "2\n"
},
{
"input": "10\n1 2 3 5 8 13 21 34 55 89\n",
"output": "10\n"
},
{
"input": "6\n10 20 30 10 40 50\n",
"output": "4\n"
},
{
"input": "2\n1 3\n",
"output": "2\n"
},
{
"input": "1\n1000\n",
"output": "1\n"
},
{
... | code_contests | python | 0.8 | 599ed0181f651b080edfd010c0e83f17 |
Fox Ciel is playing a game with numbers now.
Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible.
... | from sys import stdin, stdout, stderr, setrecursionlimit
setrecursionlimit(100000)
def debug (*e):
if not __debug__:
print(*e, file=stderr)
def dd(*vals):
import inspect, re
frame = inspect.getframeinfo(inspect.stack()[1][0])
vs = re.search("dd\((.+)\)", frame.code_context[0]).group(1).split("... | python | code_algorithm | [
{
"input": "2\n12 18\n",
"output": "12\n"
},
{
"input": "3\n2 4 6\n",
"output": "6\n"
},
{
"input": "5\n45 12 27 30 18\n",
"output": "15\n"
},
{
"input": "2\n1 2\n",
"output": "2\n"
},
{
"input": "5\n2 3 5 8 18\n",
"output": "5\n"
},
{
"input": "2\n1 1... | code_contests | python | 0.5 | 4cc947eb5eaf2e894ab828102c35bb45 |
Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly k months. He looked at the calendar and learned that at the moment is the month number s. Vasya immediately got interested in what month Codecraft III will appear. Help him understand t... | month = str(input())
k = int(input())
m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
j = m.index(month)
while k != 0:
if j == 11:
j = 0
k -= 1
else:
j += 1
k -= 1
print(m[j]) | python | code_algorithm | [
{
"input": "May\n24\n",
"output": "May\n"
},
{
"input": "November\n3\n",
"output": "February\n"
},
{
"input": "June\n97\n",
"output": "July\n"
},
{
"input": "August\n0\n",
"output": "August\n"
},
{
"input": "July\n33\n",
"output": "April\n"
},
{
"input... | code_contests | python | 0.9 | 18cb1ae22c57f02414e252942a8d1b75 |
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made lik... | n = int(input())
if 1 <= n <= 100:
for i in range(n):
word = input()
if len(word) > 10:
numWord = str(len(word[1:-1]))
print(word[0] + numWord + word[-1])
else:
print(word)
| python | code_algorithm | [
{
"input": "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis\n",
"output": "word\nl10n\ni18n\np43s\n"
},
{
"input": "26\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n",
"output": "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\... | code_contests | python | 1 | c3d83a465036ef6ec129f94c5597d5c2 |
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.
Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri,... | n,m=map(int,input().split())
min_val=9999999
for _ in range(m):
y,x=map(int,input().split())
if min_val>x-y:
x1=x
y1=y
min_val=x-y
print(min_val+1)
L=[0]*n
tp=0
listNum=[]
for i in range(n):
listNum.append(tp)
tp+=1
if tp>min_val:
tp=0
for i in listNum[:-1]:
print... | python | code_algorithm | [
{
"input": "5 3\n1 3\n2 5\n4 5\n",
"output": "2\n0 1 0 1 0\n"
},
{
"input": "4 2\n1 4\n2 4\n",
"output": "3\n0 1 2 0\n"
},
{
"input": "5 3\n1 3\n2 5\n4 5\n",
"output": "2\n0 1 0 1 0\n"
},
{
"input": "3 8\n1 2\n1 2\n1 1\n2 3\n2 3\n1 1\n1 3\n1 3\n",
"output": "1\n0 0 0\n"
... | code_contests | python | 0.6 | 29865e52b480e2539963fa25db753597 |
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the... | def main():
largemodulus = 1000000007
maxofn = 300001
n = 0
answer = 0
powersoftwo = []
multiplier = 1
for _ in range(maxofn):
powersoftwo.append(multiplier)
if multiplier >= largemodulus:
multiplier = multiplier % largemodulus
multiplier *= 2
n = int(input())
hacked =... | python | code_algorithm | [
{
"input": "3\n4 3 1\n",
"output": "9\n"
},
{
"input": "2\n4 7\n",
"output": "3\n"
},
{
"input": "20\n2 6 8 9 20 23 27 36 43 49 63 65 70 71 85 87 89 91 94 97\n",
"output": "92743989\n"
},
{
"input": "3\n465343471 465343474 465343473\n",
"output": "9\n"
},
{
"input... | code_contests | python | 0.4 | 075d24a5fe5c94c848779371c4fb1600 |
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale.
Maxim often visits h... | n,k=map(int,input().split())
mi=1
if k==0:
print(0,0)
exit()
if k==n:
print(0,0)
exit()
if 3*k <=n:
ma=2*k
else:
ma=n-k
print(mi,ma)
| python | code_algorithm | [
{
"input": "6 3\n",
"output": "1 3\n"
},
{
"input": "999999998 333333333\n",
"output": "1 666666665\n"
},
{
"input": "999999998 333333332\n",
"output": "1 666666664\n"
},
{
"input": "89 4\n",
"output": "1 8\n"
},
{
"input": "2 0\n",
"output": "0 0\n"
},
{
... | code_contests | python | 0.2 | 2e546aca5d505eff4312925d57972ca6 |
One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution...
Input
The only line of input data contains the arith... | import re;s=input();print(eval(s)+eval(re.sub("-|\+|\d",lambda m:"+-035"[2-"+--".count(m[0])::3],s))) | python | code_algorithm | [
{
"input": "2+2\n",
"output": "-46\n"
},
{
"input": "112-37\n",
"output": "375\n"
},
{
"input": "8-7+6-5+4-3+2-1-0\n",
"output": "4\n"
},
{
"input": "131+49+249+71-251-61+159-111+51\n",
"output": "-4913\n"
},
{
"input": "150+222-3-90-248-187+198\n",
"output": ... | code_contests | python | 0 | cc068cfb10fce574149b24735f28054c |
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that.
The box's lock is constructed... | from functools import reduce
def factorial(n):
return reduce(lambda x, y: x*y, range(1,n+1))
colors = {
'R' : 0,
'O' : 0,
'Y' : 0,
'G' : 0,
'B' : 0,
'V' : 0
}
for c in list(input()):
colors[c] += 1
amount = list(reversed(sorted([(colors[key], key) for key in colors])))
amount = [x[0... | python | code_algorithm | [
{
"input": "YYYYYY\n",
"output": "1\n"
},
{
"input": "BOOOOB\n",
"output": "2\n"
},
{
"input": "ROYGBV\n",
"output": "30\n"
},
{
"input": "VYYYVV\n",
"output": "2\n"
},
{
"input": "OBRRYY\n",
"output": "8\n"
},
{
"input": "RRYOGB\n",
"output": "15\... | code_contests | python | 0 | 4f193ce0dc789b8c8fcaa49a93074d4a |
You are a given a list of integers a_1, a_2, …, a_n and s of its segments [l_j; r_j] (where 1 ≤ l_j ≤ r_j ≤ n).
You need to select exactly m segments in such a way that the k-th order statistic of the multiset of a_i, where i is contained in at least one segment, is the smallest possible. If it's impossible to select ... | n, s, m, k = map(int, input().split())
a = list(map(int, input().split()))
nt = [-1 for i in range(n+1)]
for i in range(s):
l, r = map(int, input().split())
l-=1
for j in range(l, r):
nt[j] = max(nt[j], r)
dp = [[0 for i in range(m+1)] for i in range(n+1)]
sum = [0 for i in range(n+1)]
def solve(mid):
for i in ... | python | code_algorithm | [
{
"input": "4 3 2 2\n3 1 3 2\n1 2\n2 3\n4 4\n",
"output": "2\n"
},
{
"input": "5 3 3 5\n5 5 2 1 1\n1 2\n2 3\n3 4\n",
"output": "-1\n"
},
{
"input": "5 2 1 1\n1 2 3 4 5\n2 4\n1 5\n",
"output": "1\n"
},
{
"input": "10 1 1 8\n1 1 1 991992993 1 991992993 3 6664666 1000000000 9999... | code_contests | python | 0.4 | a29e4f6f9aec71463182d614129a7497 |
A company of n friends wants to order exactly two pizzas. It is known that in total there are 9 pizza ingredients in nature, which are denoted by integers from 1 to 9.
Each of the n friends has one or more favorite ingredients: the i-th of friends has the number of favorite ingredients equal to f_i (1 ≤ f_i ≤ 9) and y... | import sys
import math
input=sys.stdin.readline
#sys.setrecursionlimit(1000000)
I=lambda : list(map(int,input().split()))
ma =int(10000000000000000)
n,m=map(int,input().split())
a=[ma]*(515);a1=[ma]*(515);fr=[0]*(515);pos=[0]*(515)
for i in range(n):
b=I()
x=int(0)
for j in range(1,b[0]+1):
b[j]-=... | python | code_algorithm | [
{
"input": "1 5\n9 9 8 7 6 5 4 3 2 1\n3 4 1 2 3 4\n1 4 5 6 7 8\n4 4 1 3 5 7\n1 4 2 4 6 8\n5 4 1 9 2 8\n",
"output": "4 2\n"
},
{
"input": "3 4\n2 6 7\n4 2 3 9 5\n3 2 3 9\n100 1 7\n400 3 3 2 5\n100 2 9 2\n500 3 2 9 5\n",
"output": "2 3\n"
},
{
"input": "4 3\n1 1\n1 2\n1 3\n1 4\n10 4 1 2 3... | code_contests | python | 0 | 502de537d5763f920efcacb2cd707557 |
The only difference between easy and hard versions is the length of the string. You can hack this problem only if you solve both problems.
Kirk has a binary string s (a string which consists of zeroes and ones) of length n and he is asking you to find a binary string t of the same length which satisfies the following ... | s = str(input().strip())
t = list(s[::-1])
cnt = 0
for i,v in enumerate(t):
if v == '0':
cnt += 1
else:
if cnt:
cnt -= 1
else:
t[i] = '0'
print("".join(t[::-1])) | python | code_algorithm | [
{
"input": "0111001100111011101000\n",
"output": "0011001100001011101000\n"
},
{
"input": "0001111\n",
"output": "0000000\n"
},
{
"input": "110\n",
"output": "010\n"
},
{
"input": "010\n",
"output": "010\n"
},
{
"input": "0010000110000100\n",
"output": "001000... | code_contests | python | 0 | 988255304099c86cb55086601c15fc68 |
You are given a chess board with n rows and n columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board.
A knight is a chess piece that can attack a piece in cell (x_2, y_2) from the cell (x_1, y_1) if one of the following conditions is met:
* |x_... | n = int(input())
i = 0
s = ''.join(['W' if (i + j) % 2 == 0 else 'B' for i in range(n) for j in range(n)])
while(i<n*n):
print(s[i:i+n])
i+=n | python | code_algorithm | [
{
"input": "3\n",
"output": "WBW\nBWB\nWBW\n"
},
{
"input": "6\n",
"output": "WBWBWB\nBWBWBW\nWBWBWB\nBWBWBW\nWBWBWB\nBWBWBW\n"
},
{
"input": "99\n",
"output": "WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBW\nBWBWBWBWBWBWBWBWBWBWBW... | code_contests | python | 0 | 5e4bf2d736af9b0f4201e8338d3faf73 |
Please notice the unusual memory limit of this problem.
Orac likes games. Recently he came up with the new game, "Game of Life".
You should play this game on a black and white grid with n rows and m columns. Each cell is either black or white.
For each iteration of the game (the initial iteration is 0), the color of... | import sys
input = sys.stdin.readline
n, m, t = map(int, input().split())
cell = [list(input().rstrip()) for _ in range(n)]
cnt = [[False] * m for _ in range(n)]
for i in range(n):
for j in range(m):
if i - 1 >= 0 and cell[i - 1][j] == cell[i][j]:
cnt[i][j] = True
if i + 1 < n and cell... | python | code_algorithm | [
{
"input": "3 3 3\n000\n111\n000\n1 1 1\n2 2 2\n3 3 3\n",
"output": "1\n1\n1\n"
},
{
"input": "5 2 2\n01\n10\n01\n10\n01\n1 1 4\n5 1 4\n",
"output": "0\n0\n"
},
{
"input": "1 1 3\n0\n1 1 1\n1 1 2\n1 1 3\n",
"output": "0\n0\n0\n"
},
{
"input": "5 5 3\n01011\n10110\n01101\n1101... | code_contests | python | 0 | 19f27dd7f5d6fdfa28be73c521fe8655 |
Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i ci times. Eugeny's play list is organized as follows: first song number 1 plays c1 times, then song number 2 plays... | # Getting input
song = list()
n,m=map(int,input().split())
song.append(0)
for i in range(n):
note, times = map(int,input().split())
total_time = song[i] +note*times
song.append(total_time)
eugeny_moments = list(map(int, input().split()))
# computing resul... | python | code_algorithm | [
{
"input": "1 2\n2 8\n1 16\n",
"output": "1\n1\n"
},
{
"input": "4 9\n1 2\n2 1\n1 1\n2 2\n1 2 3 4 5 6 7 8 9\n",
"output": "1\n1\n2\n2\n3\n4\n4\n4\n4\n"
},
{
"input": "3 3\n2 8\n5 1\n10 5\n13 16 62\n",
"output": "1\n1\n3\n"
},
{
"input": "10 10\n3 1\n2 7\n5 1\n7 2\n9 10\n9 5\n... | code_contests | python | 0.8 | bd9572529e66ad366b5b01897b7bf743 |
Valera wanted to prepare a Codesecrof round. He's already got one problem and he wants to set a time limit (TL) on it.
Valera has written n correct solutions. For each correct solution, he knows its running time (in seconds). Valera has also wrote m wrong solutions and for each wrong solution he knows its running time... | m, n = map(int, input().split())
a = sorted([int(x) for x in input().split()])
b = sorted([int(x) for x in input().split()])
for i in range(100):
if (i >= a[-1] and i < b[0] and i >= 2 * a[0]):
print(i)
break
else:
print(-1) | python | code_algorithm | [
{
"input": "3 1\n3 4 5\n6\n",
"output": "-1\n"
},
{
"input": "3 6\n4 5 2\n8 9 6 10 7 11\n",
"output": "5\n"
},
{
"input": "3 2\n10 20 30\n30 40\n",
"output": "-1\n"
},
{
"input": "1 1\n49\n100\n",
"output": "98\n"
},
{
"input": "1 1\n4\n9\n",
"output": "8\n"
... | code_contests | python | 0.5 | efe31f316a66a8e4444c88bdc3fd4ebf |
Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number a, consisting of digits from 1 to 9.
Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine ... | s=input()
ans=0
i=1
past=int(s[0])
c=1
ans=1
while(i<len(s)):
if(int(s[i])+past==9):
c+=1
past=int(s[i])
else:
if(c%2==1 and c!=1):
ans*=(c//2+1)
c=1
past=int(s[i])
i+=1
if(c!=1 and c%2==1):
ans*=(c//2+1)
print(ans)
| python | code_algorithm | [
{
"input": "1\n",
"output": "1\n"
},
{
"input": "123456789987654321\n",
"output": "1\n"
},
{
"input": "369727\n",
"output": "2\n"
},
{
"input": "123456789\n",
"output": "1\n"
},
{
"input": "3639272918194549\n",
"output": "16\n"
},
{
"input": "121217272... | code_contests | python | 0 | 3f5b4bd8e4fab58ac5f8e7a649f480d4 |
After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all... | from sys import stdin
from collections import defaultdict
def main():
stdin.readline()
num = {}
stat = lambda word: (word.count('r'),
len(word), num.setdefault(word, len(num)))
essay = list(map(stat, stdin.readline().lower().split()))
queue = []
for word in essay:
queue.append(word)
n_synonym ... | python | code_algorithm | [
{
"input": "2\nRuruRu fedya\n1\nruruRU fedor\n",
"output": "1 10\n"
},
{
"input": "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n",
"output": "2 6\n"
},
{
"input": "5\naa bb cc ee ff\n5\naa a\nbb aa\ncc bb\nee cc\nff bb\n",
"output": "0 5\n"
},
{
"input": "2\nABBABAABBAABABBAB... | code_contests | python | 0.8 | 74e6bba5589bcb0a0043d776e53ed18f |
Life in Bertown has become hard. The city has too many roads and the government spends too much to maintain them. There are n junctions and m two way roads, at which one can get from each junction to any other one. The mayor wants to close some roads so that the number of roads left totaled to n - 1 roads and it were s... | import sys
from array import array # noqa: F401
def input():
return sys.stdin.buffer.readline().decode('utf-8')
n, m, k = map(int, input().split())
adj = [[] for _ in range(n)]
dp = [[0] * (1 << n) for _ in range(1 << n)]
for u, v in (map(int, input().split()) for _ in range(m)):
u, v = u - 1, v - 1
a... | python | code_algorithm | [
{
"input": "4 6 2\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n",
"output": "12\n"
},
{
"input": "3 3 2\n1 2\n2 3\n1 3\n",
"output": "3\n"
},
{
"input": "4 6 3\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n",
"output": "4\n"
},
{
"input": "10 25 9\n10 8\n9 10\n6 9\n8 5\n1 7\n4 7\n5 3\n8 6\n9 2\n10 1\n7 2... | code_contests | python | 0 | 03848c07ec68e5a3403507208871f009 |
A long time ago (probably even in the first book), Nicholas Flamel, a great alchemist and the creator of the Philosopher's Stone, taught Harry Potter three useful spells. The first one allows you to convert a grams of sand into b grams of lead, the second one allows you to convert c grams of lead into d grams of gold a... | from functools import reduce
from operator import mul
c = list(map(int, input().split()))
r, h = reduce(mul, c[1::2], 1), reduce(mul, c[0::2], 1)
rw = r > h or (c[2] == 0 and c[3] != 0) or (c[0] == 0 and c[1] * c[3] != 0)
print("Ron" if rw else "Hermione") | python | code_algorithm | [
{
"input": "1 1 0 1 1 1\n",
"output": "Ron\n"
},
{
"input": "100 10 200 20 300 30\n",
"output": "Hermione\n"
},
{
"input": "1 0 1 2 1 2\n",
"output": "Hermione\n"
},
{
"input": "100 200 250 150 200 250\n",
"output": "Ron\n"
},
{
"input": "100 50 50 200 200 100\n",... | code_contests | python | 0.1 | 51bad8c801a3629ef45e24b0b12b1c51 |
Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.
There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end... | def compute_prefix_function(p):
"""
Вычисляет префикс функцию паттерна p
:param p: паттерн для kmp
:return: преикс функцию для p
"""
s = [0] * len(p)
border = 0 # текущая граница
for i in range(1, len(p)):
while border > 0 and p[i] != p[border]:
border = s[border - 1... | python | code_algorithm | [
{
"input": "7\naogogob\n",
"output": "a***b\n"
},
{
"input": "9\nogoogoogo\n",
"output": "*********\n"
},
{
"input": "13\nogogmgogogogo\n",
"output": "***gmg***\n"
},
{
"input": "2\nog\n",
"output": "og\n"
},
{
"input": "1\no\n",
"output": "o\n"
},
{
"... | code_contests | python | 0.3 | a121353efb4a04ade41c637440c94478 |
Given a positive integer n, find k integers (not necessary distinct) such that all these integers are strictly greater than 1, and their product is equal to n.
Input
The first line contains two integers n and k (2 ≤ n ≤ 100000, 1 ≤ k ≤ 20).
Output
If it's impossible to find the representation of n as a product of k... | #!/usr/bin/env python
from math import sqrt
dic = {}
def kfact(n, k):
if (n, k) in dic:
return dic[(n, k)]
if k == 1:
return [n]
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
if kfact(n // i, k - 1):
result = [i] + kfact(n // i, k - 1)
... | python | code_algorithm | [
{
"input": "1024 5\n",
"output": "2 2 2 2 64\n"
},
{
"input": "100000 2\n",
"output": "2 50000\n"
},
{
"input": "100000 20\n",
"output": "-1\n"
},
{
"input": "15 6\n",
"output": "-1\n"
},
{
"input": "13 3\n",
"output": "-1\n"
},
{
"input": "35557 5\n",... | code_contests | python | 0.3 | 505bab96fa6a4bc6b0189d788cff07de |
Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different speci... | def nine(p) :
s=''
for i in range(p) :
s+='9'
return int(s)
def prosh(p) :
ans=0
for i in range(1,p+1) :
ans+=nine(i)*9
return ans
n,k=map(int,input().split())
l=[0]*29
for i in range(19) :
e=nine(19-i)
l[i]=int(k/e)
k-=l[i]*e
if ... | python | code_algorithm | [
{
"input": "10 9\n",
"output": "1\n"
},
{
"input": "12 1\n",
"output": "3\n"
},
{
"input": "25 20\n",
"output": "0\n"
},
{
"input": "300 1000\n",
"output": "0\n"
},
{
"input": "1000000000000000000 100000000000000\n",
"output": "999899999999999991\n"
},
{
... | code_contests | python | 0.9 | abbb185a5c533603159a04b5189e0f13 |
Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice".
... | k, a, b = map(int, input().split())
A = []
for _ in range(3):
temp = [int(x) for x in input().split()]
A.append(temp)
B = []
for _ in range(3):
temp = [int(x) for x in input().split()]
B.append(temp)
flag, ac, bc, cur = 0, 0, 0, 1
m = {}
ans1, ans2 = [0]*1000, [0]*1000
while cur <= k:
if (a, b) in m and flag != 1:... | python | code_algorithm | [
{
"input": "8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3\n",
"output": "5 2\n"
},
{
"input": "10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2\n",
"output": "1 9\n"
},
{
"input": "5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2\n",
"output": "0 0\n"
},
{
"input": "10000000... | code_contests | python | 0 | b43d71a5398aadc7034181ba1c5c9b9c |
Firecrackers scare Nian the monster, but they're wayyyyy too noisy! Maybe fireworks make a nice complement.
Little Tommy is watching a firework show. As circular shapes spread across the sky, a splendid view unfolds on the night of Lunar New Year's eve.
A wonder strikes Tommy. How many regions are formed by the circl... | from decimal import *
getcontext().prec = 40
eps = Decimal('1e-10')
class Circle:
def __init__(self, x, y, r):
self.x = x
self.y = y
self.r = r
def contains(self, c):
dd = (self.x - c.x)**2 + (self.y - c.y)**2 # dd = d*d
return dd < (self.r - c.r)**2 and self.r > c.r
def in_touches(self, c):
dd = (... | python | code_algorithm | [
{
"input": "3\n0 0 2\n3 0 2\n6 0 2\n",
"output": "6\n"
},
{
"input": "3\n0 0 1\n2 0 1\n4 0 1\n",
"output": "4\n"
},
{
"input": "3\n0 0 2\n2 0 2\n1 1 2\n",
"output": "8\n"
},
{
"input": "3\n3 4 9\n2 -3 1\n-1 1 4\n",
"output": "4\n"
},
{
"input": "3\n2 -5 2\n-5 -6 3... | code_contests | python | 0 | 9cb60c35181249d79632e06850d8d568 |
It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem.
There are n displays placed along a road, and the i-th of them can display a text with font size s_i only. Maria Stepanovna wants to rent such t... | mod = 1000000007
MOD = 998244353
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
it = lambda : tuple(map(int, input().split()))
ls = lambda : list(input())
n=ii()
l=il()
c=il()
mn=10**20
f... | python | code_algorithm | [
{
"input": "3\n100 101 100\n2 4 5\n",
"output": "-1\n"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n10 13 11 14 15 12 13 13 18 13\n",
"output": "33\n"
},
{
"input": "5\n2 4 5 4 10\n40 30 20 10 40\n",
"output": "90\n"
},
{
"input": "3\n1 2 3\n1 1 1\n",
"output": "3\n"
},
{... | code_contests | python | 0.1 | 53d5410655bcf09635ea4e3ff8ed7be2 |
The [BFS](https://en.wikipedia.org/wiki/Breadth-first_search) algorithm is defined as follows.
1. Consider an undirected graph with vertices numbered from 1 to n. Initialize q as a new [queue](http://gg.gg/queue_en) containing only vertex 1, mark the vertex 1 as used.
2. Extract a vertex v from the head of the qu... | from sys import stdin
from collections import deque
n=int(stdin.readline())
g=dict()
for i in range(n-1):
a,b=map(int,stdin.readline().split())
g.setdefault(a, set()).add(b)
g.setdefault(b, set()).add(a)
a=[int(x) for x in stdin.readline().split()]
ans = True
if n > 1 and a[0] == 1:
q=deque()
m=[0]*... | python | code_algorithm | [
{
"input": "4\n1 2\n1 3\n2 4\n1 2 4 3\n",
"output": "No\n"
},
{
"input": "4\n1 2\n1 3\n2 4\n1 2 3 4\n",
"output": "Yes\n"
},
{
"input": "4\n1 2\n1 3\n1 4\n2 1 3 4\n",
"output": "No\n"
},
{
"input": "4\n2 3\n3 4\n4 1\n2 3 4 1\n",
"output": "No\n"
},
{
"input": "6\n... | code_contests | python | 0 | 167f2875f7786d26028de9b1d193b3f9 |
Codefortia is a small island country located somewhere in the West Pacific. It consists of n settlements connected by m bidirectional gravel roads. Curiously enough, the beliefs of the inhabitants require the time needed to pass each road to be equal either to a or b seconds. It's guaranteed that one can go between any... | import heapq
n,m,a,b=map(int,input().split())
graph={i:[] for i in range(n)}
for i in range(m):
u,v,w=map(int,input().split())
graph[u-1].append((v-1,w))
graph[v-1].append((u-1,w))
components=[-1]*n
comp=-1
for i in range(n):
if components[i]==-1:
comp+=1
components[i]=comp
prev=... | python | code_algorithm | [
{
"input": "6 7 13 22\n1 2 13\n2 3 13\n1 4 22\n3 4 13\n4 5 13\n5 6 13\n6 1 13\n",
"output": "0 13 26 39 26 13\n"
},
{
"input": "5 5 20 25\n1 2 25\n2 3 25\n3 4 20\n4 5 20\n5 1 20\n",
"output": "0 25 60 40 20\n"
},
{
"input": "3 2 509529 5982470\n1 2 509529\n3 2 509529\n",
"output": "0... | code_contests | python | 0 | b837ba73c0b82ae2e9d6733749554334 |
Consider a sequence of digits of length 2^k [a_1, a_2, …, a_{2^k}]. We perform the following operation with it: replace pairs (a_{2i+1}, a_{2i+2}) with (a_{2i+1} + a_{2i+2})mod 10 for 0≤ i<2^{k-1}. For every i where a_{2i+1} + a_{2i+2}≥ 10 we get a candy! As a result, we will get a sequence of length 2^{k-1}.
Less for... | import sys
input = lambda: sys.stdin.readline().strip()
from math import log2
n = int(input())
ls = list(map(int, input().split()))
Sum = [0]
for i in ls:
Sum.append(Sum[-1]+i)
q = int(input())
for i in range(q):
a, b = map(int, input().split())
print((Sum[b]-Sum[a-1])//10)
| python | code_algorithm | [
{
"input": "8\n8 7 3 1 7 0 9 4\n3\n1 8\n2 5\n7 7\n",
"output": "3\n1\n0\n"
},
{
"input": "6\n0 1 2 3 3 5\n3\n1 2\n1 4\n3 6\n",
"output": "0\n0\n1\n"
},
{
"input": "2\n9 9\n2\n1 1\n1 2\n",
"output": "0\n1\n"
},
{
"input": "4\n8 5 6 6\n3\n2 3\n2 3\n3 4\n",
"output": "1\n1\n... | code_contests | python | 0.3 | f44ddd6393ed0e8be142d2e9d33607fc |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.