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
p02886 AtCoder Beginner Contest 143 - TAKOYAKI FESTIVAL 2019_8024
It's now the season of TAKOYAKI FESTIVAL! This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i. As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points. There are \frac{N...
n = int(input()) l = list(map(int, input().split())) print(sum(l[i]*sum(l[i+1:]) for i in range(len(l)-1)))
{ "input": [ "3\n3 1 2", "7\n5 0 7 8 3 3 2", "3\n3 1 3", "7\n5 0 7 8 6 3 2", "3\n3 1 5", "7\n5 0 7 10 6 3 2", "3\n1 1 5", "7\n5 0 7 2 6 3 2", "3\n1 1 8", "7\n5 -1 7 2 6 3 2", "3\n0 1 8", "7\n5 -1 7 2 12 3 2", "3\n0 1 2", "7\n1 -1 7 2 12 3 2", "3\n0 1 1", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It's now the season of TAKOYAKI FESTIVAL! This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i. As...
p03021 AtCoder Grand Contest 034 - Complete Compress_8027
You are given a tree with N vertices numbered 1, 2, ..., N. The i-th edge connects Vertex a_i and Vertex b_i. You are also given a string S of length N consisting of `0` and `1`. The i-th character of S represents the number of pieces placed on Vertex i. Snuke will perform the following operation some number of times:...
import sys input = sys.stdin.readline n = int(input()) s = "Q"+input() ab = [list(map(int,input().split())) for i in range(n-1)] graph = [[] for i in range(n+1)] deg = [0 for i in range(n+1)] for a,b in ab: graph[a].append(b) graph[b].append(a) deg[a] += 1 deg[b] += 1 leaf = set() for i in range(1,n+1): if de...
{ "input": [ "7\n0010101\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7", "7\n0010110\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7", "2\n01\n1 2", "2\n01\n2 2", "7\n0011101\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7", "7\n0111101\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7", "7\n0110110\n1 1\n2 3\n1 4\n1 5\n2 6\n6 7", "7\n0110110\n1 2\n2 3\n1 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a tree with N vertices numbered 1, 2, ..., N. The i-th edge connects Vertex a_i and Vertex b_i. You are also given a string S of length N consisting of `0` and `1`. The ...
p03161 Educational DP Contest - Frog 2_8031
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. 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 the following: Stone i + 1, i + 2, \ld...
N,K=map(int,input().split()) h=list(map(int,input().split())) dp=[10**10]*(N)#dp[i]→i+1番目の柱にくる最小コスト dp[0]=0 for i in range(N-1): dp[i+1]=min(dp[i+1-j]+abs(h[i+1]-h[i+1-j]) for j in range(1,min(i+1,K)+1)) print(dp[N-1])
{ "input": [ "2 100\n10 10", "5 3\n10 30 40 50 20", "10 4\n40 10 20 70 80 10 20 70 80 60", "3 1\n10 20 10", "2 100\n10 9", "5 3\n10 30 40 50 24", "3 4\n40 10 20 70 80 10 20 70 80 60", "3 1\n0 20 10", "3 4\n40 10 17 70 80 10 20 70 80 60", "2 100\n7 10", "10 4\n40 9 20 70 80 ...
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. There is a frog who is initially on Stone 1. He will repeat the following ac...
p03304 SoundHound Inc. Programming Contest 2018 -Masters Tournament- - Ordinary Beauty_8035
Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d. For example, when d=1, the beauty of the sequence (3, 2, 3, 10, 9) is 3. There are a total of n^m sequences of length m where each element is an integer between 1 and n (inclusi...
n,m,d=map(int,input().split()) if d!=0: print((2*(n-d)*(m-1))/(n**2)) else: print(((n-d)*(m-1))/(n**2))
{ "input": [ "2 3 1", "1000000000 180707 0", "1000000000 180707 1", "1000000000 241440 1", "1001000000 241440 5", "1001000000 353960 8", "0001000000 353960 8", "0001000000 353960 15", "1001100000 353960 15", "1001100100 149885 23", "1001101100 149885 23", "1011101100 14...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let us define the beauty of a sequence (a_1,... ,a_n) as the number of pairs of two adjacent elements in it whose absolute differences are d. For example, when d=1, the beauty of the ...
p03464 AtCoder Grand Contest 020 - Ice Rink Game_8039
An adult game master and N children are playing a game on an ice rink. The game consists of K rounds. In the i-th round, the game master announces: * Form groups consisting of A_i children each! Then the children who are still in the game form as many groups of A_i children as possible. One child may belong to at m...
from math import ceil def solve(rounds): mx, mn = 3, 2 if rounds[-1] != 2: return (-1,) for r in reversed(rounds[:-1]): if mx < r: return (-1,) mn = ceil(mn / r) * r mx = mx // r * r + r - 1 if mn > mx: return (-1,) return mn, mx k = int(input(...
{ "input": [ "5\n3 4 100 3 2", "10\n2 2 2 2 2 2 2 2 2 2", "4\n3 4 3 2", "5\n3 7 100 3 2", "10\n2 3 2 2 2 2 2 2 2 2", "4\n3 3 2 2", "4\n4 3 2 2", "4\n3 5 3 2", "4\n1 1 2 2", "4\n3 4 2 2", "5\n3 7 100 5 2", "5\n3 7 100 2 2", "5\n3 4 100 3 3", "5\n3 7 101 3 2", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: An adult game master and N children are playing a game on an ice rink. The game consists of K rounds. In the i-th round, the game master announces: * Form groups consisting of A_i ch...
p03623 AtCoder Beginner Contest 071 - Meal Delivery_8043
Snuke lives at position x on a number line. On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery. Snuke decided to get food delivery from the closer of stores A and B. Find out which store is closer to Snuke's residence. Here, the distance between two points s and...
a,b,c=map(int,input().split()) print('A' if abs(a-b)<abs(c-a) else 'B')
{ "input": [ "1 999 1000", "5 2 7", "1 999 0000", "1 381 1110", "5 1 7", "1 999 0100", "10 1 7", "1 999 0101", "10 2 7", "1 999 0001", "10 1 9", "1 381 0001", "10 2 9", "1 381 0101", "17 2 9", "1 381 0111", "23 2 9", "1 381 0110", "23 3 9", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke lives at position x on a number line. On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery. Snuke decided to get food deli...
p03783 AtCoder Regular Contest 070 - NarrowRectangles_8046
AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the vertical range of [i-1,i] and the horizontal range of [l_i,r_i], as shown in the following figure: <image> AtCoDeer will move these rect...
N = int(input()) P = [list(map(int, input().split())) for i in range(N)] INF = 10**18 from heapq import heappush, heappop l0, r0 = P[0] L = [-l0+1] R = [l0-1] s = t = 0 res = 0 for i in range(N-1): l0, r0 = P[i] l1, r1 = P[i+1] s += (r1 - l1); t += (r0 - l0) if -s-L[0] <= l1-1 <= t+R[0]: ...
{ "input": [ "3\n2 5\n4 6\n1 4", "5\n123456 789012\n123 456\n12 345678901\n123456 789012\n1 23", "3\n1 3\n5 7\n1 3", "1\n1 400", "5\n999999999 1000000000\n1 2\n314 315\n500000 500001\n999999999 1000000000", "3\n2 5\n4 6\n2 4", "5\n123456 789012\n17 456\n12 345678901\n123456 789012\n1 23", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoDeer the deer found N rectangle lying on the table, each with height 1. If we consider the surface of the desk as a two-dimensional plane, the i-th rectangle i(1≤i≤N) covers the v...
p03952 AtCoder Grand Contest 006 - Median Pyramid Easy_8050
We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally. The pyramid is built so that the blocks at the centers of the steps are aligned vertically. <image> A pyramid with N=4 steps Snuke wrote a per...
import sys sys.setrecursionlimit(10 ** 8) ini = lambda: int(sys.stdin.readline()) inm = lambda: map(int, sys.stdin.readline().split()) inl = lambda: list(inm()) ins = lambda: sys.stdin.readline().rstrip() debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw)) n, x = inm() def solv...
{ "input": [ "4 4", "2 1", "1 1", "2 2", "3 3", "7 7", "6 6", "5 5", "8 8", "4 1", "2 3", "3 1", "0 1", "7 1", "5 1", "9 1", "-1 1", "16 1", "-2 1", "13 1", "11 1", "0 -1", "10 1", "15 1", "17 1", "4 7", "-3 1"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a pyramid with N steps, built with blocks. The steps are numbered 1 through N from top to bottom. For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally. The pyra...
p00042 A Thief_8054
A thief sneaked into a museum with a lot of treasures with only one large furoshiki. There are many things I want to steal, but the weight that the furoshiki can withstand is limited, and if it exceeds this, the furoshiki will tear. Therefore, the thief must consider a combination of treasures that will not break the p...
""" 0-1ナップザック wi...i個目の重さ vi...i個目の価値 2次元 dp[i][j]...i個目までの宝で重さjの時の価値の最大 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - wi] + vi) (wi <= j) dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) (wi > j) 1次元 dp[j]...重さjの時の価値の最大 dp[j] = max(dp[j], dp[j - wi] + vi) (wi <= j <= w) wi <= j <...
{ "input": [ "50\n5\n60,10\n100,20\n120,30\n210,45\n10,4\n50\n5\n60,10\n100,20\n120,30\n210,45\n10,4\n0", "50\n5\n60,10\n100,20\n120,30\n310,45\n10,4\n50\n5\n60,10\n100,20\n120,30\n210,45\n10,4\n0", "50\n5\n60,10\n100,20\n110,30\n310,45\n10,4\n50\n5\n60,10\n100,20\n120,30\n210,45\n00,4\n0", "50\n5\n60...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A thief sneaked into a museum with a lot of treasures with only one large furoshiki. There are many things I want to steal, but the weight that the furoshiki can withstand is limited,...
p00174 Badminton_8058
It's been a while since I played with A, B, and C. Mr. A and Mr. B became players, and Mr. C became a referee and played a badminton singles game. The rules decided by the three people are as follows. * 3 Play the game. * The person who gets 11 points first wins the game. * The first serve of the first game starts wit...
import sys while True: a=[] for i in range(3): z=str(input()) if z=="0": sys.exit() a.append(z) A,B,C=a[0],a[1],a[2] A=A[1:]+B[0] B=B[1:]+C[0] C=C[1:] print(A.count("A"), A.count("B")) print(B.count("A"), B.count("B")) d,e=C.count("A"),C.count("B")...
{ "input": [ "ABAABBBAABABAAABBAA\nAABBBABBABBAAABABABAAB\nBABAABAABABABBAAAB\nAABABAAABBAABBBABAA\nAAAAAAAAAAA\nABBBBBBBBBB\n0", "AABBAAABABAABBBAABA\nAABBBABBABBAAABABABAAB\nBABAABAABABABBAAAB\nAABABAAABBAABBBABAA\nAAAAAAAAAAA\nABBBBBBBBBB\n0", "ABAABBBAABABAAABBAA\nAABBBABBABBAAABABABAAB\nBABAABAABABAB...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It's been a while since I played with A, B, and C. Mr. A and Mr. B became players, and Mr. C became a referee and played a badminton singles game. The rules decided by the three peopl...
p00330 Word_8062
The smallest unit of data handled by a computer is called a bit, and the amount of information that represents multiple bits together is called a word. Currently, many computers process one word as 32 bits. For a computer that represents one word in 32 bits, create a program that outputs the amount of data W given in ...
# coding: utf-8 # Your code here! W = int(input()) print(W*32)
{ "input": [ "4", "3", "0", "5", "1", "2", "-1", "8", "-2", "14", "6", "26", "-3", "46", "-4", "80", "-7", "12", "-11", "24", "-10", "40", "-18", "67", "-35", "10", "-19", "18", "-21", "-5", "-6...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The smallest unit of data handled by a computer is called a bit, and the amount of information that represents multiple bits together is called a word. Currently, many computers proce...
p00503 Fish_8065
problem To the west of the Australian continent is the wide Indian Ocean. Marine researcher JOI is studying the properties of N species of fish in the Indian Ocean. For each type of fish, a rectangular parallelepiped habitat range is determined in the sea. Fish can move anywhere in their habitat, including boundaries...
# copy n, k = map(int, input().split()) plst = [] xlst = [] ylst = [] dlst = [] for _ in range(n): x1, y1, d1, x2, y2, d2 = map(int, input().split()) plst.append((x1, y1, d1, x2, y2, d2)) xlst.append(x1) xlst.append(x2) ylst.append(y1) ylst.append(y2) dlst.append(d1) dlst.append(d2) xl...
{ "input": [ "3 2\n30 50 0 50 70 100\n10 20 20 70 90 60\n40 60 20 90 90 70", "3 2\n10 50 0 50 70 100\n10 20 20 70 90 60\n40 60 20 90 90 70", "3 2\n10 50 0 50 70 100\n10 20 20 70 90 60\n40 60 20 90 90 98", "3 2\n10 50 0 50 70 000\n10 20 20 70 90 60\n40 60 20 90 90 98", "3 2\n20 50 0 50 70 000\n10 3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem To the west of the Australian continent is the wide Indian Ocean. Marine researcher JOI is studying the properties of N species of fish in the Indian Ocean. For each type of...
p00687 Unable Count_8069
> I would, if I could, > If I couldn't how could I? > I couldn't, without I could, could I? > Could you, without you could, could ye? > Could ye? could ye? > Could you, without you could, could ye? It is true, as this old rhyme says, that we can only DO what we can DO and we cannot DO what we cannot DO. Changing ...
while True: n,a,b = map(int, input().split()) if n==0: break ok = [1]*(n+1) for i in range(b): for j in range(0,n-i*a+1,b): ok[i*a+j] = 0 print(sum(ok))
{ "input": [ "10 2 3\n10 2 5\n100 5 25\n0 0 0", "10 2 3\n10 2 5\n100 5 40\n0 0 0", "10 1 3\n10 2 5\n100 5 40\n0 0 0", "10 2 3\n14 2 5\n100 6 25\n0 0 0", "10 1 3\n10 2 5\n110 5 40\n0 0 0", "15 1 3\n10 2 6\n110 5 40\n0 0 0", "15 2 3\n10 2 6\n110 5 40\n0 0 0", "15 2 3\n13 2 6\n110 5 40\n0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: > I would, if I could, > If I couldn't how could I? > I couldn't, without I could, could I? > Could you, without you could, could ye? > Could ye? could ye? > Could you, without y...
p00829 Leaky Cryptography_8073
The ACM ICPC judges are very careful about not leaking their problems, and all communications are encrypted. However, one does sometimes make mistakes, like using too weak an encryption scheme. Here is an example of that. The encryption chosen was very simple: encrypt each chunk of the input by flipping some bits acco...
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**10 mod = 998244353 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdi...
{ "input": [ "8\n1 1 1 1 1 1 1 1 8\n3 2 3 2 3 2 3 2 6\n3 4 4 7 7 b a 2 2e\ne1 13 ce 28 ca 6 ab 46 a6d\nb08 49e2 6128 f27 8cf2 bc50 7380 7fe1 723b\n4eba eb4 a352 fd14 6ac1 eed1 dd06 bb83 392bc\nef593c08 847e522f 74c02b9c 26f3a4e1 e2720a01 6fe66007\n7a4e96ad 6ee5cef6 3853cd88\n60202fb8 757d6d66 9c3a9525 fbcd7983 82...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The ACM ICPC judges are very careful about not leaking their problems, and all communications are encrypted. However, one does sometimes make mistakes, like using too weak an encrypti...
p01093 Selection of Participants of an Experiment_8078
Selection of Participants of an Experiment Dr. Tsukuba has devised a new method of programming training. In order to evaluate the effectiveness of this method, he plans to carry out a control experiment. Having two students as the participants of the experiment, one of them will be trained under the conventional metho...
while True: n = int(input()) if n == 0: break score = list(map(int,input().split())) score.sort() ans = max(score) for i in range(1,n): aa = abs(score[i] - score[i-1]) if ans > aa: ans = aa print(ans)
{ "input": [ "5\n10 10 10 10 10\n5\n1 5 8 9 11\n7\n11 34 83 47 59 29 70\n0", "5\n10 10 1 10 10\n5\n1 5 8 9 11\n7\n11 34 83 47 59 29 70\n0", "5\n10 10 1 10 10\n5\n1 5 8 9 11\n7\n11 34 83 47 59 29 48\n0", "5\n10 10 1 10 10\n5\n1 5 8 5 11\n7\n11 34 83 47 59 29 48\n0", "5\n10 10 1 10 10\n5\n1 5 8 5 11...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Selection of Participants of an Experiment Dr. Tsukuba has devised a new method of programming training. In order to evaluate the effectiveness of this method, he plans to carry out ...
p01545 House Moving_8085
Taro has decided to move. Taro has a lot of luggage, so I decided to ask a moving company to carry the luggage. Since there are various weights of luggage, I asked them to arrange them in order from the lightest one for easy understanding, but the mover left the luggage in a different order. So Taro tried to sort the l...
import math class FenwickTree: def __init__(self, a_list, f, default): # 0-indexed self.N = len(a_list) self.bit = a_list[:] self.f = f self.default = default for _ in range(self.N, 1 << (math.ceil(math.log(self.N, 2)))): self.bit.append(self.default) ...
{ "input": [ "8\n6 2 1 3 8 5 4 7", "5\n1 5 3 2 4", "7\n1 2 3 4 5 6 7", "4\n1 4 2 3", "7\n1 2 5 4 3 6 7", "4\n2 4 2 2", "4\n1 4 3 2", "7\n1 3 2 4 5 6 7", "4\n3 4 1 2", "8\n7 2 1 3 8 5 4 6", "7\n1 2 8 1 3 6 7", "5\n2 5 3 1 4", "8\n5 2 1 3 8 6 4 7", "7\n2 1 5 4 3 6...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Taro has decided to move. Taro has a lot of luggage, so I decided to ask a moving company to carry the luggage. Since there are various weights of luggage, I asked them to arrange the...
p01701 North North West_8089
Problem Statement We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north and west, and northnorthwest is between north and northwest. In this problem, we describe more detailed direction between north and west a...
import re while 1: s=input() if s=='#':break s=re.split('[w|n]',s)[1:];c=1 d=90 if s[-1]=='est' else 0 for x in s[:-1][::-1]: d*=2 d+=90if x=='est'else-90 c*=2 while d&1==0 and c&1==0:d//=2;c//=2 if c==1:print(d) else:print(d,c,sep='/')
{ "input": [ "north\nwest\nnorthwest\nnorthnorthwest\nwestwestwestnorth\n#" ], "output": [ "0\n90\n45\n45/2\n315/4" ] }
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Statement We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north an...
p01845 Curry Making_8093
Curry making As the ACM-ICPC domestic qualifying is approaching, you who wanted to put more effort into practice decided to participate in a competitive programming camp held at a friend's house. Participants decided to prepare their own meals. On the first night of the training camp, the participants finished the da...
Q=[] while True: A=list(map(int,input().split())) if A==[0,0,0,0]: break else: Q.append(A) for k in range(len(Q)): for i in range(100000): P=(Q[k][0]+i*Q[k][3])/Q[k][2]-Q[k][1] if P>=0: print(i) break
{ "input": [ "10 5 3 4\n2 5 2 3\n91 13 7 62\n10 1 3 5\n20 100 2 20\n2 14 7 1\n0 0 0 0", "10 5 3 4\n2 5 2 3\n150 13 7 62\n10 1 3 5\n20 100 2 20\n2 14 7 1\n0 0 0 0", "10 5 3 4\n1 5 2 3\n150 13 7 62\n10 1 3 5\n20 100 3 20\n2 14 7 1\n0 0 0 0", "10 5 3 1\n2 5 2 3\n150 13 7 62\n10 1 3 5\n20 100 2 20\n2 14 7...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Curry making As the ACM-ICPC domestic qualifying is approaching, you who wanted to put more effort into practice decided to participate in a competitive programming camp held at a fr...
p01981 Change of the Era Name_8096
Revised The current era, Heisei, will end on April 30, 2019, and a new era will begin the next day. The day after the last day of Heisei will be May 1, the first year of the new era. In the system developed by the ACM-ICPC OB / OG Association (Japanese Alumni Group; JAG), the date uses the Japanese calendar (the Japa...
import sys def solve(g, y, m, d): y = int(y) m = int(m) d = int(d) if y == 31 and m >= 5: print('?', y-30, m, d) elif y >= 32: print('?', y-30, m, d) else: print(g, y, m, d) while True: s = input() if s == "#": break solve(*map(str, s.split())...
{ "input": [ "HEISEI 1 1 8\nHEISEI 31 4 30\nHEISEI 31 5 1\nHEISEI 99 12 31\nHEISEI 38 8 30\nHEISEI 98 2 22\nHEISEI 2 3 26\nHEISEI 28 4 23\n#", "HEISEI 1 1 8\nHEISEI 31 4 30\nIESIEH 31 5 1\nHEISEI 99 12 31\nHEISEI 38 8 30\nHEISEI 98 2 22\nHEISEI 2 3 26\nHEISEI 28 4 23\n#", "HEISEI 1 1 8\nHEISEI 31 4 30\nIE...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Revised The current era, Heisei, will end on April 30, 2019, and a new era will begin the next day. The day after the last day of Heisei will be May 1, the first year of the new era....
p02127 AABABCAC_8099
Problem Given the strings $ s $, $ t $. First, let the string set $ A = $ {$ s $}, $ B = \ phi $. At this time, I want to perform the following operations as much as possible. operation step1 Perform the following processing for all $ u ∊ A $. 1. From all subsequences of $ u $ (not necessarily contiguous), choose ...
s = input() t = " " + input() line = [1] next = 1 t_size = len(t) flag = False for i in s: if t[next] == i: line[0] += 1 next = line[0] line += [0] j = 0 while line[j] == t_size: line[j] = 0 next = line[j + 1] = line[j + 1] + 1 j += 1 ...
{ "input": [ "abaabbabaabaabbabbabaabbab\nab", "AABABCAC\nA", "AbCdEfG\naBcDeFg", "abaabbabaab`abbabbabaabbab\nab", "AABABCAC\nB", "AbCdEfG\naBcDeFh", "abaabbbbaab`abbabbabaabaab\nab", "AACABCAC\nB", "AbGdEfC\naBcDeFh", "abaabbbbaab`abbabbabaabaab\n`b", "AACABCAC\nC", "...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem Given the strings $ s $, $ t $. First, let the string set $ A = $ {$ s $}, $ B = \ phi $. At this time, I want to perform the following operations as much as possible. opera...
p02268 Binary Search_8102
You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Notes Constraints * Elements in S is sorted in ascending order * n ≤ 100000 * q ≤ 50000 * 0 ≤ an element in S ≤ 109 * 0 ≤ an element in T ≤ 109 ...
import bisect n=int(input()) S=list(map(int,input().split())) q=int(input()) T=list(map(int,input().split())) ans=0 for t in T: x=bisect.bisect_left(S,t) if S[x]==t: ans+=1 print(ans)
{ "input": [ "5\n1 1 2 2 3\n2\n1 2", "3\n1 2 3\n1\n5", "5\n1 2 3 4 5\n3\n3 4 1", "5\n1 0 2 2 3\n2\n1 2", "3\n2 2 3\n1\n5", "5\n1 0 4 2 3\n2\n1 1", "5\n1 2 3 4 1\n5\n2 4 1", "5\n1 2 3 4 5\n3\n1 4 1", "5\n1 0 4 2 3\n2\n1 2", "3\n1 2 3\n1\n4", "5\n1 4 3 4 5\n3\n1 4 1", "3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Notes Co...
p02415 Toggling Cases_8106
Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string. Constraints * The length of the input string < 1200 Input A string is given in a line. Output Print the converted string in a line. Note that you do not need to convert any characters other than alphabetical lett...
t = input() t = t.swapcase() print(t)
{ "input": [ "fAIR, LATER, OCCASIONALLY CLOUDY.", "f@IR, LATER, OCCASIONALLY CLOUDY.", "f@IR, LATER, YLLANOISACCO CLOUDY.", "f@IR, LATER, OLLANOISACCY CLOUDY.", "f@IR, LATER, OLKANOISACCY CLOUDY.", ",RI@f LATER, OLKANOISACCY CLOUDY.", ",RI@f KATER, OLKANOISACCY CLOUDY.", ",RI@g KATER, ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which converts uppercase/lowercase letters to lowercase/uppercase for a given string. Constraints * The length of the input string < 1200 Input A string is given i...
1000_D. Yet Another Problem On a Subsequence_8116
The sequence of integers a_1, a_2, ..., a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the sequences [3, 7, 8], [2, 5, 4, 1], [0] — are not. A sequence of integers is called good if it can be divided into a positive number of good arrays....
import sys input=sys.stdin.readline N=10**4+10 mod=998244353 g1=[1,1] g2=[1,1] inv=[0,1] for i in range(2,N+1): g1.append((g1[-1]*i)%mod) inv.append((-inv[mod%i]*(mod//i))%mod) g2.append((g2[-1]*inv[-1])%mod) def comb(m,r,mod): if r<0 or r>m: return 0 return (g1[m]*g2[r]*g2[m-r])%mod n=int(i...
{ "input": [ "4\n1 1 1 1\n", "3\n2 1 1\n", "1\n1\n", "1\n0\n", "1\n2\n", "4\n1 1 2 1\n", "3\n2 2 1\n", "4\n1 0 2 1\n", "4\n-2 1 5 0\n", "1\n-1\n", "1\n3\n", "1\n4\n", "4\n1 0 1 1\n", "3\n0 2 1\n", "1\n-2\n", "1\n6\n", "3\n0 4 1\n", "1\n-3\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The sequence of integers a_1, a_2, ..., a_k is called a good array if a_1 = k - 1 and a_1 > 0. For example, the sequences [3, -1, 44, 0], [1, -99] are good arrays, and the sequences [...
106_A. Card Game_8125
There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want. To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", ...
a = input() b,c = input().split() lst = ["6", "7", "8", "9", "T", "J", "Q", "K" , "A"] if b[1] ==c[1]: print('YES' if lst.index(b[0]) > lst.index(c[0]) else 'NO') elif b[1] == a: print('YES') else: print('NO')
{ "input": [ "C\n7H AS\n", "H\nQH 9S\n", "S\n8D 6D\n", "H\nQD KD\n", "D\n9S 8S\n", "H\nTH 8H\n", "D\nAC KS\n", "H\nAH KH\n", "H\nAD KD\n", "D\n8S 6C\n", "H\nAC QC\n", "H\nJC JH\n", "H\n7D 6D\n", "H\nJS TS\n", "C\n6C 6D\n", "H\n9D 8S\n", "C\nJC AH\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules ...
1090_M. The Pleasant Walk_8129
There are n houses along the road where Anya lives, each one is painted in one of k possible colors. Anya likes walking along this road, but she doesn't like when two adjacent houses at the road have the same color. She wants to select a long segment of the road such that no two adjacent houses have the same color. H...
# n, k = map(int, input().split()) l = list(map(int, input().split())) m = [] f = [] s=set(l) if len(s)== 1: print("1") else: for i in range(len(l) - 1): if l[i] == l[i + 1]: f.append(m) # print(len(f)) # print(m) m = [] else: if le...
{ "input": [ "8 3\n1 2 3 3 2 1 2 2\n", "3 3\n1 2 3\n", "4 2\n1 1 2 2\n", "100 100\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 4...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n houses along the road where Anya lives, each one is painted in one of k possible colors. Anya likes walking along this road, but she doesn't like when two adjacent houses...
110_C. Lucky Sum of Digits_8133
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with...
n=int(input()) from collections import Counter c=Counter([]) c[1]=[-1,2] c[2]=[-2,4] c[3]=[-3,6] c[4]=[0,1] c[5]=[-1,3] c[6]=[-2,5] a=n%7 s=n//7 f=0 if a==0: print ('7'*s) else: s+=c[a][0] f+=c[a][1] if s<0: if n%4==0: print ('4'*(n//4)) else: print (-1) else:...
{ "input": [ "11\n", "10\n", "999980\n", "9854\n", "10000\n", "474\n", "999990\n", "10000\n", "1000\n", "999990\n", "10417\n", "1000000\n", "3840\n", "7\n", "4\n", "11000\n", "1024\n", "800000\n", "111\n", "888887\n", "999998\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. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, ...
1157_F. Maximum Balanced Circle_8139
There are n people in a row. The height of the i-th person is a_i. You can choose any subset of these people and try to arrange them into a balanced circle. A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than 1. For example, let heights of chosen peop...
import sys from collections import namedtuple from itertools import groupby input = sys.stdin.readline def main(): Data = namedtuple('Data', ['arr', 'l', 'r']) _ = int(input()) a = list(map(int, input().split())) f = [ [0, 0] for i in range(max(a) + 2) ] for x in a: f[x][0] = x f...
{ "input": [ "3\n5 1 4\n", "7\n4 3 5 1 2 2 1\n", "5\n3 7 5 1 5\n", "7\n2 2 3 2 1 2 2\n", "4\n200000 200000 200000 200000\n", "1\n200000\n", "3\n200000 200000 200000\n", "2\n200000 200000\n", "1\n1\n", "10\n2 3 6 7 4 6 3 2 6 6\n", "7\n4 3 5 1 2 2 1\n", "5\n20 21 22 23 24...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n people in a row. The height of the i-th person is a_i. You can choose any subset of these people and try to arrange them into a balanced circle. A balanced circle is such...
1215_C. Swap Letters_8148
Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b". Monocarp wants to make these two strings s and t equal to each other. He can do the following operation any number of times: choose an index pos_1 in the string s, choose an index pos_2 in the string...
a=int(input()) s=input() r=input() cmap=[] tmap=[] for i in range(len(s)): if(s[i]=='a' and r[i]=='b'): cmap.append(i) elif(s[i]=='b' and r[i]=='a'): tmap.append(i) r=len(cmap) t=len(tmap) if(((r%2==1 and t%2==0) or (r%2==0 and t%2==1))): print(-1) else: total=r//2+t//2 if(r...
{ "input": [ "1\na\nb\n", "8\nbabbaabb\nabababaa\n", "4\nabab\naabb\n", "10\naaaaaaaaaa\nbbbbbbbbbb\n", "101\nbabbbbabbbbaabbbbbbbabbbababababbbbbbbbabaababbababbaaabbababbbbbbbbbaabbbababbbbbabbbbbabbbaabbbaabb\nbbabbbbbbabbbbbbbbbbbabbbbbbbbbbabbabbbbbbbbbbbabbababbbabbabbbbbbbbbbbbbaabbbabbbbab...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Monocarp has got two strings s and t having equal length. Both strings consist of lowercase Latin letters "a" and "b". Monocarp wants to make these two strings s and t equal to each...
1238_E. Keyboard Purchase_8152
You have a password which you often type — a string s of length n. Every character of this string is one of the first m lowercase Latin letters. Since you spend a lot of time typing it, you want to buy a new keyboard. A keyboard is a permutation of the first m Latin letters. For example, if m = 3, then there are six ...
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline INF = 10**9 n, m = [int(item) for item in input().split()] s = input().rstrip() count = [[0] * m for _ in range(m)] ord_a = ord("a") for c1, c2 in zip(s, s[1:]): c1 = ord(c1) - ord_a c2 = ord(c2) - ord_a if c1 != c2: count[c1][c2]...
{ "input": [ "15 4\nabacabadabacaba\n", "6 3\naacabc\n", "6 4\naaaaaa\n", "44 20\nbcdefghijklmnopqrstabcdefghijklmnopqrstatata\n", "44 20\ncdefghijklmnopqrstabcdefghijklmnopqrstabtata\n", "44 20\natatbatsrqponmlkjihgfedcbatsrqponmlkjihgfedc\n", "4 4\naaaaaa\n", "44 20\natatbatsrqponmlk...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a password which you often type — a string s of length n. Every character of this string is one of the first m lowercase Latin letters. Since you spend a lot of time typing ...
1256_F. Equalizing Two Strings_8156
You are given two strings s and t both of length n and both consisting of lowercase Latin letters. In one move, you can choose any length len from 1 to n and perform the following operation: * Choose any contiguous substring of the string s of length len and reverse it; * at the same time choose any contiguous ...
import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #from bisect import bisect_left as bl, bisect_right as br, insort from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data():...
{ "input": [ "4\n4\nabcd\nabdc\n5\nababa\nbaaba\n4\nasdf\nasdg\n4\nabcd\nbadc\n", "4\n4\nabcd\nabdc\n5\nababa\nbaaba\n4\nasdf\nasdg\n4\nabdd\nbadc\n", "4\n4\nabcd\nabdc\n5\nababa\nabaab\n4\nasdf\nasdg\n4\nabcd\nbadc\n", "4\n4\nabcd\nabdc\n5\naaabc\nbabaa\n4\nfdsa\nasdg\n4\nabdd\nbadc\n", "4\n4\nda...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two strings s and t both of length n and both consisting of lowercase Latin letters. In one move, you can choose any length len from 1 to n and perform the following op...
1280_A. Cut and Paste_8160
We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i. There is one cursor. The cursor's location ℓ is denoted by an integer in \{0, …, |s|\}, with the following meaning: * If ℓ = 0, then the cursor...
for _ in range(int(input())): x = int(input()) s = input() l = 0 while(len(s)<x): s += s[l+1:]*(int(s[l])-1) l+=1 count=len(s) for k in range(l,x): count+=((count-k-1)*(int(s[k])-1)) count%= 1000000007 print(count)
{ "input": [ "4\n5\n231\n7\n2323\n6\n333\n24\n133321333\n", "1\n1000000\n221\n", "9\n1500\n1212\n1500\n1221\n1500\n122\n1500\n12121\n1500\n22\n1500\n1111112111111112\n1500\n1111111111221111111\n1500\n111111122\n1500\n11111121111121111111\n", "1\n1000000\n22\n", "1\n1000000\n1212\n", "1\n100000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We start with a string s consisting only of the digits 1, 2, or 3. The length of s is denoted by |s|. For each i from 1 to |s|, the i-th character of s is denoted by s_i. There is o...
12_E. Start of the season_8164
Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to n - 1, main diagonal should contain onl...
n=int(input()) a=[[0 for j in range(n)] for i in range(n)] for i in range(n-1): for j in range(n-1): if i!=j: a[i][j]=(i+j)%n+int(i+j>=n) a[i][n-1]=((n-1+2*i)%n+int(n-1+2*i>=n))%n+int(n-1+2*i>2*(n-1)) for i in range(n-1): a[n-1][i]=((n-1+2*i)%n+int(n-1+2*i>=n))%n+int(n-1+2*i>2*(n-1)) for...
{ "input": [ "2\n", "4\n", "16\n", "12\n", "66\n", "66\n", "40\n", "100\n", "80\n", "8\n", "128\n", "12\n", "10\n", "144\n", "14\n", "96\n", "88\n", "32\n", "20\n", "18\n", "40\n", "88\n", "18\n", "16\n", "128\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size n × n (n is even number). God...
1324_A. Yet Another Tetris Problem_8168
You are given some Tetris field consisting of n columns. The initial height of the i-th column of the field is a_i blocks. On top of these columns you can place only figures of size 2 × 1 (i.e. the height of this figure is 2 blocks and the width of this figure is 1 block). Note that you cannot rotate these figures. Yo...
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode...
{ "input": [ "4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 11\n1\n100\n", "1\n12\n2 2 2 2 1 2 2 2 2 2 2 2\n", "1\n1\n1\n", "1\n3\n1 1 2\n", "1\n1\n2\n", "4\n3\n1 1 3\n4\n1 1 2 1\n2\n11 14\n1\n100\n", "4\n3\n1 1 3\n4\n1 1 2 2\n2\n11 9\n1\n100\n", "4\n3\n2 1 3\n4\n1 1 2 2\n2\n7 9\n1\n100\n", "1\n3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given some Tetris field consisting of n columns. The initial height of the i-th column of the field is a_i blocks. On top of these columns you can place only figures of size 2...
1365_A. Matrix Game_8174
Ashish and Vivek play a game on a matrix consisting of n rows and m columns, where they take turns claiming cells. Unclaimed cells are represented by 0, while claimed cells are represented by 1. The initial state of the matrix is given. There can be some claimed cells in the initial state. In each turn, a player must ...
t = int(input()) for _ in range(t): n,m = map(int,input().split()) l = [] s = 0 t1,t2 = 0,0 for i in range(n): l1 = list(map(int,input().split())) l.append(l1) for i in range(n): f = 1 for j in range(m): if l[i][j] == 1: f = 0 if f: t1 += 1 for i in range(m): f = 1 for j in range(n): ...
{ "input": [ "4\n2 2\n0 0\n0 0\n2 2\n0 0\n0 1\n2 3\n1 0 1\n1 1 0\n3 3\n1 0 0\n0 0 0\n1 0 0\n", "4\n2 2\n0 0\n0 0\n2 2\n0 0\n0 1\n2 3\n1 0 0\n1 1 0\n3 3\n1 0 0\n0 0 0\n1 0 0\n", "4\n2 2\n0 0\n0 0\n2 2\n1 0\n0 1\n2 3\n1 0 0\n1 1 0\n3 3\n1 0 0\n0 0 0\n1 0 0\n", "4\n2 2\n0 0\n0 0\n2 2\n1 0\n0 1\n2 3\n0 0 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ashish and Vivek play a game on a matrix consisting of n rows and m columns, where they take turns claiming cells. Unclaimed cells are represented by 0, while claimed cells are repres...
1385_A. Three Pairwise Maximums_8178
You are given three positive (i.e. strictly greater than zero) integers x, y and z. Your task is to find positive integers a, b and c such that x = max(a, b), y = max(a, c) and z = max(b, c), or determine that it is impossible to find such a, b and c. You have to answer t independent test cases. Print required a, b a...
t = int(input()) input_list = [[0,0,0] for i in range(t)] for i in range(t): x,y,z = input().split() input_list[i][0] = int(x) input_list[i][1] = int(y) input_list[i][2] = int(z) ans_list = ["NO" for i in range(t)] output_list = [[0,0,0] for i in range(t)] for i in range(t): x,y,z = input_list[i] ...
{ "input": [ "5\n3 2 3\n100 100 100\n50 49 49\n10 30 20\n1 1000000000 1000000000\n", "1\n127869 127869 127869\n", "1\n78738 78738 78738\n", "2\n5 7 7\n6 7 3\n", "5\n3 2 3\n100 100 100\n50 49 49\n10 30 20\n1 1000000000 1000000000\n", "1\n12789 12789 12789\n", "1\n78788 78788 78788\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given three positive (i.e. strictly greater than zero) integers x, y and z. Your task is to find positive integers a, b and c such that x = max(a, b), y = max(a, c) and z = m...
1406_D. Three Sequences_8182
You are given a sequence of n integers a_1, a_2, …, a_n. You have to construct two sequences of integers b and c with length n that satisfy: * for every i (1≤ i≤ n) b_i+c_i=a_i * b is non-decreasing, which means that for every 1<i≤ n, b_i≥ b_{i-1} must hold * c is non-increasing, which means that for every 1<...
import sys import atexit class Fastio: def __init__(self): self.ibuf = bytes() self.obuf = bytearray() self.pil = 0 self.pir = 0 def load(self): self.ibuf = self.ibuf[self.pil:] self.ibuf += sys.stdin.buffer.read(131072) self.pil = 0 self.pir = l...
{ "input": [ "4\n2 -1 7 3\n2\n2 4 -3\n3 4 2\n", "6\n-9 -10 -9 -6 -5 4\n3\n2 6 -9\n1 2 -10\n4 6 -3\n", "1\n0\n2\n1 1 -1\n1 1 -1\n", "1\n-101\n1\n1 1 5\n", "1\n-78\n1\n1 1 5\n", "4\n2 0 7 3\n2\n2 4 -3\n3 4 2\n", "6\n-9 -10 -9 -6 -4 4\n3\n2 6 -9\n1 2 -10\n4 6 -3\n", "6\n-9 -10 -9 -7 -4 4\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a sequence of n integers a_1, a_2, …, a_n. You have to construct two sequences of integers b and c with length n that satisfy: * for every i (1≤ i≤ n) b_i+c_i=a_i ...
1427_E. Xum_8186
You have a blackboard and initially only an odd number x is written on it. Your goal is to write the number 1 on the blackboard. You may write new numbers on the blackboard with the following two operations. * You may take two numbers (not necessarily distinct) already on the blackboard and write their sum on the ...
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): print("500") cnt = 500 x = int(input()) if x % 4 == 3: print(str(x) + " + " + str(x)) print(str(2 * x) + " + " + str(x)) cnt -= 2 x *= 3 xCpy = x bigX = x while xCpy != 1: ...
{ "input": [ "3\n", "123\n", "804351\n", "9\n", "123789\n", "986115\n", "983041\n", "500001\n", "999995\n", "999999\n", "524295\n", "282147\n", "158421\n", "524289\n", "525313\n", "1025\n", "7\n", "123461\n", "8193\n", "999997\n", "11...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a blackboard and initially only an odd number x is written on it. Your goal is to write the number 1 on the blackboard. You may write new numbers on the blackboard with the ...
1450_E. Capitalism_8189
A society can be represented by a connected, undirected graph of n vertices and m edges. The vertices represent people, and an edge (i,j) represents a friendship between people i and j. In society, the i-th person has an income a_i. A person i is envious of person j if a_j=a_i+1. That is if person j has exactly 1 more...
n,m = map(int,input().split());edge = [[] for i in range(n)];cost = [[10**18*(i!=j) for j in range(n)] for i in range(n)] for _ in range(m):u,v,b = map(int,input().split());edge[u-1].append(v-1);edge[v-1].append(u-1);cost[u-1][v-1] = 1;cost[v-1][u-1] = (1 if not b else -1) used = [False]*n;cond = [0]*n;used[0] = True;s...
{ "input": [ "1 0\n", "4 4\n1 2 1\n2 3 0\n3 4 1\n4 1 1\n", "6 6\n1 2 0\n3 2 0\n2 5 0\n6 5 1\n6 3 0\n2 4 1\n", "50 49\n46 6 1\n35 27 1\n9 40 1\n43 36 1\n7 45 1\n47 46 1\n49 43 1\n4 48 1\n16 34 1\n13 22 1\n41 47 1\n6 20 1\n29 7 1\n48 14 1\n8 23 1\n21 30 1\n14 19 1\n23 31 1\n45 37 1\n19 11 1\n28 38 1\n17...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A society can be represented by a connected, undirected graph of n vertices and m edges. The vertices represent people, and an edge (i,j) represents a friendship between people i and ...
1474_C. Array Destruction_8193
You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an easy task, but it turned out that you should follow some rules: 1. In the beginning, you select any positive integer x. 2. Then you ...
from sys import stdin #import math input = stdin.readline q = int(input()) for _ in range(q): n = int(input()) a = list(map(int, input().split())) a.sort() l = a[-1] a.pop() for i in range(2*n-1): d = {} for ii in range(2*n - 1): if ii == i: continue ...
{ "input": [ "4\n2\n3 5 1 2\n3\n1 1 8 8 64 64\n2\n1 1 2 4\n5\n1 2 3 4 5 6 7 14 3 11\n", "6\n4\n5 6 9 13 36 42 47 60\n3\n1 2 3 4 5 6\n3\n1 2 3 3 5 6\n4\n5 22 42 47 49 54 69 123\n2\n1 1 2 2\n3\n1000000 1000000 990000 10000 900000 90000\n", "4\n2\n3 5 1 2\n3\n1 1 8 8 64 64\n2\n1 1 2 4\n5\n1 2 3 4 5 6 7 14 3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You found a useless array a of 2n positive integers. You have realized that you actually don't need this array, so you decided to throw out all elements of a. It could have been an e...
1523_E. Crypto Lights_8199
<image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Initially, all lights on William's device are turned off. At the beginning of a new iteration the device randomly, with a uniform distributio...
def divisors(M): d=[] i=1 while M>=i**2: if M%i==0: d.append(i) if i**2!=M: d.append(M//i) i=i+1 return d def popcount(x): x = x - ((x >> 1) & 0x55555555) x = (x & 0x33333333) + ((x >> 2) & 0x33333333) x = (x + (x >> 4)) & 0x0f0f0f0f ...
{ "input": [ "3\n3 2\n15 2\n40 15\n", "10\n100000 99995\n100000 100000\n100000 99998\n100000 99997\n100000 99997\n100000 99997\n100000 100000\n100000 99997\n100000 99999\n100000 99998\n", "10\n91147 3\n60637 60633\n58274 58270\n81624 62\n59542 17\n76280 76277\n78239 3\n84713 84710\n58143 58143\n85720 5\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: <image> To monitor cryptocurrency exchange rates trader William invented a wonderful device consisting of n lights arranged in a row. The device functions in the following way: Init...
155_D. Colliders_8203
By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated. In 2312 there was a startling discovery:...
n, m = map(int, input().split()) n += 1 s = [[] for i in range(n)] for j in range(2, n, 2): s[j] = [2] for i in range(3, n, 2): if s[i]: continue for j in range(i, n, i): s[j].append(i) p, d, r = {}, set(), [''] * m for j in range(m): t = input() i = int(t[2: ]) if t[0] == '+': ...
{ "input": [ "10 10\n+ 6\n+ 10\n+ 5\n- 10\n- 5\n- 6\n+ 10\n+ 3\n+ 6\n+ 3\n", "100 1\n+ 51\n", "100000 1\n+ 12345\n", "50 50\n+ 14\n+ 4\n+ 20\n+ 37\n+ 50\n+ 46\n+ 19\n- 20\n+ 25\n+ 47\n+ 10\n+ 6\n+ 34\n+ 12\n+ 41\n- 47\n+ 9\n+ 22\n+ 28\n- 41\n- 34\n+ 47\n+ 40\n- 12\n+ 42\n- 9\n- 4\n+ 15\n- 15\n+ 27\n+ ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: By 2312 there were n Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to n. However, scientists did not know w...
177_D2. Encrypting Messages_8207
The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY Cup contestants for help. A message is a sequence of n integers a1, a2, ..., an. Encryption uses a key which is a sequence of m integers...
n, m, c = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) s, k = 0, n - m for i in range(n): if i < m: s += b[i] a[i] = (a[i] + s) % c if i >= k: s -= b[i - k] print(' '.join(str(i) for i in a))
{ "input": [ "3 1 5\n1 2 3\n4\n", "4 3 2\n1 1 1 1\n1 1 1\n", "20 15 17\n4 9 14 11 15 16 15 4 0 10 7 12 10 1 8 6 7 14 1 13\n6 3 14 8 8 11 16 4 5 9 2 13 6 14 15\n", "5 2 7\n0 0 1 2 4\n3 5\n", "20 15 17\n4 9 14 11 15 16 15 4 0 10 7 12 10 1 8 6 7 14 1 13\n6 3 14 8 8 11 16 4 5 9 2 13 6 14 19\n", "5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Smart Beaver from ABBYY invented a new message encryption method and now wants to check its performance. Checking it manually is long and tiresome, so he decided to ask the ABBYY ...
222_A. Shooshuns and Sequence _8213
One day shooshuns found a sequence of n integers, written on a blackboard. The shooshuns can perform one operation with it, the operation consists of two steps: 1. Find the number that goes k-th in the current sequence and add the same number to the end of the sequence; 2. Delete the first number of the current s...
n,k=map(int,input().split()) l=list(map(int,input().split())) f=l[k-1] if l[k-1:].count(f) == n-k+1: r = 0 for i in range(k-2,-1,-1): if l[k-1] != l[i]: break r+=1 print(len(l[:k-1-r])) else: print(-1)
{ "input": [ "3 2\n3 1 1\n", "3 1\n3 1 1\n", "10 3\n3 2 1 1 1 1 1 1 1 1\n", "10 10\n1 2 3 4 5 6 7 8 9 10\n", "10 2\n2 1 1 1 1 1 1 1 1 1\n", "10 1\n1 2 1 1 1 1 1 1 1 1\n", "10 2\n1 2 1 1 1 1 1 1 1 1\n", "2 1\n1 1\n", "20 9\n1 2 3 4 5 6 7 8 9 10 1 1 1 1 1 1 1 1 1 1\n", "20 10\n1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day shooshuns found a sequence of n integers, written on a blackboard. The shooshuns can perform one operation with it, the operation consists of two steps: 1. Find the number ...
270_E. Flawed Flow_8219
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of n vertices and m edges. Vertices are numbered from 1 to n. Vertices 1 and n being the source and the sink respectively. However, his...
import sys from collections import deque DST_VERTEX = 0 EDGE_CAP = 1 EDGE_ID = 2 EDGE_DIR = 3 def bfs(flow, graph, n, m): dirs = [-1 for _ in range(m)] q = deque() q.append(0) q_size = 1 while q_size > 0: cur_node = q.popleft() q_size -= 1 for i in range(len(graph[cur_node]...
{ "input": [ "4 5\n1 2 10\n1 3 10\n2 3 5\n4 2 15\n3 4 5\n", "3 3\n3 2 10\n1 2 10\n3 1 5\n", "3 2\n1 2 1\n2 3 1\n", "2 1\n1 2 1\n", "10 17\n9 1 8\n7 10 1\n5 4 4\n1 10 1\n3 10 1\n10 5 1\n6 3 6\n10 4 1\n4 6 5\n7 5 3\n2 10 1\n9 3 7\n9 10 1\n8 10 1\n10 6 1\n2 7 2\n2 8 1\n", "3 3\n3 1 10000\n2 1 100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists...
294_B. Shaass and Bookshelf_8223
Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights. <image> Shaass puts the books...
n = int(input()) t = [] for i in range(n): e = list(map(int,input().split())) t.append(e) size = 0 size2 = 0 for i in t: size += i[0] size2 += i[1] matrix = [[-1000]*(size+1) for i in range(n)] for i in range(n): matrix[i][0] = 0 for i in range(n): for j in range(1,size+1): if t[i][0...
{ "input": [ "3\n1 10\n2 1\n2 4\n", "5\n1 12\n1 3\n2 15\n2 5\n2 1\n", "1\n1 2\n", "1\n1 1\n", "100\n2 2\n1 2\n1 5\n1 5\n1 2\n1 4\n2 3\n1 2\n1 5\n2 1\n2 2\n2 4\n1 2\n2 3\n1 1\n1 1\n2 5\n2 3\n2 2\n1 2\n2 1\n2 2\n1 5\n2 1\n2 4\n1 4\n1 4\n2 2\n1 1\n2 4\n1 4\n2 4\n1 2\n2 3\n2 3\n1 5\n1 5\n2 3\n1 4\n1 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages'...
365_B. The Fibonacci Segment_8231
You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r). Let's define len([l, r]) = r - l + 1, len([l, r]) is the length of the segment [l, r]. Segment [l1, r1], is longer than segment [l2, r2], if len([l1, r1]) > len([l2, r2]). Your task is to find a ...
import sys n = int(input()) a = list(map(int, input().split())) if n <= 2: print(n) sys.exit() best = 0 mem = a[:2] cur = 0 for i in a[2:]: if mem[0] + mem[1] == i: cur += 1 else: if cur > best: best = cur cur = 0 mem = mem[1:] + [i] if cur > best: best = cur print(best+2)
{ "input": [ "5\n1 1 1 1 1\n", "10\n1 2 3 5 8 13 21 34 55 89\n", "6\n10 20 30 10 40 50\n", "2\n1 3\n", "1\n1000\n", "2\n0 0\n", "4\n0 0 0 0\n", "1\n0\n", "5\n0 0 0 0 0\n", "5\n8 9 17 26 43\n", "3\n0 0 0\n", "12\n1 2 3 5 8 13 0 1 1 2 3 5\n", "2\n7 1\n", "51\n1 0 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have array a1, a2, ..., an. Segment [l, r] (1 ≤ l ≤ r ≤ n) is good if ai = ai - 1 + ai - 2, for all i (l + 2 ≤ i ≤ r). Let's define len([l, r]) = r - l + 1, len([l, r]) is the le...
389_A. Fox and Number Game_8235
Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible. ...
from sys import stdin, stdout, stderr, setrecursionlimit setrecursionlimit(100000) def debug (*e): if not __debug__: print(*e, file=stderr) def dd(*vals): import inspect, re frame = inspect.getframeinfo(inspect.stack()[1][0]) vs = re.search("dd\((.+)\)", frame.code_context[0]).group(1).split("...
{ "input": [ "2\n12 18\n", "3\n2 4 6\n", "5\n45 12 27 30 18\n", "2\n1 2\n", "5\n2 3 5 8 18\n", "2\n1 1\n", "100\n82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes ...
409_D. Big Data_8239
Little Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (Div.2). The scientists rejoiced at the gift and decided to put some important facts to this array. Here are the first few of the facts: ...
x = int(input()) v = [1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0] print(v[x-1])
{ "input": [ "7\n", "1\n", "2\n", "11\n", "15\n", "13\n", "14\n", "16\n", "12\n", "5\n", "9\n", "8\n", "4\n", "6\n", "3\n", "10\n" ], "output": [ "0", "1", "0", "0", "1", "1", "0", "0", "0", "0", "1", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Petya wanted to give an April Fools Day present to some scientists. After some hesitation he decided to give them the array that he got as a present in Codeforces Round #153 (D...
45_A. Codecraft III_8245
Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly k months. He looked at the calendar and learned that at the moment is the month number s. Vasya immediately got interested in what month Codecraft III will appear. Help him understand t...
month = str(input()) k = int(input()) m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] j = m.index(month) while k != 0: if j == 11: j = 0 k -= 1 else: j += 1 k -= 1 print(m[j])
{ "input": [ "May\n24\n", "November\n3\n", "June\n97\n", "August\n0\n", "July\n33\n", "January\n0\n", "July\n4\n", "December\n1\n", "November\n47\n", "May\n44\n", "July\n77\n", "July\n2\n", "January\n11\n", "June\n1\n", "December\n49\n", "April\n0\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today Vasya visited a widely known site and learned that the continuation of his favourite game Codecraft II will appear after exactly k months. He looked at the calendar and learned ...
529_D. Social Network_8253
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=[0]*20002 b=[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", "10 3 30000\n00:06:54\n00:42:06\n03:49:45\n04:38:35\n05:33:30\n05:51:46\n10:46:34\n14:34:59\n14:40:06\n14:53:13\n", "1 1 86400\n00:00:00\n", "5 2 40000\n06:30:57\n07:27:25\n09:10:21\n11:05:03\n12:42:37\n", ...
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...
555_A. Case of Matryoshkas_8257
Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls are numbered from 1 to n. A matryoshka with a smaller number can be ...
n,k = map(int,input().split(" ")) i=0 res=k sh=0 while i<k: i+=1 mas=list(map(int,input().split(" ")))[1:] if mas[0]==1: j=0 while j<(len(mas)-1): if(mas[j]+1!=mas[j+1]): break else: j+=1 sh+=len(mas)-j-1 res+...
{ "input": [ "3 2\n2 1 2\n1 3\n", "7 3\n3 1 3 7\n2 2 5\n2 4 6\n", "3 2\n1 2\n2 1 3\n", "100 3\n45 1 2 3 4 5 6 7 8 9 19 21 24 27 28 30 34 35 37 39 40 41 42 43 46 47 48 51 52 55 58 59 61 63 64 66 69 71 76 80 85 86 88 89 94 99\n26 10 11 15 18 23 29 31 33 36 38 44 49 54 56 60 62 65 75 78 82 83 84 95 96 97...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryo...
625_B. War of the Corporations_8265
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000. This new device is equipped with specially designed artificial intelligence (AI). Employees of Pine...
a, b = input(), input() ans = 0 index = a.find (b, 0) while index != -1: ans = ans + 1 index = a.find (b, index + len (b)) print (ans)
{ "input": [ "intellect\ntell\n", "google\napple\n", "sirisiri\nsir\n", "bbbbbb\nbb\n", "abcdabcv\nabcd\n", "sosossosos\nsos\n", "aaaaaaa\naaaa\n", "kek\nkekekek\n", "aaaaa\naaa\n", "abc\nabcabc\n", "abcabcabcabcabcabcabcabcabcabc\nabcabcabcabcabcabcabcabcabcabc\n", "ta...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to ...
673_D. Bear and Two Paths_8269
Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear Limak was once in a city a and he wanted to go to a city b. There was no direct connection so he decided to take a long walk, visiting ea...
n,k=map(int,input().split()) a,b,c,d=input().split() if n<5 or k<n+1: print(-1) else: l=list(set(map(str,range(1,n+1)))-{a,b,c,d}) print(' '.join([a,c]+l+[d,b]+['\n']+[c,a]+l+[b,d]))
{ "input": [ "7 11\n2 4 7 3\n", "1000 999\n10 20 30 40\n", "700 699\n687 69 529 616\n", "4 6\n1 2 3 4\n", "6 6\n1 2 3 4\n", "1000 1577\n698 459 326 404\n", "5 7\n4 3 2 1\n", "4 6\n1 3 2 4\n", "1000 1234\n330 433 967 641\n", "765 766\n352 536 728 390\n", "4 5\n1 3 4 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities. Bear ...
71_A. Way Too Long Words_8276
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome. Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation. This abbreviation is made lik...
n = int(input()) if 1 <= n <= 100: for i in range(n): word = input() if len(word) > 10: numWord = str(len(word[1:-1])) print(word[0] + numWord + word[-1]) else: print(word)
{ "input": [ "4\nword\nlocalization\ninternationalization\npneumonoultramicroscopicsilicovolcanoconiosis\n", "26\na\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n", "10\ngyartjdxxlcl\nfzsck\nuidwu\nxbymclornemdmtj\nilppyoapitawgje\ncibzc\ndrgbeu\nhezplmsdekhhbo\nfeuzlrimbqbyt...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome. Let's consider a word too long, if its leng...
740_C. Alyona and mex_8280
Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri,...
n,m=map(int,input().split()) min_val=9999999 for _ in range(m): y,x=map(int,input().split()) if min_val>x-y: x1=x y1=y min_val=x-y print(min_val+1) L=[0]*n tp=0 listNum=[] for i in range(n): listNum.append(tp) tp+=1 if tp>min_val: tp=0 for i in listNum[:-1]: print...
{ "input": [ "5 3\n1 3\n2 5\n4 5\n", "4 2\n1 4\n2 4\n", "5 3\n1 3\n2 5\n4 5\n", "3 8\n1 2\n1 2\n1 1\n2 3\n2 3\n1 1\n1 3\n1 3\n", "6 8\n3 5\n3 6\n4 6\n2 5\n2 5\n1 3\n3 6\n3 5\n", "5 8\n3 4\n1 5\n3 4\n3 5\n3 4\n1 4\n1 5\n2 5\n", "7 9\n6 7\n1 2\n2 5\n4 7\n1 7\n5 6\n2 3\n6 7\n1 2\n", "7 7\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special. Alyona is a capricious girl so after she gets the array, she inspects m ...
764_B. Timofey and cubes_8284
Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a row and went to unpack other presents. In this time, Timofey's elder brother, Dima reordered the cubes using the following rule. Suppose t...
n=int(input()) a=list(map(int,input().split())) i=0 temp=0 for i in range(n//2): if i%2==0: temp=a[i] a[i]=a[n-i-1] a[n-i-1]=temp for r in a: print(r,end=' ')
{ "input": [ "8\n6 1 4 2 5 6 9 2\n", "7\n4 3 7 6 9 1 2\n", "5\n241218936 -825949895 -84926813 491336344 -872198236\n", "2\n1 2\n", "9\n-7 9 -4 9 -6 11 15 2 -10\n", "1\n1424\n", "2\n2 5\n", "6\n1 2 3 4 5 6\n", "2\n3 4\n", "42\n-557774624 828320986 -345782722 -62979938 -681259411...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Every cube has a number ai, which is written on it. Timofey put all the cubes in a ro...
809_A. Do you want a date?_8290
Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the week Leha managed to get access to n computers throughout the town. Incidentally all the computers, which were hacked by Leha, lie on the...
def main(): largemodulus = 1000000007 maxofn = 300001 n = 0 answer = 0 powersoftwo = [] multiplier = 1 for _ in range(maxofn): powersoftwo.append(multiplier) if multiplier >= largemodulus: multiplier = multiplier % largemodulus multiplier *= 2 n = int(input()) hacked =...
{ "input": [ "3\n4 3 1\n", "2\n4 7\n", "20\n2 6 8 9 20 23 27 36 43 49 63 65 70 71 85 87 89 91 94 97\n", "3\n465343471 465343474 465343473\n", "5\n999999984 999999997 999999994 999999991 999999982\n", "2\n1000000000 1\n", "1\n2\n", "5\n4 7 13 17 18\n", "5\n20 17 13 7 2\n", "1\n7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Leha decided to move to a quiet town Vičkopolis, because he was tired by living in Bankopolis. Upon arrival he immediately began to expand his network of hacked computers. During the ...
854_B. Maxim Buys an Apartment_8296
Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adjacent if their indices differ by 1. Some of the apartments can already be inhabited, others are available for sale. Maxim often visits h...
n,k=map(int,input().split()) mi=1 if k==0: print(0,0) exit() if k==n: print(0,0) exit() if 3*k <=n: ma=2*k else: ma=n-k print(mi,ma)
{ "input": [ "6 3\n", "999999998 333333333\n", "999999998 333333332\n", "89 4\n", "2 0\n", "8 8\n", "4 1\n", "9 4\n", "41949373 13173511\n", "966871928 890926970\n", "1 1\n", "1000000000 0\n", "77855558 49163875\n", "547575919 313272818\n", "7 2\n", "100...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Maxim wants to buy an apartment in a new house at Line Avenue of Metropolis. The house has n apartments that are numbered from 1 to n and are arranged in a row. Two apartments are adj...
902_B. Coloring a Tree_8302
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv. Initially cv = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can c...
from queue import * n=int(input()) parent={} child={} for i in range(1,n+1): parent[i]=0 child[i]=[] l=list(map(int,input().split())) for i in range(2,n+1): parent[i]=l[i-2] child[l[i-2]].append(i) l=list(map(int,input().split())) color={} for i in range(1,n+1): color[i]=l[i-1] q=Queue() q.put...
{ "input": [ "7\n1 1 2 3 1 4\n3 3 1 1 1 2 3\n", "6\n1 2 2 1 5\n2 1 1 1 1 1\n", "4\n1 1 2\n4 1 4 1\n", "4\n1 2 3\n4 1 2 4\n", "3\n1 2\n2 1 1\n", "3\n1 1\n2 2 2\n", "5\n1 2 1 4\n1 1 1 2 2\n", "3\n1 1\n1 2 2\n", "4\n1 2 2\n3 1 2 3\n", "4\n1 2 1\n1 2 3 4\n", "2\n1\n2 2\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 rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by cv...
952_F. 2 + 2 != 4_8309
One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluating this expression. However, looks like there is a bug in the reference solution... Input The only line of input data contains the arith...
import re;s=input();print(eval(s)+eval(re.sub("-|\+|\d",lambda m:"+-035"[2-"+--".count(m[0])::3],s)))
{ "input": [ "2+2\n", "112-37\n", "8-7+6-5+4-3+2-1-0\n", "131+49+249+71-251-61+159-111+51\n", "150+222-3-90-248-187+198\n", "0-0-0-0-0-0-0-0-0-0\n", "240-120-234-156-207-189\n", "23+6-9\n", "101+200+195+231+107+222+146+254+160+209\n", "5-9-1-3+6+4-7+8-2\n", "1-2+3-4+5-6\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One very experienced problem writer decided to prepare a problem for April Fools Day contest. The task was very simple - given an arithmetic expression, return the result of evaluatin...
99_C. Help Victoria the Wise_8316
Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know yet what the surprise actually is because she cannot open the box. She hopes that you can help her in that. The box's lock is constructed...
from functools import reduce def factorial(n): return reduce(lambda x, y: x*y, range(1,n+1)) colors = { 'R' : 0, 'O' : 0, 'Y' : 0, 'G' : 0, 'B' : 0, 'V' : 0 } for c in list(input()): colors[c] += 1 amount = list(reversed(sorted([(colors[key], key) for key in colors]))) amount = [x[0...
{ "input": [ "YYYYYY\n", "BOOOOB\n", "ROYGBV\n", "VYYYVV\n", "OBRRYY\n", "RRYOGB\n", "RROOYY\n", "GOGGVG\n", "GVGBVO\n", "BOBGBB\n", "OOYYBY\n", "VVRVVV\n", "VOVRBV\n", "YYYYRB\n", "OOOOOO\n", "VOBVYB\n", "RRRGRG\n", "RRRRRR\n", "YROVBG\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasilisa the Wise from a far away kingdom got a present from her friend Helga the Wise from a farther away kingdom. The present is a surprise box, yet Vasilisa the Wise doesn't know y...
p02540 ACL Contest 1 - Reachable Towns_8328
There are N cities on a 2D plane. The coordinate of the i-th city is (x_i, y_i). Here (x_1, x_2, \dots, x_N) and (y_1, y_2, \dots, y_N) are both permuations of (1, 2, \dots, N). For each k = 1,2,\dots,N, find the answer to the following question: Rng is in City k. Rng can perform the following move arbitrarily many t...
n = int(input()) xy = [] idx = [None]*n ys = [None]*n ans = [None]*n accumx = [None]*n accumn = [None]*n for i in range(n): x, y = map(int, input().split()) idx[x-1] = i ys[x-1] = y-1 accumx[-1] = ys[-1] for i in range(n-2, -1, -1): accumx[i] = max(accumx[i+1], ys[i]) accumn[0] = ys[0] for i in range(1,...
{ "input": [ "7\n6 4\n4 3\n3 5\n7 1\n2 7\n5 2\n1 6", "4\n1 4\n2 3\n3 1\n4 2", "7\n6 4\n4 2\n3 5\n7 1\n2 7\n5 3\n1 6", "4\n2 4\n1 3\n3 1\n4 2", "4\n2 4\n1 3\n3 2\n4 1", "4\n1 4\n2 3\n3 2\n4 1", "4\n1 4\n2 2\n3 1\n4 3", "4\n2 4\n1 2\n3 1\n4 3", "7\n6 4\n4 3\n3 5\n7 1\n2 6\n5 2\n1 7",...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N cities on a 2D plane. The coordinate of the i-th city is (x_i, y_i). Here (x_1, x_2, \dots, x_N) and (y_1, y_2, \dots, y_N) are both permuations of (1, 2, \dots, N). For ...
p02671 AtCoder Grand Contest 044 - Strange Dance_8331
There are 3^N people dancing in circle. We denote with 0,1,\dots, 3^{N}-1 the positions in the circle, starting from an arbitrary position and going around clockwise. Initially each position in the circle is occupied by one person. The people are going to dance on two kinds of songs: salsa and rumba. * When a salsa i...
n = int(input()) t = input() new_pos = [0] new_w = [0]*len(t) for i in range(1, n+1): ith_bit = [0]*(3**i) # ith_bit[p] : 位置 (p mod 3**i) のi番目bit for k in range(3): for l in range(3**(i-1)): ith_bit[k*3**(i-1)+l] = k pos = new_pos w = new_w # 繰り上がりが起きても ith_bit を正確に捉えるため、j 曲流したあ...
{ "input": [ "3\nSRSRRSRRRSRRRR", "2\nRRSRSSSSR", "1\nSRS", "2\nSRRRSSSSR", "1\nRRS", "1\nSRR", "2\nRSSSSRSRR", "1\nSSR", "2\nSRSRSRSSR", "1\nRSR", "2\nSSSSSRSRR", "2\nSRSRSRSRR", "3\nRRRRSRRRSRRSRS", "2\nRSSRRSSSR", "2\nSRRRRSSSR", "2\nRRSRSRSSR", "...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are 3^N people dancing in circle. We denote with 0,1,\dots, 3^{N}-1 the positions in the circle, starting from an arbitrary position and going around clockwise. Initially each p...
p02800 Keyence Programming Contest 2020 - Monochromization_8334
We have an H \times W grid, where each square is painted white or black in the initial state. Given are strings A_1, A_2, ..., A_H representing the colors of the squares in the initial state. For each pair (i, j) (1 \leq i \leq H, 1 \leq j \leq W), if the j-th character of A_i is `.`, the square at the i-th row and j-t...
import sys input = sys.stdin.readline H,W=map(int,input().split()) A=[input().strip() for i in range(H)] mod=998244353 # factorial,facotiralの逆数を事前計算. FACT=[1] for i in range(1,21): FACT.append(FACT[-1]*i%mod) FACT_INV=[pow(FACT[-1],mod-2,mod)] for i in range(20,0,-1): FACT_INV.append(FACT_INV[-1]*i%mod) FA...
{ "input": [ "3 3\n...\n...\n...", "2 2\n#.\n.#", "2 4\n...\n...#", "6 7\n.......\n.......\n.#.....\n..#....\n.#.#...\n.......", "2 2\n.\n.#", "3 2\n...\n...\n...", "2 1\n#.\n.#", "1 4\n...\n...#", "2 2\n..-\n...\n...", "4 3\n...\n...\n...", "2 8\n...\n...#", "6 2\n.......
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have an H \times W grid, where each square is painted white or black in the initial state. Given are strings A_1, A_2, ..., A_H representing the colors of the squares in the initia...
p02936 AtCoder Beginner Contest 138 - Ki_8337
Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0. Now, the following Q operations will be performed: * Operation...
import sys sys.setrecursionlimit(10**7) input = sys.stdin.readline n,q=map(int,input().split()) c=[[] for _ in range(n)] for _ in range(n-1): a,b=map(int,input().split()) a,b=a-1,b-1 c[a].append(b) c[b].append(a) px=[0]*n for I in range(q): p,x=map(int,input().split()) px[p-1]+=x ans=[0]*n def dfs(g,h=...
{ "input": [ "6 2\n1 2\n1 3\n2 4\n3 6\n2 5\n1 10\n1 10", "4 3\n1 2\n2 3\n2 4\n2 10\n1 100\n3 1", "4 3\n1 2\n2 3\n1 4\n2 10\n1 100\n3 1", "4 3\n1 2\n2 3\n1 4\n2 10\n1 000\n3 1", "4 3\n1 2\n2 3\n2 4\n2 19\n1 100\n3 1", "2 3\n1 2\n2 3\n1 4\n2 10\n1 100\n3 1", "4 3\n1 2\n2 3\n2 4\n4 10\n1 100\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is a rooted tree with N vertices numbered 1 to N. The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i. Each of the vertices has a counter ...
p03073 AtCoder Beginner Contest 124 - Coloring Colorfully_8341
N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N. The i-th tile from the left is painted black if the i-th character of S is `0`, and painted white if that character is `1`. You want to repaint some of the tiles black or white, so that any two a...
s=input();z=sum([int(s[i])==i%2 for i in range(len(s))]);print(min(z,len(s)-z))
{ "input": [ "10010010", "0", "000", "10110010", "010", "10110110", "00110110", "100", "10100110", "01110110", "01110111", "10011010", "1", "10111010", "10100111", "10100010", "01100110", "01100111", "101", "10111000", "10100101", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N. The i-th tile from the left is painted black if the i-th ch...
p03215 Dwango Programming Contest V - Sum AND Subarrays_8345
One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N. He is interested in properties of the sequence a. For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to ...
import sys input = sys.stdin.readline n, k = map(int,input().split()) A = list(map(int,input().split())) S = [0] for i in range(n): S.append(A[i] + S[-1]) B = [] for i in range(len(S)): for j in range(i+1, len(S)): B.append(S[j] - S[i]) # B.sort() # print(B) ans = 0 for i in range(50, -1, -1): ans ...
{ "input": [ "4 2\n2 5 2 5", "8 4\n9 1 8 2 7 5 6 4", "8 4\n9 1 8 2 7 5 6 6", "8 4\n9 1 8 2 7 8 6 6", "4 4\n2 5 2 5", "8 8\n9 1 8 2 7 5 6 4", "8 8\n9 1 11 2 7 5 6 4", "8 4\n8 1 15 2 7 8 6 6", "8 6\n8 1 15 2 7 8 6 6", "8 6\n8 1 27 2 7 8 6 6", "8 7\n7 1 11 2 14 5 6 4", "8 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N. He is interested in properties of the sequence a. For a nonempty contigu...
p03364 AtCoder Grand Contest 023 - Find Symmetries_8349
Snuke has two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called Square (i,j). There is a lowercase English letter written in each square on the first board. The letter written in Square (i,j) is S_{i...
#023_B n = int(input()) s = [input() * 2 for _ in range(n)] ans = 0 for b in range(0, n): flg = True for i in range(n): for j in range(n): if s[i][b+j] != s[j][b+i]: flg = False break if flg: ans += 1 print(ans * n)
{ "input": [ "2\nab\nca", "5\nabcde\nfghij\nklmno\npqrst\nuvwxy", "4\naaaa\naaaa\naaaa\naaaa", "2\nab\nda", "5\nabcde\nfghij\nklmno\npqrst\nuvvxy", "4\naaaa\naaaa\naaaa\nbaaa", "2\nab\nad", "5\nabcde\nfghij\nonmlk\npqrst\nuvwxy", "4\nbaaa\naaaa\naaaa\nbaaa", "2\nab\nac", "5...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke has two boards, each divided into a grid with N rows and N columns. For both of these boards, the square at the i-th row from the top and the j-th column from the left is called...
p03687 AtCoder Grand Contest 016 - Shrinking_8354
Snuke can change a string t of length N into a string t' of length N - 1 under the following rule: * For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (i + 1)-th character of t. There is a string s consisting of lowercase English letters. Snuke's objective is to apply the above operati...
s = input().strip() l = len(s) d, a = {}, {} for i, c in enumerate(s): if c in d: d[c] = max(d[c], i - a[c]) else: d[c] = i a[c] = i + 1 for c, v in d.items(): d[c] = max(v, l - a[c]) print(min(d.values()))
{ "input": [ "serval", "whbrjpjyhsrywlqjxdbrbaomnw", "jackal", "zzz", "resval", "whbrjpjyhsrywlqjxcbrbaomnw", "zzy", "lakjac", "xmboabrbnxjrlwyrthyjpjrchv", "whcrjpbyhtxywkrlxnbrbaojmr", "smjoabjdoxmrjwypugydyqrchv", "whdrpxdwhuozxkrmxodjaaojkt", "xwwwnajdrxmrqwzpug...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke can change a string t of length N into a string t' of length N - 1 under the following rule: * For each i (1 ≤ i ≤ N - 1), the i-th character of t' must be either the i-th or (...
p03840 AtCoder Grand Contest 008 - Tetromino Tiling_8358
A tetromino is a figure formed by joining four squares edge to edge. We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively: a60bcb8e9e8f22e3af51049eda063392.png Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession...
a = list(map(int, input().split())) if a[0] % 2 == 1 and a[3] % 2 == 1 and a[4] % 2 == 1: ans = ((a[0] // 2) * 4 + a[1] * 2 + (a[3] // 2) * 4 + (a[4] // 2) * 4) // 2 print(ans + 3) elif a[0] % 2 == 1 and a[3] % 2 == 1 and a[4] % 2 == 0 and a[4] > 0: ans = ((a[0] // 2) * 4 + a[1] * 2 + (a[3] // 2) * 4 + (a[...
{ "input": [ "2 1 1 0 0 0 0", "0 0 10 0 0 0 0", "0 0 10 0 1 0 0", "1 1 1 0 0 0 0", "0 0 -1 0 2 0 2", "4 0 1 0 0 0 0", "0 3 0 0 1 0 0", "0 3 0 0 2 0 0", "6 0 2 0 0 0 0", "11 0 2 0 0 0 0", "14 0 2 0 0 0 0", "2 5 1 0 1 2 2", "23 0 2 0 0 0 0", "1 4 0 0 4 0 3", "...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A tetromino is a figure formed by joining four squares edge to edge. We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively: ...
p04007 AtCoder Grand Contest 004 - AND Grid_8362
Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns. Snuke painted some of the cells red in his paper. Here, the cells painted red were 4-connected, that is, it was possible to traverse from any red cell to any other red cell, by moving to vertically ...
h, w = map(int, input().split()) ans1 = [['.'] * w for _ in range(h)] ans2 = [['.'] * w for _ in range(h)] for i in range(h): ans1[i][0] = '#' ans2[i][-1] = '#' if i % 2: for j in range(w - 1): ans1[i][j] = '#' else: for j in range(1, w): ans2[i][j] = '#' for i in...
{ "input": [ "5 5\n.....\n.#.#.\n.....\n.#.#.\n.....", "7 13\n.............\n.###.###.###.\n.#.#.#...#...\n.###.#...#...\n.#.#.#.#.#...\n.#.#.###.###.\n.............", "5 5\n.....\n.#.#.\n.....\n.#.#.\n.../.", "7 13\n.............\n.###.###.###.\n.#.#.#...#...\n.###.#...#...\n...#.#.#.#.#.\n.#.#.###.#...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke and Ciel went to a strange stationery store. Each of them got a transparent graph paper with H rows and W columns. Snuke painted some of the cells red in his paper. Here, the c...
p00093 Leap Year_8366
Create a program that outputs all leap years between the year a and year b. The leap year conditions are as follows. However, 0 <a ≤ b <3,000. If there is no leap year in the given period, output "NA". * The year is divisible by 4. * However, a year divisible by 100 is not a leap year. * However, a year divisible by ...
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0093 ???????????´ """ import sys def is_leap(year): ''' ?????????????????´??? ???????????´ ???????????????True/False??§?????? ''' if year % 400 == 0: return True elif year % 100 == 0: return False elif ...
{ "input": [ "2001 2010\n2005 2005\n2001 2010\n0 0", "2001 2010\n1393 2005\n2001 2010\n0 0", "2063 2010\n1393 2005\n2001 2010\n0 0", "2063 2076\n1393 2005\n2001 2010\n0 0", "2063 2076\n197 2005\n2001 2010\n0 0", "2063 2076\n197 607\n2001 2010\n0 0", "2063 2076\n197 842\n2001 2010\n0 0", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Create a program that outputs all leap years between the year a and year b. The leap year conditions are as follows. However, 0 <a ≤ b <3,000. If there is no leap year in the given p...
p00225 Kobutanukitsuneko_8370
Relative B man came to A child's house. He is 3 years old and loves singing. He is singing the song "Kobutanuki Tsuneko" (written and composed by Naozumi Yamamoto), which he learned from kindergarten. In this song, the four words "kobuta," "raccoon dog," "fox," and "cat" are arranged in order, and the last and first so...
from collections import* def searchnode(i,u,path): #つなげるものの個数の確認 r = 1 u[i] = 1 # print(u) for j in range(26): if path[(i,j)] and (not u[j]): r += searchnode(j,u,path) return r def chrtoint(a): return ord(a)-ord('a') while 1: n = int(input()) if n==0: brea...
{ "input": [ "5\napple\nyellow\ngeorgia\nking\nemail\n7\napple\nyellow\ngeorgia\nking\nemail\nwink\nlucky\n0", "5\napple\nyellow\ngeorgia\ngnik\nemail\n7\napple\nyellow\ngeorgia\nking\nemail\nwink\nlucky\n0", "5\napple\nyellow\ngeorgia\ngnik\nemail\n7\napple\nzellow\ngeorgia\nking\nemail\nwink\nlucky\n0",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Relative B man came to A child's house. He is 3 years old and loves singing. He is singing the song "Kobutanuki Tsuneko" (written and composed by Naozumi Yamamoto), which he learned f...
p00387 Party Dress_8373
Yae joins a journey plan, in which parties will be held several times during the itinerary. She wants to participate in all of them and will carry several dresses with her. But the number of dresses she can carry with her may be smaller than that of the party opportunities. In that case, she has to wear some of her dre...
n,m = list(map(int,input().split())) print(max(m//n+(m%n != 0),1))
{ "input": [ "25 10", "3 5", "10 10", "3 9", "3 15", "3 26", "1 26", "1 43", "1 65", "1 18", "2 8", "2 3", "5 26", "1 30", "2 30", "3 28", "1 12", "1 20", "5 32", "2 21", "2 26", "1 75", "1 118", "1 36", "1 0", "2 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Yae joins a journey plan, in which parties will be held several times during the itinerary. She wants to participate in all of them and will carry several dresses with her. But the nu...
p00603 Riffle Shuffle_8377
There are a number of ways to shuffle a deck of cards. Riffle shuffle is one such example. The following is how to perform riffle shuffle. There is a deck of n cards. First, we divide it into two decks; deck A which consists of the top half of it and deck B of the bottom half. Deck A will have one more card when n is ...
from collections import deque import sys def suffle(deck, c): l = len(deck) if l % 2 == 0: mid = l//2 else: mid = (l-1)//2 deckA = deck[mid:] deckB = deck[:mid] deckC = [] while(len(deckA) != 0 or len(deckB) != 0): deckC.extend(deckA[:c]) deckA = deckA[c:] deck...
{ "input": [ "9 1\n3\n9 4\n1 2 3 4", "9 1\n3\n9 4\n1 2 6 4", "9 1\n3\n11 4\n1 2 6 4", "9 1\n3\n9 4\n1 1 6 6", "9 1\n2\n11 4\n1 2 6 4", "8 1\n3\n16 4\n1 1 3 4", "9 1\n2\n17 4\n1 2 6 4", "9 1\n3\n15 4\n2 1 6 6", "8 1\n3\n24 4\n1 1 3 4", "9 1\n4\n5 4\n1 2 6 4", "2 1\n4\n5 4\n1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are a number of ways to shuffle a deck of cards. Riffle shuffle is one such example. The following is how to perform riffle shuffle. There is a deck of n cards. First, we divid...
p00740 Next Mayor_8381
One of the oddest traditions of the town of Gameston may be that even the town mayor of the next term is chosen according to the result of a game. When the expiration of the term of the mayor approaches, at least three candidates, including the mayor of the time, play a game of pebbles, and the winner will be the next ...
while True: n, p = map(int, input().split()) if n == 0: break lst = [0] * n ind = 0 rest = p while True: if rest == 0: rest = lst[ind] lst[ind] = 0 else: lst[ind] += 1 rest -= 1 if lst[ind] == p: print(ind) break ind = (ind + 1) % n
{ "input": [ "3 2\n3 3\n3 50\n10 29\n31 32\n50 2\n50 50\n0 0", "3 2\n3 3\n3 50\n10 29\n57 32\n50 2\n50 50\n0 0", "3 2\n3 3\n4 50\n10 29\n57 32\n50 2\n50 50\n0 0", "3 2\n3 3\n4 50\n10 29\n57 32\n50 1\n50 50\n0 0", "3 3\n3 3\n3 50\n10 29\n31 32\n50 2\n50 50\n0 0", "3 2\n3 2\n3 50\n10 29\n57 32\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One of the oddest traditions of the town of Gameston may be that even the town mayor of the next term is chosen according to the result of a game. When the expiration of the term of t...
p01142 Karakuri Doll_8387
Karakuri Doll Karakuri doll English text is not available in this practice contest. After many years of research, Karakuri puppeteer JAG has succeeded in developing a wonderful tea-drawing doll that combines traditional and latest techniques. This tea-drawing doll is placed in a teacup with tea in the kitchen (K) in...
from collections import deque import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): W, H = map(int, readline().split()) if W == H == 0: return False S = [readline().strip() for i in range(H)] dd = ((-1, 0), (0, -1), (1, 0), (0, 1)) sx = sy = gx = gy = 0 for i i...
{ "input": [ "5 3\n#####\n#K.M#\n#####\n9 5\n#########\n#.....###\n#.###..M#\n#K#######\n#########\n9 5\n#########\n#K......#\n####.####\n####M####\n#########\n9 5\n#########\n#M......#\n####.####\n####K####\n#########\n7 9\n#######\n#####M#\n#####.#\n#.....#\n#.###.#\n#.....#\n#.#####\n#K#####\n#######\n7 6\n###...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Karakuri Doll Karakuri doll English text is not available in this practice contest. After many years of research, Karakuri puppeteer JAG has succeeded in developing a wonderful tea...
p01281 Tatami_8390
A tatami mat, a Japanese traditional floor cover, has a rectangular form with aspect ratio 1:2. When spreading tatami mats on a floor, it is prohibited to make a cross with the border of the tatami mats, because it is believed to bring bad luck. Your task is to write a program that reports how many possible ways to sp...
def solve(): H, W = map(int, input().split()) if H == 0: return False if H*W % 2 == 1: print(0) return True state = [[-1]*W for i in range(H)] def dfs(k): if k == H*W: return 1 i, j = divmod(k, W) if state[i][j] != -1: return df...
{ "input": [ "3 4\n4 4\n0 0", "3 1\n4 4\n0 0", "3 1\n6 4\n0 0", "3 4\n4 1\n0 0", "3 1\n4 2\n0 0", "3 2\n6 4\n0 0", "3 7\n4 1\n0 0", "4 4\n4 1\n0 0", "4 1\n4 2\n0 0", "3 7\n7 1\n0 0", "4 1\n6 4\n0 0", "4 1\n4 1\n0 0", "4 1\n10 4\n0 0", "4 4\n9 1\n0 0", "8 4\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A tatami mat, a Japanese traditional floor cover, has a rectangular form with aspect ratio 1:2. When spreading tatami mats on a floor, it is prohibited to make a cross with the border...
p01451 Roads on Towns_8393
The Kingdom of Neva is home to two ethnic groups, the Totata and the Tutete. The biggest feature of the Totata tribe is that they eat sweet and sour pork with pineapple. However, the Tutete tribe eats vinegared pork in pineapple. These two peoples couldn't get along with each other, and the Totata and Tutete have been ...
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": [ "2 3\n4 0\n0 0\n2 3\n2 -2\n3 1", "2 2\n0 0\n1 1\n2 0\n2 -1", "5 2\n-2 1\n1 2\n-1 3\n-1 5\n1 4\n0 4\n0 0", "2 3\n4 0\n-1 0\n2 3\n2 -2\n3 1", "2 2\n0 0\n1 2\n2 0\n2 -1", "5 2\n-2 0\n1 2\n-1 3\n-1 5\n1 4\n0 4\n0 0", "2 3\n2 2\n-2 0\n4 3\n2 -2\n-1 1", "2 3\n2 2\n-2 0\n4 6\n2 -...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Kingdom of Neva is home to two ethnic groups, the Totata and the Tutete. The biggest feature of the Totata tribe is that they eat sweet and sour pork with pineapple. However, the ...
p01601 Palindromic Number_8397
Palindrome Problem Statement Find the number of palindromes closest to the integer n. Note that the non-negative integer x is the number of palindromes, which means that the character string in which x is expressed in decimal notation and the character string in which it is inverted are equal. For example, 0,7,33,10...
n = int(input()) s = list(range(10)) for i in range(1, 100): t = str(i) s.append(t+t[::-1]) for j in range(10): s.append(t+str(j)+t[::-1]) s = list(map(int, s)) s.sort() if n in s: print(n) else: if n < s[0]: print(s[0]) elif n > s[-1]: print(s[-1]) else: re...
{ "input": [ "13", "106", "7447", "4", "8", "4752", "7", "23", "8517", "6", "39", "3483", "5", "1868", "1", "2667", "92", "3332", "61", "4989", "2", "5975", "5760", "741", "1111", "1000", "1001", "117", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Palindrome Problem Statement Find the number of palindromes closest to the integer n. Note that the non-negative integer x is the number of palindromes, which means that the charac...
p01757 Tournament_8400
This year too, the time has come for the National Programming Championships. In the district tournament where the right to participate in the national tournament is bet, 2n teams will face each other in a one-on-one winning tournament system. Team numbers 0, .. .2n − 1 are assigned to the tournament table, and the con...
import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): N, M = map(int, readline().split()) *A, = map(int, readline().split()) *B, = map(int, readline().split()) C = [] prv = 0 for i in range(M): C.append((A[i+1] - prv, 1 << B[i])) prv = A[i+1] ans = 0 ...
{ "input": [ "1 1\n0 2\n1", "1 1\n0 4\n1", "2 1\n0 4\n1", "0 1\n0 4\n0", "2 1\n0 4\n2", "3 1\n0 8\n0", "3 1\n0 8\n2", "3 1\n0 8\n3", "1 1\n0 8\n1", "1 1\n-1 3\n1", "2 1\n0 4\n0", "1 1\n0 2\n0", "1 1\n-2 2\n1", "0 1\n0 8\n0", "0 1\n0 1\n0", "0 2\n-1 1\n1"...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This year too, the time has come for the National Programming Championships. In the district tournament where the right to participate in the national tournament is bet, 2n teams will...
p02177 iff_8405
problem There are $ N $ propositions, named $ 1, 2, \ cdots, N $, respectively. Also, $ M $ information about the propositions is given. The $ i $ th information is "$ a_i $$". Given in the form "b_i $", which means that $ a_i $ is $ b_i $. ("If" is a logical conditional and the transition law holds.) $ For each propo...
# -*- coding: utf-8 -*- import sys input = sys.stdin.readline def inpl(): return list(map(int, input().split())) N, M = inpl() G = [[] for _ in range(N)] rG = [[] for _ in range(N)] for i in range(M): a, b = inpl() G[a-1].append(b-1) rG[b-1].append(a-1) def SCC(G, rG): N = len(G) def dfs(i): ...
{ "input": [ "5 2\n1 2\n2 1", "5 2\n1 2\n2 2", "3 2\n1 2\n2 1", "3 2\n1 2\n3 1", "6 2\n1 2\n2 1", "9 2\n1 2\n2 4", "6 2\n1 1\n2 1", "17 2\n1 2\n2 4", "4 2\n2 2\n3 1", "26 2\n2 2\n4 4", "12 2\n2 2\n4 4", "1 0\n2 2\n1 1", "17 2\n1 2\n2 1", "7 1\n2 1\n3 0", "28...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem There are $ N $ propositions, named $ 1, 2, \ cdots, N $, respectively. Also, $ M $ information about the propositions is given. The $ i $ th information is "$ a_i $$". Given...
p02320 Knapsack Problem with Limitations_8407
You have N items that you want to put them into a knapsack. Item i has value vi, weight wi and limitation mi. You want to find a subset of items to put such that: * The total value of the items is as large as possible. * The items have combined weight at most W, that is capacity of the knapsack. * You can select at m...
def main(): import sys from collections import deque input = sys.stdin.readline N, W = map(int, input().split()) dp = [0]*(W+1) for _ in [0]*N: v, w, m = map(int, input().split()) for ofs in range(w): q = deque() for j in range(W+1): ...
{ "input": [ "2 100\n1 1 100\n2 1 50", "4 8\n4 3 2\n2 1 1\n1 2 4\n3 2 2", "2 100\n1 1 100\n2 1 25", "5 8\n4 3 2\n2 1 1\n1 2 4\n3 2 2", "2 101\n1 1 100\n2 1 25", "5 8\n4 2 2\n2 1 1\n1 2 4\n3 2 2", "2 101\n1 1 100\n1 1 25", "5 3\n4 2 2\n2 1 1\n1 2 3\n3 2 2", "5 3\n4 1 2\n2 1 1\n1 2 3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have N items that you want to put them into a knapsack. Item i has value vi, weight wi and limitation mi. You want to find a subset of items to put such that: * The total value ...
p02465 Set Difference_8410
Find the difference of two sets $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and $B = \\{b_0, b_1, ..., b_{m-1}\\}$, $A - B$. Constraints * $1 \leq n, m \leq 200,000$ * $0 \leq a_0 < a_1 < ... < a_{n-1} \leq 10^9$ * $0 \leq b_0 < b_1 < ... < b_{m-1} \leq 10^9$ Input The input is given in the following format. $n$ $a_0 \; a...
input() a = set(map(int, input().split())) input() b = set(map(int, input().split())) [print(s) for s in map(str, sorted(list(a-b)))]
{ "input": [ "5\n1 2 3 5 8\n2\n2 5", "5\n1 2 3 5 8\n2\n4 5", "5\n1 2 3 5 8\n0\n4 5", "5\n0 2 3 5 8\n0\n0 5", "5\n-1 2 3 5 8\n0\n0 5", "5\n-1 2 3 5 14\n0\n0 9", "5\n0 2 3 5 8\n2\n4 5", "5\n-1 2 3 5 16\n0\n0 5", "5\n0 2 3 5 11\n2\n4 5", "5\n-1 2 3 5 27\n0\n0 18", "5\n0 2 3 5 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Find the difference of two sets $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and $B = \\{b_0, b_1, ..., b_{m-1}\\}$, $A - B$. Constraints * $1 \leq n, m \leq 200,000$ * $0 \leq a_0 < a_1 < .....
1055_E. Segments on the Line_8421
You are a given a list of integers a_1, a_2, …, a_n and s of its segments [l_j; r_j] (where 1 ≤ l_j ≤ r_j ≤ n). You need to select exactly m segments in such a way that the k-th order statistic of the multiset of a_i, where i is contained in at least one segment, is the smallest possible. If it's impossible to select ...
n, s, m, k = map(int, input().split()) a = list(map(int, input().split())) nt = [-1 for i in range(n+1)] for i in range(s): l, r = map(int, input().split()) l-=1 for j in range(l, r): nt[j] = max(nt[j], r) dp = [[0 for i in range(m+1)] for i in range(n+1)] sum = [0 for i in range(n+1)] def solve(mid): for i in ...
{ "input": [ "4 3 2 2\n3 1 3 2\n1 2\n2 3\n4 4\n", "5 3 3 5\n5 5 2 1 1\n1 2\n2 3\n3 4\n", "5 2 1 1\n1 2 3 4 5\n2 4\n1 5\n", "10 1 1 8\n1 1 1 991992993 1 991992993 3 6664666 1000000000 999999999\n3 10\n", "10 5 4 10\n1 2 3 4 5 6 7 8 9 10\n3 8\n4 7\n5 6\n1 10\n2 9\n", "1 5 2 1\n1\n1 1\n1 1\n1 1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are a given a list of integers a_1, a_2, …, a_n and s of its segments [l_j; r_j] (where 1 ≤ l_j ≤ r_j ≤ n). You need to select exactly m segments in such a way that the k-th orde...
1077_F1. Pictures with Kittens (easy version)_8425
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of n consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the i-th p...
import sys S = sys.stdin.read() S = list(map(lambda x: list(map(int, x)), list(map(lambda x: x.split(), [x for x in S.split('\n') if len(x) > 1])))) n, k, x, B = S[0][0], S[0][1], S[0][2], S[1] X = [[-1 for i in range(x+1)] for i in range(n+1)] X[0][0] = 0 for i in range(1, n+1): for j in range(1, x+1): ...
{ "input": [ "4 3 1\n1 100 1 1\n", "5 2 3\n5 1 3 10 1\n", "6 1 5\n10 30 30 70 10 10\n", "14 5 1\n777952841 904112822 619719668 821319583 273287753 183942799 391488524 199988810 174106140 533625353 704165043 288839769 328084203 509492291\n", "1 1 1\n465559034\n", "1 1 1\n311658179\n", "14 5...
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 constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of...
1146_C. Tree Diameter_8432
There is a weighted tree with n nodes and n-1 edges. The nodes are conveniently labeled from 1 to n. The weights are positive integers at most 100. Define the distance between two nodes to be the sum of edges on the unique path between the nodes. You would like to find the diameter of the tree. Diameter is the maximum ...
# AC import sys class Main: def __init__(self): self.buff = None self.index = 0 def next(self): if self.buff is None or self.index == len(self.buff): self.buff = sys.stdin.readline().split() self.index = 0 val = self.buff[self.index] self.index ...
{ "input": [ "2\n5\n9\n6\n10\n9\n10\n2\n99\n", "2\n5\n1 2 3\n2 3 4\n2 4 5\n4 5 1\n2\n1 2 99\n", "2\n5\n1 3 3\n2 3 4\n2 4 5\n4 5 1\n2\n1 2 99\n", "2\n5\n9\n12\n10\n9\n10\n2\n99\n", "2\n5\n17\n12\n10\n9\n10\n2\n99\n", "2\n5\n1 3 4\n2 3 4\n2 4 5\n4 5 0\n2\n0 2 99\n", "2\n7\n1 3 4\n2 3 4\n2 4 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a weighted tree with n nodes and n-1 edges. The nodes are conveniently labeled from 1 to n. The weights are positive integers at most 100. Define the distance between two nod...
1167_C. News Distribution_8436
In some social network, there are n users communicating with each other in m groups of friends. Let's analyze the process of distributing some news between users. Initially, some user x receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least o...
import sys n, m = map(int, input().split()) par = [0] * (n + 1) cnt = [0] * (n + 1) for i in range(n + 1): par[i] = i def find(a): if par[a] == a: return a par[a] = find(par[a]) return par[a] for i in sys.stdin.readlines(): x = list(map(int, i[:-1].split())) if x[0]: ta = find(x[1]) for i i...
{ "input": [ "7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 6 7\n", "7 2\n0\n0\n", "7 5\n3 2 5 4\n0\n2 1 2\n1 1\n2 5 7\n", "7 5\n3 2 5 4\n0\n2 1 2\n1 0\n2 6 7\n", "7 5\n3 2 5 4\n0\n2 2 2\n1 0\n2 5 7\n", "7 5\n3 2 5 5\n0\n2 2 2\n1 0\n2 5 7\n", "7 5\n3 2 1 5\n0\n2 2 2\n1 0\n2 5 7\n", "9 5\n3 2 1 5\n0\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In some social network, there are n users communicating with each other in m groups of friends. Let's analyze the process of distributing some news between users. Initially, some use...
1185_F. Two Pizzas_8440
A company of n friends wants to order exactly two pizzas. It is known that in total there are 9 pizza ingredients in nature, which are denoted by integers from 1 to 9. Each of the n friends has one or more favorite ingredients: the i-th of friends has the number of favorite ingredients equal to f_i (1 ≤ f_i ≤ 9) and y...
import sys import math input=sys.stdin.readline #sys.setrecursionlimit(1000000) I=lambda : list(map(int,input().split())) ma =int(10000000000000000) n,m=map(int,input().split()) a=[ma]*(515);a1=[ma]*(515);fr=[0]*(515);pos=[0]*(515) for i in range(n): b=I() x=int(0) for j in range(1,b[0]+1): b[j]-=...
{ "input": [ "1 5\n9 9 8 7 6 5 4 3 2 1\n3 4 1 2 3 4\n1 4 5 6 7 8\n4 4 1 3 5 7\n1 4 2 4 6 8\n5 4 1 9 2 8\n", "3 4\n2 6 7\n4 2 3 9 5\n3 2 3 9\n100 1 7\n400 3 3 2 5\n100 2 9 2\n500 3 2 9 5\n", "4 3\n1 1\n1 2\n1 3\n1 4\n10 4 1 2 3 4\n20 4 1 2 3 4\n30 4 1 2 3 4\n", "3 3\n1 9\n6 4 9 5 7 8 1\n3 5 8 3\n1 2 7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A company of n friends wants to order exactly two pizzas. It is known that in total there are 9 pizza ingredients in nature, which are denoted by integers from 1 to 9. Each of the n ...
1204_D1. Kirk and a Binary String (easy version)_8444
The only difference between easy and hard versions is the length of the string. You can hack this problem only if you solve both problems. Kirk has a binary string s (a string which consists of zeroes and ones) of length n and he is asking you to find a binary string t of the same length which satisfies the following ...
s = str(input().strip()) t = list(s[::-1]) cnt = 0 for i,v in enumerate(t): if v == '0': cnt += 1 else: if cnt: cnt -= 1 else: t[i] = '0' print("".join(t[::-1]))
{ "input": [ "0111001100111011101000\n", "0001111\n", "110\n", "010\n", "0010000110000100\n", "0000000000000000\n", "0000100010001110\n", "1111111111111111\n", "0100000001000001\n", "1001101000001101\n", "1\n", "0\n", "1111111111111010\n", "1111101000110110\n", ...
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 length of the string. You can hack this problem only if you solve both problems. Kirk has a binary string s (a string which ...
1221_B. Knights_8448
You are given a chess board with n rows and n columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board. A knight is a chess piece that can attack a piece in cell (x_2, y_2) from the cell (x_1, y_1) if one of the following conditions is met: * |x_...
n = int(input()) i = 0 s = ''.join(['W' if (i + j) % 2 == 0 else 'B' for i in range(n) for j in range(n)]) while(i<n*n): print(s[i:i+n]) i+=n
{ "input": [ "3\n", "6\n", "99\n", "37\n", "96\n", "5\n", "69\n", "55\n", "4\n", "76\n", "47\n", "3\n", "42\n", "97\n", "13\n", "7\n", "100\n", "10\n", "98\n", "56\n", "12\n", "59\n", "64\n", "24\n", "11\n", "1\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 chess board with n rows and n columns. Initially all cells of the board are empty, and you have to put a white or a black knight into each cell of the board. A knight...
1266_C. Diverse Matrix_8453
Let a be a matrix of size r × c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 1 to r, columns are numbered from 1 to c. We can construct an array b consisting of r + c integers as follows: for each i ∈ [1, r], let b_i be the greatest common divisor of integers in the i-th ...
import sys, os, io def rs(): return sys.stdin.readline().rstrip() def ri(): return int(sys.stdin.readline()) def ria(): return list(map(int, sys.stdin.readline().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') ...
{ "input": [ "1 1\n", "2 2\n", "9 1\n", "1 2\n", "2 3\n", "26 122\n", "3 1\n", "1 3\n", "2 1\n", "1 7\n", "132 1\n", "3 2\n", "2 2\n", "1 132\n", "500 1\n", "77 15\n", "13 56\n", "1 500\n", "492 71\n", "16 1\n", "3 4\n", "3 122\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let a be a matrix of size r × c containing positive integers, not necessarily distinct. Rows of the matrix are numbered from 1 to r, columns are numbered from 1 to c. We can construct...
1287_E1. Madhouse (Easy version)_8456
This problem is different with hard version only by constraints on total answers length It is an interactive problem Venya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string s of length n, consisting only of lowercase English letters. The player can ask tw...
from bisect import bisect_left as bl from bisect import bisect_right as br import heapq import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline # M = mod = 998244353 def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n...
{ "input": [ "4\n\na\naa\na\n\ncb\nb\nc\n\nc", "99\nyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n", "4\nabcd\n", "100\ntssttststtssttsssttsssttttttsttttssstssstsstssstststttssssstttsssttststststtttsssstsstsssssttttttsst\n", "9\nrnnnqnnrn\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This problem is different with hard version only by constraints on total answers length It is an interactive problem Venya joined a tour to the madhouse, in which orderlies play wit...
1307_E. Cow and Treats_8459
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass! On the field, there is a row of n units of grass, each with a sweetness s_i. Farmer John has m cows, each with a favorite sweetness f_i and a hunger value h_i. He would like to pick two disjoint subset...
import sys input = sys.stdin.readline from itertools import accumulate mod=10**9+7 n,m=map(int,input().split()) G=list(map(int,input().split())) CP=[[0]*(n+1) for i in range(n+1)] for i in range(m): f,e=map(int,input().split()) CP[f][e]+=1 SUMCP=[] for i in range(n+1): SUMCP.append(list(accumulate(C...
{ "input": [ "3 2\n2 3 2\n3 1\n2 1\n", "5 2\n1 1 1 1 1\n1 2\n1 3\n", "5 1\n1 1 1 1 1\n2 5\n", "5 2\n1 1 1 1 1\n1 2\n1 4\n", "5 1\n1 1 1 1 1\n1 3\n", "5 1\n1 2 3 2 1\n1 2\n", "5 5\n1 1 1 1 1\n1 1\n1 2\n1 3\n1 4\n1 5\n", "2 1\n2 2\n1 1\n", "5 2\n1 1 1 1 1\n1 1\n1 3\n", "5 1\n1 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass! On the field, there is a row of n units of grass, each with a sw...
1330_E. Drazil Likes Heap_8462
Drazil likes heap very much. So he created a problem with heap: There is a max heap with a height h implemented on the array. The details of this heap are the following: This heap contains exactly 2^h - 1 distinct positive non-zero integers. All integers are distinct. These numbers are stored in the array a indexed f...
import io import os import sys # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') fileno = sys.stdin.fileno() input = io.BytesIO( os.read(fileno, os.fstat(fileno).st_size) ).readline T = int(input()) def trim(A, h, g): N = len(A) ans = [] initial_root = 0 items_to_leave = pow(2, g) ...
{ "input": [ "2\n3 2\n7 6 3 5 4 2 1\n3 2\n7 6 5 4 3 2 1\n", "2\n3 2\n7 6 3 5 4 2 1\n3 2\n7 6 5 4 3 2 1\n", "2\n3 2\n7 6 3 5 4 2 1\n3 2\n14 6 5 4 3 2 1\n", "2\n3 1\n7 6 3 5 4 2 1\n3 2\n14 6 5 4 3 2 1\n", "2\n3 2\n7 6 3 5 4 2 1\n3 1\n23 6 5 4 3 2 1\n", "2\n3 1\n7 6 3 5 4 2 1\n3 1\n23 6 5 4 3 2 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Drazil likes heap very much. So he created a problem with heap: There is a max heap with a height h implemented on the array. The details of this heap are the following: This heap c...
1350_E. Orac and Game of Life_8465
Please notice the unusual memory limit of this problem. Orac likes games. Recently he came up with the new game, "Game of Life". You should play this game on a black and white grid with n rows and m columns. Each cell is either black or white. For each iteration of the game (the initial iteration is 0), the color of...
import sys input = sys.stdin.readline n, m, t = map(int, input().split()) cell = [list(input().rstrip()) for _ in range(n)] cnt = [[False] * m for _ in range(n)] for i in range(n): for j in range(m): if i - 1 >= 0 and cell[i - 1][j] == cell[i][j]: cnt[i][j] = True if i + 1 < n and cell...
{ "input": [ "3 3 3\n000\n111\n000\n1 1 1\n2 2 2\n3 3 3\n", "5 2 2\n01\n10\n01\n10\n01\n1 1 4\n5 1 4\n", "1 1 3\n0\n1 1 1\n1 1 2\n1 1 3\n", "5 5 3\n01011\n10110\n01101\n11010\n10101\n1 1 4\n1 2 3\n5 5 3\n", "1 1 1\n1\n1 1 1000000000000000000\n", "2 2 1\n10\n11\n1 2 1000000000000000000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Please notice the unusual memory limit of this problem. Orac likes games. Recently he came up with the new game, "Game of Life". You should play this game on a black and white grid ...
1371_A. Magical Sticks_8469
A penguin Rocher has n sticks. He has exactly one stick with length i for all 1 ≤ i ≤ n. He can connect some sticks. If he connects two sticks that have lengths a and b, he gets one stick with length a + b. Two sticks, that were used in the operation disappear from his set and the new connected stick appears in his se...
for _ in range(int(input())): n=int(input()) print(max(1,n//2 + int(n%2)))
{ "input": [ "4\n1\n2\n3\n4\n", "4\n1\n2\n3\n8\n", "4\n1\n2\n1\n4\n", "4\n1\n2\n3\n12\n", "4\n2\n2\n3\n15\n", "4\n2\n2\n3\n5\n", "4\n1\n2\n4\n4\n", "4\n1\n2\n3\n14\n", "4\n1\n3\n1\n4\n", "4\n2\n2\n6\n15\n", "4\n1\n2\n5\n4\n", "4\n1\n2\n3\n20\n", "4\n1\n3\n3\n5\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A penguin Rocher has n sticks. He has exactly one stick with length i for all 1 ≤ i ≤ n. He can connect some sticks. If he connects two sticks that have lengths a and b, he gets one ...
1393_D. Rarity and New Dress_8473
Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners. First of all, she needs a dress pattern, which she is going to cut out from the rectangular piece of the multicolored fabric. The piec...
r,c=map(int,input().split()) rama=[] for i in range(r): rama.append(list((input()))) visit=[[1 for i in range(c)] for j in range(r)] for i in range(2,r): for j in range(1,c-1): if(rama[i][j]==rama[i-1][j-1]==rama[i-1][j+1]==rama[i-2][j]==rama[i-1][j]): visit[i][j]=min(visit[i-1][j-1],visit[...
{ "input": [ "5 5\nzbacg\nbaaac\naaaaa\neaaad\nweadd\n", "3 3\naaa\naaa\naaa\n", "3 4\nabab\nbaba\nabab\n", "1 1\nn\n", "3 4\nabab\nabab\nabab\n", "5 5\nzbacg\nbaaac\naaaaa\ndaaae\nweadd\n", "1 1\nm\n", "3 3\naba\naaa\naaa\n", "5 5\nzbacf\nbcaaa\naaaaa\neaaad\nveadd\n", "5 5\na...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Carousel Boutique is busy again! Rarity has decided to visit the pony ball and she surely needs a new dress, because going out in the same dress several times is a sign of bad manners...
1418_C. Mortal Kombat Tower_8477
You and your friend are playing the game Mortal Kombat XI. You are trying to pass a challenge tower. There are n bosses in this tower, numbered from 1 to n. The type of the i-th boss is a_i. If the i-th boss is easy then its type is a_i = 0, otherwise this boss is hard and its type is a_i = 1. During one session, eith...
t = int(input()) for w in range(t): n = int(input()) a = list(map(int, input().split())) p = 0 if a[0] == 0 else 1 a = a[1:] n1 = 0 for x in a: if x == 0: n1 = 0 else: n1 += 1 if n1 % 3 == 0: p += 1 print(p)
{ "input": [ "6\n8\n1 0 1 1 0 1 1 1\n5\n1 1 1 1 0\n7\n1 1 1 1 0 0 1\n6\n1 1 1 1 1 1\n1\n1\n1\n0\n", "6\n8\n1 0 1 1 0 1 1 1\n5\n1 1 1 1 1\n7\n1 1 1 1 0 0 1\n6\n1 1 1 1 1 1\n1\n1\n1\n0\n", "6\n8\n1 0 1 1 0 1 0 1\n5\n1 1 1 1 1\n7\n1 1 1 1 0 0 1\n6\n1 1 1 1 1 1\n1\n1\n1\n0\n", "6\n8\n1 0 1 1 0 1 1 1\n5\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You and your friend are playing the game Mortal Kombat XI. You are trying to pass a challenge tower. There are n bosses in this tower, numbered from 1 to n. The type of the i-th boss ...
1436_B. Prime Square_8481
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square — a prime square. A square of size n × n is called prime if ...
t = int(input()) for _ in range(t): n = int(input()) for j in range(n): b = [0] * n b[j] = 1 b[-j-1] = 1 if j != (n-1) and j != (n//2): print(*b) else: if n % 2 == 0: print(*b) else: if j == (n-1): ...
{ "input": [ "2\n4\n2\n", "10\n65\n70\n66\n68\n69\n62\n64\n63\n61\n67\n", "10\n12\n16\n13\n17\n14\n20\n19\n11\n18\n15\n", "10\n2\n3\n3\n2\n2\n3\n2\n3\n2\n3\n", "10\n30\n24\n21\n22\n25\n26\n27\n23\n29\n28\n", "1\n13\n", "1\n20\n", "10\n50\n49\n45\n46\n42\n44\n48\n41\n43\n47\n", "1\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no s...