Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
t=int(input()) for i in range(t): a=[int(x) for x in list(input())] n=len(a) zero=0 arr=0 for i in range(n): if a[i]==1: size=2 num=1 arr+=1 if i!=n-1: j=i+1 if a[j]==1: num*=2+1 else: num*=2 while num<=size+zero and num>=size: arr+=1 if j==n-1: break j+=1 if a[j]==1: num=num*2+1 else: num*=2 size+=1 zero=0 else: zero+=1 print(arr)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s = input() num_zero = 0 ans = 0 length = len(s) for i in range(length): if s[i] == "0": num_zero += 1 else: act_num = 1 j = i is_right = True while j < length and is_right: if (act_num-(j-i+1)) <= num_zero: ans += 1 j += 1 if j < length: act_num = act_num*2+int(s[j]) else: is_right = False num_zero = 0 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
''' CODED WITH LOVE BY SATYAM KUMAR ''' from sys import stdin, stdout import heapq import cProfile, math from collections import Counter, defaultdict, deque from bisect import bisect_left, bisect, bisect_right import itertools from copy import deepcopy from fractions import Fraction import sys, threading import operator as op from functools import reduce import sys sys.setrecursionlimit(10 ** 6) # max depth of recursion threading.stack_size(2 ** 27) # new thread will get stack of such size fac_warm_up = False printHeap = str() memory_constrained = False P = 10 ** 9 + 7 class MergeFind: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n self.lista = [[_] for _ in range(n)] def find(self, a): to_update = [] while a != self.parent[a]: to_update.append(a) a = self.parent[a] for b in to_update: self.parent[b] = a return self.parent[a] def merge(self, a, b): a = self.find(a) b = self.find(b) if a == b: return if self.size[a] < self.size[b]: a, b = b, a self.num_sets -= 1 self.parent[b] = a self.size[a] += self.size[b] self.lista[a] += self.lista[b] def set_size(self, a): return self.size[self.find(a)] def __len__(self): return self.num_sets def display(string_to_print): stdout.write(str(string_to_print) + "\n") def prime_factors(n): # n**0.5 complex factors = dict() for i in range(2, math.ceil(math.sqrt(n)) + 1): while n % i == 0: if i in factors: factors[i] += 1 else: factors[i] = 1 n = n // i if n > 2: factors[n] = 1 return (factors) def all_factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def fibonacci_modP(n, MOD): if n < 2: return 1 return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn( fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD def factorial_modP_Wilson(n, p): if (p <= n): return 0 res = (p - 1) for i in range(n + 1, p): res = (res * cached_fn(InverseEuler, i, p)) % p return res def binary(n, digits=20): b = bin(n)[2:] b = '0' * (digits - len(b)) + b return b def is_prime(n): """Returns True if n is prime.""" if n < 4: return True if n % 2 == 0: return False if n % 3 == 0: return False i = 5 w = 2 while i * i <= n: if n % i == 0: return False i += w w = 6 - w return True def generate_primes(n): prime = [True for i in range(n + 1)] p = 2 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 1 return prime factorial_modP = [] def warm_up_fac(MOD): nonlocal factorial_modP, fac_warm_up if fac_warm_up: return factorial_modP = [1 for _ in range(fac_warm_up_size + 1)] for i in range(2, fac_warm_up_size): factorial_modP[i] = (factorial_modP[i - 1] * i) % MOD fac_warm_up = True def InverseEuler(n, MOD): return pow(n, MOD - 2, MOD) def nCr(n, r, MOD): nonlocal fac_warm_up, factorial_modP if not fac_warm_up: warm_up_fac(MOD) fac_warm_up = True return (factorial_modP[n] * ( (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD)) % MOD)) % MOD def test_print(*args): if testingMode: print(args) def display_list(list1, sep=" "): stdout.write(sep.join(map(str, list1)) + "\n") def display_2D_list(li): for i in li: print(i) def prefix_sum(li): sm = 0 res = [] for i in li: sm += i res.append(sm) return res def get_int(): return int(stdin.readline().strip()) def get_tuple(): return list(map(int, stdin.readline().split())) def get_list(): return list(map(int, stdin.readline().split())) memory = dict() def clear_cache(): nonlocal memory memory = dict() def cached_fn(fn, *args): nonlocal memory if args in memory: return memory[args] else: result = fn(*args) memory[args] = result return result def ncr(n, r): return math.factorial(n) / (math.factorial(n - r) * math.factorial(r)) def binary_search(i, li): fn = lambda x: li[x] - x // i x = -1 b = len(li) while b >= 1: while b + x < len(li) and fn(b + x) > 0: # Change this condition 2 to whatever you like x += b b = b // 2 return x # -------------------------------------------------------------- MAIN PROGRAM TestCases = True fac_warm_up_size = 10 ** 5 + 100 optimise_for_recursion = False # Can not be used clubbed with TestCases WHen using recursive functions, use Python 3 def main(): li = list(stdin.readline().strip()) k = 0 res = 0 for index, cri in enumerate(li): if cri == '0': k += 1 else: sm = 0 for i in range(18): try: sm = sm * 2 + int(li[index + i]) if i + 1 <= sm <= i + 1 + k: res += 1 except: a = 1 k = 0 print(res) # --------------------------------------------------------------------- END= if TestCases: for i in range(get_int()): main() else: main() if not optimise_for_recursion else threading.Thread(target=main).start()
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
for _ in range(int(input())): s=input() n=len(s) i=0 j=0 c=0 ans=0 while(i<n): if(s[i]=='0'): c+=1 if(s[i]=='1'): x=0 y=0 for j in range(i,n): x=x*2 if(s[j]=='1'): x+=1 # print(x,y,c) if(x-y-1<=c): ans+=1 # print(i,j) else: break y+=1 c=0 i+=1 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from collections import defaultdict from math import log2 def zeros(n): return n - int(log2(n)) - 1 def binary(n): s = "" while(n > 0): s = str(n & 1) + s n = n // 2 return s t = int(input()) d = defaultdict(list) for i in range(1, 2 * 10**5 + 1): z = zeros(i) d[z].append(i) for _ in range(t): s = input() n = len(s) zs = [0] * n z = 0 for i in reversed(list(range(n))): if (s[i] == '0'): z += 1 else: z = 0 zs[i] = z total = 0 for i in range(n): z = zs[i] candidates = d[z] j = i + z for c in candidates: cS = binary(c) cSL = len(cS) jEnd = j + cSL if (jEnd > n): continue if (s[j:jEnd] == cS): total += 1 print(total)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import bisect def solve(s,ans): count = 0 one = [] n = len(s) for i in range(n): if s[i] == '1': one.append(i) for i in range(n): curr = 0 if s[i] == '0': start = bisect.bisect(one,i) if start < len(one): start = one[start] else: start = n else: start = i #print(i,start) for j in range(start,n): curr *= 2 if s[j] == '1': curr += 1 #print(curr,i,j-i+1,j) if curr == j-i+1: count += 1 if curr > n-i: break ans.append(count) def main(): t = int(input()) ans = [] for i in range(t): s = input() solve(s,ans) for i in ans: print(i) main()
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
import math for _ in range(int(input())): s = input() n = len(s) ans = 0 lg = int(math.log2(n)) + 1 npfx = 0 for i in range(n): if s[i] == '0': npfx += 1 continue ans += 1 cv = 1 ln = 1 for j in range(i + 1, min(i + lg + 1, n)): ln += 1 cv *= 2 cv += s[j] == '1' ans += (ln <= cv) and (ln + npfx >= cv) npfx = 0 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from math import log t=int(input()) aa=[1] for i in range(100): aa.append(aa[-1]*2) for _ in range(t): s=input() ii=[] co=0 j=0 ss={} ind=-1 for i in s: if i=="0": if ind==-1: ind=j co+=1 else: if ind!=-1: ss[j-1]=ind co=0 ind=-1 j+=1 if s[-1]=="0": ss[len(s)-1]=ind ans=0 for i in range(1,int(log(len(s))/log(2))+2): for j in range(len(s)-i+1): st=s[j:j+i] if st[0]=="1": tot=0 for ii in range(i): if st[-ii-1]=="1": # print(ii) tot+=aa[ii] le=tot-i try: xx=ss[j-1] if (j-xx)>=le: ans+=1 # print(i,j,tot,st) except: if le==0: ans+=1 # print(i,j,tot,st) pass print(ans) # print()
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
def run(a, ind, l): newSt = '' ans = 0 for i in range(ind, len(a)): newSt += a[i] if int(newSt, 2) == (i - l + 1): ans += 1 if int(newSt, 2) > (i - l + 1): return ans return ans n = int(input()) for kkk in range(n): st = input() uk = [0] * len(st) for i in range(len(uk)): uk[i] = i for j in range(len(uk)): if st[j] == '1': uk[0] = j break for i in range(1, len(uk)): if i < uk[i - 1]: uk[i] = uk[i - 1] else: for j in range(i, len(uk)): if st[j] == '1': uk[i] = j break s = 0 for i in range(len(uk)): if ((uk[i] != i) or st[i] == '1'): s += run(st, uk[i], i) print(s)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
'''input 4 0110 0101 00001000 0001000 ''' for test in range(int(input())): s = input() ans = 0 for l in range(1, min(20, len(s))+1): p = 0 for i in range(len(s)-l+1): if s[i]=='0': p += 1 continue x = int(s[i:i+l], 2) if x>=l and (x-l)<=p: ans+=1 p = 0 print(ans)
You are given a binary string $s$ (recall that a string is binary if each character is either $0$ or $1$). Let $f(t)$ be the decimal representation of integer $t$ written in binary form (possibly with leading zeroes). For example $f(011) = 3, f(00101) = 5, f(00001) = 1, f(10) = 2, f(000) = 0$ and $f(000100) = 4$. The substring $s_{l}, s_{l+1}, \dots , s_{r}$ is good if $r - l + 1 = f(s_l \dots s_r)$. For example string $s = 1011$ has $5$ good substrings: $s_1 \dots s_1 = 1$, $s_3 \dots s_3 = 1$, $s_4 \dots s_4 = 1$, $s_1 \dots s_2 = 10$ and $s_2 \dots s_4 = 011$. Your task is to calculate the number of good substrings of string $s$. You have to answer $t$ independent queries. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) โ€” the number of queries. The only line of each query contains string $s$ ($1 \le |s| \le 2 \cdot 10^5$), consisting of only digits $0$ and $1$. It is guaranteed that $\sum\limits_{i=1}^{t} |s_i| \le 2 \cdot 10^5$. -----Output----- For each query print one integer โ€” the number of good substrings of string $s$. -----Example----- Input 4 0110 0101 00001000 0001000 Output 4 3 4 3
from math import log2 from math import ceil for _ in range(int(input())): S = list(map(int, list(input()))) combs = 0 maxlen = ceil(log2(len(S))) #print(maxlen) prezeros = 0 next1 = [0] * len(S) nxt = len(S) - 1 for i in range(len(S) - 1, -1, -1): if S[i] == 1: nxt = i next1[i] = nxt for l in range(len(S)): if S[l] == 0: nxtl = next1[l] val = 0 lcomb = 0 for r in range(nxtl, min(nxtl + maxlen + 1, len(S))): val = 2 * val + S[r] if val == r - l + 1: # print(l, r) lcomb += 1 combs += lcomb continue val = 0 lcomb = 0 for r in range(l, min(l + maxlen + 1, len(S))): val = 2 * val + S[r] if val == r - l + 1: #print(l, r) lcomb += 1 combs += lcomb prezeros = 0 print(combs)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
def check(M): sm = 0 for i in range(n): if a[i] > M: sm += b[i] return sm <= M gans = [] for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) L = 0 R = max(a) while R - L > 1: M = (L + R) // 2 if check(M): R = M else: L = M gans.append(R) print(*gans, sep='\n')
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
def helper(m): s = 0 for i in range(len(a)): if a[i] > m: s += b[i] return s <= m for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) l, r = 1, 10**9 while l < r: mid = l + (r-l)//2 temp = helper(mid) if temp: r = mid else: l = mid+1 print(l)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
import sys input=sys.stdin.readline def f(x): ans=0 for i in range(n): if(a[i]<=x): continue ans+=b[i] if(ans<=x): return 1 return 0 def bsearch(l,r): m=(l+r)//2 if(f(m)): if(f(m-1)==0): return m return bsearch(l,m-1) return bsearch(m+1,r) t=int(input()) for you in range(t): n=int(input()) l=input().split() a=[int(i) for i in l] l=input().split() b=[int(i) for i in l] print(bsearch(0,10**9+5))
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
for _ in range(int(input())): n = int(input()) a = [*list(map(int, input().split()))] b = [*list(map(int, input().split()))] lo = 0 hi = sum(a) while lo < hi: mid = (lo + hi) // 2 if sum(y if x > mid else 0 for x,y in zip(a,b)) <= mid: hi = mid else: lo = mid + 1 print(lo)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
def f(aa, bb, target): total = 0 for i in range(len(aa)): if aa[i] > target: total += bb[i] return total <= target t = int(input()) for case in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) low = 1 high = 1000000000 while low < high: mid = (low + high) // 2 res = f(a, b, mid) if res: # mid is possible high = mid else: low = mid + 1 print(low)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
"""T=int(input()) for _ in range(0,T): n=int(input()) a,b=map(int,input().split()) s=input() s=[int(x) for x in input().split()] for i in range(0,len(s)): a,b=map(int,input().split())""" T=int(input()) for _ in range(0,T): n=int(input()) a=[int(x) for x in input().split()] b=[int(x) for x in input().split()] low=0 high=max(a) ans=max(a) while(low<=high): mid=(low+high)>>1 tot=0 for i in range(0,len(a)): if(a[i]>mid): tot+=b[i] if(tot<=mid): ans=min(ans, mid) high=mid-1 else: low=mid+1 print(ans)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
import sys def input(): return sys.stdin.readline() for _ in range(int(input())): n = int(input()) A = list(map(int, input().split())) B = list(map(int, input().split())) x = 10 ** 9 y = 0 while x > y + 1: z = (x + y) // 2 t = 0 for i in range(n): if A[i] > z: t += B[i] if t > z: y = z else: x = z print(x)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
import sys import math def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline().strip() t = II() for q in range(t): n = II() a = LI() b = LI() b = sorted(enumerate(b), key=lambda x: a[x[0]]) b = [i[1] for i in b] a.sort() x = [] s = 0 for i in range(n-1,-1,-1): s+=b[i] x.append(s) x = x[:][::-1] ans = s for i in range(n): if i == n-1: ans = min(ans,a[i]) else: ans = min(ans,max(a[i],x[i+1])) print(ans)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
def read_generator(): while True: tokens = input().split(' ') for t in tokens: yield t reader = read_generator() def readword(): return next(reader) def readint(): return int(next(reader)) def readfloat(): return float(next(reader)) def readline(): return input() def solve(a, b, n): l = 1 r = 10 ** 9 while r - l > 1: t = (l + r) // 2 if possible(a, b, n, t): r = t else: l = t if possible(a, b, n, l): return l return r def possible(a, b, n, t): s = 0 for i in range(n): if a[i] > t: s += b[i] return s <= t tests = readint() for t in range(tests): n = readint() a = [readint() for _ in range(n)] b = [readint() for _ in range(n)] print(solve(a, b, n))
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
import sys input = iter(sys.stdin.read().splitlines()).__next__ t = int(input()) for _ in range(t): n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] times = list(zip(a, b)) times.sort() # print(times) pickup_time = sum(b) best_time = pickup_time for num_deliveries in range(1, n+1): pickup_time -= times[num_deliveries-1][1] delivery_time = times[num_deliveries-1][0] best_time = min(best_time, max(pickup_time, delivery_time)) if pickup_time < delivery_time: break print(best_time)
Petya is preparing for his birthday. He decided that there would be $n$ different dishes on the dinner table, numbered from $1$ to $n$. Since Petya doesn't like to cook, he wants to order these dishes in restaurants. Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from $n$ different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant $i$, in this case the courier will arrive in $a_i$ minutes, Petya goes to the restaurant $i$ on his own and picks up the dish, he will spend $b_i$ minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently. For example, if Petya wants to order $n = 4$ dishes and $a = [3, 7, 4, 5]$, and $b = [2, 1, 2, 4]$, then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in $3$ minutes, the courier of the fourth restaurant will bring the order in $5$ minutes, and Petya will pick up the remaining dishes in $1 + 2 = 3$ minutes. Thus, in $5$ minutes all the dishes will be at Petya's house. Find the minimum time after which all the dishes can be at Petya's home. -----Input----- The first line contains one positive integer $t$ ($1 \le t \le 2 \cdot 10^5$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case begins with a line containing one integer $n$ ($1 \le n \le 2 \cdot 10^5$)ย โ€” the number of dishes that Petya wants to order. The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^9$)ย โ€” the time of courier delivery of the dish with the number $i$. The third line of each test case contains $n$ integers $b_1 \ldots b_n$ ($1 \le b_i \le 10^9$)ย โ€” the time during which Petya will pick up the dish with the number $i$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerย โ€” the minimum time after which all dishes can be at Petya's home. -----Example----- Input 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 Output 5 3 2 3
def xxx(x): nonlocal a,b res=0 for i in range(len(a)): if a[i]>x: res+=b[i] if res<=x: return True else: return False for i in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) i1=0 i2=sum(b) while i2-i1>1: m=(i1+i2)//2 if xxx(m): i2=m else: i1=m print(i2)
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
t=int(input()) for you in range(t): n=int(input()) for i in range(n): print(4*n-2*i,end=" ") print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
for _ in range(int(input())): n = int(input()) for i in range(4 * n, 2 * n, -2): print(i, end=' ') print(' ')
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
import sys input=sys.stdin.readline from collections import defaultdict for _ in range(int(input())): n=int(input()) x=4*n for i in range(n): print(x,end=" ") x-=2 print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
for _ in range (int(input())): n=int(input()) for i in range (4*n,4*n-2*n,-2): print(i,end=' ') print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
import sys,math # import re # from heapq import * # from collections import defaultdict as dd # from collections import Counter as cc # sys.setrecursionlimit(10**6)#thsis is must mod = 10**9+7; md = 998244353 input = lambda: sys.stdin.readline().strip() inp = lambda: list(map(int,input().split())) #______________________________________________________ for _ in range(int(input())): n = int(input()) t = 4*n for i in range(n): print(t,end = " ") t-=2 print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
for i in range(int(input())): n=int(input()) l=[] a=4*n for j in range(n): l.append(a) a-=2 print(*l)
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
import sys import math import itertools import functools import collections import operator import fileinput import copy import string ORDA = 97 # a def ii(): return int(input()) def mi(): return list(map(int, input().split())) def li(): return [int(i) for i in input().split()] def lcm(a, b): return abs(a * b) // math.gcd(a, b) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=2): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): new_number = 0 while number > 0: new_number += number % base number //= base return new_number def cdiv(n, k): return n // k + (n % k != 0) def ispal(s): for i in range(len(s) // 2 + 1): if s[i] != s[-i - 1]: return False return True for _ in range(ii()): n = ii() print(*[i for i in range(2 * n + 2, 4 * n + 2, 2)])
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
a = int(input()) for i in range (a): b = int(input()) for j in range (2 * b + 2, 4 * b, 2): print(j, end = " ") print(4 * b)
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
def main(): t = int(input()) for _ in range(t): n = int(input()) p = [] for i in range(n): p.append(4 * n - i * 2) print(*p) main()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
t = int(input()) for case in range(t): n = int(input()) ans = [2 * n + 2 * i + 2 for i in range(n)] print(*ans)
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
import math import sys def chek(m, b, c, li): for i in range(li): if m[i] + b[i] > c: return False return True # 113759 input = lambda: sys.stdin.readline().rstrip() f = int(input()) for _ in range(f): n = int(input()) n = n * 4 for i in range(n, n // 2 , -1): if i % 2 == 0: print(i, end=' ') print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
import sys input=sys.stdin.readline T=int(input()) for _ in range(T): #N,K=map(int,input().split()) #A=list(map(int,input().split())) n=int(input()) v=4*n for i in range(n): print(v,end=" ") v=v-2 print()
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
for i in range(int(input())): n=int(input()) g=[] c=4*n for j in range(n): g.append(c) c=c-2 print(' '.join(map(str,g)))
Today the kindergarten has a new group of $n$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $1$ to $4n$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $a$ and $b$ ($a \neq b$) will indulge if: $gcd(a, b) = 1$ or, $a$ divides $b$ or $b$ divides $a$. $gcd(a, b)$ย โ€” the maximum number $x$ such that $a$ is divisible by $x$ and $b$ is divisible by $x$. For example, if $n=3$ and the kids sit on chairs with numbers $2$, $3$, $4$, then they will indulge since $4$ is divided by $2$ and $gcd(2, 3) = 1$. If kids sit on chairs with numbers $4$, $6$, $10$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $2$ of the kid that can indulge. More formally, she wants no pair of chairs $a$ and $b$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. -----Input----- The first line contains one integer $t$ ($1 \leq t \leq 100$)ย โ€” the number of test cases. Then $t$ test cases follow. Each test case consists of one line containing an integer $n$ ($1 \leq n \leq 100$)ย โ€” the number of kids. -----Output----- Output $t$ lines, which contain $n$ distinct integers from $1$ to $4n$ย โ€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $n$ numbers in any order. -----Example----- Input 3 2 3 4 Output 6 4 4 6 10 14 10 12 8
# map(int, input().split()) def main(): n = int(input()) for i in range(4 * n, 2 * n, -2): print(i, end = ' ') print('') rw = int(input()) for wewq in range(rw): main()
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
import sys import random from fractions import Fraction from math import * def input(): return sys.stdin.readline().strip() def iinput(): return int(input()) def finput(): return float(input()) def tinput(): return input().split() def linput(): return list(input()) def rinput(): return list(map(int, tinput())) def fiinput(): return list(map(float, tinput())) def rlinput(): return list(map(int, input().split())) def trinput(): return tuple(rinput()) def srlinput(): return sorted(list(map(int, input().split()))) def NOYES(fl): if fl: print("NO") else: print("YES") def YESNO(fl): if fl: print("YES") else: print("NO") def main(): n = iinput() #k = iinput() #m = iinput() #n = int(sys.stdin.readline().strip()) #n, k = rinput() #n, m = rinput() #m, k = rinput() #n, k, m = rinput() #n, m, k = rinput() #k, n, m = rinput() #k, m, n = rinput() #m, k, n = rinput() #m, n, k = rinput() #q = srlinput() #q = linput() s, t, res = 1, 1, 0 while s <= n: res += 1 n -= s t = 2 * t + 1 s = (t * (t + 1)) // 2 print(res) for i in range(iinput()): main()
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
for _ in [0]*int(input()): n=int(input());o=0;c=1 while n >= 0: n-=c*(c+1)//2;o+=1;c=2*c+1 print(o-1)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
tests = int(input()) for test in range(tests): n = int(input()) ans = 0 s = 0 for i in range(1, 31): d = 2 ** i - 1 r = d * (d + 1) // 2 if s + r <= n: ans += 1 s += r else: break print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
t = int(input()) for _ in range(t): x = int(input()) ans = 0 size = 1 temp = (size*(size+1))//2 while x >= temp: ans += 1 x -= temp size = 2*size + 1 temp = (size*(size+1))//2 print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
q = int(input()) for _ in range(q): n = int(input()) wyn = 0 pot = 1 total = 1 while total <= n: wyn += 1 pot += 1 total += (2**pot-1)*(2**pot)//2 print(wyn)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
for _ in range(int(input())): n = int(input()) i = 1 have = 0 ans = 0 while have + i * (i + 1) // 2 <= n: have += i * (i + 1) // 2 ans += 1 i = i * 2 + 1 print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
from bisect import bisect_left,bisect_right a=[1] f=1 while a[-1]<=10**18: f=f*2+1 a.append(a[-1]+f*(f+1)//2) for _ in range(int(input())): n=int(input()) print(bisect_right(a,n))
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
import bisect p2 = [2 ** n - 1 for n in range(32)] p2 = [x * (x + 1) // 2 for x in p2] for i in range(1, 32): p2[i] += p2[i - 1] for _ in range(int(input())): n = int(input()) print(bisect.bisect_right(p2, n) - 1)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
for _ in range(int(input())): ans=0 n=int(input()) cp=1 while cp*(cp+1)//2<=n: ans+=1 n-=cp*(cp+1)//2 cp=cp*2+1 print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
y=lambda:int(input()) for _ in range(y()): n=y();c=0 while 2**(2*c+1)-2**c<=n:n-=2**(2*c+1)-2**c;c+=1 print(c)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
l=[] i=1 while(i<10**18+5): l.append((i*(i+1))//2) i=i*2+1 t=int(input()) for you in range(t): n=int(input()) count=0 sum=0 for i in range(len(l)): sum+=l[i] if(sum>n): break print(i)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
from bisect import bisect_right t = int(input()) a = [] s = 0 x = 2 while s <= 10 ** 18: s += x * (x - 1) // 2 a.append(s) x *= 2 for _ in range(t): print(bisect_right(a, int(input())))
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
nice = [1] while nice[-1] <= 10**18: nice.append((nice[-1]<<1)+1) for i in range(len(nice)): nice[i] = nice[i] * (nice[i] + 1) // 2 t = int(input()) for _ in range(t): x = int(input()) i = 0 num = 0 while x > 0 and i < len(nice): if x >= nice[i]: x -= nice[i] num += 1 i += 1 print(num)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
T = int(input()) def need(n): return (n*(n+1))//2 for t in range(T): x = int(input()) ans = 0 nxt = 1 while True: if x < need(nxt): break ans+=1 x -= need(nxt) nxt = nxt*2+1 print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
from math import log2 for _ in range(int(input())): x = int(input()) acc = 0 for i in range(1, 60): acc += (2**i-1) * (2**(i-1)) if acc > x: break print(i-1)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
import sys input = sys.stdin.readline T = int(input()) for t in range(T): X = int(input()) answer = 0 pow2 = 1 while True: r = 2**pow2 - 1 needed = r*(r+1) // 2 if needed <= X: answer += 1 X -= needed else: break pow2 += 1 print(answer)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
for _ in range(int(input())): n = int(input()) i = 1 while True: x = pow(2,i-1)*(pow(2,i)-1) if n < x: break n -= x i += 1 print(i-1)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
gans = [] for _ in range(int(input())): n = int(input()) ans = 0 cur = 1 while n >= cur * (cur + 1) // 2: n -= cur * (cur + 1) // 2 ans += 1 cur = cur * 2 + 1 #print(cur, n) gans.append(ans) print('\n'.join(map(str, gans)))
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
arr=[] s=1 i=1 while s<((10**18)+1): temp=(s*(s+1))//2 arr.append(temp) s+=(2**i) i+=1 t=int(input()) for i in range(t): x=int(input()) ans=0 j=0 while x>0: if arr[j]<=x: x-=arr[j] ans+=1 else: x=0 j+=1 print(ans)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
l = [1] for i in range(70): l.append(2*l[-1] + 1) # print(l[-1]) l = [(ll * (ll+1))//2 for ll in l] lll = [l[0]] for i in range(1, 70): lll.append(lll[-1] + l[i]) l = lll for t in range(int(input())): n = int(input()) for i in range(70): if l[i] > n: print(i) break
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
t = int(input()) j = 0 r = 0 R = [] for i in range(10**5): j = 2*j+1 s = j*(j+1)//2 r += s R.append(r) if r > 10**18: break import bisect for _ in range(t): x = int(input()) i = bisect.bisect_right(R, x) print(i)
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
A = [1] s = 1 for i in range(32): s *= 4 A.append(A[-1] * 2 + s) S = [0] * 33 for i in range(32): S[i+1] = S[i] + A[i] T = int(input()) for _ in range(T): N = int(input()) for i in range(33): if S[i] > N: print(i - 1) break
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $n$ stairs, then it is made of $n$ columns, the first column is $1$ cell high, the second column is $2$ cells high, $\ldots$, the $n$-th column if $n$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $n$ stairs is called nice, if it may be covered by $n$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $7$ stairs looks like: [Image] Find out the maximal number of different nice staircases, that can be built, using no more than $x$ cells, in total. No cell can be used more than once. -----Input----- The first line contains a single integer $t$ $(1 \le t \le 1000)$ ย โ€” the number of test cases. The description of each test case contains a single integer $x$ $(1 \le x \le 10^{18})$ ย โ€” the number of cells for building staircases. -----Output----- For each test case output a single integer ย โ€” the number of different nice staircases, that can be built, using not more than $x$ cells, in total. -----Example----- Input 4 1 8 6 1000000000000000000 Output 1 2 1 30 -----Note----- In the first test case, it is possible to build only one staircase, that consists of $1$ stair. It's nice. That's why the answer is $1$. In the second test case, it is possible to build two different nice staircases: one consists of $1$ stair, and another consists of $3$ stairs. This will cost $7$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $2$. In the third test case, it is possible to build only one of two nice staircases: with $1$ stair or with $3$ stairs. In the first case, there will be $5$ cells left, that may be used only to build a staircase with $2$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $1$. If Jett builds a staircase with $3$ stairs, then there are no more cells left, so the answer is $1$ again.
import sys ii = lambda: sys.stdin.readline().strip() idata = lambda: [int(x) for x in ii().split()] def solve(): ans = 0 cnt = 1 s = 1 n = int(ii()) while s <= n: cnt = 2 * cnt + 1 ans += 1 n -= s s = (cnt * (cnt + 1)) // 2 print(ans) return for t in range(int(ii())): solve()
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
# # ------------------------------------------------ # ____ _ Generatered using # / ___| | | # | | __ _ __| | ___ _ __ ______ _ # | | / _` |/ _` |/ _ \ '_ \|_ / _` | # | |__| (_| | (_| | __/ | | |/ / (_| | # \____\____|\____|\___|_| |_/___\____| # # GNU Affero General Public License v3.0 # ------------------------------------------------ # Author : prophet # Created : 2020-07-12 11:19:01.523119 # UUID : aXsU7xuXyjk3Ky2f # ------------------------------------------------ # production = True import sys, math, collections def input(input_format = 0, multi = 0): if multi > 0: return [input(input_format) for i in range(multi)] else: next_line = sys.stdin.readline()[:-1] if input_format >= 10: use_list = False input_format = int(str(input_format)[-1]) else: use_list = True if input_format == 0: formatted_input = [next_line] elif input_format == 1: formatted_input = list(map(int, next_line.split())) elif input_format == 2: formatted_input = list(map(float, next_line.split())) elif input_format == 3: formatted_input = list(next_line) elif input_format == 4: formatted_input = list(map(int, list(next_line))) elif input_format == 5: formatted_input = next_line.split() else: formatted_input = [next_line] return formatted_input if use_list else formatted_input[0] def out(output_line, output_format = 0, newline = True): formatted_output = "" if output_format == 0: formatted_output = str(output_line) elif output_format == 1: formatted_output = " ".join(map(str, output_line)) elif output_format == 2: formatted_output = "\n".join(map(str, output_line)) print(formatted_output, end = "\n" if newline else "") def log(*args): if not production: print("$$$", end = "") print(*args) enu = enumerate ter = lambda a, b, c: b if a else c ceil = lambda a, b: -(-a // b) def mapl(iterable, format = 0): if format == 0: return list(map(int, iterable)) elif format == 1: return list(map(str, iterable)) elif format == 2: return list(map(list, iterable)) # # >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<< # def solve(): s = input(3) u = [0] * 3 for i in s: if i == "R": u[0] += 1 elif i == "P": u[1] += 1 elif i == "S": u[2] += 1 log(u) y = 0 p = 0 for i, j in enu(u): if j > y: y = j p = i if p == 0: a = "P" elif p == 1: a = "S" elif p == 2: a = "R" out(a * len(s)) return for i in range(input(11)): solve() # solve() # # >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<< #
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
# import sys from collections import Counter # input = sys.stdin.readline # T = int(input()) # for t in range(T): # s = input() # pos = set(range(len(s))) # answer = [''] # for i in range(len(s)): # lets = [s[k] for k in pos] # if not pos: # break # cc = Counter(lets) # fl = cc.most_common()[0][0] # choice = '' # if fl == 'R': # choice = 'P' # elif fl == 'S': # choice = 'R' # else: # choice = 'S' # answer.append(choice) # next_pos = set() # for p in pos: # if s[p] == choice: # np = p+1 # if np >= len(s): # np = 0 # next_pos.add(np) # pos = next_pos # while len(answer) < len(s): # answer.append('R') # print(''.join(answer)) T = int(input()) for t in range(T): s = input() cc = Counter(s) fl = cc.most_common()[0][0] if fl == 'R': choice = 'P' elif fl == 'S': choice = 'R' else: choice = 'S' print(choice*len(s))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
T = int(input()) for _ in range(T): s = input().strip() rc = 0 sc = 0 pc = 0 for c in s: if c == 'R': rc += 1 elif c == 'S': sc += 1 else: pc += 1 if rc == max(rc,sc,pc): print('P'*len(s)) elif sc == max(rc,sc,pc): print('R'*len(s)) else: print('S'*len(s))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
t=int(input()) for you in range(t): s=input() n=len(s) numofr=0 numofs=0 numofp=0 for i in s: if(i=='R'): numofr+=1 elif(i=='S'): numofs+=1 else: numofp+=1 z=max(numofr,numofp,numofs) if(z==numofr): print('P'*n) elif(z==numofs): print('R'*n) else: print('S'*n)
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
import sys import math import collections import heapq def set_debug(debug_mode=False): if debug_mode: fin = open('input.txt', 'r') sys.stdin = fin def int_input(): return list(map(int, input().split())) def __starting_point(): # set_debug(True) t = int(input()) # t = 1 for ti in range(1, t + 1): # n = int(input()) s = input() c = collections.Counter(s) m = max(c['R'], c['S'], c['P']) if m == c['R']: print('P' * len(s)) elif m == c['S']: print('R' * len(s)) else: print('S' * len(s)) __starting_point()
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
t = int(input()) for _ in range(t): s = input() n = len(s) cnt_r = s.count("R") cnt_s = s.count("S") cnt_p = s.count("P") max_cnt = max(cnt_r, cnt_s, cnt_p) if max_cnt == cnt_r: print("P" * n) elif max_cnt == cnt_s: print("R" * n) else: print("S" * n)
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
for _ in range(int(input())): s = input() d = dict() d['R'] = 0 d['S'] = 0 d['P'] = 0 d1 = dict() d1['R'] = 'P' d1['S'] = 'R' d1['P'] = 'S' for i in s: d[i] += 1 ans = '' c = '' mx = -1 for i in list(d.items()): if mx < i[1]: c = d1[i[0]] mx = i[1] print(c * len(s))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
import sys t = int(sys.stdin.readline().strip()) for _ in range(t): s = sys.stdin.readline().strip() x, y, z = s.count('R'), s.count('S'), s.count('P') if max(x, y, z) == x: print('P'*len(s)) elif max(x, y, z) == y: print('R'*len(s)) else: print('S'*len(s))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
#!/usr/bin/env python3 from collections import Counter def ans(S): freqs = Counter(S) arg_max = max(freqs, key=freqs.get) d = { 'R': 'P', 'P': 'S', 'S': 'R' } return d[arg_max]*len(S) T = int(input()) for t in range(T): S = input() print(ans(S))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
t=int(input()) for _ in range(t): s=input() rcount=0 pcount=0 scount=0 for i in range(len(s)): if s[i]=='R': rcount+=1 if s[i]=='S': scount+=1 if s[i]=='P': pcount+=1 ans=[] if rcount>=pcount and rcount>=scount: for i in range(len(s)): ans.append('P') elif scount>=pcount and scount>=rcount: for i in range(len(s)): ans.append('R') else: for i in range(len(s)): ans.append('S') print(''.join(ans))
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $s = s_1 s_2 \dots s_{n}$ of length $n$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $pos$ ($1 \le pos \le n$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $s_{pos}$: if $s_{pos}$ is equal to R the bot chooses "Rock"; if $s_{pos}$ is equal to S the bot chooses "Scissors"; if $s_{pos}$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $s_{pos + 1}$. In the third roundย โ€” on $s_{pos + 2}$ and so on. After $s_n$ the bot returns to $s_1$ and continues his game. You plan to play $n$ rounds and you've already figured out the string $s$ but still don't know what is the starting index $pos$. But since the bot's tactic is so boring, you've decided to find $n$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $c_1 c_2 \dots c_n$ and if the bot starts from index $pos$ then you'll win in $win(pos)$ rounds. Find $c_1 c_2 \dots c_n$ such that $\frac{win(1) + win(2) + \dots + win(n)}{n}$ is maximum possible. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$)ย โ€” the number of test cases. Next $t$ lines contain test casesย โ€” one per line. The first and only line of each test case contains string $s = s_1 s_2 \dots s_{n}$ ($1 \le n \le 2 \cdot 10^5$; $s_i \in \{\text{R}, \text{S}, \text{P}\}$)ย โ€” the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case, print $n$ choices $c_1 c_2 \dots c_n$ to maximize the average number of wins. Print them in the same manner as the string $s$. If there are multiple optimal answers, print any of them. -----Example----- Input 3 RRRR RSP S Output PPPP RSP R -----Note----- In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $n = 4$ rounds, so the average is also equal to $4$. In the second test case: if bot will start from $pos = 1$, then $(s_1, c_1)$ is draw, $(s_2, c_2)$ is draw and $(s_3, c_3)$ is draw, so $win(1) = 0$; if bot will start from $pos = 2$, then $(s_2, c_1)$ is win, $(s_3, c_2)$ is win and $(s_1, c_3)$ is win, so $win(2) = 3$; if bot will start from $pos = 3$, then $(s_3, c_1)$ is lose, $(s_1, c_2)$ is lose and $(s_2, c_3)$ is lose, so $win(3) = 0$; The average is equal to $\frac{0 + 3 + 0}{3} = 1$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game: $\beta$
import sys import math def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return map(int, sys.stdin.readline().split()) def SI(): return sys.stdin.readline().strip() t = II() for q in range(t): s = SI() ans = "" d = {"R":"P","S":"R","P":"S"} m = 0 if s.count("R")>m: m = s.count("R") ans = "R" if s.count("S")>m: m = s.count("S") ans = "S" if s.count("P")>m: ans = "P" ans = d[ans] print(ans*len(s))
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
import sys input = sys.stdin.readline from bisect import bisect_right bin_s = [1] while bin_s[-1] <= 10 ** 9: bin_s.append(bin_s[-1] * 2) def main(): n, q = map(int, input().split()) alst = list(map(int, input().split())) dp = [[-1, -1] for _ in range(n)] dp[0] = [alst[0], 0] for i, a in enumerate(alst[1:], start = 1): dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + a) dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - a) print(max(dp[-1])) for _ in range(int(input())): main()
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
for i in range(int(input())): n, q = list(map(int, input().split())) a = list(map(int, input().split())) tot = 0 small = 400000 big = 0 goingUp = True for i, val in enumerate(a): if goingUp: if val > big: big = val else: tot += big goingUp = False small = val else: if val < small: small = val else: tot -= small goingUp = True big = val if goingUp: tot += big print(tot)
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n,q=list(map(int,input().split())) A=list(map(int,input().split())) #Q=[tuple(map(int,input().split())) for i in range(q)] DP0=[0]*n DP1=[0]*n for i in range(n): DP0[i]=max(DP0[i-1],DP1[i-1]+A[i]) DP1[i]=max(DP1[i-1],DP0[i-1]-A[i]) #print(DP0) #print(DP1) print(DP0[-1])
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
import sys input = sys.stdin.readline for nt in range(int(input())): n,q = map(int,input().split()) a = list(map(int,input().split())) if n==1: print (a[0]) continue dp = [[0,0] for i in range(n)] dp[0][0] = a[0] dp[1][0] = max(a[0],a[1]) dp[1][1] = max(0,a[0]-a[1]) for i in range(2,n): dp[i][0] = max(dp[i-1][0],dp[i-1][1]+a[i]) dp[i][1] = max(dp[i-1][1],dp[i-1][0]-a[i]) print (max(dp[-1][0],dp[-1][1]))
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
t = int(input()) for _ in range(t): n, q = list(map(int, input().split())) a = list(map(int, input().split())) best_p = best_m = 0 for x in a: best_p = max(best_p, best_m - x) best_m = max(best_m, best_p + x) print(max(best_p, best_m))
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n,q = map(int,stdin.readline().split()) a = list(map(int,stdin.readline().split())) dp = [0,float("-inf")] for i in range(n): ndp = [dp[0],dp[1]] ndp[0] = max(ndp[0] , dp[1]-a[i]) ndp[1] = max(ndp[1] , dp[0]+a[i]) dp = ndp print (max(dp))
This is the easy version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokรฉmon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokรฉmon! Pokรฉmon trainer Andrew decided to help Pikachu to build a pokรฉmon army to resist. First, Andrew counted all the pokรฉmonย โ€” there were exactly $n$ pikachu. The strength of the $i$-th pokรฉmon is equal to $a_i$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $b$ from $k$ indices such that $1 \le b_1 < b_2 < \dots < b_k \le n$, and his army will consist of pokรฉmons with forces $a_{b_1}, a_{b_2}, \dots, a_{b_k}$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + \dots$. Andrew is experimenting with pokรฉmon order. He performs $q$ operations. In $i$-th operation Andrew swaps $l_i$-th and $r_i$-th pokรฉmon. Note: $q=0$ in this version of the task. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokรฉmon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokรฉmon, or team R will realize their tricky plan! -----Input----- Each test contains multiple test cases. The first line contains one positive integer $t$ ($1 \le t \le 10^3$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \le n \le 3 \cdot 10^5, q = 0$) denoting the number of pokรฉmon and number of operations respectively. The second line contains $n$ distinct positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) denoting the strengths of the pokรฉmon. $i$-th of the last $q$ lines contains two positive integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) denoting the indices of pokรฉmon that were swapped in the $i$-th operation. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$. -----Output----- For each test case, print $q+1$ integers: the maximal strength of army before the swaps and after each swap. -----Example----- Input 3 3 0 1 3 2 2 0 1 2 7 0 1 2 5 4 3 6 7 Output 3 2 9 -----Note----- In third test case we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $5โˆ’3+7=9$.
INF = 10 ** 15 for _ in range(int(input())): n, q = tuple(map(int, input().split())) arr = list(map(int, input().split())) a = -INF b = 0 for i in arr: if a == -INF: c = 0 d = i else: c = b - i d = a + i a, b = max(a, c), max(b, d) print(max(a, b))
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
for haaghfj in range(int(input())): x,y,k = list(map(int,input().split())) print(k + (y * k + k - 1 +x-2) // (x - 1))
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
import sys import math import collections import bisect import itertools import decimal import copy import heapq # import numpy as np # sys.setrecursionlimit(10 ** 6) INF = 10 ** 20 MOD = 10 ** 9 + 7 # MOD = 998244353 ni = lambda: int(sys.stdin.readline().rstrip()) ns = lambda: list(map(int, sys.stdin.readline().rstrip().split())) na = lambda: list(map(int, sys.stdin.readline().rstrip().split())) na1 = lambda: list([int(x) - 1 for x in sys.stdin.readline().rstrip().split()]) flush = lambda: sys.stdout.flush() # ===CODE=== def main(): t = ni() for _ in range(t): x, y, k = ns() ans = k total = k + k * y - 1 ans += -(-total // (x - 1)) print(ans) def __starting_point(): main() __starting_point()
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
for _ in range(int(input())): x, y, k = [int(s) for s in input().split()] n = ((y + 1) * k - 1 + (x - 2)) // (x - 1) print(n + k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
q = int(input()) for t in range(q): x, y, k = list(map(int, input().split())) a = ((y + 1) * k - 1 + x - 1 - 1) // (x - 1) b = k print(a + b)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
t = int(input()) for i in range(t): x, y, k = list(map(int, input().split())) a = (y + 1) * k - 1 # print(a) print((a - 1) // (x - 1) + 1 + k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
from collections import defaultdict from queue import deque def arrinp(): return [*list(map(int, input().split(' ')))] def mulinp(): return list(map(int, input().split(' '))) def intinp(): return int(input()) def solution(): x,y,k = mulinp() num = y*k + k ans = (num-1)//(x-1) if (num-1)%(x-1) != 0: ans += 1 ans += k print(ans) testcases = 1 testcases = int(input()) for _ in range(testcases): solution()
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
t=int(input()) for i in range(t): z=list(map(int, input().split())) x=z[0] y=z[1] k=z[2] palok=k*y+k-1 ans=0 ans=palok//(x-1) if palok%(x-1)!=0: ans+=1 print(ans+k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
tests = int(input()) for test in range(tests): a = 1 x, y, k = list(map(int, input().split())) a1 = (k * (y + 1) - 1 + x - 2) // (x - 1) print(a1 + k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
t = int(input()) for _ in range(t): x, y, k = map(int, input().split()) n = (k * (y + 1) - 1 + (x - 2)) // (x - 1) print(n + k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
for _ in range(int(input())): x, y, k = map(int, input().split()) print((k*y+k-1+x-2)//(x-1)+k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
from math import ceil def read_int(): return int(input()) def read_ints(): return list(map(int, input().split(' '))) t = read_int() for case_num in range(t): x, y, k = read_ints() a = (k * (y + 1) - 2) // (x - 1) + 1 + k print(a)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
import sys input = sys.stdin.readline for _ in range(int(input())): x, y, k = [int(i) for i in input().split()] stick_need = k+k*y-1 num_stick_trade = (stick_need+x-2)//(x-1) print(num_stick_trade+k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
for _ in range(int(input())): x,y,k=map(int,input().split()) a=(y+1)*k-1 b=x-1 if a%b==0: c=a//b else: c=a//b+1 print(c+k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x)-1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): x,y,k=MI() a=(k*(y+1)-1+x-2)//(x-1) print(a+k)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return list(map(int, minp().split())) def solve(): x, y, k = mints() ta = k*(y+1) d = ((ta-1)+(x-2))//(x-1) print(d+k) for i in range(mint()): solve()
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
#!/usr/bin/env python3 import sys input = sys.stdin.readline t = int(input()) for i in range(t): x, y, k = map(int, input().split()) needed = k + y * k trades = ((needed - 1) + (x - 2)) // (x - 1) trades += k print(trades)
You are playing a very popular game called Cubecraft. Initially, you have one stick and want to craft $k$ torches. One torch can be crafted using one stick and one coal. Hopefully, you've met a very handsome wandering trader who has two trade offers: exchange $1$ stick for $x$ sticks (you lose $1$ stick and gain $x$ sticks). exchange $y$ sticks for $1$ coal (you lose $y$ sticks and gain $1$ coal). During one trade, you can use only one of these two trade offers. You can use each trade offer any number of times you want to, in any order. Your task is to find the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. You have to answer $t$ independent test cases. -----Input----- The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) โ€” the number of test cases. Then $t$ test cases follow. The only line of the test case contains three integers $x$, $y$ and $k$ ($2 \le x \le 10^9$; $1 \le y, k \le 10^9$) โ€” the number of sticks you can buy with one stick, the number of sticks required to buy one coal and the number of torches you need, respectively. -----Output----- For each test case, print the answer: the minimum number of trades you need to craft at least $k$ torches. The answer always exists under the given constraints. -----Example----- Input 5 2 1 5 42 13 24 12 11 12 1000000000 1000000000 1000000000 2 1000000000 1000000000 Output 14 33 25 2000000003 1000000001999999999
t = int(input()) for ii in range(t): x, y, k = map(int, input().split()) coals = k sticks = k sticks = k*y + k num = (sticks-1)//(x-1) if (sticks-1)%(x-1) != 0: num+=1 num += k print(num)
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
import sys #sys.stdin=open("data.txt") input=sys.stdin.readline # this math tutorial is boring classy=set() for i in range(19): for j in range(i): for k in range(j): for a in range(10): # a=0 for good measure for b in range(10): for c in range(10): what=a*10**i+b*10**j+c*10**k classy.add(what) li=sorted(classy) def counting(i): # return len([x for x in li if x <= i])+C lo=0 hi=len(li)-1 while lo<hi: mid=(lo+hi+1)//2 if li[mid]<=i: lo=mid else: hi=mid-1 return lo for _ in range(int(input())): a,b=map(int,input().split()) print(counting(b)-counting(a-1))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
# import collections, atexit, math, sys, bisect sys.setrecursionlimit(1000000) def getIntList(): return list(map(int, input().split())) try : #raise ModuleNotFoundError import numpy def dprint(*args, **kwargs): print(*args, **kwargs, file=sys.stderr) dprint('debug mode') except ModuleNotFoundError: def dprint(*args, **kwargs): pass inId = 0 outId = 0 if inId>0: dprint('use input', inId) sys.stdin = open('input'+ str(inId) + '.txt', 'r') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ if outId>0: dprint('use output', outId) sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #ๆ ‡ๅ‡†่พ“ๅ‡บ้‡ๅฎšๅ‘่‡ณๆ–‡ไปถ atexit.register(lambda :sys.stdout.close()) #idle ไธญไธไผšๆ‰ง่กŒ atexit N, = getIntList() def memo(func): cache={} def wrap(*args): if args not in cache: cache[args]=func(*args) return cache[args] return wrap @memo def comb (n,k): if k>n: return 0 if k==0: return 1 if n==k: return 1 return comb(n-1,k-1) + comb(n-1,k) def getclam(K, left = 3): if K==0: return 1 if left ==0: return 1 s = str(K) l = len(s) r = 0 x = int(s[0]) if l>1: for i in range(left+1): r += comb(l-1,i) * 9 ** i if x>0: for i in range(left): r += comb(l-1,i) * 9 ** i * (x-1) s1 = s[1:] y = 0 if s1: y = int(s1) if x!=0: left-=1 r+= getclam( y, left) return r else: return x+1 for i in range(1000, 1100): continue dprint(i, getclam(i)) for _ in range(N): L,R = getIntList() r = getclam(R) - getclam(L-1) print(r)
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
USE_STDIO = False if not USE_STDIO: try: import mypc except: pass def Cnk(n, k): ans = 1 for i in range(k): ans *= n - i ans //= i + 1 return ans def main(): num = [[0] * 4 for _ in range(19)] for i in range(19): for j in range(4): if j: num[i][j] += num[i][j-1] if i >= j: num[i][j] += 9 ** j * Cnk(i, j) def count(n): if n == 0: return 0 n = list(map(int, str(n))) l = len(n) ans = 0 for i in range(1, l): ans += 9 * num[i - 1][2] cur = 3 for i in range(l): if n[i] > 0: ans += (n[i] - 1) * num[l - i - 1][cur - 1] if i: ans += num[l - i - 1][cur] cur -= 1 if cur <= 0: break ans += 1 return ans q, = list(map(int, input().split(' '))) for _ in range(q): L, R = list(map(int, input().split(' '))) ans = count(R) - count(L - 1) print(ans) def __starting_point(): main() __starting_point()
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
t = int(input()) arr = [] for i in range(1, 19): arr.append((i - 1) * (i - 2) // 2 * 9 * 9 * 9 + (i - 1) * 9 * 9 + 9) pref = [0] for i in arr: pref.append(pref[-1] + i) def f(x): if x == 0: return 0 s = str(x) n = len(s) ans = pref[n - 1] cnt = 0 for i in range(n): a = int(s[i]) if a != 0: if cnt == 0: ans += (a - 1) * (n - i - 1) * (n - i - 2) // 2 * 9 * 9 + (a - 1) * (n - i - 1) * 9 + (a - 1) cnt += 1 elif cnt == 1: ans += (n - i - 1) * (n - i - 2) // 2 * 9 * 9 + (n - i - 1) * 9 + 1 if a != 1: ans += (a - 1) * (n - i - 1) * 9 + (a - 1) cnt += 1 elif cnt == 2: ans += (n - i - 1) * 9 + 1 if a != 1: ans += (a - 1) cnt += 1 break return ans + 1 for i in range(t): l, r = map(int, input().split()) l -= 1 print(f(r) - f(l)) '''n = int(input()) arr1 = list(map(int, input().split())) m = int(input()) arr2 = list(map(int, input().split())) l = 0 r = 0 if arr1[l] == arr2[r] ''' ''' n, k = map(int, input().split()) print((k + n - 1) // n) '''
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
def f(n): if n == 0: return 1 dp = [[[0] * 2 for j in range(4)] for z in range(len(n))] dp[0][3][0] = 1 dp[0][2][0] = int(n[0]) - 1 dp[0][2][1] = 1 for i in range(1, len(n)): for j in range(4): if n[i] == '0': dp[i][j][0] += dp[i - 1][j][0] dp[i][j][1] += dp[i - 1][j][1] else: dp[i][j][0] += dp[i - 1][j][0] + dp[i - 1][j][1] for z in range(1, 10): if z < int(n[i]): if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] + dp[i - 1][j + 1][1] elif z == int(n[i]): if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] dp[i][j][1] += dp[i - 1][j + 1][1] else: if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] res = 0 for j in range(4): res += dp[len(n) - 1][j][0] + dp[len(n) - 1][j][1] return res t = int(input()) while t: t -= 1 l, r = list(map(int, input().split())) print(f(str(r)) - f(str(l - 1)))
Let's call some positive integer classy if its decimal representation contains no more than $3$ non-zero digits. For example, numbers $4$, $200000$, $10203$ are classy and numbers $4231$, $102306$, $7277420000$ are not. You are given a segment $[L; R]$. Count the number of classy integers $x$ such that $L \le x \le R$. Each testcase contains several segments, for each of them you are required to solve the problem separately. -----Input----- The first line contains a single integer $T$ ($1 \le T \le 10^4$) โ€” the number of segments in a testcase. Each of the next $T$ lines contains two integers $L_i$ and $R_i$ ($1 \le L_i \le R_i \le 10^{18}$). -----Output----- Print $T$ lines โ€” the $i$-th line should contain the number of classy integers on a segment $[L_i; R_i]$. -----Example----- Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
def f(n): if n == 0: return 1 dp = [[[0] * 2 for j in range(4)] for z in range(len(n))] dp[0][3][0] = 1 dp[0][2][0] = int(n[0]) - 1 dp[0][2][1] = 1 for i in range(1, len(n)): for j in range(4): if n[i] == '0': dp[i][j][0] += dp[i - 1][j][0] dp[i][j][1] += dp[i - 1][j][1] else: dp[i][j][0] += dp[i - 1][j][0] + dp[i - 1][j][1] if j >= 3: continue for z in range(1, 10): if z < int(n[i]): dp[i][j][0] += dp[i - 1][j + 1][0] + dp[i - 1][j + 1][1] elif z == int(n[i]): dp[i][j][0] += dp[i - 1][j + 1][0] dp[i][j][1] += dp[i - 1][j + 1][1] else: dp[i][j][0] += dp[i - 1][j + 1][0] res = 0 for j in range(4): res += dp[len(n) - 1][j][0] + dp[len(n) - 1][j][1] return res t = int(input()) while t: t -= 1 l, r = list(map(int, input().split())) print(f(str(r)) - f(str(l - 1)))