id stringlengths 14 117 | description stringlengths 29 13k | code stringlengths 10 49.8k | test_samples dict | source class label 3
classes | prompt stringlengths 391 104k |
|---|---|---|---|---|---|
p02296 Distance_23655 | For given two segments s1 and s2, print the distance between them.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -10000 ≤ xpi, ypi ≤ 10000
* p0 ≠ p1 and p2 ≠ p3.
Input
The entire input looks like:
q (the number of queries)
1st query
2nd query
...
qth... | def dot(a, b):
return a.real * b.real + a.imag * b.imag
def cross(a, b):
return a.real * b.imag - a.imag * b.real
def ccw(p0, p1, p2):
a = p1-p0
b = p2-p0
if cross(a,b) > 0:
return 1 #couner_clockwise
elif cross(a,b) <0:
return -1 #clockwise
elif dot(a,b) < 0:
return 2 #online_back
elif ab... | {
"input": [
"3\n0 0 1 0 0 1 1 1\n0 0 1 0 2 1 1 2\n-1 0 1 0 0 1 0 -1",
"3\n0 0 1 0 0 1 1 1\n0 0 1 0 2 1 1 2\n-1 1 1 0 0 1 0 -1",
"3\n0 1 1 0 0 1 1 1\n-1 0 1 0 2 1 1 2\n-1 1 0 0 0 1 0 -1",
"3\n0 1 1 0 0 1 1 1\n-1 0 1 1 2 1 1 2\n-1 1 0 0 0 1 0 -1",
"3\n0 1 1 0 0 1 1 1\n-1 0 1 1 1 1 1 2\n-1 1 0 0 0 0... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
For given two segments s1 and s2, print the distance between them.
s1 is formed by end points p0 and p1, and s2 is formed by end points p2 and p3.
Constraints
* 1 ≤ q ≤ 1000
* -100... |
p02443 Reverse_23658 | Write a program which reads a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and reverse specified elements by a list of the following operation:
* reverse($b, e$): reverse the order of $a_b, a_{b+1}, ..., a_{e-1}$
Constraints
* $1 \leq n \leq 1,000$
* $-1,000,000,000 \leq a_i \leq 1,000,000,000$
* $1 \leq ... | n = int(input())
a = list(map(int, input().split(' ')))
q = int(input())
for i in range(q):
b, e = list(map(int, input().split(' ')))
a[b:e] = a[b:e][::-1]
print(' '.join(list(map(str, a))))
| {
"input": [
"8\n1 2 3 4 5 6 7 8\n2\n1 6\n3 8",
"8\n1 2 3 4 5 6 7 8\n2\n1 6\n1 8",
"8\n1 1 3 4 5 6 7 8\n2\n1 6\n1 8",
"8\n1 1 3 4 5 5 7 8\n2\n1 6\n1 8",
"8\n1 4 3 4 5 6 7 8\n2\n1 6\n3 8",
"8\n1 2 3 4 5 0 7 8\n2\n1 6\n1 8",
"8\n1 1 4 4 5 6 7 8\n2\n1 6\n1 8",
"8\n1 1 3 4 7 5 7 8\n2\n1 6\... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Write a program which reads a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and reverse specified elements by a list of the following operation:
* reverse($b, e$): reverse ... |
1020_A. New Building for SIS_23668 | You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.
The bu... | t,f,lw,hw,q = map(int,input().split())
while(q):
t1,f1,t2,f2=map(int,input().split())
if(t1==t2):
print(abs(f1-f2))
else:
ans=abs(t1-t2)
if((f1>=lw and f1<=hw) or (f2>=lw and f2<=hw)):
ans+=abs(f1-f2)
print(ans)
elif(f1<lw and f2 <lw) :
ans+=(abs(lw-f1)+abs(lw-f2))
print(an... | {
"input": [
"3 6 2 3 3\n1 2 1 3\n1 4 3 4\n1 2 2 3\n",
"100000000 100000000 100000000 100000000 1\n100000000 100000000 100000000 1\n",
"1 1 1 1 1\n1 1 1 1\n",
"1 0 1 1 1\n1 1 1 1\n",
"1 0 1 2 1\n1 1 1 2\n",
"1 0 1 2 1\n2 1 1 2\n",
"1 0 1 2 1\n3 1 0 1\n",
"1 0 2 2 1\n3 1 0 1\n",
"3 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations... |
1043_D. Mysterious Crime_23672 | Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her house? Mr Gawry, world-famous detective, is appointed to find the criminal. He asked m neighbours of Ada about clients w... | n, m = map(int, input().split())
a = []
for i in range(m):
a.append(list(map(int, input().split())))
b = [set([]) for i in range(n)]
for i in range(n):
for j in range(m):
ya = a[j][i]
if i == n-1:
b[ya-1].add(100000000)
else:
b[ya-1].add(a[j][i+1])
class UnionFind():
def __init__(self,si... | {
"input": [
"2 2\n1 2\n2 1\n",
"3 2\n1 2 3\n2 3 1\n",
"5 6\n1 2 3 4 5\n2 3 1 4 5\n3 4 5 1 2\n3 5 4 2 1\n2 3 5 4 1\n1 2 3 4 5\n",
"1 1\n1\n",
"2 1\n1 2\n",
"10 5\n6 8 4 1 7 10 3 2 5 9\n4 1 7 10 2 5 9 6 8 3\n3 6 8 4 1 7 10 2 5 9\n2 5 9 8 4 1 7 10 3 6\n2 5 9 6 8 4 1 7 10 3\n",
"10 3\n4 10 5 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be f... |
1066_B. Heaters_23676 | Vova's house is an array consisting of n elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The i-th element of the array is 1 if there is a heater in the position i, otherwise the i-th element of the array is 0.
Each heater has a va... | n,r=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
i=ans=0
while i<n:
pointer=i
f=0
while pointer<n:
if pointer-r+1>i:
break
if a[pointer]==1:
j=pointer
f=1
pointer+=1
if f==0:
pointer=i-1
while pointer>... | {
"input": [
"5 10\n0 0 0 0 0\n",
"5 3\n1 0 0 0 1\n",
"6 2\n0 1 1 0 0 1\n",
"10 3\n0 0 1 1 0 1 0 0 0 1\n",
"33 729\n0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1 1 0 0 0 1\n",
"1 502\n1\n",
"13 7\n0 0 0 0 0 0 0 1 0 0 0 1 0\n",
"3 6\n1 1 1\n",
"187 64\n1 1 1 1 1 1 1 1 1 1 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Vova's house is an array consisting of n elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The i... |
1107_G. Vasya and Maximum Profit_23682 | Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit.
Vasya has n problems to choose from. They are numbered from 1 to n. The difficulty of the i-th problem is d_i. Moreover, the problems are given in the increasing order by the... | import sys
it = iter(sys.stdin.readlines())
input = it.__next__
n, k = map(int, input().split())
d, c = [0] * n, [0] * n
for i in range(n):
a, b = map(int, input().split())
d[i], c[i] = a, k - b
p, r, pre, suf, sm, mx = list(range(n)), [0] * n, c[:], c[:], c[:], c[:]
def find(x):
if x != p[x]:
p[x... | {
"input": [
"5 10\n1 15\n5 3\n6 11\n7 2\n11 22\n",
"3 5\n1 8\n2 19\n3 11\n",
"10 8\n1 16\n23 27\n26 2\n32 21\n38 50\n51 53\n62 79\n77 97\n94 18\n100 5\n",
"9 985\n227 166\n325 695\n408 426\n548 491\n627 627\n733 288\n802 59\n924 471\n933 492\n",
"1 235\n15 210\n",
"3 476\n343 574\n744 244\n84... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit.
Vasya has n problems to choose from. ... |
1137_B. Camp Schedule_23686 | The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedule. After some continuous discussion, they came up with a schedule s, which can be represented as a binary string, in which the i-th symb... |
def findNeedSubstring(s):
n = len(s)
rst = ''
tmp = 0
for i in range(1, n-1, 1):
if(s[0:i] == s[n-i:n]):
rst = s[0:i] # this is the longest substring that prefix = suffix
tmp = i
return s[tmp: n]
def polyHash(s):
rst = [0] * (len(s) + 1)
h... | {
"input": [
"10\n11100\n",
"10010110\n100011\n",
"101101\n110\n",
"11100010100111101011101101011011011100010001111001\n0000011001\n",
"00000000000000000000000000000000000000000000000000\n111111111111111111111111111111\n",
"11100100\n01101001\n",
"11111111\n1\n",
"111111111111111111111... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The new camp by widely-known over the country Spring Programming Camp is going to start soon. Hence, all the team of friendly curators and teachers started composing the camp's schedu... |
1155_D. Beautiful Array_23690 | You are given an array a consisting of n integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.
You may choose at most one consecutive subarray o... | import sys
#Library Info(ACL for Python/Pypy) -> https://github.com/not522/ac-library-python
def input():
return sys.stdin.readline().rstrip()
DXY = [(0, -1), (1,0), (0, 1), (-1,0)] #L,D,R,Uの順番
INF = 1 << 64
def main():
n,x = map(int,input().split())
a = list(map(int,input().split()))
A = [0] + a.c... | {
"input": [
"5 10\n-1 -2 -3 -4 -5\n",
"12 -3\n1 3 3 7 1 3 3 7 1 3 3 7\n",
"5 -2\n-3 8 -2 1 -6\n",
"5 0\n1 2 -3 -4 6\n",
"40 -98\n67397987 -343838159 -618322596 -546414490 293066140 -773772966 277974552 -434260219 -791222310 -340023233 -737934384 910703436 -308962211 735427170 -284205825 955071831... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given an array a consisting of n integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty ... |
1178_A. Prime Minister_23694 | Alice is the leader of the State Refactoring Party, and she is about to become the prime minister.
The elections have just taken place. There are n parties, numbered from 1 to n. The i-th party has received a_i seats in the parliament.
Alice's party has number 1. In order to become the prime minister, she needs to b... | import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in s... | {
"input": [
"3\n80 60 60\n",
"2\n6 5\n",
"3\n100 50 50\n",
"4\n51 25 99 25\n",
"8\n100 40 60 60 60 60 60 60\n",
"5\n2 1 1 1 4\n",
"4\n100 50 51 100\n",
"4\n40 20 29 80\n",
"7\n5 1 2 3 4 5 6\n",
"100\n100 48 48 22 21 27 51 43 53 34 54 37 45 37 27 53 39 53 37 44 55 44 56 36 55 4... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Alice is the leader of the State Refactoring Party, and she is about to become the prime minister.
The elections have just taken place. There are n parties, numbered from 1 to n. Th... |
1196_A. Three Piles of Candies_23698 | Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the... | for z in range (int(input())):
a=list(map(int,input().split()))
print(sum(a)//2) | {
"input": [
"4\n1 3 4\n1 10 100\n10000000000000000 10000000000000000 10000000000000000\n23 34 45\n",
"1\n111 2 3\n",
"1\n111 4 3\n",
"4\n1 3 4\n1 10 100\n10000000000000000 10000000000000000 10000000000000000\n23 2 45\n",
"1\n111 3 3\n",
"4\n1 3 2\n1 10 100\n10000000000000000 10000000000000000... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob take... |
1213_C. Book Reading_23702 | Polycarp is reading a book consisting of n pages numbered from 1 to n. Every time he finishes the page with the number divisible by m, he writes down the last digit of this page number. For example, if n=15 and m=5, pages divisible by m are 5, 10, 15. Their last digits are 5, 0, 5 correspondingly, their sum is 10.
You... | q = int(input())
for i in range(q):
n, m = input().split(" ")
n = int(n)
m = int(m)
if n == m:
print(m%10)
continue
inicial = m
aux = (m%10)
m += inicial
while (m%10) != (inicial%10) and m <= n:
aux += (m%10)
m += inicial
m = m-inicial
... | {
"input": [
"7\n1 1\n10 1\n100 3\n1024 14\n998244353 1337\n123 144\n1234312817382646 13\n",
"7\n1 1\n10 2\n100 3\n1024 14\n998244353 1337\n123 144\n1234312817382646 13\n",
"7\n1 1\n10 2\n100 3\n1024 14\n998244353 1337\n123 144\n1234312817382646 16\n",
"7\n1 1\n10 2\n100 3\n1024 14\n998244353 1337\n12... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Polycarp is reading a book consisting of n pages numbered from 1 to n. Every time he finishes the page with the number divisible by m, he writes down the last digit of this page numbe... |
1236_D. Alice and the Doll_23706 | Alice got a new doll these days. It can even walk!
Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column.
However, the d... | from sys import stdout
printn = lambda x: stdout.write(x)
inn = lambda : int(input())
inl = lambda: list(map(int, input().split()))
inm = lambda: map(int, input().split())
DBG = True and False
BIG = 999999999
R = 10**9 + 7
def ddprint(x):
if DBG:
print(x)
n,m,k = inm()
hx = {}
hy = {}
cnt = 0
for i ... | {
"input": [
"3 3 2\n3 1\n2 2\n",
"3 3 2\n2 2\n2 1\n",
"3 3 8\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n",
"48145 89209 5\n3 1\n3 2\n3 3\n2 3\n1 3\n",
"2 2 2\n1 2\n2 2\n",
"10 10 46\n5 4\n3 5\n5 3\n3 2\n6 6\n3 7\n7 5\n3 6\n6 3\n10 3\n5 7\n5 6\n10 7\n7 6\n5 5\n4 5\n7 4\n10 2\n10 6\n6 7\n10 1\n6 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Alice got a new doll these days. It can even walk!
Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the ... |
1254_B2. Send Boxes to Alice (Hard Version)_23709 | This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems
Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend... | # 素因数分解
def prime_decomposition(n):
i = 2
table = []
while i * i <= n:
while n % i == 0:
n //= i
table.append(i)
i += 1
if n > 1:
table.append(n)
return table
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
# ... | {
"input": [
"3\n4 8 5\n",
"5\n3 10 2 1 5\n",
"1\n1\n",
"4\n0 5 15 10\n",
"16\n47 14 19 48 2 41 38 25 5 24 38 22 1 30 38 41\n",
"3\n0 0 1\n",
"15\n1 1 1 0 0 0 1 1 1 0 0 0 1 1 1\n",
"17\n64 26 39 57 12 0 64 26 35 8 50 3 64 16 4 42 33\n",
"19\n51 65 42 25 41 45 68 58 2 33 9 17 28 41 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
This is the harder version of the problem. In this version, 1 ≤ n ≤ 10^6 and 0 ≤ a_i ≤ 10^6. You can hack this problem if you locked it. But you can hack the previous problem only if ... |
1278_B. A and B_23713 | You are given two integers a and b. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 1; during the second operation you choose one of these numbers and increase it by 2, and so on. You choose the number of these operations yourself.
For example, if... | cum_sum = [0]*47000
cum_sum[1]=1
for i in range(2,47000):
cum_sum[i] = cum_sum[i-1]+i
n = int(input())
for i in range(n):
a,b = map(int,input().split())
delta = max(a,b) - min(a,b)
cnt = cum_sum.index([el for el in cum_sum if el <= delta][-1])
eq = a+b+cum_sum[cnt]
while (eq%2!=0 or eq//2 <... | {
"input": [
"3\n1 3\n11 11\n30 20\n",
"1\n1 139282\n",
"1\n1999 1998\n",
"1\n1 265787\n",
"1\n1999 2915\n",
"3\n0 3\n11 11\n30 20\n",
"1\n1 47024\n",
"1\n1947 2915\n",
"3\n1 3\n11 1\n30 20\n",
"1\n1 50548\n",
"1\n1947 2838\n",
"3\n1 3\n11 0\n30 20\n",
"1\n1 29189\n... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given two integers a and b. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by 1; during the second operat... |
1321_C. Remove Adjacent_23717 | You are given a string s consisting of lowercase Latin letters. Let the length of s be |s|. You may perform several operations on this string.
In one operation, you can choose some index i and remove the i-th character of s (s_i) if at least one of its adjacent characters is the previous letter in the Latin alphabet f... | t=int(input())
s=input()
ans=''
count=0
while(True):
flag=True
j=-1
ch=0
for i in range(len(s)):
if (i!=0 and ord(s[i-1])-ord(s[i])==-1) or (i!=len(s)-1 and ord(s[i+1])-ord(s[i])==-1):
if j==-1 or ord(ch)<ord(s[i]):
ch=s[i]
j=i
flag=Fal... | {
"input": [
"6\nabbbbb\n",
"4\nbcda\n",
"8\nbacabcab\n",
"7\ncddcbcb\n",
"100\nmxjrojjwtrauhhccftvjsyfyfsnbdnwjfltyjetsylbddrkoqjxbmientcowknrecfqcvxfgsbymwyvakmbulhvrxzvzbygajtgc\n",
"8\naaaaaaaa\n",
"100\nyltrygcqmgjzsxoahbvovspancmoaltdrxgjnxwxbrehubvradguoqgiodzanljxtszdutuzgcnihmwevo... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given a string s consisting of lowercase Latin letters. Let the length of s be |s|. You may perform several operations on this string.
In one operation, you can choose some i... |
1340_B. Nastya and Scoreboard_23721 | Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer.
The poor boy was very upset because of that. He w... | import sys
def f():
n, k = map(int, input().split())#2000, 2000
ch = {0:'1110111',1:'0010010',2:'1011101',3:'1011011',4:'0111010',5:'1101011',6:'1101111',7:'1010010',8:'1111111',9:'1111011'}
l = []
for _ in range(n):
l.append(sys.stdin.readline().rstrip())
def tr(s1, s2):
tot = 0
... | {
"input": [
"3 5\n0100001\n1001001\n1010011\n",
"2 5\n0010010\n0010010\n",
"1 7\n0000000\n",
"3 8\n0111010\n1011011\n1011101\n",
"2 7\n0010010\n1000000\n",
"3 7\n0111001\n1010011\n1000000\n",
"10 10\n1110111\n0111111\n1111111\n1111111\n0111111\n1111111\n0111111\n1111110\n1111111\n1111111\... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe ... |
1362_B. Johnny and His Hobbies_23725 | Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, Johnny is unaware that combining these two activities can get him in a lot of trouble.
There is a set S containing very important number... | t=int(input())
for T in range(t):
n=int(input())
lista=[int(x) for x in input().split()]
ma=max(lista)
mi=min(lista)
listb=[0]*n
ans=0
for k in range(1,2*ma+1):
temp=0
for i in range(n):
listb[i]=lista[i]^k
tk=0
if(listb[i] not in lista):
temp=1
break
if(temp==0):
ans=1
print(k)
b... | {
"input": [
"6\n4\n1 0 2 3\n6\n10 7 14 8 3 12\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 1023\n",
"6\n4\n1 0 2 3\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 1023\n",
"6\n4\n1 0 2 5\n6\n10 7 14 8 0 12\n2\n0 2\n3\n1 2 3\n6\n1 4 6 10 11 12\n2\n0 1023\n",
"6\n4\n1 0 2 3\n6\n10 7 14 8 0 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Among Johnny's numerous hobbies, there are two seemingly harmless ones: applying bitwise operations and sneaking into his dad's office. As it is usually the case with small children, ... |
1382_C2. Prefix Flip (Hard Version)_23729 | This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of the problem are solved.
There are two binary strings a and b of length n (a binary string is a string consisting of symbols 0 and 1). In ... | t=int(input())
for _ in range(t):
n=int(input())
arr=list(input().rstrip("\n"))
arr1=list(input().rstrip("\n"))
arr=[int(x) for x in arr]
arr1=[int(x) for x in arr1]
#print(arr,n)
result=[]
if arr == arr1:
print(0)
else:
if n == 1:
print("1 1")
... | {
"input": [
"5\n2\n01\n10\n5\n01011\n11100\n2\n01\n01\n10\n0110011011\n1000110100\n1\n0\n1\n",
"5\n2\n01\n10\n5\n01011\n11100\n2\n01\n01\n10\n0110011011\n1000110100\n1\n0\n1\n",
"5\n2\n01\n10\n5\n01011\n11100\n2\n01\n01\n10\n0100011011\n1000110100\n1\n0\n1\n",
"5\n2\n01\n10\n5\n01010\n11100\n2\n01\n0... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
This is the hard version of the problem. The difference between the versions is the constraint on n and the required number of operations. You can make hacks only if all versions of t... |
1447_A. Add Candies_23734 | There are n bags with candies, initially the i-th bag contains i candies. You want all the bags to contain an equal amount of candies in the end.
To achieve this, you will:
* Choose m such that 1 ≤ m ≤ 1000
* Perform m operations. In the j-th operation, you will pick one bag and add j candies to all bags apart ... | a=int(input())
for i in range(a):
b=int(input())
arr=[str(i) for i in range(b,1,-1)]
arr=arr[::-1]
print(len(arr))
print(' '.join(arr))
| {
"input": [
"2\n2\n3\n",
"100\n100\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n7... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
There are n bags with candies, initially the i-th bag contains i candies. You want all the bags to contain an equal amount of candies in the end.
To achieve this, you will:
* Cho... |
1471_B. Strange List_23738 | You have given an array a of length n and an integer x to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be q. If q is divisible by x, the robot adds x copies of the integer q/x to the end of the array, and moves on to the next element. Note ... | t=int(input())
for _ in range(t):
n,x=[int(z) for z in input().split()]
a=[int(x) for x in input().split()]
minCycles=float('inf')
minCycIdx=-1
for i,z in enumerate(a):
nCycles=0
while z%x==0:
nCycles+=1
z//=x
if nCycles<minCycles:
min... | {
"input": [
"2\n1 2\n12\n4 2\n4 6 8 2\n",
"3\n2 3\n1 12\n4 2\n4 6 8 9\n5 7\n49 49 49 7 49\n",
"2\n5 2\n8 16 84 58 17\n5 2\n39 12 59 31 93\n",
"1\n20 9\n288 279 252 279 288 207 261 270 297 288 276 265 209 217 214 300 227 221 215 271\n",
"3\n2 3\n1 12\n4 2\n4 6 8 9\n5 7\n49 49 49 9 49\n",
"2\n5... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You have given an array a of length n and an integer x to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element ... |
1498_B. Box Fitting_23742 | You are given n rectangles, each of height 1. Each rectangle's width is a power of 2 (i. e. it can be represented as 2^x for some non-negative integer x).
You are also given a two-dimensional box of width W. Note that W may or may not be a power of 2. Moreover, W is at least as large as the width of the largest recta... | from sys import stdin
from math import log2
input = lambda: stdin.readline().strip()
t = int(input())
for _ in range(t):
n, W = map(int, input().split())
ls = list(map(int, input().split()))
d = []
for i in range(20):
d.append(0)
for i in ls:
d[int(log2(i))]+=1
result = 0
wh... | {
"input": [
"2\n5 16\n1 2 8 4 8\n6 10\n2 8 8 2 2 8\n",
"2\n5 13\n1 2 8 4 8\n6 10\n2 8 8 2 2 8\n",
"2\n5 13\n1 2 8 2 8\n6 10\n4 8 8 2 2 8\n",
"2\n5 12\n1 4 8 4 8\n6 10\n2 8 8 2 2 8\n",
"2\n5 8\n1 2 8 2 8\n6 11\n4 8 8 2 2 8\n",
"2\n5 12\n1 2 8 4 8\n6 12\n2 2 8 2 2 8\n",
"2\n5 19\n1 2 8 2 4\... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given n rectangles, each of height 1. Each rectangle's width is a power of 2 (i. e. it can be represented as 2^x for some non-negative integer x).
You are also given a two-d... |
1520_A. Do Not Be Distracted!_23746 | Polycarp has 26 tasks. Each task is designated by a capital letter of the Latin alphabet.
The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot re... | def solve(task):
unique = set(task)
segments = 0
prev = ' '
for letter in task:
if letter != prev:
segments+=1
prev = letter
return "YES" if len(unique) == segments else "NO"
if __name__ == '__main__':
T = int(input().strip())
for _ in range(T):
days ... | {
"input": [
"5\n3\nABA\n11\nDDBBCCCBBEZ\n7\nFFGZZZY\n1\nZ\n2\nAB\n",
"1\n14\nVORISLOVEELINA\n",
"1\n10\nAAAAAAAAAF\n",
"1\n10\nAAAAAAAAAJ\n",
"1\n10\nAAAAAAAAAC\n",
"1\n10\nAAAAAAAAAA\n",
"1\n10\nILOVEELINA\n",
"1\n24\nDEBOOFORCEDMETODOTHISSMH\n",
"1\n10\nAAAAAAAABB\n",
"1\n14... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Polycarp has 26 tasks. Each task is designated by a capital letter of the Latin alphabet.
The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve s... |
1547_F. Array Stabilization (GCD version)_23750 | You are given an array of positive integers a = [a_0, a_1, ..., a_{n - 1}] (n ≥ 2).
In one step, the array a is replaced with another array of length n, in which each element is the [greatest common divisor (GCD)](http://tiny.cc/tuy9uz) of two neighboring elements (the element itself and its right neighbor; consider t... | #!/usr/bin/env python
#from __future__ import division, print_function
import math
import os
import sys
#from fractions import *
from sys import *
from decimal import *
from io import BytesIO, IOBase
from itertools import accumulate,combinations,permutations,combinations_with_replacement,product
from collections import... | {
"input": [
"5\n4\n16 24 10 5\n4\n42 42 42 42\n3\n4 6 4\n5\n1 2 3 4 5\n6\n9 9 27 9 9 63\n",
"1\n97\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 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 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\n",
"1\n97\... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given an array of positive integers a = [a_0, a_1, ..., a_{n - 1}] (n ≥ 2).
In one step, the array a is replaced with another array of length n, in which each element is the ... |
175_C. Geometry Horse_23754 | Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value.
There are n types of geometric figures. The number of figures of type ki and figure cost ci is kno... |
n = int(input())
figures = []
for _ in range(n):
amount, cost = map(int, input().split())
figures += [(cost, amount)]
figures.sort()
t = int(input())
T = list(map(int, input().split()))
result = 0
destructed = 0
i, j = 0, 0
while i < len(figures):
if j < len(T) and destructed + figures[i][1] >= T[j]... | {
"input": [
"2\n3 8\n5 10\n1\n20\n",
"1\n5 10\n2\n3 6\n",
"20\n1 676\n10 2\n10 467\n7 826\n7 138\n8 76\n8 148\n2 121\n7 527\n3 571\n10 410\n7 174\n2 318\n6 97\n3 919\n8 684\n3 586\n4 570\n10 494\n8 582\n74\n1 6 10 15 20 22 25 26 27 29 32 33 34 37 39 44 49 52 53 55 56 61 65 66 70 72 74 77 79 80 83 85 88 9... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Vasya plays the Geometry Horse.
The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figur... |
195_D. Analyzing Polyline_23758 | As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son complete a programming task. The task goes like that.
Let's consider a set of fun... | from sys import stdin
from math import gcd
n = int(stdin.readline())
iset = set()
for _ in range(n):
k,b = map(int,stdin.readline().split())
if k!=0:
if b==0:
iset.add((0,1))
else:
x = gcd(k,b)
if k*b>0:
iset.add((abs(b//x),abs(k//x)))
... | {
"input": [
"3\n1 0\n0 2\n-1 1\n",
"3\n-2 -4\n1 7\n-5 1\n",
"1\n1 0\n",
"10\n148 134\n145 140\n105 144\n196 199\n195 166\n110 175\n140 198\n112 188\n147 145\n153 196\n",
"1\n3 5\n",
"10\n9 9\n-5 2\n-2 9\n0 6\n6 7\n-1 -10\n-8 6\n3 6\n-3 -9\n0 4\n",
"4\n1000000000 -1000000000\n-1000000000 1... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let th... |
219_D. Choosing Capital for Treeland_23762 | The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the direction of the roads into consideration, we can get from any city to any other one.
The council of the elders has recently decided to ... | #More es mi pastor nada me faltara
import sys
from collections import defaultdict
from typing import NamedTuple
class EdgeNode(NamedTuple):
vertex: int
weight: int
class Graph:
def __init__(self, n):
self.graph = d = {i: [] for i in range(n + 1)}
self.distance = [0] * (n + 1)
... | {
"input": [
"3\n2 1\n2 3\n",
"4\n1 4\n2 4\n3 4\n",
"15\n15 6\n2 15\n14 15\n15 12\n1 2\n10 15\n7 4\n13 7\n11 2\n5 2\n7 2\n3 2\n7 8\n9 15\n",
"2\n1 2\n",
"12\n10 7\n6 10\n8 10\n4 12\n10 4\n3 12\n1 2\n2 7\n4 5\n11 5\n11 9\n",
"13\n7 13\n5 4\n5 1\n11 12\n8 3\n13 4\n10 7\n11 5\n8 13\n2 8\n10 6\n4 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The country Treeland consists of n cities, some pairs of them are connected with unidirectional roads. Overall there are n - 1 roads in the country. We know that if we don't take the ... |
243_A. The Brand New Function_23766 | Polycarpus has a sequence, consisting of n non-negative integers: a1, a2, ..., an.
Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of bitwise OR of all the sequence elements with indexes from l to r. Formally: f(l, r) = al | al + 1 | ... | ar.
Polycarpus took a piece of... | n, a, b = input(), set(), set()
for i in map(int, input().split()):
b = set(i | j for j in b)
b.add(i)
a.update(b)
print(len(a))
| {
"input": [
"10\n1 2 3 4 5 6 1 2 9 10\n",
"3\n1 2 0\n",
"1\n1000000\n",
"5\n0 1 2 0 4\n",
"1\n123\n",
"10\n6 8 4 5 1 9 10 2 3 7\n",
"1\n0\n",
"7\n1 2 4 8 16 32 64\n",
"10\n375813 659427 484038 348181 432640 368050 271089 721588 345312 630771\n",
"1\n1001000\n",
"5\n0 1 2 1... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Polycarpus has a sequence, consisting of n non-negative integers: a1, a2, ..., an.
Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of b... |
291_A. Spyke Talks_23772 | Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We know that when two people call each other via Spyke, the Spyke network assigns a unique ID to this call, a positive integer session number.... | n=int(input())
l=list(map(int,input().split()))
ans=0
repeat={}
for i in l:
if i in repeat:
repeat[i]+=1
if i==0:
pass
elif repeat[i]>2:
ans=-1
break
else:
ans+=1
else:
repeat[i]=1
print(ans) | {
"input": [
"3\n1 1 1\n",
"1\n0\n",
"6\n0 1 7 1 7 10\n",
"1\n1000000000\n",
"2\n1 0\n",
"6\n6 6 0 8 0 0\n",
"5\n1 0 0 0 1\n",
"2\n2 3\n",
"5\n10000 10000 1 1 10000\n",
"100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Polycarpus is the director of a large corporation. There are n secretaries working for the corporation, each of them corresponds via the famous Spyke VoIP system during the day. We kn... |
316_D1. PE Lesson_23776 | Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning.... | m = 1000000007
n = int(input())
a = map(int, input().split())
t1, t2 = 0, 0
for i in a:
if i == 1:
t1 += 1
else:
t2 += 1
a = [1, 2]
for i in range(3, t1+1):
a = a[::-1]
a[1] = (a[0]+(i-1)*a[1])%m
if not t1 or t1 == 1:
a[1] = 1
for i in range(t1+1, n+1):
a[1] = a[1]*i%m
print(a[1]) | {
"input": [
"5\n1 2 2 1 2\n",
"8\n1 2 2 1 2 1 1 2\n",
"10\n1 1 1 1 2 1 1 1 1 1\n",
"10\n1 1 1 1 1 2 1 1 2 1\n",
"9\n1 1 1 2 2 1 1 1 1\n",
"1\n2\n",
"10\n2 2 2 2 2 2 1 2 1 1\n",
"100\n2 2 2 2 1 1 2 2 1 2 2 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 2 1 1 2 2 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 2 1 2 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very c... |
339_D. Xenia and Bit Operations_23780 | Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.
Namely, it takes several iterations to calculate value v. At the first iteration, Xeni... | from operator import or_, xor
import sys
greg=1
#Stolen from tchr to test Xenia and Bit Operations speed.
n, m = map(int, input().split())
t = [list(map(int, input().split()))]
for i in range(n):
t += [[(or_, xor)[i & 1](*t[i][j: j + 2]) for j in range(0, len(t[i]), 2)]]
for s in sys.stdin:
p, b = s.split()... | {
"input": [
"2 4\n1 6 3 5\n1 4\n3 4\n1 2\n1 2\n",
"1 10\n6 26\n1 11\n1 9\n1 31\n1 10\n2 12\n1 8\n2 10\n2 4\n2 18\n1 31\n",
"1 10\n22 17\n2 15\n2 12\n1 6\n1 16\n2 24\n1 21\n2 19\n2 25\n2 19\n2 1\n",
"2 10\n15 27 17 18\n2 12\n4 19\n2 3\n4 1\n3 15\n3 11\n2 5\n1 26\n1 17\n3 18\n",
"2 10\n31 17 22 5\n... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they wor... |
362_A. Two Semiknights Meet_23784 | A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 squares to the right, 2 squares forward and 2 squares to the left, 2 squares backward and 2 to the right and 2 squares backward and 2 to the le... | t = int(input())
for _ in range(t):
s = [input() for i in range(8)]
oh = True
flag = True
for i in range(8):
for j in range(8):
if(s[i][j] == 'K'):
if(flag):
pos1x = i
pos1y = j
flag = False
e... | {
"input": [
"2\n........\n........\n......#.\nK..##..#\n.......#\n...##..#\n......#.\nK.......\n\n........\n........\n..#.....\n..#..#..\n..####..\n...##...\n........\n....K#K#\n",
"4\n#.##..##\n###.#.K#\n###.####\n.####.#.\n.#######\n######K#\n####.#..\n#.######\n\n#.......\n....##K.\n........\n...#.##.\n#.... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
A boy Petya loves chess very much. He even came up with a chess piece of his own, a semiknight. The semiknight can move in any of these four directions: 2 squares forward and 2 square... |
406_C. Graph Cutting_23789 | Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.
Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partiti... | import sys
input = sys.stdin.readline
print = sys.stdout.write
def get_input():
n, m = [int(x) for x in input().split(' ')]
graph = [[] for _ in range(n + 1)]
for _ in range(m):
c1, c2 = [int(x) for x in input().split(' ')]
graph[c1].append(c2)
graph[c2].append(c1)
if m % 2 ... | {
"input": [
"8 12\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n3 5\n3 6\n5 6\n6 7\n6 8\n7 8\n",
"3 3\n1 2\n2 3\n3 1\n",
"3 2\n1 2\n2 3\n",
"4 6\n1 2\n2 3\n3 4\n4 1\n1 3\n2 4\n",
"4 5\n1 2\n2 3\n3 4\n4 1\n1 3\n",
"4 4\n1 2\n2 3\n3 1\n1 4\n",
"10 9\n2 1\n3 1\n4 2\n5 4\n6 2\n7 4\n8 1\n9 8\n10 8\n",
"7... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.
Chris is given a simple undirected connected graph with n v... |
456_A. Laptops_23795 | One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there are two laptops, such that the price of the first laptop is less (strictly smaller) than the price of the second laptop but the quality ... | n = int(input())
for _ in range(n):
a,b = map(int,input().split())
if a!=b:
print ('Happy Alex')
exit()
print ('Poor Alex') | {
"input": [
"2\n1 2\n2 1\n",
"3\n1 2\n2 3\n3 1\n",
"3\n2 2\n3 3\n1 1\n",
"4\n4 1\n3 2\n2 3\n1 4\n",
"3\n1 2\n2 1\n3 3\n",
"1\n1 1\n",
"3\n3 3\n1 2\n2 1\n",
"3\n3 2\n1 1\n2 3\n",
"2\n2 1\n1 2\n",
"2\n1 1\n2 2\n",
"3\n2 3\n1 1\n3 2\n",
"3\n1 2\n2 5\n3 1\n",
"4\n4 2\n... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
One day Dima and Alex had an argument about the price and quality of laptops. Dima thinks that the more expensive a laptop is, the better it is. Alex disagrees. Alex thinks that there... |
478_D. Red-Green Towers_23798 | There are r red and g green blocks for construction of the red-green tower. Red-green tower can be built following next rules:
* Red-green tower is consisting of some number of levels;
* Let the red-green tower consist of n levels, then the first level of this tower should consist of n blocks, second level — of n... | """
#If FastIO not needed, used this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
import bisect
from types import GeneratorType
BUFSIZE = 8192
class SortedList:
def __init__(self, iterable=[], _load=200):
... | {
"input": [
"1 1\n",
"4 6\n",
"9 7\n",
"136749 183300\n",
"100000 1\n",
"18 3\n",
"10000 10000\n",
"121147 78249\n",
"121146 78249\n",
"1 100000\n",
"2954 1977\n",
"38 36\n",
"10 10\n",
"89671 32487\n",
"6 6\n",
"25580 17318\n",
"200000 199999\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
There are r red and g green blocks for construction of the red-green tower. Red-green tower can be built following next rules:
* Red-green tower is consisting of some number of lev... |
5_D. Follow Traffic Rules_23810 | Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the ro... | """
Codeforces
5D - Follow Traffic Rules
http://codeforces.com/contest/5/problem/D
Héctor González Belver
../07/2018
"""
import sys
def time_distance(v0, a, d):
#quadratic equation. Time is positive ==> root with (+discriminant) in quadratic formula
#d = v0*t + (a/2)*t^2
return (-v0 + (v0**2 + 2*a*d)**0.5)/a
d... | {
"input": [
"5 70\n200 170 40\n",
"1 1\n2 1 3\n",
"20 40\n9958 9799 30\n",
"23 4\n5 2 13\n",
"46 20\n4 2 35\n",
"38 3\n47 16 4\n",
"39 42\n35 19 12\n",
"5527 8154\n3974 3550 1639\n",
"11 13\n24 15 37\n",
"4 120\n5112 3000 130\n",
"200 1000\n3 2 10000\n",
"200 1000\n3 2... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Everybody knows that the capital of Berland is connected to Bercouver (the Olympic capital) by a direct road. To improve the road's traffic capacity, there was placed just one traffic... |
622_B. The Time_23814 | You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.
Note that you should find only the time after a minutes, see the examples to clarify the problem statement.
You can read more about 24-hour format here <https://en.wikipedia.org/wiki/24-hour_clock>.
Input
The first line... | #!/usr/bin/env python3
import datetime
def main(args):
hh, mm = list(map(int, input().split(':')))
a = int(input())
mm += a
hh += (mm // 60)
mm %= 60
hh %= 24
print(f"{hh:02d}:{mm:02d}")
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
| {
"input": [
"10:10\n0\n",
"23:59\n10\n",
"20:20\n121\n",
"00:01\n59\n",
"00:55\n4321\n",
"22:30\n30\n",
"10:05\n1\n",
"03:10\n20\n",
"23:00\n60\n",
"23:01\n59\n",
"19:02\n59\n",
"20:01\n2699\n",
"03:30\n0\n",
"15:04\n36\n",
"01:10\n1\n",
"05:12\n0\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given the current time in 24-hour format hh:mm. Find and print the time after a minutes.
Note that you should find only the time after a minutes, see the examples to clarify ... |
670_D2. Magic Powder - 2_23820 | The term of this problem is the same as the previous one, the only exception — increased restrictions.
Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence a1, a2, ...,... | n, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
l = 0
r = 10 ** 10
while r - l > 1:
m = (r + l) // 2
poroshok = 0
for i in range(n):
tr = A[i] * m - B[i]
poroshok += max(tr, 0)
if poroshok > k:
r = m
else:
l = m... | {
"input": [
"1 1000000000\n1\n1000000000\n",
"3 1\n2 1 4\n11 3 16\n",
"10 1\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n1 1 1 1 1 1 1 1 1 1\n",
"4 3\n4 3 5 6\n11 12 14 20\n",
"1 1\n4\n6\n",
"10 926\n5 6 8 1 2 5 1 8 4 4\n351 7... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The term of this problem is the same as the previous one, the only exception — increased restrictions.
Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, ... |
696_A. Lorenzo Von Matterhorn_23824 | Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two... | def find_path(x,y):
p1,p2 = [],[]
while x!=0:
p1.append(x)
x = x//2
while y!=0:
p2.append(y)
y = y//2
p1 = p1[::-1]
p2 = p2[::-1]
# print (p1,p2)
for i in range(min(len(p1),len(p2))):
if p1[i]==p2[i]:
ind = i
else:
break
path = []
for i in range(ind,len(p1)):
path.append(p1[i])
path = path... | {
"input": [
"7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4\n",
"2\n1 100000000000000 200000000000000 1\n2 276447232 552894464\n",
"2\n1 562949953421312 562949953421311 1\n2 562949953421312 562949953421311\n",
"1\n2 666077344481199252 881371880336470888\n",
"10\n2 37526406560905229 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and ano... |
717_B. R3D3’s Summer Adventure_23827 | R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktails and going to concerts of popular local bands. While listening to "The White Buttons" and their hit song "Dacan the Baker", he met anoth... | import sys,heapq
#sys.stdin=open("data.txt")
input=sys.stdin.readline
n,a,b=map(int,input().split())
if a<b: a,b=b,a
if b==0:
# 1 01 001 0001 ... is optimal, plus a long series of 0's
print((n-1)*a)
else:
# pascal's triangle thing
pascal=[[1]*20005]
for i in range(20004):
n... | {
"input": [
"4 1 2\n",
"8954 4025 7157\n",
"1006 9237 1123\n",
"6222 7124 6784\n",
"5992 2773 6869\n",
"6995 4636 8251\n",
"1736 5628 2595\n",
"100000000 100000000 0\n",
"2793 175 3594\n",
"100000000 50000000 0\n",
"4755 6759 4899\n",
"28258585 6194848 49146833\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
R3D3 spent some time on an internship in MDCS. After earning enough money, he decided to go on a holiday somewhere far, far away. He enjoyed suntanning, drinking alcohol-free cocktail... |
761_C. Dasha and Password_23833 | After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:
* There is at least one digit in the string,
* There is at least one lowercase (small) letter of the Latin alphabet in the string... | n,m=map(int,input().split())
m1,m2,m3=[m+1]*n,[m+1]*n,[m+1]*n
for i in range(n):
s=input()
for j, c in enumerate(s):
z=min(j, m-j)
if c>='0' and c<='9':
m1[i]=min(m1[i],z)
else:
if c>='a' and c<='z':
m2[i]=min(m2[i],z)
else:
... | {
"input": [
"3 4\n1**2\na3*0\nc4**\n",
"5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&\n",
"3 1\n1\nz\n#\n",
"10 2\n0n\n5h\n7&\n1b\n5&\n4*\n9k\n0*\n7m\n62\n",
"25 16\n&*#&#**sw&**&#&#\n&*#*d#j*3b&q***#\n###&yq*v3q*&##**\n#**&#jpt#*#*#\n***#y*cd&l*oe*##\n&&&***#k*e&*p&#*\n&###*&fkn*... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements... |
784_E. Twisted Circuit_23837 | <image>
Input
The input consists of four lines, each line containing a single digit 0 or 1.
Output
Output a single digit, 0 or 1.
Example
Input
0
1
1
0
Output
0 | import collections as col
import itertools as its
import sys
import operator
from bisect import bisect_left, bisect_right
from copy import copy, deepcopy
class Solver:
def __init__(self):
pass
def solve(self):
a = int(input())
b = int(input())
c = int(input())
d = ... | {
"input": [
"0\n1\n1\n0\n",
"1\n0\n0\n1\n",
"0\n1\n0\n0\n",
"1\n1\n0\n1\n",
"0\n1\n0\n1\n",
"0\n0\n0\n1\n",
"0\n0\n1\n0\n",
"1\n0\n1\n1\n",
"1\n1\n1\n0\n",
"0\n1\n1\n1\n",
"1\n0\n1\n0\n",
"1\n1\n0\n0\n",
"0\n0\n0\n0\n",
"0\n0\n1\n1\n",
"1\n0\n0\n0\n",
"... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
<image>
Input
The input consists of four lines, each line containing a single digit 0 or 1.
Output
Output a single digit, 0 or 1.
Example
Input
0
1
1
0
Output
0
### Input:
... |
805_D. Minimum number of steps_23841 | We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring, our job is done. Print the minimum number of steps we should perform to make our job done modulo 109 + 7... | def mess():
String=input()
count_it=0
Counter=0
for i in String:
if i=='a':
count_it = (count_it * 2) % Modulo
count_it+=1
elif i=='b':
Counter+=count_it
#count_it =(count_it* 2)%Modulo
return Counter
if __name__ == "__main__":
M... | {
"input": [
"aab\n",
"ab\n",
"aabbaababbabbbaabbaababaaaabbaaaabaaaaaababbaaaabaababbabbbb\n",
"bbababbbaabaaaaaaaabbabbbb\n",
"abbbbbbbbbbbbbbbbbbbbbbbbbbaababaaaaaaabaabaaababaabaababaaabababaababab\n",
"aababbaaaabbaabbbbbbbbabbababbbaaabbaaabbabbba\n",
"aaabaaaabbababbaabbababbbbaaaaa... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If... |
830_D. Singer House_23845 | It is known that passages in Singer house are complex and intertwined. Let's define a Singer k-house as a graph built by the following process: take complete binary tree of height k and add edges from each vertex to all its successors, if they are not yet present.
<image> Singer 4-house
Count the number of non-empty ... | print([0,1,9,245,126565,54326037,321837880,323252721,754868154,328083248,838314395,220816781,893672292,166441208,251255697,114256285,118775501,482714697,11784725,460862131,550384565,106742050,425241115,626692854,674266678,320014275,345949512,527320049,897822749,137190263,491039182,810384961,482023334,658099864,88679098... | {
"input": [
"3\n",
"2\n",
"20\n",
"9\n",
"181\n",
"261\n",
"8\n",
"121\n",
"351\n",
"71\n",
"141\n",
"361\n",
"1\n",
"61\n",
"301\n",
"392\n",
"10\n",
"321\n",
"161\n",
"151\n",
"41\n",
"251\n",
"241\n",
"395\n",
"271... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
It is known that passages in Singer house are complex and intertwined. Let's define a Singer k-house as a graph built by the following process: take complete binary tree of height k a... |
876_A. Trip For Meal_23851 | Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. There are winding paths between each pair of houses. The length of a path between Rabbit's and Owl's houses is a meters, between Rabbit's an... | # 876A
def main():
n, a, b, c = int(input()), int(input()), int(input()), int(input())
if n == 1:
return 0
n -= 1
count = 0
state = 0
while n:
if state == 0:
if a > b:
count += b
state = 2
else:
count += a
state = 1
elif state == 1:
if a > c:
count += c
state = 2
else:
... | {
"input": [
"1\n2\n3\n5\n",
"3\n2\n3\n1\n",
"1\n2\n6\n1\n",
"1\n1\n1\n99\n",
"2\n3\n2\n1\n",
"62\n27\n16\n72\n",
"1\n15\n13\n11\n",
"5\n3\n3\n1\n",
"1\n1\n1\n1\n",
"75\n50\n50\n5\n",
"9\n37\n85\n76\n",
"65\n5\n8\n7\n",
"5\n5\n5\n5\n",
"100\n100\n100\n100\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. Winnie has got three best friends: Rabbit, Owl and Eeyore, each of them lives in his own house. The... |
8_A. Train and Peter_23855 | Peter likes to travel by train. He likes it so much that on the train he falls asleep.
Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.
The boy start... | import re
I = input
s, first, second = I(), I(), I()
forward = re.fullmatch('\w*' + first + '\w*' + second + '\w*', s)
backward = re.fullmatch('\w*' + first + '\w*' + second + '\w*', s[::-1])
if forward:
print('both' if backward else 'forward')
else:
print('backward' if backward else 'fantasy') | {
"input": [
"atob\na\nb\n",
"aaacaaa\naca\naa\n",
"babaabababaaaababaabababaabababababababbababbbabbaabababaababbaabbababaababaaabababaabbaababaaababaa\nabaabababaa\nabaabbaa\n",
"abacaba\naca\nba\n",
"aabaab\naba\nab\n",
"bbabbbbababbaabaabaa\nabb\nbaab\n",
"aababaaababaabbaabababaaababa... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Peter likes to travel by train. He likes it so much that on the train he falls asleep.
Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. The... |
94_A. Restoring Password_23859 | Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was said to contain "some real funny stuff about swine influenza". The antivirus had no objections and Igor K. run the flash application he had ... | s = input()
a = []
for i in range(10):
a.append(input())
p = 0
while(p < 80):
for i in range(10):
if a[i] == s[p:p+10]:
print(i, end='')
p += 10
break
| {
"input": [
"10101101111001000010100100011010101101110010110111011000100011011110010110001000\n1001000010\n1101111001\n1001000110\n1010110111\n0010110111\n1101001101\n1011000001\n1110010101\n1011011000\n0110001000\n",
"01001100100101100000010110001001011001000101100110010110100001011010100101101100\n01001100... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was sai... |
977_E. Cyclic Components_23863 | You are given an undirected graph consisting of n vertices and m edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph theory.
An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices.... | from sys import stdin
from collections import deque
def bfs(x):
vis[x] = True
q = deque([x])
ans = 1
while q:
cur = q.popleft()
ans &= len(g[cur]) == 2
for x in g[cur]:
if not vis[x]:
vis[x] = True
q.append(x)
return ans
n, m = ... | {
"input": [
"5 4\n1 2\n3 4\n5 4\n3 5\n",
"17 15\n1 8\n1 12\n5 11\n11 9\n9 15\n15 5\n4 13\n3 13\n4 3\n10 16\n7 10\n16 7\n14 3\n14 4\n17 6\n",
"5 10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5\n",
"5 5\n1 2\n2 3\n3 4\n4 1\n3 5\n",
"5 10\n1 2\n2 3\n3 4\n4 5\n5 1\n1 4\n2 4\n3 5\n3 1\n2 5\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given an undirected graph consisting of n vertices and m edges. Your task is to find the number of connected components which are cycles.
Here are some definitions of graph t... |
p02655 AtCoder Grand Contest 045 - Fragile Balls_23878 | We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick up one of the balls from that box, and put it into another box.
Since the balls are very easy to break, you cannot move Ball i more th... | import sys
from bisect import bisect_left
from collections import defaultdict
from itertools import accumulate
class UnionFind:
def __init__(self, n):
self.table = [-1] * n
def _root(self, x):
stack = []
tbl = self.table
while tbl[x] >= 0:
stack.append(x)
... | {
"input": [
"3 3\n1 2 1\n2 1 1\n1 3 2",
"1 1\n1 1 1",
"2 2\n1 2 1\n2 1 1",
"5 5\n1 2 1\n2 1 1\n1 3 2\n4 5 1\n5 4 1",
"6 3\n1 2 1\n2 1 1\n1 3 2",
"2 2\n1 2 1\n1 1 1",
"5 5\n1 2 1\n3 1 1\n1 3 2\n4 5 1\n5 4 1",
"5 5\n1 2 1\n3 1 1\n1 3 2\n1 5 1\n5 4 1",
"2 1\n1 1 1",
"5 5\n1 2 1\n... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
We have N boxes numbered 1 to N, and M balls numbered 1 to M. Currently, Ball i is in Box A_i.
You can do the following operation:
* Choose a box containing two or more balls, pick ... |
p02784 AtCoder Beginner Contest 153 - Common Raccoon vs Monster_23882 | Raccoon is fighting with a monster.
The health of the monster is H.
Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i. There is no other way to decrease the monster's health.
Raccoon wins when the monster's health becomes 0 or below.
If Raccoon can win without using... | h,n = map(int,input().split())
AS = list(map(int,input().split()))
print('Yes' if h <= sum(AS) else 'No') | {
"input": [
"211 5\n31 41 59 26 53",
"10 3\n4 5 6",
"20 3\n4 5 6",
"210 5\n31 41 59 26 53",
"211 5\n31 33 59 26 53",
"10 3\n4 4 6",
"20 3\n3 5 6",
"210 3\n31 41 59 26 53",
"211 5\n31 33 59 26 24",
"10 3\n4 0 6",
"20 3\n3 3 6",
"210 3\n31 55 59 26 53",
"211 9\n31 33... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Raccoon is fighting with a monster.
The health of the monster is H.
Raccoon can use N kinds of special moves. Using the i-th move decreases the monster's health by A_i. There is no ... |
p02919 AtCoder Beginner Contest 140 - Second Sum_23886 | Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.
Find \displaystyle \sum_{L=1}^{N-1} \sum_{R=L+1}^{N} X_{L,R}.
Constraints
* 2 \le N \le 10^5
* 1 \le P_i \le N
* P_i \neq P_j (i \neq j)
* All values... | # 区間ではなく各要素を独立に考える典型のやつ
# ある値について条件を満たすようにしたいとき
# その値より大きいものだけ探せば良い
# 具体的には自分より大きい値を1つだけ含むような区間が条件を満たす
# 愚直にやると各値について, その左右を近い方から順に見ていかなければならず間に合わない
# 自分より大きい値だけ知りたいので, 要素の大きい順に見ていくことで計算量が減る
# 普通にやったのが下のコードでAC+TLE(恐らく合ってる)
def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
... | {
"input": [
"5\n1 2 3 4 5",
"3\n2 3 1",
"8\n8 2 7 3 4 5 6 1",
"3\n1 3 2",
"3\n3 1 2",
"5\n1 3 2 4 5",
"5\n2 3 1 4 5",
"8\n8 1 7 3 4 5 6 2",
"3\n3 2 1",
"3\n2 1 3",
"3\n1 2 3",
"3\n1 3 2",
"3\n1 2 3",
"3\n3 1 2",
"3\n2 1 3",
"3\n3 2 1"
],
"output": [... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Given is a permutation P of \\{1, 2, \ldots, N\\}.
For a pair (L, R) (1 \le L \lt R \le N), let X_{L, R} be the second largest value among P_L, P_{L+1}, \ldots, P_R.
Find \displayst... |
p03055 AtCoder Grand Contest 033 - Removing Coins_23890 | Takahashi and Aoki will play a game on a tree. The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i.
At the beginning of the game, each vertex contains a coin. Starting from Takahashi, he and Aoki will alternately perform the following operation:
* Choose a vertex ... | import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline
def solve():
N = int(rl())
graph = [[] for _ in range(N)]
for _ in range(N - 1):
a, b = map(lambda x: int(x) - 1, rl().split())
graph[a].append(b)
graph[b].append(a)
def bfs(no... | {
"input": [
"7\n1 7\n7 4\n3 4\n7 5\n6 3\n2 1",
"3\n1 2\n2 3",
"6\n1 2\n2 3\n2 4\n4 6\n5 6",
"3\n2 2\n2 3",
"6\n1 2\n2 1\n2 4\n4 6\n5 6",
"7\n1 7\n7 1\n3 4\n7 5\n6 3\n2 1",
"6\n1 2\n2 3\n2 4\n6 6\n5 6",
"6\n1 2\n2 1\n2 4\n4 6\n4 6",
"7\n1 7\n7 1\n3 4\n2 5\n6 3\n2 1",
"6\n1 3\n2... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Takahashi and Aoki will play a game on a tree. The tree has N vertices numbered 1 to N, and the i-th of the N-1 edges connects Vertex a_i and Vertex b_i.
At the beginning of the game... |
p03199 CADDi 2018 - Square_23893 | Takahashi has an N \times N grid. The square at the i-th row and the j-th column of the grid is denoted by (i,j). Particularly, the top-left square of the grid is (1,1), and the bottom-right square is (N,N).
An integer, 0 or 1, is written on M of the squares in the Takahashi's grid. Three integers a_i,b_i and c_i desc... | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
"""
・適当な部分空間なので、達成可能でありさえすれば数え上げは簡単
・対角成分に関して向かい側はまとめる。「必ず0」「必ず1」「free」
・対角から遠いところについては調整不可能。「必ず1」がここにあると死亡
・そうでない場合、対角~対角+2までを矛盾なく作れれば勝ち
"""
import itertools
MOD = 998244353
N,M = map(int,readline().... | {
"input": [
"100000 0",
"4 8\n1 1 1\n1 2 0\n3 2 1\n1 4 0\n2 1 1\n1 3 0\n3 4 1\n4 4 1",
"4 5\n1 3 1\n2 4 0\n2 3 1\n4 2 1\n4 4 1",
"3 5\n1 3 1\n3 3 0\n3 1 0\n2 3 1\n3 2 1",
"3 3\n1 1 1\n3 1 0\n2 3 1",
"4 5\n1 3 0\n2 4 0\n2 3 1\n4 2 1\n4 4 1",
"2 1\n1 1 1\n3 1 0\n2 3 1",
"2 0\n1 1 0\n3 1... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Takahashi has an N \times N grid. The square at the i-th row and the j-th column of the grid is denoted by (i,j). Particularly, the top-left square of the grid is (1,1), and the botto... |
p03346 AtCoder Grand Contest 024 - Backfront_23897 | You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:
* Choose an element in the sequence and move it to the beginning or the end of the sequence.
Find the minimum number of op... | N = int(input())
P = [int(input()) for _ in range(N)]
L = [1]*(N+1)
for i in range(N):
if P[i] < N and L[P[i]+1] == 1:
L[P[i]+1] = L[P[i]]+1
if L[P[i]] == 1:
L[P[i]] = 0
ans = N-max(L)
print(ans) | {
"input": [
"4\n1\n3\n2\n4",
"6\n3\n2\n5\n1\n4\n6",
"8\n6\n3\n1\n2\n7\n4\n8\n5",
"6\n2\n3\n5\n1\n4\n6",
"8\n6\n3\n2\n1\n7\n4\n8\n5",
"4\n1\n4\n2\n3",
"6\n3\n1\n5\n2\n4\n6",
"4\n3\n1\n2\n4",
"8\n6\n3\n1\n2\n5\n4\n8\n7",
"4\n2\n4\n1\n3",
"8\n6\n3\n2\n1\n4\n7\n8\n5",
"6\n... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following ... |
p03506 Code Festival Team Relay (Parallel) - Evergrowing Tree_23900 | You are given an integer N. Consider an infinite N-ary tree as shown below:
<image>
Figure: an infinite N-ary tree for the case N = 3
As shown in the figure, each vertex is indexed with a unique positive integer, and for every positive integer there is a vertex indexed with it. The root of the tree has the index 1. ... | N, Q = map(int, input().split())
vws = [tuple(map(int, input().split())) for _ in range(Q)]
anss = []
if N == 1:
anss = [min(v, w) for v, w in vws]
else:
for v, w in vws:
v, w = v-1, w-1
while v != w:
if v > w:
v = (v-1)//N
else:
w = (w-1)... | {
"input": [
"3 3\n5 7\n8 11\n3 9",
"100000 2\n1 2\n3 4",
"3 3\n5 7\n8 14\n3 9",
"3 3\n9 7\n8 14\n3 9",
"3 3\n9 7\n8 14\n3 5",
"3 3\n5 7\n8 8\n3 9",
"100000 1\n1 2\n3 4",
"3 3\n9 5\n5 14\n3 5",
"3 3\n5 7\n8 8\n4 9",
"3 3\n9 5\n7 14\n3 5",
"3 3\n8 7\n8 8\n4 9",
"2 3\n9 7... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given an integer N. Consider an infinite N-ary tree as shown below:
<image>
Figure: an infinite N-ary tree for the case N = 3
As shown in the figure, each vertex is indexed... |
p03667 AtCoder Grand Contest 017 - Snuke and Spells_23904 | There are N balls in a row. Initially, the i-th ball from the left has the integer A_i written on it.
When Snuke cast a spell, the following happens:
* Let the current number of balls be k. All the balls with k written on them disappear at the same time.
Snuke's objective is to vanish all the balls by casting the ... | from collections import Counter
n, m = map(int, input().split())
an = list(map(int, input().split()))
ac_ = Counter(an)
ac = {i: ac_[i] if i in ac_ else 0 for i in range(1, n + 1)}
ad = [0] * n
for a, c in ac.items():
for i in range(max(0, a - c), a):
ad[i] += 1
ans = ad.count(0)
anss = []
for x, y in (ma... | {
"input": [
"5 3\n1 1 3 4 5\n1 2\n2 5\n5 4",
"4 4\n4 4 4 4\n4 1\n3 1\n1 1\n2 1",
"10 10\n8 7 2 9 10 6 6 5 5 4\n8 1\n6 3\n6 2\n7 10\n9 7\n9 9\n2 4\n8 1\n1 8\n7 7",
"5 3\n1 1 5 4 5\n1 2\n2 5\n5 4",
"10 10\n8 7 2 9 10 6 6 5 5 4\n8 1\n6 3\n6 2\n7 10\n9 7\n9 9\n2 4\n8 2\n1 8\n7 7",
"5 3\n1 1 3 1 5... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
There are N balls in a row. Initially, the i-th ball from the left has the integer A_i written on it.
When Snuke cast a spell, the following happens:
* Let the current number of bal... |
p03822 AtCoder Grand Contest 009 - Tournament_23908 | N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament. For some reasons, the tournament may not be "fair" for all the contestants. That is, the number of the matches that must be played in order to win the championship may be different for each contestant. The struct... | import sys
sys.setrecursionlimit(10**5)
n = int(input())
dic = {}
for i in range(n+1):
if i <= 1: continue
ai = int(input())
if ai in dic:
dic[ai].append(i)
else:
dic[ai] = [i]
def dfs(nxt):
lst = []
if nxt in dic:
while dic[nxt] != []:
child = dic[nxt].pop(... | {
"input": [
"7\n1\n2\n1\n3\n1\n4",
"4\n4\n4\n1",
"5\n1\n1\n2\n4",
"7\n1\n2\n2\n3\n1\n4",
"5\n1\n1\n1\n4",
"5\n1\n0\n1\n3",
"7\n1\n4\n2\n3\n1\n3",
"5\n0\n0\n1\n3",
"4\n3\n4\n0",
"7\n1\n4\n2\n3\n1\n4",
"5\n1\n1\n1\n3",
"7\n1\n4\n2\n3\n1\n7",
"7\n1\n2\n1\n3\n1\n0",
... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
N contestants participated in a competition. The total of N-1 matches were played in a knockout tournament. For some reasons, the tournament may not be "fair" for all the contestants.... |
p03989 AtCoder Grand Contest 005 - ~K Perm Counting_23911 | Snuke loves permutations. He is making a permutation of length N.
Since he hates the integer K, his permutation will satisfy the following:
* Let the permutation be a_1, a_2, ..., a_N. For each i = 1,2,...,N, |a_i - i| \neq K.
Among the N! permutations of length N, how many satisfies this condition?
Since the ans... | from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
fro... | {
"input": [
"425 48",
"4 1",
"4 3",
"3 1",
"4 2",
"425 69",
"7 1",
"6 3",
"2 1",
"5 1",
"319 69",
"7 2",
"12 3",
"319 88",
"10 2",
"319 138",
"2 2",
"135 138",
"101 244",
"100 638",
"425 8",
"4 5",
"723 69",
"12 1",
"... | 5ATCODER | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Snuke loves permutations. He is making a permutation of length N.
Since he hates the integer K, his permutation will satisfy the following:
* Let the permutation be a_1, a_2, ..., a... |
p00077 Run Length_23915 | If the strings are consecutive, you can replace the characters with a rule to shorten the string. For example, for the string AAAA, the expression @ 4A will compress one character. Create a program that restores the character string compressed by this rule to the original character string. However, it is assumed that t... | while True:
try:
s = input()
except EOFError:
break
r = ''
n = 1
for c in s:
if n < 0:
n = int(c)
elif c == '@':
n = -1
else:
r += c * n
n = 1
print(r) | {
"input": [
"ab@5C1@8050\n@99+1=1@90",
"ab@5C1@8050\n@99,1=1@90",
"ab@5C2@8050\n@99,1=1@90",
"ab@5C2?8050\n@99,1=1@90",
"ab@5C1@8050\nA99+1=1@90",
"ab@5C2@8050\n@89,1=1@90",
"ab@5C2?8050\n@91,1=9@90",
"`b@5C1@8050\nA99+1=1@90",
"ab@5C3@8050\n@89,1=1@90",
"`b@5C2@8050\nA99+1=1@... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
If the strings are consecutive, you can replace the characters with a rule to shorten the string. For example, for the string AAAA, the expression @ 4A will compress one character. Cr... |
p00209 Scene in a Picture_23919 | Mr. A came to Aizu for sightseeing. You can overlook the Aizu Basin from the window of the hotel where you stayed. As I was looking at the scenery, I noticed a piece of the photo on the floor. Apparently I took the outside view from the window. "Which area did you take?" A asked, holding the photo so that the view outs... | from sys import stdin
readline = stdin.readline
from itertools import product
from operator import itemgetter
from math import isinf
def scrap_top_left(picture):
for py, px in product(range(len(picture)), repeat=2):
if picture[py][px] != -1:
return px, py
def is_match(window, picture, wx, ... | {
"input": [
"8 4\n2 1 3 1 1 5 1 3\n2 3 2 4 1 0 2 1\n0 3 1 2 1 1 4 2\n1 2 3 2 1 1 5 4\n0 2 0 1 1 3 2 1\n1 3 1 2 2 4 3 2\n5 1 2 1 4 1 1 5\n4 1 1 0 1 2 2 1\n2 -1 -1 -1\n0 3 -1 -1\n-1 2 2 4\n-1 1 -1 1\n5 3\n1 0 2 3 5\n2 3 7 2 1\n2 5 4 2 2\n8 9 0 3 3\n3 6 0 4 7\n-1 -1 2\n-1 3 5\n0 4 -1\n0 0",
"8 4\n2 1 3 1 1 5 1... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Mr. A came to Aizu for sightseeing. You can overlook the Aizu Basin from the window of the hotel where you stayed. As I was looking at the scenery, I noticed a piece of the photo on t... |
p00369 Paper Fortune_23922 | If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written.
Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your ... | def sub(maxs, mins):
for i in range(len(maxs)):
if maxs[i] != mins[i]:
if i == len(maxs)-1:
return int(maxs[i]) - int(mins[i])
if i == len(maxs)-2:
return int(maxs[i:i+2]) - int(mins[i:i+2])
return 10
return 0
def checkEqual(S):
an... | {
"input": [
"123125129",
"119138",
"11121314",
"9714431",
"16612328",
"23422731",
"754526",
"955577",
"75547",
"2112",
"799",
"88",
"32523857",
"4787",
"1859551",
"135661",
"3675",
"156692",
"167918384",
"83994",
"4837847",
"1451... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written.
Each digit ranges from 1 to 9 (zero is avoided because i... |
p00586 A + B Problem_23926 | Compute A + B.
Constraints
* -1000 ≤ A, B ≤ 1000
Input
The input will consist of a series of pairs of integers A and B separated by a space, one pair of integers per line. The input will be terminated by EOF.
Output
For each pair of input integers A and B, you must output the sum of A and B in one line.
Example
... | ans=""
while True:
try:
info=input()
except:
break
if ans!="":
ans+="\n"
info_2=info.split()
ans+=str(int(info_2[0])+int(info_2[1]))
print(ans) | {
"input": [
"1 2\n10 5\n100 20",
"1 2\n10 5\n101 20",
"1 2\n10 4\n101 20",
"1 2\n10 4\n101 37",
"1 2\n11 4\n101 37",
"1 3\n11 4\n101 37",
"1 6\n11 4\n101 37",
"1 6\n2 4\n101 37",
"1 6\n1 4\n101 37",
"1 6\n1 0\n101 37",
"0 6\n1 0\n101 37",
"0 6\n1 0\n100 37",
"0 6\n... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Compute A + B.
Constraints
* -1000 ≤ A, B ≤ 1000
Input
The input will consist of a series of pairs of integers A and B separated by a space, one pair of integers per line. The inp... |
p00723 Organize Your Train part II_23930 | RJ Freight, a Japanese railroad company for freight operations has recently constructed exchange lines at Hazawa, Yokohama. The layout of the lines is shown in Figure B-1.
<image>
Figure B-1: Layout of the exchange lines
A freight train consists of 2 to 72 freight cars. There are 26 types of freight cars, which are d... | import itertools
def main():
n = int(input())
for _ in range(n):
s = input()
ssum = set()
for i in range(len(s)):
l = s[:i]
rl = l[::-1]
r = s[i:]
rr = r[::-1]
ssum.add(l+r)
ssum.add(r+l)
ssum.add(rl+r)
... | {
"input": [
"4\naa\nabba\nabcd\nabcde",
"4\naa\nabba\nbbcd\nabcde",
"4\nab\nabba\nbbcd\nedcba",
"4\nb`\nabba\nbcbe\nbbced",
"4\n`b\nabba\nbebe\nbecbd",
"4\n`b\naabb\nbebe\nbecbd",
"4\n`c\naabb\nbebe\nbecce",
"4\ne`\na`bb\nbbee\neddeb",
"4\ne`\na`ba\nbbee\neddeb",
"4\n_f\na`ba\... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
RJ Freight, a Japanese railroad company for freight operations has recently constructed exchange lines at Hazawa, Yokohama. The layout of the lines is shown in Figure B-1.
<image>
Fi... |
p01126 Amida_23937 | The mayor of Amida, the City of Miracle, is not elected like any other city. Once exhausted by long political struggles and catastrophes, the city has decided to leave the fate of all candidates to the lottery in order to choose all candidates fairly and to make those born under the lucky star the mayor. I did. It is a... | while True:
n, m, a = map(int, input().split(' '))
if n == 0 and m == 0 and a == 0:
break
hl = [list(map(int, input().split(' '))) for i in range(m)]
hl.sort(key=lambda x: x[0], reverse=True)
vl = a
for l in hl:
if l[1] == vl:
vl = l[2]
elif l[2] == vl:
... | {
"input": [
"4 4 1\n3 1 2\n2 2 3\n3 3 4\n1 3 4\n0 0 0",
"4 4 1\n3 1 2\n2 2 4\n3 3 4\n1 3 4\n0 0 0",
"4 4 1\n3 1 2\n2 2 3\n4 3 4\n1 3 4\n0 0 0",
"4 4 1\n3 1 2\n2 2 4\n3 3 4\n1 1 4\n0 0 0",
"4 4 1\n0 1 2\n2 2 3\n3 3 4\n0 3 4\n0 0 0",
"4 3 1\n3 1 2\n0 1 3\n4 3 4\n1 0 1\n0 0 0",
"4 3 1\n3 1 1... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The mayor of Amida, the City of Miracle, is not elected like any other city. Once exhausted by long political struggles and catastrophes, the city has decided to leave the fate of all... |
p01425 White Bird_23942 | Angry Birds is a mobile game of a big craze all over the world. You were convinced that it was a waste of time to play the game, so you decided to create an automatic solver.
<image>
You are describing a routine that optimizes the white bird's strategy to defeat a pig (enemy) by hitting an egg bomb. The white bird fo... | # aoj 2308
import sys
input = sys.stdin.readline
def calc_y(vy, t):
return vy*t - g/2*t*t
def loc(y, b, t):
if y < b+eps:
return -1
if y > t-eps:
return 1
return 0
def can_hit(x, y):
if x == 0:
return False
a = g*g/4
b = g*y-V*V
c = x*x+y*y
D = b*b-4*a*c... | {
"input": [
"0 7 3 1",
"1 7 2 2\n0 1 1 2",
"1 7 3 1\n1 1 2 2",
"1 4 2 2\n0 1 1 2",
"0 7 2 1",
"1 5 3 1\n1 1 2 2",
"2 4 2 2\n0 1 1 2",
"1 5 3 1\n1 1 2 4",
"2 4 2 2\n0 2 1 2",
"1 5 3 1\n1 1 3 4",
"2 4 2 2\n0 2 2 2",
"2 4 4 2\n0 2 1 2",
"2 3 4 2\n0 2 1 2",
"2 3 4 ... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Angry Birds is a mobile game of a big craze all over the world. You were convinced that it was a waste of time to play the game, so you decided to create an automatic solver.
<image>... |
p01741 Manhattan_23948 | In Manhattan, roads run where the x or y coordinate is an integer. Both Sunuke-kun's house and Sumeke-kun's house are on the road, and the straight line distance (Euclidean distance) is just d. Find the maximum value that can be considered as the shortest distance when traveling along the road from Sunuke-kun's house t... | #!/usr/bin/env python3
d = float(input())
ans = max(2**0.5 * d, int(d) + 1)
print(ans)
| {
"input": [
"1.000",
"2.345",
"1.6623460269048462",
"3.25937378137542",
"2.618243582076879",
"4.191829833885925",
"2.6469616558038407",
"4.247442548717362",
"2.711344148850446",
"4.7522412032328525",
"2.895597134917855",
"5.55775493898071",
"2.9286608953325257",
... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
In Manhattan, roads run where the x or y coordinate is an integer. Both Sunuke-kun's house and Sumeke-kun's house are on the road, and the straight line distance (Euclidean distance) ... |
p01881 Help the Princess!_23951 | Example
Input
2 4
%.@\$
..\$\$
Output
Yes | import queue
di = [0,1,0,-1]
dj = [1,0,-1,0]
h, w = map(int, input().split())
field = [input() for i in range(h)]
pdist = [[1000]*w for i in range(h)]
sdist = [[1000]*w for i in range(h)]
pque = queue.Queue()
sque = queue.Queue()
for i,row in enumerate(field):
for j in range(w):
if row[j] is '@':
... | {
"input": [
"2 4\n%.@\\$\n..\\$\\$",
"2 4\n%.\\@$\n..\\$\\$",
"2 4\n%.\\@$\n$\\$\\..",
"2 3\n%.\\@$\n$\\$\\..",
"1 4\n%.\\@$\n..\\$\\$",
"1 4\n%.\\@$\n-.\\$\\$",
"1 4\n\\.%@$\n-.\\$\\$",
"1 7\n\\.%@$\n-.\\$\\$",
"2 4\n%.]@$\n..\\$\\$",
"2 3\n%.\\@$\n$]$\\..",
"1 4\n%.\\@$\... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Example
Input
2 4
%.@\$
..\$\$
Output
Yes
### Input:
2 4
%.@\$
..\$\$
### Output:
Yes
### Input:
2 4
%.\@$
..\$\$
### Output:
Yes
### Code:
import queue
di = [0,1,0,-1]
dj =... |
p02018 Surmise_23954 | D: Sontaku (Surmise)
Some twins like even numbers.
Count how many even numbers are in $ N $ integers $ A_1, A_2, A_3, \ dots, A_N $.
input
The integer $ N $ is given on the first line.
On the second line, $ N $ integers $ A_1, A_2, A_3, \ dots, A_N $ are given, separated by blanks.
output
Output an even number. ... | N = int(input())
A = [int(x)%2 for x in input().split()]
ans = N - sum(A)
print(ans)
| {
"input": [
"5\n4 3 5 2 6",
"5\n4 3 5 2 8",
"5\n4 3 1 2 9",
"5\n5 12 0 2 18",
"5\n2 12 0 2 18",
"5\n5 3 5 2 11",
"5\n1 47 1 5 9",
"5\n4 3 1 2 8",
"5\n4 6 1 2 9",
"5\n4 12 1 2 9",
"5\n5 12 1 2 9",
"5\n5 12 1 2 12",
"5\n5 12 1 2 18",
"5\n4 12 0 2 18",
"5\n4 3... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
D: Sontaku (Surmise)
Some twins like even numbers.
Count how many even numbers are in $ N $ integers $ A_1, A_2, A_3, \ dots, A_N $.
input
The integer $ N $ is given on the first ... |
p02301 Diameter of a Convex Polygon_23959 | Find the diameter of a convex polygon g. In other words, find a pair of points that have maximum distance between them.
Constraints
* 3 ≤ n ≤ 80000
* -100 ≤ xi, yi ≤ 100
* No point in the g will occur more than once.
Input
n
x1 y1
x2 y2
:
xn yn
The first integer n is the number of points in g.
In the following ... | #!/usr/bin/python3
import array
from fractions import Fraction
import functools
import itertools
import math
import os
import sys
def main():
N = read_int()
P = []
for _ in range(N):
x, y = [Fraction(s) for s in inp().split()]
P.append(Vec(x, y))
print(solve(N, P))
def lcm(a, b):
... | {
"input": [
"4\n0.0 0.0\n1.0 0.0\n1.0 1.0\n0.0 1.0",
"3\n0.0 0.0\n4.0 0.0\n2.0 2.0",
"4\n0.0 0.0\n1.0 0.7861454176159767\n1.0 1.0\n0.0 1.0",
"3\n0.006102823275073499 0.0\n4.0 0.0\n2.0 2.0",
"4\n0.0 0.0\n1.0 0.7861454176159767\n1.6165667240883559 1.0\n0.0 1.0",
"4\n0.0 0.5064552534171158\n1.0 ... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Find the diameter of a convex polygon g. In other words, find a pair of points that have maximum distance between them.
Constraints
* 3 ≤ n ≤ 80000
* -100 ≤ xi, yi ≤ 100
* No point ... |
p02448 Sorting Tuples_23962 | Write a program which reads $n$ items and sorts them. Each item has attributes $\\{value, weight, type, date, name\\}$ and they are represented by $\\{$ integer, integer, upper-case letter, integer, string $\\}$ respectively. Sort the items based on the following priorities.
1. first by value (ascending)
2. in case of... | # AOJ ITP2_5_B: Sorting Tuples
# Python3 2018.6.24 bal4u
tbl = []
n = int(input())
for i in range(n):
a = input().split()
tbl.append((int(a[0]), int(a[1]), a[2], int(a[3]), a[4]))
tbl.sort()
for i in tbl: print(*i)
| {
"input": [
"5\n105 24 C 1500000000000 white\n100 23 C 1500000000000 blue\n105 23 A 1480000000000 pink\n110 25 B 1500000000000 black\n110 20 A 1300000000000 gree",
"5\n105 24 C 1500000000000 white\n100 23 C 1500000000000 blue\n105 23 A 1480000000000 pink\n110 25 B 1500000000000 black\n110 20 A 1484487224415 ... | 6AIZU | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Write a program which reads $n$ items and sorts them. Each item has attributes $\\{value, weight, type, date, name\\}$ and they are represented by $\\{$ integer, integer, upper-case l... |
1005_C. Summarize to the Power of Two_23972 | A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative integer d).
For example, the following sequences are good:
* [5, 3, 11] (for example, for a_1=5 we can choose a_2=3. Note that their sum ... | from collections import*
input()
arr = Counter(map(int,input().split()))
binpow = [2**i for i in range(31)]
deleted = 0
for item in list(arr.elements()):
flag = True
for bp in binpow:
if (arr.get(bp - item) != None):
if (bp - item == item):
if (arr.get(bp - item) > 1):
... | {
"input": [
"6\n4 7 1 5 4 9\n",
"5\n1 2 3 4 5\n",
"1\n16\n",
"4\n1 1 1 1023\n",
"3\n1 2 3\n",
"3\n1 3 3\n",
"2\n2 2\n",
"2\n3 3\n",
"1\n1\n",
"2\n1 6\n",
"3\n1 3 2\n",
"3\n1 2 2\n",
"6\n1 7 7 7 7 7\n",
"2\n3 1\n",
"3\n1 1 3\n",
"3\n1 1 2\n",
"2\n2 1... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
A sequence a_1, a_2, ..., a_n is called good if, for each element a_i, there exists an element a_j (i ≠ j) such that a_i+a_j is a power of two (that is, 2^d for some non-negative inte... |
1029_A. Many Equal Substrings_23976 | You are given a string t consisting of n lowercase Latin letters and an integer number k.
Let's define a substring of some string s with indices from l to r as s[l ... r].
Your task is to construct such string s of minimum possible length that there are exactly k positions i such that s[i ... i + n - 1] = t. In other... | n, k = map(int, input().split())
s = input()
ans = None
for i in range(1, n):
prefix = s[0:n-i]
suffix = s[i:n]
if prefix == suffix:
ans = prefix
ans += s[n-i:n] * k
break
if ans is None:
ans = s * k
print (ans)
| {
"input": [
"3 2\ncat\n",
"3 4\naba\n",
"5 3\naaaaa\n",
"4 25\nlclc\n",
"4 5\nadaa\n",
"5 3\naaaba\n",
"11 3\nabracadabra\n",
"5 4\naaaaa\n",
"3 1\naba\n",
"4 5\nabaa\n",
"9 2\ncccababbc\n",
"7 2\nabcabca\n",
"4 4\naaaa\n",
"3 2\nabb\n",
"4 2\nxxxb\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You are given a string t consisting of n lowercase Latin letters and an integer number k.
Let's define a substring of some string s with indices from l to r as s[l ... r].
Your task... |
1071_A. Cram Time_23982 | In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely.
Lesha knows that today he can study for at most a hours, and he will have b hours to study tomorro... | def get_out(list):
print(len(list))
out=' '.join(list)
print(out)
arr = input().split()
a = int(arr[0])
b = int(arr[1])
s=a+b
temp=0
i=0
while(temp<=s):
i+=1
temp+=i
i-=1
list_a=[]
list_b=[]
for x in range(i,0,-1):
if(a-x>=0):
a-=x
list_a.append(str(x))
eli... | {
"input": [
"9 12\n",
"3 3\n",
"8 2\n",
"9 13\n",
"338744770 11863670\n",
"90 900\n",
"13 2\n",
"9 6\n",
"10 5\n",
"129149616 197519964\n",
"80678366 309992762\n",
"394605 111\n",
"0 15\n",
"8 7\n",
"660 6\n",
"8976394 2830976\n",
"79256157 81208498... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decide... |
1093_C. Mishka and the Last Exam_23986 | Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left.
There were n classes of that subject during the semester and on i-th class professor mentioned some non-negative inte... | n = int(input())
b = input().split()
b = [int(x) for x in b]
a = [0 for x in range(n)]
a[0] = 0
a[n - 1] = b[0]
for i in range(1, n//2):
left = i
right = n - i - 1
a[left] = 0
a[right] = b[i]
if a[right] > a[right + 1]:
diff = a[right] - a[right + 1]
a[right] -= diff
a[left]... | {
"input": [
"6\n2 1 2\n",
"4\n5 6\n",
"40\n195437 184828 171943 162996 150492 149962 136828 123282 118146 105706 94465 86963 79359 61479 55725 49170 36501 26335 18468 42\n",
"2\n0\n",
"16\n2 2 2 2 2 2 2 2\n",
"2\n7\n",
"20\n98 95 96 95 99 95 92 83 89 93\n",
"4\n5 6\n",
"200\n0 0 0... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that... |
1113_C. Sasha and a Bit of Relax_23990 | Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to relax unusually. During leisure time Sasha likes to upsolve unsolved problems because upsolving is very useful.
Therefore, Sasha decided ... | n = int(input())
a = [int(x) for x in input().split()]
pref = [0]
for i in range(n):
pref.append(pref[-1] ^ a[i])
dp = [[0 for i in range(2**20 + 5)] for j in range(2)]
ans = 0
for i in range(len(pref)):
ans += dp[i % 2][pref[i]]
dp[i % 2][pref[i]] += 1
#print(ans, pref[i])
print(ans)
| {
"input": [
"5\n1 2 3 4 5\n",
"6\n3 2 2 3 7 6\n",
"3\n42 4 2\n",
"2\n60202 951227\n",
"2\n32274 951227\n",
"5\n2 2 3 4 5\n",
"6\n3 2 2 5 7 6\n",
"5\n2 2 4 4 10\n",
"3\n48 4 2\n",
"2\n35508 951227\n",
"5\n2 2 3 4 8\n",
"6\n3 2 2 5 13 6\n",
"3\n3 4 2\n",
"2\n9498... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Sasha likes programming. Once, during a very long contest, Sasha decided that he was a bit tired and needed to relax. So he did. But since Sasha isn't an ordinary guy, he prefers to r... |
1141_F1. Same Sum Blocks (Easy)_23994 | This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of contiguous (consecutive) elements a[l], a[l+1], ..., a[r] (1 ≤ l ≤ r ≤ n). Thus, a block is defined by a pair of indices (l, r).
Find a... | import time
def index(key, item, index):
if key in index:
index[key].append(item)
else:
index[key] = [item]
def schedule2(times):
# print(times)
result = []
a_min = 0
for s, e in times:
if s >= a_min:
result.append((s, e))
a_min = e
# print(... | {
"input": [
"7\n4 1 2 2 1 5 3\n",
"4\n1 1 1 1\n",
"11\n-5 -4 -3 -2 -1 0 1 2 3 4 5\n",
"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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
This problem is given in two editions, which differ exclusively in the constraints on the number n.
You are given an array of integers a[1], a[2], ..., a[n]. A block is a sequence of... |
1182_E. Product Oriented Recurrence_24000 | Let f_{x} = c^{2x-6} ⋅ f_{x-1} ⋅ f_{x-2} ⋅ f_{x-3} for x ≥ 4.
You have given integers n, f_{1}, f_{2}, f_{3}, and c. Find f_{n} mod (10^{9}+7).
Input
The only line contains five integers n, f_{1}, f_{2}, f_{3}, and c (4 ≤ n ≤ 10^{18}, 1 ≤ f_{1}, f_{2}, f_{3}, c ≤ 10^{9}).
Output
Print f_{n} mod (10^{9} + 7).
Exam... | mod = 1000000007
X = [[1,1,1],
[1,0,0],
[0,1,0]]
def mul(A, B):
return [[sum(a*b for a,b in zip(X_row,Y_col)) % ( mod-1 ) for Y_col in zip(*B)] for X_row in A]
def pow_n(A, n):
if n == 1:
return A
A_ = pow_n(A, n // 2)
if n % 2 == 0:
return A_ * A_ % mod
... | {
"input": [
"5 1 2 5 3\n",
"17 97 41 37 11\n",
"665602877663246700 127718737 487095706 390504167 575630009\n",
"828401532544180275 298741808 401572465 730243936 632306255\n",
"82914 49871 44839 35650 66461\n",
"302350777208037330 536815215 180873643 161182477 807774336\n",
"10000000000000... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Let f_{x} = c^{2x-6} ⋅ f_{x-1} ⋅ f_{x-2} ⋅ f_{x-3} for x ≥ 4.
You have given integers n, f_{1}, f_{2}, f_{3}, and c. Find f_{n} mod (10^{9}+7).
Input
The only line contains five in... |
11_E. Forward, march!_24003 | Jack has become a soldier now. Unfortunately, he has trouble with the drill. Instead of marching beginning with the left foot and then changing legs with each step, as ordered, he keeps repeating a sequence of steps, in which he sometimes makes the wrong steps or — horror of horrors! — stops for a while. For example, i... | def solve():
original = input()
temp = [original[0]]
for i in range(1, len(original)):
if original[i] == original[i - 1] != 'X':
temp.append('X')
temp.append(original[i])
augmented = ''.join(temp)
answer = 0
if augmented[0] == augmented[-1] != 'X':
answer = ma... | {
"input": [
"LXRR\n",
"X\n",
"RXXRLRRLRR\n",
"RXRRXXXRXXRR\n",
"XRRLXRRRLRLXXLRRLRXL\n",
"RXLLRXXLLRLRLLXXXXRXXRLLRRRRXLLRRLXXLRXXLXLRLLLLLRXLRXLXRXLXLRRXXRRLXLXLRLRXRXLXXXLRXLLXLRRRRXRLLXRRLLRRXRRRLRLLRLRXXXRLXLXLLLRXXLLXLRXXLLLRLXRRXRLXLXXXXXXLXRXXRXXXRXLXXRRXLLXRRXLXXRRXLXXRLXLXXLXRXXRLRLL... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Jack has become a soldier now. Unfortunately, he has trouble with the drill. Instead of marching beginning with the left foot and then changing legs with each step, as ordered, he kee... |
1283_A. Minutes Before the New Year_24012 | New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows h hours and m minutes, where 0 ≤ hh < 24 and 0 ≤ mm < 60. We use 24-hour time format!
Your task is to find the number of minutes before the New Year. You know that New Year comes when the... | for _ in range(int(input())):
h,m = map(int,input().split())
print((24*60)-(h*60+m)) | {
"input": [
"5\n23 55\n23 0\n0 1\n4 20\n23 59\n",
"5\n23 55\n22 30\n21 10\n20 21\n19 59\n",
"1\n1 2\n",
"5\n23 55\n22 45\n21 10\n20 21\n19 59\n",
"1\n1 3\n",
"5\n23 55\n22 45\n21 2\n20 21\n19 59\n",
"1\n2 3\n",
"5\n23 55\n22 45\n21 2\n0 21\n19 59\n",
"1\n0 3\n",
"5\n23 14\n22 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows h hours and m minutes, where 0 ≤ hh < 24 and 0 ≤ mm... |
1326_F1. Wise Men (Easy Version)_24015 | This is the easy version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.
n wise men live in a beautiful city. Some of them know each other.
For each of the n! possible permutations p_1, p_2, …, p_n of the wise... | from sys import stdout
n = int(input())
class Person:
num = n - 1
def __init__(self, rel):
self.relationship = int(rel, 2)
def __getitem__(self, k):
return (self.relationship >> Person.num - k) & 1
rel = [Person(input()) for _ in range(n)]
dp = [[0] * n for _ in range(1 << n)]
for ... | {
"input": [
"4\n0101\n1000\n0001\n1010\n",
"3\n011\n101\n110\n",
"5\n01001\n10110\n01010\n01101\n10010\n",
"2\n00\n00\n",
"2\n01\n10\n",
"14\n00000000010010\n00011000101000\n00010000000001\n01101000000000\n01010101000000\n00001000000001\n00000000000000\n00001000000001\n01000000000010\n1000000... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
This is the easy version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved.
... |
1345_F. Résumé Review_24018 | Uh oh! Applications to tech companies are due soon, and you've been procrastinating by doing contests instead! (Let's pretend for now that it is actually possible to get a job in these uncertain times.)
You have completed many programming projects. In fact, there are exactly n types of programming projects, and you ha... | import sys
import heapq as hq
readline = sys.stdin.readline
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
eps = 10**-7
def solve():
n, k = nm()
a = nl()
ans = [0]*n
ok = 10**9; ng = -4*... | {
"input": [
"10 32\n1 2 3 4 5 5 5 5 5 5\n",
"5 8\n4 4 8 2 1\n",
"100 75533\n1535 204 1393 1665 561 1702 1961 35 1609 604 1884 242 1259 1302 710 814 17 1548 572 1627 1783 860 404 1041 1693 1160 152 1611 25 1926 1815 1561 1698 574 1268 110 1702 400 1893 2 709 123 805 481 1619 215 1330 986 986 188 735 1984 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Uh oh! Applications to tech companies are due soon, and you've been procrastinating by doing contests instead! (Let's pretend for now that it is actually possible to get a job in thes... |
1367_E. Necklace Assembly_24022 | The store sells n beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them.
A necklace is a set of beads connected in a circle.
For example, if the store sells beads "a", "b", "c", "a", "c", "c", then you can assemb... | from collections import Counter
from math import ceil, sqrt
class DisjSet:
def __init__(self, n):
# Constructor to create and
# initialize sets of n items
self.rank = [1] * n
self.parent = [i for i in range(n)]
self.extra = []
# Finds set of given item x
def find(s... | {
"input": [
"6\n6 3\nabcbac\n3 6\naaa\n7 1000\nabczgyo\n5 4\nababa\n20 10\naaebdbabdbbddaadaadc\n20 5\necbedececacbcbccbdec\n",
"1\n7 1782\nabcabca\n",
"1\n364 1485\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccc... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The store sells n beads. The color of each bead is described by a lowercase letter of the English alphabet ("a"–"z"). You want to buy some beads to assemble a necklace from them.
A n... |
1409_C. Yet Another Array Restoration_24028 | We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater than 0) integers.
* The array contains two elements x and y (these elements are known for you) such that x < y.
* If you sort the arr... | T = int(input())
for t in range(T):
n, x, y = [int(i) for i in input().split()]
found_flag = False
for d in range(1,51):
if (y-x) % d == 0:
for a in range(1, x+1):
if (x-a) % d == 0 and (y-a) % d == 0 and a + (n-1)* d >= y:
ans = [a+i*d for i in range(... | {
"input": [
"5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22\n",
"1\n2 1 49\n",
"5\n2 1 49\n5 20 50\n6 20 50\n5 3 8\n9 13 22\n",
"1\n3 1 49\n",
"5\n2 1 25\n5 20 50\n6 20 50\n5 3 8\n9 13 22\n",
"1\n3 1 59\n",
"5\n4 1 25\n5 20 50\n6 20 50\n5 3 8\n9 13 22\n",
"5\n4 1 25\n5 20 50\n6 20 50\n5 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:
* The array consists of n distinct positive (greater th... |
1430_A. Number of Apartments_24032 | Recently a new building with a new layout was constructed in Monocarp's hometown. According to this new layout, the building consists of three types of apartments: three-room, five-room, and seven-room apartments. It's also known that each room of each apartment has exactly one window. In other words, a three-room apar... | for _ in range(int(input())):
n = int(input())
if n%3==0:print(n//3,0,0)
elif n%5==0:print(0,n//5,0)
elif n%7==0:print(0,0,n//7)
elif n%3==1 and n>7:print((n-7)//3,0,1)
elif n%3==2 and n>7:print((n-5)//3,1,0)
else:print(-1) | {
"input": [
"4\n30\n67\n4\n14\n",
"1\n1\n",
"4\n30\n67\n4\n14\n",
"2\n556\n556\n",
"1\n2\n",
"4\n16\n67\n4\n14\n",
"2\n556\n806\n",
"4\n30\n67\n8\n14\n",
"1\n3\n",
"4\n16\n12\n4\n14\n",
"2\n556\n128\n",
"4\n30\n44\n8\n14\n",
"1\n5\n",
"4\n16\n20\n4\n14\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Recently a new building with a new layout was constructed in Monocarp's hometown. According to this new layout, the building consists of three types of apartments: three-room, five-ro... |
1453_C. Triangles_24036 | Gildong has a square board consisting of n rows and n columns of square cells, each consisting of a single digit (from 0 to 9). The cell at the j-th column of the i-th row can be represented as (i, j), and the length of the side of each cell is 1. Gildong likes big things, so for each digit d, he wants to find a triang... | import sys
input=lambda: sys.stdin.readline().rstrip("\r\n") #FOR READING STRING/TEXT INPUTS.
def oneLineArrayPrint(arr):
print(' '.join([str(x) for x in arr]))
def getAns(d): #return answer for digit d
ans=0
maxI=-1
minI=n
smallestJForEachI=[None for _ in range(n)]
largestJForEachI=[None for ... | {
"input": [
"5\n3\n000\n122\n001\n2\n57\n75\n4\n0123\n4012\n3401\n2340\n1\n9\n8\n42987101\n98289412\n38949562\n87599023\n92834718\n83917348\n19823743\n38947912\n",
"5\n3\n000\n122\n001\n2\n57\n75\n4\n0123\n4012\n3401\n2340\n1\n9\n8\n42987101\n98289412\n55677983\n87599023\n92834718\n83917348\n19823743\n389479... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Gildong has a square board consisting of n rows and n columns of square cells, each consisting of a single digit (from 0 to 9). The cell at the j-th column of the i-th row can be repr... |
1477_A. Nezzar and Board_24040 | n distinct integers x_1,x_2,…,x_n are written on the board. Nezzar can perform the following operation multiple times.
* Select two integers x,y (not necessarily distinct) on the board, and write down 2x-y. Note that you don't remove selected numbers.
Now, Nezzar wonders if it is possible to have his favorite nu... | import os
import sys
from io import BytesIO, IOBase
from math import gcd
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in ... | {
"input": [
"6\n2 1\n1 2\n3 0\n2 3 7\n2 -1\n31415926 27182818\n2 1000000000000000000\n1 1000000000000000000\n2 -1000000000000000000\n-1000000000000000000 123\n6 80\n-5 -20 13 -14 -2 -11\n",
"1\n2 0\n5 7\n",
"1\n2 -1\n5 7\n",
"6\n2 1\n1 2\n3 0\n2 3 7\n2 -1\n40263371 27182818\n2 1000000000000000000\n1 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
n distinct integers x_1,x_2,…,x_n are written on the board. Nezzar can perform the following operation multiple times.
* Select two integers x,y (not necessarily distinct) on the b... |
1503_E. 2-Coloring_24043 | There is a grid with n rows and m columns. Every cell of the grid should be colored either blue or yellow.
A coloring of the grid is called stupid if every row has exactly one segment of blue cells and every column has exactly one segment of yellow cells.
In other words, every row must have at least one blue cell, an... | M=998244353;N=4042
try:
import __pypy__
int_add=__pypy__.intop.int_add
int_sub=__pypy__.intop.int_sub
int_mul=__pypy__.intop.int_mul
def make_mod_mul(mod=M):
fmod_inv=1.0/mod
def mod_mul(a,b,c=0):
res=int_sub(
int_add(int_mul(a,b),c),
int_m... | {
"input": [
"2 2\n",
"4 3\n",
"2020 2021\n",
"4 4\n",
"1000 1000\n",
"10 19\n",
"3 3\n",
"8 8\n",
"2019 2018\n",
"4 1\n",
"3 1\n",
"1848 1937\n",
"2000 2000\n",
"2 1\n",
"4 2\n",
"2021 2\n",
"9 9\n",
"101 100\n",
"7 7\n",
"999 2013\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
There is a grid with n rows and m columns. Every cell of the grid should be colored either blue or yellow.
A coloring of the grid is called stupid if every row has exactly one segmen... |
1527_C. Sequence Pair Weight_24047 | The weight of a sequence is defined as the number of unordered pairs of indexes (i,j) (here i < j) with same value (a_{i} = a_{j}). For example, the weight of sequence a = [1, 1, 2, 2, 1] is 4. The set of unordered pairs of indexes with same value are (1, 2), (1, 5), (2, 5), and (3, 4).
You are given a sequence a of n... | def cal(n,ind_list):
ind1_list = [ind+1 for ind in ind_list]
red_list = [n-ind for ind in ind_list]
pre_sum = [0]
for ind1 in ind1_list:
pre_sum.append(pre_sum[-1]+ind1)
res = 0
for i in range(1,len(red_list)):
res += red_list[i]*pre_sum[i]
return res
def getRes... | {
"input": [
"2\n4\n1 2 1 1\n4\n1 2 3 4\n",
"2\n4\n1 2 2 1\n4\n1 2 3 4\n",
"2\n4\n2 2 2 1\n4\n1 2 3 4\n",
"2\n4\n2 2 1 1\n4\n1 2 3 4\n",
"2\n4\n2 2 2 1\n4\n1 3 3 4\n",
"2\n4\n1 2 2 1\n4\n1 4 3 4\n",
"2\n4\n2 2 4 1\n4\n1 2 3 4\n",
"2\n4\n2 2 2 2\n4\n1 3 3 4\n",
"2\n4\n1 2 1 1\n4\n2 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The weight of a sequence is defined as the number of unordered pairs of indexes (i,j) (here i < j) with same value (a_{i} = a_{j}). For example, the weight of sequence a = [1, 1, 2, 2... |
159_B. Matchmaker_24051 | Polycarpus has n markers and m marker caps. Each marker is described by two numbers: xi is the color and yi is the diameter. Correspondingly, each cap is described by two numbers: aj is the color and bj is the diameter. Cap (aj, bj) can close marker (xi, yi) only if their diameters match, that is, bj = yi. Besides, a m... | def main():
from sys import stdin
l = list(map(int, stdin.read().split()))
n, yx = l[0] * 2, [[0] * 1001 for _ in range(1001)]
cnt = yx[0]
u = v = 0
for y, x in zip(l[3:n + 3:2], l[2:n + 2:2]):
cnt[y] += 1
yx[y][x] += 1
ba, l = list(zip(l[n + 3::2], l[n + 2::2])), []
for ... | {
"input": [
"2 2\n1 2\n2 1\n3 4\n5 1\n",
"3 4\n1 2\n3 4\n2 4\n5 4\n2 4\n1 1\n1 2\n",
"1 1\n1 1\n1 1\n",
"1 1\n1 1\n2 2\n",
"6 7\n2 1\n1 1\n2 2\n1 2\n1 1\n1 2\n2 1\n1 1\n1 1\n1 1\n1 2\n2 2\n1 1\n",
"6 7\n1 1\n1 1\n1 2\n1 2\n2 2\n1 2\n2 1\n2 1\n2 1\n2 1\n1 1\n1 1\n1 2\n",
"6 7\n2 1\n2 2\n2 ... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Polycarpus has n markers and m marker caps. Each marker is described by two numbers: xi is the color and yi is the diameter. Correspondingly, each cap is described by two numbers: aj ... |
178_D3. Magic Squares_24054 | The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.
The magic square is a matrix of size n × n. The elements of this matrix are integers.... | import sys, random
def f(b):
global a
a = [[0] * n for o in range(n)]
for i in range(n):
for j in range(n):
a[i][j] = b[i * n + j]
rez = 0
for i in range(n):
ns = 0
for j in range(n):
ns += a[i][j]
rez += abs(su - ns)
for j in range(n)... | {
"input": [
"3\n1 2 3 4 5 6 7 8 9\n",
"3\n1 0 -1 0 2 -1 -2 0 1\n",
"2\n5 5 5 5\n",
"4\n-71868244 -24941524 -24941524 -24941524 -24941524 -24941524 -24941524 -6170836 21985196 21985196 21985196 21985196 40755884 40755884 40755884 68911916\n",
"4\n-7727968 -8509407 -9290846 -10072285 -10853724 -124... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to ... |
24_B. F1 Champions_24062 | Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive points in the following order 25, 18, 15, 12, 10, 8, 6, 4, 2, 1. At the conclusion of the championship the driver with most points is the cha... | points = [25, 18, 15, 12, 10, 8, 6, 4, 2, 1] + [0] * 40
numRaces = int(input())
scores = {}
for race in range(numRaces):
numDrivers = int(input())
for d in range(numDrivers):
driver = input()
if not driver in scores:
scores[driver] = [0] + [0] * 50 + [driver]
scores[driver][0] += points[d]
sc... | {
"input": [
"2\n7\nProst\nSurtees\nNakajima\nSchumacher\nButton\nDeLaRosa\nBuemi\n8\nAlonso\nProst\nNinoFarina\nJimClark\nDeLaRosa\nNakajima\nPatrese\nSurtees\n",
"3\n3\nHamilton\nVettel\nWebber\n2\nWebber\nVettel\n2\nHamilton\nVettel\n",
"2\n1\nA\n1\nA\n",
"20\n4\nb\nba\nab\na\n4\nba\nab\na\nb\n4\na... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive poin... |
320_C. Malek Dance Club_24071 | As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member o... | #!/usr/local/bin/python3.3 -tt
import sys
if __name__ == '__main__':
for l in sys.stdin:
s = l.strip()
break
n = len(s)
d = 2 ** (n - 1)
print(d * int(s, 2) % 1000000007)
| {
"input": [
"1\n",
"11\n",
"01\n",
"10001010011010010101101010111001001001011110110101011000010100110\n",
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\n",
"11110111000110101111100100111110000011\n",
"10000000011010101010111110010011... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidenta... |
391_A. Genetic Engineering_24077 | You will receive 3 points for solving this problem.
Manao is designing the genetic code for a new type of algae to efficiently produce fuel. Specifically, Manao is focusing on a stretch of DNA that encodes one protein. The stretch of DNA is represented by a string containing only the characters 'A', 'T', 'G' and 'C'.
... | class CodeforcesTask391ASolution:
def __init__(self):
self.result = ''
self.dna = ''
def read_input(self):
self.dna = input()
def process_task(self):
currc = ''
count = 0
inserts = 0
for c in self.dna:
if currc != c:
currc... | {
"input": [
"AACCAACCAAAAC\n",
"GTTAAAG\n",
"CCCTTCACCCGGATCCAAATCCCTTAGAAATAATCCCCGACGGCGTTGTATCACCTCTGCACTTGTTAGTAAGGTCAGGCGTCCATTACGGAAGAACGTA\n",
"AGCCGCCCCCCCAAAAAAGGGGGAAAAAAAAAAAAAAAAAAAAACTTTTGGAAACCCCCCCCTTTTTTTTTTTTTTTTTTTTTTTTTGGGGAAGGGGGGG\n",
"AAG\n",
"C\n",
"AAAAAAAAAAAAAAAA... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
You will receive 3 points for solving this problem.
Manao is designing the genetic code for a new type of algae to efficiently produce fuel. Specifically, Manao is focusing on a stre... |
412_E. E-mail Addresses_24081 | One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.
Today, the online news thundered with terrible information. The R1 database crashed and almost no data could be saved except for one big string. The developers assume ... | from sys import stdin, stdout, setrecursionlimit
input = stdin.readline
# setrecursionlimit(int(1e6))
inf = float('inf')
from functools import cmp_to_key
from collections import defaultdict as dd
from collections import Counter, deque
from heapq import *
import math
from math import floor, ceil, sqrt
def geti(): return... | {
"input": [
"gerald.agapov1991@gmail.com\n",
".asd123__..@\n",
"a___@1.r\n",
"x@x.x@x.x_e_@r1.com\n",
"@\n",
"_@0.z\n",
"0000000a@000a111.r\n",
"a@0.\n",
"@0.z\n",
"@1.r\n",
"0@0.x\n",
"a@r.z\n",
"a@0z\n",
"a@.z\n",
"a_0.z\n",
"a0.z\n",
"@.\n",
... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
One of the most important products of the R1 company is a popular @r1.com mail service. The R1 mailboxes receive and send millions of emails every day.
Today, the online news thunder... |
43_B. Letter_24085 | Vasya decided to write an anonymous letter cutting the letters out of a newspaper heading. He knows heading s1 and text s2 that he wants to send. Vasya can use every single heading letter no more than once. Vasya doesn't have to cut the spaces out of the heading — he just leaves some blank space to mark them. Help him;... | s1 = ''.join(input().split())
s2 = ''.join(input().split())
for i in s2:
if s2.count(i) > s1.count(i):
print("NO")
exit(0)
print("YES") | {
"input": [
"Instead of dogging your footsteps it disappears but you dont notice anything\nYour dog is upstears\n",
"abcdefg hijk\nk j i h g f e d c b a\n",
"Instead of dogging Your footsteps it disappears but you dont notice anything\nYour dog is upstears\n",
"Instead of dogging Your footsteps it di... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Vasya decided to write an anonymous letter cutting the letters out of a newspaper heading. He knows heading s1 and text s2 that he wants to send. Vasya can use every single heading le... |
485_C. Bits_24092 | Let's denote as <image> the number of bits set ('1' bits) in the binary representation of the non-negative integer x.
You are given multiple queries consisting of pairs of integers l and r. For each query, find the x, such that l ≤ x ≤ r, and <image> is maximum possible. If there are multiple such numbers find the sma... | for i in range(int(input())):
l,r=map(int,input().split())
L=list(bin(l))
R=list(bin(r))
L=L[2:]
R=R[2:]
w=0
c=0
L=['0']*(len(R)-len(L))+L
#print(L,R)
ans=0
if l==r:
print(l)
continue
for i in range(len(R)):
if L[i]!=R[i]:
for j in rang... | {
"input": [
"3\n1 2\n2 4\n1 10\n",
"17\n0 0\n0 8\n1 8\n36 39\n3 4\n3 7\n2 17\n8 12\n9 12\n10 12\n10 15\n6 14\n8 15\n9 15\n15 15\n100000000000000000 1000000000000000000\n99999999999999999 1000000000000000000\n",
"55\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Let's denote as <image> the number of bits set ('1' bits) in the binary representation of the non-negative integer x.
You are given multiple queries consisting of pairs of integers l... |
509_C. Sums of Digits_24096 | Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequence bi.
Vasya wonders what the numbers ai could be like. Of all the possibl... | def fill9(x,u):
k=x//9
for i in range(k):
u[i]=9
if x%9:
u[k]=x%9
return k+1
else:
return k
n=input()
n=int(n)
u=[0 for i in range(300//9*150+150)]
k=1
for i in range(n):
x=input()
x=int(x)
t=k-1
while t>=0:
if u[t]+9*t<x:
t=fill9(x,u)
... | {
"input": [
"3\n3\n2\n1\n",
"3\n1\n2\n3\n",
"1\n139\n",
"100\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representat... |
533_E. Correcting Mistakes_24100 | Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether t... | def aux(s, t):
n = len(s)
lpr = 0
for i in range(n):
if s[i] != t[i]:
break
lpr += 1
lsf = 0
for i in range(n-1, -1, -1):
if s[i] != t[i]:
break
lsf += 1
if(lpr == n):
return 2
return (s[lpr:n-lsf-1] == t[lpr+1:n-lsf]) + (t[l... | {
"input": [
"7\nreading\ntrading\n",
"5\nsweet\nsheep\n",
"3\ntoy\ntry\n",
"4\nabcc\nbcab\n",
"4\nacca\nbabb\n",
"4\naabc\naacb\n",
"100\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabbbbbabbaaabbbbbbbbbbbbbbbababaaababbaaabeabbabaabbab\naaabaabbbababbbaabbbbbaaababbabbaaabbabaabbabbabb... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typin... |
558_D. Guess Your Way Out! II_24104 | Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the nodes of the tree such tha... | h,q=map(int,input().split())
d=[(2**h,0),(2**(h-1),0)]
for _ in range(q):
i,l,r,a=map(int,input().split())
l,r=l*2**(h-i),(r+1)*2**(h-i)
d.extend([[(l,1),(r,-1)],[(0,1),(l,-1),(r,1)]][a])
s=0
l=0
d=sorted(d)
for (a,x),(b,_) in zip(d,d[1:]):
s+=x
if a!=b and s==0:q=a;l+=b-a
print(("Game cheated!",q,"... | {
"input": [
"4 3\n4 10 14 1\n3 6 6 0\n2 3 3 1\n",
"4 2\n3 4 5 1\n2 3 3 1\n",
"4 2\n3 4 6 1\n4 12 15 1\n",
"3 1\n3 4 6 0\n",
"1 1\n1 1 1 0\n",
"1 0\n",
"4 3\n4 10 14 1\n3 7 6 0\n2 3 3 1\n",
"4 2\n3 4 5 1\n2 3 6 1\n",
"1 1\n1 1 1 1\n",
"4 3\n4 10 26 1\n3 7 6 0\n2 3 3 1\n",
"... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Amr bought a new video game "Guess Your Way Out! II". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height h. The player is initially ... |
584_B. Kolya and Tanya _24108 | Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.
More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the ci... | n = int(input())
print((27 ** n - 7 ** n) % (10 ** 9 + 7))
| {
"input": [
"2\n",
"1\n",
"68841\n",
"5984\n",
"10\n",
"74\n",
"15\n",
"22\n",
"10868\n",
"2091\n",
"58000\n",
"9877\n",
"9\n",
"4\n",
"6\n",
"10000\n",
"866\n",
"8\n",
"14\n",
"4343\n",
"20\n",
"3\n",
"18569\n",
"2441\n"... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.
More formally, ... |
606_B. Testing Robots_24112 | The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning of each test the robot prototype will be placed in cell (x0, y0) of a rectangular squared field of size x × y, after that a mine will be i... | q,b,c,d = map(int,input().split())
s = input()
x,y =c,d
l = 1
print(1,end = ' ')
a = set()
a.add((c,d))
for i in range(len(s)-1):
item = s[i]
if (item == 'U'):
if(x == 1 or (x-1,y) in a):
print(0,end = ' ')
if(x-1,y) in a:
x-=1
else:
print(1,en... | {
"input": [
"2 2 2 2\nULD\n",
"3 4 2 2\nUURDRDRL\n",
"4 4 2 2\nDRUL\n",
"1 2 1 2\nLR\n",
"1 1 1 1\nURDLUURRDDLLURDL\n",
"15 17 8 9\nURRDLUULLDDDRRUR\n",
"2 1 1 1\nD\n",
"15 17 8 9\nURRDLUULLDDRR\n",
"15 17 8 9\nURRDLU\n",
"15 17 8 9\nURRDLUULLDDR\n",
"15 17 8 9\nURRDLUULLD... | 2CODEFORCES | Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem.
### Description:
The Cybernetics Failures (CF) organisation made a prototype of a bomb technician robot. To find the possible problems it was decided to carry out a series of tests. At the beginning o... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.