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
784_B. Kids' Riddle_465
Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? Input The input contains a single integer n (0 ≤ n ≤ 2000000000). Output Output a single integer. Examples Input 11 Output 2 Input 14 Output 0 Input 61441 Output 2 Input 571576 Output 10 Input 2128506 Output 3
a=str(hex(int(input()))) b=0 for i in range(2,len(a)): if a[i]=="0" or a[i]=="4" or a[i]=="6" or a[i]=="9" or a[i]=="a" or a[i]=="d": b+=1 elif a[i]=="8" or a[i]=="b": b+=2 print(b)
{ "input": [ "14\n", "2128506\n", "11\n", "571576\n", "61441\n", "143165576\n", "1919020031\n", "1204252996\n", "210637432\n", "724264821\n", "1741\n", "619489590\n", "1199537418\n", "1075765759\n", "747976826\n", "638486017\n", "58438190\n", "12...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? Input The input contains a single integer n (0 ≤ n ≤ 2000000000). Output Output a single integer. Exa...
805_A. Fake NP_469
Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path. You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times. Solve...
n, k=map(int ,input().split()) if n==k and n%2==1: print(n) else: print(2)
{ "input": [ "19 29\n", "3 6\n", "252662256 252662260\n", "141650963 141650963\n", "4 12\n", "1002523 1002523\n", "93 95\n", "331900277 331900277\n", "56 92\n", "10 100\n", "3 1000000000\n", "13 13\n", "2 879190747\n", "999900001 1000000000\n", "820844234 89...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Tavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path. You are given l and r. For all integers from l to r,...
830_A. Office Keys_473
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else. You are to determine the mi...
o=lambda:[int(f)for f in input().split()] n,k,p=o() a=sorted(o()) b=sorted(o()) print(min(max(abs(b[i + d] - a[i]) + abs(b[i + d] - p) for i in range(n)) for d in range(k - n + 1)))
{ "input": [ "1 2 10\n11\n15 7\n", "2 4 50\n20 100\n60 10 40 80\n", "1 1 10\n10\n10\n", "2 2 10\n9 11\n11 8\n", "1 1 1000000000\n1000000000\n1\n", "1 1 50\n1\n1000000000\n", "2 2 5\n2 3\n4 6\n", "5 20 1\n314 316 328 323 321\n30 61 11 83 19 63 97 87 14 79 43 57 75 48 47 95 41 27 8 88\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, ta...
851_B. Arpa and an exam about geometry_477
Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a, b, c. Find a point and an angle such that if we rotate the page around the point by the angle, the new position of a is the same as the old position of b, and the new position of b is the same as the old position of c....
ax,ay,bx,by,cx,cy=map(int,input().split()) ab=(ax-bx)**2+(ay-by)**2 bc=(bx-cx)**2+(by-cy)**2 if ab==bc and (ay-by)*(bx-cx)!=(by-cy)*(ax-bx): print("Yes") else: print("No")
{ "input": [ "0 1 1 1 1 0\n", "1 1 0 0 1000 1000\n", "264193194 -448876521 736684426 -633906160 -328597212 -47935734\n", "-357531221 381512519 -761132895 -224448284 328888775 -237692564\n", "-1000000000 -1000000000 0 0 1000000000 999999999\n", "0 2 4 5 4 0\n", "0 0 2 45 0 90\n", "-1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a, b, c. Find a point and an angle such that if we rotate the page around the point ...
920_E. Connected Components?_486
You are given an undirected graph consisting of n vertices and <image> edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that there is no edge between x and y, and if some pair of vertices is not listed in the input, then there is an edge between these vertices. ...
n,m=map(int,input().split()) non=[{i} for i in range(n)] for i in range(m): u,v=map(int,input().split()) u,v=u-1,v-1 non[u].add(v) non[v].add(u) vertex=set(range(n)) ans=[] while(vertex): a=next(iter(vertex)) vertex.remove(a) stk=[a] cou=1 while(stk): v=stk.pop() s=ve...
{ "input": [ "5 5\n1 2\n3 4\n3 2\n4 2\n2 5\n", "7 20\n4 6\n6 7\n4 5\n1 2\n2 4\n1 7\n3 5\n2 1\n6 2\n6 1\n7 3\n3 2\n3 6\n3 1\n3 4\n2 5\n1 6\n7 4\n6 3\n7 5\n", "8 23\n1 2\n1 4\n1 6\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n3 4\n3 5\n3 6\n3 7\n3 8\n4 5\n4 6\n4 7\n5 6\n5 7\n5 8\n6 8\n7 8\n", "4 4\n2 1\n3 1\n1 4\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an undirected graph consisting of n vertices and <image> edges. Instead of giving you the edges that exist in the graph, we give you m unordered pairs (x, y) such that t...
949_C. Data Center Maintenance_489
BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!). Main feature of services offered by BigData Inc. is the access availability guarantee even under the ci...
from collections import deque import sys input = sys.stdin.readline n, m, MOD = map(int, input().split()) u = list(map(int, input().split())) info = [list(map(int, input().split())) for i in range(m)] graph = [[] for i in range(n)] rev_graph = [[] for i in range(n)] set_ = set() INF = 10 ** 9 for a, b in info: a...
{ "input": [ "3 3 5\n4 4 0\n1 3\n3 2\n3 1\n", "4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3\n", "10 9 5\n0 0 0 0 0 0 0 0 0 4\n10 3\n10 7\n10 5\n10 8\n10 9\n10 1\n10 4\n10 6\n10 2\n", "10 20 5\n2 2 1 4 0 3 0 4 1 3\n6 1\n8 5\n2 10\n3 5\n1 9\n4 6\n9 7\n2 3\n7 4\n10 8\n4 9\n2 5\n4 10\n2 8\n10 3\n1 8\n8 10\n6 7...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: BigData Inc. is a corporation that has n data centers indexed from 1 to n that are located all over the world. These data centers provide storage for client data (you can figure out t...
977_B. Two-gram_493
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams. You are given a string s consisting of n capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of ...
n = int(input()) k = str(input()) Max = 0 for i in range(n-1): t=k[i]+k[i+1] z=0 for j in range(0,n-1): s=k[j]+k[j+1] if (s==t): z=z+1 #print(z) if (z>Max): Max=z res=t print(res)
{ "input": [ "5\nZZZAA\n", "7\nABACABA\n", "15\nMIRZOYANOVECLOX\n", "23\nAABBBAAACCCCCAAADDDDDDD\n", "2\nQA\n", "11\nGGRRAATTZZZ\n", "6\nAZAZAZ\n", "10\nSQSQSQSQTG\n", "3\nKEK\n", "100\nURXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECY...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams. You are given a string s consisting of n c...
996_F. Game_497
Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of the game, the variables x_1, x_2, ..., x_n are all set to -1. Each round, with equal probability, one of Allen or Bessie gets to make a mo...
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import time (n, r) = (int(i) for i in input().split()) c = [int(i) for i in input().split()] start = time.time() s = sum(c) n2 = 2**n ans = [s/n2] for i in range(r): (k, new) = (int(i) for i in input().split()) s += new - c[k] c[k] = new ...
{ "input": [ "2 0\n1 1 1 1\n", "2 2\n0 1 2 3\n2 5\n0 4\n", "1 0\n2 3\n", "2 0\n1 1 0 1\n", "2 2\n0 1 2 3\n2 1\n0 4\n", "1 0\n2 1\n", "2 0\n1 0 0 1\n", "2 2\n0 1 2 4\n2 1\n0 4\n", "2 2\n0 1 2 4\n3 1\n0 4\n", "2 2\n0 1 0 4\n3 1\n0 4\n", "2 2\n0 1 0 4\n2 1\n0 4\n", "2 2\n0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Allen and Bessie are playing a simple number game. They both know a function f: \{0, 1\}^n → R, i. e. the function takes n binary arguments and returns a real value. At the start of t...
p02652 AtCoder Grand Contest 045 - 01 Unbalanced_510
Given is a string S, where each character is `0`, `1`, or `?`. Consider making a string S' by replacing each occurrence of `?` with `0` or `1` (we can choose the character for each `?` independently). Let us define the unbalancedness of S' as follows: * (The unbalancedness of S') = \max \\{ The absolute difference be...
from itertools import accumulate S = input() N = len(S) A = [0] + list(accumulate(1 if s == "1" else -1 for s in S)) ma = max(A) cur = A[-1] C = [ma - cur] for a in reversed(A): cur = max(a, cur) C.append(ma - cur) d, e = 0, 0 D, E = A[:], A[:] for i, (s, c) in enumerate(zip(S, reversed(C[:-1])), 1): if...
{ "input": [ "??00????0??0????0?0??00??1???11?1?1???1?11?111???1", "0??0", "0??", "0?0?", "00??", "??00????0??1????0?0??00??1???11?1?1???1?11?111???1", "1???11??11?1???1?1?11???1??00??0?0?1??1??0>>??00??", "1???111?11?1???111?11???1??00??0?0?@?????0????00??", "????111?11?1?1?111?11...
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, where each character is `0`, `1`, or `?`. Consider making a string S' by replacing each occurrence of `?` with `0` or `1` (we can choose the character for each `...
p02781 AtCoder Beginner Contest 154 - Almost Everywhere Zero_514
Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten. Constraints * 1 \leq N < 10^{100} * 1 \leq K \leq 3 Input Input is given from Standard Input in the following format: N K Output Print the count. Examples Input 100 1 Output 19 Inpu...
import functools import sys @functools.lru_cache(None) def doit(n, k): if len(n) == 0 or k < 0: return k == 0 d = int(n[0]) return sum(doit(n[1:] if i == d else '9' * (len(n) - 1), k - 1 if i > 0 else k) for i in range(d + 1)) sys.setrecursionlimit(404) print(doit(input(), int(input())))
{ "input": [ "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999\n3", "314159\n2", "25\n2", "100\n1", "314159\n1", "25\n1", "100\n2", "314159\n3", "999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten. Constraints * 1 \leq N < 10^{100} * 1 \leq K \leq 3 Input ...
p02916 AtCoder Beginner Contest 140 - Buffet_518
Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once. The i-th dish (1 \leq i \leq N) he ate was Dish A_i. When he eats Dish i (1 \leq i \leq N), he gains B_i satisfaction points. Additionally, when he eats Dish i+1 just after eating Dish i (1 \...
N=int(input()) A=list(map(int,input().split())) B=list(map(int,input().split())) C=list(map(int,input().split())) pnt=sum(B) for i in range(N-1): if A[i+1]==A[i]+1: pnt+=C[A[i]-1] print(pnt)
{ "input": [ "2\n1 2\n50 50\n50", "4\n2 3 4 1\n13 5 8 24\n45 9 15", "3\n3 1 2\n2 5 4\n3 6", "2\n1 0\n50 50\n50", "4\n2 3 4 1\n13 5 1 24\n45 9 15", "4\n2 3 4 1\n13 5 1 24\n45 9 16", "4\n2 3 4 1\n14 5 1 24\n45 9 16", "4\n2 3 4 1\n7 5 1 24\n45 9 16", "3\n3 1 2\n2 5 3\n3 6", "4\n2 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Takahashi went to an all-you-can-eat buffet with N kinds of dishes and ate all of them (Dish 1, Dish 2, \ldots, Dish N) once. The i-th dish (1 \leq i \leq N) he ate was Dish A_i. Wh...
p03194 CADDi 2018 for Beginners - Product and GCD_524
There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P. Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N. Constraints * 1 \leq N \leq 10^{12} * 1 \leq P \leq 10^{12} Input Input is...
import math import collections # 試し割法 N, P = map(int, input().split()) def trial_division(n): # 素因数を格納するリスト factor = [] # 2から√n以下の数字で割っていく tmp = int(math.sqrt(n)) + 1 for num in range(2, tmp): while n % num == 0: n //= num factor.append(num) # リストが空ならそれは素数 ...
{ "input": [ "5 1", "4 972439611840", "3 24", "1 111", "5 2", "1 972439611840", "1 110", "1 2", "1 534720932937", "1 18", "1 010", "1 3", "1 867963093279", "1 15", "1 011", "1 5", "1 100", "1 1431379180228", "1 101", "1 243501193440", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N integers a_1, a_2, ..., a_N not less than 1. The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P. Find the maxim...
p03343 AtCoder Regular Contest 098 - Range Minimum Queries_528
You are given an integer sequence A of length N and an integer K. You will perform the following operation on this sequence Q times: * Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one ...
N, K, Q = map(int, input().split()) X = list(map(int, input().split())) r = 10**18 for y in X: tmp = [] tmp2 = [] for x in X: if x < y: tmp.sort() tn = len(tmp) if len(tmp) > K-1: tmp2 += tmp[:tn-K+1] tmp = [] continue ...
{ "input": [ "11 7 5\n24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784", "5 3 2\n4 3 1 5 2", "10 1 6\n1 1 2 3 5 8 13 21 34 55", "11 7 5\n24979445 861648772 623690081 433933447 476190629 262703497 147009374 971407775 628894325 731963982 822804...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an integer sequence A of length N and an integer K. You will perform the following operation on this sequence Q times: * Choose a contiguous subsequence of length K, th...
p03503 AtCoder Beginner Contest 080 - Shopping Street_532
Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those ...
n = int(input()) f = [[int(x) for x in input().split()] for i in range(n)] p = [[int(x) for x in input().split()] for i in range(n)] t={i:0 for i in range(1,2**10)} for i in range(1,2**10): d, b={j:0 for j in range(n)}, format(i, "010b") for j in range(n): for k in range(10): if (i>>k)%2&f[j][k]==1: d[j]+...
{ "input": [ "1\n1 1 0 1 0 0 0 1 0 1\n3 4 5 6 7 8 9 -2 -3 4 -2", "3\n1 1 1 1 1 1 0 0 1 1\n0 1 0 1 1 1 1 0 1 0\n1 0 1 1 0 1 0 1 0 1\n-8 6 -2 -8 -8 4 8 7 -6 2 2\n-9 2 0 1 7 -5 0 -2 -6 5 5\n6 -6 7 -9 6 -5 8 0 -9 -7 -7", "2\n1 1 1 1 1 0 0 0 0 0\n0 0 0 0 0 1 1 1 1 1\n0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1\n0 -2 -2 -2...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Joisino is planning to open a shop in a shopping street. Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop mus...
p03664 AtCoder Regular Contest 078 - Mole and Abandoned Mine_535
Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1 through N and M edges. The i-th edge connects Vertices a_i and b_i, and it costs c_i yen (the currency of Japan) to remove it. Mole would like to remove som...
n, m = map(int, input().split()) g = [[0 for j in range(n)] for i in range(n)] for i in range(m): u, v, w = map(int, input().split()) g[u - 1][v - 1] = g[v - 1][u - 1] = w e = [sum(g[i][j] for i in range(n) if S >> i & 1 for j in range(i + 1, n) if S >> j & 1) for S in range(1 << n)] dp = [[-10 ** 9 for j i...
{ "input": [ "15 22\n8 13 33418\n14 15 55849\n7 10 15207\n4 6 64328\n6 9 86902\n15 7 46978\n8 14 53526\n1 2 8720\n14 12 37748\n8 3 61543\n6 5 32425\n4 11 20932\n3 12 55123\n8 2 45333\n9 12 77796\n3 9 71922\n12 15 70793\n2 4 25485\n11 6 1436\n2 7 81563\n7 11 97843\n3 1 40491", "2 1\n1 2 1", "4 6\n1 2 100\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mole decided to live in an abandoned mine. The structure of the mine is represented by a simple connected undirected graph which consists of N vertices numbered 1 through N and M edge...
p03819 AtCoder Regular Contest 068 - Snuke Line_539
Snuke has decided to play a game, where the player runs a railway company. There are M+1 stations on Snuke Line, numbered 0 through M. A train on Snuke Line stops at station 0 and every d-th station thereafter, where d is a predetermined constant for each train. For example, if d = 3, the train stops at station 0, 3, 6...
class BIT: def __init__(self, n): self.n = n self.bit = [0] * (n + 1) def add(self, k, x): while k <= self.n: self.bit[k] += x k += k & -k def sum(self, k): s = 0 while k > 0: s += self.bit[k] k -= k & -k retur...
{ "input": [ "3 3\n1 2\n2 3\n3 3", "7 9\n1 7\n5 9\n5 7\n5 9\n1 1\n6 8\n3 4", "7 9\n1 9\n5 9\n5 7\n5 9\n1 1\n6 8\n3 4", "7 9\n1 9\n9 9\n5 7\n5 9\n1 1\n6 8\n3 4", "7 11\n1 9\n5 9\n5 7\n5 9\n1 1\n6 8\n3 4", "7 9\n1 9\n9 9\n5 7\n5 9\n1 1\n8 8\n3 4", "7 9\n1 6\n5 13\n5 7\n5 9\n1 1\n6 8\n3 4", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Snuke has decided to play a game, where the player runs a railway company. There are M+1 stations on Snuke Line, numbered 0 through M. A train on Snuke Line stops at station 0 and eve...
p03986 AtCoder Grand Contest 005 - STring_543
We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`. Takahashi, who hates the string `ST`, will perform the following operation 10^{10000} times: * Among the occurrences of `ST` in X as (contiguous) substrings, remove the leftmost one. If there is no occ...
x=input() s=0;t=0 for i in range(len(x)): if x[i]=='S':s+=1 elif s==0:t+=1 else:s-=1 print(s+t)
{ "input": [ "TSTTSS", "SSTTST", "TSSTTTSS", "SSTTTSST", "STTTSS", "SSSTTT", "TTTSSS", "TTTTSSSS", "TSTSST", "SSTTTS", "TSSTST", "STTSST", "TSSSTT", "TTSSST", "TSSTTS", "STSTST", "TSTSTS", "TSSTTSST", "STSTTS", "TTSTSSST", "STTSTS", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have a string X, which has an even number of characters. Half the characters are `S`, and the other half are `T`. Takahashi, who hates the string `ST`, will perform the following ...
p00074 Videotape_547
There is a 120 minute videotape with standard recording. When I set the VCR counter to 00:00:00 with the tape completely rewound and recorded in standard recording mode, I got a certain counter value. Enter this counter value (hours, minutes, seconds), find the length of the remaining tape (recordable time), and create...
def time(sec): h = sec // 3600 ti = sec % 3600 m = ti // 60 s = ti % 60 return [h,m,s] while True: t,h,s = map(int,input().split()) if t == h == s == -1: break sec = t*3600 + h * 60 + s sec = 7200 - sec ans = time(sec) t_ans = time(3 * sec) if ans[2] < 10: ...
{ "input": [ "1 30 0\n-1 -1 -1", "1 36 0\n-1 -1 -1", "0 30 0\n-1 -1 -1", "0 32 0\n-1 -1 -1", "1 16 0\n-1 -1 -1", "0 36 0\n-1 -1 -1", "0 22 0\n-1 -1 -1", "0 55 0\n-1 -1 -1", "0 22 1\n-1 -1 -1", "0 31 1\n-1 -1 -1", "0 26 1\n-1 -1 -1", "0 9 1\n-1 -1 -1", "1 36 1\n-1 -1...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a 120 minute videotape with standard recording. When I set the VCR counter to 00:00:00 with the tape completely rewound and recorded in standard recording mode, I got a certa...
p00206 Next Trip_551
You want to go on a trip with a friend. However, friends who have a habit of spending money cannot easily save travel expenses. I don't know when my friends will go on a trip if they continue their current lives. So, if you want to travel early, you decide to create a program to help your friends save in a planned mann...
# -*- coding: utf-8 -*- """ http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0206 """ import sys from sys import stdin input = stdin.readline def main(args): while True: L = int(input()) if L == 0: break ans = 'NA' for i in range(1, 12+1): if ans ==...
{ "input": [ "10000\n5000 3150\n5000 5000\n0 0\n5000 1050\n5000 3980\n5000 210\n5000 5000\n5000 5000\n0 0\n5000 2100\n5000 2100\n5000 2100\n29170\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n100000 100000\n10...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You want to go on a trip with a friend. However, friends who have a habit of spending money cannot easily save travel expenses. I don't know when my friends will go on a trip if they ...
p00365 Age Difference_554
A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages w...
y1,m1,d1 = map(int, input().split()) y2,m2,d2 = map(int, input().split()) if y1 > y2 or (y1 == y2 and (m1 > m2 or ( m1 == m2 and d1 > d2 ))): y1, y2 = y2, y1 m1, m2 = m2, m1 d1, d2 = d2, d1 if m1 < m2 or (m1 == m2 and d1 < d2): print(y2 - y1 + 1) else: print(y2 - y1)
{ "input": [ "2008 2 29\n2015 3 1", "1999 9 9\n2001 11 3", "2008 2 29\n2015 1 1", "1505 9 9\n2001 11 3", "1505 9 9\n2619 11 3", "1505 13 9\n2619 11 3", "573 1 44\n2015 1 1", "595 2 44\n2015 2 1", "597 2 44\n2015 2 1", "246 21 0\n2619 11 5", "246 21 0\n5129 11 5", "431 2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in...
p00720 Earth Observation with a Mobile Robot Team_558
A new type of mobile robot has been developed for environmental earth observation. It moves around on the ground, acquiring and recording various sorts of observational data using high precision sensors. Robots of this type have short range wireless communication devices and can exchange observational data with ones ne...
from heapq import heappush, heappop import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): N, T, R = map(int, readline().split()) if N == T == R == 0: return False S = [None]*N TS = [None]*N for i in range(N): s = readline().strip() S[i] = s pr...
{ "input": [ "3 5 10\nred\n0 0 0\n5 0 0\ngreen\n0 5 5\n5 6 1\nblue\n0 40 5\n5 0 0\n3 10 5\natom\n0 47 32\n5 -10 -7\n10 1 0\npluto\n0 0 0\n7 0 0\n10 3 3\ngesicht\n0 25 7\n5 -7 -2\n10 -1 10\n4 100 7\nimpulse\n0 -500 0\n100 10 1\nfreedom\n0 -491 0\n100 9 2\ndestiny\n0 -472 0\n100 7 4\nstrike\n0 -482 0\n100 8 3\n0 0 ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A new type of mobile robot has been developed for environmental earth observation. It moves around on the ground, acquiring and recording various sorts of observational data using hig...
p00991 Grid_562
Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1) is 1. And. You can also move between (e, c-1) and (e, 0), and between (r-1, f) and (0, f) at a cost of 1. At this time, find the number...
# Edit: 2014/09/17 # Lang: Python3 # Time: 00.04s from math import factorial if __name__ == "__main__": r, c, ar, ac, br, bc = map(int, input().strip("\n").split(" ")) maxans = 100000007 # 100,000,007 # tate Row dr = min(abs(br - ar), r - abs(br - ar)) if 2 * dr == r: gainr = 2 else:...
{ "input": [ "500 500 0 0 200 200", "4 4 0 0 1 1", "4 4 0 0 3 3", "2 3 0 0 1 2", "500 500 0 0 107 200", "4 1 0 0 1 1", "4 4 1 0 3 3", "2 3 0 0 1 1", "500 500 0 0 107 239", "2 1 0 0 1 1", "678 500 0 0 107 168", "500 500 0 0 200 277", "500 500 1 0 107 239", "678 3...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Two coordinates (a1, a2) and (b1, b2) on a two-dimensional grid of r × c are given. The cost of moving from a cell (e, f) to one of the cells (e + 1, f), (e-1, f), (e, f + 1), (e, f-1...
p01422 Beautiful Currency_568
KM country has N kinds of coins and each coin has its value a_i. The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beautiful by changing the values of some (possibly no) coins. A currency system is called beautiful if each coin has an integer value and the...
# coding:utf-8 import sys input = sys.stdin.readline INF = float('inf') MOD = 10 ** 9 + 7 def inpl(): return list(map(int, input().split())) def solve(N): A = inpl() dp = [INF] * (A[0] * 2) for i in range(A[0]//2, A[0]*2): dp[i] = abs(i - A[0]) / A[0] # A[0]の価格を変えたときのconfusion ratio for...
{ "input": [ "3\n6 11 24", "3\n6 11 30", "3\n6 11 12", "3\n6 13 24", "3\n6 4 30", "3\n12 11 12", "3\n6 20 24", "3\n6 7 30", "3\n11 20 24", "3\n10 7 30", "3\n1 15 12", "3\n8 7 30", "3\n1 1 12", "3\n11 12 15", "3\n8 7 11", "3\n14 14 26", "3\n7 15 28", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: KM country has N kinds of coins and each coin has its value a_i. The king of the country, Kita_masa, thought that the current currency system is poor, and he decided to make it beaut...
p02298 Is-Convex_578
For a given polygon g, print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a sequence of points p1, p2,..., pn where line segments connecting pi and pi+1 (1 ≤ i ≤ n-1) are sides of the polygon. The line segment con...
from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inp(): return int(input()) def inpl(): return list(map(int, input().split())) def inpl_str(): return list(input().split()) #######################...
{ "input": [ "5\n0 0\n2 0\n1 1\n2 2\n0 2", "4\n0 0\n3 1\n2 3\n0 3", "5\n0 0\n2 1\n1 1\n2 2\n0 2", "4\n0 0\n3 0\n2 1\n0 3", "4\n0 0\n3 1\n2 1\n0 3", "5\n0 0\n3 1\n1 1\n2 2\n0 2", "5\n0 0\n3 1\n1 1\n3 2\n0 2", "4\n0 0\n3 0\n2 0\n0 3", "5\n0 0\n3 1\n1 1\n3 2\n1 2", "4\n0 0\n3 1\n2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: For a given polygon g, print "1" if g is a convex polygon, "0" otherwise. Here, in a convex polygon, all interior angles are less than or equal to 180 degrees. g is represented by a ...
p02445 Swap_581
Write a program which reads a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and swap specified elements by a list of the following operation: * swapRange($b, e, t$): For each integer $k$ ($0 \leq k < (e - b)$, swap element $(b + k)$ and element $(t + k)$. Constraints * $1 \leq n \leq 1,000$ * $-1,000,000,0...
n = int(input()) num = list(map(int, input().split())) q = int(input()) for _ in range(q): b, e, t = map(int, input().split()) for i in range(e-b): num[b+i], num[t+i] = num[t+i], num[b+i] print(' '.join(str(n) for n in num))
{ "input": [ "11\n1 2 3 4 5 6 7 8 9 10 11\n1\n1 4 7", "11\n1 2 3 4 5 6 7 8 9 10 11\n2\n1 4 7", "11\n1 2 3 4 5 6 2 8 9 10 11\n2\n1 4 1", "11\n1 2 3 3 5 6 2 8 9 10 11\n2\n1 4 1", "11\n1 2 3 3 5 6 2 8 18 10 11\n2\n1 4 1", "11\n1 0 3 3 5 6 2 8 18 10 11\n2\n1 4 1", "11\n1 0 3 3 5 6 2 8 34 10 11...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Write a program which reads a sequence of integers $A = \\{a_0, a_1, ..., a_{n-1}\\}$ and swap specified elements by a list of the following operation: * swapRange($b, e, t$): For ea...
1012_B. Chemical table_590
Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each element can be described by its coordinates (r, c) (1 ≤ r ≤ n, 1 ≤ c ≤ m) in the table. Recently scientists discovered that for every four...
class UnionFind: def __init__(self, n): self.par = [-1]*n self.rank = [0]*n def Find(self, x): if self.par[x] < 0: return x else: self.par[x] = self.Find(self.par[x]) return self.par[x] def Unite(self, x, y): x = self.Find(x) ...
{ "input": [ "1 5 3\n1 3\n1 1\n1 5\n", "2 2 3\n1 2\n2 2\n2 1\n", "4 3 6\n1 2\n1 3\n2 2\n2 3\n3 1\n3 3\n", "20 20 20\n18 16\n4 20\n2 5\n7 4\n11 13\n6 10\n20 8\n14 6\n3 12\n5 1\n16 7\n10 9\n1 11\n12 18\n19 15\n13 19\n17 3\n9 17\n15 2\n8 14\n", "20 20 1\n17 13\n", "1 1 0\n", "10000 9999 1\n57...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Innopolis University scientists continue to investigate the periodic table. There are n·m known elements and they form a periodic table: a rectangle with n rows and m columns. Each el...
1037_B. Reach Median_594
You are given an array a of n integers and an integer s. It is guaranteed that n is odd. In one operation you can either increase or decrease any single element by one. Calculate the minimum number of operations required to make the median of the array being equal to s. The median of the array with odd length is the ...
# -*- coding: utf-8 -*- # @Date : 2018-09-03 08:46:01 # @Author : raj lath (oorja.halt@gmail.com) # @Link : http://codeforces.com/contest/1037/problem/B # @Version : 1.0.0 import os from sys import stdin max_val=int(10e12) min_val=int(-10e12) def read_int() : return int(stdin.readline()) def read_ints() ...
{ "input": [ "3 8\n6 5 8\n", "7 20\n21 15 12 11 20 19 12\n", "3 1\n1 2 5\n", "1 1\n100000\n", "5 1\n2 2 4 6 1\n", "3 10\n5 5 10\n", "1 1\n1\n", "3 4\n1 2 5\n", "1 100\n88\n", "1 100\n105\n", "3 2\n1 2 5\n", "3 10\n5 3 10\n", "3 4\n0 2 5\n", "1 100\n5\n", "7 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a of n integers and an integer s. It is guaranteed that n is odd. In one operation you can either increase or decrease any single element by one. Calculate the...
105_C. Item World_598
Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def) and resistance to different types of impact (res). Each item belongs to one class. In this problem we will only consider three of such...
# written with help of failed tests def searchBest(iType, number, rType, countResidents): global items, equipped best = 0 ret = None for item, params in items.items(): if params[0] == iType: val = int(params[number]) if countResidents: for resid in equippe...
{ "input": [ "4\nsword weapon 10 2 3 2\npagstarmor armor 0 15 3 1\niceorb orb 3 2 13 2\nlongbow weapon 9 1 2 1\n6\nmike gladiator 5 longbow\nbobby sentry 6 pagstarmor\npetr gladiator 7 iceorb\nteddy physician 6 sword\nblackjack sentry 8 sword\njoe physician 6 iceorb\n", "4\nsword weapon 10 2 3 2\npagstarmor a...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Each item in the game has a level. The higher the level is, the higher basic parameters the item has. We shall consider only the following basic parameters: attack (atk), defense (def...
1081_D. Maximum Distance_602
Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago. You are given a connected undirected graph with n vertices and m weighted edges. There are k special vertices: x_1, x_2, …, x_k. Let's define the cost of the path as the maximum weight of the edges in it. And th...
""" @author: phamv """ ####Function Definition def find(x): while f[x] != x : f[x] = f[f[x]] x = f[x] return x def merge(u, v) : u, v = map(find, (u, v)) f[u] = v; if u == v: return False ret = s[u] > 0 and s[v] > 0 s[v] += s[u] return ret ############### n, m...
{ "input": [ "4 5 3\n1 2 3\n1 2 5\n4 2 1\n2 3 2\n1 4 4\n1 3 3\n", "2 3 2\n2 1\n1 2 3\n1 2 2\n2 2 1\n", "4 4 3\n1 2 3\n1 2 1\n2 3 2\n1 3 3\n1 4 4\n", "4 3 2\n1 4\n1 2 1\n2 3 5\n3 4 1\n", "3 2 2\n1 2\n1 2 1\n2 3 47\n", "3 2 2\n1 2\n1 2 1\n2 3 1000\n", "3 2 2\n1 2\n1 2 10\n2 3 100\n", "3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago. You are given a connected undirected graph with n vertices and m weighted e...
1129_A2. Toy Train_608
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit afte...
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init...
{ "input": [ "2 3\n1 2\n1 2\n1 2\n", "5 7\n2 4\n5 1\n2 3\n3 4\n4 1\n5 3\n3 5\n", "5 1\n3 2\n", "3 3\n1 2\n1 2\n1 2\n", "5 3\n2 4\n5 4\n3 2\n", "20 5\n3 12\n5 20\n16 4\n13 3\n9 14\n", "3 2\n3 1\n1 3\n", "3 1\n3 1\n", "3 2\n1 3\n2 1\n", "50 20\n4 18\n39 33\n49 32\n7 32\n38 1\n46 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a t...
1149_B. Three Religions_611
During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace. The Word of Uni...
n, q = map(int, input().split()) s = '!' + input() nxt = [[n + 1] * (n + 2) for _ in range(26)] for i in range(n - 1, -1, -1): c = ord(s[i + 1]) - 97 for j in range(26): nxt[j][i] = nxt[j][i + 1] nxt[c][i] = i + 1 w = [[-1], [-1], [-1]] idx = lambda i, j, k: i * 65536 + j * 256 + k dp = [0] * (256...
{ "input": [ "6 8\nabdabc\n+ 1 a\n+ 1 d\n+ 2 b\n+ 2 c\n+ 3 a\n+ 3 b\n+ 1 c\n- 2\n", "6 8\nabbaab\n+ 1 a\n+ 2 a\n+ 3 a\n+ 1 b\n+ 2 b\n+ 3 b\n- 1\n+ 2 z\n", "1 1\nt\n+ 2 p\n", "2 12\naa\n+ 1 a\n+ 2 a\n+ 3 a\n- 1\n+ 1 a\n- 2\n+ 2 a\n- 3\n+ 3 a\n+ 2 a\n- 1\n- 3\n", "2 10\nuh\n+ 1 h\n+ 2 u\n+ 3 h\n- 1\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information...
1189_A. Keanu Reeves_615
After playing Neo in the legendary "Matrix" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem. Let's call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For exa...
n = int(input()) s = str(input()) c = 0 for i in range(len(s)): if s[i]=='0': c+=1 else: c-=1 if c!=0: print("1"+ "\n" + s) else: print("2" + "\n" + s[0:-1],s[-1])
{ "input": [ "1\n1\n", "6\n100011\n", "2\n10\n", "72\n111101100111001110000000100010100000011011100110001010111010101011111100\n", "3\n101\n", "18\n101111001111000110\n", "7\n1111000\n", "3\n010\n", "12\n101010100101\n", "1\n0\n", "4\n1010\n", "6\n101100\n", "100\n0...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After playing Neo in the legendary "Matrix" trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the fol...
1208_A. XORinacci_619
Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that he named XORinacci numbers. He defined them as follows: * f(0) = a; * f(1) = b; * f(n) = f(n-1) ⊕ f(n-2) when n > 1, where ⊕ de...
import sys from collections import defaultdict as dd from collections import deque from functools import * from fractions import Fraction as f from copy import * from bisect import * from heapq import * from math import * from itertools import permutations ,product def eprint(*args): print(*args, file=sys.stderr...
{ "input": [ "3\n3 4 2\n4 5 0\n325 265 1231232\n", "10\n669924290 408119795 804030560\n663737793 250734602 29671646\n431160679 146708815 289491233\n189259304 606497663 379372476\n707829111 49504411 81710658\n54555019 65618101 626948607\n578351356 288589794 974275296\n400531973 205638174 323247740\n219131617 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Cengiz recently learned Fibonacci numbers and now he is studying different algorithms to find them. After getting bored of reading them, he came with his own new type of numbers that ...
1227_A. Math Problem_623
Your math teacher gave you the following problem: There are n segments on the x-axis, [l_1; r_1], [l_2; r_2], …, [l_n; r_n]. The segment [l; r] includes the bounds, i.e. it is a set of such x that l ≤ x ≤ r. The length of the segment [l; r] is equal to r - l. Two segments [a; b] and [c; d] have a common point (inters...
t = int(input()) for i in range(t): n = int(input()) x = [] y = [] for i in range(n): a, b = map(int, input().split()) x.append(a) y.append(b) if n == 1: print(0) elif min(y) > max(x): print(0) else: print(abs(max(x)-min(y)))
{ "input": [ "4\n3\n4 5\n5 9\n7 7\n5\n11 19\n4 17\n16 16\n3 12\n14 17\n1\n1 10\n1\n1 1\n", "1\n2\n999999997 999999998\n999999999 1000000000\n", "4\n1\n1 1000000000\n5\n1 1\n12 18\n1000000000 1000000000\n1 1\n8888888 88888888\n5\n2 5\n3 6\n4 7\n5 8\n6 9\n3\n1 1000000000\n1 1\n1000000000 1000000000\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Your math teacher gave you the following problem: There are n segments on the x-axis, [l_1; r_1], [l_2; r_2], …, [l_n; r_n]. The segment [l; r] includes the bounds, i.e. it is a set ...
124_D. Squares_627
You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one. A square (x; y) is cons...
#!/usr/bin/python3 def cds(a, b, x, y): return (x + y) // (2 * a), (x - y) // (2 * b) def norm(x, y): return max(x, y) a, b, x1, y1, x2, y2 = map(int, input().split()) xp1, yp1 = cds(a, b, x1, y1) xp2, yp2 = cds(a, b, x2, y2) print(norm(abs(xp1 - xp2), abs(yp1 - yp2)))
{ "input": [ "2 2 1 0 0 1\n", "2 2 10 11 0 1\n", "2 4 3 -1 3 7\n", "605 297 -251700323 -366763764 -445828791 325081312\n", "14 9 44 45 -50 -9\n", "18 17 -26078453 -12853708 26705417 -4593122\n", "472555248 417950652 -897989583 -805741694 915661619 800897620\n", "1005 557 -451917708 -32...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in f...
1269_B. Modulo Equality_631
You are given a positive integer m and two integer sequence: a=[a_1, a_2, …, a_n] and b=[b_1, b_2, …, b_n]. Both of these sequence have a length n. Permutation is a sequence of n different positive integers from 1 to n. For example, these sequences are permutations: [1], [1,2], [2,1], [6,7,3,4,1,2,5]. These are not: [...
import sys input=sys.stdin.readline from collections import deque n,m=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) a.sort() b.sort() a=deque(a) b=deque(b) ans=0 for _ in range(n): if a==b: break f=1 for j in range(n-1): if b[j+1]-a[j+1]!=b[j]-a[j]: f=0 break i...
{ "input": [ "3 2\n0 0 0\n1 1 1\n", "4 3\n0 0 2 1\n2 0 1 1\n", "5 10\n0 0 0 1 2\n2 1 0 0 0\n", "20 10000000\n8861863 2169292 3484361 511558 5975675 1413584 774309 5847326 6668965 2531461 3337531 9484932 2648359 3710600 2232337 5474539 2785576 4119997 5005708 1717831\n247095 4003803 2013625 1177623 739...
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 m and two integer sequence: a=[a_1, a_2, …, a_n] and b=[b_1, b_2, …, b_n]. Both of these sequence have a length n. Permutation is a sequence of n dif...
1291_B. Array Sharpening_635
You're given an array a_1, …, a_n of n non-negative integers. Let's call it sharpened if and only if there exists an integer 1 ≤ k ≤ n such that a_1 < a_2 < … < a_k and a_k > a_{k+1} > … > a_n. In particular, any strictly increasing or strictly decreasing array is sharpened. For example: * The arrays [4], [0, 1], [...
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) rok = True rrok = True if n == 2 and a[0] == 0 and a[1] == 0: print("No") else: if n%2 == 0: ar = [0]*n for i in range(n//2): ...
{ "input": [ "10\n1\n248618\n3\n12 10 8\n6\n100 11 15 9 7 8\n4\n0 1 1 0\n2\n0 0\n2\n0 1\n2\n1 0\n2\n1 1\n3\n0 1 0\n3\n1 0 1\n", "10\n1\n248618\n3\n12 10 8\n6\n100 11 15 9 2 8\n4\n0 1 1 0\n2\n0 0\n2\n0 1\n2\n1 0\n2\n1 1\n3\n0 1 0\n3\n1 0 1\n", "10\n1\n451008\n3\n12 10 8\n6\n000 11 15 9 2 8\n4\n0 1 1 0\n2\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You're given an array a_1, …, a_n of n non-negative integers. Let's call it sharpened if and only if there exists an integer 1 ≤ k ≤ n such that a_1 < a_2 < … < a_k and a_k > a_{k+1}...
1311_C. Perform the Combo_639
You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press all buttons in the order they appear in s. I.e. if s="abca" then you have to press 'a', then 'b', 'c' and 'a' again. You know that you ...
from sys import stdin from bisect import bisect_left from collections import Counter for k in range(int(stdin.readline())): n,m=[int(x) for x in stdin.readline().split()] s=input() d=Counter(s) l=list(map(int,stdin.readline().split())) l.sort() ans=[0 for j in range(0,26)] for j in range(0,l...
{ "input": [ "3\n4 2\nabca\n1 3\n10 5\ncodeforces\n2 8 3 2 9\n26 10\nqwertyuioplkjhgfdsazxcvbnm\n20 10 1 2 3 5 10 5 9 4\n", "2\n2 2\nyz\n1 1\n2 2\nyz\n1 1\n", "1\n11 2\nthisisatest\n3 5\n", "2\n2 2\nyz\n1 1\n2 2\nzy\n1 1\n", "1\n11 2\nthisisatest\n3 7\n", "3\n4 2\nabca\n1 3\n10 5\ncodeforces\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You want to perform the combo on your opponent in one popular fighting game. The combo is the string s consisting of n lowercase Latin letters. To perform the combo, you have to press...
1334_A. Level Statistics_643
Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level. All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases...
T=int(input()) list=[] c=-1 d=-1 for i in range(T): n=int(input()) k="Yes" for j in range(n): a,b=map(int,input().split()) if a>=b and c<=a and d<=b and (b-d)<=(a-c): g=0 else: k="No" c=a d=b c=-1 d=-1 list.append(k) for i in range(len(list)): print(list[i])
{ "input": [ "6\n3\n0 0\n1 1\n1 2\n2\n1 0\n1000 3\n4\n10 1\n15 2\n10 2\n15 2\n1\n765 432\n2\n4 4\n4 3\n5\n0 0\n1 0\n1 0\n1 0\n1 0\n", "1\n2\n110 2\n115 112\n", "10\n5\n88 60\n10 3\n48 21\n90 70\n40 88\n5\n20 81\n39 98\n34 87\n100 82\n21 21\n2\n46 91\n89 71\n2\n81 98\n25 36\n3\n84 97\n40 32\n17 29\n2\n56 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level. All levels in this game...
1354_C2. Not So Simple Polygon Embedding_647
The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, n is always even, and in C2, n is always odd. You are given a regular polygon with 2 ⋅ n vertices (it's convex and has equal sides and equal angles) and all its sides have length 1. Let's name it as 2n...
# Why do we fall ? So we can learn to pick ourselves up. from math import pi,cos t = int(input()) for _ in range(0,t): n = int(input()) theta = pi/4 delta = pi/n maxi,mini,x = 0,0,0 for i in range(0,2*n): x += cos(theta) theta -= delta maxi = max(maxi,x) mini = min(m...
{ "input": [ "3\n3\n5\n199\n", "99\n3\n5\n7\n9\n11\n13\n15\n17\n19\n21\n23\n25\n27\n29\n31\n33\n35\n37\n39\n41\n43\n45\n47\n49\n51\n53\n55\n57\n59\n61\n63\n65\n67\n69\n71\n73\n75\n77\n79\n81\n83\n85\n87\n89\n91\n93\n95\n97\n99\n101\n103\n105\n107\n109\n111\n113\n115\n117\n119\n121\n123\n125\n127\n129\n131\n13...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The statement of this problem is the same as the statement of problem C1. The only difference is that, in problem C1, n is always even, and in C2, n is always odd. You are given a re...
1374_B. Multiply by 2, divide by 6_651
You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder). Your task is to find the minimum number of moves needed to obtain 1 from n or determine if it's impossible to do that. You have to answer t independent test cases. Input The fi...
t=int(input()) for i in range(t): n=int(input()) if n==1: print(0) else: if n%3!=0: print(-1) else: threes=0 twos=0 while n%3==0: threes+=1 n=n//3 while n%2==0: twos+=1 ...
{ "input": [ "7\n1\n2\n3\n12\n12345\n15116544\n387420489\n", "1\n999838675\n", "1\n782058266\n", "7\n1\n2\n4\n12\n12345\n15116544\n387420489\n", "7\n1\n2\n4\n24\n12345\n2324038\n387420489\n", "7\n1\n2\n4\n24\n12345\n2324038\n655615894\n", "7\n1\n2\n3\n12\n22908\n15116544\n387420489\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an integer n. In one move, you can either multiply n by two or divide n by 6 (if it is divisible by 6 without the remainder). Your task is to find the minimum number of...
1397_C. Multiples of Length_655
You are given an array a of n integers. You want to make all elements of a equal to zero by doing the following operation exactly three times: * Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different). It can...
from sys import stdin def inp(): return stdin.buffer.readline().rstrip().decode('utf8') def itg(): return int(stdin.buffer.readline()) def mpint(): return map(int, stdin.buffer.readline().split()) # ############################## import # ############################## main # for __ in range(itg()):...
{ "input": [ "4\n1 3 2 4\n", "2\n-492673762 -496405053\n", "13\n-958184557 -577042357 -616514099 -553646903 -719490759 -761325526 -210773060 -44979753 864458686 -387054074 546903944 638449520 299190036\n", "5\n450402558 -840167367 -231820501 586187125 -627664644\n", "32\n474784688 671671886 -59275...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given an array a of n integers. You want to make all elements of a equal to zero by doing the following operation exactly three times: * Select a segment, for each number ...
1420_D. Rescue Nibel!_659
Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins... nothing happened. Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional de...
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)] def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e for l in range(d)] for k in range(c)] for j in range(b)]...
{ "input": [ "3 3\n1 3\n2 3\n3 3\n", "3 1\n1 1\n2 2\n3 3\n", "7 3\n1 7\n3 8\n4 5\n6 7\n1 3\n5 10\n8 9\n", "3 2\n1 1\n2 2\n3 3\n", "5 2\n1 3\n2 4\n3 5\n4 6\n5 7\n", "10 7\n1 10\n2 10\n3 10\n4 10\n5 10\n1 2\n1 3\n1 4\n1 5\n1 6\n", "2 2\n1 1\n1 1\n", "20 12\n18525 35038\n15816 31586\n1864...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the...
1466_C. Canine poetry_665
After his wife's tragic death, Eurydice, Orpheus descended to the realm of death to see her. Reaching its gates was uneasy, but passing through them proved to be even more challenging. Mostly because of Cerberus, the three-headed hound of Hades. Orpheus, a famous poet, and musician plans to calm Cerberus with his poe...
import math t=int(input()) for i in range(t): s=input() s=list(s) l=len(s) if l==1: print(0) elif l==2: if s[0]==s[1]: print(1) else: print(0) else: j=0 c=0 while(j<l): if j==l-1: break ...
{ "input": [ "7\nbabba\nabaac\ncodeforces\nzeroorez\nabcdcba\nbbbbbbb\na\n", "7\nabbab\nabaac\ncodeforces\nzeroorez\nabcdcba\nbbbbbbb\na\n", "7\nabbab\nabaac\ncodeforces\nzeroorez\nabadcbc\nbbbbbbb\na\n", "7\nabbab\nabaac\ncodeforces\nzeroorez\nabadcbc\nbbbbabb\na\n", "7\nbbbaa\nabaac\ncodeforces\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After his wife's tragic death, Eurydice, Orpheus descended to the realm of death to see her. Reaching its gates was uneasy, but passing through them proved to be even more challenging...
1490_D. Permutation Transformation_669
A permutation — is a sequence of length n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3, 5, 2, 1, 4], [1, 3, 2] — permutations, and [2, 3, 2], [4, 3, 1], [0] — no. Polycarp was recently gifted a permutation a[1 ... n] of length n. Polycarp likes trees more than permutations, s...
import sys input=sys.stdin.readline I = lambda : list(map(int,input().split())) t,=I() for _ in range(t): n,=I() l=I() an=[0]*n for i in range(n): x=y=0 j=i+1 x=l[i] while j<n: if l[j]>x: an[i]+=1 x=l[j] if l[j]==n: break j+=1 j=i-1 x=l[i] while j>-1: if l[j]>x: an[i]+=1 ...
{ "input": [ "3\n5\n3 5 2 1 4\n1\n1\n4\n4 3 1 2\n", "3\n5\n3 5 1 2 4\n1\n1\n4\n4 3 1 2\n", "3\n5\n4 5 2 1 3\n1\n1\n4\n4 3 1 2\n", "3\n5\n3 4 2 1 5\n1\n1\n4\n4 3 1 2\n", "3\n5\n2 5 3 1 4\n1\n1\n4\n4 3 1 2\n", "3\n5\n4 5 1 2 3\n1\n1\n4\n4 3 1 2\n" ], "output": [ "\n1 0 2 3 1 \n0 \n0 1 3 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A permutation — is a sequence of length n integers from 1 to n, in which all the numbers occur exactly once. For example, [1], [3, 5, 2, 1, 4], [1, 3, 2] — permutations, and [2, 3, 2]...
167_C. Wizards and Numbers_675
In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it — a and b. The order of the numbers is not important. Let's consider a ≤ b for the sake of definiteness. The players can cast one of the two spells in turns: * Replace b with b - ak. Number k can be chosen by...
def solve(a, b): if a == 0: return False if solve(b % a, a): b //= a return not (b % (a + 1) & 1) return True n = int(input()) for _ in range(n): a, b = [int(x) for x in input().split()] if a > b: a, b = b, a if solve(a, b): print("First") else: ...
{ "input": [ "4\n10 21\n31 10\n0 1\n10 30\n", "7\n576460752303423487 2\n82 9\n101 104\n10 21\n31 10\n0 1\n10 30\n", "1\n128817972817282999 327672410994637530\n", "66\n7 0\n5 7\n1 3\n3 2\n3 5\n0 6\n1 2\n0 7\n4 5\n4 7\n5 1\n2 0\n4 0\n0 5\n3 6\n7 3\n6 0\n5 2\n6 6\n1 7\n5 6\n2 2\n3 4\n2 1\n5 3\n4 6\n6 2\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it — a and b. The order of the numbers is not important. Let's consider a ≤ b...
209_B. Pixels_679
Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that is, the total number of pixels decreases by one). Besides, if pixels of colors x and y (x ≠ y) meet in a violent fight, then the pixel t...
a = list(map(int,input().split())) def calc(a): return int((((a[1]-a[0])+(a[1]+a[0]))/2)) a.sort() if a[1] % 2 == 0 and a[0] % 2 == 0: print(calc(a)) elif a[1] % 2 == 0 or a[0] % 2 == 0: print(a[2]) else: print(calc(a))
{ "input": [ "1 1 1\n", "3 1 0\n", "9 530792195 6\n", "0 0 10\n", "1944219055 454183506 1369298327\n", "914835 2742837 9234739\n", "10 10 0\n", "2 2 0\n", "0 0 1\n", "3 10 1007169359\n", "0 1 0\n", "1016450951 2 9\n", "0 2 10\n", "2147483648 0 2147483647\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Flatland is inhabited by pixels of three colors: red, green and blue. We know that if two pixels of different colors meet in a violent fight, only one of them survives the fight (that...
235_A. LCM Challenge_683
Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many numbers, so I'll choose three positive integers (they don't have to be distinct) which are not greater than n. Can you help me to find th...
import sys, math input = sys.stdin.readline def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) import collections as col import math def s...
{ "input": [ "7\n", "9\n", "447244\n", "958507\n", "816923\n", "836603\n", "862795\n", "756604\n", "1000000\n", "4\n", "668827\n", "245\n", "520731\n", "642635\n", "8\n", "244\n", "148\n", "213\n", "984699\n", "20\n", "810411\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Some days ago, I learned the concept of LCM (least common multiple). I've played with it for several times and I want to make a big number with it. But I also don't want to use many ...
25_D. Roads not only in Berland_687
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are n cities in Berland and neighboring countries in total and exactly n - 1 two-way r...
''' Auther: ghoshashis545 Ashis Ghosh College: jalpaiguri Govt Enggineering College ''' from os import path import sys from heapq import heappush,heappop from functools import cmp_to_key as ctk from collections import deque,defaultdict as dd from bisect import bisect,bisect_left,bisect_right,insort,insort_lef...
{ "input": [ "7\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n", "2\n1 2\n", "4\n1 4\n3 1\n3 4\n", "5\n4 1\n4 3\n5 3\n2 4\n", "21\n7 15\n13 1\n14 3\n4 10\n2 3\n16 18\n17 20\n16 20\n8 4\n3 12\n2 17\n13 11\n16 1\n13 2\n13 5\n8 9\n6 14\n3 17\n16 9\n13 8\n", "3\n3 1\n3 2\n", "6\n5 2\n5 3\n1 4\n3 1\n5 6\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries...
306_C. White, Black and White Again_693
Polycarpus is sure that his life fits the description: "first there is a white stripe, then a black one, then a white one again". So, Polycarpus is sure that this rule is going to fulfill during the next n days. Polycarpus knows that he is in for w good events and b not-so-good events. At least one event is going to ta...
import sys MOD = int(1e9) + 9 def inv(n): return pow(n, MOD - 2, MOD) def combo(n): rv = [0 for __ in range(n + 1)] rv[0] = 1 for k in range(n): rv[k + 1] = rv[k] * (n - k) % MOD * inv(k + 1) % MOD return rv with sys.stdin as fin, sys.stdout as fout: n, w, b = map(int, next(fin).spl...
{ "input": [ "3 2 1\n", "3 2 2\n", "4 2 2\n", "3 3 1\n", "300 2 300\n", "4000 4000 1\n", "4000 4000 100\n", "4 2 3\n", "3 300 300\n", "4000 100 4000\n", "3 300 1\n", "300 300 1\n", "4000 1000 3000\n", "3 2 4000\n", "4 3 2\n", "3 2 300\n", "4000 3998 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarpus is sure that his life fits the description: "first there is a white stripe, then a black one, then a white one again". So, Polycarpus is sure that this rule is going to ful...
378_C. Maze_701
Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have a common side. Pavel drew a grid maze with all empty cells forming a connected area. That is, you can go from any empty cell to any oth...
import random, math, sys from copy import deepcopy as dc from bisect import bisect_left, bisect_right from collections import Counter input = sys.stdin.readline # Function to take input def input_test(): n, m, k = map(int, input().strip().split(" ")) grid = [] for i in range(n): grid.append(list(input()....
{ "input": [ "3 4 2\n#..#\n..#.\n#...\n", "5 4 5\n#...\n#.#.\n.#..\n...#\n.#.#\n", "19 20 196\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: Pavel loves grid mazes. A grid maze is an n × m rectangle maze where each cell is either empty, or is a wall. You can go from one cell to another only if both cells are empty and have...
39_E. What Has Dirichlet Got to Do with That?_705
You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1 items, that leads to the existence of a box in which there are at least two items. Having heard of that principle, but having not mastered the technique of logical thinking, 8 year olds Stas and Masha invented a game. T...
a, b, L = list(map(int, input().split())) memo = {} #10^9 rougly equals 31700 * 31700 memo[(31701, 1)] = ((L - 31701) + 1)% 2 #2**30 > 10^9 memo[(1, 30)] = -1 for i in range(31700, a - 1, -1): for j in range(29, b - 1, -1): if i**j>=L: continue s = set() if (i + 1) *...
{ "input": [ "3 1 4\n", "2 2 10\n", "5 5 16808\n", "1 4 10\n", "2 2 5\n", "9 9 1000000000\n", "3 17 999999997\n", "3 3 64\n", "5 1 64\n", "29 2 1000000000\n", "1010 1 1000000000\n", "3 2 27\n", "1 1 60\n", "2 4 64\n", "2 2 64\n", "3 4 1000\n", "2 3 6...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1 items, that leads to the existence of a box in which there are at least two items. ...
425_A. Sereja and Swaps_709
As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose two indexes i, j (i ≠ j); * perform assignments tmp = a[i], a[i] = a[j], a[j] = tmp. What maximum value of function m(a) can Serej...
#!/usr/local/bin/python3 n, k = map(int, input().split()) a = list(map(int, input().split())) r_sum = a[0] for l in range(n): for r in range(l, n): inside = sorted(a[l:r+1]) outside = sorted(a[:l] + a[r+1:], reverse=True) t_sum = sum(inside) for i in range(min(k, len(inside), len(outside))): if outside[i] >...
{ "input": [ "5 10\n-1 -1 -1 -1 -1\n", "10 2\n10 -1 2 2 2 2 2 2 -1 10\n", "1 10\n1\n", "10 1\n-1 1 1 1 1 1 1 1 1 1\n", "78 8\n-230 -757 673 -284 381 -324 -96 975 249 971 -355 186 -526 804 147 -553 655 263 -247 775 108 -246 -107 25 -786 -372 -24 -619 265 -192 269 392 210 449 335 -207 371 562 307 14...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: As usual, Sereja has array a, its elements are integers: a[1], a[2], ..., a[n]. Let's introduce notation: <image> A swap operation is the following sequence of actions: * choose ...
449_D. Jzzhu and Numbers_712
Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k. Jzzhu wonders, how many groups exists such that ai1 & ai2 & ... & aik = 0 (1 ≤ k ≤ n)? Help him and print this number modulo 1000000007 (109 + 7). Operation x & y deno...
import sys input = lambda : sys.stdin.readline().rstrip() sys.setrecursionlimit(2*10**5+10) write = lambda x: sys.stdout.write(x+"\n") debug = lambda x: sys.stderr.write(x+"\n") writef = lambda x: print("{:.12f}".format(x)) # zeta mebius def zeta_super(val, n): # len(val)==2^n out = val[:] for i in rang...
{ "input": [ "3\n2 3 3\n", "4\n0 1 2 3\n", "6\n5 2 0 5 2 1\n", "2\n1 31\n", "2\n1 0\n", "10\n450661 128600 993228 725823 293549 33490 843121 903634 556169 448234\n", "1\n1\n", "5\n1 3 5 7 9\n", "6\n524 529 5249 524 529 529\n", "3\n1 2 3\n", "2\n0 1\n", "55\n0 1 2 3 4 5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Jzzhu have n non-negative integers a1, a2, ..., an. We will call a sequence of indexes i1, i2, ..., ik (1 ≤ i1 < i2 < ... < ik ≤ n) a group of size k. Jzzhu wonders, how many groups...
494_A. Treasure_716
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the ...
#!/usr/bin/env python3 s = input() count = 0 res = [] last = s.rfind("#") for i, c in enumerate(s): if c == '(': count += 1 elif c == ')': count -= 1 else: if i < last: res.append(1) count -= 1 else: num = max(1, 1 + s.count("(") - s.coun...
{ "input": [ "(((#)((#)\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: Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'...
518_E. Arthur and Questions_720
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This sequence had the following property: if you write out the sums of all its segments consisting of k consecutive elements (a1 + a2 ... + ak, ...
import sys n, k = map(int, input().split()) a = input().split() INF = 10 ** 9 + 7 OK = True for i in range(n): if a[i] == "?": a[i] = INF else: a[i] = int(a[i]) for i in range(len(a)): if a[i] == INF: j = i + k while j < len(a) and a[j] == INF: j += k c...
{ "input": [ "3 2\n? 1 2\n", "5 1\n-10 -9 ? -7 -6\n", "5 3\n4 6 7 2 9\n", "7 2\n-10 0 ? 1 ? 2 10\n", "1 1\n0\n", "5 1\n-3 -2 -1 0 1\n", "7 1\n-4 ? ? ? ? ? 2\n", "17 1\n? -13 ? ? ? -3 ? ? ? ? ? 10 ? ? ? ? 100\n", "3 1\n-5 ? 0\n", "7 2\n-10 0 ? 1 6 2 ?\n", "3 1\n-3 ? -2\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length n (a1, a2, ..., an), consisting of integers and integer k, not exceeding n. This se...
544_E. Remembering Strings_723
You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i....
from sys import stdin n,m=map(int,stdin.readline().strip().split()) s=[] for i in range(n): s.append(list(map(ord,list(stdin.readline().strip())))) for j in range(m): s[-1][j]=s[-1][j]-97 ct=[tuple(map(int,stdin.readline().strip().split())) for i in range(n)] mc=[[0 for i in range(22)] for j in range(22...
{ "input": [ "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n", "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n", "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1\n", "1 1\na\n10\n", "1 20\naaaaaaaaaaaaaaaaaaaa\n924705 786913 546594 427698 741583 189683 35408...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some positi...
571_B. Minimization_727
You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n. You need to permute the array elements so that value <image> became minimal possible. In particular, it is allowed not to change order of elements at all. Input The first line contains two integers n,...
f = lambda: map(int, input().split()) n, k = f() p = sorted(f()) m, d = n // k, n % k u, v = d + 1, k - d + 1 g = [0] * u * v i = 0 for a in range(u): j = a * m + a - 1 for b in range(v): x = g[i - 1] + p[j] - p[j - m + 1] if b else 9e9 y = g[i - v] + p[j] - p[j - m] if a else 9e9 if i...
{ "input": [ "5 2\n3 -5 3 -5 3\n", "3 2\n1 2 4\n", "6 3\n4 3 4 3 2 5\n", "30 2\n-999999924 -499999902 500000091 -999999998 500000030 -999999934 500000086 -499999918 -499999998 67 -999999964 -499999975 -499999947 -499999925 3 -499999985 14 500000015 500000022 88 25 -499999909 500000051 -499999984 -9999...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You've got array A, consisting of n integers and a positive integer k. Array A is indexed by integers from 1 to n. You need to permute the array elements so that value <image> beca...
614_C. Peter and Snow Blower_734
Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow blowing machines. In order to make it work, you need to tie it to some point that it does not cover, and then switch it on. As a result it...
import math import sys def calculate_area(n, x, y, vertices): r_max = -sys.maxsize r_min = sys.maxsize last_d = -1 for v in vertices: d = distance_two_points(v, (x, y)) if d > r_max: r_max = d if d < r_min: r_min = d last_v = vertices[0] for i i...
{ "input": [ "3 0 0\n0 1\n-1 2\n1 2\n", "4 1 -1\n0 0\n1 2\n2 0\n1 1\n", "4 0 0\n1 -1\n1 3\n3 3\n3 -1\n", "3 0 0\n-10 1\n0 2\n1 1\n", "3 0 0\n-1 1\n0 3\n1 1\n", "20 -999719 -377746\n-999718 -377746\n-997432 -940486\n-982215 -950088\n-903861 -997725\n-127953 -999833\n846620 -999745\n920305 -9929...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Peter got a new snow blower as a New Year present. Of course, Peter decided to try it immediately. After reading the instructions he realized that it does not work like regular snow b...
687_A. NP-Hard Problem_742
Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. <image> or <image>...
M=lambda:map(int,input().split()) n,m=M() graph=[set() for i in range(n)] for _ in range(m): a,b=M() graph[a-1].add(b-1) graph[b-1].add(a-1) visited=[-1 for i in range(n)] stack=[] for i in range(n): if visited[i]==-1 and len(graph[i])>0: visited[i]=True stack+=[i] while stack: ...
{ "input": [ "4 2\n1 2\n2 3\n", "3 3\n1 2\n2 3\n1 3\n", "10 16\n6 10\n5 2\n6 4\n6 8\n5 3\n5 4\n6 2\n5 9\n5 7\n5 1\n6 9\n5 8\n5 10\n6 1\n6 7\n6 3\n", "10 9\n2 5\n2 4\n2 7\n2 9\n2 3\n2 8\n2 6\n2 10\n2 1\n", "20 22\n3 18\n9 19\n6 15\n7 1\n16 8\n18 7\n12 3\n18 4\n9 15\n20 1\n4 2\n6 7\n14 2\n7 15\n7 10...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph G is given. Subset A of its verti...
730_G. Car Repair Shop_748
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time. Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order...
t, p = 1, [] for i in range(int(input())): l, d = map(int, input().split()) if t > l: for i, q in enumerate(p, 1): if q[0] <= l <= q[1] - d: p.insert(i, [l + d, q[1]]) q[1] = l break else: for q in p: if q[0]...
{ "input": [ "4\n1000000000 1000000\n1000000000 1000000\n100000000 1000000\n1000000000 1000000\n", "3\n9 2\n7 3\n2 4\n", "1\n1 5000000\n", "1\n1000000000 1\n", "10\n588 12\n560 10\n593 14\n438 15\n761 11\n984 6\n503 2\n855 19\n538 2\n650 7\n", "20\n360 26\n475 17\n826 12\n815 23\n567 28\n897 2...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given ...
754_B. Ilya and tic-tac-toe game_752
Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's turn in the game when they left it. Determine whether Ilya could have won the game by making...
from sys import exit l1 = input() l2 = input() l3 = input() l4 = input() grid = [[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]] cross = 0 dots = [] for i in range(0, 4): if l1[i] == ".": dots += [[0+2, i+2]] eli...
{ "input": [ "o.x.\no...\n.x..\nooxx\n", "x.ox\nox..\nx.o.\noo.x\n", "xx..\n.oo.\nx...\noox.\n", "x..x\n..oo\no...\nx.xo\n", "xoox\n.xx.\no..o\n..xo\n", "o.xx\nxo.o\n...o\n..x.\n", "xxoo\no.oo\n...x\nx..x\n", "o..x\n....\n...x\n..o.\n", "....\n.o..\n....\nox.x\n", "xxo.\n.oo.\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired an...
774_K. Stepan and Vowels_756
Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa". Sergey does not like such behavior, so he wants to write a program to format the words written by Stepan. This program must combine all consecutive equal vowels to a single vowel. The vowe...
import math from sys import stdin, stdout fin = stdin fout = stdout n = int(fin.readline().strip()) s = fin.readline().strip() ans = [] gl = frozenset({'a', 'e', 'i', 'y', 'o', 'u'}) met = False cdel = False for i in range(n): if i > 0: if s[i] != s[i - 1]: met = False cdel = Fa...
{ "input": [ "18\naeiouyaaeeiioouuyy\n", "22\niiiimpleeemeentatiioon\n", "13\npobeeeedaaaaa\n", "24\naaaoooiiiuuuyyyeeeggghhh\n", "3\neoo\n", "36\naeiouyaaeeiioouuyyaaaeeeiiiooouuuyyy\n", "3\nooo\n", "1\na\n", "4\neeoo\n", "5\noooee\n", "5\nooeoo\n", "1\nf\n", "200\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Stepan likes to repeat vowel letters when he writes words. For example, instead of the word "pobeda" he can write "pobeeeedaaaaa". Sergey does not like such behavior, so he wants to ...
820_A. Mister B and Book Reading_762
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages. At first day Mister B read v0 pages, but after that he started to speed up. Every day, starting from the second, he read a pages more than on the previous day (at first day he read v0 pages, at second —...
c,v0,v1,a,l = list(map(int, input().split(" "))) count=1 sum=v0 while sum<c: sum+=min(v0+count*a-l,v1-l) count+=1 print(count)
{ "input": [ "5 5 10 5 4\n", "12 4 12 4 1\n", "15 1 100 0 0\n", "10 1 4 10 0\n", "100 1 2 1000 0\n", "1 11 12 0 10\n", "1 2 3 0 0\n", "8 6 13 2 5\n", "8 3 5 1 0\n", "17 10 12 6 5\n", "6 4 4 1 2\n", "50 4 5 5 0\n", "20 2 40 1 1\n", "1000 501 510 1 499\n", "12...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had c pages. At first day Mister B read v0 pages, but after that he started to...
846_B. Math Show_766
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order. By solving s...
n, k, m = list(map(int, input().split())) t = sorted(map(int, input().split())) res = 0 for x in range(min(m//sum(t),n)+1): rem = m - x*sum(t) r = x*(k+1) for i in range(k): div = min(rem//t[i], n-x) rem -= div*t[i] r += div res = max(res, r) print(res)
{ "input": [ "5 5 10\n1 2 4 8 16\n", "3 4 11\n1 2 3 4\n", "1 3 0\n6 3 4\n", "5 4 32\n4 2 1 1\n", "32 6 635\n3 4 2 1 7 7\n", "3 7 20012\n1 1 1 1 1 1 10000\n", "1 5 44\n2 19 18 6 8\n", "1 3 8\n5 4 4\n", "3 3 15\n1 2 1\n", "1 1 1\n1\n", "1 1 0\n2\n", "12 1 710092\n145588\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time r...
893_A. Chess For Three_772
Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a bit difficult for them because chess is a game for two players, not three. So they play with each other according to following rules: ...
num=int(input()) spectator=3 p1=1 p2=2 yes=True for i in range(0,num): winner=int(input()) if winner is spectator: print("NO") yes=False break if p1 is winner: temp=spectator spectator=p2 p2=temp else: temp=spectator spectator=p1 p1...
{ "input": [ "2\n1\n2\n", "3\n1\n1\n2\n", "99\n1\n3\n2\n2\n3\n1\n1\n3\n3\n3\n3\n3\n3\n1\n1\n3\n3\n3\n3\n1\n1\n3\n2\n1\n1\n1\n1\n1\n1\n1\n3\n2\n2\n2\n1\n3\n3\n1\n1\n3\n2\n1\n3\n3\n1\n2\n3\n3\n3\n1\n2\n2\n2\n3\n3\n3\n3\n3\n3\n2\n2\n2\n2\n3\n3\n3\n1\n1\n3\n2\n1\n1\n2\n2\n2\n3\n3\n2\n1\n1\n2\n2\n1\n3\n2\n1\n1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Alex, Bob and Carl will soon participate in a team chess tournament. Since they are all in the same team, they have decided to practise really hard before the tournament. But it's a b...
937_D. Sleepy Game_777
Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. One of the vertices contains a chip. Initially the chip is located at vertex s. Players take turns moving the chip along some edge of the graph. Petya goes first. Player who can't m...
n,m = map(int, input().split()) g = [[] for i in range(n)] fs = set() for i in range(n): a = list(map(int , input().split())) c = a[0] if c == 0: fs.add(i) continue for j in range(1,c+1): g[i].append(a[j]-1) s = int(input())-1 prev0 = [None for i in range(n)] prev1=[None for i i...
{ "input": [ "3 2\n1 3\n1 1\n0\n2\n", "2 2\n1 2\n1 1\n1\n", "5 6\n2 2 3\n2 4 5\n1 4\n1 5\n0\n1\n", "11 20\n1 2\n2 7 6\n1 7\n4 10 9 3 2\n2 9 2\n1 3\n0\n0\n3 1 6 7\n4 11 7 5 6\n2 2 8\n4\n", "5 5\n1 2\n1 3\n2 1 4\n1 5\n0\n1\n", "5 5\n1 2\n1 3\n2 2 4\n1 5\n0\n1\n", "4 3\n1 2\n1 3\n1 1\n0\n1\n"...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Petya and Vasya arranged a game. The game runs by the following rules. Players have a directed graph consisting of n vertices and m edges. One of the vertices contains a chip. Initial...
990_B. Micro-World_783
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them. You know that you have n bacteria in the Petri dish and size of the i-th bacteria is a_i. Also you know intergalactic positive integer constan...
n, m = map(int, input().split()) l = sorted(map(int, input().split())) t, b = l[::-1], -m for a in l: while b < a: if a <= b + m: n -= 1 b = t.pop() print(n)
{ "input": [ "6 5\n20 15 10 15 20 25\n", "7 1\n101 53 42 102 101 55 54\n", "7 1000000\n1 1 1 1 1 1 1\n", "2 1\n1 1\n", "4 1\n2 2 1 1\n", "10 1\n2 6 3 4 2 4 4 3 2 1\n", "2 1\n999152 999153\n", "8 1000000\n1 1 5 1000000 1000000 2 2 2\n", "9 2\n1 6 1 5 5 8 6 8 7\n", "15 1\n1 1 1 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them. You k...
p02610 AIsing Programming Contest 2020 - Camel Train_796
We have N camels numbered 1,2,\ldots,N. Snuke has decided to make them line up in a row. The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwise. Snuke wants to maximize the total happiness of the camels. Find the maximum possible total happiness of the camel. Solve this probl...
import sys from heapq import heappush, heappop from operator import itemgetter sys.setrecursionlimit(10 ** 7) rl = sys.stdin.readline def solve(): N = int(rl()) res = 0 camel_left, camel_right = [], [] for _ in range(N): K, L, R = map(int, rl().split()) res += min(L, R) if R <...
{ "input": [ "3\n2\n1 5 10\n2 15 5\n3\n2 93 78\n1 71 59\n3 57 96\n19\n19 23 16\n5 90 13\n12 85 70\n19 67 78\n12 16 60\n18 48 28\n5 4 24\n12 97 97\n4 57 87\n19 91 74\n18 100 76\n7 86 46\n9 100 57\n3 76 73\n6 84 93\n1 6 84\n11 75 94\n19 15 3\n12 11 34", "3\n2\n1 5 10\n2 15 5\n3\n2 93 78\n1 71 59\n3 57 96\n19\n1...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have N camels numbered 1,2,\ldots,N. Snuke has decided to make them line up in a row. The happiness of Camel i will be L_i if it is among the K_i frontmost camels, and R_i otherwi...
p02741 Panasonic Programming Contest 2020 - Kth Term_800
Print the K-th element of the following sequence of length 32: 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 Constraints * 1 \leq K \leq 32 * All values in input are integers. Input Input is given from Standard Input in the following format: K Output Print ...
n = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51] K = int(input()) print(n[K-1])
{ "input": [ "27", "6", "16", "19", "8", "24", "32", "28", "-22", "1", "15", "5", "-9", "-27", "-13", "29", "-20", "001", "8", "1", "32", "5", "15", "24", "-9", "-13", "19", "29", "16", "28", "-...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Print the K-th element of the following sequence of length 32: 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51 Constraints * 1 \l...
p02876 AtCoder Grand Contest 040 - Balance Beam_803
We have N balance beams numbered 1 to N. The length of each beam is 1 meters. Snuke walks on Beam i at a speed of 1/A_i meters per second, and Ringo walks on Beam i at a speed of 1/B_i meters per second. Snuke and Ringo will play the following game: * First, Snuke connects the N beams in any order of his choice and m...
import sys input = sys.stdin.readline def gcd(a, b): while b: a, b = b, a % b return a N = int(input()) S = 0 Y = [] for i in range(N): a, b = map(int, input().split()) if b > a: S += b-a Y.append((b, b)) else: Y.append((a, b)) Y = sorted(Y) YY = [0] * (N+1) for i in range(...
{ "input": [ "3\n4 1\n5 2\n6 3", "4\n1 5\n4 7\n2 1\n8 4", "2\n3 2\n1 2", "10\n866111664 178537096\n705445072 318106937\n472381277 579910117\n353498483 865935868\n383133839 231371336\n378371075 681212831\n304570952 16537461\n955719384 267238505\n844917655 218662351\n550309930 62731178", "3\n1 1\n5 ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: We have N balance beams numbered 1 to N. The length of each beam is 1 meters. Snuke walks on Beam i at a speed of 1/A_i meters per second, and Ringo walks on Beam i at a speed of 1/B_...
p03010 diverta 2019 Programming Contest 2 - Diverta City_806
Diverta City is a new city consisting of N towns numbered 1, 2, ..., N. The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length of each road is undecided. A Hamiltonian path is a path that starts at one of the towns and visits each of the other towns exactly once...
from itertools import combinations, permutations N = int(input()) # 整数列の生成 # s = [1] # while len(s) < 10 : # i = s[-1] + 1 # while True : # path = s.copy() + [i] # flag = True # for comb in combinations(s + [i], 2) : # if not sum(comb) in path : # path....
{ "input": [ "4", "3", "2", "6", "7", "8", "10", "1", "5", "9", "001", "010", "5", "6", "1", "9", "7", "2", "10", "8", "001", "010" ], "output": [ "0 111 157 193\n111 0 224 239\n157 224 0 258\n193 239 258 0", "0 6 15\n...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Diverta City is a new city consisting of N towns numbered 1, 2, ..., N. The mayor Ringo is planning to connect every pair of two different towns with a bidirectional road. The length...
p03150 KEYENCE Programming Contest 2019 - KEYENCE String_810
A string is called a KEYENCE string when it can be changed to `keyence` by removing its contiguous substring (possibly empty) only once. Given a string S consisting of lowercase English letters, determine if S is a KEYENCE string. Constraints * The length of S is between 7 and 100 (inclusive). * S consists of lowerc...
S = input() k = "keyence" n = len(S)-7 for i in range(len(S)-n+1): if S[:i]+S[i+n:] == k: print("YES") break else: print("NO")
{ "input": [ "keyence", "ashlfyha", "keyofscience", "mpyszsbznf", "ecneyek", "ashkfyha", "eeyofscienck", "mnyszsbzpf", "ecneyel", "arhkfyha", "eeyofsciencj", "mnyszsb{pf", "ecleyen", "arhkyfha", "eeyofscienci", "mnzszsb{pf", "neyelce", "ahfykhra"...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A string is called a KEYENCE string when it can be changed to `keyence` by removing its contiguous substring (possibly empty) only once. Given a string S consisting of lowercase Engl...
p03294 AtCoder Beginner Contest 103 - Modulo Summation_814
You are given N positive integers a_1, a_2, ..., a_N. For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N). Here, X\ mod\ Y denotes the remainder of the division of X by Y. Find the maximum value of f. Constraints * All values in input are integers. * 2 \leq N \leq 3000 * 2 ...
N=int(input()) S=sum(list(map(int,input().split(' ')))) print(S-N)
{ "input": [ "5\n7 46 11 20 11", "7\n994 518 941 851 647 2 581", "3\n3 4 6", "5\n7 46 11 17 11", "7\n994 518 941 851 647 2 568", "3\n3 1 6", "5\n7 46 11 17 13", "7\n994 518 941 523 647 2 568", "3\n3 2 6", "5\n7 13 11 17 13", "7\n994 518 941 523 647 1 568", "3\n3 3 6", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given N positive integers a_1, a_2, ..., a_N. For a non-negative integer m, let f(m) = (m\ mod\ a_1) + (m\ mod\ a_2) + ... + (m\ mod\ a_N). Here, X\ mod\ Y denotes the remai...
p03452 AtCoder Regular Contest 090 - People on a Line_818
There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one person is standing at the same coordinate. You will given M pieces of information regarding the positions of these people. The i-th piece of...
def inpl(): return [int(i) for i in input().split()] def find(x): if par[x] == x: return x else: par[x],dist[x] = find(par[x]),dist[x]+dist[par[x]] return par[x] N, M = inpl() par = list(range(N+1)) dist = [0 for _ in range(N+1)] for _ in range(M): l, r, d = inpl() fl = find(l) ...
{ "input": [ "3 3\n1 2 1\n2 3 1\n1 3 5", "100 0", "10 3\n8 7 100\n7 9 100\n9 8 100", "3 3\n1 2 1\n2 3 1\n1 3 2", "4 3\n2 1 1\n2 3 5\n3 4 2", "000 0", "10 3\n8 7 000\n7 9 100\n9 8 100", "4 3\n2 1 1\n2 3 3\n3 4 2", "10 3\n8 7 000\n7 9 110\n9 8 100", "4 3\n1 1 1\n2 3 3\n3 4 2", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There are N people standing on the x-axis. Let the coordinate of Person i be x_i. For every i, x_i is an integer between 0 and 10^9 (inclusive). It is possible that more than one pers...
p03612 AtCoder Beginner Contest 072 - Derangement_822
You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero): Operation: Swap two adjacent elements in the permutation. You want to have p_i ≠ i for all 1≤i≤N. Find the minimum required number of operations to achieve this. Constraint...
N = int(input()) p = list(map(int, input().split())) p.append(0) cnt = 0 for i in range(N): if p[i] == i + 1: p[i], p[i+1] = p[i+1], p[i] cnt += 1 print (cnt)
{ "input": [ "5\n1 4 3 5 2", "9\n1 2 4 9 5 8 7 3 6", "2\n1 2", "2\n2 1", "5\n1 4 4 5 2", "9\n1 2 4 9 5 8 7 6 6", "9\n1 2 4 9 5 8 4 6 6", "5\n0 4 4 5 1", "2\n1 0", "2\n1 1", "5\n1 4 4 5 1", "2\n1 -1", "2\n0 2", "9\n2 2 4 9 5 8 4 6 6", "2\n2 2", "2\n0 1", ...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N. You can perform the following operation any number of times (possibly zero): Operation: Swap two adjacent elements...
p03940 AtCoder Grand Contest 007 - Shik and Game_828
Imagine a game played on a line. Initially, the player is located at position 0 with N candies in his possession, and the exit is at position E. There are also N bears in the game. The i-th bear is located at x_i. The maximum moving speed of the player is 1 while the bears do not move at all. When the player gives a c...
import sys readline = sys.stdin.readline class Segtree: def __init__(self, A, intv, initialize = True, segf = max): self.N = len(A) self.N0 = 2**(self.N-1).bit_length() self.intv = intv self.segf = segf if initialize: self.data = [intv]*self.N0 + A + [intv]*(self...
{ "input": [ "3 9 1\n1 3 8", "2 1000000000 1000000000\n1 999999999", "3 9 3\n1 3 8", "3 9 1\n2 3 8", "2 1000000000 1000000000\n1 199586132", "3 9 3\n1 3 7", "3 9 3\n1 0 7", "2 1000001000 1000000000\n2 199586132", "3 9 5\n1 0 7", "2 1010001000 1000000000\n2 199586132", "2 10...
5ATCODER
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Imagine a game played on a line. Initially, the player is located at position 0 with N candies in his possession, and the exit is at position E. There are also N bears in the game. Th...
p00032 Plastic Board_832
There is a factory that inputs the data of the side and diagonal lengths to the machine and cuts out the plastic plate. At this factory, we cut out only parallelogram molds, although they vary in size. You have been ordered by your boss to count the number of rectangles and rhombuses produced among the parallelograms t...
rect = 0 loze = 0 while True: try: n, m, o = map(int, input().split(',')) if n ** 2 + m ** 2 == o ** 2: rect += 1 if n == m: loze += 1 except: print(rect) print(loze) break
{ "input": [ "3,4,5\n5,5,8\n4,4,4\n5,4,3", "3,4,5\n5,5,8\n4,5,4\n5,4,3", "3,3,5\n5,5,8\n4,4,4\n5,4,3", "3,4,5\n5,5,8\n4,5,4\n3,4,5", "3,4,5\n8,5,5\n4,5,4\n3,4,5", "3,5,5\n6,5,8\n4,4,5\n5,4,3", "3,3,5\n5,5,8\n3,4,4\n5,4,3", "3,4,5\n8,5,5\n4,5,4\n5,4,3", "4,4,5\n5,5,8\n4,5,4\n3,4,5",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: There is a factory that inputs the data of the side and diagonal lengths to the machine and cuts out the plastic plate. At this factory, we cut out only parallelogram molds, although ...
p00163 Highway Toll_836
In 20XX, the Aizu Chuo Road, which has a total distance of 58km and 6 sections from Atsushiokanomachi, Kitakata City to Minamiaizucho, is scheduled to be completed and opened. For half a year after opening, the toll will be halved for vehicles that pass the departure IC or arrival IC between 17:30 and 19:30 and have a...
# Aizu Problem 00163: Highway Tooll # import sys, math, os, bisect # read input: PYDEV = os.environ.get('PYDEV') if PYDEV=="True": sys.stdin = open("sample-input.txt", "rt") M = [[0, 300, 500, 600, 700,1350,1650], [6, 0, 350, 450, 600,1150,1500], [13, 7, 0, 250, 400,1000,1350], [18, 12, 5,...
{ "input": [ "2\n17 25\n4\n17 45\n4\n17 25\n7\n19 35\n0", "2\n17 25\n4\n17 45\n4\n17 25\n7\n1 35\n0", "2\n19 25\n4\n17 37\n4\n17 47\n7\n0 63\n0", "2\n14 25\n4\n3 37\n4\n17 47\n7\n0 63\n0", "2\n13 25\n3\n17 45\n4\n17 25\n7\n1 35\n0", "2\n14 25\n4\n17 37\n0\n17 47\n7\n0 63\n0", "2\n13 25\n3\...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: In 20XX, the Aizu Chuo Road, which has a total distance of 58km and 6 sections from Atsushiokanomachi, Kitakata City to Minamiaizucho, is scheduled to be completed and opened. For ha...
p00320 Cuboid_839
The educational program (AHK Education) of the Aiz Broadcasting Corporation broadcasts a program called "Play with Tsukuro" for children. Today is the time to make a box with drawing paper, but I would like to see if the rectangular drawing paper I prepared can make a rectangular parallelepiped. However, do not cut or ...
lst = [set(map(int, input().split())) for _ in range(6)] rec = [] while lst: x = lst[0] count = lst.count(x) if count % 2 == 1: print("no") break rec.append((count, x)) for _ in range(count): lst.pop(lst.index(x)) else: if len(rec) == 1: if len(rec[0][1]) == 1: print("yes") else: ...
{ "input": [ "2 2\n2 3\n2 3\n2 3\n2 2\n3 2", "2 2\n2 3\n2 3\n2 3\n2 2\n2 2", "2 2\n2 3\n2 3\n2 3\n2 2\n5 2", "2 2\n2 3\n2 2\n2 3\n2 2\n2 2", "2 2\n2 3\n2 3\n2 2\n2 2\n5 2", "2 2\n2 3\n2 2\n2 3\n2 2\n2 3", "2 2\n2 3\n2 3\n2 2\n2 2\n10 2", "2 2\n2 3\n4 2\n2 3\n2 2\n2 3", "2 2\n2 3\n2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The educational program (AHK Education) of the Aiz Broadcasting Corporation broadcasts a program called "Play with Tsukuro" for children. Today is the time to make a box with drawing ...
p00490 Best Pizza_843
problem Chairman K is a regular customer of the JOI pizza shop in the center of JOI city. For some reason, he decided to start a life-saving life this month. So he wanted to order the pizza with the highest calories per dollar among the pizzas he could order at the JOI pizza store. Let's call such a pizza the "best pi...
#C N = int(input()) A,B = map(int,input().split()) C = int(input()) T = [int(input()) for i in range(N)] T.sort(reverse=True) cal = C cost = A for t in T: if cal/cost < (cal+t)/(cost+B): cal+=t cost+=B else: break print(cal//cost)
{ "input": [ "3\n12 2\n200\n50\n300\n100", "3\n12 2\n200\n50\n300\n110", "3\n17 2\n200\n73\n300\n110", "3\n12 2\n200\n50\n599\n100", "3\n12 2\n200\n73\n575\n110", "3\n17 2\n200\n50\n300\n010", "3\n17 2\n63\n50\n300\n010", "3\n12 2\n200\n54\n506\n110", "3\n17 2\n38\n50\n300\n010", ...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: problem Chairman K is a regular customer of the JOI pizza shop in the center of JOI city. For some reason, he decided to start a life-saving life this month. So he wanted to order th...
p00676 KND is So Sexy_847
Problem KND is a student programmer at the University of Aizu. His chest is known to be very sexy. <image> For simplicity, the part of the skin that can be seen from the chest is represented by the isosceles triangle ABC in the figure. However, due to the slack in the clothes, the two sides AC and BC (where these l...
import math while True: try: a,l,x=map(int, input().split()) temp=(l+x)/2 except EOFError: break def heron(i,j,k): d = (i+j+k)/2 return math.sqrt(d*(d-i)*(d-j)*(d-k)) print((str(heron(a,l,l)+heron(l,temp,temp)*2)))
{ "input": [ "2 2 1\n2 3 1\n3 2 3\n2 3 5", "2 2 1\n2 3 1\n3 2 3\n1 3 5", "4 2 1\n2 3 1\n3 2 3\n1 3 5", "2 2 1\n2 3 1\n3 2 5\n1 3 5", "2 2 0\n2 3 1\n3 2 3\n2 3 5", "2 2 1\n2 3 1\n1 2 3\n1 3 5", "4 2 1\n2 3 1\n3 2 3\n1 3 0", "2 2 1\n2 3 1\n1 2 5\n1 3 5", "2 2 0\n2 3 0\n3 2 3\n2 3 5",...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Problem KND is a student programmer at the University of Aizu. His chest is known to be very sexy. <image> For simplicity, the part of the skin that can be seen from the chest is ...
p00819 Unreliable Message_851
The King of a little Kingdom on a little island in the Pacific Ocean frequently has childish ideas. One day he said, “You shall make use of a message relaying game when you inform me of something.” In response to the King’s statement, six servants were selected as messengers whose names were Mr. J, Miss C, Mr. E, Mr. 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": [ "5\nAJMP\naB23d\nE\n86AE\nAM\n6\nJPEM\nWaEaETC302Q\nCP\nrTurnAGundam1isdefferentf", "5\nAJMP\naB33d\nE\n86AE\nAM\n6\nJPEM\nWaEaETC302Q\nCP\nrTurnAGundam1isdefferentf", "5\nAJMP\naB33d\nE\n86AE\nAM\n6\nEPJM\nWaEaETC302Q\nCP\nrTurnAGundam1isdefferentf", "5\nAJMP\naB33d\nE\n86AE\nAM\n0\nEPJM...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The King of a little Kingdom on a little island in the Pacific Ocean frequently has childish ideas. One day he said, “You shall make use of a message relaying game when you inform me ...
p00950 Infallibly Crack Perplexing Cryptarithm_854
Example Input ACM Output 0
from itertools import permutations base = "=+-*()01" s = input() l = len(s) mapping = {} counter = {} cnt = 0 for c in s: if c in base: continue if c not in mapping: mapping[c] = cnt cnt += 1 v = mapping[c] counter[v] = counter.get(v, 0) + 1 if cnt > 8: print(0) exit(0) ...
{ "input": [ "ACM", "ABM", "BIB", "1MI", "MCA", "MAC", "M@C", "C@M", "@MC", "?MC", "M?C", "C?M", "CM?", "DM?", "?MD", "@MD", "@ME", "@EM", "@DM", "MD@", "@CM", "@CN", "NC@", "CN@", "@NC", "@OC", "@CO", "C@O...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input ACM Output 0 ### Input: ACM ### Output: 0 ### Input: ABM ### Output: 0 ### Code: from itertools import permutations base = "=+-*()01" s = input() l = len(s) map...
p01353 Rabbit Plays Games!_860
A rabbit is playing a role-playing game. Just before entering the castle, he was ambushed by an enemy! It was a battle between one hero operated by a rabbit and n enemies. Each character has four stats, health hi, attack power ai, defense power di, and agility si. I = 0 is the information of the main character, 1 ≤ i ...
import sys readline = sys.stdin.readline write = sys.stdout.write def solve(): N = int(readline()) ha, aa, da, sa = map(int, readline().split()) ans = 0 S = [] for i in range(N): hi, ai, di, si = map(int, readline().split()) m0 = max(ai - da, 0) if si > sa: ans +...
{ "input": [ "1\n1 1 1 1\n10000 10000 10000 10000", "2\n10 3 1 2\n2 4 1 3\n2 2 1 1", "1\n1 0 1 1\n10000 10000 10000 10000", "2\n10 3 1 2\n2 4 1 3\n1 2 1 1", "2\n10 3 1 2\n2 4 1 3\n1 3 1 1", "2\n10 3 1 2\n3 4 1 3\n1 3 1 1", "2\n10 3 2 2\n3 4 1 3\n1 3 1 1", "1\n1 0 1 1\n10000 10010 10000...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A rabbit is playing a role-playing game. Just before entering the castle, he was ambushed by an enemy! It was a battle between one hero operated by a rabbit and n enemies. Each chara...
p01835 Donut Decoration_865
Example Input 3 2 3 1 2 1 2 3 2 3 3 1 Output 1
import sys class Set: __slots__ = ["data", "one", "N", "N0", "size"] def __init__(self, N): self.data = [0]*(N+1) self.one = [0]*(N+1) self.N = N self.N0 = 2**(N.bit_length()-1) self.size = 0 def __get(self, k): s = 0 data = self.data while ...
{ "input": [ "3 2\n3\n1 2 1\n2 3 2\n3 3 1", "2 2\n3\n1 2 1\n2 3 2\n3 3 1", "3 2\n1\n1 2 1\n2 3 2\n3 3 1", "2 2\n3\n2 2 1\n2 3 2\n3 3 1", "3 2\n1\n1 1 1\n2 3 2\n3 3 1", "0 2\n3\n1 2 1\n2 3 2\n3 3 1", "0 2\n3\n1 2 1\n2 3 0\n3 3 1", "2 2\n3\n2 2 1\n2 3 4\n3 3 1", "3 2\n1\n1 2 1\n2 3 2...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Example Input 3 2 3 1 2 1 2 3 2 3 3 1 Output 1 ### Input: 3 2 3 1 2 1 2 3 2 3 3 1 ### Output: 1 ### Input: 2 2 3 1 2 1 2 3 2 3 3 1 ### Output: 1 ### Code: import sys class S...
p01970 The Diversity of Prime Factorization_867
D: The Diversity of Prime Factorization Problem Ebi-chan has the FACTORIZATION MACHINE, which can factorize natural numbers M (greater than 1) in O ($ \ log $ M) time! But unfortunately, the machine could display only digits and white spaces. In general, we consider the factorization of M as p_1 ^ {e_1} \ times p_2 ...
from collections import defaultdict MAX = 1000000 ROOT = 1000 MOD = 1000000007 is_prime = [True] * (MAX + 1) is_prime[0] = is_prime[1] = False for i in range(2, ROOT + 1): if is_prime[i]: for j in range(i * i, MAX + 1, i): is_prime[j] = False n = int(input()) qlst = list(map(int, input().split())) total1 =...
{ "input": [ "3\n2 3 3", "3\n2 3 6", "3\n2 3 11", "3\n3 2 3", "3\n2 5 3", "3\n2 2 3", "3\n2 2 11", "3\n3 2 6", "3\n2 2 13", "3\n4 2 6", "3\n2 2 0", "3\n4 2 2", "3\n3 2 0", "3\n2 2 2", "3\n2 3 2", "3\n2 1 2", "3\n2 1 0", "3\n2 2 1", "3\n4 2 1"...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: D: The Diversity of Prime Factorization Problem Ebi-chan has the FACTORIZATION MACHINE, which can factorize natural numbers M (greater than 1) in O ($ \ log $ M) time! But unfortuna...
p02257 Prime Numbers_872
A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program which reads a list of N integers and prints the number of prime numbers in the list. Constraints 1 ≤ N ≤ 10000 2 ≤ an element of the ...
import math n = int(input()) count = 0 for i in range(n): t = int(input()) a = int(t ** (1 / 2)) end = 0 for j in range(2, a + 1): if t % j == 0: end = 1 break if end == 0: count += 1 print(count)
{ "input": [ "5\n2\n3\n4\n5\n6", "11\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17", "11\n7\n8\n9\n10\n11\n12\n13\n14\n27\n16\n17", "11\n7\n8\n9\n10\n11\n22\n13\n2\n27\n8\n17", "5\n2\n4\n7\n5\n6", "11\n7\n8\n9\n26\n15\n22\n2\n14\n27\n8\n27", "11\n14\n8\n9\n26\n15\n22\n2\n14\n27\n8\n27", "11...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7. Write a program...
p02405 Print a Chessboard_876
Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm. .#.#.#.#. .#.#.#.#.# .#.#.#.#. .#.#.#.#.# .#.#.#.#. .#.#.#.#.# Note that the top left corner should be drawn by '#'. Constraints * 1 ≤ H ≤ 300 * 1...
while True: a,b = map(int, input().split()) if a==b == 0: break for i in range(a): s = "" for j in range(b): s += "#" if (i+j) % 2 == 0 else "." print(s) print("")
{ "input": [ "3 4\n5 6\n3 3\n2 2\n1 1\n0 0", "3 4\n5 6\n3 3\n2 2\n1 0\n0 0", "3 7\n5 6\n3 3\n2 2\n1 0\n0 0", "3 7\n5 6\n3 0\n2 2\n1 0\n0 0", "6 7\n5 6\n3 0\n2 2\n1 0\n0 0", "6 11\n5 6\n3 0\n2 2\n1 0\n0 0", "3 4\n5 6\n2 3\n2 2\n1 1\n0 0", "3 4\n5 6\n3 3\n2 4\n1 0\n0 0", "3 7\n5 6\n4...
6AIZU
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Draw a chessboard which has a height of H cm and a width of W cm. For example, the following figure shows a chessboard which has a height of 6 cm and a width of 10 cm. .#.#.#.#. .#....
1030_E. Vasya and Good Sequences_886
Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its binary representation. For example, Vasya can transform number 6 (... 00000000110_2) into 3 (... 00000000011_2), 12 (... 000000001100_2), 10...
#Code by Sounak, IIESTS #------------------------------warmup---------------------------- import os import sys import math from io import BytesIO, IOBase from fractions import Fraction import collections from itertools import permutations from collections import defaultdict from collections import deque import threadi...
{ "input": [ "4\n1 2 1 16\n", "3\n6 7 14\n", "5\n1000000000000000000 352839520853234088 175235832528365792 753467583475385837 895062156280564685\n", "1\n15\n", "1\n4\n", "1\n17\n", "3\n10 7 14\n", "3\n10 7 16\n", "1\n7\n", "1\n24\n", "1\n9\n", "1\n2\n", "1\n10\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Vasya has a sequence a consisting of n integers a_1, a_2, ..., a_n. Vasya may pefrom the following operation: choose some number from the sequence and swap any pair of bits in its bin...
1075_D. Intersecting Subtrees_891
You are playing a strange game with Li Chen. You have a tree with n nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from 1 to n. Neither of you know the other's labelling of the tree. You and Li Chen each chose a subtree (i.e., a connected su...
from collections import deque import sys t = int(input()) for i in range(t): n = int(input()) edge = {} for j in range(1,n+1): a = set() edge[j] = a for k in range(n-1): a,b = map(int,input().split()) edge[a].add(b) edge[b].add(a) k1 = int(input()) x = inp...
{ "input": [ "1\n3\n1 2\n2 3\n1\n1\n1\n2\n2\n1\n", "2\n6\n1 2\n1 3\n1 4\n4 5\n4 6\n4\n1 3 4 5\n3\n3 5 2\n3\n6\n1 2\n1 3\n1 4\n4 5\n4 6\n3\n1 2 3\n3\n4 1 6\n5\n", "1\n1\n1\n1\n1\n1\n1\n", "1\n3\n2 3 1\n1 2\n2 3\n1\n1\n1\n2\n", "2\n6\n5 3 2 4 1 6\n1 2\n1 3\n1 4\n4 5\n4 6\n4\n1 3 4 5\n3\n3 5 2\n6\n5 ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are playing a strange game with Li Chen. You have a tree with n nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the...
1096_E. The Top Scorer_894
Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are p players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability. They have just finished the...
base=998244353; def power(x, y): if(y==0): return 1 t=power(x, y//2) t=(t*t)%base if(y%2): t=(t*x)%base return t; def inverse(x): return power(x, base-2) f=[1] iv=[1] for i in range(1, 5555): f.append((f[i-1]*i)%base) iv.append(inverse(f[i])) def C(n, k): return (f[n]...
{ "input": [ "10 30 10\n", "2 6 3\n", "5 20 11\n", "1 5000 4999\n", "2 1 0\n", "83 2813 123\n", "93 2364 2364\n", "100 1 0\n", "21 862 387\n", "1 1 0\n", "93 2364 1182\n", "1 0 0\n", "100 5000 30\n", "100 0 0\n", "45 2315 2018\n", "45 886 245\n", "69...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are p players doing penalty shoot-outs. Winner is the one who scores the m...
1144_B. Parity Alternated Deletions_900
Polycarp has an array a consisting of n integers. He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it (after the first move the array contains n-1 elements). For each of the next moves he chooses any element with the only restriction: its...
n=int(input()) arr=list(map(int,input().split())) arr.sort() even=[] odd=[] e=0 o=0 for i in arr: if (i%2)==0: even=even+[i] e=e+1 else: odd=odd+[i] o=o+1 if (e>o) and (e-o)>1: print(sum(even[:(e-o-1)])) elif (o>e) and (o-e)>1: print(sum(odd[:(o-e-1)])) else: print(0)
{ "input": [ "2\n1000000 1000000\n", "6\n5 1 2 4 6 3\n", "5\n1 5 7 8 2\n", "5\n1 1 1 1 1\n", "5\n2 1 1 1 1\n", "5\n2 1 1 1 2\n", "6\n5 1 3 4 8 3\n", "5\n1 5 7 1 2\n", "6\n5 1 3 4 5 3\n", "2\n1000010 1001000\n", "2\n1000110 1001000\n", "2\n1000110 1000000\n", "2\n000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Polycarp has an array a consisting of n integers. He wants to play a game with this array. The game consists of several moves. On the first move he chooses any element and deletes it...
1165_A. Remainder_904
You are given a huge decimal number consisting of n digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1. You may perform several (possibly zero) operations with this number. During each operation you are allowed to change any digit of your number; you may change 0...
n,x,y = map(int,input().split()) s = input()[-x:] if(y == 0): num = s[:-(y+1)].count('1') else: num = s[:-(y+1)].count('1') + s[-y:].count('1') if(s[-(y+1)] == "0"): num = num + 1 print(num)
{ "input": [ "11 5 2\n11010100101\n", "11 5 1\n11010100101\n", "6 4 2\n100010\n", "4 2 1\n1000\n", "8 5 2\n10000100\n", "11 5 2\n11010000101\n", "64 40 14\n1010011100101100101011000001000011110111011011000111011011000100\n", "7 5 3\n1011000\n", "8 5 1\n10000000\n", "5 2 1\n1101...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given a huge decimal number consisting of n digits. It is guaranteed that this number has no leading zeros. Each digit of this number is either 0 or 1. You may perform severa...
1202_C. You Are Given a WASD-string..._910
You have a string s — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: * 'W' — move one cell up; * 'S' — move one cell down; * 'A' — move one cell left; * 'D' — move one cell right. Let Grid(s) be the grid of minimum possibl...
def lim(s): now = 0 up, down = 0, 0 for i in s: now += i up = max(up, now) down = min(down, now) return up, down def f(a): return a[0] - a[1] + 1 def upg(s): t = lim(s) up, down = t[0], t[1] arr = [1, 1] now = 0 for i in range(len(s) - 1): if now =...
{ "input": [ "3\nDSAWWAW\nD\nWA\n", "3\nDSAWWAW\nD\nAW\n", "3\nWSAWDAW\nD\nAW\n", "3\nDAAWWSW\nD\nAW\n", "3\nWAWWASD\nD\nWA\n", "3\nASAWWDW\nD\nWA\n", "3\nWAWWASD\nD\nAW\n", "3\nWSAWDAW\nD\nWA\n", "3\nWDWWASA\nD\nWA\n", "3\nWDWWASA\nD\nAW\n", "3\nWSADWAW\nD\nWA\n", "3\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You have a string s — a sequence of commands for your toy robot. The robot is placed in some cell of a rectangular grid. He can perform four commands: * 'W' — move one cell up; ...
1244_C. The Football Season_915
The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets w points, and the opposing team gets 0 points. If the game results in ...
import sys from sys import argv def extendedEuclideanAlgorithm(old_r, r): negative = False s, old_t = 0, 0 old_s, t = 1, 1 if (r < 0): r = abs(r) negative = True while r > 0: q = old_r // r #MCD: r, old_r = old_r - q * r, r #Coeficiente s: ...
{ "input": [ "30 60 3 1\n", "20 0 15 5\n", "10 51 5 4\n", "728961319347 33282698448966372 52437 42819\n", "461788563846 36692905412962338 93797 64701\n", "567018385179 15765533940665693 35879 13819\n", "21644595275 987577030498703 66473 35329\n", "1000000000000 1000000000000 6 3\n", ...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a vict...
1264_A. Beautiful Regional Contest_919
So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i problems. Since the participants are primarily sorted by the number of solved problems, then p_1 ≥ p_2 ≥ ... ≥ p_n. Help the jury distrib...
'''input 5 12 5 4 4 3 2 2 1 1 1 1 1 1 4 4 3 2 1 1 1000000 20 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 32 64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11 ''' t=int(input()) for i in range(t): n=int(input()) s=list(map(int,input().split())) if n//2<3: print("...
{ "input": [ "5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1\n32\n64 64 63 58 58 58 58 58 37 37 37 37 34 34 28 28 28 28 28 28 24 24 19 17 17 17 17 16 16 16 16 11\n", "5\n12\n5 4 4 3 2 2 1 1 1 1 1 1\n4\n4 3 2 1\n1\n1000000\n20\n20 19 18 17 16 15 1...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: So the Beautiful Regional Contest (BeRC) has come to an end! n students took part in the contest. The final standings are already known: the participant in the i-th place solved p_i p...
1285_C. Fadi and LCM_923
Today, Osama gave Fadi an integer X, and Fadi was wondering about the minimum possible value of max(a, b) such that LCM(a, b) equals X. Both a and b should be positive integers. LCM(a, b) is the smallest positive integer that is divisible by both a and b. For example, LCM(6, 8) = 24, LCM(4, 12) = 12, LCM(2, 3) = 6. O...
def LMC(a, b): n = a * b while a != 0 and b != 0: if a > b: a = a % b else: b = b % a nod = a + b nok = n // nod return nok from math import sqrt, ceil n = int(input()) dividers = [] for i in range(1, ceil(sqrt(n))): if n % i == 0: dividers.appe...
{ "input": [ "1\n", "4\n", "6\n", "2\n", "205078485761\n", "873109054817\n", "518649879439\n", "401021537803\n", "821985629174\n", "614685146646\n", "551519879446\n", "583102513046\n", "690824608515\n", "681460970070\n", "355170254369\n", "924639053494\n...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Today, Osama gave Fadi an integer X, and Fadi was wondering about the minimum possible value of max(a, b) such that LCM(a, b) equals X. Both a and b should be positive integers. LCM(...
1304_E. 1-Trees and Queries_927
Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree? Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wan...
import sys, os class RangeQuery: def __init__(self, data, func=min): self.func = func self._data = _data = [list(data)] i, n = 1, len(_data[0]) while 2 * i <= n: prev = _data[-1] _data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)]) ...
{ "input": [ "5\n1 2\n2 3\n3 4\n4 5\n5\n1 3 1 2 2\n1 4 1 3 2\n1 4 1 3 3\n4 2 3 3 9\n5 2 3 3 9\n", "9\n3 9\n3 4\n7 2\n6 9\n5 3\n6 2\n8 3\n1 9\n10\n8 4 8 2 5\n9 2 7 4 4\n8 5 7 3 3\n1 2 3 8 4\n2 9 2 4 3\n6 4 3 4 5\n6 7 6 6 4\n7 5 3 1 4\n5 4 7 8 3\n4 5 1 5 2\n", "14\n4 9\n3 7\n4 1\n3 2\n14 9\n7 6\n10 13\n8 7\...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge i...
1328_F. Make k Equal_931
You are given the array a consisting of n elements and the integer k ≤ n. You want to obtain at least k equal elements in the array a. In one move, you can make one of the following two operations: * Take one of the minimum elements of the array and increase its value by one (more formally, if the minimum value of ...
n, k = map(int, input().split()) a = sorted(list(map(int, input().split()))) cnt = dict() sum = dict() res = n * 20 for x in a: y = x cur = 0 while True: if y == 0: break if y not in cnt: cnt[y] = 0 sum[y] = 0 if cnt[y] < k: cnt[y] +...
{ "input": [ "6 5\n1 2 2 4 2 3\n", "7 5\n3 3 2 1 1 1 3\n", "21 6\n12 15 14 4 4 7 2 4 11 1 15 4 12 11 12 8 11 12 3 4 4\n", "50 25\n19 1 17 6 4 21 9 16 5 21 2 12 17 11 54 18 36 20 34 17 32 1 4 14 26 11 6 2 7 5 2 3 12 16 20 5 16 1 18 55 16 20 2 3 2 12 65 20 7 11\n", "5 2\n9 9 9 9 9\n", "1 1\n1000...
2CODEFORCES
Below is a problem description that describes the problem. Write code in Python that appropriately solves the problem. ### Description: You are given the array a consisting of n elements and the integer k ≤ n. You want to obtain at least k equal elements in the array a. In one move, you can make one of the following ...