instruction
stringlengths
29
13k
input
dict
output
stringlengths
10
49.8k
There is an evil creature in a square on N-by-M grid (2 \leq N, M \leq 100), and you want to kill it using a laser generator located in a different square. Since the location and direction of the laser generator are fixed, you may need to use several mirrors to reflect laser beams. There are some obstacles on the grid ...
{ "input": [ "3 3 1\nS#.\n...\n.#G", "3 3 1\nS#G\n...\n.#.", "3 3 2\nS#.\n...\n.#G", "4 3 2\nS..\n...\n..#\n.#G", "1 3 1\nS#G\n...\n.#.", "3 3 2\nS#.\n...\n/#G", "4 3 2\nS..\n...\n..#\nG#.", "3 3 2\nS#.\n...\n.$G", "1 3 2\nS#G\n...\n.#.", "5 3 2\nS#.\n...\n/#G", "3 3 0\nS#....
from collections import deque N, M, A = map(int, input().split()) MP = [[0]*M for i in range(N)] sx = sy = gx = gy = -1 for i in range(N): for j, c in enumerate(input()): if c == '#': MP[i][j] = 1 elif c == 'S': sx = j; sy = i elif c == 'G': gx = j; gy = i...
problem AOR Ika and you came to the tournament-style table tennis tournament singles section for reconnaissance. For AOR Ika-chan, who wants to record all the games, you decide to ask for the number of games that will be played in this tournament. There are $ N $ players in the tournament, each with a uniform number ...
{ "input": [ "2 0", "2 1", "2 2", "2 4", "0 4", "-1 4", "-1 0", "1 -1", "-1 2", "-1 -4", "-2 -7", "-1 -7", "-1 -5", "1 -6", "2 -6", "2 -8", "5 -10", "5 -8", "9 -8", "9 -14", "9 -25", "9 -20", "18 -20", "18 -36", "18 -3...
N,M = map(int, input().split()) l = [] for i in range(M): l.append(int(input())) count = N - 1 - M print(count)
A histogram is made of a number of contiguous bars, which have same width. For a given histogram with $N$ bars which have a width of 1 and a height of $h_i$ = $h_1, h_2, ... , h_N$ respectively, find the area of the largest rectangular area. Constraints * $1 \leq N \leq 10^5$ * $0 \leq h_i \leq 10^9$ Input The inp...
{ "input": [ "8\n2 1 3 5 3 4 2 1", "3\n2 0 1", "8\n2 1 3 5 3 7 2 1", "3\n3 0 1", "8\n2 1 3 5 3 7 3 1", "3\n5 0 0", "3\n0 0 0", "8\n2 2 3 5 4 7 3 2", "3\n0 0 1", "8\n3 6 3 6 4 7 0 4", "8\n3 6 4 6 4 7 0 4", "3\n2 0 2", "8\n2 1 3 2 4 7 3 1", "8\n2 2 2 5 4 7 3 1", ...
""" Writer:SPD_9X2 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_C&lang=ja ヒストグラム上の最大長方形 またdpかな…? 参考: http://algorithms.blog55.fc2.com/blog-entry-132.html """ #最後に要素0を追加してくこと def Largest_rectangle_in_histgram(lis): stk = [] ans = 0 N = len(lis) for i in range(N): if len(st...
Difference of Big Integers Given two integers $A$ and $B$, compute the difference, $A - B$. Input Two integers $A$ and $B$ separated by a space character are given in a line. Output Print the difference in a line. Constraints * $-1 \times 10^{100000} \leq A, B \leq 10^{100000}$ Sample Input 1 5 8 Sample O...
{ "input": [ "5 8", "5 4", "5 5", "5 1", "4 1", "2 0", "8 0", "15 0", "15 1", "8 1", "0 1", "0 2", "0 3", "-1 3", "-1 4", "-1 7", "-1 8", "0 16", "0 31", "0 45", "-1 45", "0 69", "0 55", "0 10", "2 16", "-2 4", ...
A, B = map(int, input().split()) print(A - B)
You received a notebook which is called Death Note. This notebook has infinite number of pages. A rule is written on the last page (huh) of this notebook. It says: "You have to write names in this notebook during n consecutive days. During the i-th day you have to write exactly a_i names.". You got scared (of course yo...
{ "input": [ "1 100\n99\n", "4 20\n10 9 19 2\n", "3 5\n3 7 9\n", "1 1\n2\n", "10 4\n9 5 6 4 3 9 5 1 10 7\n", "9 9\n1 7 1 8 4 7 9 8 8\n", "1 1\n1\n", "16 2\n999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 999999999 ...
(n,m) = tuple(map(int, input().split(" "))) x = list(map(int, input().split(" "))) nib = 0 ans = [] cou = 0; for i in x: nib+=i if (nib<m): ans.append(nib//m) else: ans.append(nib//m) nib-=(nib//m)*m print (*ans)
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called "Testing Pants for Sadness". The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to ...
{ "input": [ "2\n2 2\n", "2\n1 1\n", "1\n10\n", "100\n1 3 2 1 1 2 1 3 2 2 3 1 1 1 2 2 1 3 3 1 1 2 2 3 2 1 3 1 3 2 1 1 3 3 2 1 2 2 2 3 2 2 3 2 2 3 2 1 3 1 1 2 1 3 2 2 1 1 1 1 1 1 3 1 2 3 1 1 1 1 1 2 3 3 1 1 1 1 2 3 3 1 3 2 2 3 2 1 3 2 2 3 1 1 3 2 3 2 3 1\n", "1\n84355694\n", "3\n2 4 1\n", "...
import sys,math s = int(input()) arr = list(map(int,input().split(' '))) m = 0 for i in range(0,s): m+=(arr[i]-1)*(i+1)+1 print(m) ''' 2 2 2 1+1 2 1+1+1 3 (1+1+1,1) 4 3 3 1+1+1 3 1+1+1+1+1 5 1+1+1,1+1+1,1 7 3 2 4 1 1+1 1+1+1+1+1+1+1 1 1+1+1+1 '''
JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an integer n, you can perform the following operations zero or more times: * mul x: multiplies n by x (where x is an arbitrary positive integer). * sqrt: replaces n with √...
{ "input": [ "20\n", "5184\n", "2\n", "328509\n", "279936\n", "341\n", "605000\n", "11\n", "4\n", "786432\n", "982081\n", "52488\n", "6444\n", "12\n", "1105\n", "49\n", "252601\n", "1000000\n", "162000\n", "29341\n", "1729\n", "30...
import math def sieve(n): mark = [0]*(n+1) prime = [] for i in range(3, n+1, 2): if not mark[i]: for j in range(3*i, n+1, i+i): mark[j] = 1 prime.append(2) for i in range(3, n+1, 2): if not mark[i]: prime.append(i) return prime def isp...
The Fair Nut lives in n story house. a_i people live on the i-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening. It was decided ...
{ "input": [ "2\n1 1\n", "3\n0 2 1\n", "5\n6 1 1 8 3\n", "5\n4 9 4 2 6\n", "3\n1 2 2\n", "100\n23 94 2 59 41 51 92 74 92 76 37 98 76 47 60 4 22 32 22 32 57 39 68 60 38 41 61 7 34 98 42 44 52 100 81 24 16 51 10 84 34 52 73 100 69 38 14 77 32 4 59 37 68 81 6 37 52 6 96 22 12 23 63 57 59 18 20 1 ...
n = int(input()) arr = list(map(int, input().split())) cur = 0 ans = 100000000 for x in range(n): cur = 0 for i in range(n): summ = 0 summ += abs(x - i) summ += i summ += x summ += x summ += i summ += abs(x - i) summ *= arr[i] cur += summ ...
Cowboy Vlad has a birthday today! There are n children who came to the celebration. In order to greet Vlad, the children decided to form a circle around him. Among the children who came, there are both tall and low, so if they stand in a circle arbitrarily, it may turn out, that there is a tall and low child standing n...
{ "input": [ "3\n30 10 20\n", "5\n2 1 1 3 2\n", "5\n7318577 1728333 8514304 9971719 9004162\n", "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70...
n = int(input()) arr = [int(i) for i in input().split()] arr1, arr2 = [], [] arr.sort(reverse = True) for i in range(n): if i%2==0: arr1.append(arr[i]) else: arr2.append(arr[i]) arr2.reverse() for i in arr2: arr1.append(i) print(*arr1)
The Kingdom of Kremland is a tree (a connected undirected graph without cycles) consisting of n vertices. Each vertex i has its own value a_i. All vertices are connected in series by edges. Formally, for every 1 ≤ i < n there is an edge between the vertices of i and i+1. Denote the function f(l, r), which takes two in...
{ "input": [ "3\n2 1 3\n", "10\n1 5 2 5 5 3 10 6 5 1\n", "4\n2 1 1 3\n", "9\n1 9 1 1 4 5 9 9 1\n", "44\n42 44 4 24 29 17 32 30 30 9 28 9 22 16 20 41 17 19 41 23 28 19 38 44 1 34 41 34 19 36 32 37 22 31 4 3 27 23 21 21 26 39 20 8\n", "40\n35 33 20 13 21 4 16 21 22 35 20 39 32 29 27 5 24 3 12 10...
n = int(input()) + 1 res = 0 a = tuple(map(int, input().split())) for ai in a: res += ai * (n - ai) for ai, aj in map(sorted, zip(a, a[1:])): res -= ai * (n - aj) print(res) #
Nauuo is a girl who loves drawing circles. One day she has drawn a circle and wanted to draw a tree on it. The tree is a connected undirected graph consisting of n nodes and n-1 edges. The nodes are numbered from 1 to n. Nauuo wants to draw a tree on the circle, the nodes of the tree should be in n distinct points o...
{ "input": [ "4\n1 2\n1 3\n1 4\n", "4\n1 2\n1 3\n2 4\n", "8\n4 5\n1 2\n6 3\n2 3\n2 8\n4 7\n2 4\n", "7\n2 7\n2 6\n4 7\n7 3\n7 5\n1 7\n", "3\n1 2\n3 2\n", "2\n2 1\n", "10\n5 4\n5 2\n3 7\n9 3\n3 2\n3 1\n3 8\n9 10\n1 6\n", "6\n2 1\n3 2\n4 1\n5 4\n1 6\n", "5\n3 5\n4 3\n2 4\n1 2\n", ...
import sys n = int(sys.stdin.readline().strip()) D = [0] * n p = 998244353 for i in range (0, n - 1): u, v = list(map(int, sys.stdin.readline().strip().split())) D[u-1] = D[u-1] + 1 D[v-1] = D[v-1] + 1 ans = n for i in range (0, n): for j in range (0, D[i]): ans = (ans * (j + 1)) % p print(an...
Tokitsukaze is one of the characters in the game "Kantai Collection". In this game, every character has a common attribute — health points, shortened to HP. In general, different values of HP are grouped into 4 categories: * Category A if HP is in the form of (4 n + 1), that is, when divided by 4, the remainder is ...
{ "input": [ "33\n", "98\n", "85\n", "99\n", "50\n", "100\n", "59\n", "60\n", "95\n", "93\n", "91\n", "30\n", "88\n", "83\n", "32\n", "52\n", "43\n", "67\n", "42\n", "36\n", "76\n", "77\n", "35\n", "6\n", "28\n", "...
n=int(input()) d={} d[0]='1 A' d[1]='0 A' d[2]='1 B' d[3]='2 A' x=n%4 print(d[x])
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of d...
{ "input": [ "4 2\n4478\n", "7 4\n4727447\n", "7 74\n4777774\n", "3 99\n447\n", "47 7\n77774477747474477477477774747747447447774777474\n", "74 1000000000\n77474447774774747474777447474777777477474444477747444777447444474744744444\n", "10 200\n6860544593\n", "100 1000000000\n58493474544...
n, k = map(int, input().split()) s = list(input()) cnt = 0 for i in range(n - 1): if cnt == k: break if s[i] == '4' and s[i + 1] == '7': if i & 1: if s[i - 1] == '4': if (cnt - k) & 1: s[i] = '7' print(''.join(s)) ex...
You are given a huge integer a consisting of n digits (n is between 1 and 3 ⋅ 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032...
{ "input": [ "3\n0709\n1337\n246432\n", "1\n1003\n", "1\n890\n", "3\n477\n1337\n246432\n", "1\n454\n", "3\n477\n1216\n246432\n", "1\n638\n", "3\n716\n1216\n246432\n", "1\n540\n", "3\n716\n1336\n246432\n", "1\n119\n", "3\n469\n1336\n246432\n", "1\n176\n", "3\n469...
# -*- coding: utf-8 -*- """ Created on Fri Nov 1 14:39:37 2019 @author: uditg """ ans={} test=int(input()) for each in range(test): num=input() n=len(num) num={i:int(num[i]) for i in range(n)} output={} even={} odd={} even_count=0 odd_count=0 for i in range(n): if num[i]%2=...
A new delivery of clothing has arrived today to the clothing store. This delivery consists of a ties, b scarves, c vests and d jackets. The store does not sell single clothing items — instead, it sells suits of two types: * a suit of the first type consists of one tie and one jacket; * a suit of the second type ...
{ "input": [ "4\n5\n6\n3\n1\n2\n", "17\n14\n5\n21\n15\n17\n", "12\n11\n13\n20\n4\n6\n", "88\n476\n735\n980\n731\n404\n", "844\n909\n790\n209\n809\n949\n", "406\n847\n512\n65\n86\n990\n", "91\n75\n768\n322\n530\n291\n", "58967\n2953\n35483\n681\n780\n304\n", "58253\n17658\n9101\n949...
a = int(input()) b = int(input()) c = int(input()) d = int(input()) c1 = int(input()) c2 = int(input()) p = 0 if c2 > c1: m = min(b,c,d) b -= m c -= m d -= m p += m * c2 if d > 0: p += (min(a,d) * c1) else: m = min(a,d) a -= m d -= m p += m * c1 if d > 0: p += (min(b,c,...
There is a robot in a warehouse and n packages he wants to collect. The warehouse can be represented as a coordinate grid. Initially, the robot stays at the point (0, 0). The i-th package is at the point (x_i, y_i). It is guaranteed that there are no two packages at the same point. It is also guaranteed that the point ...
{ "input": [ "3\n5\n1 3\n1 2\n3 3\n5 5\n4 3\n2\n1 0\n0 1\n1\n4 3\n", "2\n1\n1 1\n1\n1 1\n", "2\n1\n1 2\n1\n1 1\n", "2\n1\n1 2\n1\n2 1\n", "2\n1\n1 2\n1\n1 2\n", "2\n1\n1 3\n1\n2 1\n", "2\n1\n1 0\n1\n1 2\n", "2\n1\n1 3\n1\n1 1\n", "2\n1\n1 0\n1\n1 1\n", "2\n1\n0 3\n1\n1 1\n", ...
t=int(input()) for q in range(t): n=int(input()) a=[list(map(int,input().split())) for i in range(n)] a.sort() ans="" x,y=0,0 f=0 for i in range(n): if a[i][0]>=x and a[i][1]>=y: ans+='R'*(a[i][0]-x) x=a[i][0] ans+='U'*(a[i][1]-y) y=a[i][1] else: f=1 print('NO') break if f==0: print('...
After a long party Petya decided to return home, but he turned out to be at the opposite end of the town from his home. There are n crossroads in the line in the town, and there is either the bus or the tram station at each crossroad. The crossroads are represented as a string s of length n, where s_i = A, if there is...
{ "input": [ "5\n2 2 1\nBB\n1 1 1\nAB\n3 2 8\nAABBBBAABB\n5 3 4\nBBBBB\n2 1 1\nABABAB\n", "5\n2 1 1\nBB\n1 1 1\nAB\n3 2 8\nAABBBBAABB\n5 3 4\nBBBBB\n2 1 1\nABABAB\n", "5\n2 1 1\nBB\n1 1 1\nAB\n3 0 8\nAABBBBAABB\n5 3 4\nBBBBB\n2 1 1\nABABAB\n", "5\n3 1 0\nBB\n1 1 2\nBA\n3 0 8\nAABBBBAABB\n5 3 4\nBBBBB\...
t = int(input()) for _ in range(t): a, b, p = map(int, input().split()) s = input() d = {'A': a, 'B': b} i = len(s)-1 prev = len(s)-1 while i >= 0: i -= 1 curr = s[i] while s[i] == curr and i >= 0: i -= 1 i += 1 p -= d[curr] if p < 0: ...
This is the easy version of the problem. The only difference between easy and hard versions is the constraint of m. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! <image> As a doll collector, Chiori has got n dolls. The i-th doll has a non-negati...
{ "input": [ "6 7\n11 45 14 9 19 81\n", "4 4\n3 5 8 14\n", "30 30\n65536 536870912 2097152 512 32768 16384 8388608 16777216 524288 4194304 1 1048576 2048 262144 64 4096 8192 256 131072 2 268435456 33554432 32 16 134217728 8 67108864 128 4 1024\n", "6 35\n23601314651 29074846252 10638992479 32779777411...
import sys MOD = 998244353 BOUND = 21 n, m = map(int, input().split()) l = list(map(int,input().split())) basis = [] for p in range(m-1,-1,-1): p2 = pow(2,p) nex = -1 for i in range(n): if l[i] >= p2: nex = l[i] break if nex != -1: basis.append(nex) for...
You might have remembered Theatre square from the [problem 1A](https://codeforces.com/problemset/problem/1/A). Now it's finally getting repaved. The square still has a rectangular shape of n × m meters. However, the picture is about to get more complicated now. Let a_{i,j} be the j-th square in the i-th row of the pav...
{ "input": [ "4\n1 1 10 1\n.\n1 2 10 1\n..\n2 1 10 1\n.\n.\n3 3 3 7\n..*\n*..\n.*.\n", "1\n3 3 2 4\n**.\n***\n***\n", "1\n9 9 9 9\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n.........\n", "1\n3 3 2 7\n**.\n***\n***\n", "4\n1 1 10 1\n.\n1 2 10 1\n..\n2 1...
cases=input() for i in range(0,int(cases)): inss=input() list=inss.split(' ') n=int(list[0]) m=int(list[1]) x=int(list[2]) y=int(list[3]) price=0 if 2*x > y: lame=True else: lame=False for count in range(0,n): data=input() ###print(data) ...
There are many freight trains departing from Kirnes planet every day. One day on that planet consists of h hours, and each hour consists of m minutes, where m is an even number. Currently, there are n freight trains, and they depart every day at the same time: i-th train departs at h_i hours and m_i minutes. The gover...
{ "input": [ "2 24 60 16\n16 0\n17 15\n", "2 24 60 15\n16 0\n17 15\n", "9 3 24 9\n1 15\n0 6\n0 22\n2 22\n2 7\n1 7\n1 23\n2 14\n0 14\n", "10 11 12 6\n9 5\n8 5\n9 11\n7 5\n10 5\n1 5\n2 11\n3 5\n2 5\n3 11\n", "10 24 60 8\n10 59\n21 56\n0 42\n21 27\n5 24\n5 53\n16 5\n5 22\n10 11\n10 46\n", "9 24 6...
from collections import defaultdict import sys input=sys.stdin.readline n,h,m,k=map(int,input().split()) l=[] a=[] start=0 for i in range(n): H,M=map(int,input().split()) M=M%(m//2) a.append(M) l.append((M+1,1,i)) l.append(((M+k)%(m//2),-1,i)) if (M+1)>(M+k)%(m//2): start+=1 l.append((0,...
Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has n rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height). Boris cho...
{ "input": [ "1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500\n", "1\n9 10 7\n1\n7 1 3\n", "20\n110 466 472\n112 153 152\n424 492 490\n348 366 113\n208 337 415\n491 448 139\n287 457 403\n444 382 160\n325 486 284\n447 454 136\n216 412 418\n217 208 228\n109 436 291\n293 382 421\n483 339 174\n213 327 183\n278 268 ...
#------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() ...
A famous gang of pirates, Sea Dogs, has come back to their hideout from one of their extravagant plunders. They want to split their treasure fairly amongst themselves, that is why You, their trusted financial advisor, devised a game to help them: All of them take a sit at their round table, some of them with the golde...
{ "input": [ "4 2\n1 2\n2 2\n", "3 2\n1 1\n2 2\n", "6 2\n2 3\n4 1\n", "25 2\n1 23\n3 1\n", "178279081 0\n", "999999999 1\n100 1000000000\n", "1 1\n1 1\n", "1001 1\n60 10001\n", "10 4\n4 1\n6 5\n7 1\n8 3\n", "999999999 1\n999999999 999999999\n", "1000 1\n1 1000\n", "1000...
import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write...
The hobbits Frodo and Sam are carrying the One Ring to Mordor. In order not to be spotted by orcs, they decided to go through the mountains. The mountain relief can be represented as a polyline with n points (x_i, y_i), numbered from 1 to n (x_i < x_{i + 1} for 1 ≤ i ≤ n - 1). Hobbits start their journey at the point ...
{ "input": [ "6 10\n10 40\n20 10\n25 30\n30 15\n50 15\n65 30\n", "2 10000\n0 10000\n400000 0\n", "9 5\n0 0\n5 10\n15 10\n20 0\n25 11\n30 0\n35 10\n50 10\n60 5\n", "4 6\n0 0\n2 4\n7 5\n10 0\n", "5 2\n1 3\n4 4\n5 7\n7 5\n10 4\n", "5 2\n1 3\n4 4\n5 1\n7 5\n10 4\n", "4 6\n0 0\n4 4\n7 5\n10 0\n...
import sys input = sys.stdin.readline n, H = map(int, input().split());points = [tuple(map(int, input().split())) for _ in range(n)];eye = (points[-1][0], points[-1][1] + H);peak = points[-1];out = 0 def dot(a,b): return a[0] * b[0] + a[1] * b[1] def sub(a,b): return (a[0] - b[0], a[1] - b[1]) def norm(a): ret...
You are given two integers n and k. You are asked to choose maximum number of distinct integers from 1 to n so that there is no subset of chosen numbers with sum equal to k. A subset of a set is a set that can be obtained from initial one by removing some (possibly all or none) elements of it. Input The first line c...
{ "input": [ "3\n3 2\n5 3\n1 1\n", "70\n1 1\n2 1\n2 2\n3 1\n3 2\n3 3\n4 1\n4 2\n4 3\n4 4\n5 1\n5 2\n5 3\n5 4\n5 5\n6 1\n6 2\n6 3\n6 4\n6 5\n6 6\n7 1\n7 2\n7 3\n7 4\n7 5\n7 6\n7 7\n8 1\n8 2\n8 3\n8 4\n8 5\n8 6\n8 7\n8 8\n9 1\n9 2\n9 3\n9 4\n9 5\n9 6\n9 7\n9 8\n9 9\n10 1\n10 2\n10 3\n10 4\n10 5\n10 6\n10 7\n10 ...
t=int(input()) for i in range(t): n,k = map(int,input().split()) ans=[] if n==1 and k==1: print(0) continue else: if k%2==0: for j in range(k//2,k): ans.append(j) for j in range(k+1,n+1): ans.append(j) else: for j in range(k//2+1,k): ans.append(j) for j in range(k+1,n+1): ans.a...
In Fire City, there are n intersections and m one-way roads. The i-th road goes from intersection a_i to b_i and has length l_i miles. There are q cars that may only drive along those roads. The i-th car starts at intersection v_i and has an odometer that begins at s_i, increments for each mile driven, and resets to ...
{ "input": [ "4 5\n1 2 1\n2 3 1\n3 1 2\n1 4 1\n4 3 2\n2\n1 2 4\n4 3 5\n", "4 4\n1 2 1\n2 3 1\n3 1 2\n1 4 3\n3\n1 1 3\n1 2 4\n4 0 1\n", "2 1\n2 1 1\n1\n2 1 2\n", "4 5\n1 2 1\n2 3 1\n3 1 2\n1 4 1\n4 3 2\n2\n1 2 4\n3 3 5\n", "3 1\n2 1 1\n1\n2 1 2\n", "3 1\n2 1 1\n1\n2 0 2\n", "4 4\n1 2 1\n2 3...
import sys from sys import stdin import math from collections import deque import sys class scc_graph: def __init__(self, N): self.N = N self.edges = [] def csr(self): self.start = [0]*(self.N+1) self.elist = [0]*len(self.edges) for e in self.edges: sel...
Input The input contains two integers a1, a2 (0 ≤ ai ≤ 109), separated by a single space. Output Output a single integer. Examples Input 3 14 Output 44 Input 27 12 Output 48 Input 100 200 Output 102
{ "input": [ "27 12\n", "3 14\n", "100 200\n", "59961393 89018456\n", "937477084 827336327\n", "0 0\n", "957747793 424238335\n", "859484421 914544919\n", "365180540 540383426\n", "35005211 521595368\n", "369133069 125898167\n", "911759956 749241873\n", "336465782 86...
from math import floor x, y = input().split() x = int(x) y = int(y) rev = 0 while y > 0: a = int(y % 10) rev = rev * 10 + a y = floor(y / 10) b = x + rev print(b)
Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the informati...
{ "input": [ "5 3\n1 2 1 1 3\n", "4 2\n1 2 1 2\n", "3 1\n1 1 1\n", "63 5\n76826 79919 83599 93821 79919 46132 46132 46132 79919 76826 79919 79919 76826 79919 79919 76826 76826 46132 76826 40347 79919 46132 76826 83599 79919 79919 46132 46132 46132 83599 83599 79919 46132 83599 93821 76826 81314 79919 ...
def answer(): ans,count,j=0,0,0 d=dict() for i in range(n): while(j==0 or d[a[j-1]] < k): if(j==n): j+=1 break try:d[a[j]]+=1 except:d[a[j]]=1 count += 1 m=n-count+1 j+=1 if(...
There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); *...
{ "input": [ "7 4\n1&gt;3&gt;22&lt;\n1 3\n4 7\n7 7\n1 7\n", "5 1\n1>3><\n4 5\n", "4 1\n34><\n1 4\n", "3 10\n<<<\n2 3\n3 3\n2 3\n3 3\n1 3\n1 1\n1 2\n3 3\n1 1\n2 2\n", "7 4\n1>3>22<\n1 3\n4 7\n7 7\n1 7\n", "1 1\n0\n1 1\n", "10 30\n306<<>4>04\n2 2\n6 6\n1 10\n1 8\n2 4\n9 10\n2 8\n3 5\n7 7\n2 ...
n, q = map(int, input().split()) s = input() for _ in range(q): l, r = map(int, input().split()) t = list(s[l-1:r]) p, d = 0, 1 res = [0] * 10 while 0 <= p < len(t): if '0' <= t[p] <= '9': k = int(t[p]) res[k] += 1 if k > 0: t[p] = str(k-1)...
Vasya has found a piece of paper with a coordinate system written on it. There are n distinct squares drawn in this coordinate system. Let's number the squares with integers from 1 to n. It turned out that points with coordinates (0, 0) and (ai, ai) are the opposite corners of the i-th square. Vasya wants to find such...
{ "input": [ "3 1\n2 4 1\n", "4 3\n5 1 3 4\n", "4 50\n5 1 10 2\n", "20 17\n407 2799 2032 2154 8119 9404 6718 7914 916 4720 1267 9403 5497 4518 9072 7828 8364 8230 3057 7770\n", "2 1\n1 2\n", "1 2\n10\n", "2 1\n8 20\n", "5 1\n1 2 3 4 5\n", "50 25\n1498786 501094 6396443 1167317 7194...
def ans(k,a): a.sort() if(k>len(a)): return "-1" else: if(k!=0): return a[len(a)-k] n,k=map(int,input().split(" ")) arr=list(map(int,input().split(" "))) if (ans(k,arr)=="-1"): print("-1") else: print(ans(k,arr)," ",ans(k,arr))
Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only on...
{ "input": [ "4 3\n", "8 4\n", "5 5\n", "1000000000000000000 1000000000\n", "525 34\n", "1000000000 999999999\n", "1 1000000000\n", "223265034477 726990\n", "499999998500000000 1000000000\n", "499999999500000000 1000000000\n", "499999998500000001 1000000000\n", "4624989...
n, k = map(int, input().split()) def prod(n): if n%2: return n*((n+1)//2) else: return (n//2)*(n+1) def total_count(n, k): if k >= n: return (0, 0, 1) else: count = 0 l = 1; r = k s = prod(k) while l <= r: mid = (l+r)//2 if n > s - prod(mid) + mid: r = mid-1 else: l = mid+1 n = n...
SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the target in turns, and SmallR shoots first. The probability of shooting the target each time is <image> for SmallR while <image> for Zanoes. The one who shoots in the target first should be the winner. Output the probability th...
{ "input": [ "1 2 1 2\n", "719 735 626 990\n", "1 3 1 3\n", "64 704 148 603\n", "486 868 929 999\n", "533 773 823 998\n", "897 957 92 898\n", "933 977 266 450\n", "518 816 243 359\n", "3 4 3 4\n", "699 925 441 928\n", "450 885 755 836\n", "882 962 311 811\n", "1...
a, b, c, d = map(int, input().split(' ')) x = a/b y = c/d print(x / (1-(1-x) * (1-y)))
Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork. For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing ...
{ "input": [ "4 8\n", "1 5\n", "1 2\n", "4 10\n", "5 4\n", "4247 8495\n", "3 2\n", "6 5\n", "1 4\n", "7101 14204\n", "8826 12432\n", "4 3\n", "2 5\n", "3830 6966\n", "1027 2030\n", "4610 4609\n", "2 3\n", "42346 51898\n", "3 1\n", "32342 ...
mod = 1000000007 ii = lambda : int(input()) si = lambda : input() dgl = lambda : list(map(int, input())) f = lambda : map(int, input().split()) il = lambda : list(map(int, input().split())) ls = lambda : list(input()) n0, n1 = f() s='0'*(n0+n1) if n1<n0-1: print(-1) elif n1==n0-1 or n1==n0: s=s.replace('0','01'...
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be t...
{ "input": [ "4\n1 0 0 -1\n", "2\n1 -1\n", "5\n10 11 12 13 -40\n", "3\n0 2 3\n", "5\n0 4 10 -5 -5\n", "4\n0 100 100 -200\n", "4\n10 10 -10 -10\n", "2\n1 10000\n", "3\n10 10 -10\n", "100\n-57 -64 83 76 80 27 60 76 -80 -56 52 72 -17 92 -96 87 41 -88 94 89 12 42 36 34 -100 -43 -42...
import os import math cumsum = [int(x) for x in os.read(0, os.fstat(0).st_size).split()] n = cumsum[0] cumsum[0] = 0 for i in range(n): cumsum[i+1] += cumsum[i] def work(besta, i, sqrtbesta, lowerbound, upperbound, cumsumi): for j in range(i+1, min(n, i - 1 + sqrtbesta) + 1): if lowerbound < cumsum[j]...
Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of n distinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly:...
{ "input": [ "2\n1 2\n", "4\n2 1 3 4\n", "4\n3 1 2 4\n", "3\n3 2 1\n", "6\n19517752 43452931 112792556 68417469 779722934 921694415\n", "4\n1 4 2 3\n", "5\n226959376 840957605 833410429 273566427 872976052\n", "6\n169793171 335736854 449917902 513287332 811627074 938727967\n", "6\n...
# coding=utf-8 if __name__ == '__main__': n = int(input()) line = str(input()).split() line = [int(it) for it in line] temp_line = line.copy() temp_line.sort() ptr_head = 0 ptr_tail = n - 1 while line[ptr_head] == temp_line[ptr_head]: if ptr_head == ptr_tail: break ptr_head += 1 while line[ptr_tail] =...
Our good friend Mole is trying to code a big message. He is typing on an unusual keyboard with characters arranged in following way: qwertyuiop asdfghjkl; zxcvbnm,./ Unfortunately Mole is blind, so sometimes it is problem for him to put his hands accurately. He accidentally move...
{ "input": [ "R\ns;;upimrrfod;pbr\n", "R\nwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww\n", "L\nq\n", "R\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", "R\n.\n", "L\ngggggggggggggg...
move=input() ans="" s=input() keyboard=["qwertyuiop","asdfghjkl;","zxcvbnm,./"] if move =="R": for i in range(len(s)): for j in range(len(keyboard)): if s[i] in keyboard[j]: ans+=keyboard[j][keyboard[j].index(s[i])-1] elif move =="L": for i in range(len(s)): for j in ...
Vasya became interested in bioinformatics. He's going to write an article about similar cyclic DNA sequences, so he invented a new method for determining the similarity of cyclic sequences. Let's assume that strings s and t have the same length n, then the function h(s, t) is defined as the number of positions in whic...
{ "input": [ "2\nAG\n", "3\nTTT\n", "1\nC\n", "20\nTAAGCGACCAGGTGCTTTAC\n", "15\nAGCGAATCCCATTGT\n", "4\nGACT\n", "1\nT\n", "3\nGCA\n", "318\nTATCAATCGGTACGTGCGCATCATTGTCAATCGGGCTTCATGGCTTGCGGGCGCTACCCGAGGGGAAGCTGCGGACAGGTAGGTAAGATACACACGAACCAAACGGAGTTATGTTGGATAAATTGGCTGGAAGGGCGTAG...
# problem statement: https://codeforces.com/problemset/problem/520/C modulo = 1000000007 n = int(input()) char_count = [0] * 256 s = input() for i in range(n): char_count[ord(s[i])] += 1 max_char_count = max(char_count) num_max_char = 0 for i in range(256): if char_count[i] == max_char_count: num_max_char += 1 pr...
Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set. Now Wilbur wants to number the points in the set he has, that is ...
{ "input": [ "5\n2 0\n0 0\n1 0\n1 1\n0 1\n0 -1 -2 1 0\n", "3\n1 0\n0 0\n2 0\n0 1 2\n", "1\n0 0\n-9876\n", "9\n0 0\n1 0\n2 0\n0 1\n1 1\n2 1\n1 2\n2 2\n0 2\n0 0 0 -1 -1 -2 1 1 2\n", "40\n0 0\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n0 7\n0 8\n0 9\n0 10\n0 11\n0 12\n1 0\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 0\n...
from collections import defaultdict def solve(): N = int(input()) maxx = 0 maxy = 0 WS = defaultdict(list) for i in range(N): x, y = map(int, input().split()) WS[y - x].append((x, y)) maxx = max(maxx, x) maxy = max(maxy, y) for w in WS: WS[w].sort(rev...
Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k. Input The first line of the input contains i...
{ "input": [ "6 2 3\n1 2 1 1 0 3\n1 6\n3 5\n", "5 3 1\n1 1 1 1 1\n1 5\n2 4\n1 3\n", "6 2 4\n1 2 1 1 0 3\n1 6\n3 5\n", "5 3 1\n0 1 1 1 1\n1 5\n2 4\n1 3\n", "6 2 3\n1 4 1 1 0 3\n1 6\n3 5\n", "5 3 1\n0 1 1 1 1\n1 5\n2 4\n1 2\n", "5 3 0\n0 1 1 1 1\n1 5\n2 4\n1 2\n", "6 2 4\n1 2 1 1 1 5\n1 ...
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file....
Berland scientists face a very important task - given the parts of short DNA fragments, restore the dinosaur DNA! The genome of a berland dinosaur has noting in common with the genome that we've used to: it can have 26 distinct nucleotide types, a nucleotide of each type can occur at most once. If we assign distinct En...
{ "input": [ "3\nbcd\nab\ncdef\n", "4\nx\ny\nz\nw\n", "4\nrz\nvu\nxy\npg\n", "70\nxv\nlu\ntb\njx\nseh\nc\nm\ntbr\ntb\ndl\ne\nd\nt\np\nn\nse\nna\neh\nw\np\nzkj\nr\nk\nrw\nqf\ndl\ndl\ns\nat\nkjx\na\nz\nmig\nu\nse\npse\nd\ng\nc\nxv\nv\ngo\nps\ncd\nyqf\nyqf\nwzk\nxv\nat\nw\no\nl\nxvm\nfpse\nz\nk\nna\nv\ns...
n = int(input()) genom = dict() heads = set() for i in range(n): part = input() heads.add(part[0]) for i in range(len(part)): prev = None next = None if i - 1 >= 0: prev = part[i - 1] if i + 1 < len(part): next = part[i + 1] if part[i] in g...
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion: * An 'L' indicates he should move one unit left. * An 'R' indicates he should move one unit right. * A 'U' indicates he should move one unit up. * A 'D' indicates he shoul...
{ "input": [ "UDUR\n", "RUUR\n", "RRU\n", "RDDLLDLUUUDDRDRURLUUURLLDDLRLUURRLLRRLDRLLUDRLRULLDLRRLRLRLRUDUUDLULURLLDUURULURLLRRRURRRDRUUDLDRLRDRLRRDDLDLDLLUDRUDRLLLLDRDUULRUURRDLULLULDUDULRURRDDDLLUDRLUDDLDDDRRDDDULLLLDLDRLRRLRRDDRLULURRUDRDUUUULDURUDRDLDDUDUDRRURDULRRUDRLRRDLUURURDLDRLRDUDDDLDDDURURL...
s = input() ud = lr = 0 for ch in s: if(ch=='R'): lr = lr+1 if(ch=='L'): lr = lr-1 if(ch=='U'): ud = ud+1 if(ch=='D'): ud = ud-1 if((abs(lr) + abs(ud))%2==1): print(-1) else: print(int((abs(lr) + abs(ud))/2))
One day, the Grasshopper was jumping on the lawn and found a piece of paper with a string. Grasshopper became interested what is the minimum jump ability he should have in order to be able to reach the far end of the string, jumping only on vowels of the English alphabet. Jump ability is the maximum possible length of ...
{ "input": [ "ABABBBACFEYUKOTT\n", "AAA\n", "HDDRZDKCHHHEDKHZMXQSNQGSGNNSCCPVJFGXGNCEKJMRKSGKAPQWPCWXXWHLSMRGSJWEHWQCSJJSGLQJXGVTBYALWMLKTTJMFPFS\n", "AABBYYYYYYYY\n", "XXFPZDRPXLNHGDVCBDKJMKLGUQZXLLWYLOKFZVGXVNPJWZZZNRMQBRJCZTSDRHSNCVDMHKVXCXPCRBWSJCJWDRDPVZZLCZRTDRYA\n", "SGDDFCDRDWGPNNFBBZZ...
def solve(a): l = [0] li = [] j = 0 for i in range(len(a)): if a[i] == "A" or a[i] == "E" or a[i] == "I" or a[i] == "O" or a[i] == "U" or a[i] == "Y": l.append(i + 1) j += 1 li.append(l[j] - l[j-1]) l.append(i + 1) j += 1 li.append(l[j] - l[j-1...
Bash has set out on a journey to become the greatest Pokemon master. To get his first Pokemon, he went to Professor Zulu's Lab. Since Bash is Professor Zulu's favourite student, Zulu allows him to take as many Pokemon from his lab as he pleases. But Zulu warns him that a group of k > 1 Pokemon with strengths {s1, s2, ...
{ "input": [ "5\n2 3 4 6 7\n", "3\n2 3 4\n", "3\n49999 49999 99998\n", "3\n457 457 457\n", "2\n4 9\n", "3\n49999 99998 99998\n", "10\n1 1 1 1 1 1 1 1 1 1\n", "2\n88258 44129\n", "7\n9 4 2 3 3 9 8\n", "3\n2 1 1\n", "3\n1 1 2\n", "4\n49441 25225 9081 4036\n", "6\n1623...
def function1(n,s): if n==1: return 1 pokemonj=0 pokecage=[0 for i in range(100001)] for i in range(n): pokecage[s[i]]+=1 maxyincage=min(pokecage[1],1) a = [i for i in range(100001)] a[1] = 0 i = 2 while i <= 100000: if a[i] != 0: pokemonj=0 ...
Peter decided to lay a parquet in the room of size n × m, the parquet consists of tiles of size 1 × 2. When the workers laid the parquet, it became clear that the tiles pattern looks not like Peter likes, and workers will have to re-lay it. The workers decided that removing entire parquet and then laying it again is v...
{ "input": [ "4 3\nULR\nDLR\nLRU\nLRD\nULR\nDUU\nUDD\nDLR", "2 3\nULR\nDLR\nLRU\nLRD\n", "34 5\nLRUUU\nUUDDD\nDDULR\nLRDUU\nULRDD\nDULRU\nUDLRD\nDULRU\nUDLRD\nDLRUU\nULRDD\nDLRLR\nLRUUU\nLRDDD\nUUULR\nDDDLR\nLRULR\nLRDLR\nLRLRU\nULRUD\nDLRDU\nLRLRD\nULRUU\nDUUDD\nUDDUU\nDLRDD\nLRLRU\nUUUUD\nDDDDU\nLRUUD\n...
#!/usr/bin/env python3 board = [] n, m = 0, 0 def rotate(x, y): if board[x][y] == 'L': board[x][y] = board[x][y+1] = 'U' board[x+1][y] = board[x+1][y+1] = 'D' else: board[x][y] = board[x+1][y] = 'L' board[x][y+1] = board[x+1][y+1] = 'R' def fix(x, y, moves): if board[x+1][...
As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it... In...
{ "input": [ "abcheaibcdi\n", "hiedi\n", "fbldqzggeunkpwcfirxanmntbfrudijltoertsdvcvcmbwodbibsrxendzebvxwydpasaqnisrijctsuatihxxygbeovhxjdptdcppkvfytdpjspvrannxavmkmisqtygntxkdlousdypyfkrpzapysfpdbyprufwzhunlsfugojddkmxzinatiwfxdqmgyrnjnxvrclhxyuwxtshoqdjptmeecvgmrlvuwqtmnfnfeeiwcavwnqmyustawbjodzwsqmnjxh...
li = list(input()) def f(n,s): global li while True: if li[n] != s: li.pop(n) else: break try: f(0,'h') f(1,'e') f(2,'i') f(3,'d') f(4,'i') except: print("NO") else: print("YES")
Makes solves problems on Decoforces and lots of other different online judges. Each problem is denoted by its difficulty — a positive integer number. Difficulties are measured the same across all the judges (the problem with difficulty d on Decoforces is as hard as the problem with difficulty d on any other judge). M...
{ "input": [ "4 20\n10 3 6 3\n", "3 3\n2 1 9\n", "1 2\n9\n", "7 6\n4 20 16 14 3 17 4\n", "1 1\n1000000000\n", "4 2\n45 44 35 38\n", "2 1\n4 1000000000\n", "2 1\n1 4\n", "100 10\n246 286 693 607 87 612 909 312 621 37 801 558 504 914 416 762 187 974 976 123 635 488 416 659 988 998 93...
n,k=map(int,input().split()) ar=sorted(list(map(int,input().split()))) ans=0 for x in ar: if k <= x <= k*2: k=x while x/2 > k: ans+=1 k*=2 if k <= x <= k*2: k=x print(ans)
From beginning till end, this message has been waiting to be conveyed. For a given unordered multiset of n lowercase English letters ("multi" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times: * Remove any two elements s and t...
{ "input": [ "12\n", "3\n", "99681\n", "8\n", "831\n", "9\n", "233\n", "1926\n", "7\n", "39393\n", "4\n", "2\n", "5\n", "1\n", "14514\n", "99998\n", "418\n", "82944\n", "11\n", "100000\n", "1317\n", "6\n", "10\n", "0\n", ...
# def ap(l): # d = set() # for i in range(1, len(l)): # d.add(l[i]-l[i-1]) # if(len(d) == 1): # return d.pop() # else: # return "re" # def app(ll, oo, nm, ooo): # k = [] # for j in range(n): # if(j%oo !=0): # k.append(ll[j]) # if(ap(k) != "re" an...
Arkady wants to water his only flower. Unfortunately, he has a very poor watering system that was designed for n flowers and so it looks like a pipe with n holes. Arkady can only use the water that flows from the first hole. Arkady can block some of the holes, and then pour A liters of water into the pipe. After that,...
{ "input": [ "4 10 3\n2 2 2 2\n", "5 10 10\n1000 1 1 1 1\n", "4 80 20\n3 2 1 4\n", "2 10000 1\n1 9999\n", "10 300 100\n20 1 3 10 8 5 3 6 4 3\n", "1 2 1\n1\n", "2 2 2\n1 10000\n", "10 300 100\n20 25 68 40 60 37 44 85 23 96\n", "1 1 1\n1\n", "2 11000 1\n1 9999\n", "10 300 100...
n, a, b = [int(i) for i in input().split(' ')] sizes = [int(i) for i in input().split(' ')] st = sum(sizes) s = (sizes[0] * a) / b sb = st - s blockable = sorted(sizes[1:], reverse=True) blocked_no = 0 blocked_amount = 0 for i in range(len(blockable)): if blocked_amount < sb: blocked_no += 1 block...
Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you. Both participants communicated to each other a set ...
{ "input": [ "2 3\n1 2 4 5\n1 2 1 3 2 3\n", "2 2\n1 2 3 4\n1 5 3 4\n", "2 2\n1 2 3 4\n1 5 6 4\n", "2 3\n1 2 1 3\n1 2 1 3 2 3\n", "3 7\n8 2 7 9 8 1\n3 1 8 1 2 7 4 7 4 2 1 4 4 6\n", "1 10\n3 9\n3 2 3 4 5 3 5 7 8 6 2 5 7 8 2 4 1 7 5 1\n", "12 1\n6 2 6 4 8 6 6 9 5 6 6 1 9 1 1 3 3 9 2 4 5 2 8 1...
import sys #sys.stdin=open("data.txt") input=sys.stdin.readline n,m=map(int,input().split()) possible1=[set() for _ in range(200)] possible2=[set() for _ in range(200)] weird=[0]*15 p1=list(map(int,input().split())) p2=list(map(int,input().split())) for i in range(n): for j in range(m): a=sorted(p1[i*2...
A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.) Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits. Constraints * 1 \leq K \leq N \leq 1000 * 1 \leq...
{ "input": [ "1 1\n1000", "5 3\n50 100 80 120 80", "5 3\n50 000 80 120 80", "5 3\n83 000 80 120 80", "5 3\n83 010 80 120 80", "5 3\n83 000 80 120 99", "5 3\n83 000 16 120 99", "2 3\n83 001 16 120 99", "1 0\n1000", "5 3\n50 100 80 120 150", "5 3\n28 000 80 120 80", "5 3\...
_,K,*P=map(int,open(0).read().split()) print(sum(sorted(P)[:K]))
Takahashi wants to print a document with N pages double-sided, where two pages of data can be printed on one sheet of paper. At least how many sheets of paper does he need? Constraints * N is an integer. * 1 \leq N \leq 100 Input Input is given from Standard Input in the following format: N Output Print the a...
{ "input": [ "5", "100", "2", "9", "110", "3", "11", "111", "6", "101", "1", "8", "18", "14", "15", "26", "36", "28", "65", "20", "42", "67", "47", "57", "55", "40", "24", "75", "69", "92", "148...
N = int(input()) print(str(N//2+N%2))
Given are N points on the circumference of a circle centered at (0,0) in an xy-plane. The coordinates of the i-th point are (\cos(\frac{2\pi T_i}{L}),\sin(\frac{2\pi T_i}{L})). Three distinct points will be chosen uniformly at random from these N points. Find the expected x- and y-coordinates of the center of the circ...
{ "input": [ "3 4\n0\n1\n3", "10 100\n2\n11\n35\n42\n54\n69\n89\n91\n93\n99", "4 8\n1\n3\n5\n6", "10 100\n2\n10\n35\n42\n54\n69\n89\n91\n93\n99", "4 8\n1\n5\n5\n6", "10 100\n2\n10\n35\n42\n54\n69\n87\n91\n93\n99", "4 8\n1\n2\n5\n6", "4 8\n1\n2\n5\n5", "4 8\n1\n3\n5\n5", "4 15\n...
from math import cos, sin, pi from itertools import combinations N, L = map(int, input().split()) T = [int(input()) for _ in range(N)] PI2oL = 2*pi/L csum, ssum = 0, 0 count = 0 for i, j in combinations(range(N), 2): count += 1 mid1, num2 = (T[i] + T[j]) / 2, j - i - 1 mid2, num1 = (mid1 + L/2) % L, N - nu...
We have A apples and P pieces of apple. We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan. Find the maximum number of apple pies we can make with what we have now. Constraints * All values in input are integers. * 0 \leq A, P \leq 100 Input Input is g...
{ "input": [ "1 3", "32 21", "0 1", "2 3", "53 21", "1 1", "2 6", "34 21", "0 0", "0 6", "47 21", "0 -1", "47 2", "-1 -1", "2 5", "47 3", "-1 -2", "-2 -1", "2 12", "15 4", "2 14", "29 4", "1 -1", "4 14", "58 4", "7...
A, P = map(int, input().split()) print(((A*3)+P)//2)
There is a set A = \\{ a_1, a_2, \ldots, a_N \\} consisting of N positive integers. Taro and Jiro will play the following game against each other. Initially, we have a pile consisting of K stones. The two players perform the following operation alternately, starting from Taro: * Choose an element x in A, and remove e...
{ "input": [ "1 100000\n1", "3 21\n1 2 3", "2 5\n2 3", "2 4\n2 3", "2 7\n2 3", "3 20\n1 2 3", "1 100000\n2", "3 21\n1 1 3", "2 3\n2 3", "2 7\n1 3", "1 100000\n4", "3 21\n1 1 4", "2 4\n2 5", "2 1\n1 3", "2 4\n3 5", "2 2\n1 3", "2 2\n2 3", "2 5\n2 ...
n, k = [int(i) for i in input().split()] A = [int(i) for i in input().split()] DP = [False] * (k + 1) for i in range(1, k+1): DP[i] = any(not DP[i-a] for a in A if i - a >= 0) print('First' if DP[k] else 'Second')
There is always an integer in Takahashi's mind. Initially, the integer in Takahashi's mind is 0. Takahashi is now going to eat four symbols, each of which is `+` or `-`. When he eats `+`, the integer in his mind increases by 1; when he eats `-`, the integer in his mind decreases by 1. The symbols Takahashi is going t...
{ "input": [ "+-++", "-+--", "----", "--+-", "++-+", "++++", "-+-+", "---+", "+---", "-+++", "+++-", "++--", "+--+", "--++", "+-+-", "-++-", "--+-", "++-+", "---+", "+++-", "-+++", "+---", "++++", "-++-", "+-+-", "...
s=list(input()) print(s.count("+")*2-4)
You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order: * Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of dam...
{ "input": [ "4 1000000000\n1 1\n1 10000000\n1 30000000\n1 99999999", "1 10\n3 5", "2 10\n3 5\n2 6", "5 500\n35 44\n28 83\n46 62\n31 79\n40 43", "4 1000000000\n1 1\n1 00000000\n1 30000000\n1 99999999", "1 10\n4 5", "2 10\n2 5\n2 6", "5 500\n35 44\n28 83\n46 62\n31 79\n0 43", "4 100...
import math N,H=map(int,input().split()) ab=[[int(x) for x in input().split()] for _ in range(N)] a,_=max(ab) B=sorted([b for _,b in ab if b > a], reverse=True) sb=sum(B) if sb < H: print(len(B)+math.ceil((H-sb)/a)) else: B.insert(0, 0) for i in range(1, len(B)): B[i] += B[i-1] if B[i] >= H: break print(i)
You are given a tree with N vertices. Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N-1 edges, where N is the number of its vertices. The i-th edge (1≤i≤N-1) connects Vertices a_i and b_i, and has a length of c_i. You are also given Q queries and an integer K. In the j-th qu...
{ "input": [ "5\n1 2 1\n1 3 1\n2 4 1\n3 5 1\n3 1\n2 4\n2 3\n4 5", "7\n1 2 1\n1 3 3\n1 4 5\n1 5 7\n1 6 9\n1 7 11\n3 2\n1 3\n4 5\n6 7", "10\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4 5 1000000000\n5 6 1000000000\n6 7 1000000000\n7 8 1000000000\n8 9 1000000000\n9 10 1000000000\n1 1\n9 10", "7\n1 ...
import sys input = sys.stdin.readline N = int(input()) G = [[] * N for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) G[a - 1].append((b - 1, c)) G[b - 1].append((a - 1, c)) Q, K = map(int, input().split()) D = [-1] * N s = [(K - 1, 0)] while s: v, c = s.pop() D[v] = c for vv,...
There is a square-shaped grid with N vertical rows and N horizontal columns. We will denote the square at the i-th row from the top and the j-th column from the left as (i,\ j). Initially, each square is either white or black. The initial color of the grid is given to you as characters a_{ij}, arranged in a square sha...
{ "input": [ "2\n..\n..", "2\n#.\n.#", "2", "3\n.#.\n\n.#.", "3\n...\n.#.\n...", "2\n.\n.#", "2\n/.\n..", "2\n#.\n#.", "3\n.#.\n\n.$.", "3\n...\n.#.\n/..", "2\n.\n#.", "2\n#.\n-#", "0", "2\n./\n..", "2\n#.\n#-", "3\n.\".\n\n.$.", "3\n...\n.#/\n/..", ...
def main(): n = int(input()) grid = [input() for _ in [0]*n] for i in grid: if "#" in i: break else: print(-1) return ans = 10**20 # i行目に何個黒があるか black_cnt = [0]*n # i列目に黒が一つでもあればTrue exist = [False]*n for i in range(n): for j in range(n...
One day Mr. Takahashi picked up a dictionary containing all of the N! permutations of integers 1 through N. The dictionary has N! pages, and page i (1 ≤ i ≤ N!) contains the i-th permutation in the lexicographical order. Mr. Takahashi wanted to look up a certain permutation of length N in this dictionary, but he forgo...
{ "input": [ "1\n0", "5\n1 2 3 5 4", "4\n0 2 3 0", "10\n0 3 0 0 1 0 4 0 0 0", "3\n0 0 0", "1\n1", "4\n0 1 3 0", "3\n0 0 1", "4\n0 1 2 0", "3\n0 0 2", "3\n0 0 3", "10\n0 3 0 0 1 0 6 0 0 0", "4\n0 1 0 0", "4\n0 0 0 0", "4\n0 0 0 1", "4\n0 2 3 1", "3\n0...
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(max(1000, 10**9)) write = lambda x: sys.stdout.write(x+"\n") M = 10**9+7 n = int(input()) p = list(map(int, input().split())) g1 = [None]*(n+10) v = 1 g1[0] = 1 for i in range(1,len(g1)): v *= i v %= M g1[i] = v def init(...
n! = n × (n − 1) × (n − 2) × ... × 3 × 2 × 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of...
{ "input": [ "2\n12\n10000\n0", "2\n21\n10000\n0", "2\n21\n11000\n0", "2\n6\n11000\n0", "2\n6\n11100\n0", "2\n6\n10000\n0", "2\n42\n11000\n0", "2\n3\n11100\n0", "2\n6\n10101\n0", "2\n1\n11110\n0", "2\n12\n11000\n0", "2\n10\n10000\n0", "2\n3\n10100\n0", "2\n0\n11...
import sys f = sys.stdin while True: n = int(f.readline()) if n == 0: break zero_count = 0 while n: n //= 5 zero_count += n print(zero_count)
White Tiger University holds a programming contest every year. The contest starts with a score of 0 for all teams, and points are added according to the answer status. In this contest, rankings will be made in descending order of score. When the total number of teams is N, each team is assigned a number from 1 to N. If...
{ "input": [ "3 11\n0 2 5\n0 1 5\n0 3 4\n1 1\n1 2\n1 3\n0 3 2\n1 1\n0 2 1\n1 2\n1 3", "5 2\n1 1\n1 2", "3 11\n0 2 5\n0 1 5\n0 3 6\n1 1\n1 2\n1 3\n0 3 2\n1 1\n0 2 1\n1 2\n1 3", "3 11\n0 2 5\n0 1 5\n0 3 8\n1 1\n1 2\n1 3\n0 3 2\n1 1\n0 2 1\n1 2\n1 3", "3 10\n0 2 5\n0 1 5\n0 3 6\n1 1\n1 2\n1 3\n0 3 2\...
from bisect import bisect_left as bl def main(): n, c = map(int, input().split()) ranking = [(0, i) for i in range(n)] points = [0 for _ in range(n)] for _ in range(c): com = input().split() if com[0] == "1": m = int(com[1]) - 1 print(ranking[m][-1] + 1, -ranking[m][0]) else: t, p...
The deadline of Prof. Hachioji’s assignment is tomorrow. To complete the task, students have to copy pages of many reference books in the library. All the reference books are in a storeroom and only the librarian is allowed to enter it. To obtain a copy of a reference book’s page, a student should ask the librarian to...
{ "input": [ "2 1 1\n1\n50\n2 1 2\n1\n50\n1\n60\n2 1 2\n2\n60 61\n1\n70\n4 2 3\n3\n60 61 62\n1\n70\n2\n80 81\n3 1 2\n3\n60 61 62\n2\n70 60\n1 2 5\n2\n87 95\n3\n96 71 35\n2\n68 2\n3\n3 18 93\n2\n57 2\n2 2 1\n5\n1 2 1 3 1\n0 0 0", "2 1 1\n1\n50\n2 2 2\n1\n50\n1\n60\n2 1 2\n2\n60 61\n1\n70\n4 2 3\n3\n60 61 62\n1...
def solve(): from collections import deque from sys import stdin f_i = stdin ans = [] while True: m, c, n = map(int, f_i.readline().split()) if m == 0: break book_pos = dict() studens = deque() shelf = m + 1 for i in ...
Arithmetic Progressions An arithmetic progression is a sequence of numbers $a_1, a_2, ..., a_k$ where the difference of consecutive members $a_{i+1} - a_i$ is a constant ($1 \leq i \leq k-1$). For example, the sequence 5, 8, 11, 14, 17 is an arithmetic progression of length 5 with the common difference 3. In this pro...
{ "input": [ "6\n0 1 3 5 6 9", "6\n0 2 3 5 6 9", "6\n0 1 3 5 8 9", "6\n0 1 3 5 7 9", "6\n-1 1 3 5 7 9", "6\n-1 2 0 8 6 9", "6\n0 4 3 5 6 9", "6\n0 2 3 5 6 14", "6\n0 2 3 5 7 9", "6\n-1 1 3 5 6 9", "6\n-1 2 3 5 6 9", "6\n0 4 3 5 6 10", "6\n0 2 3 5 4 9", "6\n0 1 3...
#!/usr/bin/python3 import os import sys def main(): N = read_int() V = read_ints() print(solve(N, V)) def solve(N, V): V.sort() pos = {} for i, a in enumerate(V): pos[a] = i best = 2 done = [[False] * N for _ in range(N)] for i in range(N): a = V[i] for j...
Taro's Shopping Mammy decided to give Taro his first shopping experience. Mammy tells him to choose any two items he wants from those listed in the shopping catalogue, but Taro cannot decide which two, as all the items look attractive. Thus he plans to buy the pair of two items with the highest price sum, not exceedin...
{ "input": [ "3 45\n10 20 30\n6 10\n1 2 5 8 9 11\n7 100\n11 34 83 47 59 29 70\n4 100\n80 70 60 50\n4 20\n10 5 10 16\n0 0", "3 45\n17 20 30\n6 10\n1 2 5 8 9 11\n7 100\n11 34 83 47 59 29 70\n4 100\n80 70 60 50\n4 20\n10 5 10 16\n0 0", "3 45\n17 20 30\n6 10\n1 2 5 8 4 11\n7 000\n11 34 83 47 59 29 70\n4 100\n...
while True: n,m = map(int,input().split(" ")) if n == 0: break items = [] for i in input().split(" "): items.append(int(i)) #全探索 items2 = items sums = [] for i in range(len(items)): for j in range(len(items)): if i == j: continue ...
Problem C: Seishun 18 Kippu A student at R University, sirokurostone, was about to attend a training camp at Atsu University. Other members plan to use the Shinkansen, but sirokurostone was going to use the Seishun 18 Ticket. Similarly, a person who likes 2D with a youth 18 ticket was also trying to participate in the...
{ "input": [ "4 4\nA B G\nA B 40 3\nB C 80 0\nA G 40 0\nB G 80 0\n5 6\nKusatsu Tokyo Aizu\nTokyo Nagoya 120 3\nKusatsu Nagoya 40 0\nKusatsu Kanazawa 40 0\nKanazawa Aizu 40 0\nTokyo Kanazawa 40 0\nTokyo Aizu 80 4\n0 0", "4 4\nA B G\nA B 40 3\nB C 80 0\nA G 40 0\nB G 80 0\n5 6\nKusatsu Tokyo Aizu\nTokyo Nagoya ...
from heapq import heappush, heappop while True: n, m = map(int, input().split()) if n == 0:break s, p, g = input().split() edges = {} for _ in range(m): a, b, d, t = input().split() if a not in edges:edges[a] = [] if b not in edges:edges[b] = [] d = int(d) t = int(t) edges[a].append((b...
Hakone Ekiden is one of the Japanese New Year's traditions. In Hakone Ekiden, 10 runners from each team aim for the goal while connecting the sashes at each relay station. In the TV broadcast, the ranking change from the previous relay station is displayed along with the passing order of each team at the relay station....
{ "input": [ "3\n-\nU\nD", "10\nU\nD\nU\nD\nU\nD\nU\nD\nU\nD", "8\nU\nD\nD\nD\nD\nD\nD\nD", "2\nD\nU", "5\nU\nU\n-\nD\nD", "2\nD\nT", "2\nE\nU", "2\nF\nU", "2\nD\nS", "2\nC\nU", "2\nB\nU", "2\nD\nV", "2\nD\nR", "2\nA\nU", "2\nD\nW", "2\nG\nU", "2\nD\...
#!/usr/bin/env python3 import sys import math from bisect import bisect_right as br from bisect import bisect_left as bl sys.setrecursionlimit(2147483647) from heapq import heappush, heappop,heappushpop from collections import defaultdict from itertools import accumulate from collections import Counter from collection...
Problem statement Here are N mysteriously shaped vases. The i-th jar is a shape in which K_i right-sided cylinders are vertically connected in order from the bottom. The order in which they are connected cannot be changed. Mr. A has a volume of water of M. Pour this water into each jar in any amount you like. It does ...
{ "input": [ "2 15\n2 3 3 7 2\n2 7 1 1 4", "2 15\n2 3 3 7 2\n1 7 1 1 4", "2 15\n2 3 5 7 2\n0 7 1 1 4", "2 15\n2 5 5 8 2\n1 7 1 1 4", "2 4\n2 3 3 7 1\n0 7 1 1 6", "2 25\n2 3 3 7 2\n2 7 1 1 4", "2 0\n2 3 5 7 2\n1 7 1 1 4", "2 4\n2 1 3 7 1\n0 7 1 1 6", "2 15\n2 2 5 7 3\n0 11 0 1 4", ...
def main(): n, m = map(int, input().split()) height = [] for _ in range(n): lst = list(map(int, input().split())) k = lst[0] ss = lst[1::2] hs = lst[2::2] v_acc = 0 h_acc = 0 index = 0 s, h = ss[0], hs[0] save = [] for i in range(m + 1): if i < v_acc + s * h: ...
A: IP Address (Internet Protocol Address) problem Since one number string is given, find the number of valid delimiters for IPv4 IP addresses. However, the effective delimiters for IPv4 IP addresses are as follows. * The sequence of numbers is divided into four, and each of the separated columns satisfies all of the...
{ "input": [ "123456789", "243960381", "17566177", "2154497", "961829", "1544196", "373537", "2961", "7074896", "1117147", "2844628", "11343273", "62476594", "1581113", "2527273", "2532591", "164664", "2642", "3591", "4233", "561", ...
def check(num): if num == "":return False if num[0] == "0":return num == "0" return 0 <= int(num) <= 255 s = input() ans = 0 for i in range(1, 4): for j in range(1, 4): for k in range(1, 4): n1 = s[:i] n2 = s[i:i+j] n3 = s[i+j:i+j+k] n4 = s[i+j+k:] if check(n1) and check(n2) an...
Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, inturn, less than or equal to each element of A[q+1..r]. I...
{ "input": [ "12\n13 19 9 5 12 8 7 4 21 2 6 11", "12\n13 19 9 5 12 8 7 4 21 2 10 11", "12\n13 19 9 5 12 8 7 4 21 2 10 18", "12\n13 19 9 5 12 8 7 4 21 0 10 18", "12\n13 19 9 5 12 8 7 4 37 0 10 18", "12\n13 19 9 5 12 8 7 4 13 0 10 18", "12\n25 19 9 5 12 8 7 4 13 0 10 18", "12\n25 19 9 5 ...
def partition(A, p, r): i = p - 1 x = A[r] for j in range(p,r): if A[j] <= x: i += 1 A[i], A[j] = A[j], A[i] A[i+1], A[r] = A[r], A[i+1] return i n = int(input()) A = list(map(int, input().split())) i = partition(A, 0, n-1) str_A = list(map(str,A)) str_A[i+1]...
Given a non-negative decimal integer $x$, convert it to binary representation $b$ of 32 bits. Then, print the result of the following operations to $b$ respecitvely. * Inversion: change the state of each bit to the opposite state * Logical left shift: shift left by 1 * Logical right shift: shift right by 1 Constraint...
{ "input": [ "13", "8", "9", "5", "7", "3", "2", "1", "10", "0", "4", "-1", "-2", "14", "-3", "-4", "-6", "-8", "-10", "-12", "-18", "-9", "-27", "6", "12", "-5", "16", "-7", "24", "-17", "19", ...
x = int(input()) print('{:032b}'.format(x)) print('{:032b}'.format(~x & (2 ** 32 - 1))) print('{:032b}'.format(x << 1 & (2 ** 32 - 1))) print('{:032b}'.format(x >> 1))
You are given a string s consisting of n lowercase Latin letters. n is even. For each position i (1 ≤ i ≤ n) in string s you are required to change the letter on this position either to the previous letter in alphabetic order or to the next one (letters 'a' and 'z' have only one of these options). Letter in every posi...
{ "input": [ "5\n6\nabccba\n2\ncf\n4\nadfa\n8\nabaazaba\n2\nml\n", "1\n4\nyaaa\n", "1\n6\ndazbyd\n", "4\n2\nay\n2\nbz\n2\nya\n2\nzb\n", "1\n4\nauux\n", "3\n2\nab\n2\nbc\n26\nabcdefghijklmnopqrstuvwxyz\n", "1\n2\nba\n", "2\n2\nzb\n2\nza\n", "3\n2\nbz\n2\nyz\n2\nab\n", "1\n4\nauu...
def get_mask(inp): return(5 << ord(inp) - ord('a')) n = int(input()) for i in range(0, n): input() st = input() ls = [] for j in st: ls.append(get_mask(j)) for j in range(0, len(ls) // 2): if(ls[j] & ls[-1 * (j + 1)] == 0): print("NO") break else...
Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map. The treasure map can be represented as a rectangle n × m in size. Each cell stands for an islands' square (the square's side length equals...
{ "input": [ "3 4\n####\n#.A#\n####\n2\nW 1\nN 2\n", "6 10\n##########\n#K#..#####\n#.#..##.##\n#..L.#...#\n###D###A.#\n##########\n4\nN 2\nS 1\nE 1\nW 2\n", "4 9\n#########\n#AB#CDEF#\n#.......#\n#########\n1\nW 3\n", "10 19\n###################\n#######QR..H#######\n#M##O...G....U#####\n#.##AK#Z.......
#!/usr/bin/env python3 from sys import stdin n, m = map(int, stdin.readline().rstrip().split()) island = [] pos = {} for i in range(n): island.append(stdin.readline().rstrip()) for j, c in enumerate(island[i]): if c >= 'A' and c <= 'Z': pos[c] = [i, j] l_reach = [[-1 for j in range(m)] fo...
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well. The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes th...
{ "input": [ "6\n", "16\n", "536870912\n", "88888888\n", "798324712\n", "800000000\n", "669278610\n", "999999999\n", "900000000\n", "25\n", "551350800\n", "735134400\n", "11\n", "5\n", "14732891\n", "4\n", "17239871\n", "400000000\n", "36\n",...
n=int(input()) a={1} for i in range(2,int(n**0.5)+1): if n%i==0: a.add(i) a.add(n//i) ans=[1] a=list(a) for i in a: term=n//i ans.append((term*(2+(term-1)*i))//2) ans.sort() print(*ans)
You are given an integer n (n ≥ 0) represented with k digits in base (radix) b. So, $$$n = a_1 ⋅ b^{k-1} + a_2 ⋅ b^{k-2} + … a_{k-1} ⋅ b + a_k.$$$ For example, if b=17, k=3 and a=[11, 15, 7] then n=11⋅17^2+15⋅17+7=3179+255+7=3441. Determine whether n is even or odd. Input The first line contains two integers b and...
{ "input": [ "13 3\n3 2 7\n", "99 5\n32 92 85 74 4\n", "2 2\n1 0\n", "10 9\n1 2 3 4 5 6 7 8 9\n", "6 1\n0\n", "10 3\n4 4 4\n", "99 20\n97 97 97 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 98 97\n", "99 10\n97 97 97 97 97 97 97 97 97 97\n", "97 21\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
b,k=map(int,input().split()) arr=list(map(int,input().split())) arr.insert(0,0) s=0 for i in range(1,len(arr)): s=s+(arr[-i]*pow(b,i-1,1000000000)) if(s&1): print("odd") else: print("even")
Let s be some string consisting of symbols "0" or "1". Let's call a string t a substring of string s, if there exists such number 1 ≤ l ≤ |s| - |t| + 1 that t = s_l s_{l+1} … s_{l + |t| - 1}. Let's call a substring t of string s unique, if there exist only one such l. For example, let s = "1010111". A string t = "010...
{ "input": [ "4 4\n", "7 3\n", "5 3\n", "101 3\n", "2 2\n", "100000 32194\n", "99999 33333\n", "5 5\n", "99991 70227\n", "100000 4\n", "3 1\n", "22359 1593\n", "1 1\n", "99999 1\n", "99999 50719\n", "3 3\n", "79 61\n", "88759 13661\n", "10000...
# ========= /\ /| |====/| # | / \ | | / | # | /____\ | | / | # | / \ | | / | # ========= / \ ===== |/====| # code if __name__ == "__main__": n,k = map(int,input().split()) a = (n - k)//2 ps = '0'*a + '1' s =...
While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid. A 1-st order rhombus is just a square 1 × 1 (i.e just a cell). A n-th order rhombus for all n ≥ 2 one obtains from a n-1-th order rhombus adding all cells which have a common side with it to it (look ...
{ "input": [ "3\n", "1\n", "2\n", "37\n", "25\n", "64\n", "72\n", "11\n", "22\n", "8\n", "34\n", "38\n", "94\n", "77\n", "15\n", "49\n", "86\n", "57\n", "68\n", "98\n", "50\n", "75\n", "16\n", "80\n", "62\n", "47\n...
n = int(input()) print(sum(range(n)) * 4 + 1)
For years, the Day of city N was held in the most rainy day of summer. New mayor decided to break this tradition and select a not-so-rainy day for the celebration. The mayor knows the weather forecast for the n days of summer. On the i-th day, a_i millimeters of rain will fall. All values a_i are distinct. The mayor k...
{ "input": [ "5 5 5\n100000 10000 1000 100 10\n", "10 2 2\n10 9 6 7 8 3 2 1 4 5\n", "10 2 3\n10 9 6 7 8 3 2 1 4 5\n", "100 7 4\n100 99 98 97 96 95 94 70 88 73 63 40 86 28 20 39 74 82 87 62 46 85 58 49 89 19 17 55 90 2 1 4 69 77 35 76 79 31 78 91 26 47 64 67 68 23 37 10 72 59 54 80 14 93 27 38 24 18 51...
import sys,heapq,math from collections import deque,defaultdict printn = lambda x: sys.stdout.write(x) inn = lambda : int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) DBG = True # and False R = 10**9 + 7 def ddprint(x): if DBG: print(x) n,x,y = inm() a ...
In addition to complaints about lighting, a lot of complaints about insufficient radio signal covering has been received by Bertown city hall recently. n complaints were sent to the mayor, all of which are suspiciosly similar to each other: in the i-th complaint, one of the radio fans has mentioned that the signals of ...
{ "input": [ "2 4 4 2\n1 3\n2 3\n1 4\n1 2\n3 4\n1 4\n1 2\n3 4\n", "2 4 4 2\n1 3\n2 4\n1 2\n1 2\n3 4\n3 4\n1 2\n3 4\n", "10 15 50 10\n6 8\n10 15\n9 12\n1 8\n4 8\n1 12\n14 15\n4 7\n5 9\n7 11\n7 50\n4 41\n10 50\n5 33\n10 26\n14 42\n8 37\n4 45\n5 48\n1 27\n1 8\n4 25\n3 29\n4 19\n3 36\n8 13\n2 5\n5 7\n4 6\n3 7...
# ------------------- fast io -------------------- import os import sys input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- def find_SCC(graph): SCC, S, P = [], [], [] depth = [0] * len(graph) stack = list(range(len(graph))) while stack: ...
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area. To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of n r...
{ "input": [ "2 3\n", "2 1\n", "3 6\n", "2 5\n", "1 2\n", "100000 100000\n", "1 3\n", "1 100000\n", "91697 91697\n", "1 99999\n", "100000 1\n", "91248 82914\n", "1 1\n", "52702 64157\n", "99998 1\n", "99411 90913\n", "99999 1\n", "3 1\n", "1 ...
n, m = map(int, input().split()) mod = 10**9+7 a = [] a.append(0) a.append(2) a.append(4) for i in range(3, max(n, m)+1): a.append((a[i-1]+a[i-2])%mod) print((a[m]-2 + a[n])%mod)
Let's call an array t dominated by value v in the next situation. At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v) > occ(v') for any other number v'. For example, arrays [1...
{ "input": [ "4\n1\n1\n6\n1 2 3 4 5 1\n9\n4 1 2 4 5 4 3 2 1\n4\n3 3 3 3\n", "1\n11\n1 1 1 1 1 1 1 1 1 1 1\n", "1\n12\n1 1 1 1 1 1 1 1 1 1 1 1\n", "1\n2\n2 2\n", "1\n11\n1 1 1 2 1 1 1 1 1 1 1\n", "4\n1\n1\n6\n1 2 3 4 5 2\n9\n4 1 2 4 5 4 3 2 1\n4\n3 3 3 3\n", "4\n1\n1\n6\n1 2 3 4 5 2\n9\n4 1...
import collections ii = lambda: int(input()) iia = lambda: list(map(int,input().split())) isa = lambda: list(input().split()) t = ii() for i in range(t): n = ii() x = iia() if(n==1): print('-1') else: dist = collections.defaultdict(lambda : n) last = collections.defaultdict(lambda : -1) mini = n flag = ...
Anu has created her own function f: f(x, y) = (x | y) - y where | denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR). For example, f(11, 6) = (11|6) - 6 = 15 - 6 = 9. It can be proved that for any nonnegative numbers x and y value of f(x, y) is also nonnegative. She would like to r...
{ "input": [ "1\n13\n", "4\n4 0 11 6\n", "5\n108499762 843249791 917433490 622172778 725664656\n", "6\n787704054 796688644 754725914 561572949 573891604 312543334\n", "4\n433189692 522048869 125182076 157942182\n", "5\n809571641 29322377 935888946 833709370 2457463\n", "2\n151282707 316934...
import sys,os,io input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline n = int(input()) a = [int(i) for i in input().split()] m = 0 mi = 0 b = [-1]*(35) for i in range (len(a)): bi = bin(a[i])[2:][::-1] for j in range (len(bi)): if bi[j]=='1': if b[j]==-1: b[j] = ...
The next lecture in a high school requires two topics to be discussed. The i-th topic is interesting by a_i units for the teacher and by b_i units for the students. The pair of topics i and j (i < j) is called good if a_i + a_j > b_i + b_j (i.e. it is more interesting for the teacher). Your task is to find the number...
{ "input": [ "4\n1 3 2 4\n1 3 2 4\n", "5\n4 8 2 6 2\n4 5 4 1 3\n", "4\n289148443 478308391 848805621 903326233\n164110294 114400164 187849556 86077714\n", "2\n335756331 677790822\n388993348 109229235\n", "7\n207890396 779834136 486233939 78683313 737666916 867859889 430916890\n641454151 744134965 ...
R = lambda:map(int,input().split()) n = int(input()) a = list(R()) b = list(R()) dif = [(b[i] - a[i], 1) for i in range(n)] for x in dif: if x[0] < 0: dif.append((-x[0], 0)) dif = sorted(dif) count = 0 ans = 0 for i in range(1, len(dif)): if dif[i][1] == 1: count += 1 if dif[i][1] == 0: an...
Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1, 2, 1, 3, 1, 2, 1], then possible subsequences are: [1, 1, 1, 1], [3] and [1, 2, 1, 3, 1, 2, 1], but not [3, 2, 3] and [...
{ "input": [ "4\n5\n1 2 3 -1 -2\n4\n-1 -2 -1 -3\n10\n-2 8 3 8 -4 -15 5 -2 -3 1\n6\n1 -1000000000 1 -1000000000 1 -1000000000\n", "2\n2\n-1 -1\n1\n-2\n", "10\n10\n-1 5 6 2 -8 -7 -6 5 -3 -1\n10\n1 2 3 4 5 6 7 8 9 10\n1\n6\n1\n-7\n11\n5 -4 1 2 3 -5 -7 -10 -2 1 12\n4\n-4 -5 -6 1\n7\n1 2 6 3 2 -6 -2\n5\n-1 -5 ...
t = int(input()) for tt in range(t): n = int(input()) arr = list(map(int,input().split())) mx = arr[0] sm = 0 for j in range(n): if (arr[j] * mx < 0 ): sm += mx mx = arr[j] else: mx = max(mx , arr[j]) print(sm + mx)
Vivek has encountered a problem. He has a maze that can be represented as an n × m grid. Each of the grid cells may represent the following: * Empty — '.' * Wall — '#' * Good person — 'G' * Bad person — 'B' The only escape from the maze is at cell (n, m). A person can move to a cell only if it shares a...
{ "input": [ "6\n1 1\n.\n1 2\nG.\n2 2\n#B\nG.\n2 3\nG.#\nB#.\n3 3\n#B.\n#..\nGG.\n2 2\n#B\nB.\n", "5\n2 5\nB.GGG\n.B.G.\n3 6\nBB#.GG\nB.#B.G\n.#.#G.\n3 2\n..\n..\n..\n1 4\nBBB.\n4 9\n###B##.B#\n.####.##G\n##.###.#.\n...B#.B#.\n", "1\n1 1\n.\n", "4\n1 2\nG.\n1 2\n#.\n1 2\n..\n1 2\nB.\n", "6\n1 1\n....
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode...
You are given a string s[1 ... n] consisting of lowercase Latin letters. It is guaranteed that n = 2^k for some integer k ≥ 0. The string s[1 ... n] is called c-good if at least one of the following three conditions is satisfied: * The length of s is 1, and it consists of the character c (i.e. s_1=c); * The lengt...
{ "input": [ "6\n8\nbbdcaaaa\n8\nasdfghjk\n8\nceaaaabb\n8\nbbaaddcc\n1\nz\n2\nac\n", "6\n8\nbbdcaaaa\n8\nasdfghjk\n8\nceaaaabb\n8\nbbaaddcc\n1\nz\n2\nbc\n", "6\n8\nbbdcaaaa\n8\nasdfghjk\n8\nceaaaacb\n8\nbbaaddcc\n1\ny\n2\nbc\n", "6\n8\nbbdcaaaa\n8\nasdfghjk\n8\nbbaaaaec\n8\nbbaadccc\n1\nz\n2\nac\n", ...
import sys input = sys.stdin.readline def print(val): sys.stdout.write(str(val) + '\n') def solve(s,l,r,c): if l+1 == r: return int(s[l] != c) replace1 = replace2 = 0 for i in range(l,(l+r)//2): if s[i] != c: replace1 += 1 for i in range((l+r)//2, r): if s[i] != c...
Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace "Zmey-Gorynych", and offers Alex a job: if he shows his pro...
{ "input": [ "7\n2\n2 5\n4\n1 8 2 3\n3\n3 8 9\n5\n64 25 75 100 50\n1\n42\n6\n96 128 88 80 52 7\n5\n2 4 8 16 17\n", "1\n32\n660 510 840 483 805 140 280 476 474 70 120 728 449 210 111 645 105 580 420 84 798 520 132 168 814 329 552 145 332 602 296 430\n", "1\n16\n1000 50 643 72 726 500 250 790 925 700 600 57...
import sys,bisect,string,math,time,functools,random,fractions from heapq import heappush,heappop,heapify from collections import deque,defaultdict,Counter from itertools import permutations,combinations,groupby rep=range;R=range def Golf():n,*t=map(int,open(0).read().split()) def I():return int(input()) def S_():return...
Igor had a sequence d_1, d_2, ..., d_n of integers. When Igor entered the classroom there was an integer x written on the blackboard. Igor generated sequence p using the following algorithm: 1. initially, p = [x]; 2. for each 1 ≤ i ≤ n he did the following operation |d_i| times: * if d_i ≥ 0, then he looke...
{ "input": [ "3\n100\n5 -3 6\n", "3\n3\n1 -1 2\n", "5\n34\n1337 -146 42 -69 228\n", "3\n1\n999999999 0 1000000000\n", "50\n834035286\n1000000000 1000000000 1000000000 244981634 0 -1000000000 -208185761 0 0 0 1000000000 126125866 0 0 0 -385821645 0 0 0 0 1000000000 1000000000 1000000000 974558309 0...
import sys, io, os if os.environ['USERNAME']=='kissz': inp=open('in55.txt','r').readline def debug(*args): print(*args,file=sys.stderr) else: inp=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def debug(*args): pass def mexp(size,power): A=[] for i in range(size): ...
Several months later Alex finally got his brother Bob's creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the progr...
{ "input": [ "0 0 0 3\n2 0 0 0\n2 2 2 0\n0 2 2 2\n", "1 1 6 1\n1 0 6 0\n6 0 6 1\n1 1 1 0\n", "2 -1 -2 -1\n2 -1 2 -1\n2 -1 -2 -1\n2 -1 2 -1\n", "0 0 0 1\n0 1 0 1\n0 1 0 0\n0 1 0 1\n", "0 0 2 2\n0 0 2 0\n2 2 2 2\n0 2 0 2\n", "130120899 550158649 130120899 831843953\n130120899 550158649 130120899...
def is_rect(es): v = set([]) for e in es: if not ((e[0] == e[2]) or (e[1] == e[3])): return False v.add((e[0], e[1])) v.add((e[2], e[3])) if len(v) != 4: return False xs = set([]) ys = set([]) for vi in v: xs.add(vi[0]) ys.add(vi[1]...
As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answe...
{ "input": [ "4 1\n+2\n-3\n+4\n-1\n", "1 1\n+1\n", "3 2\n-1\n-2\n-3\n", "10 4\n-8\n+1\n-6\n-10\n+5\n-6\n-8\n-8\n-4\n-8\n", "28 12\n+10\n-7\n+17\n-20\n+7\n-7\n+13\n-21\n-7\n-7\n-18\n+7\n+7\n+3\n+6\n+14\n+7\n-24\n-21\n-7\n-7\n+4\n+7\n-7\n+21\n-7\n-26\n+7\n", "10 6\n+10\n+10\n+10\n+3\n+10\n+10\n+...
n, m = map(int, input().split()) t = [int(input()) for i in range(n)] s, p = 0, [0] * (n + 1) for i in t: if i < 0: m -= 1 p[-i] -= 1 else: p[i] += 1 q = {i for i in range(1, n + 1) if p[i] == m} if len(q) == 0: print('Not defined\n' * n) elif len(q) == 1: j = q.pop() print('\n'.join(['T...
The Smart Beaver from ABBYY was offered a job of a screenwriter for the ongoing TV series. In particular, he needs to automate the hard decision: which main characters will get married by the end of the series. There are n single men and n single women among the main characters. An opinion poll showed that viewers lik...
{ "input": [ "2 4 3\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n", "2 4 7\n1 1 1\n1 2 2\n2 1 3\n2 2 7\n", "4 16 105\n2 4 15\n1 1 16\n2 2 57\n3 4 31\n1 2 47\n2 3 28\n1 3 70\n4 2 50\n3 1 10\n4 1 11\n4 4 27\n1 4 56\n3 3 28\n3 2 28\n2 1 33\n4 3 63\n", "5 15 90\n2 3 71\n5 1 72\n3 2 29\n2 5 35\n5 4 49\n2 1 5\n3 3 37\n5 2 ...
I=lambda:list(map(int,input().split())) n,k,T=I() t=[I()for _ in '0'*k] def b(h,w,r,a): if h>n:a+=[r] else: b(h+1,w,r,a) for f,s,v in t: if f==h and s in w:b(h+1,w-set([s]),r+v,a) return a print(sorted(b(1,set(range(1,n+1)), 0,[]))[T-1])
Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high. At the beginning of the first experiment there is a single bacterium in the test tub...
{ "input": [ "2 2 4 100\n", "3 1 3 5\n", "1 4 4 7\n", "5 5 2 10\n", "1000000 1000000 1 1\n", "5 5 2 1\n", "1 1 2 2\n", "1 1 1000000 1\n", "2 1 371319 775111\n", "1000000 1 1 1\n", "5 2 68 144841\n", "702841 39 622448 218727\n", "1 1 1 1\n", "1 1 100 2\n", "1...
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUF...
A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least x points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances. ...
{ "input": [ "6 7\n4 3 5 6 4 4\n8 6 0 4 3 4\n", "5 2\n1 1 1 1 1\n1 1 1 1 1\n", "1 100\n56\n44\n", "3 10\n9 9 0\n0 0 10\n", "5 45\n1 2 3 4 5\n10 20 30 40 50\n", "10 26872\n84744 76378 25507 49544 44949 65159 78873 9386 2834 83577\n43277 76228 210 44539 72154 22876 94528 90143 3059 2544\n", ...
from sys import stdin from collections import deque n,x = [int(x) for x in stdin.readline().split()] s1 = deque(sorted([int(x) for x in stdin.readline().split()])) s2 = deque(sorted([int(x) for x in stdin.readline().split()])) place = 0 for score in s1: if s2[-1] + score >= x: place += 1 s2.pop(...
Polycarpus has an array, consisting of n integers a1, a2, ..., an. Polycarpus likes it when numbers in an array match. That's why he wants the array to have as many equal numbers as possible. For that Polycarpus performs the following operation multiple times: * he chooses two elements of the array ai, aj (i ≠ j); ...
{ "input": [ "3\n1 4 1\n", "2\n2 1\n", "6\n-1 1 0 0 -1 -1\n", "4\n2 0 -2 -1\n", "4\n2 -7 -2 -6\n", "100\n968 793 -628 -416 942 -308 977 168 728 -879 952 781 -425 -475 -480 738 -740 142 -319 -116 -701 -183 41 324 -918 -391 -176 781 763 888 475 -617 134 -802 -133 -211 855 -869 -236 503 550 387 1...
x=int(input()) sm=sum(list(map(int,input().split())))%x if sm==0: print(x) else: print(x-1)
The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set Ui = (ui, 1, ui, 2, ..., ui, |Ui|). Here and below we'll presup...
{ "input": [ "11 3\n", "5 2\n", "950059 419028\n", "18 6\n", "13141 3789\n", "2 2\n", "20354 1334\n", "999999 333334\n", "13141 3789\n", "10 2\n", "9 3\n", "20354 1334\n", "6 2\n", "20354 8642\n", "3 2\n", "365561 143151\n", "18 10\n", "37749 133...
import sys; class MyReader: # file = null; def __init__(self): filename = "file.in"; if self.isLocal(): self.file = open(filename); self.str = [""]; self.ind = 1; def isLocal(self): return len(sys.argv) > 1 and sys.argv[1] == "SCHULLZ"; d...
Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not. Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the number...
{ "input": [ "-1 4 15\n", "1 2 5\n", "0 -1 5\n", "-99 -91 523666385\n", "1 -999999999 239239239\n", "-1000000000000000000 -1000000000000000000 -999999999999999999\n", "-100000000000000 1 233\n", "-134 -345 -133\n", "36 -58 428518679\n", "-47 -31 -669106932\n", "171 185 5349...
from sys import stdin from math import ceil x, y, m = map(int, stdin.readline().split()) ans = 0 if x >= m or y >= m: print(ans) elif x + y > min(x, y): ma, mi = max(x, y), min(x, y) ans += ((ma - mi) // ma) # + ((ma - mi) % ma) mi, ma = min(ma, mi + ans * ma), max(ma, mi + ans * ma) while ma < ...
A girl named Xenia has a cupboard that looks like an arc from ahead. The arc is made of a semicircle with radius r (the cupboard's top) and two walls of height h (the cupboard's sides). The cupboard's depth is r, that is, it looks like a rectangle with base r and height h + r from the sides. The figure below shows what...
{ "input": [ "1 1\n", "2 1\n", "1 2\n", "5 3\n", "1526002 6904227\n", "4 1\n", "674098 1358794\n", "5 5\n", "100 87\n", "10000000 8660255\n", "100 199\n", "100 49\n", "2 6904227\n", "6552194 8371814\n", "10000000 866254\n", "2 2\n", "5 10\n", "39...
if __name__=='__main__': inp = input() arr = inp.split(" ") r = int(arr[0]) h = int(arr[1]) ans = 2*(h//r) d = h%r if d*2>=r: ans+=2 if 4*d*d >= 3*r*r: ans+=1 else: ans+=1 print(ans)
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2." Same with some writers, she wants to make an example with some certain output...
{ "input": [ "2", "1", "9", "1000000\n", "999983\n", "9999991\n", "2\n", "73939133\n", "16\n", "9\n", "29\n", "27\n", "29000087\n", "2691939\n", "13567\n", "15485863\n", "666013\n", "10\n", "58376259\n", "41313494\n", "1\n", "1000...
n, m, cnt = int(input()), 148, 0 ans = [['N'] * m for i in range(m)] def edge(i, j): ans[i][j] = ans[j][i] = 'Y' def node(*adj): global cnt i = cnt cnt += 1 for j in adj: edge(i, j) return i start, end, choice = node(), node(), node() if n&1: edge(choice, end) for i in range(1, 30...
Input The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of points on a plane. Each of the next n lines contains two real coordinates xi and yi of the <image> point, specified with exactly 2 fractional digits. All coordinates are between - 1000 and 1000, inclusive. Output Output a single real...
{ "input": [ "8\n16.94 2.42\n15.72 2.38\n14.82 1.58\n14.88 0.50\n15.76 -0.16\n16.86 -0.20\n17.00 0.88\n16.40 0.92\n", "5\n2.26 1.44\n2.28 0.64\n2.30 -0.30\n1.58 0.66\n3.24 0.66\n", "8\n6.98 2.06\n6.40 1.12\n5.98 0.24\n5.54 -0.60\n7.16 0.30\n7.82 1.24\n8.34 0.24\n8.74 -0.76\n", "5\n10.44 2.06\n10.90 0....
n = int(input()) ab = 0 for i in range(n): x, y = map(float, input().split()) ab += y print(5 + ab / n)
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks. Fortunately, Picks remembers something about his set S: * its elements were distinct integers from 1 to limit; * the value of <ima...
{ "input": [ "4 3\n", "5 1\n", "5 5\n", "62658 10881\n", "66947 17657\n", "1997 508\n", "5499 1023\n", "100000 14047\n", "21741 3575\n", "61897 33128\n", "54321 12345\n", "58934 6404\n", "89829 8298\n", "83451 17526\n", "47283 15757\n", "40287 17817\n", ...
s, limit = list(map(int, input().split())) ans = [] for i in range(limit, 0, -1): k = i & (i ^(i-1)) if s >= k: s-=k ans.append(i) if s: print(-1) else: print(len(ans)) print(*ans)
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose...
{ "input": [ "3\n1 2\n2 3\n3 4\n", "2\n1 3\n1 3\n", "10\n1 1\n8 10\n1 7\n6 8\n5 7\n1 9\n8 8\n6 10\n1 4\n3 4\n", "10\n1 4\n1 12\n5 7\n5 5\n2 5\n1 7\n1 10\n7 9\n8 9\n9 11\n", "10\n6 7\n5 11\n5 10\n9 10\n11 12\n6 12\n7 11\n1 1\n5 9\n2 8\n", "10\n6 9\n1 8\n6 12\n8 15\n2 5\n1 2\n7 15\n12 15\n5 12\n...
# http://codeforces.com/contest/45/problem/D from sys import stdin inFile = stdin tokens = [] tokens_next = 0 def next_str(): global tokens, tokens_next while tokens_next >= len(tokens): tokens = inFile.readline().split() tokens_next = 0 tokens_next += 1 return tokens[tokens_next - 1] ...
You are given a n × m field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle). Input...
{ "input": [ "3 4\n*..*\n.**.\n*.**\n", "2 2\n**\n*.\n", "1 3\n*.*\n", "5 2\n*.\n**\n.*\n..\n.*\n", "5 2\n**\n**\n**\n*.\n*.\n", "4 2\n.*\n.*\n..\n..\n", "10 26\n..**..***.**.*.***.*.***.*\n*.*.*..***.*.**..*********\n*.*.*...***.*.*.*.**....*.\n..**.**.....*....***..***.\n**..*******.*..*...
n, m = map(int, input().split()) grid = [input() for _ in range(n)] a = [[0 for _ in range(m)] for i in range(n)] b = [[0 for _ in range(m)] for i in range(n)] for i in range(n): for j in range(m): a[i][j] = a[i][j - 1] + (grid[i][j] == '*') if i: b[i][j] = b[i - 1][j] b[i][j] +=...