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
581_C. Developing Skills_3762
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an integer ai from 0 to 100. The higher the number ai is, the higher is the i-th skill of the character. The total rating of the character is...
n,k=map(int,input().split()) l=list(map(int,input().split())) rem=[] ans=0 for i in l: rem.append(i%10) ans=ans+i//10 #print(ans) rem.sort() # 0 1 2 3.. 9 9 9.. i=n-1 while(i>=0 and k>0): if(rem[i]!=0): #starting from back 9 -> 8 .... -> 2 -> 1->0 sm=10-(rem[i]) if(k>=s...
{ "input": [ "2 2\n99 100\n", "2 4\n7 9\n", "3 8\n17 15 19\n", "100 9455943\n44 8 21 71 7 29 40 65 91 70 48 19 77 48 16 22 54 4 29 34 9 22 73 34 47 41 5 83 32 91 52 6 74 64 18 23 9 4 36 78 98 20 20 3 69 86 41 67 54 76 87 84 47 6 52 87 61 100 98 80 14 14 24 99 90 73 97 79 22 65 65 51 29 44 15 67 21 58 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has n different skills, each of which is characterized by an i...
603_B. Moodular Arithmetic_3766
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where...
__author__ = 'MoonBall' import sys # sys.stdin = open('data/D.in', 'r') T = 1 M = 1000000007 def pow(x, y, m): if y == 0: return 1 if (y & 1): return pow(x, y - 1, m) * x % m else: t = pow(x, y >> 1, m) return t * t % m def process(): P, K = list(map(int, input().split...
{ "input": [ "3 2\n", "5 4\n", "69833 569\n", "6907 2590\n", "463 408\n", "95531 94787\n", "13 4\n", "807689 9869\n", "4649 4648\n", "437071 24705\n", "7 2\n", "40867 37466\n", "375103 52131\n", "203317 12108\n", "613463 269592\n", "5 3\n", "5 0\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics o...
675_A. Infinite Sequence_3772
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, the...
a, b, c = map(int, input().split()) if b == a: print('YES') elif not c: print('NO') elif (a < b) and c < 0: print('NO') elif (a > b) and c > 0: print('NO') else: print('NO' if ((b-a) % c) else 'YES')
{ "input": [ "1 -4 5\n", "1 7 3\n", "0 60 50\n", "10 10 0\n", "1 6 1\n", "-2 -2 0\n", "0 -2 -2\n", "0 -1 -1\n", "-2 0 1\n", "1 -2 0\n", "0 2 -2\n", "3 2 1\n", "0 2 -1\n", "1000000000 0 -1000000000\n", "0 -5 -3\n", "-1 -1 -2\n", "10 9 -1\n", "2 -1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring...
699_B. One Bomb_3776
You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*"). You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column...
def bombs(array, rows, cols, walls, wallsInRows, wallsInCols): if walls == 0: print("YES") print(1, 1) return for i in range(0, rows): for j in range(0, cols): s = wallsInRows[i] + wallsInCols[j] if (array[i][j] == '*' and s - 1 == walls) or (array[i][j] =...
{ "input": [ "3 3\n..*\n.*.\n*..\n", "6 5\n..*..\n..*..\n*****\n..*..\n..*..\n..*..\n", "3 4\n.*..\n....\n.*..\n", "3 3\n*..\n**.\n...\n", "3 3\n*..\n*..\n***\n", "10 10\n........*.\n*.........\n........*.\n........*.\n..........\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: You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*"). You have on...
71_D. Solitaire_3780
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm cards as a rectangle n × m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which a...
ranks = '23456789TJQKA' suits = 'CDHS' n, m = [int(i) for i in input().split()] b = [input().split() for _ in range(n)] p = [r + s for r in ranks for s in suits] j1, j2 = False, False for r in b: for c in r: if c == 'J1': j1 = True elif c == 'J2': j2 = True else: ...
{ "input": [ "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H 5S TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H QC TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "4 6\n2S 3S 4S 7S 8S AS\n5H 6H 7H J1 TC AC\n8H 9H TH 7C 8C 9C\n2D 2C 3C 4C 5C 6C\n", "7 7\n6C 8C KH 8S 2H 7S JH\n3H QC ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them. Vasya lays out nm ...
741_A. Arpa's loud Owf and Mehrdad's evil plan_3784
As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the number crushi. <image> Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are a...
def gcd(a,b): if b==0: return a return gcd(b, a%b) def lcm(a,b): return a*b/gcd(a,b) def dfs(sad, num, posjeceno): """if sad==a[sad]-1 and posjeceno!=[0]*n: return (-10, [])""" posjeceno[sad]=1 if posjeceno[a[sad]-1]==1: return (num+1, posjeceno) return dfs(a[sad]-1, ...
{ "input": [ "4\n2 1 4 3\n", "4\n4 4 4 4\n", "4\n2 3 1 4\n", "100\n99 7 60 94 9 96 38 44 77 12 75 88 47 42 88 95 59 4 12 96 36 16 71 6 26 19 88 63 25 53 90 18 95 82 63 74 6 60 84 88 80 95 66 50 21 8 61 74 61 38 31 19 28 76 94 48 23 80 83 58 62 6 64 7 72 100 94 90 12 63 44 92 32 12 6 66 49 80 71 1 20 8...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to n. Everyone has exactly one crush, i-th person's crush is person with the num...
764_E. Timofey and remoduling_3787
Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey likes to look for arithmetical progressions everywhere. One of his birthday presents was a sequence of distinct integers a1, a2, ..., an. ...
from functools import reduce def gcd_extended(bigger, less): if less == 0: return(bigger, 1, 0) mod = bigger % less div = bigger // less gcd, c_less, c_mod = gcd_extended(less, mod) #gcd == c_less * less + c_mod * mod #mod == bigger - div * less #gcd = (c_less - c_mod * div) * less ...
{ "input": [ "17 5\n0 2 4 13 15\n", "5 3\n1 2 3\n", "17 5\n0 2 4 13 14\n", "17 2\n8 3\n", "239 100\n178 74 144 43 201 189 40 175 51 31 202 114 12 17 86 78 53 196 235 158 95 224 143 198 170 117 79 81 23 197 73 165 133 166 21 50 148 34 121 223 184 45 54 228 9 238 187 19 218 169 104 62 106 46 209 182...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime m. Also, Timofey li...
834_B. The Festive Evening_3796
<image> It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectionery. Yet some of the things discussed here are not supposed to be disclosed to the general public: the information can cause discord in th...
import sys n, k = map(int, input().split()) a = list(input()) st = [0] * 26 ed = [0] * 26 for i in range(n): if st[ord(a[i])-65] == 0: st[ord(a[i])-65] = i + 1 else: ed[ord(a[i])-65] = i + 1 for i in range(26): if st[i] != 0 and ed[i] == 0: ed[i] = st[i] n = 52 i = 0 j = 0 maxi = -1 * sys.maxsize l = 0 st.sort...
{ "input": [ "5 1\nAABBB\n", "5 1\nABABB\n", "433 3\nFZDDHMJGBZCHFUXBBPIEBBEFDWOMXXEPOMDGSMPIUZOMRZQNSJAVNATGIWPDFISKFQXJNVFXPHOZDAEZFDAHDXXQKZMGNSGKQNWGNGJGJZVVITKNFLVCPMZSDMCHBTVAWYVZLIXXIADXNYILEYNIQHKMOGMVOCWGHCWIYMPEPADSJAAKEGTUSEDWAHMNYJDIHBKHVUHLYGNGZDBULRXLSAJHPCMNWCEAAPYMHDTYWPADOTJTXTXUKLCHWKUSZ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: <image> It's the end of July – the time when a festive evening is held at Jelly Castle! Guests from all over the kingdom gather here to discuss new trends in the world of confectione...
879_C. Short Program_3802
Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well. In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an...
n = int(input()) a, b = 1023, 0 for _ in range(n): c, d = input().split() d = int(d) if c == '|': a, b = a | d, b | d elif c == '&': a, b = a & d, b & d elif c == '^': a, b = a ^ d, b ^ d print('2\n| {}\n^ {}'.format(a ^ b ^ 1023, a ^ 1023)) # Made By Mostafa_Khaled
{ "input": [ "3\n&amp; 1\n&amp; 3\n&amp; 5\n", "3\n^ 1\n^ 2\n^ 3\n", "3\n| 3\n^ 2\n| 1\n", "1\n& 0\n", "1\n^ 123\n", "10\n^ 218\n& 150\n| 935\n& 61\n| 588\n& 897\n| 411\n| 584\n^ 800\n| 704\n", "3\n& 1\n& 3\n& 5\n", "1\n| 123\n", "1\n^ 0\n", "1\n| 1023\n", "3\n& 242\n^ 506\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well. In the language, there...
925_A. Stairs and Elevators_3808
In the year of 30XX participants of some world programming championship live in a single large hotel. The hotel has n floors. Each floor has m sections with a single corridor connecting all of them. The sections are enumerated from 1 to m along the corridor, and all sections with equal numbers on different floors are l...
def takeClosest(myList, myNumber): """ Assumes myList is sorted. Returns closest value to myNumber. If two numbers are equally close, return the smallest number. """ if len(myList) == 0: return 9e10 pos = bisect_left(myList, myNumber) if pos == 0: return myList[0] if pos...
{ "input": [ "5 6 1 1 3\n2\n5\n3\n1 1 5 6\n1 3 5 4\n3 3 5 3\n", "2 10 1 1 1\n1\n10\n1\n1 5 1 8\n", "4 4 1 0 1\n1\n\n1\n1 2 1 4\n", "2 5 1 0 1\n2\n\n1\n1 4 1 5\n", "10 10 1 8 4\n10\n2 3 4 5 6 7 8 9\n10\n1 1 3 1\n2 1 7 1\n1 1 9 1\n7 1 4 1\n10 1 7 1\n2 1 7 1\n3 1 2 1\n5 1 2 1\n10 1 5 1\n6 1 9 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In the year of 30XX participants of some world programming championship live in a single large hotel. The hotel has n floors. Each floor has m sections with a single corridor connecti...
954_B. String Typing_3812
You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard. Initially, you have an empty string. Until you type the whole string, you may perform the following operation: * add a character to the end of the string. Besides, at most once you may perform one...
n=int(input()) s=input() i=0 d="" ls=[] mx=-1 while i<n: temp=s[0:i+1] for j in range(i+1,n+1): if temp==s[i+1:j]: mx=max(mx,len(temp)) i+=1 if mx>0: print(len(temp)-mx+1) else: print(len(temp))
{ "input": [ "8\nabcdefgh\n", "7\nabcabca\n", "5\nababa\n", "8\naaabcabc\n", "7\nabcdabc\n", "29\nabbabbaabbbbaababbababbaabbaa\n", "5\nbaaaa\n", "54\ndgafkhlgdhjflkdafgjldjhgkjllfallhsggaaahkaggkhgjgflsdg\n", "7\nabcdbcd\n", "20\nccbbcbaabcccbabcbcaa\n", "11\nababcabcabc\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a string s consisting of n lowercase Latin letters. You have to type this string using your keyboard. Initially, you have an empty string. Until you type the whole stri...
980_A. Links and Pearls_3816
A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one. <image> You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times...
s =input() dash = s.count('-') ring = s.count('o') k = min(dash,ring) m = max(dash,ring) if dash == 0 or ring==0: print('YES') else: if dash%ring==0: print('YES') else: print('NO')
{ "input": [ "<span class=\"tex-font-style-tt\">-o---o-</span>\n", "<span class=\"tex-font-style-tt\">-o-o--</span>", "ooo\n", "<span class=\"tex-font-style-tt\">-o---</span>\n", "-oo-oo------\n", "-----------------------------------o---------------------o--------------------------\n", "--...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one. <image> You can remove a link or a pearl and insert ...
9_A. Die Roll_3820
Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Motherland of his ancestors. Wakko thought about Tasmania, its beaches, sun and sea. Dot chose Transylvania as the most mysterious and unpr...
x, y = input().split() x = int(x) y = int(y) z = 7 - max(x, y) ans = z/6 if ans == (1/6): print("1/6") elif ans == (2/6): print("1/3") elif ans == (3/6): print("1/2") elif ans == (4/6): print("2/3") elif ans == (5/6): print("5/6") else: print("1/1")
{ "input": [ "4 2\n", "6 6\n", "1 4\n", "4 4\n", "3 3\n", "6 4\n", "6 3\n", "5 4\n", "5 6\n", "3 4\n", "1 3\n", "3 2\n", "5 5\n", "1 1\n", "2 1\n", "3 1\n", "3 5\n", "1 6\n", "6 5\n", "2 3\n", "4 3\n", "2 2\n", "3 6\n", "1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Yakko, Wakko and Dot, world-famous animaniacs, decided to rest from acting in cartoons, and take a leave to travel a bit. Yakko dreamt to go to Pennsylvania, his Motherland and the Mo...
p02544 ACL Contest 1 - Shuffle Window_3832
Given are a permutation p_1, p_2, \dots, p_N of (1, 2, ..., N) and an integer K. Maroon performs the following operation for i = 1, 2, \dots, N - K + 1 in this order: * Shuffle p_i, p_{i + 1}, \dots, p_{i + K - 1} uniformly randomly. Find the expected value of the inversion number of the sequence after all the oper...
import sys readline = sys.stdin.buffer.readline mod=998244353 class BIT: def __init__(self,n): self.n=n self.buf=[0]*n def add(self,i,v): buf=self.buf while i<n: buf[i]+=v if buf[i]>=mod: buf[i]-=mod i+=(i+1)&(-i-1) def get(self,i): buf=self.buf res=0 while i>=0: res+=buf[i] if...
{ "input": [ "10 3\n1 8 4 9 2 3 7 10 5 6", "3 2\n1 2 3", "3 3\n1 2 3", "10 6\n1 8 4 9 2 3 7 10 5 6", "3 2\n2 1 3", "10 5\n1 8 4 9 2 3 7 10 5 6", "10 7\n1 8 4 9 2 3 7 10 5 6", "3 2\n2 3 1", "10 8\n1 8 4 9 2 3 7 10 5 6", "10 4\n1 8 4 9 2 3 7 10 5 6", "3 3\n1 2 2", "3 3\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given are a permutation p_1, p_2, \dots, p_N of (1, 2, ..., N) and an integer K. Maroon performs the following operation for i = 1, 2, \dots, N - K + 1 in this order: * Shuffle p_i, ...
p02675 AtCoder Beginner Contest 168 - ∴ (Therefore)_3836
The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese. When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a pos...
n = input() print('bon' if n[-1]=='3' else 'pon' if n[-1] in ['0','1','6','8'] else 'hon')
{ "input": [ "16", "2", "183", "31", "4", "23", "39", "49", "6", "76", "62", "12", "58", "70", "9", "8", "5", "0", "10", "15", "1", "7", "13", "11", "3", "14", "21", "22", "44", "52", "57", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese. When counting pencils in Japanese, the counter word "本" follows the nu...
p02803 AtCoder Beginner Contest 151 - Maze Master_3840
Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns. The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is `#`, and a "road" square if S_{ij} is `.`. From a road square, you can move to a horizontally or vertically adjacent ro...
import sys from collections import deque H,W=map(int,input().split()) S=list(sys.stdin) ans=0 for i in range(H): for j in range(W): if S[i][j]=='#': continue dist=[[-1]*W for _ in range(H)] dist[i][j]=0 que=deque() que.append((i,j)) while que: ...
{ "input": [ "3 3\n...\n...\n...", "3 5\n...#.\n.#.#.\n.#...", "3 3\n...\n../\n...", "0 5\n...#.\n.#.#.\n.#...", "0 5\n.#...\n.#.#.\n.#...", "0 5\n.#...\n.#.#.\n.$...", "0 5\n.#..-\n.#.#.\n.$...", "0 5\n.#..-\n.#.#.\n.$..-", "-1 5\n.#..-\n.#.#.\n.$..-", "0 6\n.#..-\n.#.#.\n.$.....
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns. The square at the i-th row from the top and the j-th column is a "wall" squ...
p02939 AtCoder Grand Contest 037 - Dividing a String_3844
Given is a string S consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition: * There exists a partition of S into K non-empty strings S=S_1S_2...S_K such that S_i \neq S_{i+1} (1 \leq i \leq K-1). Here S_1S_2...S_K represents the concatenation of S_1,S_2,....
s = input() t = '' k = 0 prev = '' for i in range(len(s)): t += s[i] if prev != t: k += 1 prev = t t = '' print(k)
{ "input": [ "aaaccacabaababc", "aabbaa", "aaaccacabaababb", "a`bbaa", "abbccacabaababa", "abbccaaabaababc", "b`aba`", "b``aaa", "`abab`cbbba`c`b", "`_b`ab`_babcdba", "aabccacabaababb", "a`bcaa", "bbabaabacaccbaa", "a`acaa", "c`aaaa", "b`aaaa", "abbb...
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 consisting of lowercase English letters. Find the maximum positive integer K that satisfies the following condition: * There exists a partition of S into K non-em...
p03076 AtCoder Beginner Contest 123 - Five Dishes_3848
The restaurant AtCoder serves the following five dishes: * ABC Don (rice bowl): takes A minutes to serve. * ARC Curry: takes B minutes to serve. * AGC Pasta: takes C minutes to serve. * APC Ramen: takes D minutes to serve. * ATC Hanbagu (hamburger patty): takes E minutes to serve. Here, the time to serve a dish is ...
A = [-int(input()) for _ in range(5)] m = max(A, key = lambda x: x%10) print(-(m-m//10*10+sum([a//10*10 for a in A])))
{ "input": [ "123\n123\n123\n123\n123", "29\n20\n7\n35\n120", "101\n86\n119\n108\n57", "123\n123\n161\n123\n123", "29\n20\n13\n35\n120", "101\n86\n119\n108\n92", "123\n123\n161\n123\n163", "29\n20\n9\n35\n120", "101\n86\n119\n125\n92", "123\n123\n161\n123\n263", "29\n20\n4\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The restaurant AtCoder serves the following five dishes: * ABC Don (rice bowl): takes A minutes to serve. * ARC Curry: takes B minutes to serve. * AGC Pasta: takes C minutes to serve...
p03218 Dwango Programming Contest V - Cyclic GCDs_3851
Niwango-kun has \\(N\\) chickens as his pets. The chickens are identified by numbers \\(1\\) to \\(N\\), and the size of the \\(i\\)-th chicken is a positive integer \\(a_i\\). \\(N\\) chickens decided to take each other's hand (wing) and form some cycles. The way to make cycles is represented by a permutation \\(p\\)...
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) from fractions import gcd from functools import reduce MOD = 998244353 N,*A = map(int,read().split()) A.sort() """ 安い鳥から追加していく。(サイクルの個数 -> 美しさ)が多項式倍で遷移する。 Gaussの補題より、最大個...
{ "input": [ "4\n2 5 2 5", "2\n4 3", "4\n2 5 2 8", "2\n4 4", "4\n2 9 3 8", "4\n0 9 3 8", "2\n3 8", "2\n4 1", "4\n2 1 3 1", "2\n24 8", "2\n5 8", "2\n15 11", "2\n7 8", "4\n3 12 6 4", "2\n10 16", "4\n2 5 3 8", "2\n4 7", "2\n4 8", "4\n0 1 3 8", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Niwango-kun has \\(N\\) chickens as his pets. The chickens are identified by numbers \\(1\\) to \\(N\\), and the size of the \\(i\\)-th chicken is a positive integer \\(a_i\\). \\(N\...
p03529 CODE FESTIVAL 2017 Final - Mancala_3856
Consider the following game: * The game is played using a row of N squares and many stones. * First, a_i stones are put in Square i\ (1 \leq i \leq N). * A player can perform the following operation as many time as desired: "Select an integer i such that Square i contains exactly i stones. Remove all the stones from S...
""" https://atcoder.jp/contests/cf17-final/tasks/cf17_final_g 1回の操作で捨てられる石は1個 いくつ捨てられるかを考えた方がいいだろう 大きい方から?→そんなことはない 小さいほうから?→そんなことはない 今i=aiを満たしている中で最小のものからやるべき?→これはそう 数え上げなので…? → X回とれる盤面は何通りあるか(1) または、あるindexより右をX回選択できるindexより右側の数列は何通りか(2) → 左は自由 (2)の方がやりやすそう(Nはでかいとする) → 右端は1通り (Nのみ) → 右から2番目は 1回選択が N-1,A / 2回選択が...
{ "input": [ "20 17", "2 2", "36 17", "4 2", "36 18", "4 3", "36 22", "59 22", "3 2", "49 17", "4 0", "36 9", "4 1", "29 22", "59 35", "6 1", "62 9", "8 1", "72 35", "12 1", "88 9", "72 32", "5 1", "3 3", "41 17", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider the following game: * The game is played using a row of N squares and many stones. * First, a_i stones are put in Square i\ (1 \leq i \leq N). * A player can perform the fol...
p03690 AtCoder Grand Contest 016 - XOR Replace_3859
There is a sequence of length N: a = (a_1, a_2, ..., a_N). Here, each a_i is a non-negative integer. Snuke can repeatedly perform the following operation: * Let the XOR of all the elements in a be x. Select an integer i (1 ≤ i ≤ N) and replace a_i with x. Snuke's objective is to match a with another sequence b = (...
class UnionFindVerSize(): def __init__(self, N): self._parent = [n for n in range(0, N)] self._size = [1] * N def find_root(self, x): if self._parent[x] == x: return x self._parent[x] = self.find_root(self._parent[x]) return self._parent[x] def unite(self, x, y): ...
{ "input": [ "3\n0 1 2\n3 1 0", "4\n0 1 2 3\n1 0 3 2", "3\n0 1 2\n0 1 2", "2\n1 1\n0 0", "3\n0 1 2\n3 1 1", "3\n0 0 1\n1 0 0", "3\n1 -3 1\n-3 -3 1", "4\n0 1 2 3\n1 0 3 4", "3\n0 2 2\n0 1 2", "2\n1 1\n-1 0", "3\n0 1 2\n3 0 0", "4\n0 1 2 0\n1 0 3 4", "3\n0 2 4\n0 1 2"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a sequence of length N: a = (a_1, a_2, ..., a_N). Here, each a_i is a non-negative integer. Snuke can repeatedly perform the following operation: * Let the XOR of all the e...
p00096 Sum of 4 Integers II_3867
Enter a positive integer n of 4,000 or less, with a pair of integers a, b, c, d in the range 0-1000. a + b + c + d = n Create a program that outputs the number of combinations that satisfy the conditions. Input Given multiple datasets. Each dataset is given n on one row. Please process until the end of the inpu...
import sys for n in sys.stdin:print(sum(max(0,min(i,2002-i))*max(0,min(int(n)-i+2,2000-int(n)+i))for i in range(int(n)+2)))
{ "input": [ "2\n3\n35", "2\n0\n35", "2\n-1\n35", "2\n-1\n24", "2\n-1\n43", "2\n-1\n72", "2\n-1\n81", "2\n-1\n61", "2\n-1\n82", "2\n0\n82", "2\n-1\n59", "2\n-1\n100", "2\n-2\n110", "2\n-2\n111", "2\n2\n35", "2\n0\n63", "2\n-1\n21", "2\n-1\n17", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Enter a positive integer n of 4,000 or less, with a pair of integers a, b, c, d in the range 0-1000. a + b + c + d = n Create a program that outputs the number of combinations tha...
p00228 Seven Segments_3871
The screen that displays digital numbers that you often see on calculators is called a "7-segment display" because the digital numbers consist of seven parts (segments). The new product to be launched by Wakamatsu will incorporate a 7-segment display into the product, and as an employee, you will create a program to d...
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0228 """ import sys from sys import stdin input = stdin.readline LED_pattern = [ ##-gfedcba 0b0111111, # 0 0b0000110, # 1 0b1011011, # 2 0b1001111, #...
{ "input": [ "3\n0\n5\n1\n1\n0\n-1", "3\n0\n5\n1\n1\n1\n-1", "3\n0\n9\n1\n1\n0\n-1", "3\n0\n5\n1\n1\n2\n-1", "3\n1\n9\n1\n1\n0\n-1", "3\n0\n6\n1\n1\n2\n-1", "3\n1\n9\n0\n1\n0\n-1", "3\n0\n5\n0\n1\n0\n-1", "3\n1\n5\n1\n1\n0\n-1", "3\n0\n6\n1\n1\n1\n-1", "3\n0\n7\n1\n1\n0\n-1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The screen that displays digital numbers that you often see on calculators is called a "7-segment display" because the digital numbers consist of seven parts (segments). The new prod...
p00390 A Round Table for Sages_3874
$N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation. * If sage $i$ is right-handed and a left-handed sage sits on his right, a level of frustration $w_i$ occurs to him. A right-handed sage on his right d...
n = int(input()) aw = [[0,0] for i in range(n)] for i, a in enumerate(map(int, input().split())): aw[i][0] = a for i, w in enumerate(map(int, input().split())): aw[i][1] = w migimin = 1001 hidarimin = 1001 for a,w in aw: if a: hidarimin = min(hidarimin, w) else: migimin = min(migimin, w) if hidarimin>1000 or mi...
{ "input": [ "3\n0 0 0\n1 2 3", "5\n1 0 0 1 0\n2 3 5 1 2", "3\n0 0 0\n1 1 3", "5\n1 0 0 1 0\n2 3 6 1 2", "5\n1 1 0 1 0\n4 3 5 1 4", "5\n1 1 0 1 0\n4 3 0 1 4", "5\n1 0 0 1 0\n2 3 5 2 2", "3\n0 1 0\n1 1 2", "3\n0 0 1\n1 2 5", "3\n0 0 1\n1 0 7", "3\n0 0 1\n1 0 14", "5\n1 1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: $N$ sages are sitting around a round table with $N$ seats. Each sage holds chopsticks with his dominant hand to eat his dinner. The following happens in this situation. * If sage $i$...
p00606 Cleaning Robot_3878
Dr. Asimov, a robotics researcher, loves to research, but hates houseworks and his house were really dirty. So, he has developed a cleaning robot. As shown in the following figure, his house has 9 rooms, where each room is identified by an alphabet: <image> The robot he developed operates as follows: * If the bat...
# AOJ 1020: Cleaning Robot # Python3 2018.7.5 bal4u mv = ((-1,0),(0,1),(1,0),(0,-1)) while True: n = int(input()) if n == 0: break t1, t2, t3 = input().split() s, t, b = ord(t1)-ord('A'), ord(t2)-ord('A'), ord(t3)-ord('A') f = [[[0.0 for a in range(3)] for c in range(3)] for r in range(17)] f[0][s//3][s%3] = 1 ...
{ "input": [ "1\nE A C\n1\nE B C\n2\nE A B\n0", "1\nD A C\n1\nE B C\n2\nE A B\n0", "1\nD A C\n1\nE B C\n2\nF A B\n0", "1\nD A C\n0\nE B C\n2\nF A B\n0", "1\nC A C\n0\nE B C\n2\nF A B\n0", "1\nE A B\n1\nE B C\n2\nE A B\n0", "1\nD A C\n1\nE A D\n2\nF A B\n0", "1\nC A D\n1\nD B C\n2\nF A ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dr. Asimov, a robotics researcher, loves to research, but hates houseworks and his house were really dirty. So, he has developed a cleaning robot. As shown in the following figure, h...
p00743 Discrete Speed_3882
Consider car trips in a country where there is no friction. Cars in this country do not have engines. Once a car started to move at a speed, it keeps moving at the same speed. There are acceleration devices on some points on the road, where a car can increase or decrease its speed by 1. It can also keep its speed there...
from collections import defaultdict from heapq import heappop, heappush while True: n, m = map(int, input().split()) if n==0 and m==0: break s, g = map(int, input().split()) graph = defaultdict(list) for _ in range(m): x, y, d, c = map(int, input().split()) graph[x].append((y, d, c)) graph[y...
{ "input": [ "2 0\n1 2\n5 4\n1 5\n1 2 1 1\n2 3 2 2\n3 4 2 2\n4 5 1 1\n6 6\n1 6\n1 2 2 1\n2 3 2 1\n3 6 2 1\n1 4 2 30\n4 5 3 30\n5 6 2 30\n6 7\n1 6\n1 2 1 30\n2 3 1 30\n3 1 1 30\n3 4 100 30\n4 5 1 30\n5 6 1 30\n6 4 1 30\n0 0", "2 0\n1 2\n5 4\n1 5\n1 2 1 1\n2 3 2 2\n3 4 2 2\n4 5 1 1\n6 6\n1 6\n1 2 2 1\n2 3 2 1\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider car trips in a country where there is no friction. Cars in this country do not have engines. Once a car started to move at a speed, it keeps moving at the same speed. There a...
p01146 Princess in Danger_3887
Princess in Danger Princess crisis English text is not available in this practice contest. A brave princess in a poor country's tomboy is married to another country for a political marriage. However, a villain who was trying to kill the princess attacked him on the way to his wife, and the princess was seriously inj...
from heapq import heappop, heappush def dijkstra(links, n, m, freezables, s, t): queue = [(0, 0, s, m)] visited = [0] * n while queue: link_cost, cost, node, remain = heappop(queue) if node == t: # Congratulations! if link_cost <= m: return link_cost...
{ "input": [ "2 1 0 1 0 1\n0 1 2\n3 1 1 2 0 1\n2\n0 2 1\n1 2 1\n3 2 1 2 0 1\n2\n0 2 1\n1 2 1\n4 4 1 4 1 3\n2\n0 1 2\n1 2 4\n0 2 1\n3 0 3\n5 3 2 6 0 3\n1 2\n2 1 2\n1 0 1\n3 4 1\n2 4 1\n4 1 2\n2 0 2\n5 4 2 6 0 3\n1 2\n4 2 4\n2 1 2\n4 3 1\n0 1 5\n1 4 2\n2 0 3\n0 0 0 0 0 0", "2 1 0 1 0 1\n0 1 2\n3 1 1 2 0 1\n2\n0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Princess in Danger Princess crisis English text is not available in this practice contest. A brave princess in a poor country's tomboy is married to another country for a political...
p01284 Erratic Sleep Habits_3891
Peter is a person with erratic sleep habits. He goes to sleep at twelve o'lock every midnight. He gets up just after one hour of sleep on some days; he may even sleep for twenty-three hours on other days. His sleeping duration changes in a cycle, where he always sleeps for only one hour on the first day of the cycle. ...
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 = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdin....
{ "input": [ "2\n1 23\n3\n1 1\n2 1\n3 1\n0", "2\n0 23\n3\n1 1\n2 1\n3 1\n0", "2\n0 23\n3\n1 1\n1 1\n3 1\n0", "2\n1 23\n3\n1 1\n4 1\n3 1\n0", "2\n1 23\n3\n2 1\n10 1\n3 1\n0", "2\n0 23\n3\n1 1\n2 1\n3 2\n0", "2\n0 23\n3\n1 1\n1 0\n3 1\n0", "2\n0 23\n3\n1 2\n1 0\n3 1\n0", "2\n0 23\n3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Peter is a person with erratic sleep habits. He goes to sleep at twelve o'lock every midnight. He gets up just after one hour of sleep on some days; he may even sleep for twenty-three...
p01766 Soccer_3898
Problem statement We played an AI soccer match between Country A and Country B. You have a table that records the player who had the ball at a certain time and its position. The table consists of N rows, and the i-th row from the top consists of the following elements. * Number of frames f_i * The uniform number of t...
import math n = int(input()) prev = list(map(int, input().split())) max_disA = -1 max_disB = -1 min_timeA = 10**9 min_timeB = 10**9 for _ in range(n-1): curr = list(map(int, input().split())) if prev[1] != curr[1] and prev[2] == curr[2]: dis = math.sqrt((prev[3] - curr[3])**2 + (prev[4] - curr[4])**2...
{ "input": [ "5\n0 1 0 3 4\n30 1 0 3 4\n90 2 0 6 8\n120 1 1 1 1\n132 2 1 2 2", "5\n0 1 0 3 4\n30 1 0 3 4\n90 2 0 6 8\n120 1 1 1 1\n132 0 1 2 2", "5\n0 1 0 3 4\n16 1 0 3 4\n90 2 0 6 8\n120 1 1 1 1\n132 -1 1 2 2", "5\n0 1 0 3 4\n16 1 0 3 4\n90 2 1 6 8\n120 1 1 1 1\n132 -1 1 2 2", "5\n0 1 0 3 6\n16 1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem statement We played an AI soccer match between Country A and Country B. You have a table that records the player who had the ball at a certain time and its position. The tabl...
p01900 Mod!Mod!_3901
C: Mod! Mod! story That's right! I'm looking for eyewitness testimony! A phantom thief has appeared in Aizu! Everyone's horse stick was stolen! Who is the culprit! ?? Unravel! Mod! Mod! Problem statement "Eyes" ... it's a miracle bud that swells in the hearts of the chosen ones ... You can steal anything with the s...
# AOJ 2800: Mod!Mod! # Python3 2018.7.11 bal4u n = int(input()) a = list(map(int, input().split())) c = [0]*3 for i in a: c[i%3] += 1 if (c[1]|c[2]) == 0: ans = 1 else: ans, n = c[0], n-c[0] if n <= 3: ans += n else: t = max(-3, min(3, c[1]-c[2])) if t > 0: ans += 2*c[2]+t else: ans += 2*c[1]-t print(ans)
{ "input": [ "6\n2 5 2 5 2 1", "6\n2 5 2 6 2 1", "6\n1 25 1 10 1 3", "6\n0 5 0 11 2 2", "6\n0 3 0 9 0 0", "6\n2 5 0 6 2 1", "6\n2 9 0 6 2 1", "6\n2 9 1 6 2 1", "6\n0 9 1 6 2 1", "6\n0 9 1 6 2 2", "6\n0 9 1 6 2 3", "6\n0 9 1 5 2 3", "6\n0 9 1 3 2 3", "6\n0 10 1 3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: C: Mod! Mod! story That's right! I'm looking for eyewitness testimony! A phantom thief has appeared in Aizu! Everyone's horse stick was stolen! Who is the culprit! ?? Unravel! Mod! ...
p02037 Tile_3905
problem I want to put as many rectangular tiles as possible on a rectangular wall with a size of $ h $ in height and $ w $ in width, and a size of $ a $ in height and $ b $ in width. The following conditions must be met when attaching tiles. * Do not stack tiles. * Do not apply tiles diagonally, that is, any edge o...
h,w=map(int,input().split()) a,b=map(int,input().split()) high=(h//a)*a wide=(w//b)*b print(h*w-high*wide)
{ "input": [ "5 8\n2 2", "5 8\n3 2", "5 8\n6 2", "6 8\n6 3", "12 8\n6 3", "12 8\n9 3", "16 8\n9 3", "27 8\n9 6", "27 8\n9 9", "27 14\n9 9", "27 14\n9 16", "27 15\n5 16", "27 9\n5 16", "0 9\n5 29", "-1 10\n2 49", "-1 12\n4 49", "12 8\n11 3", "12 8...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem I want to put as many rectangular tiles as possible on a rectangular wall with a size of $ h $ in height and $ w $ in width, and a size of $ a $ in height and $ b $ in width....
p02323 Traveling Salesman Problem_3910
For a given weighted directed graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it starts. * It visits each vertex exactly once. Constraints * 2 ≤ |V| ≤ 15 * 0 ≤ di ≤ 1,000 * There are no multiedge Input |V| |E| s0 t0 d...
v,e=[int(j) for j in input().split()] d=[[10**18]*v for i in range(v)] dp=[[-1]*v for i in range(1<<v)] for i in range(e): s,t,w=[int(j) for j in input().split()] d[s][t]=w def f(s,n,dp): if dp[s][n]>=0: return dp[s][n] if s==(1<<v)-1 and n==0: dp[s][n]=0 return 0 tmp=10**1...
{ "input": [ "3 3\n0 1 1\n1 2 1\n0 2 1", "4 6\n0 1 2\n1 2 3\n1 3 9\n2 0 1\n2 3 6\n3 2 4", "3 3\n0 0 1\n1 2 1\n0 2 1", "4 6\n0 1 2\n2 2 3\n1 3 9\n2 0 1\n2 3 6\n3 2 4", "4 6\n0 1 2\n2 2 3\n1 3 1\n2 0 1\n2 3 6\n3 2 4", "4 6\n0 1 2\n2 2 3\n1 3 14\n2 0 1\n2 3 19\n3 2 4", "1 1\n0 0 0\n1 2 1\n0 2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For a given weighted directed graph G(V, E), find the distance of the shortest route that meets the following criteria: * It is a closed cycle where it ends at the same point it star...
p02468 Power_3914
For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M. Constraints * 1 ≤ m ≤ 100 * 1 ≤ n ≤ 109 Input m n Two integers m and n are given in a line. Output Print mn (mod 1,000,000,007) in a line. Examples Input 2 3 Output 8 Input 5 8 Output...
# https://onlinejudge.u-aizu.ac.jp/courses/library/6/NTL/1/NTL_1_B # pythonのpowが優秀でMODまで取ることができる MOD = 10**9 + 7 a, b = list(map(int, input().split())) print(pow(a, b, MOD))
{ "input": [ "2 3", "5 8", "2 1", "5 9", "2 2", "7 9", "3 3", "13 9", "1 3", "13 1", "12 1", "3 1", "0 1", "4 3", "5 15", "6 9", "2 4", "2 9", "3 4", "12 2", "-1 1", "-2 1", "4 4", "2 15", "6 8", "4 9", "3 2", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For given integers m and n, compute mn (mod 1,000,000,007). Here, A (mod M) is the remainder when A is divided by M. Constraints * 1 ≤ m ≤ 100 * 1 ≤ n ≤ 109 Input m n Two integ...
1003_C. Intense Heat_3924
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually cal...
n, k = map(int, input().split()) ar = list(map(int, input().split())) average = M = 0 for i in range(n): s = 0 for j in range(i,n): s += ar[j] if (j-i+1) >= k: average = s / (j-i+1) M = max(M, average) print(M)
{ "input": [ "4 3\n3 4 1 2\n", "5 5\n4 6 6 6 2\n", "5 1\n3 10 9 10 6\n", "3 2\n2 1 2\n", "5 2\n7 3 3 1 8\n", "5 4\n5 1 10 6 1\n", "5 3\n1 7 6 9 1\n", "1 1\n5000\n", "5 1\n3 10 6 10 6\n", "3 2\n0 1 2\n", "5 2\n7 3 6 1 8\n", "5 2\n5 1 10 6 1\n", "1 1\n3612\n", "4 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnor...
1027_D. Mouse Hunt_3928
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years. The dormitory consists of n rooms and a single mouse! Girls decided to set mouse tra...
n = int(input()) C = [int(s) for s in input().split(" ")] A = [int(s)-1 for s in input().split(" ")] al = [False for i in range(0, n)] ans = 0 for v in range(0, n): if al[v]: continue sequence = [] while not al[v]: sequence.append(v) al[v] = True v = A[v] if v in sequence...
{ "input": [ "4\n1 10 2 10\n2 4 2 2\n", "5\n1 2 3 2 10\n1 3 4 3 3\n", "7\n1 1 1 1 1 1 1\n2 2 2 3 6 7 6\n", "3\n1 2 4\n2 2 1\n", "5\n5 4 2 6 10\n2 4 3 3 2\n", "4\n2 1 2 2\n2 3 3 2\n", "8\n1 2 2 9 7 5 6 5\n8 4 1 5 5 1 1 8\n", "3\n76 74 84\n3 2 1\n", "3\n1 2 4\n2 3 3\n", "7\n2 1 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the uni...
1046_C. Space Formula_3932
Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race. Given the current ranking in the competition and points distribution for the next race, your task is to calculate the best possible ranking for a given astronaut after the next r...
n,k=map(int,input().strip().split()) cr = list(map(int,input().strip().split())) pa = list(map(int,input().strip().split())) x = cr[k-1]+pa[0] p = 0 if k==1: print(1) else: for i in range(k-1): if cr[i]+pa[-1]<=x: p +=1 del pa[-1] print(k-p)
{ "input": [ "4 3\n50 30 20 10\n15 10 7 3\n", "4 4\n29 25 13 10\n20 9 3 0\n", "4 2\n29 28 27 9\n1 1 1 0\n", "4 4\n29 25 13 9\n20 9 8 0\n", "4 1\n29 25 13 9\n3 2 1 0\n", "4 4\n29 25 13 0\n20 9 8 0\n", "1 1\n5\n5\n", "4 4\n29 25 13 10\n20 9 8 0\n", "4 2\n29 28 13 9\n1 1 1 0\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Formula 1 officials decided to introduce new competition. Cars are replaced by space ships and number of points awarded can differ per race. Given the current ranking in the competit...
1070_B. Berkomnadzor_3936
Berkomnadzor — Federal Service for Supervision of Communications, Information Technology and Mass Media — is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet. Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 ...
#!/usr/bin/env python3 # Copied solution import collections import sys import traceback class Input(object): def __init__(self): self.fh = sys.stdin def next_line(self): while True: line = sys.stdin.readline() if line == '\n': continue retur...
{ "input": [ "5\n-127.0.0.4/31\n+127.0.0.8\n+127.0.0.0/30\n-195.82.146.208/29\n-127.0.0.6/31\n", "4\n-149.154.167.99\n+149.154.167.100/30\n+149.154.167.128/25\n-149.154.167.120/29\n", "2\n+127.0.0.1/32\n-127.0.0.1\n", "1\n-149.154.167.99\n", "11\n-0.0.0.15/32\n-0.0.0.12/31\n-0.0.0.7/32\n-0.0.0.11/...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Berkomnadzor — Federal Service for Supervision of Communications, Information Technology and Mass Media — is a Berland federal executive body that protects ordinary residents of Berla...
1091_F. New Year and the Mallard Expedition_3940
Bob is a duck. He wants to get to Alice's nest, so that those two can duck! <image> Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a straight line, consisting of n segments. Bob is located to the left of the first segment, while Alice's nest is on the right of the last seg...
from heapq import heappush, heappop n = int(input()) L = list(map(int, input().split())) T = input() # fly -> walk, time cost: +4s, stamina: +2 # walk in place, time cost: +5s, stamina: +1 #fly -> swim, time cost: +2s, stamina: +2 #swim in place, time cost: +3s, stamina:+1 ans = sum(L) Q = [] for l, t in zip(L, T)...
{ "input": [ "2\n1 2\nWL\n", "3\n10 10 10\nGLW\n", "2\n10 10\nWL\n", "1\n10\nG\n", "4\n1 1 1 1\nWGWL\n", "2\n2 10\nGL\n", "3\n10 10 50\nWGL\n", "2\n100 100\nWG\n", "10\n324839129156 133475576222 987711341463 185514358673 88712073883 243757012712 950854402304 21947060440 59856728996...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Bob is a duck. He wants to get to Alice's nest, so that those two can duck! <image> Duck is the ultimate animal! (Image courtesy of See Bang) The journey can be represented as a str...
1110_D. Jongmah_3944
You are playing a game of Jongmah. You don't need to know the rules to solve this problem. You have n tiles in your hand. Each tile has an integer between 1 and m written on it. To win the game, you will need to form some number of triples. Each triple consists of three tiles, such that the numbers written on the tile...
from collections import Counter n, m = map(int, input().split()) B = list(map(int, input().split())) cnt = Counter(B) A = sorted(cnt.keys()) n = len(A) dp = [[0] * 3 for _ in range(3)] for i, a in enumerate(A): dp2 = [[0] * 3 for _ in range(3)] for x in range(1 if i >= 2 and a - 2 != A[i - 2] else 3): f...
{ "input": [ "10 6\n2 3 3 3 4 4 4 5 5 6\n", "13 5\n1 1 5 1 2 3 3 2 4 2 3 4 5\n", "12 6\n1 5 3 3 3 4 3 5 3 2 3 3\n", "14 9\n5 5 6 8 8 4 3 2 2 7 3 9 5 4\n", "14 11\n10 8 6 1 7 2 9 5 5 10 3 5 11 3\n", "1 1\n1\n", "3 3\n2 2 2\n", "10 4\n1 4 2 3 1 3 3 4 1 3\n", "3 4\n1 2 4\n", "10 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are playing a game of Jongmah. You don't need to know the rules to solve this problem. You have n tiles in your hand. Each tile has an integer between 1 and m written on it. To w...
1140_A. Detective Book_3948
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The i-th page contains some mystery that will be explained on page a_i (a_i ≥ i). Ivan wants to read the whole book. Each day, he reads the first page he didn'...
import sys #p = [[] for i in range(N)] #for i in range(N): # p[i] += map(int, sys.stdin.readline().split()) #Q = input() #for i in range(Q): # print(list(map(int, sys.stdin.readline().split()))) n = int(input()) a = [int(c) for c in sys.stdin.readline().split()] aktmax, d = 0, 0 for i, c in enumerate(a, start=1)...
{ "input": [ "9\n1 3 3 6 7 6 8 8 9\n", "1\n1\n", "13\n1 2 3 4 5 6 7 8 9 10 11 12 13\n", "11\n1 2 3 4 5 6 7 8 9 10 11\n", "15\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n", "10\n1 3 4 5 5 6 7 8 9 10\n", "12\n1 2 3 4 5 6 7 8 9 10 11 12\n", "4\n1 2 4 4\n", "17\n9 4 3 6 7 6 8 8 9 10 11 12 13...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The i-th page contains ...
1180_D. Tolik and His Uncle_3954
This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle. After a long time thinking, Tolik's uncle hasn't any...
# [0,0,0] # [0,0,0] # [1,0,0] # [0,0,0] # [0,0,0] # [1,0,1] # [0,0,0] # [0,0,0] # [1,1,1] # [0,0,0] # [0,0,0] # [1,1,1] # [0,0,1] # [0,0,0] # [0,0,0] 3,2 # # 0,0 3,2 # 0,1 3,1 # 0,2 3,0 # 1,0 2,2 # 1,1 2,1 # 1,2 2,0 n, m = map(int, input().split()) ans = [] if n % 2 == 0: for i in range(int(n / 2)): ...
{ "input": [ "1 1\n", "2 3\n", "782 19\n", "1 2892\n", "61 389\n", "4830 1\n", "6354 1\n", "8 8\n", "6 8\n", "1249 19\n", "1 3575\n", "61 407\n", "5803 1\n", "6354 2\n", "8 4\n", "3 3\n", "1122 19\n", "1 6589\n", "5803 2\n", "6925 2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't...
1199_D. Welfare State_3958
There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have. Sometimes the governm...
import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def main(): n = int(input()) A = list(map(int, input().split())) q = int(input()) Q = [] for i in range(q): temp = list(map(int, input().split())) t = temp[0] if t == 1: p, x = ...
{ "input": [ "5\n3 50 2 1 10\n3\n1 2 0\n2 8\n1 3 20\n", "4\n1 2 3 4\n3\n2 3\n1 2 2\n2 1\n", "4\n1 2 3 4\n2\n2 3\n2 2\n", "4\n1 2 3 4\n2\n2 1000\n2 1\n", "1\n1\n3\n2 4\n1 1 2\n2 10\n", "5\n1 2 3 4 5\n10\n1 1 0\n2 1\n1 2 0\n2 2\n1 3 0\n2 3\n1 4 0\n2 4\n1 5 0\n2 5\n", "10\n1 2 3 4 5 6 7 8 9 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a country with n citizens. The i-th of them initially has a_{i} money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or ear...
1216_C. White Sheet_3962
There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume that the bottom left corner of the table has coordinates (0, 0), and coordinate axes are left and bottom sides of the table, then the bott...
a=list(map(int,input().split())) x1,y1,x2,y2=a[0],a[1],a[2],a[3] a=list(map(int,input().split())) x3,y3,x4,y4=a[0],a[1],a[2],a[3] a=list(map(int,input().split())) x5,y5,x6,y6=a[0],a[1],a[2],a[3] l1=l2=l3=l4=1 if (x1>=x5 and x2<=x6 and y1>=y5 and y1<=y6) or (x1>=x3 and x2<=x4 and y1>=y3 and y1<=y4) or (y1>=y5 and y1<=y6...
{ "input": [ "0 0 1000000 1000000\n0 0 499999 1000000\n500000 0 1000000 1000000\n", "3 3 7 5\n0 0 4 6\n0 0 7 4\n", "5 2 10 5\n3 1 7 6\n8 1 11 7\n", "2 2 4 4\n1 1 3 5\n3 1 5 5\n", "7 1 8 3\n0 0 4 2\n2 1 18 21\n", "5 2 10 5\n8 1 11 7\n3 1 7 6\n", "3 3 4 4\n0 0 1 1\n3 3 4 4\n", "50 100 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle with its sides parallel to the sides of the table. If you will take a look from above and assume t...
1257_F. Make Them Similar_3968
Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 100); * 1337 and 4213 are similar (binary representations are 10100111001 and 1000001110101); * 3 and 2 are not similar (binary repr...
def main(): import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) dic1 = {} dic2 = {} A1 = [0] * N for i, a in enumerate(A): A1[i] = a>>15 for i in range(2**15): tmp = [0] * N for j, a in enumerate(A1): a ^= i...
{ "input": [ "4\n3 17 6 0\n", "3\n1 2 3\n", "2\n7 2\n", "3\n43 12 12\n", "2\n105953580 334230288\n", "2\n1073709056 32767\n", "10\n22 27 7 14 10 9 10 15 13 0\n", "10\n22 15 29 8 5 17 27 27 2 10\n", "100\n784 784 784 784 784 784 784 784 784 784 784 784 784 784 784 784 784 784 784 78...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Let's call two numbers similar if their binary representations contain the same number of digits equal to 1. For example: * 2 and 4 are similar (binary representations are 10 and 1...
1281_A. Suffix Three_3972
We just discovered a new data structure in our research group: a suffix three! It's very useful for natural language processing. Given three languages and three suffixes, a suffix three can determine which language a sentence is written in. It's super simple, 100% accurate, and doesn't involve advanced machine learni...
n = int(input()) for _ in range(n): s = input().split("_")[-1:][0] if s[-2:] == "po": print("FILIPINO") elif s[-4:] == "desu" or s[-4:] == "masu": print("JAPANESE") else: print("KOREAN")
{ "input": [ "8\nkamusta_po\ngenki_desu\nohayou_gozaimasu\nannyeong_hashimnida\nhajime_no_ippo\nbensamu_no_sentou_houhou_ga_okama_kenpo\nang_halaman_doon_ay_sarisari_singkamasu\nsi_roy_mustang_ay_namamasu\n", "30\nopo\np_po\npopo\ndesu\npo\nudesu\npodesu\ndesupo\nsddesu\nmumasu\nmmnida\nmmasu\nmasu\ndesu_po\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We just discovered a new data structure in our research group: a suffix three! It's very useful for natural language processing. Given three languages and three suffixes, a suffix th...
1301_A. Three Strings_3976
You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th letter of c is c_i. For every i (1 ≤ i ≤ n) you must swap (i.e. exchange) c_i with either a_i or b_i. So in total you'll perform exactly...
t = int(input()) for T in range(t): a = input() b = input() c = input() n = len(a) count = 0 for i in range(n): if a[i] == c[i] or b[i] == c[i]: count += 1 if count == n: print("YES") else: print("NO")
{ "input": [ "4\naaa\nbbb\nccc\nabc\nbca\nbca\naabb\nbbaa\nbaba\nimi\nmii\niim\n", "1\nab\nab\nbb\n", "1\nba\nab\nbb\n", "4\naaa\nbbb\nccc\nabc\nbca\ncca\naabb\nbbaa\nbaba\nimi\nmii\niim\n", "4\naaa\nbbb\nccc\nabc\nbca\ncca\naabb\nbbaa\nbaba\niim\nmii\niim\n", "1\nba\nbb\ncb\n", "4\naa`\nb...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given three strings a, b and c of the same length n. The strings consist of lowercase English letters only. The i-th letter of a is a_i, the i-th letter of b is b_i, the i-th ...
1325_A. EhAb AnD gCd_3980
You are given a positive integer x. Find any such 2 positive integers a and b such that GCD(a,b)+LCM(a,b)=x. As a reminder, GCD(a,b) is the greatest integer that divides both a and b. Similarly, LCM(a,b) is the smallest integer such that both a and b divide it. It's guaranteed that the solution always exists. If ther...
# your code goes here for _ in range(int(input())): x=int(input()) print(1,end=" ") print(x-1)
{ "input": [ "2\n2\n14\n", "100\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a positive integer x. Find any such 2 positive integers a and b such that GCD(a,b)+LCM(a,b)=x. As a reminder, GCD(a,b) is the greatest integer that divides both a and b...
1343_F. Restore the Permutation by Sorted Segments_3984
We guessed a permutation p consisting of n integers. The permutation of length n is the array of length n where each element from 1 to n appears exactly once. This permutation is a secret for you. For each position r from 2 to n we chose some other index l (l < r) and gave you the segment p_l, p_{l + 1}, ..., p_r in s...
for _ in range(int(input())): n = int(input()) segs = [tuple(sorted(int(x) for x in input().split()[1:])) for __ in range(n - 1)] segs_having = [[j for j, seg in enumerate(segs) if i in seg] for i in range(n + 1)] segs_set = set(segs) for first in range(1, n + 1): try: segs_copy ...
{ "input": [ "5\n6\n3 2 5 6\n2 4 6\n3 1 3 4\n2 1 3\n4 1 2 4 6\n5\n2 2 3\n2 1 2\n2 1 4\n2 4 5\n7\n3 1 2 6\n4 1 3 5 6\n2 1 2\n3 4 5 7\n6 1 2 3 4 5 6\n3 1 3 6\n2\n2 1 2\n5\n2 2 5\n3 2 3 5\n4 2 3 4 5\n5 1 2 3 4 5\n", "1\n6\n4 2 3 4 5\n2 1 2\n2 3 5\n2 2 5\n6 1 2 3 4 5 6\n", "1\n6\n4 2 3 4 5\n2 1 3\n2 3 5\n2 2 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We guessed a permutation p consisting of n integers. The permutation of length n is the array of length n where each element from 1 to n appears exactly once. This permutation is a se...
1385_G. Columns Swaps_3990
You are given a table a of size 2 × n (i.e. two rows and n columns) consisting of integers from 1 to n. In one move, you can choose some column j (1 ≤ j ≤ n) and swap values a_{1, j} and a_{2, j} in it. Each column can be chosen no more than once. Your task is to find the minimum number of moves required to obtain pe...
for t in range(int(input())): n = int(input()) aa = [ [int(s)-1 for s in input().split(' ')] for _ in [0, 1] ] maxv = max(max(aa[0]), max(aa[1])) minv = min(min(aa[0]), min(aa[1])) if minv != 0 or maxv != n - 1: print(-1) continue counts = [0] * n index = ...
{ "input": [ "6\n4\n1 2 3 4\n2 3 1 4\n5\n5 3 5 1 4\n1 2 3 2 4\n3\n1 2 1\n3 3 2\n4\n1 2 2 1\n3 4 3 4\n4\n4 3 1 4\n3 2 2 1\n3\n1 1 2\n3 2 2\n", "6\n4\n1 2 3 4\n2 3 1 4\n5\n5 3 5 1 4\n1 2 3 2 4\n3\n1 2 1\n3 3 2\n4\n1 2 2 1\n3 4 3 4\n4\n4 3 1 4\n3 2 2 2\n3\n1 1 2\n3 2 2\n", "6\n4\n1 2 3 4\n2 3 1 4\n5\n5 3 5 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a table a of size 2 × n (i.e. two rows and n columns) consisting of integers from 1 to n. In one move, you can choose some column j (1 ≤ j ≤ n) and swap values a_{1, j}...
1407_E. Egor in the Republic of Dagestan_3994
Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan. There are n cities in the republic, some of them are connected by m directed roads without any additional conditions. In other words, road system of Dagestan represents an arbitrary d...
import sys input = sys.stdin.readline from collections import deque n, m = map(int, input().split()) back = [[] for i in range(n)] for _ in range(m): u, v, w = map(int, input().split()) u -= 1 v -= 1 back[v].append((u,w)) out = [2] * n outl = [-1] * n outl[-1] = 0 q = deque([n - 1]) while q: ...
{ "input": [ "3 4\n1 2 0\n1 3 1\n2 3 0\n2 3 1\n", "5 10\n1 2 0\n1 3 1\n1 4 0\n2 3 0\n2 3 1\n2 5 0\n3 4 0\n3 4 1\n4 2 1\n4 5 0\n", "4 8\n1 1 0\n1 3 0\n1 3 1\n3 2 0\n2 1 0\n3 4 1\n2 4 0\n2 4 1\n", "1 0\n", "10 40\n9 9 1\n4 10 0\n4 1 1\n6 4 1\n9 9 1\n6 4 1\n2 7 1\n3 7 1\n5 9 1\n1 9 0\n9 5 1\n1 3 0\n7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Egor is a famous Russian singer, rapper, actor and blogger, and finally he decided to give a concert in the sunny Republic of Dagestan. There are n cities in the republic, some of th...
1428_C. ABBB_3998
Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated....
# from debug import * import sys;input = sys.stdin.readline for _ in range(int(input())): s = input().strip() stack = [] for i in s: if stack and i == 'B': stack.pop() else: stack.append(i) print(len(stack))
{ "input": [ "3\nAAA\nBABA\nAABBBABBBB\n", "3\nAAA\nAABA\nAABBBABBBB\n", "3\nAAB\nBABA\nAABBBABBBB\n", "3\nAAB\nBABA\nBBBBABBBAA\n", "3\nAAB\nBAAA\nBBBBABBBAA\n", "3\nAAA\nBABB\nAABBBABBBB\n", "3\nAAB\nABAB\nBBBBABBBAA\n", "3\nBAA\nBAAA\nBBBBABBBAA\n", "3\nBBA\nBAAA\nBBABABBBBB\n",...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or...
1451_C. String Equality_4002
Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters. He wants to convert string a into string b by performing some (possibly zero) operations on a. In one move, he can either * choose an index i (1 ≤ i≤ n-1) and swap a_i and a_{i+1}, or * choos...
import sys input = sys.stdin.readline for _ in range(int(input())): n,k=map(int,input().split()) a=input() b=input() l1=[0]*26 l2=[0]*26 for i in range(n): l1[ord(a[i])-97]+=1 l2[ord(b[i])-97]+=1 f=False for i in range(25): z=l1[i]-l2[i] if(z<0 or z%k): ...
{ "input": [ "4\n3 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nddddcc\n", "4\n1 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nddddcc\n", "4\n3 3\nabc\nbcd\n4 2\nabba\nazza\n2 1\nzz\naa\n6 2\naaabba\nedddcc\n", "4\n3 3\nabb\nacd\n4 2\naaba\nazza\n2 1\nzz\naa\n6 2\naaabba\nedddcc\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ashish has two strings a and b, each of length n, and an integer k. The strings only contain lowercase English letters. He wants to convert string a into string b by performing some ...
1475_C. Ball in Berland_4006
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls. Each class must present two couples to the ball. In Vasya's class, a boys and b girls wish to participate. But not all boys and not...
# -*- coding: utf-8 -*- """ Created on Wed Jan 27 00:37:06 2021 @author: rishi """ import math def ncr(n,r): p=1 k=1 if(n-r<r): r=n-r; if(r!=0): while(r>0): p*=n k*=r m=math.gcd(p,k) p//=m k//=m n-=1 r-...
{ "input": [ "3\n3 4 4\n1 1 2 3\n2 3 2 4\n1 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2\n", "3\n6 4 4\n1 1 2 3\n2 3 2 4\n1 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2\n", "3\n6 4 4\n1 1 2 3\n2 3 2 3\n2 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2\n", "3\n6 4 4\n1 1 2 3\n2 3 1 4\n1 1 1\n1\n1\n2 2 4\n1 1 2 2\n1 2 1 2\n", "3\n6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and gi...
1500_A. Going Home_4010
It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya's friend presented her an integer array a. Several hours after starting her journey home Nastya remembered about the present. To ente...
def show(x, y, z, w): print("YES") print(x+1, y+1, z+1, w+1) exit(0) n=int(input()) ara=list(int(x) for x in input().split()) n=min(n, 2000) found={} for i in range(0, n): for j in range(i+1, n): cur_sm=ara[i]+ara[j] if cur_sm in found.keys() and found[cur_sm][0]!=i and found[cur_sm][1]!=j and found[cur_sm][0...
{ "input": [ "5\n1 3 1 9 20\n", "6\n2 1 5 2 7 4\n", "5\n1 2 4 8 11\n", "6\n1 2 5 8 10 12\n", "5\n1 4 10 11 13\n", "6\n1 2 2 4 8 11\n", "60\n1147861 2041994 899052 683478 931002 704737 1406756 1283948 1952357 1238212 1164251 1507228 932218 731251 1289342 1308931 1577922 1763727 1747882 1005...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nasty...
1525_C. Robot Collisions_4014
There are n robots driving along an OX axis. There are also two walls: one is at coordinate 0 and one is at coordinate m. The i-th robot starts at an integer coordinate x_i~(0 < x_i < m) and moves either left (towards the 0) or right with the speed of 1 unit per second. No two robots start at the same coordinate. Whe...
from collections import deque def main(): t=int(input()) allans=[] for _ in range(t): n,m=readIntArr() xes=readIntArr() direction=input().split() ans=[-1]*n # moving to each other will collide first, followed by moving in same direction, ...
{ "input": [ "5\n7 12\n1 2 3 4 9 10 11\nR R L L R R R\n2 10\n1 6\nR R\n2 10\n1 3\nL L\n1 10\n5\nR\n7 8\n6 1 7 2 3 5 4\nR L R L L L L\n", "1\n20 50\n4 6 8 9 10 13 14 16 18 20 23 25 30 32 33 38 42 43 45 46\nL L L L L L L L L L R R R L L R R L R R\n", "1\n10 20\n2 3 7 9 10 13 14 16 17 18\nL L R L L R R R L L...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n robots driving along an OX axis. There are also two walls: one is at coordinate 0 and one is at coordinate m. The i-th robot starts at an integer coordinate x_i~(0 < x_i ...
177_G2. Fibonacci Strings_4018
Fibonacci strings are defined as follows: * f1 = «a» * f2 = «b» * fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". You are given a Fibonacci string and m strings si. For each string si, find the number of times it occurs in the given Fibonacci string as...
F = ['', 'a', 'b', 'ba', 'bab', 'babba', 'babbabab', 'babbababbabba', 'babbababbabbababbabab', 'babbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabbababbababbabbababbabab', 'babbababbabbababbababbabbababbabbababbababbabbababbababbabbababbabbababbababbabbababbabba', 'babbababbabbababbababbabbababbabba...
{ "input": [ "6 5\na\nb\nab\nba\naba\n", "2 14\na\nb\naa\nab\nba\nbb\naaa\naab\naba\nabb\nbaa\nbab\nbba\nbbb\n", "1 10\nb\na\nb\na\nb\na\nb\na\nb\na\n", "10 10\nbbb\nabb\nbab\naab\nbba\naba\nbaa\naaa\nbbb\nabb\n", "50 100\nb\naab\nab\naaa\na\naab\naaa\nbb\na\na\nb\naab\nbbb\naa\nbbb\nb\nab\nab\nbb...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Fibonacci strings are defined as follows: * f1 = «a» * f2 = «b» * fn = fn - 1 fn - 2, n > 2 Thus, the first five Fibonacci strings are: "a", "b", "ba", "bab", "babba". Y...
19_A. World Football Cup_4021
Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the decision to change some World Cup regulations: * the final tournament features n teams (n is always even) * the first n / 2 teams ...
import sys # from itertools import product import logging logging.root.setLevel(level=logging.INFO) team_size = int(sys.stdin.readline()) teams = {} for _ in range(team_size): name = sys.stdin.readline().strip() teams[name] = { "point":0, "delta":0, "score":0 } contest_size = team_...
{ "input": [ "4\nA\nB\nC\nD\nA-B 1:1\nA-C 2:2\nA-D 1:0\nB-C 1:0\nB-D 0:3\nC-D 0:3\n", "2\na\nA\na-A 2:1\n", "2\nEULEUbCmfrmqxtzvg\nuHGRmKUhDcxcfqyruwzen\nuHGRmKUhDcxcfqyruwzen-EULEUbCmfrmqxtzvg 13:92\n", "4\nTeMnHVvWKpwlpubwyhzqvc\nAWJwc\nbhbxErlydiwtoxy\nEVASMeLpfqwjkke\nAWJwc-TeMnHVvWKpwlpubwyhzqvc ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Everyone knows that 2010 FIFA World Cup is being held in South Africa now. By the decision of BFA (Berland's Football Association) next World Cup will be held in Berland. BFA took the...
223_B. Two Strings_4025
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < k|x| ≤ |s|). You've got two strings — s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at...
import bisect import string s = input() t = input() max_match = [0 for i in range(len(s))] min_match = [0 for i in range(len(s))] char_idx = [0 for i in range(30)] char_occur = [ [] for i in range(30) ] for (i, ch) in enumerate(t): idx = ord(ch) - ord('a') char_occur[idx].append(i) for ch in string.ascii_lo...
{ "input": [ "abc\nba\n", "abacaba\naba\n", "abab\nab\n", "abcdadbcd\nabcd\n", "cctckkhatkgrhktihcgififfgfctctkrgiakrifazzggfzczfkkahhafhcfgacccfakkarcatkfiktczkficahgiriakccfiztkhkgrfkrimgamighhtamrhxftaadwxgfggytwjccgkdpyyatctfdygxggkyycpjyfxyfdwtgytcacawjddjdctyfgddkfkypyxftxxtaddcxxpgfgxgdfggf...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 ≤ k1 < k2 < ... < k|x| ≤ |s|). You've got two strings ...
272_A. Dima and Friends_4032
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and t...
def solve(n,d): other = 0 for i in d: other += i ways = 0 for i in range(1,6): if (other + i) % (n + 1) != 1: ways += 1 print(ways) def main(): n = int(input()) d = input() d = [int(i) for i in d.split()] solve(n,d) main()
{ "input": [ "1\n2\n", "2\n3 5\n", "1\n1\n", "49\n1 4 4 3 5 2 2 1 5 1 2 1 2 5 1 4 1 4 5 2 4 5 3 5 2 4 2 1 3 4 2 1 4 2 1 1 3 3 2 3 5 4 3 4 2 4 1 4 1\n", "1\n5\n", "25\n4 5 5 5 3 1 1 4 4 4 3 5 4 4 1 4 4 1 2 4 2 5 4 5 3\n", "100\n4 4 2 5 4 2 2 3 4 4 3 2 3 3 1 3 4 3 3 4 1 3 1 4 5 3 4 3 1 1 1 3...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place. To...
295_C. Greg and Friends_4036
One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided to get across the river. Luckily, there was a boat by the river bank, just where the guys were standing. We know that the boat can hold pe...
from collections import deque n, k = [int(i) for i in input().split()] a = [int(i) for i in input().split()] c50 = sum([1 for i in a if i == 50]) c100 = sum([1 for i in a if i == 100]) c = [[0] * 51 for i in range(51)] c[0][0] = 1 c[1][0] = 1 c[1][1] = 1 for x in range(2, 51): for y in range(x + 1): c[x][y...
{ "input": [ "3 100\n50 50 100\n", "1 50\n50\n", "2 50\n50 50\n", "5 188\n50 50 50 50 50\n", "33 123\n50 100 100 100 50 100 50 50 50 50 50 100 100 50 100 50 100 50 50 50 50 50 50 50 100 100 50 50 100 100 100 100 100\n", "5 258\n100 100 50 50 50\n", "32 121\n100 100 100 100 100 50 100 100 5...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: One day Greg and his friends were walking in the forest. Overall there were n people walking, including Greg. Soon he found himself in front of a river. The guys immediately decided t...
319_A. Malek Dance Club_4040
As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidentally Natalia Fan Club also has 2n members. Each member of MDC is assigned a unique id i from 0 to 2n - 1. The same holds for each member o...
s = input() res = pow(2, len(s)-1)*(int(s, 2)) print (res%1000000007)
{ "input": [ "1\n", "01\n", "11\n", "0111001111110010000001111100110100111110001100100001111111110000010010111010010010010111000110001111\n", "10110\n", "0110011110111000001101001010101000011011101001001101000000111101010101111101010011101001111010111001\n", "100111100\n", "0\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As a tradition, every year before IOI all the members of Natalia Fan Club are invited to Malek Dance Club to have a fun night together. Malek Dance Club has 2n members and coincidenta...
343_A. Rational Resistance_4044
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance R0 = 1. Elements with other resistance can be constructed from these resistors. In this problem, we will co...
I = lambda : list(map(int, input().split(' '))) a, b = I() ans = 0 while a > 0 and b > 0 and a//b > 0 or b//a > 0: ans += a//b a, b = b, a%b print(ans)
{ "input": [ "3 2\n", "1 1\n", "199 200\n", "4052739537881 6557470319842\n", "1 923438\n", "1 2\n", "1 3\n", "288565475053 662099878640\n", "18 55\n", "29906716 35911991\n", "10000000000 1000000001\n", "60236007668635342 110624799949034113\n", "4 43470202936783249\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical r...
366_C. Dima and Salad_4048
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have n fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit sala...
from bisect import bisect_right n, k = map(int, input().split()) t = sorted((u - k * v, v) for u, v in zip(*(map(int, input().split()), map(int, input().split())))) m = n - bisect_right(t, (0, 0)) l, p, t = 0, [0] * 100001, t[:: -1] for d, v in t[: m]: for j in range(l, 0, -1): if p[j]: p[j + d] = max(p[j +...
{ "input": [ "5 3\n4 4 4 4 4\n2 2 2 2 2\n", "3 2\n10 8 1\n2 7 1\n", "1 1\n1\n2\n", "19 2\n68 24 95 24 94 82 37 87 68 67 59 28 68 5 70 53 80 46 61\n60 74 46 9 40 45 58 51 96 4 42 33 12 40 34 9 58 84 91\n", "2 1\n75 65\n16 60\n", "21 8\n50 39 28 27 58 46 95 46 50 8 28 94 61 58 57 7 1 38 9 34 12\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Ser...
38_B. Chess_4052
Two chess pieces, a rook and a knight, stand on a standard chessboard 8 × 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the other one. Your task is to find the number of ways to place another knight on the board so that none of the three pieces on the board bea...
l=[] for i in range(1,9): for j in range(1,9): l.append(int(str(i)+str(j))) l2=[] def check(l2,c,d): if c+1<=8 and d+2<=8: l2.append(int(str(c+1)+str(d+2))) if c+2<9 and d+1<9: l2.append(int(str(c+2)+str(d+1))) if c-1>0 and d-2>0: l2.append(int(str(c-1)+str(d-2))) i...
{ "input": [ "a1\nb2\n", "a8\nd4\n", "c1\nd2\n", "g1\ne6\n", "h8\ng2\n", "a8\nf1\n", "e4\nc1\n", "b6\ng7\n", "a4\nd1\n", "h6\na1\n", "e3\ng5\n", "g2\nb7\n", "b2\ne1\n", "e1\nd6\n", "e8\nb6\n", "b6\nd8\n", "a1\nb7\n", "e6\nf2\n", "g8\nb7\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two chess pieces, a rook and a knight, stand on a standard chessboard 8 × 8 in size. The positions in which they are situated are known. It is guaranteed that none of them beats the o...
40_A. Find Color_4056
Not so long ago as a result of combat operations the main Berland place of interest — the magic clock — was damaged. The cannon's balls made several holes in the clock, that's why the residents are concerned about the repair. The magic clock can be represented as an infinite Cartesian plane, where the origin correspond...
# _ ##################################################################################################################### from math import sqrt, ceil def colorOfDamagedArea(x, y): location = sqrt(x*x+y*y) area_sBorder = ceil(location) if location == area_sBorder: return 'black' area_sAddress...
{ "input": [ "-2 1\n", "4 3\n", "2 1\n", "-12 -35\n", "-215 -996\n", "-96 -556\n", "-1000 0\n", "20 -21\n", "0 0\n", "207 -224\n", "-668 970\n", "-496 -644\n", "253 -204\n", "-129 489\n", "15 -8\n", "-589 952\n", "0 2\n", "-216 -90\n", "-72 -...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Not so long ago as a result of combat operations the main Berland place of interest — the magic clock — was damaged. The cannon's balls made several holes in the clock, that's why the...
483_A. Counterexample _4064
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the...
s, e = [int(num) for num in input().split()] def GCD(a, b): return GCD(b, a % b) if b else a result = -1 for i in range(s, e + 1): for j in range(i + 1, e + 1): if GCD(i, j) == 1: for k in range(j + 1, e + 1): if GCD(j, k) == 1 and GCD(i, k) != 1: print...
{ "input": [ "900000000000000009 900000000000000029\n", "2 4\n", "10 11\n", "998169857863230068 998169857863230083\n", "996517375802030516 996517375802030518\n", "267367244641009850 267367244641009899\n", "648583102513043 648583102513053\n", "268193483524125978 268193483524125979\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often...
507_A. Amr and Music_4068
Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea. Amr has n instruments, it takes ai days to learn i-th instrument. Being busy, Amr dedicated k days to learn how to play the maximum possible number of instruments. Amr asked for your help ...
I = lambda: map(int, input().split()) n, k = I() l = sorted(zip(I(), range(1, n+1))) h = [] for i, j in l: k -= i if k>=0: h.append(j) print(len(h));print(*h)
{ "input": [ "5 6\n4 3 1 1 2\n", "1 3\n4\n", "4 10\n4 3 1 2\n", "2 100\n100 100\n", "5 49\n16 13 7 2 1\n", "99 10000\n42 88 21 63 59 38 23 100 86 37 57 86 11 22 19 89 6 19 15 64 18 77 83 29 14 26 80 73 8 51 14 19 9 98 81 96 47 77 22 19 86 71 91 61 84 8 80 28 6 25 33 95 96 21 57 92 96 57 31 88 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea. Amr has n instruments, it takes ai days to learn i-t...
556_B. Case of Fake Numbers_4072
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers f...
import functools as ft if __name__ == '__main__': n = int(input()) a = list(map(int, input().split())) b = [i for i in range(n)] for i in range(1, n + 1): a = [(a[j] + 1) % n if not j % 2 else (a[j] - 1) % n for j in range(n)] cnt = ft.reduce(lambda x, y: x + y, [a[j] == b[j] for j in ...
{ "input": [ "4\n0 2 3 1\n", "5\n4 2 1 4 3\n", "3\n1 0 0\n", "3\n0 1 1\n", "4\n0 1 2 2\n", "100\n79 74 22 11 73 70 33 50 9 81 17 14 23 44 4 90 20 22 19 94 66 80 70 42 22 82 49 42 36 7 90 91 80 33 26 52 6 77 30 94 99 6 46 84 96 40 89 2 88 65 80 93 5 60 25 15 32 26 68 85 62 74 69 55 84 0 85 91 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's...
626_C. Block Towers_4080
Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks. The students don’t want to use too many blocks, but they also want to be uniq...
n, m = map(int, input().split()) x = max(n*2, m*3) while x//2+x//3-x//6 < m+n: x += 1 print(x)
{ "input": [ "3 2\n", "5 0\n", "1 3\n", "4 2\n", "984327 24352\n", "918273 219\n", "651 420\n", "5 3\n", "6 4\n", "192056 131545\n", "108752 129872\n", "1285 877\n", "817544 553980\n", "500000 167000\n", "961920 647392\n", "1515 1415\n", "32 16\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blo...
650_A. Watchmen_4084
Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi). They need to arrange a plan, but there are some difficulties on their way. As you know, Doctor Manhattan consider...
import collections r=0;a,b,c=[collections.Counter() for _ in [0,0,0]] for _ in range(int(input())): x,y=map(int, input().split()) r+=a[x]+b[y]-c[(x,y)] a[x]+=1;b[y]+=1;c[(x,y)]+=1 print(r)
{ "input": [ "3\n1 1\n7 5\n1 5\n", "6\n0 0\n0 1\n0 2\n-1 1\n0 1\n1 1\n", "2\n1 0\n0 19990213\n", "10\n46 -55\n46 45\n46 45\n83 -55\n46 45\n83 -55\n46 45\n83 45\n83 45\n46 -55\n", "2\n2 1\n1 2\n", "3\n8911 7861\n-6888 7861\n8911 7861\n", "2\n0 1000000000\n1 -7\n", "2\n1000000000 0\n-7 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is loca...
675_D. Tree Construction_4088
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help. You are given a sequence a, consisting of n distinct integers, that is used to construct the binary search tree. Below is the formal d...
__author__ = "House" import bisect if __name__ == "__main__": n = int(input()) s = [int(i) for i in input().split()] f = [[s[0], 0]] outp = list() for i in range(1, n): now = [s[i], i] idx = bisect.bisect_left(f, now) ans = 0 if idx == 0: ans = f[0][0] ...
{ "input": [ "5\n4 2 3 1 6\n", "3\n1 2 3\n", "2\n2 1\n", "10\n991309218 517452607 870021923 978357992 136426010 10601767 302627526 883615372 163475700 600546765\n", "3\n1 3 2\n", "4\n2 1 3 4\n", "3\n2 1 3\n", "4\n3 4 2 1\n", "4\n1 2 3 4\n", "2\n1 2\n", "3\n3 1 2\n", "3\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help...
765_C. Table Tennis Game 2_4098
Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothing. Once one of the players scores exactly k points, the score is reset and a new set begins. Across all the sets Misha scored a points ...
""" Created by Henrikh Kantuni on 2/14/17 """ if __name__ == '__main__': k, a, b = [int(x) for x in input().split()] score_a = 0 if a >= k: score_a = a // k score_b = 0 if b >= k: score_b = b // k if score_a == 0 and score_b == 0: print(-1) else: if score_...
{ "input": [ "11 2 3\n", "11 11 5\n", "18 11 21\n", "6 5 7\n", "9 8 17\n", "6 7 4\n", "10 20 2\n", "11 8 23\n", "10 11 5\n", "11 15 2\n", "10 2 82\n", "4 0 2\n", "5 6 3\n", "10 1 11\n", "10 11 11\n", "5 11 1\n", "11 25 0\n", "11 10 19\n", "12...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each serve is won by one of the players, he receives one point and the loser receives nothi...
80_B. Depression_4104
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle... Everybody was upset. The beautiful ...
# # solving: # from: https://vjudge.net/contest/417235#problem/D def main(): inlist = input().split(":") hh, mm = int(inlist[0]), int(inlist[1]) deg_per_h = 360/12 deg_per_m = 360/60 print("{} {}".format(((hh+mm/60) * deg_per_h) % 360, (mm * deg_per_m) % 360)) main() ...
{ "input": [ "04:30\n", "08:17\n", "12:00\n", "17:48\n", "08:31\n", "23:01\n", "06:36\n", "10:54\n", "00:24\n", "12:30\n", "11:32\n", "23:58\n", "07:20\n", "19:55\n", "23:00\n", "00:01\n", "20:45\n", "23:45\n", "11:59\n", "20:12\n", "...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had v...
87_A. Trains_4113
Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware of the other one's existence. When Vasya has some free time, he goes to one of his girlfriends. He descends into the subway at some time,...
a,b=map(int,input().split()) if(a==b): print('Equal') exit() import math lcm=(a*b)//(math.gcd(a,b)) if(a<b): da=(lcm//a)-1 ma=lcm//b if(da>ma): print('Dasha') elif(da<ma): print('Masha') else: print('Equal') else: da=(lcm//a) ma=(lcm//b)-1 if(da>ma): ...
{ "input": [ "2 3\n", "3 7\n", "5 3\n", "999983 999979\n", "419 430\n", "51291 51292\n", "99087 99090\n", "906 912\n", "5036 5037\n", "9886 8671\n", "450766 610961\n", "999999 1000000\n", "99207 30728\n", "664690 630787\n", "999997 1000000\n", "638067 40...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya the programmer lives in the middle of the Programming subway branch. He has two girlfriends: Dasha and Masha, who live at the different ends of the branch, each one is unaware o...
903_C. Boxes Packing_4117
Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai. Mishka can put a box i into another box j if the following conditions are met: * i-th box is not put into another box; * j-th box doesn't contain any other boxes; * box i is smaller than box j (ai < aj). Mishka ...
from sys import stdin,stdout from collections import Counter def ai(): return list(map(int, stdin.readline().split())) def ei(): return map(int, stdin.readline().split()) def ip(): return int(stdin.readline().strip()) def op(ans): return stdout.write(str(ans) + '\n') t = ip() li = ai() x = max(li) c = Counter(li).val...
{ "input": [ "3\n1 2 3\n", "4\n4 2 4 3\n", "10\n58 58 58 58 58 58 58 58 58 58\n", "8\n1 2 3 4 5 6 7 8\n", "1\n2\n", "1\n131\n", "11\n1 1 1 1 1 1 1 1 1 1 1\n", "10\n86 89 89 86 86 89 86 86 89 89\n", "8\n1 1 1 1 1 1 1 1\n", "1\n1\n", "1\n5\n", "9\n1 1 1 1 1 1 1 1 1\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mishka has got n empty boxes. For every i (1 ≤ i ≤ n), i-th box is a cube with side length ai. Mishka can put a box i into another box j if the following conditions are met: * i-t...
954_E. Water Taps_4123
Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to ai ml per second (this amount may be a real number). The water delivered by i-th tap has temperature ti. If for every <image> you set i-th tap to deliver exactly xi ml of ...
n,t=map(int,input().strip().split(' ')) x= list(map(int,input().strip().split(' '))) temp= list(map(int,input().strip().split(' '))) for i in range(n): temp[i]-=t pos = [] neg = [] ans = 0 negsum = 0 possum = 0 for i in range(n): if(temp[i]<0): negsum+=temp[i]*x[i] neg.append([temp[i]*-1,x[i]]) elif(temp...
{ "input": [ "2 12\n1 3\n10 15\n", "2 100\n3 10\n50 150\n", "3 9\n5 5 30\n6 6 10\n", "1 1000000\n1000000\n1000000\n", "1 200\n1000000\n100\n", "1 1\n1000000\n1\n", "20 30\n70 97 14 31 83 22 83 56 19 87 59 7 7 89 24 82 34 40 6 24\n10 4 47 46 11 18 32 55 16 32 53 37 43 32 41 46 57 14 60 44\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Consider a system of n water taps all pouring water into the same container. The i-th water tap can be set to deliver any amount of water from 0 to ai ml per second (this amount may b...
980_D. Perfect Groups_4127
SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square. Each integer must be in exactly one gro...
#!/usr/bin/env python3 from math import sqrt primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 2...
{ "input": [ "2\n5 5\n", "1\n0\n", "5\n5 -4 2 1 8\n", "30\n1 2 0 2 1 0 0 1 2 0 1 2 3 0 4 1 0 0 0 3 2 0 1 0 1 0 5 0 6 0\n", "2\n2 0\n", "80\n8861 -8846 -3257 8263 -8045 4549 9626 -8599 5755 -3559 5813 -7411 9151 -1847 2441 4201 2381 4651 -6571 199 -6737 -6333 -9433 -4967 9041 -9319 6801 5813 -2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be div...
9_D. How many trees?_4131
In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still he managed to get some information from there. For example, he managed to learn that User launches games for pleasure — and then terribl...
n,h=map(int,input().split()) L=[] for i in range(n+1): g=[] for j in range(n+1): g.append(0) L.append(g) L[0][0]=1 for i in range(1,n+1): for j in range(1,n+1): sumu=0 for m in range(1,i+1): t1=L[m-1][j-1] tot=0 for k in range(0,j): ...
{ "input": [ "3 2\n", "3 3\n", "21 12\n", "23 21\n", "16 5\n", "14 11\n", "16 11\n", "8 5\n", "28 23\n", "7 7\n", "35 35\n", "23 17\n", "22 22\n", "32 27\n", "15 5\n", "7 4\n", "27 11\n", "24 20\n", "33 17\n", "10 10\n", "33 26\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In one very old text file there was written Great Wisdom. This Wisdom was so Great that nobody could decipher it, even Phong — the oldest among the inhabitants of Mainframe. But still...
p02547 AtCoder Beginner Contest 179 - Go to Jail_4144
Tak performed the following action N times: rolling two dice. The result of the i-th roll is D_{i,1} and D_{i,2}. Check if doublets occurred at least three times in a row. Specifically, check if there exists at lease one i such that D_{i,1}=D_{i,2}, D_{i+1,1}=D_{i+1,2} and D_{i+2,1}=D_{i+2,2} hold. Constraints * 3 \...
n=int(input()) ans=0 flg=0 aa="No" for _ in range(n): a,b=map(int,input().split()) if a==b: ans+=1 else: ans=0 if ans==3: aa="Yes" print(aa)
{ "input": [ "5\n1 2\n6 6\n4 4\n3 3\n3 2", "5\n1 1\n2 2\n3 4\n5 5\n6 6", "6\n1 1\n2 2\n3 3\n4 4\n5 5\n6 6", "5\n1 2\n6 6\n4 4\n3 3\n3 3", "5\n1 2\n2 2\n3 4\n5 5\n6 6", "6\n1 1\n2 2\n3 3\n4 4\n5 5\n10 6", "5\n1 2\n6 6\n4 1\n3 3\n3 2", "5\n1 2\n2 2\n3 4\n9 5\n6 6", "6\n1 0\n2 2\n3 3\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Tak performed the following action N times: rolling two dice. The result of the i-th roll is D_{i,1} and D_{i,2}. Check if doublets occurred at least three times in a row. Specifical...
p02678 AtCoder Beginner Contest 168 - .. (Double Dots)_4148
There is a cave. The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside. It is dark in th...
from collections import deque N,M=map(int,input().split()) G=[ [] for _ in range(N) ] for i in range(M): A,B=map(int,input().split()) A-=1 B-=1 G[A].append(B) G[B].append(A) searched=[0]*N searched[0]=1 ans=[-1]*N d=deque() d.append(0) while(len(d)!=0): tmp=d.popleft() for g in G[tmp]: if searched[g]...
{ "input": [ "6 9\n3 4\n6 1\n2 4\n5 3\n4 6\n1 5\n6 2\n4 5\n5 6", "4 4\n1 2\n2 3\n3 4\n4 2", "4 4\n1 4\n2 3\n3 4\n4 2", "6 9\n3 4\n6 1\n2 4\n5 3\n4 6\n1 5\n6 2\n4 5\n5 3", "4 4\n1 4\n2 3\n4 4\n4 2", "6 9\n3 4\n6 1\n2 4\n5 3\n4 6\n1 5\n6 4\n4 5\n5 3", "6 9\n3 4\n6 1\n2 1\n5 3\n4 6\n1 5\n6 4\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a cave. The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. O...
p02806 Dwango Programming Contest 6th - Falling Asleep_4152
Niwango created a playlist of N songs. The title and the duration of the i-th song are s_i and t_i seconds, respectively. It is guaranteed that s_1,\ldots,s_N are all distinct. Niwango was doing some work while playing this playlist. (That is, all the songs were played once, in the order they appear in the playlist, w...
n = int(input()) slst = [] tlst = [] for _ in range(n): s,t = map(str,input().split()) slst.append(s) tlst.append(int(t)) x = input() print(sum(tlst[slst.index(x)+1:]))
{ "input": [ "15\nypnxn 279\nkgjgwx 464\nqquhuwq 327\nrxing 549\npmuduhznoaqu 832\ndagktgdarveusju 595\nwunfagppcoi 200\ndhavrncwfw 720\njpcmigg 658\nwrczqxycivdqn 639\nmcmkkbnjfeod 992\nhtqvkgkbhtytsz 130\ntwflegsjz 467\ndswxxrxuzzfhkp 989\nszfwtzfpnscgue 958\npmuduhznoaqu", "3\ndwango 2\nsixth 5\nprelims 25...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Niwango created a playlist of N songs. The title and the duration of the i-th song are s_i and t_i seconds, respectively. It is guaranteed that s_1,\ldots,s_N are all distinct. Niwan...
p02942 AtCoder Grand Contest 037 - Sorting a Grid_4155
We have a grid with N rows and M columns of squares. Each integer from 1 to NM is written in this grid once. The number written in the square at the i-th row from the top and the j-th column from the left is A_{ij}. You need to rearrange these numbers as follows: 1. First, for each of the N rows, rearrange the number...
from collections import defaultdict, deque N, M = map(int, input().split()) A = [[0] * M for _ in range(N)] B = [[0] * M for _ in range(N)] C = [[0] * M for _ in range(N)] G = defaultdict(lambda: defaultdict(int)) F = defaultdict(lambda: defaultdict(int)) rows = [0] * (N * M + 1) for i in range(N): A_list = list...
{ "input": [ "3 2\n2 6\n4 3\n1 5", "3 4\n1 4 7 10\n2 5 8 11\n3 6 9 12", "3 2\n2 5\n4 3\n1 5", "3 2\n2 6\n4 3\n2 5", "3 2\n1 6\n4 3\n2 5", "3 2\n1 6\n4 4\n2 5", "3 4\n1 4 7 10\n2 6 8 11\n3 6 9 12", "3 2\n2 5\n3 3\n1 5", "3 2\n2 6\n4 3\n2 6", "3 2\n1 6\n4 3\n2 6", "3 2\n1 6\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a grid with N rows and M columns of squares. Each integer from 1 to NM is written in this grid once. The number written in the square at the i-th row from the top and the j-th...
p03079 ExaWizards 2019 - Regular Triangle_4159
You are given three integers A, B and C. Determine if there exists an equilateral triangle whose sides have lengths A, B and C. Constraints * All values in input are integers. * 1 \leq A,B,C \leq 100 Input Input is given from Standard Input in the following format: A B C Output If there exists an equilateral ...
s = input().split(" ") if s[0] == s[1] == s[2]: print("Yes") else: print("No")
{ "input": [ "2 2 2", "3 4 5", "2 4 2", "0 0 0", "3 4 2", "2 6 2", "3 4 1", "2 7 2", "3 0 1", "1 7 2", "4 0 1", "0 7 2", "4 0 2", "0 13 2", "5 0 2", "1 13 2", "6 0 2", "1 13 3", "8 0 2", "1 15 3", "8 -1 2", "2 15 3", "15 -1 2"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given three integers A, B and C. Determine if there exists an equilateral triangle whose sides have lengths A, B and C. Constraints * All values in input are integers. * 1 ...
p03222 AtCoder Beginner Contest 113 - Number of Amidakuji_4163
Amidakuji is a traditional method of lottery in Japan. To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of each vertical line is H+1 [cm], and the endpoints of the horizontal lines must be at 1, 2, 3, ..., or H [cm] from the top of a vertical l...
H,W,K = map(int,input().split()) dp = [] for i in range(H+1): L = [0]*W dp.append(L) fb = [1,1] for i in range(W-2): fb.append(fb[i]+fb[i+1]) dp[0][0] = 1 if W != 1: for i in range(1,H+1): for j in range(W): if 1 <= j <= W-2: dp[i][j] = dp[i-1][j-1]*fb[j-1]*fb[W-j-1]+dp[i-1][j]*fb[j]*fb[W-j-1]...
{ "input": [ "2 3 1", "1 3 2", "1 3 1", "7 1 1", "2 3 3", "15 8 5", "4 3 1", "1 2 2", "1 5 1", "16 8 5", "4 6 1", "1 9 1", "16 8 3", "8 6 1", "1 9 2", "15 8 3", "9 6 1", "0 9 2", "2 6 1", "2 7 1", "1 7 1", "2 7 2", "2 6 2", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Amidakuji is a traditional method of lottery in Japan. To make an amidakuji, we first draw W parallel vertical lines, and then draw horizontal lines that connect them. The length of ...
p03370 AtCoder Beginner Contest 095 - Bitter Alchemy_4167
Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams o...
N,X = map(int, input().split()) M = [int(input()) for _ in range(N)] X = X - sum(M) print(N + X//min(M))
{ "input": [ "3 1000\n120\n100\n140", "5 3000\n150\n130\n150\n130\n110", "4 360\n90\n90\n90\n90", "3 1001\n120\n100\n140", "5 3000\n150\n130\n150\n130\n100", "3 1001\n120\n110\n140", "4 360\n90\n13\n126\n90", "4 293\n90\n13\n126\n90", "4 293\n144\n13\n126\n90", "4 293\n144\n13\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These do...
p03693 AtCoder Beginner Contest 064 - RGB Cards_4172
AtCoDeer has three cards, one red, one green and one blue. An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card. We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer. Is this integer...
print("YES" if int(input()[0::2])%4==0 else "NO")
{ "input": [ "4 3 2", "2 3 4", "7 3 2", "2 3 3", "7 3 0", "2 6 3", "7 6 0", "2 6 0", "9 6 0", "0 6 0", "5 6 0", "0 1 0", "8 6 0", "0 0 0", "4 3 3", "2 0 4", "1 3 3", "0 3 0", "2 6 1", "1 6 0", "0 2 0", "8 6 1", "1 0 0", "5...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: AtCoDeer has three cards, one red, one green and one blue. An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue car...
p03846 AtCoder Beginner Contest 050 - Lining Up_4176
There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person remembered the following fact: the absolute difference of the number of the people who were standing to the left of that person, and the nu...
n=int(input()) A=list(map(int,input().split())) cnt=[abs(i-(n-i-1)) for i in range(n)] if sorted(A)==sorted(cnt): print((2**(n//2))%(10**9+7)) else: print(0)
{ "input": [ "8\n7 5 1 1 7 3 5 3", "7\n6 4 0 2 4 0 2", "5\n2 4 4 0 2", "8\n7 5 1 2 7 3 5 3", "7\n6 4 0 2 4 0 4", "5\n0 4 4 0 2", "7\n6 4 0 2 4 -1 4", "5\n0 4 4 0 1", "7\n6 4 0 2 8 -1 4", "5\n0 4 1 0 1", "7\n6 4 0 2 8 0 4", "5\n0 4 0 0 1", "7\n6 4 1 2 8 0 4", "5\...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N people, conveniently numbered 1 through N. They were standing in a row yesterday, but now they are unsure of the order in which they were standing. However, each person re...
p04014 AtCoder Beginner Contest 044 - Digit Sum_4180
For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows: * f(b,n) = n, when n < b * f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b, and n \ {\rm mod} \ b denotes the remainder of n div...
n=int(input()) s=int(input()) b_ans=1 if n==s: b_ans=n+1 if b_ans==1: for b in range(2,int(n**0.5)+1): nn=n st=[] while(nn>0): st.append(nn%b) nn=(nn-st[-1])/b if sum(st)==s: b_ans=b break if b_ans==1: for p in range(int(n**0.5),0,-1): b=(n-s+p)/p if b.is_i...
{ "input": [ "87654\n30", "87654\n138", "87654\n45678", "31415926535\n1", "1\n31415926535", "87654\n60", "87654\n178", "87654\n26288", "31415926535\n2", "1\n40483159044", "87654\n58", "87654\n92", "87654\n15577", "31415926535\n4", "87654\n114", "87654\n3...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows: * f(b,n) = n, when n < b * f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n...
p00099 Surf Smelt Fishing Contest II_4184
A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time. Create a program that reads the participant number and the number of fish caught or released in order as one event, and outputs the participant number and the number of animals that have acquired the most smel...
# AOJ 0099 Surf Smelt Fishing Contest II # Python3 2018.6.22 bal4u import heapq MAX = 1000000 SFT = 20 # 2**20 = 1048576 > MAX Q = [] # tbl[0:キー, 1:Magic] n, q = list(map(int, input().split())) tbl = [0]*(n+1) for i in range(q): id, v = list(map(int, input().split())) tbl[id] += v heapq.heappush(Q, ((-tbl[id] << S...
{ "input": [ "3 5\n1 4\n2 5\n1 3\n3 6\n2 7", "3 5\n1 4\n2 5\n1 3\n3 4\n2 7", "3 5\n1 4\n2 3\n1 3\n3 6\n2 7", "3 5\n1 4\n2 5\n1 3\n3 4\n2 11", "3 5\n1 4\n2 3\n1 3\n3 6\n2 1", "3 5\n1 4\n2 5\n1 5\n3 4\n2 11", "3 5\n1 4\n2 3\n2 3\n3 6\n2 1", "3 5\n1 4\n3 5\n1 5\n3 4\n2 11", "3 5\n1 4\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A smelt fishing tournament was held at Lake Hibara. It seems that catch and release is recommended this time. Create a program that reads the participant number and the number of fis...
p00231 Dangerous Bridge_4188
In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of passers-by, the bridge is so old that it is almost broken. <image> Yabashi is strong enough to withstand up to 150 [kg]. For example,...
# coding: utf-8 # Your code here! class Sch: def __init__(self, m, t): self.m = m self.t = t def __lt__(self,other): if self.t == other.t: return self.m < other.m else: return self.t < other.t while True: N = int(input()) if N =...
{ "input": [ "3\n80 0 30\n50 5 25\n90 27 50\n3\n80 0 30\n70 5 25\n71 30 50\n0", "3\n80 0 30\n50 5 25\n90 27 50\n3\n80 0 30\n70 5 25\n71 30 84\n0", "3\n80 0 15\n64 5 25\n149 27 50\n3\n80 0 10\n70 10 25\n22 30 84\n0", "3\n80 0 30\n50 5 25\n90 27 50\n3\n80 0 30\n70 5 25\n71 23 50\n0", "3\n1 0 30\n50 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In Aizuwakamatsu Village, which is located far north of Aizuwakamatsu City, a bridge called "Yabashi" is the only way to move to the surrounding villages. Despite the large number of ...
p00393 Beautiful Sequence_4191
Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists on...
MOD = 1000000007 N, M = (int(x) for x in input().split()) pow = [0] * (N + 1) dp = [0] * (N + 1) pow[0] = 1 for i in range(1, N + 1): pow[i] = pow[i - 1] * 2 pow[i] %= MOD dp[0] = 1 for i in range(1, M + 1): dp[i] = pow[i] dp[M] -= 1 for i in range(M + 1, N + 1): dp[i] = dp[i - 1] + (dp[i - 1] -...
{ "input": [ "4 3", "4 2", "6 3", "1 2", "6 4", "8 2", "6 1", "2 2", "6 5", "13 2", "10 1", "14 1", "5 2", "27 1", "4 1", "9 3", "10 4", "8 1", "13 4", "6 2", "16 1", "10 2", "13 1", "3 1", "15 4", "7 3", "12 6...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of ...
p00746 Pablo Squarson's Headache_4197
Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth. At the center of his studio, there is a huuuuuge table and beside it are many, many squares of the same size. Pablo Squarson puts one of the squ...
while True: N = int(input()) w=[0]*N h=[0]*N wx=[-1,0,1,0] hy=[0,-1,0,1] if N == 0: break for i in range(1,N): n,d = map(int,input().split()) w[i] = w[n]+wx[d] h[i] = h[n]+hy[d] print(max(w)-min(w)+1,max(h)-min(h)+1)
{ "input": [ "1\n5\n0 0\n0 1\n0 2\n0 3\n12\n0 0\n1 0\n2 0\n3 1\n4 1\n5 1\n6 2\n7 2\n8 2\n9 3\n10 3\n10\n0 2\n1 2\n2 2\n3 2\n2 1\n5 1\n6 1\n7 1\n8 1\n0", "1\n5\n0 0\n0 1\n0 2\n0 3\n12\n0 0\n1 1\n2 0\n3 1\n4 1\n5 1\n6 2\n7 2\n8 2\n9 3\n10 3\n10\n0 2\n1 2\n2 2\n3 2\n2 1\n5 1\n6 1\n7 1\n8 1\n0", "1\n5\n0 0\n0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Pablo Squarson is a well-known cubism artist. This year's theme for Pablo Squarson is "Squares". Today we are visiting his studio to see how his masterpieces are given birth. At the ...
p00885 Balloon Collecting_4201
"Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one after another, and the player manipulates a robot vehicle on the ground to capture the balloons. The player can control the vehicle to mov...
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 = 10**9+7 dd = [(0,-1),(1,0),(0,1),(-1,0)] ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)] def LI(): return [int(x) for x in sys.stdin....
{ "input": [ "2\n10 100\n100 270\n2\n10 100\n100 280\n3\n100 150\n10 360\n40 450\n3\n100 150\n10 360\n40 440\n2\n100 10\n50 200\n2\n100 100\n50 110\n1\n15 10\n4\n1 10\n2 20\n3 100\n90 200\n0", "2\n10 100\n100 270\n2\n10 100\n100 280\n3\n100 150\n10 360\n40 450\n3\n100 150\n9 360\n40 440\n2\n100 10\n50 200\n2\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: "Balloons should be captured efficiently", the game designer says. He is designing an oldfashioned game with two dimensional graphics. In the game, balloons fall onto the ground one a...
p01016 Password_4204
Taro had his own personal computer and set a password for login. However, Taro inadvertently forgot the password. Then, remembering that there was a piece of paper with the password written down, Taro found the paper and was surprised to see it. The paper was cut and there were only fragments, and there were some stain...
a = input() b = input() length_a = len(a) length_b = len(b) def check(): for i in range(length_a - length_b + 1): for j in range(length_b): if b[j] == "_" or a[i + j] == b[j]:continue else:break else: print("Yes") return print("No") return ...
{ "input": [ "KUSATSU\nKSATSU", "ABCABC\nACBA_B", "RUPCUAPC\n__PC", "AIZU\n_A", "ABCDE\nABC", "KUSATSU\nLSATSU", "RUPCUAOC\n__PC", "ABC@BC\nACBA_B", "AI[U\n_A", "ABCDE\nCBA", "KUSATSU\nLSATRU", "CB@CBA\nACBA_B", "RUPCUAOC\n__OC", "U[IA\n_A", "AACDE\nCBA", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Taro had his own personal computer and set a password for login. However, Taro inadvertently forgot the password. Then, remembering that there was a piece of paper with the password w...
p01288 Marked Ancestor_4210
You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark) Mark node v. * Q v: (Query) Print the index of the nearest marked ancestor of node v which is nearest to it. Initially, only the root no...
def root(x): while P[x] != x: x = P[x] return x def mark(x): P[x-1] = x-1 def query(x): v = root(x-1) +1 return v while True: N,Q = map(int,input().strip().split(" ")) if N == Q == 0: break P = [] P.append(0) for i in range(N-1): p_i = int(input().strip()...
{ "input": [ "6 3\n1\n1\n2\n3\n3\nQ 5\nM 3\nQ 5\n0 0", "6 3\n2\n1\n2\n3\n3\nQ 5\nM 3\nQ 5\n0 0", "6 3\n1\n1\n2\n3\n3\nQ 5\nM 4\nQ 5\n0 0", "6 3\n0\n1\n2\n3\n3\nQ 5\nM 5\nQ 5\n0 0", "6 3\n0\n1\n2\n3\n1\nQ 5\nM 4\nQ 4\n0 0", "6 3\n1\n2\n2\n6\n3\nQ 1\nM 2\nQ 5\n0 0", "6 3\n1\n1\n3\n3\n3\nQ 5\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a tree T that consists of N nodes. Each node is numbered from 1 to N, and node 1 is always the root node of T. Consider the following two operations on T: * M v: (Mark)...
p01457 Carpenters' Language_4214
International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a...
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": [ "3\n0 ( 10\n10 ) 20\n0 ( 10", "3\n0 ( 10\n10 ) 5\n10 ) 5", "3\n0 ) 10\n10 ( 5\n10 ( 5", "3\n0 ( 10\n10 ) 5\n10 ( 5", "3\n-1 ( 10\n10 ) 5\n10 ) 5", "3\n-1 ( 9\n10 ) 9\n20 ( 4", "3\n-1 ) 0\n8 ( 19\n4 ' 2", "3\n-2 ) 0\n7 ) 0\n4 ( 11", "3\n-1 ( 0\n10 ) 4\n20 ( 4", "3\n...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syn...
p01769 Hopping Hearts_4219
Problem statement N-winged rabbit is on a balance beam of length L-1. The initial position of the i-th rabbit is the integer x_i, which satisfies 0 ≤ x_ {i} \ lt x_ {i + 1} ≤ L−1. The coordinates increase as you move to the right. Any i-th rabbit can jump to the right (ie, move from x_i to x_i + a_i) any number of tim...
def main(): MOD = 1000000007 n, l = map(int, input().split()) xlst = map(int, input().split()) alst = map(int, input().split()) can_use = [] for x, a in zip(xlst, alst): if a == 0:s = {x} else:s = {k for k in range(x, l, a)} can_use.append(s) dp = [[0] * l for _ in range(n)] for j in rang...
{ "input": [ "1 3\n0\n1", "1 6\n0\n1", "2 3\n0\n1", "1 6\n1\n1", "2 3\n0\n0", "1 3\n1\n1", "1 11\n0\n1", "1 15\n0\n1", "1 5\n2\n1", "1 8\n0\n1", "1 13\n0\n1", "1 8\n1\n1", "1 11\n1\n1", "1 15\n1\n1", "1 5\n1\n1", "1 9\n0\n1", "1 28\n1\n1", "1 13\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem statement N-winged rabbit is on a balance beam of length L-1. The initial position of the i-th rabbit is the integer x_i, which satisfies 0 ≤ x_ {i} \ lt x_ {i + 1} ≤ L−1. Th...
p02041 LISum_4223
problem Given the sequence $ A $ of length $ N $. Find the maximum value of $ \ sum B_i $, where $ B $ is one of the longest increasing subsequences of the sequence $ A $. The longest increasing subsequence of the sequence $ A $ is the longest subsequence that satisfies $ A_i <A_j $ with all $ i <j $. output Outp...
import bisect N = int(input()) A = list(map(int,input().split())) lis = [] hist = [] for a in A: i = bisect.bisect_left(lis,a) if i == len(lis): lis.append(a) hist.append([a]) else: lis[i] = a hist[i].append(a) ans = last = hist[-1][0] for row in reversed(hist[:-1]): i...
{ "input": [ "4\n6 4 7 8", "4\n7 4 7 8", "4\n7 4 1 8", "4\n7 4 2 9", "4\n7 4 2 6", "4\n7 4 0 10", "4\n7 8 0 10", "4\n14 8 0 10", "4\n14 15 0 10", "4\n3 9 0 10", "4\n6 4 3 8", "4\n7 5 7 8", "4\n6 4 3 3", "4\n7 8 2 15", "4\n13 4 3 9", "4\n7 4 0 16", "4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem Given the sequence $ A $ of length $ N $. Find the maximum value of $ \ sum B_i $, where $ B $ is one of the longest increasing subsequences of the sequence $ A $. The longe...
p02184 Canisar Cipher_4225
C: Canisal cryptography problem Ebi-chan was given the string C obtained by encrypting a non-negative integer D with "canisal cipher". This cipher replaces each number in decimal notation with a fixed number (not necessarily different from the original). Different numbers will not be replaced with the same number, an...
# 半分全列挙 from itertools import permutations, combinations M = int(input()) C = list(map(int, input())) if len(C)==1 and M==0: print(0) exit() mod = 10**9 + 7 L = [0] * 10 b = 1 for c in C[::-1]: L[c] += b b = b * 10 % mod for t_half1 in combinations(range(10), 5): L1, L2 = L[:5], L[5:] t_half2 =...
{ "input": [ "2\n1000000007", "2\n1036219253", "2\n8", "2\n1612950356", "2\n2998753809", "2\n1978754927", "2\n2655259302", "2\n3155611415", "2\n1020631181", "2\n120033745", "2\n37469551", "2\n8809352", "2\n6981477", "2\n4234377", "2\n1585800", "2\n293024...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: C: Canisal cryptography problem Ebi-chan was given the string C obtained by encrypting a non-negative integer D with "canisal cipher". This cipher replaces each number in decimal no...
p02326 Largest Square_4228
Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W c2,1 c2,2 ... c2,W : cH,1 cH,2 ... cH,W In the first line, two integers H and W separated by a space character are given. In the follo...
h,w=map(int,input().split()) grid=[list(map(int,input().split())) for i in range(h)] def Largest_Square(grid): h=len(grid);w=len(grid[0]) dp=[[0]*w for i in range(h)] maxlenth=0 for i in range(h): for j in range(w): dp[i][j]=(grid[i][j]+1)%2 maxlenth=max(maxlenth,dp[i][j...
{ "input": [ "4 5\n0 0 1 0 0\n1 0 0 0 0\n0 0 0 1 0\n0 0 0 1 0", "4 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 0 0 1 0", "1 5\n1 0 1 0 0\n1 0 0 0 0\n0 0 0 1 -1\n0 -1 0 1 0", "1 1\n1 0 0 0 0\n1 0 0 0 1\n0 -1 0 1 -1\n0 -1 0 2 0", "4 5\n0 0 1 0 0\n1 0 0 0 1\n0 0 0 1 0\n0 -1 0 1 0", "4 5\n0 0 1 0 0\n1 0...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix which only contains 0s. Constraints * 1 ≤ H, W ≤ 1,400 Input H W c1,1 c1,2 ... c1,W...