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
1194_D. 1-2-K Game_25517
Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one). Players take turns, Alice is first. Each player during his or her turn has to move the chip 1, 2 or k cells to the left (so, if the ch...
from sys import stdin, stdout from collections import Counter def rsingle_int(): return int(stdin.readline().rstrip()) def rmult_int(): return [ int(x) for x in stdin.readline().rstrip().split() ] def rmult_str(): return stdin.readline().rstrip().split() def r_str(): return stdin.readline().rstri...
{ "input": [ "4\n0 3\n3 3\n3 4\n4 4\n", "1\n25 6\n", "1\n73 18\n", "1\n24 6\n", "24\n19 9\n20 9\n21 9\n22 9\n23 9\n24 9\n25 9\n26 9\n27 9\n28 9\n29 9\n30 9\n31 9\n32 9\n1000000000 999999999\n999999996 999999999\n999999997 999999999\n1000000000 6\n1000000000 3\n1000000000 9\n999999999 6\n999999996 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice and Bob play a game. There is a paper strip which is divided into n + 1 cells numbered from left to right starting from 0. There is a chip placed in the n-th cell (the last one)...
1276_C. Beautiful Rectangle_25522
You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the n numbers may not be chosen. A rectangle (rectangular matrix)...
n = int(input()) arr = list(map(int, input().split())) d = {} for i in arr: d[i] = d.get(i, 0) + 1 d2 = {} for k, v in d.items(): d2.setdefault(v, []).append(k) s = n prev = 0 ansp = ansq = anss = 0 for p in range(n, 0, -1): q = s // p if p <= q and q * p > anss: anss = q * p ansq = q ...
{ "input": [ "12\n3 1 4 1 5 9 2 6 5 3 5 8\n", "5\n1 1 1 1 1\n", "15\n17 5 20 11 14 9 5 12 5 11 5 14 1 12 18\n", "7\n2 1 3 3 2 3 3\n", "4\n1 2 3 3\n", "11\n4 4 5 5 5 3 5 5 5 3 2\n", "5\n1 5 5 4 2\n", "6\n7 8 6 8 9 8\n", "15\n6 7 11 8 13 11 4 20 17 12 9 15 18 13 9\n", "11\n18 16 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given n integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle c...
1296_D. Fight with Monsters_25526
There are n monsters standing in a row numbered from 1 to n. The i-th monster has h_i health points (hp). You have your attack power equal to a hp and your opponent has his attack power equal to b hp. You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it...
n,a,b,k= map(int,input().split()) l=[int(x) for x in input().split()] power=[] for x in l: rem=x%(a+b) if rem==0: if b%a!=0: power.append((b//a)+1) else: power.append((b//a)) else: if rem<=a: power.append(0) else: if (rem-a)%a==0: power.append(((rem-a)//a)) else: power.append(((r...
{ "input": [ "7 4 2 1\n1 3 5 4 2 7 6\n", "6 2 3 3\n7 10 50 12 1 8\n", "1 1 100 99\n100\n", "2 1 49 2\n50 50\n", "2 1 100 2\n1 101\n", "2 2 49 2\n50 50\n", "7 4 2 1\n1 3 4 4 2 7 6\n", "6 2 3 2\n7 10 50 12 1 8\n", "2 1 100 2\n2 101\n", "7 4 2 1\n1 1 5 4 2 7 6\n", "6 2 3 3\n7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n monsters standing in a row numbered from 1 to n. The i-th monster has h_i health points (hp). You have your attack power equal to a hp and your opponent has his attack pow...
131_D. Subway_25530
A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used to mo...
import sys from collections import deque sys.setrecursionlimit(30000) sz = 3333 cyclic = 0 begin = 0 end = 0 cycle = [] graph = [] used = [] parent = [] dist = [] def dfs(current,prev): global graph,begin,end,used,parent used[current] = 1 for nxt in graph[current]: if used[nxt] != 2 and nxt != prev...
{ "input": [ "6\n1 2\n3 4\n6 4\n2 3\n1 3\n3 5\n", "4\n1 3\n4 3\n4 2\n1 2\n", "4\n4 3\n1 3\n2 1\n2 3\n", "5\n4 1\n1 3\n5 3\n2 4\n5 2\n", "8\n1 2\n2 3\n3 1\n3 4\n4 5\n5 6\n6 7\n6 8\n", "10\n10 7\n1 5\n2 1\n4 1\n1 10\n3 1\n9 7\n1 8\n7 6\n7 1\n", "10\n8 2\n10 5\n9 5\n3 9\n4 7\n3 1\n7 2\n10 6\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A subway scheme, classic for all Berland cities is represented by a set of n stations connected by n passages, each of which connects exactly two stations and does not pass through an...
1339_A. Filling Diamonds_25534
You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. 2 coverings are different if some 2 triangles are covered by the same diamond shape in ...
n = int(input()) for _ in range(n): a = input() print(a)
{ "input": [ "2\n2\n1\n", "1\n1000000000\n", "1\n0000000000\n", "2\n2\n2\n", "1\n0010000000\n", "2\n2\n4\n", "1\n0001000000\n", "2\n4\n4\n", "1\n0001000001\n", "2\n4\n1\n", "1\n0001000011\n", "2\n3\n1\n", "1\n0001001011\n", "2\n3\n2\n", "1\n0001011011\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have integer n. Calculate how many ways are there to fully cover belt-like area of 4n-2 triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rot...
1360_E. Polygon_25538
Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held. The soldiers placed a cannon above each cell in the first row and a cannon to the left of each cell in the first column. Thus, exactly 2...
def solve(): n = int(input()) polygon = [None for i in range(n)] for i in range(n): polygon[i] = list(map(int, input())) for i in range(n - 2, -1, -1): for j in range(i, -1, -1): if polygon[i][j] == 1: if polygon[i][j + 1] == 0 and polygon[i + 1][j] == 0: ...
{ "input": [ "5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0111\n", "5\n4\n0010\n0011\n0000\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\n0111\n4\n0100\n1110\n0101\n0110\n", "5\n4\n0010\n0011\n1001\n0000\n2\n10\n01\n2\n00\n00\n4\n0101\n1111\n0101\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polygon is not only the best platform for developing problems but also a square matrix with side n, initially filled with the character 0. On the polygon, military training was held....
1380_F. Strange Addition_25542
Let a and b be some non-negative integers. Let's define strange addition of a and b as following: 1. write down the numbers one under another and align them by their least significant digit; 2. add them up digit by digit and concatenate the respective sums together. Assume that both numbers have an infinite n...
from sys import stdin import sys class LazySegTree: def __init__(self,N,first): self.LV = (N-1).bit_length() self.NO = 2**self.LV self.First = first self.data = [first] * (2*self.NO) self.lazy = [None] * (2*self.NO) def calc(self,l,r): return min(l,r)...
{ "input": [ "2 3\n14\n2 4\n2 1\n1 0\n", "3 1\n139\n2 1\n", "2 3\n14\n2 4\n2 1\n1 1\n", "2 3\n11\n2 4\n2 1\n1 0\n", "2 3\n11\n1 4\n2 1\n1 0\n", "2 3\n14\n2 4\n2 1\n2 0\n", "2 3\n11\n1 4\n2 2\n1 0\n", "2 3\n11\n1 5\n2 2\n1 0\n", "3 1\n139\n3 1\n", "2 3\n11\n1 5\n2 4\n1 0\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let a and b be some non-negative integers. Let's define strange addition of a and b as following: 1. write down the numbers one under another and align them by their least signific...
1424_F. Coins_25547
A famous gang of pirates, Sea Dogs, has come back to their hideout from one of their extravagant plunders. They want to split their treasure fairly amongst themselves, that is why You, their trusted financial advisor, devised a game to help them: All of them take a sit at their round table, some of them with the golde...
import os import sys from io import BytesIO, IOBase def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode s...
{ "input": [ "4 2\n1 2\n2 2\n", "6 2\n2 3\n4 1\n", "3 2\n1 1\n2 2\n", "25 2\n1 23\n3 1\n", "1000000000 3\n30490 19232\n45250 999980767\n264372930 1\n", "999999999 1\n999999999 999999999\n", "1000000000 1\n1 1000000000\n", "1 1\n1 1\n", "1001 1\n60 10001\n", "999999999 1\n100 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A famous gang of pirates, Sea Dogs, has come back to their hideout from one of their extravagant plunders. They want to split their treasure fairly amongst themselves, that is why You...
1445_B. Elimination_25551
There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests. A result of the elimination stage is the tota...
for k in range(int(input())): a, b, c, d = input().split() a, b, c, d = int(a), int(b), int(c), int(d) if a+b >= c+d: print(a+b) else: print(c + d)
{ "input": [ "2\n1 2 2 1\n4 8 9 2\n", "1\n0 0 0 0\n", "1\n0 0 1 0\n", "2\n1 2 2 1\n4 8 9 4\n", "2\n1 2 2 1\n4 8 12 4\n", "1\n0 0 2 0\n", "1\n0 0 1 -1\n", "2\n1 2 3 1\n4 8 12 4\n", "1\n4 0 0 0\n", "1\n1 -1 3 0\n", "1\n7 0 0 -2\n", "1\n7 -1 1 -2\n", "1\n0 -4 7 -2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participant...
146_B. Lucky Mask_25555
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya calls a mask of a positive integer n the number that is obtained after successive writing of...
s=input() a,b=(s.split()[0]),(s.split()[1]) dif=len(a)-len(b) mask='' tmp=int(a) tmp+=1 t=str(tmp) while(1): for i in t: if(i in ['4','7']): mask+=i if(mask==b): break else: tmp+=1 t=str(tmp) mask='' print(tmp)
{ "input": [ "1 7\n", "100 47\n", "39999 4774\n", "40007 74\n", "31975 74\n", "69700 77477\n", "74 77\n", "45679 77777\n", "1 4\n", "40007 74444\n", "740 4\n", "369 47\n", "47 74\n", "4 4\n", "45896 4\n", "76492 447\n", "55557 74\n", "99997 47\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744...
1495_D. BFS Trees_25558
We define a spanning tree of a graph to be a BFS tree rooted at vertex s if and only if for every node t the shortest distance between s and t in the graph is equal to the shortest distance between s and t in the spanning tree. Given a graph, we define f(x,y) to be the number of spanning trees of that graph that are ...
import sys, os from collections import defaultdict, deque if os.environ['USERNAME']=='kissz': inp=open('in2.txt','r').readline def debug(*args): print(*args,file=sys.stderr) else: inp=sys.stdin.readline def debug(*args): pass # SCRIPT STARTS HERE def solve(): n,m=map(int,inp()....
{ "input": [ "8 9\n1 2\n1 3\n1 4\n2 7\n3 5\n3 6\n4 8\n2 3\n3 4\n", "4 4\n1 2\n2 3\n3 4\n1 4\n", "6 7\n1 2\n1 3\n2 4\n3 4\n3 5\n4 6\n5 6\n", "5 6\n1 2\n1 4\n2 3\n3 4\n2 5\n3 5\n", "1 0\n", "8 9\n1 2\n1 3\n2 4\n2 7\n3 5\n3 6\n4 8\n2 3\n3 4\n", "4 4\n1 2\n1 3\n3 4\n1 4\n", "6 7\n1 2\n1 3\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We define a spanning tree of a graph to be a BFS tree rooted at vertex s if and only if for every node t the shortest distance between s and t in the graph is equal to the shortest di...
1519_A. Red and Blue Beans_25562
You have r red and b blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: * has at least one red bean (or the number of red beans r_i ≥ 1); * has at least one blue bean (or the number of blue beans b_i ≥ 1); * the number of red and blue beans should diff...
def solve(): r, b, d = map(int, input().split(' ')) packets = min(r, b) ma = max(r, b) if d == 0: if r == b: return "YES" return "NO" needed = -1 if ma % packets == 0: needed = ma // packets else: needed = ma // packets + 1 if (needed - 1)...
{ "input": [ "4\n1 1 0\n2 7 3\n6 1 4\n5 4 0\n", "1\n2 1 1\n", "10\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1000000000\n1000000000 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have r red and b blue beans. You'd like to distribute them among several (maybe, one) packets in such a way that each packet: * has at least one red bean (or the number of red...
1546_A. AquaMoon and Two Arrays_25566
AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays a and b, both consist of n non-negative integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero): * She chooses two indices i and j (1 ≤ i, j ≤ n), then decreases the i-th element ...
cases=int(input()) for idx in range(cases): n=int(input()) a=list(map(int, input().split())) b=list(map(int, input().split())) if sum(a)!=sum(b): print(-1) elif a==b: print(0) else: tuples=[] count=0 for i in range(n): if a[i]==b[i]: continue if a[...
{ "input": [ "4\n4\n1 2 3 4\n3 1 2 4\n2\n1 3\n2 1\n1\n0\n0\n5\n4 3 2 1 0\n0 1 2 3 4\n", "2\n2\n1 1\n3 3\n4\n1 2 3 10\n9 6 7 50\n", "3\n33\n1 0 3 0 6 5 0 5 5 3 0 1 0 0 0 0 2 3 0 0 2 1 0 0 2 1 0 3 0 3 0 5 1\n2 6 5 1 0 6 0 0 4 0 0 0 2 0 0 2 6 6 0 1 0 0 0 0 0 0 1 0 0 5 1 3 0\n33\n6 5 3 0 3 3 0 2 1 0 0 0 0 0 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays a and b, both consist of n non-negative integers. AquaMoon can perform the following oper...
173_B. Chamber of Secrets_25570
"The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anyb...
import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.wri...
{ "input": [ "3 3\n.#.\n...\n.#.\n", "4 3\n##.\n...\n.#.\n.#.\n", "5 5\n.....\n.#...\n.....\n.....\n#.###\n", "3 4\n..##\n....\n..#.\n", "2 42\n..........#.......#..........###........#.\n.....#######.......#..#....#...##.........\n", "6 8\n...##...\n.#.#.#.#\n#.#.#.#.\n.#.#.#.#\n#.#.#.#.\n#.....
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: "The Chamber of Secrets has been opened again" — this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired...
193_C. Hamming Distance_25573
Hamming distance between strings a and b of equal length (denoted by h(a, b)) is equal to the number of distinct integers i (1 ≤ i ≤ |a|), such that ai ≠ bi, where ai is the i-th symbol of string a, bi is the i-th symbol of string b. For example, the Hamming distance between strings "aba" and "bba" equals 1, they have ...
def get_input(): a, b, d = map(int, input().split()) c, e = map(int, input().split()) f = int(input()) return [a, b, c, d, e, f] def check_condition(a, b, c, d, e, f): condition1 = (a + b + c) % 2 == 0 condition2 = (d + e + a) % 2 == 0 condition3 = (e + f + c) % 2 == 0 condi...
{ "input": [ "4 4 4\n4 4\n4\n", "1 2 3\n1 2\n3\n", "3 6 4\n3 5\n6\n", "60218 34235 60087\n62830 60263\n83853\n", "5 3 5\n6 4\n2\n", "5 6 6\n6 5\n5\n", "10 10 8\n8 8\n10\n", "6 6 6\n6 6\n6\n", "99828 54425 67603\n60232 60026\n59994\n", "1 0 1\n0 1\n0\n", "79085 19303 48758\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Hamming distance between strings a and b of equal length (denoted by h(a, b)) is equal to the number of distinct integers i (1 ≤ i ≤ |a|), such that ai ≠ bi, where ai is the i-th symb...
266_A. Stones on the Table_25582
There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had different colors. Stones in a row are considered neighboring if there are no other stones between them. Input The first line contains integ...
n=int(input()) word=input() count=0 for i in range(0,n-1): if word[i]==word[i+1]: count+=1 print(count)
{ "input": [ "5\nRRRRR\n", "4\nBRBG\n", "3\nRRG\n", "20\nRRGBBRBRGRGBBGGRGRRR\n", "50\nRBGBGGRRGGRGGBGBGRRBGGBGBRRBBGBBGBBBGBBRBBRBRBRGRG\n", "25\nBBGBGRBGGBRRBGRRBGGBBRBRB\n", "50\nRBGGBGGRBGRBBBGBBGRBBBGGGRBBBGBBBGRGGBGGBRBGBGRRGG\n", "50\nGRBGGRBRGRBGGBBBBBGGGBBBBRBRGBRRBRGBBBRBBRRG...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n stones on the table in a row, each of them can be red, green or blue. Count the minimum number of stones to take from the table so that any two neighboring stones had diff...
28_A. Bender Problem_25586
Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a closed polyline, which vertices should be nails (in the given order). The segments of the polyline should be parallel to the coordinate ...
from collections import defaultdict def main(): n, m = map(int, input().split()) tmp = list(tuple(map(int, input().split())) for _ in range(n)) nails = [abs(a - c) + abs(b - d) for (a, b), (c, d) in zip(tmp, tmp[2:] + tmp[:2])] segments = defaultdict(list) for i, s in enumerate(map(int, input().sp...
{ "input": [ "4 2\n0 0\n0 2\n2 2\n2 0\n4 4\n", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n2 2 3\n", "6 3\n0 0\n1 0\n1 1\n2 1\n2 2\n0 2\n3 2 3\n", "4 4\n0 0\n0 1\n1 1\n1 0\n1 1 1 1\n", "4 2\n0 0\n0 2\n2 2\n2 0\n200000 200000\n", "4 4\n1679 -198\n9204 -198\n9204 -5824\n1679 -5824\n18297 92466 187436 17...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Robot Bender decided to make Fray a birthday present. He drove n nails and numbered them from 1 to n in some order. Bender decided to make a picture using metal rods. The picture is a...
315_A. Sereja and Bottles_25590
Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles. Sereja knows that the i-th bottle is from brand ai, besides, you can use it to open other bottles of brand bi. You can use one bottle ...
n = int(input()) a = [] b = [] for i in range(n): ap, bp = map(int, input().split()) a.append(ap) b.append(bp) ok = [False] * n for i in range(n): for j in range(n): if b[i] == a[j] and i != j: ok[j] = True print(ok.count(False))
{ "input": [ "4\n1 2\n2 3\n3 4\n4 1\n", "4\n1 1\n2 2\n3 3\n4 4\n", "3\n1 2\n1 2\n1 1\n", "4\n2 3\n1 772\n3 870\n3 668\n", "6\n4 843\n2 107\n10 943\n9 649\n7 806\n6 730\n", "3\n2 828\n4 392\n4 903\n", "49\n1 758\n5 3\n5 3\n4 2\n4 36\n3 843\n5 107\n1 943\n1 649\n2 806\n3 730\n2 351\n2 102\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja and his friends went to a picnic. The guys had n soda bottles just for it. Sereja forgot the bottle opener as usual, so the guys had to come up with another way to open bottles...
337_C. Quiz_25594
Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly,...
import os import sys from io import BytesIO, IOBase import math def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mo...
{ "input": [ "5 3 2\n", "5 4 2\n", "87413058 85571952 12\n", "1000000000 999998304 7355\n", "23888888 16789012 2\n", "23888888 19928497 812\n", "901024556 900000000 6\n", "300000000 300000000 299999999\n", "10 8 3\n", "2 2 2\n", "1000000000 999999904 225255\n", "1000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Manao is taking part in a quiz. The quiz consists of n consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answer...
383_C. Propagating tree_25599
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ai. The root of the tree is node 1. This tree has a special property: when a value val is added to a value of node i, the value -val is ...
class BIT(): """区間加算、一点取得クエリをそれぞれO(logN)で応えるデータ構造を構築する add: 区間[begin, end)にvalを加える get_val: i番目(0-indexed)の値を求める """ def __init__(self, n): self.n = n self.bit = [0] * (n + 1) def get_val(self, i): i = i + 1 s = 0 while i <= self.n: s += self....
{ "input": [ "5 5\n1 2 1 1 2\n1 2\n1 3\n2 4\n2 5\n1 2 3\n1 1 2\n2 1\n2 2\n2 4\n", "10 10\n418 45 865 869 745 901 177 773 854 462\n4 8\n1 4\n3 6\n1 5\n1 10\n5 9\n1 2\n4 7\n1 3\n2 2\n1 6 246\n1 4 296\n1 2 378\n1 8 648\n2 6\n1 5 288\n1 6 981\n1 2 868\n2 7\n", "10 10\n137 197 856 768 825 894 86 174 218 326\n7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numbered from 1 to n, each node i having an initial value ...
404_B. Marathon_25603
Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0, 0) and the length of the side equals a meters. The sides of the square are parallel to coordinate axes. As the length of t...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys def main(): a, d =map(float, input().split(' ')) ans = [] for i in range(1, int(input()) + 1): cur_round_pos = d * i % (a * 4) if cur_round_pos <= a: y = 0 x = cur_round_pos elif cur_round_pos <= a ...
{ "input": [ "4.147 2.8819\n6\n", "2 5\n2\n", "40356.3702 72886.7142\n100\n", "70092.4982 95833.7741\n1000\n", "52372.0072 97869.2372\n100\n", "16904.8597 21646.2846\n10\n", "22635.8777 74922.1758\n10\n", "5681.4396 74931.1355\n10\n", "35417.5676 97878.1954\n100\n", "5037.7799 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with co...
431_C. k-Tree_25607
Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree. A k-tree is an infinite rooted tree where: * each vertex has exactly k children; * each edge has some weight; * if we look at the edges that goes ...
n,k,d = input().split() n,k,d = int(n),int(k),int(d) ans1 = [0 for _ in range(101)] ans1[0] = 1 ans = [0 for _ in range(101)] for i in range(d): ans[i] = 0 for i in range(0,n + 1): j = 1 while j <= k and i - j >= 0: ans1[i] += ans1[i - j] j += 1 for i in range(d,n + 1): j = ...
{ "input": [ "4 5 2\n", "3 3 2\n", "3 3 3\n", "4 3 2\n", "10 13 6\n", "90 97 24\n", "28 74 2\n", "8 11 4\n", "9 17 14\n", "98 98 64\n", "86 69 62\n", "40 77 77\n", "31 8 8\n", "100 100 100\n", "2 100 1\n", "78 90 38\n", "52 46 4\n", "3 2 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree. A k-tree is an inf...
476_C. Dreamoon and Sums_25613
Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if <image> and <image>, where k is some integer number in range [1, a]. By <image> we denote the quotient of integer division of...
a, b = (input()).split(' ') a = int(a) b=int(b) result = a*b*(b-1)*(a*b+b+2)//4 #result = int(result) result = result % ( 10**9 +7) print((result))
{ "input": [ "2 2\n", "1 1\n", "1000 1000\n", "3 10000000\n", "9253578 1799941\n", "666666 666666\n", "3505377 9167664\n", "7319903 9017051\n", "191919 123123\n", "123456 123456\n", "6407688 3000816\n", "2 9999999\n", "4108931 211273\n", "9900111 1082917\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is call...
49_D. Game_25617
Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1 × n square and paints the squares black and white. After that Petya can start moves — during a move he may choose any two neighboring squares of one color and repaint these two squares any way he wants, perhaps in different colors. Petya can...
n=int(input()) def hamming(a,b): global n ret=0 for i in range(n): ret+=int(a[i]!=b[i]) return ret s=input() a=['0' if q%2==0 else '1' for q in range(n)] b=['0' if q%2==1 else '1' for q in range(n)] print(min(hamming(s,a),hamming(s,b)))
{ "input": [ "5\n00100\n", "6\n111010\n", "5\n10001\n", "7\n1100010\n", "18\n110100000000000000\n", "70\n0010011001010100000110011001011111101011010110110101110101111011101010\n", "7\n0000000\n", "17\n00100000000000000\n", "16\n1101010010000000\n", "3\n111\n", "4\n0000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya and Petya have invented a new game. Vasya takes a stripe consisting of 1 × n square and paints the squares black and white. After that Petya can start moves — during a move he m...
524_D. Social Network_25620
Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was provided with information on all user requests for this time period. For each query, we know its time... and nothing else, because Polycarp...
def main(): mode="filee" if mode=="file":f=open("test.txt","r") get = lambda :[int(x) for x in (f.readline() if mode=="file" else input()).split()] gets = lambda :[str(x) for x in (f.readline()[:-1] if mode=="file" else input()).split(":")] [n,m,t]=get() [a,b]=[[0]*20002,[0]*20002] if n<m: ...
{ "input": [ "4 2 10\n17:05:53\n17:05:58\n17:06:01\n22:39:47\n", "1 2 86400\n00:00:00\n", "5 2 40000\n06:30:57\n07:27:25\n09:10:21\n11:05:03\n12:42:37\n", "1 1 86400\n00:00:00\n", "7 4 30000\n05:08:54\n05:35:53\n06:03:20\n06:17:50\n09:29:46\n11:35:29\n14:49:02\n", "10 3 30000\n00:06:54\n00:42:...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarpus got an internship in one well-known social network. His test task is to count the number of unique users who have visited a social network during the day. Polycarpus was pr...
599_C. Day at the Beach_25628
One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their time building sand castles. At the end of the day there were n castles built by friends. Castles are numbered from 1 to n, and the heig...
n = int(input()) hs = list(map(int, input().split())) max_h = 0 rs = [] max_hs = [0] * n for i, h in enumerate(hs): rs.append((h, i)) max_h = max(max_h, h) max_hs[i] = max_h rs.sort() p, r = 0, -1 ans = 0 while r < n - 1: nh, nr = rs[p] if r >= nr: p += 1 else: r = nr p +...
{ "input": [ "3\n1 2 3\n", "4\n2 1 3 2\n", "20\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\n", "25\n1 2 3 4 4 4 4 4 4 4 2 3 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day Squidward, Spongebob and Patrick decided to go to the beach. Unfortunately, the weather was bad, so the friends were unable to ride waves. However, they decided to spent their...
620_B. Grandfather Dovlet’s calculator_25632
Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (<https://en.wikipedia.org/wiki/Seven-segment_display>). <image> Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find t...
#!/usr/bin/env python3 if __name__ == '__main__': a, b = map(int, input().split()) act = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6] r = 0 for i in range(a, b+1): r += sum(act[ord(c)-ord('0')] for c in str(i)) print(r)
{ "input": [ "10 15\n", "1 3\n", "222145 353252\n", "1 100\n", "192 200\n", "213 221442\n", "100 10000\n", "1000000 1000000\n", "1 1000000\n", "1 999999\n", "2 1000000\n", "111 200\n", "371 221442\n", "100 11000\n", "15 15\n", "2 3\n", "110 200\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (<https://en.wikipedia.org/wiki/Sev...
641_A. Little Artem and Grasshopper_25636
Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him. The area looks like a strip of cells 1 × n. Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grassh...
n = int(input()) s = input() arr = list(map(int, input().split())) seen = set() current_index = 0 while True: command = s[current_index] step = arr[current_index] if command == ">": current_index += step else: current_index -= step # print(current_index) if current_index > n-...
{ "input": [ "3\n&gt;&gt;&lt;\n2 1 1\n", "2\n&gt;&lt;\n1 2\n", "5\n><>>>\n1 1 10 10 10\n", "3\n><<\n2 3 2\n", "3\n><<\n2 100 2\n", "1\n>\n1000000000\n", "2\n>>\n1 1\n", "4\n>>><\n3 2 2 3\n", "5\n>><><\n1 2 2 1 2\n", "3\n><>\n1 1 1\n", "4\n>><<\n2 100 2 100\n", "3\n><<\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him. The area looks like a strip of cells 1 × n. Each cell contains the direction for ...
690_E1. Photographs (I)_25643
The Human-Cow Confederation (HC2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of...
a = '''YES NO NO YES NO NO NO NO NO YES YES YES YES YES NO YES NO YES NO YES NO NO YES NO NO YES NO NO YES YES NO YES NO YES NO YES YES YES NO YES YES NO YES YES NO YES YES YES NO YES NO NO NO YES YES YES YES NO NO NO YES NO NO NO NO NO NO NO YES NO YES NO YES NO NO NO YES YES NO YES YES NO NO NO NO NO NO YES NO NO YES...
{ "input": [], "output": [] }
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Human-Cow Confederation (HC2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated ga...
715_A. Plus and Square Root_25647
ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ' + ' (plus) and '<image>' (square root). Initially, the number 2 is displayed on the screen. There are n + 1 levels in the game and ZS the Coder start at the level 1. When ZS the Coder is at level k, he can : 1. Pr...
import sys from math import * from fractions import gcd readints=lambda:map(int, input().strip('\n').split()) n=int(input()) prev=2 for i in range(1,n+1): nxt = (i*(i+1))**2 cur = (nxt-prev)//i print(cur) prev=int(sqrt(nxt))
{ "input": [ "4\n", "2\n", "3\n", "3\n", "7\n", "9999\n", "12345\n", "2\n", "2016\n", "1\n", "6723\n", "9417\n", "349\n", "5\n", "6\n", "8\n", "5539\n", "11623\n", "531\n", "10\n", "9\n", "3173\n", "4136\n", "1062\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: ZS the Coder is playing a game. There is a number displayed on the screen and there are two buttons, ' + ' (plus) and '<image>' (square root). Initially, the number 2 is displayed on ...
735_C. Tennis Championship_25651
Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately. Organizers are stil...
f = [0, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836...
{ "input": [ "4\n", "2\n", "10\n", "3\n", "9\n", "12405430465\n", "355687428096000\n", "10235439547\n", "61824012598535\n", "18\n", "3000000000\n", "192403205846532\n", "71624823950223\n", "5\n", "618473717761\n", "21\n", "234\n", "233\n", "9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow kno...
75_C. Modified GCD_25655
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers. A common divisor for two positive numbers is a number which both numbers are divisible by. But your teacher wants to give you a harder task, in this task...
import math from sys import * from bisect import bisect_right input=stdin.readline print=stdout.write a,b=map(int,input().split()) n=int(input()) gcd=math.gcd(a,b) ##print(gcd) factors=[] i=1 while(i*i<=gcd): if gcd%i==0: factors.append(gcd//i) if (i*i)!=gcd: factors.append(gcd//(gcd...
{ "input": [ "9 27\n3\n1 5\n10 11\n9 11\n", "90 100\n8\n55 75\n46 68\n44 60\n32 71\n43 75\n23 79\n47 86\n11 57\n", "48 72\n2\n8 29\n29 37\n", "84 100\n16\n10 64\n3 61\n19 51\n42 67\n51 68\n12 40\n10 47\n52 53\n37 67\n2 26\n23 47\n17 75\n49 52\n3 83\n63 81\n8 43\n", "48 80\n19\n1 1\n16 16\n1 16\n16...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers. A common divisor ...
780_B. The Meeting Place Cannot Be Changed_25659
The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction. At some points on the road there are n friends, and i-th of them is standing at the point xi meters and can move with any speed no greater than vi ...
def solve(t,x,v): l=[x[i]-v[i]*t for i in range(len(x))] r=[x[i]+v[i]*t for i in range(len(x))] return 1 if max(l)<=min(r) else 0 n=int(input()) x=list(map(int,input().split())) v=list(map(int,input().split())) l=0 h=10**9 cnt=0 while l<h and cnt<100: mid=l+(h-l)/2 cnt+=1 if solve(mid,x,v): ...
{ "input": [ "4\n5 10 3 2\n2 3 2 4\n", "3\n7 1 3\n1 2 1\n", "10\n20 11 17 38 15 27 2 40 24 37\n22 30 22 30 28 16 7 20 22 13\n", "3\n1 1 1\n1 1 1\n", "5\n1 15 61 29 43\n15 11 19 19 19\n", "2\n4 5\n10 8\n", "4\n14 12 10 17\n8 6 5 10\n", "3\n1 1000000000 2\n1 2 1000000000\n", "2\n1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The main road in Bytecity is a straight line from south to north. Conveniently, there are coordinates measured in meters from the southernmost building in north direction. At some po...
803_F. Coprime Subsequences_25663
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7. Note th...
# 803F import math import collections def do(): n = int(input()) nums = map(int, input().split(" ")) count = collections.defaultdict(int) for num in nums: for i in range(1, int(math.sqrt(num))+1): cp = num // i if num % i == 0: count[i] += 1 if...
{ "input": [ "4\n1 1 1 1\n", "3\n1 2 3\n", "7\n1 3 5 15 3 105 35\n", "1\n1\n", "5\n10 8 6 4 6\n", "5\n5 1 3 5 4\n", "10\n9 6 8 5 5 2 8 9 2 2\n", "100\n881 479 355 759 257 497 690 598 275 446 439 787 257 326 584 713 322 5 253 781 434 307 164 154 241 381 38 942 680 906 240 11 431 478 628...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1. Given an array a consisting o...
828_D. High Load_25667
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be ...
import sys def main(): n,k = map(int,sys.stdin.readline().split()) a = n-k if a ==1: print(2) for i in range(k): print(1,i+2) elif a > k+1 : l = ((a-1)//k +1)*2 if (a-1)%k>1: print(l+2) elif (a-1)%k==1: print(l+1) els...
{ "input": [ "3 2\n", "5 3\n", "7 2\n", "7 4\n", "5 2\n", "1000 2\n", "5 4\n", "100 98\n", "1013 508\n", "1013 507\n", "10 6\n", "1013 2\n", "1013 1012\n", "4 2\n", "1013 23\n", "1000 670\n", "1000 999\n", "1024 16\n", "1024 512\n", "1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires in...
84_E. Track_25670
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle n × m in size divi...
import sys from array import array # noqa: F401 from itertools import combinations from collections import deque def input(): return sys.stdin.buffer.readline().decode('utf-8') n, m, k = map(int, input().split()) chars = ( ['}' * (m + 2)] + ['}' + ''.join('{' if c == 'S' else '|' if c == 'T' else c for...
{ "input": [ "1 3 3\nTyS\n", "5 3 2\nSba\nccc\naac\nccc\nabT\n", "1 4 1\nSxyT\n", "3 4 1\nSxyy\nyxxx\nyyyT\n", "1 3 3\nSaT\n", "3 4 1\nSbbT\naaaa\nabba\n", "3 4 1\nSbbb\naaaT\nabbc\n", "1 2 4\nST\n", "1 2 1\nST\n", "5 3 4\naaT\nacc\nbbb\nbbc\ncSb\n", "20 20 2\nbaaaaaaaaaaaa...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller...
873_B. Balanced Substring_25674
You are given a string s consisting only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called balanced if the number of zeroes (0) equals to the number of ones in this substring. You have to determine the length of the longest balanc...
inp=lambda:map(int,input().split()) n=int(input()) s=input() a0=[0]*(10**5+1) a1=[0]*(10**5+1) if(s[0]=='0'): a0[0]=1 else: a1[0]=1 for i in range(1,n): if(s[i]=='0'): a0[i]=a0[i-1]+1 a1[i]=a1[i-1] else: a0[i]=a0[i-1] a1[i]=a1[i-1]+1 lab=[-2]*(2*10**5+1) m=[-1]*(2*10*...
{ "input": [ "3\n111\n", "8\n11010111\n", "10\n1000010110\n", "9\n001011001\n", "10\n0011011111\n", "11\n00010000011\n", "10\n0100000000\n", "15\n100000100000011\n", "18\n110010101101111111\n", "3\n011\n", "45\n011010001100001111110001011100000001101100111\n", "11\n0000...
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 only of characters 0 and 1. A substring [l, r] of s is a string slsl + 1sl + 2... sr, and its length equals to r - l + 1. A substring is called bal...
899_A. Splitting in Teams_25678
There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team. The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams...
n = int(input()) a = list(map(int,input().split())) one = a.count(1) two = a.count(2) def func(a,b): if(a>b): count = b a = a-b count = count + a//3 elif(b>a): count = a a = a-count elif(a==b): count = a return count if(one==0): print('0') el...
{ "input": [ "3\n1 1 1\n", "4\n1 1 2 1\n", "2\n2 2\n", "7\n2 2 2 1 1 1 1\n", "10\n1 2 2 1 2 2 1 2 1 1\n", "3\n1 2 2\n", "10\n2 2 1 1 1 1 1 1 1 1\n", "4\n1 1 1 1\n", "3\n2 2 2\n", "247\n2 2 1 2 1 2 2 2 2 2 2 1 1 2 2 1 2 1 1 1 2 1 1 1 1 2 1 1 2 2 1 2 1 1 1 2 2 2 1 1 2 1 1 2 1 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There were n groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the co...
91_B. Queue_25682
There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at the beginning of the queue. The i-th walrus has the age equal to ai. The i-th walrus becomes displeased if there's a younger walrus stan...
n = int(input()) A = [int(i) for i in input().split()] suf = [[10**10, -1] for i in range(n)] from bisect import bisect_left suf[-1][0] = A[-1] suf[-1][1] = n-1 for i in range(n-2, -1, -1): if suf[i+1][0] > A[i]: suf[i][0] = A[i] suf[i][1] = i else: suf[i][0] = suf[i+1][0] suf...
{ "input": [ "6\n10 8 5 3 50 45\n", "5\n10 3 1 10 11\n", "7\n10 4 6 3 2 8 15\n", "13\n16 14 12 9 11 28 30 21 35 30 32 31 43\n", "13\n18 9 8 9 23 20 18 18 33 25 31 37 36\n", "2\n1 1000000000\n", "2\n1000000000 1\n", "15\n18 6 18 21 14 20 13 9 18 20 28 13 19 25 21\n", "12\n5 1 2 5 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the n-th walrus stands at ...
975_E. Hag's Khashba_25688
Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant piece of wood trying to make it look like a goose. Anyway, his dad found out that he was doing arts rather than studying mechanics and othe...
from sys import stdin from math import * rl = lambda l: tuple(map(int, l.split())) rd = lambda: rl(input()) class Point(): def __init__(self, x, y): self.x = x self.y = y def __add__(self, pt): return Point(self.x + pt.x, self.y + pt.y) def __iadd__(self, pt): return self + p...
{ "input": [ "3 2\n-1 1\n0 0\n1 1\n1 1 2\n2 1\n", "3 4\n0 0\n2 0\n2 2\n1 1 2\n2 1\n2 2\n2 3\n", "4 10\n0 0\n2 0\n2 2\n0 2\n2 3\n2 1\n2 1\n1 1 1\n2 3\n1 2 4\n1 4 4\n2 4\n1 1 3\n2 3\n", "10 10\n0 -100000000\n1 -100000000\n1566 -99999999\n2088 -99999997\n2610 -99999994\n3132 -99999990\n3654 -99999985\n41...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Hag is a very talented person. He has always had an artist inside him but his father forced him to study mechanical engineering. Yesterday he spent all of his time cutting a giant pi...
995_D. Game_25692
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a mo...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time (n, r) = (int(i) for i in input().split()) c = [int(i) for i in input().split()] start = time.time() s = sum(c) n2 = 2**n ans = [s/n2] for i in range(r): (k, new) = (int(i) for i in input().split()) s += new - c[k] c[k] = new ...
{ "input": [ "2 2\n0 1 2 3\n2 5\n0 4\n", "2 0\n1 1 1 1\n", "1 0\n2 3\n", "2 2\n0 1 2 3\n2 5\n1 4\n", "2 0\n1 1 1 0\n", "2 2\n0 1 2 3\n2 3\n1 4\n", "2 2\n0 1 2 2\n2 3\n1 4\n", "2 2\n1 1 2 2\n0 3\n1 4\n", "2 2\n0 0 2 3\n2 5\n0 4\n", "2 0\n2 1 1 1\n", "1 0\n2 2\n", "2 0\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of t...
p02644 AtCoder Beginner Contest 170 - Pond Skater_25705
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west. Some of the squares have a lotus leaf on it and cannot be entered. The square (i,j) has a lotus leaf on i...
from collections import deque dxdy = ((-1,0), (1,0), (0,-1), (0,1)) # タプルやリストで持っておくと便利 H, W, K = map(int,input().split()) x1, y1, x2, y2 = map(int,input().split()) mp = [input() for _ in range(H)] INF = 10**10 dist = [[INF]*W for _ in range(H)] dist[x1-1][y1-1] = 0 q = deque() q.append((x1-1,y1-1)) # スタート地点をenqueue wh...
{ "input": [ "3 5 2\n3 2 3 4\n.....\n.@..@\n..@..", "3 3 1\n2 1 2 3\n.@.\n.@.\n.@.", "1 6 4\n1 1 1 6\n......", "2 3 1\n2 1 2 3\n.@.\n.@.\n.@.", "3 5 2\n3 2 3 4\n.....\n.@-.@\n..@..", "1 6 4\n1 1 1 1\n......", "3 5 4\n3 2 3 4\n.....\n.@..@\n..@..", "1 6 4\n1 2 1 6\n......", "3 5 4\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north ...
p02773 AtCoder Beginner Contest 155 - Poll_25709
We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it. Print all strings that are written on the most number of votes, in lexicographical order. Constraints * 1 \leq N \leq 2 \times 10^5 * S_i (1 \leq i \leq N) are strings consisting of lowercase English letters. * The length of S...
from collections import Counter n=int(input()) a=[input() for _ in range(n)] c=Counter(a) max_cnt=max(c.values()) names=[name for name,v in c.items() if v==max_cnt] names.sort() print(*names,sep='\n')
{ "input": [ "8\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nbuffalo", "7\nbeat\nvet\nbeet\nbed\nvet\nbet\nbeet", "7\nbass\nbass\nkick\nkick\nbass\nkick\nkick", "4\nushi\ntapu\nnichia\nkun", "8\nbuffalo\nbuffalo\nbuffalo\nbuffalo\nolaffub\nbuffalo\nbuffalo\nbuffalo", "7\nbea...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have N voting papers. The i-th vote (1 \leq i \leq N) has the string S_i written on it. Print all strings that are written on the most number of votes, in lexicographical order. ...
p03043 AtCoder Beginner Contest 126 - Dice and Coin_25714
Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them: 1. Throw the die. The current score is the result of the die. 2. As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each t...
import math n,k=map(int,input().split()) ans=0 for i in range(1,n+1): ans+=(1/2)**math.ceil(max(math.log2(k/i),0)) print(ans/n)
{ "input": [ "3 10", "100000 5", "3 4", "100000 8", "3 7", "100000 7", "6 7", "100100 7", "4 7", "100100 6", "110100 7", "111100 7", "111100 9", "111100 2", "111110 2", "111100 0", "101100 0", "101101 0", "101010 2", "111010 -1", "3 1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them: 1. Throw the die. The current scor...
p03185 Educational DP Contest - Frog 3_25718
There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds. There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone N: * If the frog is currently on Stone i, jump to one of ...
def fi(i, x): a, b = lines[i] return a*x+b def find(x): def f(i): return fi(i+1,x) > fi(i,x) mn, mx = -1, len(lines)-1 idx = (mn+mx)//2 while mx-mn>1: if f(idx): mx, idx = idx, (mn + idx)//2 continue mn, idx = idx, (mx + idx)//2 return fi(idx+...
{ "input": [ "8 5\n1 3 4 5 10 11 12 13", "2 1000000000000\n500000 1000000", "5 6\n1 2 3 4 5", "1 1000000000000\n500000 1000000", "8 5\n1 3 4 6 10 11 12 13", "2 1000000000000\n948591 1000000", "8 5\n1 3 4 6 10 11 12 14", "2 1000000000000\n710905 1000000", "2 1000000100000\n710905 10...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N stones, numbered 1, 2, \ldots, N. For each i (1 \leq i \leq N), the height of Stone i is h_i. Here, h_1 < h_2 < \cdots < h_N holds. There is a frog who is initially on St...
p03332 AtCoder Grand Contest 025 - RGB Coloring_25722
Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful tower. He defines the beauty of the tower as follows: * The beauty of the tower is the sum of the scores of the N layers, where the sco...
N, A, B, K = map(int, input().split()) mod = 998244353 # 階乗 & 逆元計算 factorial = [1] inverse = [1] for i in range(1, N+2): factorial.append(factorial[-1] * i % mod) inverse.append(pow(factorial[-1], mod-2, mod)) # 組み合わせ計算 def nCr(n, r): if n < r or r < 0: return 0 elif r == 0: return 1 ...
{ "input": [ "90081 33447 90629 6391049189", "2 5 6 0", "4 1 2 5", "90081 33447 66380 6391049189", "2 5 6 1", "90081 33447 77758 6391049189", "2 9 7 0", "4 1 3 5", "90081 33447 77758 2284871002", "6 1 2 17", "121980 33447 90629 11295524182", "4 1 1 5", "111605 33447...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi has a tower which is divided into N layers. Initially, all the layers are uncolored. Takahashi is going to paint some of the layers in red, green or blue to make a beautiful...
p03491 AtCoder Regular Contest 087 - Prefix-free Game_25725
For strings s and t, we will say that s and t are prefix-free when neither is a prefix of the other. Let L be a positive integer. A set of strings S is a good string set when the following conditions hold true: * Each string in S has a length between 1 and L (inclusive) and consists of the characters `0` and `1`. * A...
""" Writer: SPD_9X2 https://atcoder.jp/contests/arc087/tasks/arc087_c 初期状態で出ている数字を木としてあらわす 数字でふさがっている所はもう追加できない まだおけるのは、空いている部分だけ Lがでかいので、深さを考えていては死ぬ そのままふさぐと、置ける場所は1減る 伸ばしておけば、Lまで増やして置ける 置ける場所の偶奇か? 置ける場所が0で来たら負け→初手が奇数なら先手勝ち・そうでないなら後手勝ち? 伸ばせる奴は偶奇反転に使える 部分で考えてみるか→ Grundy数計算 深さ1の部分木のGrundy → 1 深さ2の部分木のGrundy → 2 深...
{ "input": [ "3 3\n0\n10\n110", "2 3\n101\n11", "1 2\n11", "2 2\n00\n11", "2 1\n0\n1", "2 2\n00\n01", "3 3\n0\n10\n100", "4 1\n0\n1", "1 4\n11", "2 1\n1\n1", "2 4\n00\n01", "3 3\n0\n10\n101", "1 4\n12", "3 4\n00\n01", "1 4\n17", "3 4\n00\n0", "3 5\n0...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For strings s and t, we will say that s and t are prefix-free when neither is a prefix of the other. Let L be a positive integer. A set of strings S is a good string set when the fol...
p03653 AtCoder Grand Contest 018 - Coins_25729
There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of those people, silver coins from Y of the people and bronze coins from Z of the people. It is not possible to get two or more different col...
from heapq import heappushpop import sys X, Y, Z = map(int, sys.stdin.readline().split()) N = X+Y+Z ABC = [list(map(int, sys.stdin.readline().split())) for _ in range(N)] ABC.sort(key = lambda x: x[0] - x[1], reverse = True) GB = [None]*N Q = [a - c for a, _, c in ABC[:X]] Q.sort() gs = sum(a for a, _, _ in ABC[:X]) G...
{ "input": [ "1 2 1\n2 4 4\n3 2 1\n7 6 7\n5 2 3", "6 2 4\n33189 87907 277349742\n71616 46764 575306520\n8801 53151 327161251\n58589 4337 796697686\n66854 17565 289910583\n50598 35195 478112689\n13919 88414 103962455\n7953 69657 699253752\n44255 98144 468443709\n2332 42580 752437097\n39752 19060 845062869\n601...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are X+Y+Z people, conveniently numbered 1 through X+Y+Z. Person i has A_i gold coins, B_i silver coins and C_i bronze coins. Snuke is thinking of getting gold coins from X of t...
p03809 AtCoder Grand Contest 010 - Cleaning_25733
There is a tree with N vertices, numbered 1 through N. The i-th of the N-1 edges connects vertices a_i and b_i. Currently, there are A_i stones placed on vertex i. Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation: * Select a pair of different...
import sys sys.setrecursionlimit(10 ** 6) def dfs(v, p, aaa): if len(links[v]) == 1: return aaa[v] children = [] for u in links[v]: if u == p: continue result = dfs(u, v, aaa) if result == -1: return -1 children.append(result) if len(c...
{ "input": [ "6\n3 2 2 2 2 2\n1 2\n2 3\n1 4\n1 5\n4 6", "5\n1 2 1 1 2\n2 4\n5 2\n3 2\n1 3", "3\n1 2 1\n1 2\n2 3", "3\n2 2 1\n1 2\n2 3", "6\n3 2 2 2 2 2\n1 2\n2 3\n1 6\n1 5\n4 6", "6\n5 2 2 2 2 2\n1 2\n2 3\n1 6\n1 5\n4 6", "6\n5 2 4 2 2 2\n1 2\n2 3\n1 6\n1 5\n4 6", "6\n5 2 4 2 2 2\n1 2\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a tree with N vertices, numbered 1 through N. The i-th of the N-1 edges connects vertices a_i and b_i. Currently, there are A_i stones placed on vertex i. Determine whether ...
p03977 Kyoto University Programming Contest 2016 - Cookie Breeding Machine_25737
A professor invented Cookie Breeding Machine for his students who like cookies very much. When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine, it consumes the cookie and generates two cookies with the taste of y and (x XOR y). Here...
N,T=0,0 for i in range(int(input())): N,T=map(int,input().split()) if N&1==0: T^=127 print(T+(N-1)*127)
{ "input": [ "3\n3 1\n4 108\n1 10", "3\n3 2\n4 108\n1 10", "3\n3 0\n4 108\n1 10", "3\n3 1\n4 98\n1 10", "3\n1 1\n4 108\n1 10", "3\n3 1\n4 108\n1 2", "3\n3 1\n2 98\n1 10", "3\n3 1\n4 50\n1 2", "3\n3 2\n2 98\n1 10", "3\n3 1\n4 75\n1 2", "3\n3 2\n2 98\n1 9", "3\n3 1\n4 75\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A professor invented Cookie Breeding Machine for his students who like cookies very much. When one cookie with the taste of x is put into the machine and a non-negative integer y les...
p00066 Tic Tac Toe_25741
Tic-tac-toe is a game in which you win when you put ○ and × alternately in the 3 × 3 squares and line up ○ or × in one of the vertical, horizontal, and diagonal lines (Fig.). 1 to Fig. 3) <image> | <image> | <image> --- | --- | --- Figure 1: ○ wins | Figure 2: × wins | Figure 3: Draw In tic-tac-toe, ○ and × alterna...
ok = [[0,4,8], [2,4,6], [0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8]] while True: try: s = input() except EOFError: break flag = False for i in ok: if s[i[0]] == s[i[1]] == s[i[2]] and s[i[0]] != 's': print(s[i[0]]) flag = True break ...
{ "input": [ "ooosxssxs\nxoosxsosx\nooxxxooxo", "ooosxssxs\nxooswsosx\nooxxxooxo", "oopsxssxs\nxooswsosx\noxooxxxoo", "oopsxrsxs\nxsoswsoox\nooooxxxox", "xprmnwrtt\ntoyqulosl\nxxxmilpm{", "ooosxssxs\nxoosxsosx\noxooxxxoo", "ooosxssxs\nxooswsosx\noxooxxxoo", "oopsxssxs\nxsoswsoox\noxoox...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Tic-tac-toe is a game in which you win when you put ○ and × alternately in the 3 × 3 squares and line up ○ or × in one of the vertical, horizontal, and diagonal lines (Fig.). 1 to Fig...
p00198 Trouble in Shinagawa's Artifacts_25745
Artist Shinagawa was asked to exhibit n works. Therefore, I decided to exhibit the six sides of the cube colored with paint as a work. The work uses all six colors, Red, Yellow, Blue, Magenta, Green, and Cyan, and each side is filled with one color. Shinagawa changed the arrangement of colors even for cubic works with ...
D = [ (1, 5, 2, 3, 0, 4), # 'U' (3, 1, 0, 5, 4, 2), # 'R' (4, 0, 2, 3, 5, 1), # 'D' (2, 1, 5, 0, 4, 3), # 'L' ] p_dice = (0, 0, 0, 1, 1, 2, 2, 3)*3 def rotate_dice(L0): L = L0[:] for k in p_dice: yield L L[:] = (L[e] for e in D[k]) while 1: N = int(input()) if N == 0: ...
{ "input": [ "3\nCyan Yellow Red Magenta Green Blue\nCyan Yellow Red Magenta Green Blue\nRed Yellow Magenta Blue Green Cyan\n4\nRed Magenta Blue Green Yellow Cyan\nRed Yellow Magenta Blue Green Cyan\nMagenta Green Red Cyan Yellow Blue\nCyan Green Yellow Blue Magenta Red\n0", "3\nCyan Yellow Red Magenta Green ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Artist Shinagawa was asked to exhibit n works. Therefore, I decided to exhibit the six sides of the cube colored with paint as a work. The work uses all six colors, Red, Yellow, Blue,...
p00352 Handsel_25748
Alice and Brown are brothers in a family and each receives pocket money in celebration of the coming year. They are very close and share the total amount of the money fifty-fifty. The pocket money each receives is a multiple of 1,000 yen. Write a program to calculate each one’s share given the amount of money Alice an...
a, b = map(int, input().split()) cash = (a+b)//2 print(cash)
{ "input": [ "5000 5000", "1000 2000", "1000 3000", "3857 5000", "0000 2000", "1010 3000", "428 5000", "0000 2954", "0010 3000", "824 5000", "0000 4848", "0000 3000", "45 5000", "0010 4848", "45 3263", "0010 8622", "0011 4882", "53 3263", "00...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice and Brown are brothers in a family and each receives pocket money in celebration of the coming year. They are very close and share the total amount of the money fifty-fifty. The...
p00559 Foehn Phenomena_25751
In the Kingdom of IOI, the wind always blows from sea to land. There are $N + 1$ spots numbered from $0$ to $N$. The wind from Spot $0$ to Spot $N$ in order. Mr. JOI has a house at Spot $N$. The altitude of Spot $0$ is $A_0 = 0$, and the altitude of Spot $i$ ($1 \leq i \leq N$) is $A_i$. The wind blows on the surface ...
n, q, s, t = map(int, input().split()) a_lst = [int(input()) for _ in range(n + 1)] diff = [a_lst[i + 1] - a_lst[i] for i in range(n)] temp = sum([-d * s if d > 0 else -d * t for d in diff]) def score(d): if d > 0: return -s * d else: return -t * d for _ in range(q): l, r, x = map(int, input().split()) ...
{ "input": [ "3 5 1 2\n0\n4\n1\n8\n1 2 2\n1 1 -2\n2 3 5\n1 2 -1\n1 3 5", "3 5 1 2\n0\n4\n1\n8\n1 2 2\n1 1 -2\n2 3 5\n1 2 -1\n1 2 5", "3 5 1 2\n0\n4\n1\n8\n1 2 2\n1 1 -3\n2 3 5\n1 2 -1\n1 2 5", "3 5 1 2\n0\n4\n1\n8\n2 2 2\n1 1 -3\n2 3 5\n1 2 -1\n1 2 5", "3 5 1 2\n0\n4\n1\n8\n2 2 2\n1 1 -3\n2 1 5\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the Kingdom of IOI, the wind always blows from sea to land. There are $N + 1$ spots numbered from $0$ to $N$. The wind from Spot $0$ to Spot $N$ in order. Mr. JOI has a house at Sp...
p00712 Unit Fraction Partition_25754
A fraction whose numerator is 1 and whose denominator is a positive integer is called a unit fraction. A representation of a positive rational number p/q as the sum of finitely many unit fractions is called a partition of p/q into unit fractions. For example, 1/2 + 1/6 is a partition of 2/3 into unit fractions. The dif...
from fractions import gcd def solve(p, q, a, n, l=1): ans = 1 if p==1 and q<=a and q>=l else 0 denom = max(l, q//p) p_denom = denom*p while n*q >= p_denom and denom <= a: #n/denom >= p/q: p_, q_ = p_denom-q, q*denom if p_ <= 0: denom += 1 p_denom += p ...
{ "input": [ "2 3 120 3\n2 3 300 3\n2 3 299 3\n2 3 12 3\n2 3 12000 7\n54 795 12000 7\n2 3 300 1\n2 1 200 5\n2 4 54 2\n0 0 0 0", "2 3 120 3\n2 3 300 3\n2 3 299 3\n2 3 12 3\n2 3 12000 7\n54 795 12000 7\n2 4 300 1\n2 1 200 5\n2 4 54 2\n0 0 0 0", "2 3 120 3\n2 3 300 3\n2 3 299 3\n2 3 12 2\n2 3 12000 7\n54 795...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A fraction whose numerator is 1 and whose denominator is a positive integer is called a unit fraction. A representation of a positive rational number p/q as the sum of finitely many u...
p00983 Reordering the Documents_25758
Reordering the Documents Susan is good at arranging her dining table for convenience, but not her office desk. Susan has just finished the paperwork on a set of documents, which are still piled on her desk. They have serial numbers and were stacked in order when her boss brought them in. The ordering, however, is not...
def main(): mod = 10 ** 9 + 7 n, m = map(int, input().split()) a = [int(x) for x in input().split()] if not m: print(0) return mx = [0] * (n + 1) mn = [mod] * (n + 1) for i in range(n): if mx[i] > a[i]: mx[i + 1] = mx[i] else: ...
{ "input": [ "6 3\n1 3 4 2 6 5", "6 3\n1 5 4 2 6 5", "2 1\n0 1 -1 1 -1 -1", "6 3\n1 5 4 2 0 5", "6 3\n1 5 4 2 0 2", "6 3\n1 5 4 2 0 3", "6 3\n1 5 4 4 0 3", "6 3\n1 5 4 1 0 3", "6 3\n1 5 1 1 0 3", "6 3\n1 5 1 1 0 0", "11 3\n1 5 4 2 0 5", "6 3\n1 5 7 2 0 2", "6 3\n1 5...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Reordering the Documents Susan is good at arranging her dining table for convenience, but not her office desk. Susan has just finished the paperwork on a set of documents, which are...
p01115 Expression Mining_25761
Expression Mining Consider an arithmetic expression built by combining single-digit positive integers with addition symbols `+`, multiplication symbols `*`, and parentheses `(` `)`, defined by the following grammar rules with the start symbol `E`. E ::= T | E '+' T T ::= F | T '*' F F ::= '1' | '2' | '3' | '4' | '5'...
import sys readline = sys.stdin.readline write = sys.stdout.write sys.setrecursionlimit(10**5) def solve(): N = int(readline()) if N == 0: return False S = readline().strip() + "$" L = len(S) pt = [0]*L st = [] for i in range(L): if S[i] == '(': st.append(i) ...
{ "input": [ "3\n(1+2)*3+3\n2\n1*1*1+1*1*1\n587\n1*(2*3*4)+5+((6+7*8))*(9)\n0", "3\n(1+2)*3+3\n3\n1*1*1+1*1*1\n587\n1*(2*3*4)+5+((6+7*8))*(9)\n0", "3\n(1+2)*3+3\n3\n1*1*1+1*1*1\n721\n1*(2*3*4)+5+((6+7*8))*(9)\n0", "3\n(1+2)*3+3\n3\n1*1*1+1*1+1\n1480\n1*(2*3*4)+5+((6+7*8))*(9)\n0", "3\n(1+2)*3+3\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Expression Mining Consider an arithmetic expression built by combining single-digit positive integers with addition symbols `+`, multiplication symbols `*`, and parentheses `(` `)`, ...
p01414 Rectangular Stamps_25765
Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well. Today's training is to increase creativity by drawing pictures. Let's draw a pattern well using a square stamp. I want to use stamps of various sizes to complete the picture of the red, ...
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**13 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....
{ "input": [ "1\n2 3\nRRGG\nBRGG\nBRRR\nBRRR", "2\n4 4\n1 1\nRRRR\nRRGR\nRBRR\nRRRR", "1\n2 3\nRRGG\nGGRB\nBRRR\nBRRR", "2\n4 5\n1 1\nRRRR\nRRGR\nRBRR\nRRRR", "2\n4 5\n2 1\nRRRR\nRRGR\nRBRR\nRRRR", "1\n2 1\nRRGG\nGGRB\nBRRR\nBRRR", "1\n2 1\nGGRR\nGGRB\nBRRR\nBRRR", "1\n1 1\nGGRR\nGGRB\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Training is indispensable for achieving good results at ICPC. Rabbit wants to win at ICPC, so he decided to practice today as well. Today's training is to increase creativity by draw...
p01568 Repairing_25768
In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network. The network consists of water pipe segments, stop valves and source point. A water pipe is represented by a segment on a 2D-plane and intersected pair of water pipe segments are connected...
from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write def dot3(O, A, B): ox, oy = O; ax, ay = A; bx, by = B return (ax - ox) * (bx - ox) + (ay - oy) * (by - oy) def cross3(O, A, B): ox, oy = O; ax, ay = A; bx, by = B return (ax - ox) * (by - oy) - (bx - ox) * (a...
{ "input": [ "2 1\n0 0 0 4\n0 2 2 2\n1 2\n0 1\n0 3", "5 3\n0 4 2 4\n0 2 2 2\n0 0 2 0\n0 0 0 4\n2 0 2 4\n0 2\n1 0\n2 2\n1 4\n2 1", "1 2\n0 0 10 0\n1 0\n9 0\n0 0\n5 0", "2 1\n0 0 0 4\n0 2 2 2\n1 2\n-1 1\n0 3", "5 3\n0 4 2 4\n0 2 2 2\n0 0 2 0\n0 0 0 4\n2 0 2 4\n0 2\n1 0\n2 2\n2 4\n2 1", "1 2\n0 -...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network. The network consists of water pipe segments, stop ...
p02005 Colorful Drink_25773
In the Jambo Amusement Garden (JAG), you sell colorful drinks consisting of multiple color layers. This colorful drink can be made by pouring multiple colored liquids of different density from the bottom in order. You have already prepared several colored liquids with various colors and densities. You will receive a d...
import sys liquids={} O=[] N = int(input()) for i in range(N): C,D=(input().split()) if C in liquids.keys(): liquids[C].append(int(D)) else: liquids[C]=[] liquids[C].append(int(D)) for i in liquids.keys(): liquids[i]=list(set(liquids[i])) liquids[i].sort() M = int(input()) ...
{ "input": [ "2\nwhite 10\nblack 10\n2\nblack\nwhite", "2\nwhite 20\nblack 10\n2\nblack\norange", "2\nwhite 20\nblack 10\n2\nblack\nwhite", "4\nred 3444\nred 3018\nred 3098\nred 3319\n4\nred\nred\nred\nred", "3\nwhite 10\nred 20\nwhite 30\n3\nwhite\nred\nwhite", "2\nwhite 10\nblack 10\n4\nblac...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the Jambo Amusement Garden (JAG), you sell colorful drinks consisting of multiple color layers. This colorful drink can be made by pouring multiple colored liquids of different den...
p02149 Lunch_25776
Problem One day, Kawabayashi is about to have lunch at the school cafeteria. There are three types of daily lunch menus for school cafeterias: A lunch, B lunch, and C lunch. Kawabayashi is a glutton, so I would like to eat all three types of daily lunch menus one by one. However, Kawabayashi decided to put up with one...
a, b, c = map(int, input().split()) if a > b and a > c: print ('A') elif b > a and b > c: print ('B') else: print('C')
{ "input": [ "1000 800 1200", "1000 900 850", "1000 800 1040", "1000 900 239", "1000 1135 850", "1000 800 838", "1000 279 239", "1000 316 838", "1100 279 239", "1001 316 838", "1100 279 393", "1001 316 1343", "0100 279 393", "1000 316 1343", "0100 369 393", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem One day, Kawabayashi is about to have lunch at the school cafeteria. There are three types of daily lunch menus for school cafeterias: A lunch, B lunch, and C lunch. Kawabaya...
p02290 Projection_25780
For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input xp1 yp1 xp2 yp2 q xp0 yp0 xp1 yp1 ... xpq−1 ypq−1 In the first line, integer coordinates of p1 and p2 are given. Then, q queries are giv...
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 0 0 3 4 1 2 5 output: 3.1200000000 4.1600000000 """ import sys class Segment(object): __slots__ = ('source', 'target') def __init__(self, source, target): self.source = complex(source) self.target = complex(target) def dot(a, b): ...
{ "input": [ "0 0 3 4\n1\n2 5", "0 0 2 0\n3\n-1 1\n0 1\n1 1", "0 0 1 4\n1\n2 5", "0 0 2 0\n3\n-1 1\n0 2\n1 1", "0 0 1 4\n1\n2 9", "0 0 1 6\n1\n2 9", "0 -1 1 6\n1\n2 9", "0 -1 2 6\n1\n2 9", "0 -1 2 2\n1\n2 9", "0 -1 4 2\n1\n2 9", "0 -2 4 2\n1\n2 9", "0 0 2 0\n3\n-1 1\n0 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For given three points p1, p2, p, find the projection point x of p onto p1p2. <image> Constraints * 1 ≤ q ≤ 1000 * -10000 ≤ xi, yi ≤ 10000 * p1 and p2 are not identical. Input x...
p02437 Priority Queue_25784
Priority queue is a container of elements which the element with the highest priority should be extracted first. For $n$ priority queues $Q_i$ ($i = 0, 1, ..., n-1$) of integers, perform a sequence of the following operations. * insert($t$, $x$): Insert $x$ to $Q_t$. * getMax($t$): Report the maximum value in $Q_t$. ...
import sys from collections import defaultdict from heapq import heappop, heappush n = int(sys.stdin.readline().split()[0]) A = defaultdict(list) ans = [] for query in sys.stdin: if query[0] == '0': t, x = query[2:].split() heappush(A[t], -int(x)) elif query[0] == '1': if A[query[2:-1]]:...
{ "input": [ "2 10\n0 0 3\n0 0 9\n0 0 1\n1 0\n2 0\n1 0\n0 0 4\n1 0\n0 1 8\n1 1", "2 10\n0 0 3\n0 0 9\n0 0 1\n1 0\n2 0\n1 0\n0 0 4\n1 0\n0 0 8\n1 1", "2 10\n0 0 3\n0 0 9\n0 0 1\n1 0\n2 1\n1 0\n0 0 4\n1 0\n0 1 8\n1 1", "2 10\n0 0 3\n0 0 9\n0 0 2\n1 1\n2 0\n1 0\n0 0 4\n1 0\n0 0 8\n1 1", "2 10\n0 0 3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Priority queue is a container of elements which the element with the highest priority should be extracted first. For $n$ priority queues $Q_i$ ($i = 0, 1, ..., n-1$) of integers, per...
1020_B. Badge_25794
In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick. Let's assume that all these students are numbered from 1 to n. The teacher came to student a and put a hole in his badge. The student, ...
n = int(input()) p = list(map(int, input().split())) for i in range(len(p)): p[i] = p[i] - 1 ans = [0] * len(p) for i in range(len(p)): visit = [0] * len(p) visit[i] = 1 p0 = i while True: pp = p[p0] if (visit[pp] == 1): ans[i] = pp break else: visit[pp] = 1 p0 = pp s =...
{ "input": [ "3\n1 2 3\n", "3\n2 3 2\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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of n students doing yet another trick. ...
1043_E. Train Hard, Win Easy_25798
Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, and each competitor will code exactly one of them. Of course, people in one team will code different problems. Rules of scoring also are...
import sys n,m = [int(x) for x in sys.stdin.buffer.readline().split()] inp = [int(x) for x in sys.stdin.buffer.read().split()] order = sorted(range(n),key=lambda i:inp[2*i]-inp[2*i+1]) score = [0]*n val = sum(inp[1:2*n:2]) for ind in range(n): i = order[ind] # Do second problem together with order[:ind] ...
{ "input": [ "3 3\n1 2\n2 3\n1 3\n1 2\n2 3\n1 3\n", "5 3\n-1 3\n2 4\n1 1\n3 5\n2 2\n1 4\n2 3\n3 5\n", "3 2\n1 2\n2 3\n1 3\n1 2\n2 3\n", "20 0\n-2 0\n0 2\n1 2\n-2 1\n2 1\n0 0\n-1 2\n-2 2\n-2 -2\n2 -1\n0 -2\n2 0\n0 -1\n0 1\n1 -1\n-1 -2\n-2 -1\n1 1\n2 -2\n2 2\n", "40 39\n0 -2\n-2 3\n3 3\n3 -2\n-1 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Zibi is a competitive programming coach. There are n competitors who want to be prepared well. The training contests are quite unusual – there are two people in a team, two problems, ...
1066_C. Books Queries_25802
You have got a shelf and want to put some books on it. You are given q queries of three types: 1. L id — put a book having index id on the shelf to the left from the leftmost existing book; 2. R id — put a book having index id on the shelf to the right from the rightmost existing book; 3. ? id — calculate the...
from sys import * n = int(stdin.readline()) A = {} i ,j = 1,0 # print(i,j) for l in range(n): x, y = stdin.readline().split("\n") x, k = map(str,x.split()) k = int(k) if(x=='?'): stdout.write(str(min(A[k]-i , j-A[k]))+'\n') elif(x=='R'): j += 1 A[k] = j else: i -= 1 A[k] = i
{ "input": [ "8\nL 1\nR 2\nR 3\n? 2\nL 4\n? 1\nL 5\n? 1\n", "10\nL 100\nR 100000\nR 123\nL 101\n? 123\nL 10\nR 115\n? 100\nR 110\n? 115\n", "7\nL 1\nR 2\nR 3\nL 4\nL 5\n? 1\n? 2\n", "6\nL 1\nR 2\nR 3\n? 2\nL 4\n? 1\n", "6\nL 1\nR 2\nR 5\n? 2\nL 4\n? 1\n", "10\nL 100\nR 101000\nR 123\nL 101\n? ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have got a shelf and want to put some books on it. You are given q queries of three types: 1. L id — put a book having index id on the shelf to the left from the leftmost exis...
1089_F. Fractions_25806
You are given a positive integer n. Find a sequence of fractions (a_i)/(b_i), i = 1 … k (where a_i and b_i are positive integers) for some k such that: $$$ \begin{cases} $b_i$ divides $n$, $1 < b_i < n$ for $i = 1 … k$ \\\ $1 ≤ a_i < b_i$ for $i = 1 … k$ \\\ \text{$∑_{i=1}^k (a_i)/(b_i) = 1 - 1/n$} \end{cases} $$$ I...
from math import sqrt from itertools import count, islice from fractions import Fraction def isPrime(n): return n > 1 and all(n % i for i in islice(count(2), int(sqrt(n) - 1))) def factors(n: int): _factors = [] for i in range(2, int(sqrt(n)) + 1): times = 0 while n % i == 0: t...
{ "input": [ "2\n", "6\n", "525518174\n", "792852058\n", "9589\n", "208466113\n", "10895\n", "517920199\n", "387420489\n", "62742241\n", "58644781\n", "71851857\n", "698377680\n", "294390793\n", "103756027\n", "5\n", "685518877\n", "18438\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 positive integer n. Find a sequence of fractions (a_i)/(b_i), i = 1 … k (where a_i and b_i are positive integers) for some k such that: $$$ \begin{cases} $b_i$ divid...
1108_A. Two distinct points_25810
You are given two segments [l_1; r_1] and [l_2; r_2] on the x-axis. It is guaranteed that l_1 < r_1 and l_2 < r_2. Segments may intersect, overlap or even coincide with each other. <image> The example of two segments on the x-axis. Your problem is to find two integers a and b such that l_1 ≤ a ≤ r_1, l_2 ≤ b ≤ r_2 an...
qry=int(input()) for _ in range(qry): lrs=list(map(int,input().split())) if lrs[0]==lrs[2]: print(lrs[0],lrs[3]) else: print(lrs[0],lrs[2])
{ "input": [ "5\n1 2 1 2\n2 6 3 4\n2 4 1 3\n1 2 1 3\n1 4 5 8\n", "1\n233 233333 123 456\n", "1\n1 2 1 2\n", "5\n1 2 1 2\n2 6 3 4\n2 4 1 3\n1 2 1 3\n1 4 5 8\n", "1\n233 233333 79 456\n", "1\n1 2 0 2\n", "5\n1 2 1 2\n2 6 3 4\n2 4 1 3\n1 2 1 3\n2 4 5 8\n", "5\n1 2 1 2\n2 6 3 4\n2 4 1 3\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two segments [l_1; r_1] and [l_2; r_2] on the x-axis. It is guaranteed that l_1 < r_1 and l_2 < r_2. Segments may intersect, overlap or even coincide with each other. <...
1178_B. WOW Factor_25818
Recall that string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly zero or all) characters. For example, for the string a="wowwo", the following strings are subsequences: "wowwo", "wowo", "oo", "wow", "", and others, but the following are not subsequences: "owoo", "owwwo", ...
s = input() n = len(s) count_o = [] count_w = 0 count = 0 for i in range(1, n): if s[i] == 'v' and s[i-1] == 'v': count_w += 1 elif s[i] == 'o': count_o.append(count_w) for c in count_o: count += c * (count_w-c) print(count)
{ "input": [ "vvvovvv\n", "vvovooovovvovoovoovvvvovovvvov\n", "ovvo\n", "vovoovovvoovvvvvvovo\n", "vvovvovvovvovv\n", "vvoovv\n", "vovvv\n", "vvovv\n", "ovvvvovovvvvovoovovovovvvvvvvoovoovvovvoooooovo\n", "voovovvvoo\n", "o\n", "vo\n", "v\n", "ovvn\n", "ovov...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recall that string a is a subsequence of a string b if a can be obtained from b by deletion of several (possibly zero or all) characters. For example, for the string a="wowwo", the fo...
1196_B. Odd Sum Segments_25822
You are given an array a consisting of n integers a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the e...
import math import sys for _ in range(int(input())): n,k=map(int,sys.stdin.readline().split()) l=list(map(int,sys.stdin.readline().split())) c=0 for i in l: if i%2!=0: c+=1 if c%2!=k%2 or c<k: print("NO") else: print("YES") cnt=1 for i in range...
{ "input": [ "3\n5 3\n7 18 3 14 1\n5 4\n1 2 3 4 5\n6 2\n1 2 8 4 10 2\n", "1\n1 1\n2\n", "3\n5 3\n7 18 3 14 1\n5 4\n1 2 3 4 5\n6 2\n2 2 8 4 10 2\n", "3\n5 3\n7 18 3 14 1\n5 4\n1 2 3 4 5\n6 2\n3 1 8 4 10 2\n", "3\n5 3\n7 26 2 14 1\n4 4\n1 2 4 4 1\n6 3\n3 2 8 4 10 0\n", "1\n1 2\n2\n", "3\n5 3...
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 a_1, a_2, ..., a_n. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum ...
1213_D1. Equalizing by Division (easy version)_25826
The only difference between easy and hard versions is the number of elements in the array. You are given an array a consisting of n integers. In one move you can choose any a_i and divide it by 2 rounding down (in other words, in one move you can set a_i := ⌊(a_i)/(2)⌋). You can perform such an operation any (possibl...
import collections import functools import math import sys import bisect def In(): return map(int, sys.stdin.readline().split()) input = sys.stdin.readline def eqdiv(): n,k = In() cost = [0]*int(1e5*2+10) count = cost[:] l = list(In()) l.sort() for i in l: i = int(i) pos...
{ "input": [ "5 3\n1 2 3 4 5\n", "5 3\n1 2 3 3 3\n", "5 3\n1 2 2 4 5\n", "50 1\n156420 126738 188531 85575 23728 72842 190346 24786 118328 137944 126942 115577 175247 85409 146194 31398 189417 52337 135886 162083 146559 131125 31741 152481 57935 26624 106893 55028 81626 99143 182257 129556 100261 1142...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The only difference between easy and hard versions is the number of elements in the array. You are given an array a consisting of n integers. In one move you can choose any a_i and d...
1236_E. Alice and the Unfair Game_25830
Alice is playing a game with her good friend, Marisa. There are n boxes arranged in a line, numbered with integers from 1 to n from left to right. Marisa will hide a doll in one of the boxes. Then Alice will have m chances to guess where the doll is. If Alice will correctly guess the number of box, where doll is now, ...
import sys input = sys.stdin.readline n,m=map(int,input().split()) A=list(map(int,input().split())) if n==1: print(0) sys.exit() from collections import Counter R=Counter() L=Counter() for i in range(n): R[i+1]=1 L[i+1]=1 for i,a in enumerate(A): x=R[a-(i+1)] del R[a-(i+1)] R[a-(i+1)-1]...
{ "input": [ "5 2\n3 1\n", "3 3\n2 2 2\n", "4 10\n4 1 4 2 2 1 4 3 3 1\n", "7 1\n3\n", "9 10\n9 8 7 4 5 8 1 6 8 2\n", "8 1\n8\n", "8 10\n7 7 7 7 7 7 7 7 7 2\n", "10 10\n1 9 7 6 2 4 7 8 1 3\n", "1000 100\n343 745 296 856 507 192 780 459 9 193 753 28 334 871 589 105 612 751 708 129 32...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice is playing a game with her good friend, Marisa. There are n boxes arranged in a line, numbered with integers from 1 to n from left to right. Marisa will hide a doll in one of t...
1278_C. Berry Jam_25836
Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n jars of strawberry and blueberry jam. All the 2n jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exa...
import os, sys, atexit from io import BytesIO, StringIO input = BytesIO(os.read(0, os.fstat(0).st_size)).readline _OUTPUT_BUFFER = StringIO() sys.stdout = _OUTPUT_BUFFER @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) t = int(input()) while t: n = int(input()) l = list(ma...
{ "input": [ "4\n6\n1 1 1 2 2 1 2 1 2 1 1 2\n2\n1 2 1 2\n3\n1 1 1 1 1 1\n2\n2 1 1 1\n", "4\n6\n1 1 1 1 2 1 2 1 2 1 1 2\n2\n1 2 1 2\n3\n1 1 1 1 1 1\n2\n2 1 1 1\n", "4\n6\n1 1 1 1 2 1 2 1 2 1 1 2\n2\n2 2 1 2\n3\n1 1 1 1 1 1\n2\n2 1 1 1\n", "4\n6\n1 1 1 1 2 1 2 1 2 1 1 2\n2\n2 2 1 2\n3\n1 1 1 1 2 1\n2\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were 2n jars of strawberry and blueberry jam. All the 2n jars a...
1321_D. Navigation System_25840
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along...
import sys from collections import deque def bfs(g,src,d,found): q=deque() q.append(src) d[src]=0 while q: rmv=q.popleft() for child in g[rmv]: if d[child]==-1: d[child]=d[rmv]+1 q.append(child) found[child]=1 elif d...
{ "input": [ "8 13\n8 7\n8 6\n7 5\n7 4\n6 5\n6 4\n5 3\n5 2\n4 3\n4 2\n3 1\n2 1\n1 8\n5\n8 7 5 2 1\n", "6 9\n1 5\n5 4\n1 2\n2 3\n3 4\n4 1\n2 6\n6 4\n4 2\n4\n1 2 3 4\n", "7 7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\n7\n1 2 3 4 5 6 7\n", "20 50\n20 3\n5 16\n1 3\n10 11\n10 15\n15 9\n20 9\n14 6\n16 5\n13 4\n11 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection ...
1340_C. Nastya and Unexpected Guest_25844
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of n lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place s...
import collections n,m=map(int,input().split()) arr=list(map(int,input().split())) arr=sorted(arr) g,r=map(int,input().split()) q=collections.deque() q.append((0,0,0)) checked=[[-1]*(g) for _ in range(m)] checked[0][0]=0 while len(q)!=0: v,t,cnt=q.popleft() if v!=m-1: cost1=arr[v+1]-arr[v] if t+cost1<=g: ...
{ "input": [ "13 4\n0 3 7 13\n9 9\n", "15 5\n0 3 7 14 15\n11 11\n", "15 5\n15 3 7 0 14\n11 11\n", "15 5\n15 14 7 3 0\n11 11\n", "100 5\n3 100 7 13 0\n99 5\n", "30 8\n0 1 9 10 23 24 26 30\n40 7\n", "4 5\n0 3 1 2 4\n2 1\n", "4 3\n0 4 2\n2 2\n", "4 5\n0 3 1 2 4\n1 1\n", "15 5\n15 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the gi...
1362_C. Johnny and Another Rating Drop_25848
The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the presented tasks are lovely, but don't show the truth about competitors' skills. The boy is now looking at the ratings of consecutive participan...
class Solution(): def __init__(self): for t in range(int(input())): n = int(input()) self.solve(n) def solve(self, n): b = format(n, "b") res = 0 while b: res += int(b, 2) b = b[:-1] print(res) Solution()
{ "input": [ "5\n5\n7\n11\n1\n2000000000000\n", "3\n576460752303423484\n576460752303423484\n576460752303423485\n", "1\n576460752303423487\n", "1\n1\n", "3\n576460752303423484\n576460752303423484\n361364603326849558\n", "1\n728718360623737783\n", "5\n5\n7\n11\n1\n2880328956332\n", "3\n5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The last contest held on Johnny's favorite competitive programming platform has been received rather positively. However, Johnny's rating has dropped again! He thinks that the present...
1382_D. Unmerge_25852
Let a and b be two arrays of lengths n and m, respectively, with no elements in common. We can define a new array merge(a,b) of length n+m recursively as follows: * If one of the arrays is empty, the result is the other array. That is, merge(∅,b)=b and merge(a,∅)=a. In particular, merge(∅,∅)=∅. * If both arrays a...
def isSubsetSum(set, n, sum): # The value of subset[i][j] will be # true if there is a # subset of set[0..j-1] with sum equal to i subset =([[False for i in range(sum + 1)] for i in range(n + 1)]) # If sum is 0, then answer is true for i in range(n + 1): ...
{ "input": [ "6\n2\n2 3 1 4\n2\n3 1 2 4\n4\n3 2 6 1 5 7 8 4\n3\n1 2 3 4 5 6\n4\n6 1 3 7 4 5 8 2\n6\n4 3 2 5 1 11 9 12 8 6 10 7\n", "1\n9\n2 1 4 3 7 6 5 10 9 8 13 12 11 18 17 16 15 14\n", "1\n16\n27 1 28 2 29 3 4 5 6 7 30 8 9 10 11 12 13 31 14 15 16 17 18 19 32 20 21 22 23 24 25 26\n" ], "output": [ ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let a and b be two arrays of lengths n and m, respectively, with no elements in common. We can define a new array merge(a,b) of length n+m recursively as follows: * If one of the a...
1425_D. Danger of Mad Snakes_25855
Mr. Chanek The Ninja is one day tasked with a mission to handle mad snakes that are attacking a site. Now, Mr. Chanek already arrived at the hills where the destination is right below these hills. The mission area can be divided into a grid of size 1000 × 1000 squares. There are N mad snakes on the site, the i'th mad s...
# from __future__ import print_function,division # range = xrange import sys input = sys.stdin.readline sys.setrecursionlimit(10**4) from sys import stdin, stdout from collections import defaultdict, Counter from functools import lru_cache M = 10**9+7 fact = [1]*(2001) def fac(n): if(n==0 or n==1): return ...
{ "input": [ "4 2 1\n1 1 10\n2 2 20\n2 3 30\n5 2 40\n", "4 1 0\n1 1 10\n1 999 30\n1 1000 40\n1 2 20\n", "1 1 0\n1 1 10\n", "20 17 0\n1 964 972064\n1 529 914335\n1 926 994468\n1 603 980092\n1 545 946148\n1 88 952185\n1 979 918633\n1 438 967889\n1 871 926455\n1 424 952048\n1 892 911827\n1 779 947360\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mr. Chanek The Ninja is one day tasked with a mission to handle mad snakes that are attacking a site. Now, Mr. Chanek already arrived at the hills where the destination is right below...
1447_B. Numbers Box_25859
You are given a rectangular grid with n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{ij} written in it. You can perform the following operation any number of times (possibly zero): * Choose any two adjacent cells and multiply the values in them b...
N = int(input()) for _ in range(N): n,m = map(int,input().split()) a = list() for i in range(n): a += list(map(int,input().split())) a.sort() minAbs = 101 countNegative = 0 for i in range(len(a)): if a[i] <= 0: countNegative += 1 a[i] = -a[i] ...
{ "input": [ "2\n2 2\n-1 1\n1 1\n3 4\n0 -1 -2 -3\n-1 -2 -3 -4\n-2 -3 -4 -5\n", "1\n3 3\n-3 -2 -1\n-3 -2 -1\n-3 -2 -1\n", "2\n2 2\n-1 1\n1 1\n3 4\n0 -2 -2 -3\n-1 -2 -3 -4\n-2 -3 -4 -5\n", "1\n3 3\n-3 -2 -2\n-3 -2 -1\n-3 -2 -1\n", "2\n2 2\n-1 1\n1 1\n3 4\n0 -1 -2 -3\n-1 -2 -3 -6\n-2 -3 -4 -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 a rectangular grid with n rows and m columns. The cell located on the i-th row from the top and the j-th column from the left has a value a_{ij} written in it. You can ...
1472_A. Cards for Friends_25863
For the New Year, Polycarp decided to send postcards to all his n friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size w × h, which can be cut into pieces. Polycarp can cut any sheet of paper w × h that he has in only two cases: * If w is even, then he can cut t...
import sys def cardsForFriends(): for _ in range(int(input())): w, h, n = map(int, input().split()) ans, tmp = 1, 1 while not w & 1: w //= 2 ans += tmp tmp *= 2 while not h & 1: h //= 2 ans += tmp tmp *= 2 if ans >= n: print('YES') else: print('NO') def main(): cardsForFriends() ...
{ "input": [ "5\n2 2 3\n3 3 2\n5 10 2\n11 13 1\n1 4 4\n", "1\n1024 1024 22212\n", "1\n8192 8192 67108864\n", "1\n8192 8192 1000000\n", "16\n8192 8192 67108864\n8192 8192 67108865\n8192 8192 70000000\n8192 8192 67108863\n1 1 1\n13 13 1\n1000 1000 100\n100 15 16\n157 185 95\n1257 1895 12\n1574 4984 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For the New Year, Polycarp decided to send postcards to all his n friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size w × h, whic...
1498_C. Planar Reflections_25867
Gaurang has grown up in a mystical universe. He is faced by n consecutive 2D planes. He shoots a particle of decay age k at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age k-1. If a particle has d...
import sys import collections import math import bisect import heapq inf = sys.maxsize def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def input(): return sys.stdin.readline().strip() mod = 1000000007 for _ in range(int(...
{ "input": [ "3\n1 1\n1 500\n500 250\n", "4\n2 3\n2 2\n3 1\n1 3\n", "37\n30 16\n22 37\n37 43\n25 31\n12 12\n15 30\n42 11\n29 28\n26 32\n12 32\n18 12\n11 48\n24 25\n15 40\n40 38\n17 12\n22 27\n35 32\n11 29\n35 24\n42 35\n21 14\n25 11\n16 14\n28 31\n49 45\n48 24\n28 20\n21 19\n43 49\n42 24\n14 21\n23 19\n19...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gaurang has grown up in a mystical universe. He is faced by n consecutive 2D planes. He shoots a particle of decay age k at the planes. A particle can pass through a plane directly, ...
1520_B. Ordinary Numbers_25871
Let's call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbers. For a given number n, find the number of ordinary numbers among the numbers from 1 to n. Input The first line contains one integer...
""" Don't see the standings during the contest!!! you will lose motivation. """ # ---------------------------------------------------Import Libraries--------------------------------------------------- import sys import time import os from math import sqrt, log, log2, ceil, log10, gcd, floor, pow, sin, cos, tan, pi...
{ "input": [ "6\n1\n2\n3\n4\n5\n100\n", "1\n69696969\n", "1\n999999999\n", "1\n124545606\n", "6\n1\n2\n3\n4\n8\n100\n", "1\n35666167\n", "1\n55658120\n", "1\n55372544\n", "1\n6576553\n", "1\n6953437\n", "1\n10835641\n", "1\n18319508\n", "1\n23542989\n", "1\n4512...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbe...
1547_G. How Many Paths?_25875
You are given a directed graph G which can contain loops (edges from a vertex to itself). Multi-edges are absent in G which means that for all ordered pairs (u, v) exists at most one edge from u to v. Vertices are numbered from 1 to n. A path from u to v is a sequence of edges such that: * vertex u is the start of ...
import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) def scc_dfs1(s, links, status, postorder): stack = [s] status[s] = 0 while stack: v = stack[-1] limit = len(links[v]) while status[v] < limit: u = links[v][status[v]] status[v] += 1 ...
{ "input": [ "5\n\n6 7\n1 4\n1 3\n3 4\n4 5\n2 1\n5 5\n5 6\n\n1 0\n\n3 3\n1 2\n2 3\n3 1\n\n5 0\n\n4 4\n1 2\n2 3\n1 4\n4 3\n", "1\n\n6 6\n1 2\n2 3\n3 4\n3 5\n4 2\n4 6\n", "1\n\n1 0\n", "1\n\n6 7\n1 2\n2 3\n2 4\n3 1\n4 5\n5 6\n6 4\n", "1\n\n10 14\n1 4\n2 1\n2 5\n4 3\n4 9\n5 1\n5 9\n6 5\n6 10\n7 8\n8 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a directed graph G which can contain loops (edges from a vertex to itself). Multi-edges are absent in G which means that for all ordered pairs (u, v) exists at most one ...
291_B. Command Line Arguments_25890
The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the behavior. Please make sure you've read the statement attentively and use it as a formal document. In the Pindows operating system a strings ...
import sys # f = sys.stdin # f = open("input.txt", "r") a = input() i = 0 while i < len(a): if a[i] == '"': end = a.index('"', i+1) print("<", a[i+1:end], ">", sep="") i = end+1 elif a[i] != " " != '"' and (a[i-1] == " " or i-1 < 0): try: end = a.index(" ", i+1) ...
{ "input": [ "\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"\n", "firstarg second \"\" \n", "\" \" \n", "j \n", "B\n", "\"7\" \"W \" \"\" \"\" \"a \" \"\" \"\" \"\" y \n", "A\n", "\"\"\n", "\"RUn.exe O\" \"\" \" 2ne, \" two! . \" \"\n", "\"\" \"\" \". \" \"A\" \"\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The problem describes the properties of a command line. The description somehow resembles the one you usually see in real operating systems. However, there are differences in the beha...
362_B. Petya and Staircases_25900
Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either jump to the next one or jump over one or two stairs at a time. But some stairs are too dirty and Petya doesn't want to step on them. No...
import sys n, m = map(int, input().split()) if m != 0: data = [int(el) for el in input().split()] data.sort() if data[0] == 1 or data[-1] == n: print('NO') sys.exit() for e in range(m - 2): if data[e] + data[e + 1] + data[e + 2] == data[e] * 3 + 3: print('NO') ...
{ "input": [ "10 5\n2 4 5 7 9\n", "10 5\n2 4 8 3 6\n", "100 3\n96 98 99\n", "2 0\n", "1000000000 10\n1 2 3 5 6 8 9 123 874 1230\n", "10 6\n2 3 5 6 8 9\n", "4 3\n2 3 4\n", "1000000000 10\n2 3 5 6 8 9 123 874 1230 1000000000\n", "10 9\n2 3 4 5 6 7 8 9 10\n", "10 1\n10\n", "8 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little boy Petya loves stairs very much. But he is bored from simple going up and down them — he loves jumping over several stairs at a time. As he stands on some stair, he can either...
385_E. Bear in the Field_25904
Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x, y). Each cell of the field contains grow...
#Simple non-optimized class of matrices. Used with small dense matrices. import functools import itertools import math class NotAMatrixError(Exception): pass class MatrixSizeError(Exception): def __init__(self, s1, s2): print('sizes do not match : ', s1, ', ', s2) class NotSquareError(Exception): ...
{ "input": [ "1 1 1 -1 -1 2\n", "5 1 2 0 1 2\n", "487599125 469431740 316230350 -77 57 18\n", "321575625 2929581 31407414 -40 -44 920902537044\n", "5928 1508 4358 75 -4 794927060433551549\n", "642762664 588605882 1 -47 82 8\n", "1 1 1 1 1 0\n", "147834 6 2565 15 -35 166779\n", "910...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Our bear's forest has a checkered field. The checkered field is an n × n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to...
456_B. Fedya and Maths_25912
Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression: (1n + 2n + 3n + 4n) mod 5 for given value of n. Fedya managed to complete the task. Can you? Note that given number n can be extremely large (e.g. it can exceed any integer type of your programming language). Input The si...
n=int(input()) if n==0: answer=4 else: a=1 if n%4==0: a+=6+1 elif n%4==1: a+=2+3 elif n%4==2: a+=4+9 else: a+=8+7 if n%2==0: a+=6 else: a+=4 answer=a%5 print(answer)
{ "input": [ "124356983594583453458888889\n", "4\n", "464\n", "13\n", "2\n", "64\n", "85447\n", "578487\n", "2563\n", "1\n", "192329\n", "51494\n", "10\n", "71752\n", "26232\n", "83\n", "247\n", "584660\n", "7854\n", "8\n", "971836\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression: (1n + 2n + 3n + 4n) mod 5 for given value of n. Fedya managed to complete the task. Ca...
552_A. Vanya and Table_25922
Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right. In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur mult...
n = int(input()) matrix = [[0] * 100 for i in range(100)] for t in range(n): x1, y1, x2, y2 = map(int, input().split()) for i in range(y1 - 1, y2): for j in range(x1 - 1, x2): matrix[i][j] += 1 answer = 0 for row in matrix: answer += sum(row) print(answer)
{ "input": [ "2\n1 1 3 3\n1 1 3 3\n", "2\n1 1 2 3\n2 2 3 3\n", "5\n4 11 20 20\n6 11 20 16\n5 2 19 15\n11 3 18 15\n3 2 14 11\n", "1\n100 100 100 100\n", "1\n1 1 1 1\n", "5\n1 1 1 100\n1 1 1 100\n1 1 1 100\n1 1 1 100\n1 1 1 100\n", "1\n1 1 1 100\n", "3\n1 1 1 1\n1 2 1 2\n1 3 1 3\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from ...
579_A. Raising Bacteria_25926
You are a lover of bacteria. You want to raise some bacteria in a box. Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, every bacterium in the box will split into two bacteria. You hope to see exactly x bacteria in the box at some moment. What is the minimu...
xStr = input() x = int(xStr) put = 0 while x>0: if x%2 == 0: x = x//2 continue else: put = put+1 x = x//2 print(put)
{ "input": [ "8\n", "5\n", "343000816\n", "999999993\n", "697681824\n", "999999994\n", "10\n", "999999990\n", "536870912\n", "9\n", "4\n", "7\n", "999999998\n", "2\n", "954746654\n", "999999992\n", "999999996\n", "536870910\n", "999999995\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are a lover of bacteria. You want to raise some bacteria in a box. Initially, the box is empty. Each morning, you can put any number of bacteria into the box. And each night, ev...
5_E. Bindian Signalizing_25930
Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a watchman, who watched the neighbourhood day and night. In case of any danger the watchman could make a fire on the hill. One watchman coul...
n = int(input()) hill = tuple(map(int, input().split())) pairs = 0 highest, at = max((h, k) for k, h in enumerate(hill)) last = highest count = 0 p = list() push = p.append pop = p.pop for at in range(at - 1, at - n, -1): current = hill[at] while current > last: pairs += count last, count = pop(...
{ "input": [ "5\n1 2 4 5 3\n", "3\n2118 2118 2118\n", "5\n763 763 763 763 763\n", "10\n5938 4836 5938 5938 4836 4836 2780 2780 1495 4836\n", "10\n4972 4972 4972 4858 4858 4972 4972 4972 4858 4972\n", "5\n3581 3581 305 305 3581\n", "4\n2440 2440 2440 2440\n", "10\n8097 8097 8097 8097 80...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Everyone knows that long ago on the territory of present-day Berland there lived Bindian tribes. Their capital was surrounded by n hills, forming a circle. On each hill there was a wa...
622_C. Not Equal on a Segment_25934
You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi. For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi. Input The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of elements in a and the number of queries. The second...
import sys n, m = map(int, sys.stdin.readline().split()) a = list(map(int, sys.stdin.readline().split())) assert(len(a) == n) prev = [-1] * n l = r = 0 while l < n: while r < n and a[l] == a[r]: r += 1 # [l, r) --> a[l] for i in range(l, r): prev[i] = l - 1 l = r for i in range(m): ...
{ "input": [ "6 4\n1 2 1 1 3 5\n1 4 1\n2 6 2\n3 4 1\n3 4 2\n", "5 1\n3 2 4 5 5\n1 3 3\n", "10 10\n1 1 1 1 1 2 1 1 1 1\n1 9 1\n6 7 1\n2 4 1\n7 8 1\n1 3 1\n10 10 1\n3 5 1\n6 7 1\n1 10 1\n6 6 1\n", "2 6\n1 1\n1 1 1\n1 1 2\n1 2 1\n1 2 2\n2 2 1\n2 2 2\n", "2 1\n1 3\n1 2 2\n", "6 1\n3 2 4 3 3 3\n1 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given array a with n integers and m queries. The i-th query is given with three integers li, ri, xi. For the i-th query find any position pi (li ≤ pi ≤ ri) so that api ≠ xi. ...
670_E. Correct Bracket Sequence Editor_25940
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, ...
n, m, p = [int(x) for x in input().split()] A = input().rstrip() B = input().rstrip() pair = [0] * n stack = [] for (i, c) in enumerate(A): if c == '(': stack.append(i) else: j = stack.pop() pair[i] = j pair[j] = i start = 0 pointer = p - 1 left = list(range(-1, n-1)) right = l...
{ "input": [ "12 5 3\n((()())(()))\nRRDLD\n", "8 8 8\n(())()()\nLLLLLLDD\n", "8 4 5\n(())()()\nRDLD\n", "88 33 12\n(())((((())()((()())())())(((())())(())()())(()))((()())())())(((())()())(())()())()()()\nLLLRRLRDRDRLDDLLRDLLDRLRDDLDRDLRR\n", "14 8 13\n((())())((()))\nDLRLLRLR\n", "28 13 16\n(...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to ...
696_B. Puzzles_25944
Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city h...
n = int(input()) if n ==1: print(1) exit(0) l = list(map(int,input().split())) w = [[]for i in range(n)] sz = [1]*n for i in range(n-1): w[l[i]-1].append(i+1) for i in range(n-1,-1,-1): for j in range(len(w[i])): sz[i]+=sz[w[i][j]] ans = [0]*n for i in range(n): for j in range(len(w[i])): ...
{ "input": [ "7\n1 2 1 1 4 4\n", "12\n1 1 2 2 4 4 3 3 1 10 8\n", "10\n1 2 2 2 5 4 6 5 6\n", "2\n1\n", "8\n1 1 2 2 3 6 1\n", "85\n1 1 2 2 4 6 1 3 6 3 3 11 9 14 12 5 8 11 16 19 12 17 2 19 1 24 6 2 6 6 24 3 20 1 1 1 17 8 4 25 31 32 39 12 35 23 31 26 46 9 37 7 5 23 41 41 39 9 11 54 36 54 28 15 25 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 through n and n - 1 roads between them. Cities and roads of USC form a rooted tree (Barney's n...
717_C. Potions Homework_25948
Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen each other after a long time. The sun is shining, birds are singing, flowers are blooming, and their Potions class teacher, professor Snipe ...
fuck = int(input()) a = sorted(int(input()) for _ in range(fuck)) print(sum(a[i]*a[-i-1] for i in range(fuck))%10007) # Surprise motherfucker
{ "input": [ "2\n1\n3\n", "2\n1\n4\n", "2\n0\n4\n", "2\n1\n1\n", "2\n1\n8\n", "2\n1\n9\n", "2\n1\n2\n", "2\n1\n17\n", "2\n1\n6\n", "2\n1\n10\n", "2\n1\n18\n", "2\n2\n18\n", "2\n4\n18\n", "2\n6\n18\n", "2\n6\n23\n", "2\n6\n30\n", "2\n6\n39\n", "2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Harry Water, Ronaldo, Her-my-oh-knee and their friends have started a new school year at their MDCS School of Speechcraft and Misery. At the time, they are very happy to have seen eac...
761_D. Dasha and Very Difficult Problem_25953
Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci = bi - ai. About sequences a and b we know that their elements are in the range from l to...
read = lambda: map(int, input().split()) n, l, r = read() a = list(read()) p = list(read()) d = [i for i in range(n)] d.sort(key = lambda x: p[x]) cur = l - a[d[0]] b = [0] * n for ind in d: b[ind] = a[ind] + cur if b[ind] < l: cur = l - a[ind] b[ind] = l cur += 1 if max(b) > r: print(-...
{ "input": [ "4 2 9\n3 4 8 9\n3 2 1 4\n", "6 1 5\n1 1 1 1 1 1\n2 3 5 4 1 6\n", "5 1 5\n1 1 1 1 1\n3 1 5 4 2\n", "6 3 7\n6 7 5 5 5 5\n2 1 4 3 5 6\n", "5 1 5\n1 1 1 1 1\n1 2 3 4 5\n", "2 1 1000000000\n1000000000 1\n2 1\n", "50 10 15\n13 14 12 14 12 15 13 10 11 11 15 10 14 11 14 12 11 10 10 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th...
784_F. Crunching Numbers Just for You_25957
You are developing a new feature for the website which sells airline tickets: being able to sort tickets by price! You have already extracted the tickets' prices, so there's just the last step to be done... You are given an array of integers. Sort it in non-descending order. Input The input consists of a single line...
a = input()[2:] n = [0 for i in range(1000000)] g = 123 for i in range(130000): g *= 3 a = list(map(int, a.split())) x = [0 for _ in range(len(a))] for i in range(len(x)): x[i] = min(a) a.remove(x[i]) for o in x: print(o, end=' ')
{ "input": [ "3 3 1 2\n", "10 54 100 27 1 33 27 80 49 27 6\n", "10 54 100 28 1 33 27 80 49 27 6\n", "10 54 100 28 1 33 27 80 49 6 6\n", "10 54 100 28 1 3 27 80 49 6 6\n", "10 54 100 27 1 33 27 80 49 21 6\n", "3 3 1 4\n", "10 90 100 28 1 33 27 80 49 6 6\n", "10 54 100 27 2 33 27 80 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are developing a new feature for the website which sells airline tickets: being able to sort tickets by price! You have already extracted the tickets' prices, so there's just the ...
876_B. Divisiblity of Differences_25967
You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible. Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in mu...
n, k , m = map(int, input().split()) a = list(map(int, input().split())) ai =[[]*100000 for i in range(100000)] h = 0 z = 0 v = 0 for i in range(n): h =a[i]%m ai[h].append(a[i]) if len(ai[h])==k: print('Yes') print(*ai[h]) v +=1 break if v == 0: print('No')
{ "input": [ "3 3 3\n1 8 4\n", "4 3 5\n2 7 7 7\n", "3 2 3\n1 8 4\n", "15 8 10\n216175135 15241965 611723934 987180005 151601897 403701727 533996295 207637446 875331635 46172555 604086315 350146655 401084142 156540458 982110455\n", "2 2 100000\n0 1\n", "3 2 3\n1 2 3\n", "4 3 3\n5 1 4 7\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 multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible....
8_B. Obsession with Robots_25971
The whole world got obsessed with robots,and to keep pace with the progress, great Berland's programmer Draude decided to build his own robot. He was working hard at the robot. He taught it to walk the shortest path from one point to another, to record all its movements, but like in many Draude's programs, there was a ...
__author__ = "runekri3" def cells_touching(cell_1, cell_2): abs_x = abs(cell_1[0] - cell_2[0]) if abs_x > 1: return False abs_y = abs(cell_1[1] - cell_2[1]) if abs_y > 1: return False if not (abs_x == 1 and abs_y == 1): return True movements = input() total_movements = le...
{ "input": [ "RRUULLDD\n", "LLUUUR\n", "DDUL\n", "LLLLLLLLRRRRDDDDDDDUUUUUU\n", "UULLDLUR\n", "URRRLULUURURLRLLLLULLRLRURLULRLULLULRRUU\n", "RDRLL\n", "RRRRRRRRRRRDDDDDDDDDDDDDDDDDDDRRRRRRRRRRRRRRRRRRRUUUUUUUUUUUUUUUUUUULLLLLLLLLLLLLLLLLLUUUUUUUUUUU\n", "DDDLLLLLLLDDDDDDDRRRRRRRUUU...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The whole world got obsessed with robots,and to keep pace with the progress, great Berland's programmer Draude decided to build his own robot. He was working hard at the robot. He tau...
922_A. Cloning Toys_25975
Imp likes his plush toy a lot. <image> Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies. Initially, Imp has only on...
# Imports ############################################## # Input (x, y) = tuple(map(lambda x:int(x), input().split())) ############################################## # Main code yes = 'Yes' no = 'No' res = yes if y == 0: res = no elif y == 1 and x != 0: res = no elif x < y-1: res = no elif ((x-y+1) % ...
{ "input": [ "4 2\n", "1000 1001\n", "6 3\n", "630719418 9872663\n", "963891449 582938127\n", "1000000000 999999999\n", "0 2\n", "481994122 678374097\n", "779351061 773124120\n", "141148629 351661795\n", "771581370 589752968\n", "422447052 772330542\n", "225821895 8...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Imp likes his plush toy a lot. <image> Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one ...
94_B. Friends_25979
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people" Igor just c...
e = [[False] * 5 for i in range(5)] for i in range(int(input())): a, b = map(int, input().split()) e[a - 1][b - 1] = e[b - 1][a - 1] = True for a in range(3): for b in range(a + 1, 4): for c in range(b + 1, 5): if len({e[a][b], e[a][c], e[b][c]}) == 1: print('WIN') ...
{ "input": [ "4\n1 3\n2 3\n1 4\n5 3\n", "5\n1 2\n2 3\n3 4\n4 5\n5 1\n", "1\n3 5\n", "2\n5 3\n1 3\n", "5\n1 3\n1 4\n2 1\n4 3\n1 5\n", "3\n4 1\n4 5\n2 1\n", "5\n1 5\n3 4\n1 4\n5 4\n4 2\n", "6\n3 2\n2 4\n3 1\n3 5\n5 2\n1 2\n", "5\n4 1\n5 1\n2 3\n2 5\n1 2\n", "1\n5 4\n", "5\n3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught h...