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
168_B. Wizards and Minimal Spell_19857
Let's dive into one of the most interesting areas of magic — writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells. Each spell consists of several lines. The line, whose first non-space chara...
v = [] c = [] resp = "" while True: try: v.append(input()) except: break i = 0 while i < len(v): k = v[i].replace(" ","") if(k == ""): v[i] = k c.append(False) elif(k[0] == '#'): c.append(True) else: v[i] = k c.append(False) i+=1 n = len(v) i = 0 while i < n - 1: if(not c[i] and not c[i+1]): ...
{ "input": [ "#\n\n#\n", "# include &lt;cstdio&gt;\n\nusing namespace std;\n\nint main ( ){\nputs(\"Hello # World\"); #\n#\n}\n", "# include <cstdio>\n\nusing namespace std;\n\nint main ( ){\nputs(\"Hello # World\"); #\n#\n}\n", "# a \n # a \n\n ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's dive into one of the most interesting areas of magic — writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic wo...
20_C. Dijkstra?_19861
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. Input The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m l...
from collections import defaultdict from heapq import heappush, heapify, heappop INF = 10 ** 18 class Graph: def __init__(self): self.adj_list = defaultdict(list) def add_edge(self, src, dest, cost): self.adj_list[src].append((dest, cost)) self.adj_list[dest].append((src, cost)) def dijkstra(graph, src, de...
{ "input": [ "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1\n", "5 6\n1 2 2\n2 5 5\n2 3 4\n1 4 1\n4 3 3\n3 5 1\n", "2 1\n1 2 1\n", "3 1\n1 2 1\n", "10 10\n1 4 201\n2 3 238\n3 4 40\n3 6 231\n3 8 45\n4 5 227\n4 6 58\n4 9 55\n5 7 14\n6 10 242\n", "10 10\n1 5 178\n1 8 221\n2 7 92\n2 8 159\n3 5 55\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n. Input The first lin...
235_E. Number Challenge_19865
Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum: <image> Find the sum modulo 1073741824 (230). Input The first line contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 2000). Output Print a sing...
primes = [] d = {} for i in range(2, 100): if i not in d: primes.append(i) j = 2 while i * j < 100: d[i*j] = 1 j += 1 def solve(x,y,z): d = {} for p in primes: d[p] = 0 for p in primes: if p > x and p > y and p > z: break ...
{ "input": [ "10 10 10\n", "4 4 4\n", "2 2 2\n", "31 51 29\n", "46 93 44\n", "36 3 32\n", "76 12 17\n", "53 29 17\n", "40 15 33\n", "4 21 45\n", "80 15 4\n", "15 93 87\n", "23 9 44\n", "63 96 11\n", "22 87 4\n", "13 22 24\n", "100 9 88\n", "33 16...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's denote d(n) as the number of divisors of a positive integer n. You are given three integers a, b and c. Your task is to calculate the following sum: <image> Find the sum modul...
260_C. Balls and Boxes_19869
Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, let's assume that its number is i, took all balls out from it (it is guaranteed that this box originally had at least one ball), and began putting ...
n, x = map(int,input().split()) a = [int(i) for i in input().split()] j = x - 1 z = min(a) while a[j] != z: if j == 0: j = n - 1 else: j -= 1 m = a[j] k = 0 if x - 1 > j: for i in range(n): if j < i <= x - 1: a[i] -= (m + 1) k += (m + 1) else: ...
{ "input": [ "5 2\n3 2 0 2 7\n", "3 3\n2 3 1\n", "4 4\n4 3 1 6\n", "48 34\n227460647 746912226 53993109 682685525 621533698 666833117 492590398 167395931 678377836 66509684 638633255 713194369 386921920 34175132 704550051 220688091 499436760 495071385 102952101 137372655 0 720974146 209203457 94668210...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Vasya had n boxes with balls in the room. The boxes stood in a row and were numbered with numbers from 1 to n from left to right. Once Vasya chose one of the boxes, let's assu...
284_C. Cows and Sequence_19873
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one of the following: 1. Add the integer xi to the first ai elements of the sequence. 2. Append an integer ki to the end of the sequenc...
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUF...
{ "input": [ "5\n2 1\n3\n2 3\n2 1\n3\n", "6\n2 1\n1 2 20\n2 2\n1 2 -3\n3\n3\n", "1\n1 1 1\n", "5\n2 -980\n1 2 -156\n2 641\n2 -253\n2 -514\n", "2\n2 1\n1 2 1\n", "1\n2 1\n", "1\n2 0\n", "5\n1 1 7\n1 1 7\n1 1 7\n2 5\n1 2 2\n", "5\n2 1\n1 2 1\n2 1\n2 1\n1 2 1\n", "5\n1 1 -48\n1 1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform n operations. Each operation is one ...
332_A. Down the Hatch!_19880
Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice! Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to pl...
n = int(input()) moves = input() i = n count = 0 while(i < len(moves)): m = moves[i - 1] if(m == moves[i - 2] and m == moves[i - 3] and m == moves[i - 3]): count += 1 i += n print(count)
{ "input": [ "4\nabbba\n", "4\nabbab\n", "2000\na\n", "2000\naabaaabaabababbbbbbabbbbb\n", "5\nbbbbb\n", "4\nababba\n", "6\naaaaaaaaaaaaaaaa\n", "4\nb\n", "7\nabbbaaabbbaaaab\n", "4\nabab\n", "4\nabaa\n", "4\na\n", "4\nbbb\n", "4\naaaaaa\n", "4\naaa\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice! Yesterday one student, Vasya and...
355_A. Vasya and Digital Root_19884
Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you. Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number n equals to: 1. dr(n) = S(n), if S(n) < 10; 2. dr(n) = dr( S(n) ), i...
k,d=map(int,input().split()) if(k==1 and d==0): print(0) elif(d==0): print("No solution") else: print(str(d)+('0'*(k-1)))
{ "input": [ "4 4\n", "1 0\n", "5 1\n", "100 4\n", "1000 1\n", "487 0\n", "1000 5\n", "960 6\n", "893 3\n", "955 7\n", "734 9\n", "998 2\n", "2 0\n", "8 7\n", "1 9\n", "678 8\n", "22 9\n", "13 5\n", "1 1\n", "123 6\n", "100 0\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you. Let's assume that S(n) is the sum of digits of number n, for example, ...
379_B. New Year Present_19888
The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not a contest) money. He's put n empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided t...
n = int(input()) l = [int(x) for x in input().split()] s = '' for x in l[:-1]: if x: s += 'P' for i in range(x - 1): s += 'RLP' s += 'R' if l[-1]: s += 'P' for i in range(l[-1] - 1): s += 'LRP' print(s)
{ "input": [ "4\n0 2 0 2\n", "2\n1 2\n", "10\n2 3 4 0 0 1 1 3 4 2\n", "2\n6 0\n", "10\n0 0 0 0 0 0 0 0 1 0\n", "5\n2 2 2 2 2\n", "10\n2 3 4 0 0 1 1 3 4 3\n", "2\n6 1\n", "5\n2 2 2 1 2\n", "2\n1 0\n", "10\n2 1 4 0 0 1 1 3 4 3\n", "2\n6 2\n", "5\n2 1 2 1 2\n", "2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception. Vasily knows that the best present is (no, it's not ...
44_C. Holidays_19896
School holidays come in Berland. The holidays are going to continue for n days. The students of school №N are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are i...
n, m = map(int, input().split()) c = [0] * n for i in range(m): a, b = map(int, input().split()) for j in range(a - 1, b): c[j] += 1 for i in range(n): if c[i] != 1: print(i + 1, c[i]) exit() print('OK') # Made By Mostafa_Khaled
{ "input": [ "10 5\n1 2\n2 3\n4 5\n7 8\n9 10\n", "10 5\n1 2\n3 3\n4 6\n7 7\n8 10\n", "10 5\n1 2\n3 3\n5 7\n7 7\n7 10\n", "10 9\n1 1\n2 2\n3 4\n6 6\n8 8\n8 10\n10 10\n10 10\n10 10\n", "100 7\n1 8\n9 26\n27 28\n29 30\n31 38\n39 95\n96 100\n", "50 50\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6\n7 7\n8 8\n9 9\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: School holidays come in Berland. The holidays are going to continue for n days. The students of school №N are having the time of their lives and the IT teacher Marina Sergeyevna, who ...
519_C. A and B and Team Training_19902
A and B are preparing themselves for programming contests. An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solvi...
a, b = map(int, input().split()) a, b = min(a, b), max(a, b) if b-a >= a: print(a) else: res = b-a a -= res res += (a//3)*2 if a%3 == 2: res += 1 print(res)
{ "input": [ "4 5\n", "2 6\n", "20001 10001\n", "10 0\n", "250000 500000\n", "39935 123534\n", "10001 20001\n", "0 21233\n", "20001 10000\n", "473686 122443\n", "20004 10001\n", "0 6\n", "70 100\n", "33333 77777\n", "12523 0\n", "1 1\n", "0 0\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A and B are preparing themselves for programming contests. An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who...
545_D. Queue_19906
Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A person will be disappointed if the time he waits is more than the time needed to serve him. The time a person waits is the total time wh...
n=int(input()) l=list(map(int,input().split())) l.sort() s=0 c=0 for i in l: if(i>=s): c=c+1 s=s+i print(c)
{ "input": [ "5\n15 2 1 5 3\n", "20\n16839799 17525904 91276752 42650694 60106463 12243176 54892123 25142243 16015971 41250998 11150057 6994983 67700784 16562412 82163675 46178521 33914268 91966607 93976858 84100064\n", "15\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "10\n10000 40000 10000 50000 20000 100000 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little girl Susie went shopping with her mom and she wondered how to improve service quality. There are n people in the queue. For each person we know time ti needed to serve him. A...
572_A. Arrays_19910
You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array. Input The first line contains t...
na,nb=map(int,input().split()) k,m=map(int,input().split()) a=[int(z)for z in input().split()] b=[int(z)for z in input().split()] print("YNEOS"[a[k-1]>=b[-m]::2]) # Interesting. --YKF
{ "input": [ "5 2\n3 1\n1 1 1 1 1\n2 2\n", "3 3\n2 1\n1 2 3\n3 4 5\n", "3 3\n3 3\n1 2 3\n3 4 5\n", "3 4\n2 2\n5 6 7\n1 2 3 4\n", "10 15\n10 1\n1 1 5 17 22 29 32 36 39 48\n9 10 20 23 26 26 32 32 33 39 43 45 47 49 49\n", "3 3\n1 2\n1 2 3\n1 2 3\n", "10 15\n1 15\n91 91 91 92 92 94 94 95 98 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose k numbers in array A and choose m numbers in array B so...
615_B. Longtail Hedgehog_19917
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hed...
from sys import stdin,stdout,setrecursionlimit setrecursionlimit(10**5) from collections import defaultdict nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int,stdin.readline().split())) PI=float('inf') def dfs(src): vis[src]=1 for neigh in g[src]: if neigh<src: if not vis[neigh]...
{ "input": [ "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n", "6 5\n1 2\n1 3\n1 4\n1 5\n1 6\n", "5 9\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n2 5\n", "2 1\n1 2\n", "5 8\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n", "10 10\n6 3\n2 9\n9 4\n4 5\n10 3\n8 3\n10 5\n7 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segme...
634_D. Package Delivery_19921
Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at the point d. Johnny's truck has a gas tank that holds exactly n liters, and his tank is initially full. As he drives, the truck consumes ...
destination, max_gas_tank_volume, gas_prices_number = map(int, input().split()) start_point = 0 gas_prices = {start_point:0} for i in range(gas_prices_number): coordinate, price = map(int, input().split()) gas_prices[coordinate] = price points = sorted(gas_prices.keys(), reverse = True) current_point = start_po...
{ "input": [ "16 5 2\n8 2\n5 1\n", "10 4 4\n3 5\n5 8\n6 3\n8 4\n", "281 12 23\n178 650197\n129 288456\n34 924882\n43 472160\n207 957083\n103 724815\n167 308008\n135 906958\n74 242828\n229 146026\n85 241042\n22 39127\n62 47524\n113 760274\n156 562141\n10 209057\n50 714473\n201 164128\n97 624021\n120 102709...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Johnny drives a truck and must deliver a package from his hometown to the district center. His hometown is located at point 0 on a number line, and the district center is located at t...
663_B. International Olympiad_19925
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Amo...
tn=[0]*10000 ts=[0]*10000 a=1989 tn[1]=1989 ts[1]=9 for i in range(1,12): a=a+(10**i) tn[i+1]=a ts[i+1]=int(str(a)[-i-1:]) noc=int(input()) for fk in range(noc): a=input()[4:] temp=len(a) a=int(a) print((a-ts[temp])%(10**temp)+tn[temp])
{ "input": [ "5\nIAO'15\nIAO'2015\nIAO'1\nIAO'9\nIAO'0\n", "4\nIAO'9\nIAO'99\nIAO'999\nIAO'9999\n", "2\nIAO'0\nIAO'00\n", "1\nIAO'10005000\n", "1\nIAO'100000\n", "1\nIAO'11100000\n", "1\nIAO'113097\n", "1\nIAO'001\n", "2\nIAO'999999999\nIAO'999999999\n", "1\nIAO'1112121\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of conseq...
755_A. PolandBall and Hypothesis_19935
PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m number n·m + 1 is a prime number". Unfortunately, PolandBall is not experienced yet and doesn't know that his hypothesis is incorrect. Co...
import sys n = int(input()) for m in range(1, 1001): for j in range(2, 1001): if j < (n * m + 1) and (n * m + 1) % j == 0: print(m) sys.exit(0)
{ "input": [ "4\n", "3\n", "986\n", "978\n", "13\n", "21\n", "980\n", "985\n", "977\n", "17\n", "11\n", "999\n", "330\n", "20\n", "306\n", "18\n", "996\n", "6\n", "270\n", "2\n", "984\n", "336\n", "210\n", "1\n", "15\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: PolandBall is a young, clever Ball. He is interested in prime numbers. He has stated a following hypothesis: "There exists such a positive integer n that for each positive integer m n...
776_B. Sherlock and his girlfriend_19939
Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry. He bought n pieces of jewelry. The i-th piece has price equal to i + 1, that is, the prices of the jewelry are 2, 3, 4, ... n + 1. Watson gave Sherlock a challenge to color these jewelry pieces such that t...
def sieve(n): mark = [True]*(n+1) p = 2 while p*p<=n: if mark[p]: for i in range(p*p,n+1,p): mark[i] = False p+=1 for i in range(2,n+1): if mark[i]: print(1,end=" ") else: print(2,end=" ") n = int(input()) if n==1 or n==2: print(1) else: print(2) sieve(n+1)
{ "input": [ "3\n", "4\n", "5137\n", "1\n", "4\n", "28163\n", "728\n", "1054\n", "19283\n", "10\n", "17\n", "85\n", "15346\n", "287\n", "36325\n", "1696\n", "3509\n", "2023\n", "3202\n", "13736\n", "3466\n", "17307\n", "641\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her some jewelry. He bought n pieces of jewelry. The i-th piece has price equal to i + ...
7_C. Line_19943
A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. Input The first line contains three integers A, B and C ( - 2·109 ≤ A, B, C ≤ 2·109) — corres...
def gcd(x,y): if x % y == 0: return y else: return gcd(y,x%y) def bezout(x,y): if x % y == 0: return (0,1) else: t,s = bezout (y,x%y) return (s,t-(x//y)*s) a,b,c = map(int,input().split()) if a == 0: if c % b == 0: print("%d %d" % (0,-c//b)) ...
{ "input": [ "2 5 3\n", "-957757861 308710346 45337024\n", "1 2000000000 -1\n", "999999999 -1000000000 500000000\n", "427055698 738296578 -52640953\n", "592707810 829317963 -753392742\n", "-1548994394 -1586527767 -1203252104\n", "1000000013 -1 135\n", "546348890 -29226055 -34113518...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from - 5·1018 to 5·1018 inclusive, or t...
846_F. Random Query_19949
You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). If l > r, then you swap values of l and r. You have to calculate the expected value of the number of unique elements in segment of the ar...
n=int(input()) a=list(map(int,input().split())) lastocc=[0]*1000006 ans=[0]*n ans[0]=1 lastocc[a[0]]=1 for i in range(1,n): ans[i]=ans[i-1]+(i+1-lastocc[a[i]]) lastocc[a[i]]=i+1 print((2*sum(ans)-n)/(n*n))
{ "input": [ "2\n2 2\n", "2\n1 2\n", "10\n9 6 8 5 5 2 8 9 2 2\n", "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 17 40 24 19 27 21\n", "1\n1000000\n", "10\n9 6 8 5 5 2 8 9 2 1\n", "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 17 40 24 19 3 21\n", "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a consisting of n positive integers. You pick two integer numbers l and r from 1 to n, inclusive (numbers are picked randomly, equiprobably and independently). ...
868_B. Race Against Time_19953
Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other. The entire universe turned into an enormous clock face with ...
##a = list(map(int, input().split())) ##print(' '.join(map(str, res))) [h, m, s, t1, t2] = list(map(int, input().split())) if h == 12: h = 0 if t1 == 12: t1 = 0 if t2 == 12: t2 = 0 if t1 > t2: t1, t2 = t2, t1 #sa = s*360/60 #ma = m*360/60+s*360/(60*60) #ha = h*360/12+m*360/(12*60)+s*360/(12*60*60)...
{ "input": [ "3 47 0 4 9\n", "12 0 1 12 1\n", "12 30 45 3 11\n", "6 54 48 12 6\n", "2 10 10 1 6\n", "3 7 32 11 10\n", "3 30 45 10 11\n", "10 29 30 4 7\n", "2 54 14 2 12\n", "5 26 14 1 12\n", "2 0 0 1 3\n", "3 16 16 3 4\n", "3 5 1 1 2\n", "12 9 55 10 2\n", "1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong...
893_E. Counting Arrays_19957
You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: * There are y elements in F, and all of them are integer numbers; * <image>. You have to count the number of pairwise distinct arrays that are y-factorizations of x. Two array...
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase import io from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque impo...
{ "input": [ "2\n6 3\n4 2\n", "1\n65536 1000000\n", "1\n524288 1000000\n", "1\n5612 11399\n", "1\n36157 1000000\n", "1\n2904 11399\n", "2\n6 3\n4 4\n", "1\n2904 8217\n", "2\n6 3\n4 3\n", "1\n1754 8217\n", "2\n6 3\n4 5\n", "1\n397 8217\n", "2\n7 3\n4 5\n", "1\n39...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given two positive integer numbers x and y. An array F is called an y-factorization of x iff the following conditions are met: * There are y elements in F, and all of them ...
915_B. Browser_19961
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this se...
n,pos,l,r = [int(i) for i in input().split()] time_l = 0; if l != 1: time_l += abs(pos - l) + 1 # move to l and delete pos1 = l else: pos1 = pos if r != n: time_l += abs(r-pos1) + 1 # move to r and delete time_r = 0; if r != n: time_r += abs(pos - r) + 1 # move to l and delete pos1 = r else: ...
{ "input": [ "5 2 1 5\n", "6 3 1 3\n", "6 3 2 4\n", "100 54 52 55\n", "6 1 2 5\n", "100 65 5 13\n", "100 65 58 60\n", "8 5 3 6\n", "10 5 3 6\n", "10 2 4 7\n", "3 3 1 1\n", "100 33 50 50\n", "10 4 2 5\n", "100 50 3 79\n", "100 50 49 99\n", "100 50 2 3\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba nee...
938_C. Constructing Tests_19965
Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m × m of this matrix contains at least one zero. Consider the following problem: You are given two integers n and m. You have to construct an m-free square matrix of size n × n such that...
import math def div(k): res = [] for i in range(1,int(math.sqrt(k))+2): if k % i == 0: res.append(i) return res t = int(input()) for i in range(t): x = int(input()) if x == 0: print("1 1") elif x == 1: print(-1) else: flag = Fal...
{ "input": [ "3\n21\n0\n1\n", "1\n28\n", "1\n56\n", "1\n14\n", "1\n9\n", "1\n2925\n", "1\n5704\n", "1\n999944\n", "1\n1980\n", "1\n144\n", "1\n297540\n", "1\n25\n", "1\n4860\n", "1\n2\n", "1\n36\n", "1\n6\n", "1\n4104\n", "1\n16\n", "1\n10\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's denote a m-free matrix as a binary (that is, consisting of only 1's and 0's) matrix such that every square submatrix of size m × m of this matrix contains at least one zero. C...
963_C. Cutting Rectangle_19969
A rectangle with sides A and B is cut into rectangles with cuts parallel to its sides. For example, if p horizontal and q vertical cuts were made, (p + 1) ⋅ (q + 1) rectangles were left after the cutting. After the cutting, rectangles were of n different types. Two rectangles are different if at least one side of one r...
n =int(input()) w=[] h=[] c=[] cntw={} cnth={} gcdC=0 cntC=0 def insert1(a,b,c): if not a in b : b[a]=c else : b[a]=b[a]+c def gcd(a,b): if a % b == 0 : return b else : return gcd(b,a%b) for i in range(0, n): a,b,d = map(int,input().split()) w.append(a) h.append(b) c.append(d) insert1(a,cntw,d) ins...
{ "input": [ "2\n1 2 5\n2 3 5\n", "1\n1 1 9\n", "2\n2 3 20\n2 4 40\n", "2\n1 2 5\n2 3 10\n", "2\n2 2 20\n2 4 40\n", "2\n2 2 20\n2 7 16\n", "1\n1 1 8\n", "2\n2 2 5\n2 -1 10\n", "2\n1 2 5\n2 0 10\n", "2\n2 2 20\n2 7 40\n", "2\n1 2 5\n2 1 10\n", "2\n1 2 5\n2 -1 10\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A rectangle with sides A and B is cut into rectangles with cuts parallel to its sides. For example, if p horizontal and q vertical cuts were made, (p + 1) ⋅ (q + 1) rectangles were le...
990_F. Flow Control_19972
You have to handle a very complex water distribution system. The system consists of n junctions and m pipes, i-th pipe connects junctions x_i and y_i. The only thing you can do is adjusting the pipes. You have to choose m integer numbers f_1, f_2, ..., f_m and use them as pipe settings. i-th pipe will distribute f_i u...
import sys from time import time t1 = time() #sys.setrecursionlimit(300000) def i_ints(): return list(map(int, sys.stdin.readline().split())) def main(): limit =10**10 n, = i_ints() s = [0] + i_ints() if sum(s): print("Impossible") return print("Possible") if n ==...
{ "input": [ "4\n3 -10 6 4\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n", "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n", "2\n-1 1\n1\n2 1\n", "1\n0\n0\n", "1\n123\n0\n", "2\n-1 1\n1\n1 2\n", "4\n3 -10 6 1\n5\n1 2\n3 2\n2 4\n3 4\n3 1\n", "2\n-2 1\n1\n2 1\n", "4\n3 -10 6 1\n5\n1 4\n3 2\n2 4\n3 4\n3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have to handle a very complex water distribution system. The system consists of n junctions and m pipes, i-th pipe connects junctions x_i and y_i. The only thing you can do is ad...
p02614 AtCoder Beginner Contest 173 - H and V_19986
We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given to you as a character c_{i,j}: the square is white if c_{i,j} is `.`, and black if c_{i,j} is `#`. Consider doing the following operation...
from itertools import groupby, accumulate, product, permutations, combinations H, W, K = map(int, input().split()) S = [input() for _ in range(H)] ans = 0 for p in product([0,1],repeat=H+W): cnt = 0 for i in range(H): for j in range(W): if S[i][j]=='#' and p[i]==0 and p[H+j]==0: cnt += 1 if cnt ...
{ "input": [ "2 3 4\n..#", "6 6 8\n..##..\n.#..#.\n....#\n\n....#\n....#", "2 3 2\n..#", "2 2 3", "2 3 2\n..#\n###", "2 2 4\n..#", "4 3 2\n..#\n###", "4 3 2\n..#\n$##", "2 3 0\n.-#", "3 3 0\n/,#", "6 3 0\n/,#", "6 3 1\n/,#", "6 3 0\n/,$", "6 2 0\n/,$", "1 4 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a grid of H rows and W columns of squares. The color of the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) is given t...
p02745 Panasonic Programming Contest 2020 - Three Substrings_19990
Snuke has a string s. From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows: * Choose a non-empty (contiguous) substring of s (possibly s itself). Then, replace some characters (possibly all or none) in it with `?`s. For example, if s is `mississippi`, we can choose the su...
R=range def F(a,b):A=len(a);r=[all([len(set([a[i+j],b[j],'?']))<3for j in R(min(A-i,len(b)))])for i in R(A)];return r+[1] def Z(X): i,j,k=X;U,V,W=M[i][j],M[j][k],M[i][k];A,B,C=map(len,[S[i]for i in X]);q=A+B+C for l in R(A+1): if U[l]: for r in R(l,A+B+1): if(B<r-l or V[r-l])*W[min(A,r)]:q=min(q,max(A,l+B,r+...
{ "input": [ "a?c\nder\ncod", "atcoder\natcoder\n???????", "`?c\nder\ncod", "atcoder\natcoder\n?????>?", "`?d\nder\ncod", "redocta\natcoder\n?????>?", "eacodtr\naoetdcr\n?????>>", "ebdobur\nppedact\n>>????@", "qcbdpvg\nbfpepbs\n?>A>>@<", "tocojcb\nbs_fpph\n=?>@@@?", "`?d\nd...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke has a string s. From this string, Anuke, Bnuke, and Cnuke obtained strings a, b, and c, respectively, as follows: * Choose a non-empty (contiguous) substring of s (possibly s i...
p02880 AtCoder Beginner Contest 144 - 81_19994
Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. Given an integer N, determine whether N can be represented as the product of two integers between 1 and 9. If it can, print `Yes`; if it cannot, print `No`. Constraints * 1 \leq N \leq 100 * N is an int...
n=int(input()) li=[i*j for i in range(1,10) for j in range(1,10)] print("Yes" if n in li else "No")
{ "input": [ "10", "81", "50", "2", "84", "83", "1", "107", "92", "0", "100", "64", "-1", "110", "91", "-2", "111", "167", "-3", "101", "235", "-4", "001", "145", "3", "199", "6", "375", "4", "348",...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. Given an integer N, determine whether N can be represented as the p...
p03014 AtCoder Beginner Contest 129 - Lamp_19998
There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares. Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it. The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and...
H, W = map(int, input().split()) S = [] for i in range(H): S.append(list(input())) U = [[0]*(W+1) for h in range(H+1)] L = [[0]*(W+1) for h in range(H+1)] for h in range(H): for w in range(W): if S[h][w] == '#': continue U[h+1][w+1] = U[h][w+1] + 1 L[h+1][w+1] = L[h+1][w] + 1 ans = 0 D =...
{ "input": [ "4 6\n#..#..\n.....#\n....#.\n#.#...", "4 6\n..#..\n.....#\n....#.\n.#...", "8 8\n..#...#.\n....#...\n......\n..###..#\n...#..#.\n....#.\n...#...\n.#..#", "3 6\n#..#..\n.....#\n....#.\n#.#...", "8 8\n..#...#.\n....#...\n......\n..###..#\n...#..#-\n....#.\n...#...\n.#..#", "4 8\n#....
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares. Snuke is going to choose one of the squares not occupied by an obstacle...
p03155 AISing Programming Contest 2019 - Bulletin Board_20002
It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board. The bulletin board is in the form of a grid with N rows and N columns, and the notice will occupy a rectangular region with H rows and W columns. How many ways are there to choose where ...
N = int(input()) H = int(input()) W = int(input()) if H > N or W > N: print(0) else: print((N-W+1)*(N-H+1))
{ "input": [ "5\n4\n2", "3\n2\n3", "100\n1\n1", "3\n4\n3", "100\n2\n1", "3\n3\n3", "100\n0\n1", "3\n3\n1", "100\n-1\n1", "3\n2\n1", "100\n-2\n1", "3\n0\n1", "100\n-1\n0", "3\n-1\n1", "100\n-1\n-1", "3\n-2\n1", "100\n-2\n-1", "3\n-2\n0", "100\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It has been decided that a programming contest sponsored by company A will be held, so we will post the notice on a bulletin board. The bulletin board is in the form of a grid with N...
p03298 AtCoder Grand Contest 026 - String Coloring_20006
You are given a string S of length 2N consisting of lowercase English letters. There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the following condition? * The string obtained by reading the characters painted red from left to right is equal to the string obtained by r...
def f(): n=int(input()) s=list(input()) a,b=s[n-1::-1],s[n:] from collections import defaultdict ad=defaultdict(int) bd=defaultdict(int) for i in range(2**n): sa,ta,sb,tb="","","","" for j in range(n): if i%2: sa+=a[j] sb+=b[j] ...
{ "input": [ "4\ncabaacba", "18\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "11\nmippiisssisssiipsspiim", "4\nabcdefgh", "4\ncacaabba", "4\nacbaacab", "18\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`aaaa", "11\nmippiisstisssiipsspiim", "4\nhgfedcba", "4\nabbaacac", "18\naaaaaaaaaaaaaaaaaa...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a string S of length 2N consisting of lowercase English letters. There are 2^{2N} ways to color each character in S red or blue. Among these ways, how many satisfy the ...
p03456 AtCoder Beginner Contest 086 - 1 21_20010
AtCoDeer the deer has found two positive integers, a and b. Determine whether the concatenation of a and b in this order is a square number. Constraints * 1 ≤ a,b ≤ 100 * a and b are integers. Input Input is given from Standard Input in the following format: a b Output If the concatenation of a and b in this o...
a,b = input().split() c = int(a+b)**0.5 print("Yes" if c.is_integer() else "No")
{ "input": [ "1 21", "12 10", "100 100", "2 21", "000 001", "5 10", "110 100", "2 22", "7 10", "101 100", "4 22", "7 16", "101 101", "8 22", "7 20", "101 111", "14 22", "7 6", "001 101", "9 22", "12 6", "100 101", "9 11", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoDeer the deer has found two positive integers, a and b. Determine whether the concatenation of a and b in this order is a square number. Constraints * 1 ≤ a,b ≤ 100 * a and b ar...
p03617 AtCoder Grand Contest 019 - Ice Tea Store_20014
You've come to your favorite store Infinitesco to buy some ice tea. The store sells ice tea in bottles of different volumes at different costs. Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen. The store has an infinite supply...
Q, H, S, D = map(int, input().split()) N = int(input()) H = min(H, 2 * Q) S = min(S, 2 * H) D = min(D, 2 * S) print((N // 2) * D + (N % 2) * S)
{ "input": [ "10 100 1000 10000\n1", "20 30 70 90\n3", "10000 1000 100 10\n1", "12345678 87654321 12345678 87654321\n123456789", "10 100 1010 10000\n1", "20 30 70 129\n3", "10000 1001 100 10\n1", "12345678 87654321 12345678 87654321\n226515686", "20 30 70 83\n3", "10000 1001 10...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've come to your favorite store Infinitesco to buy some ice tea. The store sells ice tea in bottles of different volumes at different costs. Specifically, a 0.25-liter bottle cost...
p03775 AtCoder Beginner Contest 057 - Digits in Multiplication_20018
You are given an integer N. For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B. For example, F(3,11) = 2 since 3 has one digit and 11 has two digits. Find the minimum value of F...
n=int(input()) ans=len(str(n)) for i in range(1,int(n**0.5)+2): if n%i==0: ans=min(ans,max(len(str(i)),len(str(n//i)))) print(ans)
{ "input": [ "1000003", "10000", "9876543210", "101914", "17346", "2771679283", "7026", "5110764300", "472", "10211397040", "16034112351", "010", "53937807463", "2375578529", "102392", "3360905534", "1567311305", "6220", "748", "186770780...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an integer N. For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number ...
p03944 AtCoder Beginner Contest 047 - Snuke's Coloring 2 (ABC Edit)_20022
There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white. Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) po...
w,h,n=map(int,input().split()) x1=0 y1=0 for i in range(n): x,y,a=map(int,input().split()) if a==1: x1=max(x1,x) elif a==2: w=min(w,x) elif a==3: y1=max(y,y1) elif a==4: h=min(h,y) print((w-x1)*(h-y1) if w > x1 and h > y1 else 0)
{ "input": [ "5 4 3\n2 1 1\n3 3 4\n1 4 2", "5 4 2\n2 1 1\n3 3 4", "10 10 5\n1 6 1\n4 1 3\n6 9 4\n9 4 2\n3 1 3", "5 4 3\n2 1 1\n3 6 4\n1 4 2", "5 4 2\n2 1 1\n6 3 4", "5 4 2\n2 1 2\n6 3 4", "10 10 5\n1 6 1\n4 1 1\n6 9 4\n9 4 2\n3 1 4", "5 4 1\n2 1 2\n6 3 4", "5 4 3\n2 0 2\n2 6 2\n1 4...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the ...
p00036 A Figure on Surface_20026
There is a plane like Figure 1 with 8 vertical and 8 horizontal squares. □ | □ | □ | □ | □ | □ | □ | □ --- | --- | --- | --- | --- | --- | --- | --- □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □ | □ | □ □ | □ | □ | □ | □ | □...
import sys readline = sys.stdin.readline write = sys.stdout.write B = [771, 16843009, 15, 66306, 1539, 131841, 774] C = "ABCDEFG" def solve(): N = 8 su = 0 for i in range(N): s = readline().strip() for j in range(N): if s[j] == '1': su |= 1 << (i*N + j) for i ...
{ "input": [ "00000000\n00000000\n01100000\n00110000\n00000000\n00000000\n00000000\n00000000\n\n00011110\n00000000\n00000000\n00000000\n00000000\n00000000\n00000000\n00000000\n\n00000000\n00000000\n00110000\n00110000\n00000000\n00000000\n00000000\n00000000" ], "output": [ "E\nC\nA" ] }
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a plane like Figure 1 with 8 vertical and 8 horizontal squares. □ | □ | □ | □ | □ | □ | □ | □ --- | --- | --- | --- | --- | --- | --- | --- □ | □ | □ | □ | □ | □ | □ | □ □ | ...
p00168 Kannondou_20030
There is Kannon-do in the mountain behind Ichiro's house. There are 30 steps from the foot to this Kannon-do, and Ichiro goes to Kannon-do almost every day. Ichiro can go up the stairs up to 3 steps with one foot. While playing, I noticed that there are so many types of stair climbing (the number of steps to skip). So...
import sys n=30 f=[1,1,2,4] while len(f)<=n: f.append(f[-1]+f[-2]+f[-3]) while True: n=int(input()) if n==0: sys.exit() day=f[n] if day%3650==0: print(int(day/3650)) else: print(int(day/3650)+1)
{ "input": [ "1\n10\n20\n25\n0", "1\n10\n14\n25\n0", "1\n10\n14\n23\n0", "1\n10\n14\n6\n0", "1\n1\n20\n25\n0", "1\n10\n14\n24\n0", "1\n10\n0\n25\n0", "1\n19\n14\n5\n0", "1\n18\n0\n25\n0", "1\n18\n1\n25\n0", "1\n0\n14\n6\n0", "1\n23\n14\n10\n0", "1\n18\n20\n25\n0", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is Kannon-do in the mountain behind Ichiro's house. There are 30 steps from the foot to this Kannon-do, and Ichiro goes to Kannon-do almost every day. Ichiro can go up the stair...
p00324 Bilateral Trade_20034
Aiz, which is located in cyberspace, trades information with Wakamatsu. The two countries are developing their economies by exchanging useful data with each other. The two countries, whose national policy is philanthropy and equality, and above all, the old word of the Aizu region, "what must be done", conducts regular...
n = int(input()) D = [int(input()) for i in range(n)] S = {} s = ans = 0 S[0] = -1 for i in range(n): s += D[i] if s in S: ans = max(i - S[s], ans) else: S[s] = i print(ans)
{ "input": [ "4\n1\n1\n-1\n-1", "5\n18\n102\n-155\n53\n32", "4\n0\n1\n-1\n-1", "4\n0\n2\n-1\n-1", "5\n18\n102\n-181\n53\n9", "4\n0\n0\n-1\n-1", "4\n0\n1\n-2\n-1", "5\n1\n19\n-30\n8\n2", "5\n18\n102\n-155\n53\n9", "5\n18\n146\n-181\n53\n9", "5\n33\n146\n-181\n53\n9", "4\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Aiz, which is located in cyberspace, trades information with Wakamatsu. The two countries are developing their economies by exchanging useful data with each other. The two countries, ...
p00496 Night Market_20037
Taro decided to go to the summer festival held at JOI Shrine. N night shops are open along the way to JOI Shrine. Each night shop is numbered from 1 to N in order, and the fun of playing and the time it takes to play are determined by integers. The fun of playing at night shop i is Ai, and the time it takes to play at...
def solve(): N, T, S = map(int, input().split()) a = [tuple(map(int, input().split())) for _ in [0]*N] dp = [float("-inf")]*(T+1) dp[0] = 0 for fun, mise_time in a: for prev_time, from_fun, to_fun in zip(range(T-mise_time, -1, -1), dp[T-mise_time::-1], dp[::-1]): new_time = prev...
{ "input": [ "5 20 14\n8 9\n2 4\n7 13\n6 3\n5 8", "None", "5 20 14\n8 9\n2 4\n7 13\n1 3\n5 8", "eonN", "5 20 14\n7 9\n2 4\n7 13\n1 3\n0 8", "5 20 14\n7 18\n2 4\n7 13\n1 3\n0 8", "5 5 14\n7 18\n2 4\n7 13\n1 3\n0 12", "5 5 26\n7 13\n2 6\n7 17\n1 3\n0 12", "5 2 38\n7 13\n3 0\n7 27\n1 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Taro decided to go to the summer festival held at JOI Shrine. N night shops are open along the way to JOI Shrine. Each night shop is numbered from 1 to N in order, and the fun of pla...
p00823 Molecular Formula_20042
Your mission in this problem is to write a computer program that manipulates molecular for- mulae in virtual chemistry. As in real chemistry, each molecular formula represents a molecule consisting of one or more atoms. However, it may not have chemical reality. The following are the definitions of atomic symbols and ...
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1244 from typing import List, Dict atomic_table: Dict[str, int] = {} class NoAppearAtomicSymbol(Exception): pass class Token: s: str index = 0 def __init__(self, s: str): self.s = s @property def c(self) -> str: as...
{ "input": [ "H 1\nHe 4\nC 12\nO 16\nF 19\nNe 20\nCu 64\nCc 333\nEND_OF_FIRST_PART\nH2C\n(MgF)2As\nCu(OH)2\nH((CO)2F)99\n0", "H 1\nHe 4\nC 12\nO 16\nF 19\nNe 24\nCu 64\nCc 333\nEND_OF_FIRST_PART\nH2C\n(MgF)2As\nCu(OH)2\nH((CO)2F)99\n0", "H 1\nHe 4\nC 12\nO 16\nF 22\nNe 24\nCu 64\nCc 333\nEND_OF_FIRST_PART...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Your mission in this problem is to write a computer program that manipulates molecular for- mulae in virtual chemistry. As in real chemistry, each molecular formula represents a molec...
p01087 ICPC Calculator_20047
ICPC Calculator In mathematics, we usually specify the order of operations by using parentheses. For example, 7 × (3 + 2) always means multiplying 7 by the result of 3 + 2 and never means adding 2 to the result of 7 × 3. However, there are people who do not like parentheses. International Counter of Parentheses Counci...
def inp(): global n n = int(input()) return n def strinp(): l = 0 s = list(input()) for i in s: if i == '.': l += 1 else: c = i return [l,c] def calc(p): global f l = f[p][0] c = f[p][1] p += 1 if c == '+': ans = 0 for i in range(p,n): if f[i][0] == l: break elif f[i][0] == l+1: ...
{ "input": [ "1\n9\n4\n+\n.1\n.2\n.3\n9\n+\n.0\n.+\n..*\n...1\n...*\n....1\n....2\n..0\n10\n+\n.+\n..6\n..2\n.+\n..1\n..*\n...7\n...6\n.3\n0", "1\n9\n4\n+\n.1\n.2\n.3\n9\n+\n.0\n.+\n..*\n...1\n...+\n....1\n....2\n..0\n10\n+\n.+\n..6\n..2\n.+\n..1\n..*\n...7\n...6\n.3\n0", "1\n9\n4\n+\n.1\n.2\n.3\n9\n+\n.0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: ICPC Calculator In mathematics, we usually specify the order of operations by using parentheses. For example, 7 × (3 + 2) always means multiplying 7 by the result of 3 + 2 and never ...
p01223 Saizo_20051
A TV program called "Saizo" is popular in a certain country. In this program, participants challenge field athletics and get a prize if they successfully capture it. Field athletics are made by arranging blocks of different heights in a row, and how to climb up and down the steps is important for capture (Fig. 1). You...
t = int(input()) for _ in range(t): n = int(input()) h = list(map(int, input().split())) maxv = minv = 0 for i in range(len(h)-1): maxv = max(maxv, h[i+1]-h[i]) minv = max(minv, h[i]-h[i+1]) print(maxv, minv)
{ "input": [ "5\n5\n10 70 30 50 90\n2\n20 100\n2\n100 30\n3\n50 50 50\n7\n123 45 678 901 234 567 890", "5\n5\n10 70 30 50 177\n2\n20 100\n2\n100 30\n3\n50 50 50\n7\n123 45 678 901 234 567 890", "5\n5\n10 70 5 50 177\n2\n20 100\n2\n100 30\n3\n50 50 50\n7\n123 45 678 901 234 567 890", "5\n5\n10 70 5 50 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A TV program called "Saizo" is popular in a certain country. In this program, participants challenge field athletics and get a prize if they successfully capture it. Field athletics ...
p01539 A Holiday of Miss Brute Force_20056
The full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister are now in a room lined with hexagonal tiles. The older sister seems to be very excited about the hexagon she sees for the first time. The ...
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": [ "0 0 0 2\n1\n0 1\n2 2", "0 0 0 2\n6\n0 1\n1 0\n1 -1\n0 -1\n-1 -1\n-1 0\n2 2", "0 0 0 2\n0\n2 2", "0 0 1 2\n1\n0 1\n2 2", "0 -1 0 2\n0\n2 2", "1 0 0 2\n0\n2 2", "0 0 0 2\n6\n0 1\n1 -1\n1 0\n0 -1\n-1 -1\n-1 0\n2 2", "1 2 0 0\n1\n-1 0\n4 2", "-11 -1 1 0\n1\n-1 -2\n21 33",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The full exploration sister is a very talented woman. Your sister can easily count the number of routes in a grid pattern if it is in the thousands. You and your exploration sister ar...
p01695 JAG-channel_20060
JAG-channel Nathan O. Davis operates an electronic bulletin board called JAG-channel. He is currently working on adding a new feature called Thread View. Like many other electronic bulletin boards, JAG-channel is thread-based. Here, a thread refers to a group of conversations consisting of a series of posts. There ar...
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 def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF...
{ "input": [ "6\nhoge\n.fuga\n..foobar\n..jagjag\n...zigzag\n.piyo\n8\njagjag\n.hogehoge\n..fugafuga\n...ponyoponyo\n....evaeva\n....pokemon\n...nowawa\n.buhihi\n8\nhello\n.goodmorning\n..howareyou\n.goodafternoon\n..letshavealunch\n.goodevening\n.goodnight\n..gotobed\n3\ncaution\n.themessagelengthislessthanorequ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: JAG-channel Nathan O. Davis operates an electronic bulletin board called JAG-channel. He is currently working on adding a new feature called Thread View. Like many other electronic ...
p01839 A-un Breathing_20064
A-Aun's breathing Problem Statement Maeda-san and Goto-san, who will both turn 70 years old in 2060, are long-time friends and friends who fought together at the ACM-ICPC in college. The two are still excited about competitive programming, drinking tea together. When the two of us drank tea together, Mr. Maeda said...
n = int(input()) A_cou = 0 Un_cou = 0 ans = "YES" for i in range(n): sen = input() if sen == "A": A_cou += 1 else: Un_cou += 1 if A_cou < Un_cou: ans = "NO" break if A_cou != Un_cou: ans = "NO" print(ans)
{ "input": [ "4\nA\nUn\nA\nUn", "4\nA\nUn\nB\nUn", "4\n@\nUn\nB\nUn", "4\n@\nUn\nB\nnU", "4\n@\nUn\nB\noU", "4\n@\nUn\nA\noU", "4\n@\nUn\nA\nUo", "4\n@\nTn\nA\nUo", "4\n@\nTn\nA\nVo", "4\n@\nTo\nA\nVo", "4\n@\nUo\nA\nVo", "4\n@\noU\nA\nVo", "4\n@\noU\nA\nWo", "4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A-Aun's breathing Problem Statement Maeda-san and Goto-san, who will both turn 70 years old in 2060, are long-time friends and friends who fought together at the ACM-ICPC in college...
p01975 Mapping_20067
problem AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S → S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S → S $ satisfying $ g (f (x)) = h (f (x)) $ are $ g (x). ) = Determine if h (x) $ is satisfied, and if not, configure one counterexample. Example Input 5 ...
# -*- coding: utf-8 -*- from collections import Counter def inpl(): return list(map(int, input().split())) N = int(input()) A = inpl() Ai = range(N) Ad = {a:i for i, a in enumerate(A)} F = inpl() C = [0]*N for f in F: C[Ad[f]] += 1 if not 0 in C: print("Yes") else: print("No") print(*list(map(str, A))...
{ "input": [ "5\n1 2 3 4 5\n3 4 2 5 1", "5\n2 1 3 4 5\n3 4 2 5 1", "5\n1 2 3 4 5\n2 4 3 5 1", "5\n1 3 2 4 5\n3 4 2 5 1", "5\n2 1 3 4 5\n2 4 3 5 1", "5\n1 4 3 2 5\n3 4 2 5 1", "5\n2 1 3 4 5\n3 4 2 5 1", "5\n1 2 3 4 5\n2 4 3 5 1", "5\n2 1 3 4 5\n3 4 1 5 2", "5\n1 4 3 2 5\n3 4 1 5...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem AOR Ika made a set $ S = \\ {a_1, ..., a_N \\} $ and a map $ f: S → S $. $ f (a_i) = b_i $. For any element $ x $ in the set $ S $, all maps $ g, h: S → S $ satisfying $ g (f...
p02261 Stable Sort_20072
Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' is represented by D1. Your task is to write a program which sorts a given set of cards in ascending order by their values using the Bubbl...
N = int(input()) A = [] for card in input().split(): A.append([card[0], int(card[1])]) B = A[:] for i in range(N): for j in range(N-1, i, -1): if A[j][1] < A[j-1][1]: A[j-1], A[j] = A[j], A[j-1] A = " ".join([i + str(j) for i, j in A]) print(A) print("Stable") for i in range(N-1): min...
{ "input": [ "2\nS1 H1", "5\nH4 C9 S4 D2 C3", "5\nH4 B9 S4 D2 C3", "5\nH4 C9 S4 D2 C2", "5\nH4 B9 S4 E2 C3", "5\nI4 C9 S4 D2 C2", "5\nH3 B9 S4 E2 C3", "5\nH3 B9 S4 E2 B3", "5\nH3 B9 S4 E3 B3", "5\nH3 A9 S4 E3 B3", "5\nH4 C9 S4 C2 C3", "5\nH4 B9 T4 D2 C3", "5\nI4 C9 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' i...
p02409 Official House_20076
You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tenants for each room. For each notice, you are given four integers b, f, r and v which represent that v persons entered to room r of fth fl...
a= [[[0]*10 for i in range(3)] for j in range(4)] n = int(input()) for i in range(n): b,f,r,v = map(int,input().split()) a[b-1][f-1][r-1] += v for i in range(4): for j in range(3): for k in range(10): print(" " + str(a[i][j][k]),end='') print() if i != 3: print("#"...
{ "input": [ "3\n1 1 3 8\n3 2 2 7\n4 3 8 1", "3\n1 1 3 16\n3 2 2 7\n4 3 8 1", "3\n1 1 3 8\n3 2 4 7\n4 3 8 1", "3\n1 1 3 12\n3 2 4 7\n4 3 8 1", "3\n1 1 6 12\n3 2 4 7\n4 3 8 1", "3\n1 1 10 12\n3 2 4 7\n4 3 8 1", "3\n1 1 10 12\n3 2 6 7\n4 3 8 1", "3\n1 1 10 12\n3 3 6 7\n4 3 8 1", "3\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You manage 4 buildings, each of which has 3 floors, each of which consists of 10 rooms. Write a program which reads a sequence of tenant/leaver notices, and reports the number of tena...
1015_E1. Stars Drawing (Easy Edition)_20086
A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 are not allowed). Let's consider...
n, m = map(int, input().split()) arr = [input() for i in range(n)] s1 = set() s2 = set() res = list() for i in range(n): for j in range(m): if arr[i][j] == '*': s1.add((i, j)) l = 1 while True: if i - l >= 0 and i + l < n and j - l >= 0 and j + l < m: ...
{ "input": [ "5 5\n.*...\n***..\n.*...\n.*...\n.....\n", "6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........\n", "3 3\n*.*\n.*.\n*.*\n", "5 5\n.*...\n****.\n.****\n..**.\n.....\n", "3 3\n...\n...\n...\n", "6 8\n....*...\n...**...\n..*****.\n...**...\n....*...\n........\n", "3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size o...
1039_C. Network Safety_20089
The Metropolis computer network consists of n servers, each has an encryption key in the range from 0 to 2^k - 1 assigned to it. Let c_i be the encryption key assigned to the i-th server. Additionally, m pairs of servers are directly connected via a data communication channel. Because of the encryption algorithms speci...
import sys input = sys.stdin.readline def getp(p, x): if p[x] == x: return x else: p[x] = getp(p,p[x]) return p[x] def solve(): n, m, k = map(int, input().split()) c = list(map(int, input().split())) d = dict() for i in range(m): u, v = map(int,input().split()) z = c[u-1]^c[v-1] if z in d: d[z]....
{ "input": [ "4 4 2\n0 1 0 1\n1 2\n2 3\n3 4\n4 1\n", "4 5 3\n7 1 7 2\n1 2\n2 3\n3 4\n4 1\n2 4\n", "10 40 10\n838 215 738 782 183 422 127 283 103 129\n1 2\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n4 5\n4 6\n4 7\n4 9\n4 10\n5 7\n5 8\n5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The Metropolis computer network consists of n servers, each has an encryption key in the range from 0 to 2^k - 1 assigned to it. Let c_i be the encryption key assigned to the i-th ser...
1102_F. Elongated Matrix_20095
You are given a matrix a, consisting of n rows and m columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order), but you can't change the order of cells in a row. After you pick some order of rows, you traverse the whole matrix the following way: fi...
import sys def read(): return int(input()) def reads(): return [int(x) for x in input().split()] N,M=reads() table=[reads() for i in range(N)] A=[[0]*N for i in range(N)] B=[[0]*N for i in range(N)] for i in range(N): for j in range(N): res=10**9+7 for k in range(M): res=min(res,...
{ "input": [ "6 1\n3\n6\n2\n5\n1\n4\n", "2 4\n1 2 3 4\n10 3 7 3\n", "4 2\n9 9\n10 8\n5 3\n4 3\n", "5 5\n1337 1337 1337 1337 1337\n1337 1337 1337 1337 1337\n1337 1337 1337 1337 1337\n1337 1337 1337 1337 1337\n1337 1337 1337 1337 1337\n", "1 2\n11 21\n", "2 1\n21\n11\n", "5 5\n69 69 69 69 69...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a matrix a, consisting of n rows and m columns. Each cell contains an integer in it. You can change the order of rows arbitrarily (including leaving the initial order),...
1130_E. Wrong Answer_20099
Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≤ l ≤ r ≤ n-1} ∑_{l ≤ i ≤ r} (r-l+1) ⋅ a_i. In this problem, 1 ≤ n ≤ 2 000 and |a_i| ≤ 10^6. In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it....
k = int(input()) cSum = 0 res = [-1,1] while True: if cSum + 1_000_000 < k + len(res): cSum += 1_000_000 res.append(1_000_000) else: res.append(k + len(res) - cSum) break print(len(res)) for i in res: print(i, end = ' ')
{ "input": [ "612\n", "8\n", "1000000000\n", "2000000\n", "223092870\n", "2000002\n", "6\n", "2000010\n", "2097152\n", "1999992\n", "999998\n", "1999999\n", "32768\n", "2000006\n", "65536\n", "1\n", "1999995\n", "32\n", "5\n", "2000004\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider the following problem: given an array a containing n integers (indexed from 0 to n-1), find max_{0 ≤ l ≤ r ≤ n-1} ∑_{l ≤ i ≤ r} (r-l+1) ⋅ a_i. In this problem, 1 ≤ n ≤ 2 000 ...
1151_B. Dima and a Bad XOR_20103
Student Dima from Kremland has a matrix a of size n × m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence c_1, c_2, …...
n, m = map(int, input().split()) a = [[int(i) for i in input().split()] for _ in range(n)] t = a[0][0] for i in range(1, n): t ^= a[i][0] if t != 0: print("TAK") print(' '.join('1' for i in range(n))) else: for i in range(n): for j in range(1, m): if a[i][j] != a[i][0]: ...
{ "input": [ "3 2\n0 0\n0 0\n0 0\n", "2 3\n7 7 7\n7 7 10\n", "2 2\n2 7\n2 2\n", "4 3\n1 1 1\n2 2 2\n4 3 3\n7 7 7\n", "3 2\n1 4\n2 2\n3 3\n", "3 2\n0 1\n4 4\n5 5\n", "2 1\n1\n0\n", "2 2\n8 9\n8 8\n", "1 249\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Student Dima from Kremland has a matrix a of size n × m filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise excl...
1173_A. Nauuo and Votes_20107
Nauuo is a girl who loves writing comments. One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes. It's known that there were x persons who would upvote, y persons who would downvote, and there were also another z persons who would vote, but you don't know whether they woul...
x, y, z = [int(x) for x in input().split()] if x == y and z == 0: print(0) elif x > y + z: print("+") elif y > x + z: print("-") else: print("?")
{ "input": [ "3 7 0\n", "1 1 0\n", "0 0 1\n", "2 0 1\n", "100 0 100\n", "80 63 18\n", "25 12 100\n", "80 29 11\n", "10 5 6\n", "94 37 25\n", "98 82 13\n", "21 24 18\n", "1 2 2\n", "88 88 0\n", "73 29 43\n", "58 83 39\n", "97 33 19\n", "1 3 4\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nauuo is a girl who loves writing comments. One day, she posted a comment on Codeforces, wondering whether she would get upvotes or downvotes. It's known that there were x persons w...
1190_D. Tokitsukaze and Strange Rectangle_20111
There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area. The strange area is enclosed by three lines, x = l, y = a and x = r, as its left side, its bottom side and its right side respectively, where l, r and a can be ...
import sys import copy input = sys.stdin.readline n=int(input()) P=[list(map(int,input().split())) for i in range(n)] SET_X=set() SET_Y=set() for x,y in P: SET_X.add(x) SET_Y.add(y) CX=sorted(SET_X) CY=sorted(SET_Y) LEN=len(CX) MAX=len(CX)-1 DICT_X={x:i for i,x in enumerate(CX)} DICT_Y={x:i for i,x in enu...
{ "input": [ "3\n1 1\n2 1\n3 1\n", "4\n2 1\n2 2\n3 1\n3 2\n", "3\n1 1\n1 2\n1 3\n", "4\n1 1\n1 1000000000\n1000000000 1\n1000000000 1000000000\n", "2\n7 100000\n5 100000\n", "5\n1 2\n2 1\n2 2\n2 3\n3 2\n", "8\n1 3\n2 1\n2 5\n2 6\n3 1\n4 4\n5 2\n6 2\n", "28\n2 2\n2 7\n10 7\n7 8\n10 2\n5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n points on the plane, the i-th of which is at (x_i, y_i). Tokitsukaze wants to draw a strange rectangular area and pick all the points in the area. The strange area is enc...
1209_G1. Into Blocks (easy version)_20115
This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible num...
n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} def max_frequent(s, e, a): d = {} for x in a[s: e+1]: if x not in d: d[x] = 0 d[x] += 1 return e - s + 1 - max(list(d.values())) for i, x in enumerate(a): if x not in d: ...
{ "input": [ "7 0\n3 3 1 3 2 1 2\n", "6 0\n6 6 3 3 4 4\n", "10 0\n1 2 1 2 3 1 1 1 50 1\n", "5 0\n3 7 3 7 3\n", "100 0\n91 32 10 38 92 14 100 7 48 72 47 10 76 99 56 53 41 46 68 18 37 47 61 99 16 60 12 51 17 50 69 8 82 78 34 95 3 15 79 4 51 45 83 91 81 68 79 91 16 30 6 86 72 97 63 75 67 14 50 60 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This is an easier version of the next problem. In this version, q = 0. A sequence of integers is called nice if its elements are arranged in blocks like in [3, 3, 3, 4, 1, 1]. Formal...
122_A. Lucky Division_20119
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya calls a number almost lucky if it could be evenly divided by some lucky number. Help him find ...
lucky_numbers = [4, 7, 44, 77, 47, 74, 444, 447, 474, 744, 777, 774, 747, 477] n = input() lucky = True for i in range(0, len(n)): if n[i] != '4' and n[i] != '7' : lucky = False break if lucky: print("YES") else: lucky = False for i in lucky_numbers: if int(n) % i == 0: ...
{ "input": [ "16\n", "78\n", "47\n", "42\n", "49\n", "70\n", "56\n", "298\n", "4\n", "25\n", "477\n", "48\n", "124\n", "480\n", "77\n", "94\n", "3\n", "1000\n", "141\n", "998\n", "999\n", "107\n", "882\n", "274\n", "79...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, ...
1270_G. Subset with Zero Sum_20123
You are given n integers a_1, a_2, ..., a_n, such that for each 1≤ i ≤ n holds i-n≤ a_i≤ i-1. Find some nonempty subset of these integers, whose sum is equal to 0. It can be shown that such a subset exists under given constraints. If there are several possible subsets with zero-sum, you can find any of them. Input E...
import sys input = sys.stdin.readline t=int(input()) for test in range(t): n=int(input()) A=list(map(int,input().split())) ANS=[] SET=set() NOW=1 while not (NOW in SET): ANS.append(NOW) SET.add(NOW) NOW=NOW-A[NOW-1] x=ANS.index(NOW) print(len(ANS[x:])) pr...
{ "input": [ "2\n5\n0 1 2 3 4\n4\n-3 1 1 1\n", "1\n10\n-3 -3 -3 -3 -3 -3 -3 -2 7 7\n", "1\n10\n-9 1 -7 -6 2 2 1 1 5 1\n", "1\n10\n-9 1 -1 -6 0 -4 4 4 8 2\n", "1\n10\n-3 -3 -3 -3 0 -3 -3 -2 7 7\n", "1\n10\n-9 1 -7 -6 0 2 1 1 5 1\n", "1\n10\n-3 -3 -3 0 0 -3 -2 -2 7 7\n", "1\n10\n-9 1 -1 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given n integers a_1, a_2, ..., a_n, such that for each 1≤ i ≤ n holds i-n≤ a_i≤ i-1. Find some nonempty subset of these integers, whose sum is equal to 0. It can be shown th...
1336_B. Xenia and Colorful Gems_20131
Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gems, n_g green gems and n_b blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, ...
from bisect import bisect_left def fun(arr, val): pos = bisect_left(arr, val) if(pos == 0): return arr[0] elif(pos == len(arr)): return arr[-1] else: left = arr[pos - 1] if((val - left) < (arr[pos] - val)): return left else: return arr[po...
{ "input": [ "5\n2 2 3\n7 8\n6 3\n3 1 4\n1 1 1\n1\n1\n1000000000\n2 2 2\n1 2\n5 4\n6 7\n2 2 2\n1 2\n3 4\n6 7\n3 4 1\n3 2 1\n7 3 3 4\n6\n", "1\n10 10 10\n761 599 561 998 446 576 705 131 556 821\n896 115 882 957 169 475 289 661 760 434\n131 383 139 446 908 799 56 991 360 189\n", "1\n1 1 1\n1000000000\n1\n10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. <image> Recently Xenia has bought n_r red gem...
1358_E. Are You Fired?_20135
Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months — in the i-th month the company had income equal to a_i (positive income means profit, negative income means loss, zero income means no change). Because of the general self-isolation, the f...
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) x = int(input()) chg = [0] for i in range(n//2): chg.append(x-a[i]) for i in range(1, n//2+1): chg[i] += chg[i-1] for i in range(1, n//2+1): chg[i] = min(chg[i], chg[i-1]) pref = sum(a) for k in range((n+1)//2, ...
{ "input": [ "3\n2 -1\n2\n", "6\n-2 -2 6\n-1\n", "5\n2 2 -8\n2\n", "2\n0\n0\n", "3\n-1 5\n-4\n", "7\n0 0 0 10\n1\n", "10\n-1 -1 -1 -1 6\n-1\n", "5\n17 -1345 246\n541\n", "2\n10\n-10\n", "6\n200 -99 -299\n67\n", "2\n57\n179\n", "6\n0 0 -15\n5\n", "2\n2\n2\n", "2\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Levian works as an accountant in a large company. Levian knows how much the company has earned in each of the n consecutive months — in the i-th month the company had income equal to ...
1379_A. Acacius and String_20139
Acacius is studying strings theory. Today he came with the following problem. You are given a string s of length n consisting of lowercase English letters and question marks. It is possible to replace question marks with lowercase English letters in such a way that a string "abacaba" occurs as a substring in a resulti...
import sys import os from io import BytesIO, IOBase # credits of fastio to PyRival project in Github(https://github.com/cheran-senthil/PyRival) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() ...
{ "input": [ "6\n7\nabacaba\n7\n???????\n11\naba?abacaba\n11\nabacaba?aba\n15\nasdf???f???qwer\n11\nabacabacaba\n", "1\n13\nabacab?bacaba\n", "1\n13\nabacab?bacab`\n", "6\n7\nabacaba\n7\n???????\n11\naba?abacaba\n11\nabacaba>aba\n15\nasdf???f???qwer\n11\nabacabacaba\n", "1\n13\nabadab?bacab`\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Acacius is studying strings theory. Today he came with the following problem. You are given a string s of length n consisting of lowercase English letters and question marks. It is p...
1399_E2. Weights Division (hard version)_20142
Easy and hard versions are actually different problems, so we advise you to read both statements carefully. You are given a weighted rooted tree, vertex 1 is the root of this tree. Also, each edge has its own cost. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A paren...
import sys,bisect from collections import deque input=sys.stdin.buffer.readline t=1 t=int(input()) for _ in range(t): n,S=map(int,input().split()) edge=[[] for i in range(n)] for i in range(n-1): u,v,w,c=map(int,input().split()) edge[u-1].append((v-1,w,float(c))) edge[v-1].append((...
{ "input": [ "4\n4 18\n2 1 9 2\n3 2 4 1\n4 1 1 2\n3 20\n2 1 8 1\n3 1 7 2\n5 50\n1 3 100 1\n1 5 10 2\n2 3 123 2\n5 4 55 1\n2 100\n1 2 409 2\n", "4\n4 18\n2 1 9 2\n3 2 4 1\n4 1 2 2\n3 20\n2 1 8 1\n3 1 7 2\n5 50\n1 3 100 1\n1 5 10 2\n2 3 123 2\n5 4 55 1\n2 100\n1 2 409 2\n", "4\n4 18\n2 1 9 2\n3 2 4 1\n4 1 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Easy and hard versions are actually different problems, so we advise you to read both statements carefully. You are given a weighted rooted tree, vertex 1 is the root of this tree. A...
1442_B. Identify the Operations_20147
We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≤ t_i ≤ n-i+1), remove a_{t_i} from the array, and append one of the numbers a_{t_i-1} or a_{t_i+1} (if t_i-1 or t_i+1 are within the array bounds) to the r...
mxn=998244353 for _ in range(int(input())): n,k=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) pre=[i-1 for i in range(n)] next=[i+1 for i in range(n)] vis=[0]*(n+1) dct=[0]*(n+1) for i in range(n): dct[a[i]]= i for num in b: ...
{ "input": [ "3\n5 3\n1 2 3 4 5\n3 2 5\n4 3\n4 3 2 1\n4 3 1\n7 4\n1 4 7 3 6 2 5\n3 2 4 5\n", "6\n2 1\n2 1\n1\n3 2\n1 3 2\n1 3\n4 2\n4 1 3 2\n1 2\n5 3\n4 3 2 5 1\n1 4 5\n6 3\n2 4 5 6 1 3\n2 4 5\n7 3\n7 6 4 3 2 1 5\n1 2 6\n", "6\n2 1\n2 1\n1\n3 2\n1 3 2\n1 3\n4 2\n4 1 3 2\n1 2\n5 3\n4 3 2 5 1\n1 4 5\n6 3\n2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We start with a permutation a_1, a_2, …, a_n and with an empty array b. We apply the following operation k times. On the i-th iteration, we select an index t_i (1 ≤ t_i ≤ n-i+1), rem...
1468_D. Firecrackers_20151
Consider a long corridor which can be divided into n square cells of size 1 × 1. These cells are numbered from 1 to n from left to right. There are two people in this corridor, a hooligan and a security guard. Initially, the hooligan is in the a-th cell, the guard is in the b-th cell (a ≠ b). <image> One of the poss...
from sys import stdin,stdout from math import gcd,sqrt,factorial,pi,inf from collections import deque,defaultdict from bisect import bisect,bisect_left from time import time from itertools import permutations as per input=stdin.readline R=lambda:map(int,input().split()) I=lambda:int(input()) S=lambda:input().rstrip('\r...
{ "input": [ "3\n7 2 3 6\n1 4\n7 2 3 6\n5 1\n7 2 3 6\n4 4\n", "3\n7 2 3 6\n1 4\n7 2 3 6\n5 1\n7 2 3 6\n2 4\n", "3\n7 2 3 6\n1 4\n7 2 3 6\n5 1\n12 2 3 6\n4 4\n", "3\n7 2 3 6\n1 4\n7 2 3 4\n5 1\n7 2 3 6\n2 4\n", "3\n7 2 3 6\n1 4\n7 2 3 9\n5 1\n7 2 3 6\n2 4\n", "3\n13 2 3 6\n1 8\n7 2 4 6\n5 2\n7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider a long corridor which can be divided into n square cells of size 1 × 1. These cells are numbered from 1 to n from left to right. There are two people in this corridor, a hoo...
1492_C. Maximum width_20155
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m. A sequence p_1, p_2, …, p_m, where 1 ≤ p_1 < p_2 < … < p_m ≤ n, is called beautiful, if s_{p_i} = t_i for all i from 1 to m. The width of a sequence is defined as max_{1 ...
n,m=map(int,input().split()) s=input() t=input() l=[0]*m r=[0]*m i,j=0,0 while j<m: if s[i]==t[j]: l[j]=i j+=1 i+=1 i=n-1 j=m-1 while j>0: if s[i]==t[j]: r[j]=i j-=1 i-=1 print(max(r[i+1]-l[i] for i in range(m-1)))
{ "input": [ "5 2\naaaaa\naa\n", "2 2\nab\nab\n", "5 3\nabbbc\nabc\n", "5 5\nabcdf\nabcdf\n", "5 2\nddaca\nda\n", "5 3\nbbbbc\nbbc\n", "99 54\nfdabegacbffbgcacafcegfaegcacaegdbcabfgaecgbgfgefddggcdgebaaaccbcafdgcceabddfcacgdccfcgebccfffdaccac\nfdabegabffbgcaafcegfgcacaegdbcabfaecgbgfcgbccf...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: s of length n and t of length m. A sequence p_1, p_2, …, p_m, wher...
1515_D. Phoenix and Socks_20159
To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color c_i and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: * recolor a sock to any color c' (1 ≤ c' ≤ n) * turn a left sock into a right ...
from collections import defaultdict t = int(input()) while t!=0: t=t-1 n,l,r = map(int,input().split()) list1 = list(map(int,input().split())) d1 = defaultdict(int) d2 = defaultdict(int) for i in range(l): d1[list1[i]]+=1 for i in range(l,n): d2[list1[i]]+=1 for ...
{ "input": [ "4\n6 3 3\n1 2 3 2 2 2\n6 2 4\n1 1 2 2 2 2\n6 5 1\n6 5 4 3 2 1\n4 0 4\n4 4 4 3\n", "4\n6 3 3\n1 2 6 2 2 2\n6 2 4\n1 1 2 2 2 2\n6 5 1\n6 5 4 3 2 1\n4 0 4\n4 4 4 3\n", "4\n6 3 3\n1 2 6 2 2 2\n6 2 4\n1 1 2 2 2 2\n6 5 1\n6 5 4 1 2 1\n4 0 4\n4 4 4 3\n", "4\n6 3 3\n1 2 6 2 2 2\n6 2 4\n1 1 2 3 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: To satisfy his love of matching socks, Phoenix has brought his n socks (n is even) to the sock store. Each of his socks has a color c_i and is either a left sock or right sock. Phoe...
1542_C. Strange Function_20163
Let f(i) denote the minimum positive integer x such that x is not a divisor of i. Compute ∑_{i=1}^n f(i) modulo 10^9+7. In other words, compute f(1)+f(2)+...+f(n) modulo 10^9+7. Input The first line contains a single integer t (1≤ t≤ 10^4), the number of test cases. Then t cases follow. The only line of each test c...
def gcd(a,b): """Compute the greatest common divisor of a and b""" while b > 0: a, b = b, a % b return a def lcm(a, b): """Compute the lowest common multiple of a and b""" return a * b // gcd(a, b) t=int(input()) for _ in range(t): n=int(input()) mod=10**9+7 l=1 q=1 ...
{ "input": [ "6\n1\n2\n3\n4\n10\n10000000000000000\n", "1\n4642392862013167\n", "1\n5897090464090076\n", "6\n1\n4\n3\n4\n10\n10000000000000000\n", "1\n3141480811161321\n", "1\n3202097780689747\n", "1\n5061211645916487\n", "1\n852857060842737\n", "1\n1123842695541782\n", "1\n146...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let f(i) denote the minimum positive integer x such that x is not a divisor of i. Compute ∑_{i=1}^n f(i) modulo 10^9+7. In other words, compute f(1)+f(2)+...+f(n) modulo 10^9+7. Inp...
16_C. Monitor_20167
Reca company makes monitors, the most popular of their models is AB999 with the screen size a × b centimeters. Because of some production peculiarities a screen parameters are integer numbers. Recently the screen sides ratio x: y became popular with users. That's why the company wants to reduce monitor AB999 size so th...
#------------------------template--------------------------# import os import sys from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w')...
{ "input": [ "1 1 1 2\n", "800 600 4 3\n", "1920 1200 16 9\n", "2000000000 2000000000 1999999998 1999999999\n", "721746595 799202881 143676564 380427290\n", "266102152 594841161 15854566 13392106\n", "548893795 861438648 131329677 177735812\n", "1243276346 1975662240 38441120 291740200...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Reca company makes monitors, the most popular of their models is AB999 with the screen size a × b centimeters. Because of some production peculiarities a screen parameters are integer...
190_A. Vasya and the Bus_20171
One day Vasya heard a story: "In the city of High Bertown a bus number 62 left from the bus station. It had n grown-ups and m kids..." The latter events happen to be of no importance to us. Vasya is an accountant and he loves counting money. So he wondered what maximum and minimum sum of money these passengers could h...
class CodeforcesTask190ASolution: def __init__(self): self.result = '' self.passengers = [] def read_input(self): self.passengers = [int(x) for x in input().split(" ")] def process_task(self): if self.passengers[0]: if self.passengers[0] >= self.passengers[1]: ...
{ "input": [ "0 5\n", "2 2\n", "1 2\n", "8 26\n", "80236 80236\n", "59617 0\n", "0 1\n", "2 7\n", "99227 53323\n", "9458 0\n", "63028 28217\n", "26666 52747\n", "76826 4210\n", "59 96\n", "16 70\n", "7 1\n", "63028 0\n", "100000 1\n", "45657 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day Vasya heard a story: "In the city of High Bertown a bus number 62 left from the bus station. It had n grown-ups and m kids..." The latter events happen to be of no importance...
214_A. System of Equations_20175
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a system of equations: <image> You should count, how many there are pai...
n,m = map(int,input().split()) a = 0 for i in range(0, 1000): for j in range(0, 1000): if i * i + j == n and j * j + i == m: a = a + 1 print(a)
{ "input": [ "14 28\n", "9 3\n", "4 20\n", "247 499\n", "18 198\n", "1 1\n", "855 225\n", "840 780\n", "139 433\n", "420 380\n", "535 59\n", "22 326\n", "20 11\n", "790 64\n", "1 11\n", "1 571\n", "991 931\n", "57 447\n", "2 6\n", "810 70...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new ta...
238_A. Not Wool Sequences_20179
A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 ≤ l ≤ r ≤ n) such that <image>. In other words each wool sequence contains a subsequence of consecutive elements with xor equal to 0. The expression <image> means applying the o...
n, m = map(int, input().split()) MOD = 10 ** 9 + 9 ans = pow(2, m, MOD) - 1 step = pow(2, m, MOD) - 2 for i in range(n - 1): ans = (ans * step) % MOD step -= 1 while ans < 0: ans += MOD while ans >= MOD: ans -= MOD print(ans)
{ "input": [ "3 2\n", "2 1\n", "100000 17\n", "50664 46559\n", "4 2\n", "4 11\n", "60607 63595\n", "25063 54317\n", "7683 65667\n", "5 100\n", "1 2\n", "5444 31525\n", "1 1\n", "85264 15318\n", "2 15\n", "65535 16\n", "86232 41348\n", "12345 31\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A sequence of non-negative integers a1, a2, ..., an of length n is called a wool sequence if and only if there exists two integers l and r (1 ≤ l ≤ r ≤ n) such that <image>. In other ...
262_D. Maxim and Restaurant_20183
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (...
n=int(input()) arr=list(map(int,input().split())) p=int(input()) dp=[[[0 for k in range(n+1)] for i in range(p+1)] for i in range(n+1)] for j in range(p+1): for k in range(n+1): dp[0][j][k]=1 for i in range(1,n+1): for j in range(p+1): for k in range(1,n+1): if j>=arr[k-1]: ...
{ "input": [ "3\n1 2 3\n3\n", "20\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n20\n", "5\n1 2 3 1 2\n3\n", "2\n1 2\n3\n", "1\n2\n1\n", "23\n2 1 2 1 1 1 2 2 2 1 1 2 2 1 1 1 2 1 2 2 1 1 1\n37\n", "5\n1 2 3 4 5\n20\n", "4\n1 2 3 4\n11\n", "2\n1 2\n2\n", "5\n2 3 2 3 6\n30\n", "3\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index...
334_B. Eight Point Sets_20191
Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three int...
a = [] for i in range(8): x, y = map(int, input().split()) a.append((x, y)) a.sort() if a[0][1] != a[1][1] and a[1][1] != a[2][1] and a[2][1] != a[0][1]: if a[0][0] != a[3][0] and a[3][0] != a[5][0] and a[5][0] != a[0][0]: if a[0][0] == a[1][0] == a[2][0]: if a[3][0] == a[4][0]: ...
{ "input": [ "1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n", "0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0\n", "0 0\n0 1\n0 2\n1 0\n1 2\n2 0\n2 1\n2 2\n", "0 0\n0 0\n0 0\n1 1\n1 1\n2 2\n2 2\n2 2\n", "0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n", "1 2\n1 3\n1 4\n2 2\n2 4\n4 2\n4 2\n4 4\n", "0 0\n1 0\n0 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines a...
357_B. Flag Day_20195
In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions: * overall, there must be m dances; * exactly three people must take part in each dance; * eac...
#! /usr/bin/env python n, m = [int(x) for x in input().split()] dancers = {} for i in range(m): dance = [int (x) for x in input().split()] for j in range(3): if dance[j] not in dancers: dancers[dance[j]] = j else: dancers[dance[(j + 1) % 3]] = (dancers[dance[j]] + 1) % 3...
{ "input": [ "7 3\n1 2 3\n1 4 5\n4 6 7\n", "9 3\n3 6 9\n2 5 8\n1 4 7\n", "5 2\n4 1 5\n3 1 2\n", "13 6\n8 7 6\n11 7 3\n13 9 3\n12 1 13\n8 10 4\n2 7 5\n", "98 38\n70 23 73\n73 29 86\n93 82 30\n6 29 10\n7 22 78\n55 61 87\n98 2 12\n11 5 54\n44 56 60\n89 76 50\n37 72 43\n47 41 61\n85 40 38\n48 93 20\n9...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to org...
380_A. Sereja and Prefixes_20199
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current seq...
from bisect import bisect_left m = int(input()) t, s = [input().split() for i in range(m)], [0] * m l, n = 0, int(input()) for j, i in enumerate(t): l += 1 if i[0] == '1' else int(i[1]) * int(i[2]) t[j], s[j] = l, i[1] if i[0] == '1' else int(i[1]) F = {} def f(i): if not i in F: k = bisect_left(t, ...
{ "input": [ "6\n1 1\n1 2\n2 2 1\n1 3\n2 5 2\n1 4\n16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n", "1\n1 1\n1\n1\n", "7\n1 53989\n1 47249\n1 71935\n2 1 3\n1 84520\n1 84185\n2 6 1\n14\n1 2 3 4 5 6 7 8 9 10 11 12 13 14\n", "2\n1 33085\n1 44638\n2\n1 2\n", "3\n1 97601\n1 32580\n1 70519\n3\n1 2 3\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing ou...
429_A. Xor-tree_20205
Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids which uses xor-trees. The game is played on a tree having n nodes, numbered from 1 to n. Each node i has an initial value initi, which ...
n = int(input()) p = [[] for i in range(n + 1)] for i in range(n - 1): a, b = map(int, input().split()) p[a].append(b) p[b].append(a) u, v = ' ' + input()[:: 2], ' ' + input()[:: 2] s, q = [(1, 0, 0, 0)], [] while s: a, k, i, j = s.pop() if k: if i != (u[a] != v[a]): q.append(a) ...
{ "input": [ "10\n2 1\n3 1\n4 2\n5 1\n6 2\n7 5\n8 6\n9 8\n10 5\n1 0 1 1 0 1 0 1 0 1\n1 0 1 0 0 1 1 1 0 1\n", "23\n2 1\n3 2\n4 1\n5 1\n6 5\n7 3\n8 2\n9 8\n10 5\n11 6\n12 9\n13 3\n14 11\n15 5\n16 2\n17 3\n18 10\n19 16\n20 14\n21 19\n22 17\n23 7\n0 1 0 1 1 1 0 1 1 0 0 0 1 0 0 1 1 0 1 0 0 0 0\n0 0 0 0 1 1 1 1 1 0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Iahub is very proud of his recent discovery, propagating trees. Right now, he invented a new tree, called xor-tree. After this new revolutionary discovery, he invented a game for kids...
450_D. Jzzhu and Cities_20208
Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One ...
import sys input = sys.stdin.readline import heapq n, m, k = map(int, input().split()) adj = [[] for _ in range(n+5)] for _ in range(m): u, v, w = map(int, input().split()) adj[u].append((v,w)) adj[v].append((u,w)) train = [-1 for _ in range(n+5)] ans = 0 dist = [int(1e15) for _ in range(n+5)] pq = []...
{ "input": [ "5 5 3\n1 2 1\n2 3 2\n1 3 3\n3 4 4\n1 5 5\n3 5\n4 5\n5 5\n", "2 2 3\n1 2 2\n2 1 3\n2 1\n2 2\n2 3\n", "5 5 2\n1 2 100\n2 3 100\n3 4 100\n4 5 20\n2 5 5\n5 50\n4 1\n", "2 1 5\n1 2 4\n2 3\n2 5\n2 4\n2 4\n2 5\n", "3 3 6\n1 2 499999999\n2 3 500000000\n1 3 999999999\n2 499999999\n2 500000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from ...
472_E. Design Tutorial: Learn from a Game_20212
One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task. Let's have a try. Puzzle and Dragon was a popular game in Japan, we focus on the puzzle part of that game, it is a tile-matching puzzle. <image>(Picture from Wikipedia pag...
""" Codeforces Contest 270 Problem E Author : chaotic_iak Language: Python 3.3.4 """ class Board(object): def __init__(self, init): self.grid = init moves = [] def current(self): return self.moves[-1] def print_moves(self): print(len(self.moves)-1) for i...
{ "input": [ "2 2\n1 3\n2 3\n1 2\n2 3\n", "4 1\n1\n2\n3\n4\n3\n1\n2\n4\n", "2 2\n1 3\n2 3\n1 3\n3 2\n", "1 4\n1 2 3 4\n4 3 2 1\n", "2 2\n1 2\n3 4\n4 1\n2 3\n", "20 1\n265\n180\n331\n280\n253\n49\n24\n365\n374\n251\n295\n397\n131\n44\n403\n206\n448\n457\n121\n355\n265\n180\n331\n365\n280\n253\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task. Let's have a try. Puzzle and Dragon...
497_A. Removing Columns_20216
You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table abcd edfg hijk ...
from math import * from collections import * from random import * from bisect import * import sys input=sys.stdin.readline def lis(): return list(map(int,input().split())) def ma(): return map(int,input().split()) def inp(): return int(input()) def fk(a,s,k): b=[] for i in range(len(a)): b.a...
{ "input": [ "1 10\ncodeforces\n", "4 4\ncase\ncare\ntest\ncode\n", "5 4\ncode\nforc\nesco\ndefo\nrces\n", "3 3\naaz\ndaa\nbaa\n", "3 2\naa\nbc\nba\n", "2 1\nb\na\n", "3 3\nabc\nbbb\ncba\n", "2 3\naxc\nbxa\n", "4 50\nulkteempxafxafcvfwmwhsixwzgbmubcqqceevbbwijeerqbsj\neyqxsievaratn...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an n × m rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combin...
520_C. DNA Alignment_20220
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in whic...
import sys # sys.stdin = open('in.txt') R = lambda: map(int, input().split()) n = int(input()) s = input() ca = s.count('A') cg = s.count('G') cc = s.count('C') ct = s.count('T') mx = max(ca, cg, cc, ct) sum = (ca==mx)+(cg==mx)+(cc==mx)+(ct==mx) ans = 1 for i in range(n): ans = (ans*sum)%1000000007 print(ans)
{ "input": [ "2\nAG\n", "1\nC\n", "3\nTTT\n", "20\nTAAGCGACCAGGTGCTTTAC\n", "3\nGCA\n", "318\nTATCAATCGGTACGTGCGCATCATTGTCAATCGGGCTTCATGGCTTGCGGGCGCTACCCGAGGGGAAGCTGCGGACAGGTAGGTAAGATACACACGAACCAAACGGAGTTATGTTGGATAAATTGGCTGGAAGGGCGTAGGTATATCGAGTCGCGGACCTGGCATAGACTATCAGGGGCAGCGGTACAAGGCAACCGTGA...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequ...
574_B. Bear and Three Musketeers_20226
Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys. There are n warriors. Richelimakieu wants to choose three ...
n, m = map(int, input().split()) amatr = [[0] * n for i in range(n)] alist = [[] for i in range(n)] for i in range(m): a, b = map(int, input().split()) amatr[a - 1][b - 1] = 1 amatr[b - 1][a - 1] = 1 alist[a - 1].append(b - 1) alist[b - 1].append(a - 1) known = [len(alist[i]) for i in range(n)] an...
{ "input": [ "5 6\n1 2\n1 3\n2 3\n2 4\n3 4\n4 5\n", "7 4\n2 1\n3 6\n5 1\n1 7\n", "4000 0\n", "4 6\n3 4\n1 3\n4 1\n3 2\n2 1\n4 2\n", "3 0\n", "7 14\n3 6\n2 3\n5 2\n5 6\n7 5\n7 4\n6 2\n3 5\n7 1\n4 1\n6 1\n7 6\n6 4\n5 4\n", "987 50\n221 959\n221 553\n959 695\n553 959\n819 437\n371 295\n695 55...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by ...
595_E. Edo and Magnets_20230
Edo has got a collection of n refrigerator magnets! He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be posit...
from sys import* # def check(u, d, l, r): used = [pointsx[i][1] for i in range(l)] used += [pointsx[-1 - i][1] for i in range(r)] used += [pointsy[i][1] for i in range(u)] used += [pointsy[-1 - i][1] for i in range(d)] if len(set(used)) > k: return DOHERA dx = pointsx[-1 - r][0] - points...
{ "input": [ "4 1\n1 1 2 2\n1 9 2 10\n9 9 10 10\n9 1 10 2\n", "3 1\n1 1 2 2\n2 2 3 3\n3 3 4 4\n", "3 0\n1 1 2 2\n1 1 1000000000 1000000000\n1 3 8 12\n", "1 0\n1 1 100 100\n", "11 8\n9 1 11 5\n2 2 8 12\n3 8 23 10\n2 1 10 5\n7 1 19 5\n1 8 3 10\n1 5 3 9\n1 2 3 4\n1 2 3 4\n4 2 12 16\n8 5 12 9\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Edo has got a collection of n refrigerator magnets! He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door th...
617_B. Chocolate_20234
Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces. You are asked to calculate the number of ways he can...
n = int(input()) a = list(map(int, input().split())) k = sum(a) if k == 0: print(0) exit() oneMet = False d = 1 p = 0 for i in range(n): if a[i] == 1: p += 1 if oneMet: d *= p oneMet = True p = 0 k -= 1 if k == 0: break if a[i] == 0: ...
{ "input": [ "3\n0 1 0\n", "5\n1 0 1 0 1\n", "100\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1\n", "100\n0 1 0 1 1 0 1 0 1 0 0 0 0 0 0 1 0 1 0 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part...
637_C. Promocodes with Mistakes_20238
During a New Year special offer the "Sudislavl Bars" offered n promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar "Mosquito Shelter". Of course, all the promocodes differ. As the "Mosquito Shelter" opens only at 9, and partying in Sudislavl usually begins at as ...
n = int(input()) codes = [] max_matches = 0 for i in range(n): codes.append(input()) for i in range(n - 1): for j in range(i + 1, n): matches = 0 for num in range(6): if codes[i][num] == codes[j][num]: matches += 1 max_matches = max(max_matches, matches) ...
{ "input": [ "2\n000000\n999999\n", "6\n211111\n212111\n222111\n111111\n112111\n121111\n", "10\n153474\n155468\n151419\n151479\n158478\n159465\n150498\n157416\n150429\n159446\n", "10\n878711\n193771\n965021\n617901\n333641\n307811\n989461\n461561\n956811\n253741\n", "10\n946965\n781372\n029568\n33...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: During a New Year special offer the "Sudislavl Bars" offered n promo codes. Each promo code consists of exactly six digits and gives right to one free cocktail at the bar "Mosquito Sh...
665_C. Simple Strings_20242
zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple. zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this tas...
s=list(input()) n=len(s) def call(a,b): str="anfrozeyuibiu" for i in str: if i!=a and i!=b: return i def call1(a): str="urweifbn" for i in str: if i!=a: return i for i in range(1,n): if s[i]==s[i-1]: if i==n-1: s[i]=call1(s[i]) els...
{ "input": [ "zscoder\n", "aab\n", "caaab\n", "zza\n", "aa\n", "dtottttotd\n", "ab\n", "nnop\n", "qasdasd\n", "gg\n", "rxxxrrxrxxxxxrrrrrxxxxrrrrxrxxrxxrxrxrrrxrrxrrxrxxxrxrrxrrxrxrxxxxxrxxxxrrrxrxxrxxrxxxrrrrrxrrxrrxrr\n", "h\n", "xx\n", "zz\n", "u\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple. z...
68_A. Irrational problem_20246
Little Petya was given this problem for homework: You are given function <image> (here <image> represents the operation of taking the remainder). His task is to count the number of integers x in range [a;b] with property f(x) = x. It is a pity that Petya forgot the order in which the remainders should be taken and wr...
import re import sys from bisect import bisect, bisect_left, insort, insort_left from collections import Counter, defaultdict, deque from copy import deepcopy from decimal import Decimal from itertools import ( accumulate, combinations, combinations_with_replacement, groupby, permutations, product) from math im...
{ "input": [ "2 7 1 8 2 8\n", "20 30 40 50 0 100\n", "31 41 59 26 17 43\n", "292 370 404 698 699 876\n", "17 18 19 20 17 20\n", "1 2 999 1000 30 40\n", "442 705 757 527 1869 19912\n", "41 449 328 474 150 709\n", "1 2 3 4 0 0\n", "823 431 359 590 153 3902\n", "225 862 522 61...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Petya was given this problem for homework: You are given function <image> (here <image> represents the operation of taking the remainder). His task is to count the number of i...
711_D. Directed Roads_20250
ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n. There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai ≠ i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it ...
class Solution(): def __init__(self): self.n = int(input()) self.G = [ int(x) for x in input().strip().split(' ') ] self.G.insert(0,0) self.used = [0 for i in range(self.n+1)] self.dis = [0 for i in range(self.n+1)] self.circle = [] self.mod = 10**9+7 d...
{ "input": [ "3\n2 3 1\n", "4\n2 1 1 1\n", "5\n2 4 2 5 3\n", "84\n2 50 67 79 71 45 43 40 57 20 25 8 60 47 52 10 37 23 1 28 22 26 3 42 11 63 61 68 49 32 55 18 5 24 31 70 66 27 38 41 54 12 65 51 15 34 30 35 77 74 21 62 33 16 81 14 19 48 80 73 69 78 39 6 76 46 75 72 84 29 58 59 13 17 82 9 83 4 36 56 53 7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n. There are n directed roads in the Udaylan...
732_D. Exams_20254
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On ...
import math global n,m,d,a d=[] a=[] def intdec(x): return int(x)-1 def check(x): global n,m,d,a vis=[0]*m seq=[] h=0; cnt=0 for i in range(x,-1,-1): if d[i]<0 or vis[d[i]]: if len(seq)<=h: pass else: cnt+=1 if cnt...
{ "input": [ "7 2\n0 1 0 2 1 0 2\n2 1\n", "10 3\n0 0 1 2 3 0 2 0 1 2\n1 1 4\n", "5 1\n1 1 1 1 1\n5\n", "3 2\n0 0 0\n2 1\n", "4 2\n0 1 0 2\n1 1\n", "6 2\n1 0 0 0 0 2\n1 1\n", "5 1\n0 0 0 0 1\n1\n", "6 2\n1 1 1 1 1 2\n1 1\n", "100 10\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subj...
778_A. String Game_20260
Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the word t and wants to get the word p out of it. Nastya removes letters in a certain ...
import math as mt import sys,string input=sys.stdin.readline import random from collections import deque,defaultdict L=lambda : list(map(int,input().split())) Ls=lambda : list(input().split()) M=lambda : map(int,input().split()) I=lambda :int(input()) def isSubseq(s,b,v): i=0 j=0 while(i<len(s) and j<len(b...
{ "input": [ "bbbabb\nbb\n1 6 3 4 2 5\n", "ababcba\nabb\n5 3 4 1 7 6 2\n", "aaaaaaaadbaaabbbbbddaaabdadbbbbbdbbabbbabaabdbbdababbbddddbdaabbddbbbbabbbbbabadaadabaaaadbbabbbaddb\naaaaaaaaaaaaaa\n61 52 5 43 53 81 7 96 6 9 34 78 79 12 8 63 22 76 18 46 41 56 3 20 57 21 75 73 100 94 35 69 32 4 70 95 88 44 68 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her bro...
847_K. Travel Cards_20268
In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop t...
import heapq import sys num_trips, a, b, k, f = sys.stdin.readline().strip().split(" ") a, b, k, f = int(a), int(b), int(k), int(f) #print(a, b, k, f) trips = [] for line in sys.stdin: trips.append(line.strip().split(" ")) """ a = 5 b = 3 k = 1 f = 8 trips = [["BerBank", "University"], ["University", "BerMall"],...
{ "input": [ "4 2 1 300 1000\na A\nA aa\naa AA\nAA a\n", "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank\n", "5 3 2 1 1\nC CB\nCB C\nC d\nCB d\nCB C\n", "2 2 1 2 1\nBDDB C\nC BDDB\n", "8 2 1 11 1\ndacdD cdDAA\ncdDAA dacdD\ndacdD bcCA\nbcCA B\nDDAA B\nDDAA daC\nAbCAc B\ndacdD...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the evening Polycarp decided to analyze his today's travel expenses on public transport. The bus system in the capital of Berland is arranged in such a way that each bus runs alon...
86_A. Reflection_20272
For each positive integer n consider the integer ψ(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9 - a). We say that ψ(n) is the reflection of n. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals...
def ref(n): return int(''.join([str(9-int(x)) for x in str(n)])) l,r=map(int,input().split()) ans=0 ans=max(ans,ref(l)*l) ans=max(ans,ref(r)*r) cur=5 for i in range(20): if(l<=cur<=r): ans=max(ans,ref(cur)*cur) cur*=10 print(ans)
{ "input": [ "8 10\n", "3 7\n", "1 1\n", "850 629417171\n", "3722 867350896\n", "91135741 100003483\n", "555555 555555\n", "4 6\n", "19971607 162619978\n", "1 2\n", "71990 79486\n", "1 1000000000\n", "942 572335596\n", "733403773 763985558\n", "9999 10000000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For each positive integer n consider the integer ψ(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9 - a). We say that ψ(n) is the ...
917_A. The Monster_20278
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him. <image> Thus, he came up with a puz...
import sys import math input = sys.stdin.readline from functools import cmp_to_key; def pi(): return(int(input())) def pl(): return(int(input(), 16)) def ti(): return(list(map(int,input().split()))) def ts(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) ...
{ "input": [ "((?))\n", "??()??\n", "(?\n", "?(\n", "))((()(()((((()))())()())((())())(((()()(())))))((())()()(()()(())()))()()(()()()(((()(()(()(()))))(\n", "()()((?(()(((()()(())(((()((())))(()))(()(((((())))()))(((()()()))))))(((((()))))))))\n", ")?(??((???????()?(?????????)??(????????(...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his...
93_A. Frames_20282
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well. This time Igor K. got disappointed in ...
n, m, a, b = map(int, input().split()) a -= 1 x, y = a % m, b % m d = b // m - a // m if b == n and y: d += 1 u, v = x == 0, y == 0 or b == n print(1 if u and v or d <= v else 2 if x == y or u or v or d == 1 else 3)
{ "input": [ "20 5 2 20\n", "11 4 3 9\n", "32 90 31 32\n", "19 5 7 19\n", "29 5 12 27\n", "831447817 8377 549549158 577671489\n", "691 27 313 499\n", "8 3 1 1\n", "21 5 6 18\n", "410 36 109 191\n", "8 3 1 2\n", "7 3 1 5\n", "5 5 2 4\n", "8 3 5 6\n", "12 4 5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programm...
965_D. Single-use Stones_20286
A lot of frogs want to cross a river. A river is w units width, but frogs can only jump l units long, where l < w. Frogs can also jump on lengths shorter than l. but can't jump longer. Hopefully, there are some stones in the river to help them. The stones are located at integer distances from the banks. There are a_i ...
w, l = [int(x) for x in input().split()] a = [int(x) for x in input().split()] s = res = sum(a[:l]) for i in range(l, w - 1): s += a[i] - a[i - l] res = min(res, s) print(res)
{ "input": [ "10 3\n1 1 1 1 2 1 1 1 1\n", "10 5\n0 0 1 0 2 0 0 1 0\n", "10 4\n10 10 10 10 10 10 10 10 10\n", "2 1\n0\n", "10 4\n0 0 6 2 7 1 6 4 0\n", "2 1\n5\n", "100 34\n16 0 10 11 12 13 0 5 4 14 6 15 4 9 1 20 19 14 1 7 14 11 10 20 6 9 12 8 3 19 20 4 17 17 8 11 14 18 5 20 17 0 3 18 14 12 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A lot of frogs want to cross a river. A river is w units width, but frogs can only jump l units long, where l < w. Frogs can also jump on lengths shorter than l. but can't jump longer...
992_D. Nastya and a Game_20290
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that <image>, where p is the product of all integers on the given array, s is their sum, and k is a given constant fo...
from sys import stdin import math # stdin = open('in') n, k = map(int, stdin.readline().split()) a = [int(x) for x in stdin.readline().split()] nxt = [-1]*n pref = [] f, s = -1, 0 for i in range(n): s += a[i] pref.append(s) nxt[n-1-i] = f if a[n-1-i] != 1: f = n-1-i ans = 0 for i in range(n): pos, cur = i, 0 p...
{ "input": [ "4 2\n6 3 8 1\n", "1 1\n1\n", "90 45\n15 1 1 1 1 3 1 1 5 3 5 1 1 15 15 3 1 15 1 1 1 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 1 1 1 1 1 1 1 1 1 15 1 1 1 1 5 1 1 1 1 15 1 1 1 15 1 1 1 1 1 1 1 1 3 1 1 15 3 1 1 1 15 15 1 1 1 1 15\n", "9 209\n2 7 31 673 853 1669 5821 7621 16677\n", "5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subs...
p02625 AtCoder Beginner Contest 172 - NEQ_20304
Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following conditions: * A_i \neq B_i, for every i such that 1\leq i\leq N. * A_i \neq A_j and B_i \neq B_j, for every (i, j) such that 1\leq i < j\leq N...
''' 完全順列(derangement) モンモール数(Montmort number) ''' MOD = 10**9+7 N, M = map(int, input().split()) # 片方の順列の総数を求める ans = 1 for i in range(N): ans *= M-i ans %= MOD # M枚からN枚選ぶ完全順列を計算 d = [1, M-N] for i in range(2, N+1): # 1がk番目にある # 1番目にkがある t = (i-1)*d[-2] % MOD # 1番目にkがない t += (M-N+i-1)*d[-...
{ "input": [ "141421 356237", "2 2", "2 3", "1 2", "2 6", "0 2", "2 4", "1 4", "3 4", "141421 225813", "1 1", "1 6", "2 7", "82963 225813", "1 3", "1 8", "1 7", "61759 225813", "2 8", "85920 225813", "2 14", "3299 225813", "3 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Count the pairs of length-N sequences consisting of integers between 1 and M (inclusive), A_1, A_2, \cdots, A_{N} and B_1, B_2, \cdots, B_{N}, that satisfy all of the following condit...
p02756 AtCoder Beginner Contest 158 - String Formation_20308
Takahashi has a string S consisting of lowercase English letters. Starting with this string, he will produce a new one in the procedure given as follows. The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following: * If T_i = 1: reverse the string S...
s = input() q = int(input()) t = 0 f = -1 x = ["",""] for i in range(q): que = input().split() if que[0] == "1": t += 1 else: x[(int(que[1])-1+t)%2] += que[2] s = x[0][::-1] + s + x[1] if t%2 == 1: s = s[::-1] print(s)
{ "input": [ "y\n1\n2 1 x", "a\n4\n2 1 p\n1\n2 2 c\n1", "a\n6\n2 2 a\n2 1 b\n1\n2 2 c\n1\n1", "a\n6\n2 2 a\n2 1 b\n1\n2 2 d\n1\n1", "a\n6\n2 2 b\n2 1 b\n1\n2 2 d\n1\n1", "b\n4\n2 1 p\n1\n2 2 c\n1", "a\n6\n2 2 a\n2 1 b\n1\n2 1 c\n1\n1", "a\n6\n2 2 a\n2 1 b\n1\n2 1 b\n1\n1", "`\n4\n2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi has a string S consisting of lowercase English letters. Starting with this string, he will produce a new one in the procedure given as follows. The procedure consists of Q...
p02891 AtCoder Grand Contest 039 - Connection and Disconnection_20312
Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character. Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different. Cons...
s = input() k = int(input()) m = 0 j = 0 if len(set(list(s))) == 1: print((len(s) * k) // 2) exit() for i in range(len(s)): if len(s) <= j + 1: a = m * k if s[0] == s[-1] == s[len(s) // 3]: print(a + k - 1) else: print(a) break if s[j] == s[j + 1]:...
{ "input": [ "cooooooooonteeeeeeeeeest\n999993333", "issii\n2", "qq\n81", "cooooooooonteeeeeeeedest\n999993333", "issih\n2", "rq\n81", "hssih\n2", "cooooooooonteeeeeeeedest\n1894324309", "hssih\n3", "hssih\n6", "tsedeeeeeeeetnoooooonooc\n54816478", "tsedeeeeeeeetnoooooo...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given is a string S. Let T be the concatenation of K copies of S. We can repeatedly perform the following operation: choose a character in T and replace it with a different character....
p03026 M-SOLUTIONS Programming Contest - Maximum Sum of Minimum_20316
You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N. The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i. We will write a positive integer on each vertex in T and calculate our score as follows: * On each edge, write the smaller of the integers writ...
from collections import defaultdict N = int(input()) dic = defaultdict(list) for n in range(N - 1): a, b = map(int, input().split()) dic[a].append(b) dic[b].append(a) c_list = sorted(map(int, input().split())) M = sum(c_list[:-1]) d_list = [False] * N for i in range(1, N + 1): if len(dic[i]) == 1: ...
{ "input": [ "5\n1 2\n1 3\n1 4\n1 5\n3141 59 26 53 59", "5\n1 2\n2 3\n3 4\n4 5\n1 2 3 4 5", "5\n1 2\n2 3\n3 4\n4 5\n2 2 3 4 5", "5\n1 2\n2 3\n3 4\n4 5\n1 2 3 3 5", "5\n1 2\n2 3\n3 4\n4 5\n2 2 2 4 5", "5\n1 2\n2 3\n3 4\n4 5\n1 2 1 3 5", "5\n1 2\n2 3\n3 4\n4 5\n0 2 1 3 5", "5\n1 2\n2 5\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N. The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i. We will...
p03167 Educational DP Contest - Grid 1_20320
There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i \leq H, 1 \leq j \leq W), Square (i, j) is described by a character a_{i, j}. If a_{i, j} is `.`, Square (i, j) is an empty square; if a...
H,W=map(int,input().split()) l=[[0 if i=="." else 1 for i in input()] for _ in range(H)] dp=[[0 for _ in range(W)] for _ in range(H)] dp[0][0]=1 mod=1000000007 for i in range(H): for j in range(W): if l[i][j]:continue if i>0:dp[i][j]+=(dp[i-1][j]%mod) if j>0:dp[i][j]+=(dp[i][j-1]%mod) prin...
{ "input": [ "5 5\n..#..\n.....\n...#\n.....\n..#..", "5 2\n..\n.\n..\n.#\n..", "3 4\n...#\n.#..\n....", "20 20\n....................\n....................\n....................\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: There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and the j-th column from the left. For each i and j (1 \leq i...