message
stringlengths
2
23.8k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
97
109k
cluster
float64
0
0
__index_level_0__
int64
194
217k
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,358
0
178,716
Tags: implementation, number theory, strings Correct Solution: ``` import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush # from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction import copy import time starttime = time.time() mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,4))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") # def seive(): # prime=[1 for i in range(10**6+1)] # prime[0]=0 # prime[1]=0 # for i in range(10**6+1): # if(prime[i]): # for j in range(2*i,10**6+1,i): # prime[j]=0 # return prime s=input() n=len(s) A=[i for i in range(n+1)] def par(x): if A[x]==x: return x return par(A[x]) def union(x,y): u=par(x) v=par(y) if u==v: return if u<v: A[v]=u else: A[u]=v for i in range(2,n+1): if A[i]!=i: continue for j in range(2*i,n+1,i): union(i,j) d={} for i in range(1,n+1): d[A[i]]=d.get(A[i],0)+1 cnt={} for c in s: cnt[c]=cnt.get(c,0)+1 B1=[[d[i],i] for i in d] B2=[[cnt[i],i] for i in cnt] B1.sort(reverse=True) B2.sort() i=0 C={} if len(B1)<len(B2): print("NO") exit() while(i<len(B1)): x=B1[i][0] for j in range(len(B2)): if B2[j][0]>=x: B2[j][0]-=x C[B1[i][1]]=B2[j][1] break else: print("NO") exit() i+=1 print("YES") for i in range(1,n+1): print(C[A[i]],end="") print() endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}") ```
output
1
89,358
0
178,717
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,359
0
178,718
Tags: implementation, number theory, strings Correct Solution: ``` #------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' MOD=1000000007 def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def primeN(n): prime = [True for i in range(n+1)] prime[0]=False prime[1]=False p=2 while(p*p<=n): if(prime[p]): for i in range(p*p,n+1,p): prime[i]=False p+=1 return [p for p in range(n+1) if(prime[p])] s=input() n=len(s) primes=primeN(n) C=Counter(s) ans=['' for i in range(n)] have={} for i in primes: have[i]=n//i moreThanOne=[] need=set() for p in have: if(have[p]>1): moreThanOne.append(p) for i in range(p-1,n,p): need.add(i) need=len(need) # print(need,have) ok=False for i in C: if(C[i]>=need): ok=True ele=i C[i]-=need break if(not ok): print("NO") exit() print("YES") for p in moreThanOne: for i in range(p-1,n,p): ans[i]=ele rem=list(set(s)) ind=0 for i in range(n): if(ans[i]==''): while(C[rem[ind]]==0): ind+=1 ans[i]=rem[ind] C[rem[ind]]-=1 print(*ans,sep="") ```
output
1
89,359
0
178,719
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,360
0
178,720
Tags: implementation, number theory, strings Correct Solution: ``` #!/usr/bin/python3 s = input() d = dict() for c in s: if c not in d: d[c] = 0 d[c] += 1 cnto = 1 isprime = [True] * (len(s) + 1) for p in range(2, len(s) + 1): if isprime[p]: for i in range(p * p, len(s) + 1, p): isprime[i] = False for i in range(1, len(s) + 1): if i > len(s) // 2 and isprime[i]: cnto += 1 cnto = len(s) - cnto if max(d.values()) < cnto: print("NO") else: print("YES") m = max(d.values()) for c, v in d.items(): if v == m: d[c] -= cnto mc = c break ans = [] buf = [] for c, v in d.items(): for i in range(v): buf.append(c) for i in range(1, len(s) + 1): if i == 1 or (i > len(s) // 2 and isprime[i]): ans.append(buf[-1]) buf.pop() else: ans.append(mc) print("".join(ans)) ```
output
1
89,360
0
178,721
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,361
0
178,722
Tags: implementation, number theory, strings Correct Solution: ``` from collections import Counter def is_prime(x): if x < 2: return 0 for i in range(2, x): if x % i == 0: return False return True def proc(s): n = len(s) same = set() for p in range(2,n+1): if not is_prime(p): continue if p * 2 > n: continue for i in range(2, n//p+1): same.add(p*i) same.add(p) counter = Counter(s) ch, count = counter.most_common(1)[0] if count < len(same): print("NO") return same = [x-1 for x in same] w = [x for x in s] for i in same: if w[i] == ch: continue for j in range(n): if j not in same and w[j] == ch: tmp = w[j] w[j] = w[i] w[i] = tmp break print("YES") print(''.join(w)) s = input() proc(s) ```
output
1
89,361
0
178,723
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,362
0
178,724
Tags: implementation, number theory, strings Correct Solution: ``` import math ch='abcdefghijklmnopqrstuvwxyz' def sieve(n): p = 2 while (p * p <= n): if (prime[p] == True): for i in range(p * 2, n + 1, p): prime[i] = False p += 1 prime[0]= False prime[1]= False s = ['#']+list(input()) lis=[0]*26 n = len(s)-1 prime = [True for i in range(1000 + 1)] sieve(1000) ans=['']*(n+1) aa=[] aa.append(1) for i in s[1:]: lis[ord(i)-ord('a')]+=1 for i in range(n//2+1,n+1,1): if prime[i]: aa.append(i) v = n-len(aa) th=-1 for i in range(26): if lis[i]>=v: th=i if th==-1: print("NO") exit() for i in range(2,n+1): if i not in aa: ans[i]=ch[th] lis[th]-=1 j=0 #print(ans,aa,lis) for i in aa: while j<26 and lis[j]<=0: j+=1 ans[i]=ch[j] lis[j]-=1 # print(ans,lis) print("YES") print(*ans[1:],sep='') ```
output
1
89,362
0
178,725
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,363
0
178,726
Tags: implementation, number theory, strings Correct Solution: ``` S = input() s = [] d = {} n = 0 for i in S: if ord('a') <= ord(i) <= ord('z'): n += 1 if i in d: d[i] += 1 else: d[i] = 1 primes = [] for i in range(max(3,n // 2 + 1), n+1): j = 2 f = 0 while j * j <= i: if i % j == 0: f = 1 break j += 1 if not f: primes.append(i) b = [] for i in d: b.append([d[i], i]) ans = ['' for i in range(n)] b.sort() for i in primes: ans[i-1] = b[0][1] b[0][0] -= 1 if b[0][0] == 0: b.pop(0) if len(b) >2 or (len(b)==2 and b[0][0]!=1): print('NO') else: print('YES') for i in ans: if i == '': print(b[0][1], end='') b[0][0]-=1 if b[0][0]==0: b.pop(0) else: print(i,end='') ```
output
1
89,363
0
178,727
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,364
0
178,728
Tags: implementation, number theory, strings Correct Solution: ``` def prime(a): i = 2 while i * i <= a: if a % i == 0: return False i += 1 return True tab = list(input()) letter_count = {} for letter in tab: if letter in letter_count: letter_count[letter] += 1 else: letter_count[letter] = 1 max_amount_letter = 'a' max_amount = 0 for letter, amount in letter_count.items(): if max_amount < amount: max_amount = amount max_amount_letter = letter good_prime = [False for i in range(len(tab))] good_prime[0] = True for n in range((len(tab) + 2) // 2, (len(tab) + 1)): good_prime[n - 1] = prime(n) if max_amount >= good_prime.count(False): print("YES") letter_count[max_amount_letter] = 0 for i in range(len(good_prime)): if good_prime[i] == False: good_prime[i] = max_amount_letter max_amount -= 1 for i in range(len(good_prime)): if good_prime[i] == True and max_amount > 0: good_prime[i] = max_amount_letter max_amount -= 1 i = 0 for letter, amount in letter_count.items(): for a in range(amount): while good_prime[i] != True: i += 1 good_prime[i] = letter i += 1 print(''.join(good_prime)) else: print("NO") ```
output
1
89,364
0
178,729
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.
instruction
0
89,365
0
178,730
Tags: implementation, number theory, strings Correct Solution: ``` from collections import Counter d, t = 'NO', input() c, n = Counter(t), len(t) p = [0] * (n + 1) for i in range(2, n // 2 + 1): if 1 - p[i]: p[i::i] = [1] * (n // i) s = sum(p) u = v = '' for q, k in c.items(): if v or k < s: u += q * k else: u += q * (k - s) v = q if v: d, j = 'YES\n', 0 for i in range(1, n + 1): if p[i]: d += v else: d += u[j] j += 1 print(d) ```
output
1
89,365
0
178,731
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import os,io from sys import stdout import collections import random import math from operator import itemgetter input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter # import sys # sys.setrecursionlimit(10**6) def primes(n): """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) return [2] + [i for i in range(3,n,2) if sieve[i]] def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def powerOfK(k, max): if k == 1: return [1] if k == -1: return [-1, 1] result = [] n = 1 while n <= max: result.append(n) n *= k return result def prefixSum(arr): for i in range(1, len(arr)): arr[i] = arr[i] + arr[i-1] return arr def divisors(n): i = 1 result = [] while i*i <= n: if n%i == 0: if n/i == i: result.append(i) else: result.append(i) result.append(n/i) i+=1 return result # from functools import lru_cache # @lru_cache(maxsize=None) s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) c = Counter(s) dp = [0] * len(s) primes = primes(len(s)+1) components = [] for p in primes: l = [] for i in range(p, len(s)+1, p): l.append(i) for comp in components: if len(list(set(comp) & set(l))): comp += l break else: if len(l): components.append(l) need = [] for comp in components: need.append(len(set(comp))) import heapq heap = [] for e, v in c.items(): heapq.heappush(heap, (-v, e)) result = [""] * (len(s)) for comp in components: i, letter = heapq.heappop(heap) i = -i indexes = set(comp) if i >= len(indexes): heapq.heappush(heap, (-(i-len(indexes)), letter)) for index in set(comp): result[index-1] = letter else: print("NO") break else: while len(heap): i, letter = heapq.heappop(heap) for j in range(len(result)): if i == 0: break if result[j] == "": result[j] = letter i -= 1 print("YES") print("".join(result)) # a = sorted(list(c.values())) # b = sorted(need) # # i, j = len(b)-1, len(a)-1 # while i >= 0 and j >= 0: # if b[i] <= a[j]: # a[j] -= b[i] # i -= 1 # else: # j -= 1 # # if i == -1: # print("YES") # else: # print("NO") ```
instruction
0
89,366
0
178,732
Yes
output
1
89,366
0
178,733
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` s = input() if len(s) == 1: exit(print('YES\n' + s)) d, ans, sieve = {}, [0] * len(s), [0] * 1005 for i in s: d[i] = d.get(i, 0) + 1 for i in range(2, len(s) + 1): if not sieve[i]: for j in range(i * i, 1005, i): sieve[j] = 1 mx = max(d, key=lambda x: d[x]) for j in range(2, len(s) // 2 + 1): ans[j - 1] = mx d[mx] -= 1 if d[mx] < 0: exit(print('NO')) for i in range(len(s) // 2 + 1, len(s) + 1): if not sieve[i]: mx = max(d, key=lambda x: d[x] and x != ans[1]) if not d[mx]: mx = ans[1] ans[i - 1] = mx d[mx] -= 1 else: ans[i - 1] = ans[1] d[ans[1]] -= 1 if d[mx] < 0 or d[ans[1]] < 0: exit(print('NO')) print('YES') mx = max(d, key=lambda x: d[x]) print(mx + ''.join(ans[1:])) ```
instruction
0
89,367
0
178,734
Yes
output
1
89,367
0
178,735
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import sys import math from collections import defaultdict MAXNUM = math.inf MINNUM = -1 * math.inf ASCIILOWER = 97 ASCIIUPPER = 65 def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write() pass def getPrimes(s): primes = [2] MAX = s + 1 for i in range(3, MAX): STOP = math.ceil(math.sqrt(i) + 1) isPrime = True for j in range(2, STOP): if i % j == 0: isPrime = False if isPrime: primes.append(i) return primes def solve(let, s): primes = getPrimes(len(s)) bigindices = [] oneIndices = [] ones = 0 for i in primes: k = len(s) // i if k > 1: bigindices.append(i) if k == 1: oneIndices.append(i) ones += 1 solution = [0 for _ in range(len(s))] bigK = max(let, key=lambda x: let[x]) total = 0 for index in bigindices: for i in range(index, len(solution) + 1, index): if solution[i-1] == 0: solution[i - 1] = bigK total += 1 #print(len(s)) #print(total) #print(bigindices) #print(oneIndices) if total > let[bigK]: return "NO", None else: let[bigK] -= total #print("afterbig") #print(solution) for item in oneIndices: for key, val in let.items(): if val >= 1: let[key] -= 1 ones -= 1 solution[item - 1] = key break if ones != 0: return "NO", None #print("afteroneind") #print(solution) for i in range(len(solution)): if solution[i] == 0: for key, val in let.items(): if val > 0: val -= 1 solution[i] = key break return "YES", "".join(solution) def readinput(): lettercount = defaultdict(int) string = getString() for ele in string: lettercount[ele] += 1 ans = solve(lettercount, string) print(ans[0]) if ans[0] != "NO": print(ans[1]) readinput() ```
instruction
0
89,368
0
178,736
Yes
output
1
89,368
0
178,737
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` def h(s, k): res = '' for i in s: if i != k: res += i return res def ans(s, k, times, Pr: list): d = h(s, k) j = 0 res = '' cc = 0 for i in range(len(s)): if (i + 1) not in Pr: res += k cc += 1 elif j < len(d): res += d[j] j += 1 else: res += k return res def ans1(s, k, times, Pr: list): d = h(s, k) res = '' res += d[0] j = 1 for i in range(len(s) - 1): if (i + 2) not in Pr: res += k elif j < len(d): res += d[j] j += 1 else: res += k return res def isprime(n): i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True def getprimes(n): res = [] for i in range(2, n + 1): if isprime(i): res.append(i) return res s = input() A = [] Primes = getprimes(len(s)) def f(A: list, l): res = 0 resPr = [] for i in A: if i > l // 2: res += 1 resPr.append(i) return res, resPr g, Pr = f(Primes, len(s)) Dict = dict() for i in s: if i in Dict: Dict[i] += 1 else: Dict[i] = 1 for k, v in Dict.items(): if v >= len(s) - g: print('YES') print(ans(s, k, len(s) - g, Pr), end='') exit() elif v == len(s) - g - 1: print('YES') print(ans1(s, k, len(s) - g, Pr), end='') exit() print('NO') ```
instruction
0
89,369
0
178,738
Yes
output
1
89,369
0
178,739
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` def h(s, k): res = '' for i in s: if i != k: res += i return res def ans(s, k, times, Pr: list): d = h(s, k) j = 0 res = '' cc = 0 for i in range(len(s)): if (i + 1) not in Pr: res += k cc += 1 elif j < len(d): res += d[j] j += 1 else: res += k return res def ans1(s, k, times, Pr: list): d = h(s, k) res = '' res += d[0] m = s.find(d[0]) res += ans(s[:m] + s[m + 1:], k, times, Pr) return res def isprime(n): i = 2 while i*i <= n: if n % i == 0: return False i += 1 return True def getprimes(n): res = [] for i in range(2, n + 1): if isprime(i): res.append(i) return res s = input() A = [] Primes = getprimes(len(s)) def f(A: list, l): res = 0 resPr = [] for i in A: if i > l // 2: res += 1 resPr.append(i) return res, resPr g, Pr = f(Primes, len(s)) Dict = dict() for i in s: if i in Dict: Dict[i] += 1 else: Dict[i] = 1 for k, v in Dict.items(): if v >= len(s) - g: print('YES') print(ans(s, k, len(s) - g, Pr), end='') exit() elif v == len(s) - g - 1: print('YES') print(ans1(s, k, len(s) - g, Pr), end='') exit() print('NO') ```
instruction
0
89,370
0
178,740
No
output
1
89,370
0
178,741
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` s = input() d, ans, sieve = {}, [0] * len(s), [0] * 1005 for i in s: d[i] = d.get(i, 0) + 1 for i in range(2, len(s) + 1): if not sieve[i]: for j in range(i * i, 1005, i): sieve[j] = 1 mx = max(d, key=lambda x: d[x]) for j in range(2, (len(s) + 1) // 2): ans[j - 1] = mx d[mx] -= 1 if d[mx] < 0: exit(print('NO')) for i in range((len(s) + 1) // 2, len(s) + 1): if not sieve[i]: mx = max(d, key=lambda x: d[x]) ans[i - 1] = mx d[mx] -= 1 else: ans[i - 1] = ans[1] d[ans[1]] -= 1 if d[mx] < 0 or d[ans[1]] < 0: exit(print('NO')) print('YES') mx = max(d, key=lambda x: d[x]) print(mx + ''.join(ans[1:])) ```
instruction
0
89,371
0
178,742
No
output
1
89,371
0
178,743
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import os,io from sys import stdout import collections import random import math from operator import itemgetter input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from collections import Counter # import sys # sys.setrecursionlimit(10**6) def primes(n): """ Returns a list of primes < n """ sieve = [True] * n for i in range(3,int(n**0.5)+1,2): if sieve[i]: sieve[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) return [2] + [i for i in range(3,n,2) if sieve[i]] def binomial_coefficient(n, k): if 0 <= k <= n: ntok = 1 ktok = 1 for t in range(1, min(k, n - k) + 1): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def powerOfK(k, max): if k == 1: return [1] if k == -1: return [-1, 1] result = [] n = 1 while n <= max: result.append(n) n *= k return result def prefixSum(arr): for i in range(1, len(arr)): arr[i] = arr[i] + arr[i-1] return arr def divisors(n): i = 1 result = [] while i*i <= n: if n%i == 0: if n/i == i: result.append(i) else: result.append(i) result.append(n/i) i+=1 return result # from functools import lru_cache # @lru_cache(maxsize=None) s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) c = Counter(s) dp = [0] * len(s) primes = primes(len(s)) components = [] for p in primes: l = [] for i in range(p, len(s)+1, p): l.append(i) for comp in components: if len(list(set(comp) & set(l))): comp += l break else: if len(l): components.append(l) need = [] for comp in components: need.append(len(set(comp))) import heapq heap = [] for e, v in c.items(): heapq.heappush(heap, (-v, e)) result = [""] * (len(s)) for comp in components: i, letter = heapq.heappop(heap) i = -i indexes = set(comp) if i >= len(indexes): heapq.heappush(heap, (-(i-len(indexes)), letter)) for index in set(comp): result[index-1] = letter else: print("NO") break else: while len(heap): i, letter = heapq.heappop(heap) for j in range(len(result)): if i == 0: break if result[j] == "": result[j] = letter i -= 1 print("YES") print("".join(result)) # a = sorted(list(c.values())) # b = sorted(need) # # i, j = len(b)-1, len(a)-1 # while i >= 0 and j >= 0: # if b[i] <= a[j]: # a[j] -= b[i] # i -= 1 # else: # j -= 1 # # if i == -1: # print("YES") # else: # print("NO") ```
instruction
0
89,372
0
178,744
No
output
1
89,372
0
178,745
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. Submitted Solution: ``` import math from os import startfile import random from queue import Queue import time import heapq import sys def get_primes(n): arr=[True]*(n+1) for i in range(2,int(math.sqrt(n))+1): if arr[i]: for j in range(i**2,len(arr),i): arr[i]=False primes=[] for i in range(2,len(arr)): if arr[i]: primes.append(i) return primes def main(s): n=len(s) primes=get_primes(n) cnt={} for e in s: if e not in cnt: cnt[e]=0 cnt[e]+=1 s=[0]*n for p in primes: num_needed=max(0,(n-2*p)//p)+1 a,b=None,float('inf') for e in cnt: if num_needed<=cnt[e] and cnt[e]<b: a=e b=cnt[e] if b==float('inf'): print("NO") return else: cnt[a]-=num_needed for i in range(p,n+1,p): s[i-1]=a if cnt[a]==0: cnt.pop(a) left=[] for e in cnt: for i in range(cnt[e]): left.append(e) idx=0 for i in range(len(s)): if not s[i]: s[i]=left[idx] idx+=1 print("YES") print(''.join(s)) return s=input() (main(s)) ```
instruction
0
89,373
0
178,746
No
output
1
89,373
0
178,747
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,741
0
179,482
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` from collections import defaultdict import sys, os, math if __name__ == "__main__": #n, m = list(map(int, input().split())) a00, a01, a10, a11 = map(int, input().split()) x, y = int((1 + math.sqrt(1 + 8 * a11)) // 2), int((1 + math.sqrt(1 + 8 * a00)) // 2) if a11 + a10 + a01 + a00 == 0: print(1) sys.exit(0) elif a11 + a10 + a01 == 0 and y * (y - 1) // 2 == a00: print('0' * y) sys.exit(0) elif a00 + a10 + a01 == 0 and x * (x - 1) // 2 == a11: print('1' * x) sys.exit(0) if y * (y - 1) // 2 != a00 or x * (x - 1) // 2 != a11 or x * y != a10 + a01: print("Impossible") sys.exit(0) l = x - math.ceil((x * y - a10) / y) #no.of left one r = (x * y - a10) // y #no.of right one diff = a10 - l * y #print("x, y, l, r, diff") #print(x, y, l, r, diff) if diff == 0: print('1' * l + '0' * y + '1' * r) else: print('1' * l + '0' * (y - diff) + '1' + '0' * diff + '1' * r) ```
output
1
89,741
0
179,483
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,742
0
179,484
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` a00, a01, a10, a11 = map(int, input().split()) numZeros = 0 numOnes = 0 ans = True for n0 in range(1, 2*a00 + 1): if n0 * (n0 - 1) == 2 * a00: numZeros = n0 break elif n0 * (n0 - 1) > 2 * a00: ans = False break; for n1 in range(1, 2*a11 + 1): if n1 * (n1 - 1) == 2 * a11: numOnes = n1 break elif n1 * (n1 - 1) > 2 * a11: ans = False break; res = [] def generateResult(x,a01, a10, n0, n1): while n0 > 0 or n1 > 0: if a01 >= n1 and n1 > 0: if n0 >= 1: res.append(0) a01 = a01 - n1 n0 = n0-1 else: return elif a10 >= n0 and n0 > 0: if n1 >= 1: res.append(1) a10 = a10 - n0 n1 = n1-1 else: return elif n0 > 0 and n1 > 0: return elif 0 < n0 == a01 + a10 + n0 + n1: for i in range(n0): res.append(0) n0 = 0 elif 0 < n1 == a01 + a10 + n0 + n1: for i in range(n1): res.append(1) n1 = 0 else: return if a01 > 0 or a10 > 0: numOnes = max(numOnes, 1) numZeros = max(numZeros, 1) if a00 == a01 == a10 == a11 == 0: print(1) elif ans: generateResult(res, a01, a10, numZeros, numOnes) if len(res) == numZeros + numOnes: print("".join(map(str, res))) else: print("Impossible") else: print("Impossible") ```
output
1
89,742
0
179,485
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,743
0
179,486
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Jun 1 15:32:44 2020 @author: shailesh """ import math a00,a01,a10,a11 = [int(i) for i in input().split()] #l = sorted([a00,a01,a10,a11]) done = 0 if a00==0 and a11 == 0: if a01 + a10 > 1: print('Impossible') done = 1 if not done: N0 = (1+math.sqrt(1+8*a00))/2 N1 = (1+math.sqrt(1+8*a11))/2 if a00 == 0 and N1==int(N1): if a01 == 0 and a10 == 0: print('1'*int(N1)) else: if a01 + a10 == N1: print(a10*'1'+'0'+a01*'1') else: print('Impossible') elif a11 == 0 and N0 == int(N0): if a01 == 0 and a10 == 0: print('0'*int(N0)) else: if a01 + a10 == N0: print(a01*'0'+'1'+a10*'0') else: print('Impossible') elif N0*N1 != a01 + a10 or int(N1) != N1 or int(N0)!=N0: print('Impossible') else: N0 = int(N0) N1 = int(N1) divisor,remainder = divmod(a10,N0) s = '1'*divisor + (N0 - remainder)*'0' if remainder!=0: s+=('1' + remainder*'0') N1-=1 s+= (N1-divisor)*'1' print(s) ```
output
1
89,743
0
179,487
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,744
0
179,488
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` #!/usr/bin/env python3.5 import sys import math def read_data(): return tuple(map(int, next(sys.stdin).split())) def reverse_count(k): d = math.sqrt(1 + 8*k) n = int((1+d)/2 + .5) if n*(n-1) == 2*k: return n return None def solve1(a, b, x01, x10): if x01 + x10 != a * b: return None characters = [] while a > 0 or b > 0: if a >= 1 and x01 >= b: a -= 1 x01 -= b characters.append('0') elif b >= 1 and x10 >= a: b -= 1 x10 -= a characters.append('1') else: return None return ''.join(characters) def solve(x00, x01, x10, x11): if x00 == 0: if x11 == 0: if x01 == x10 == 0: return "0" return solve1(1, 1, x01, x10) b = reverse_count(x11) if b is not None: return solve1(0, b, x01, x10) or solve1(1, b, x01, x10) return None if x11 == 0: a = reverse_count(x00) if a is not None: return solve1(a, 0, x01, x10) or solve1(a, 1, x01, x10) return None a = reverse_count(x00) b = reverse_count(x11) if a is not None and b is not None: return solve1(a, b, x01, x10) return None if __name__ == "__main__": x00, x01, x10, x11 = read_data() s = solve(x00, x01, x10, x11) if s is None: print("Impossible") else: print(s) ```
output
1
89,744
0
179,489
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,745
0
179,490
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` from math import * from sys import * def calc(f): return int((1+sqrt(8*f+1))//2) a,b,c,d=(int(z) for z in input().split()) if a==d==0 and b==1 and c==0: print("01") exit(0) if a==d==0 and b==0 and c==1: print("10") exit(0) if a==d==0: if b+c!=0: print("Impossible") exit(0) print(0) exit(0) if a==0: if(sqrt(8*d+1)!=int(sqrt(8*d+1))): print("Impossible") exit(0) if b==c==0: ans="1"*calc(d) print(ans) else: if b+c!=calc(d): print("Impossible") exit(0) ans=c*"1"+"0"+b*"1" print(ans) exit(0) if d==0: if(sqrt(8*a+1)!=int(sqrt(8*a+1))): print("Impossible") exit(0) if b==c==0: ans="0"*calc(a) print(ans) else: if b+c!=calc(a): print("Impossible") exit(0) ans=b*"0"+"1"+c*"0" print(ans) exit(0) if(sqrt(8*d+1)!=int(sqrt(8*d+1))): print("Impossible") exit(0) if(sqrt(8*a+1)!=int(sqrt(8*a+1))): print("Impossible") exit(0) x=calc(a) y=calc(d) if x*y!=b+c: print("Impossible") exit(0) ans="" res=0 while res+x<=c: ans+="1" y-=1 res+=x while res+x!=c: ans+="0" x-=1 if res!=c: ans+="1" y-=1 while x: x-=1 ans+="0" while y: y-=1 ans+="1" print(ans) ```
output
1
89,745
0
179,491
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,746
0
179,492
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` import sys import math def check(): n = 1000000 for i in range(n): if is_square(1+8*i): print(i) def is_square(integer): root = math.sqrt(integer) if int(root + 0.5) ** 2 == integer: return True else: return False def main(): a,b,c,d = map(int,sys.stdin.readline().split()) if not is_square(1+8*a) or not is_square(1+8*d): print("Impossible") return z = (1+math.sqrt(1+8*a)) o = (1+math.sqrt(1+8*d)) if z%2==1 or o%2==1: print("Impossible") return z = int(z/2) o= int(o/2) if a==0 and b==0 and c==0: z=0 if d==0 and b==0 and c==0: o=0 if z*o != b+c: print("Impossible") return # if z==0: # x = ['1']*o # print(''.join(x)) # return # if o==0: # x = ['0']*z # print(''.join(x)) # return if z==0 and o ==0: print(0) return s = z*o ans = [] while True: if s-z >=b: ans.append('1') s-=z o-=1 else: ans.append('0') z-=1 if z==0 and o==0: break print(''.join(ans)) #check() main() ```
output
1
89,746
0
179,493
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,747
0
179,494
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` def impossible(): print("Impossible") exit() def good(ones, zeros): return ones * zeros == a01 + a10 a00, a01, a10, a11 = map(int, input().split()) if int(round((a00 * 8 + 1) ** 0.5)) ** 2 != a00 * 8 + 1: impossible() if int(round((a11 * 8 + 1) ** 0.5)) ** 2 != a11 * 8 + 1: impossible() zeros = 1 + int(round((a00 * 8 + 1) ** 0.5)) zeros //= 2 ones = 1 + int(round((a11 * 8 + 1) ** 0.5)) ones //= 2 if ones == 1: if not good(ones, zeros) and not good(ones - 1, zeros): impossible() elif good(ones - 1, zeros): ones -= 1 if zeros == 1: if not good(ones, zeros) and not good(ones, zeros - 1): impossible() elif good(ones, zeros - 1) and not good(ones, zeros): zeros -= 1 if zeros == 0 and ones == 0: impossible() if zeros * ones != a01 + a10: impossible() if zeros != 0: start = a10 // zeros end = a01 // zeros if a01 % zeros == 0 and a10 % zeros == 0: print('1' * start + '0' * zeros + '1' * end) else: a01 %= zeros a10 %= zeros print('1' * start + a01 * '0' + '1' + a10 * '0' + '1' * end) else: print('1' * ones) ```
output
1
89,747
0
179,495
Provide tags and a correct Python 3 solution for this coding contest problem. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110
instruction
0
89,748
0
179,496
Tags: constructive algorithms, greedy, implementation, math Correct Solution: ``` def F(s): x00 = x01 = x10 = x11 = 0 for i in range(len(s)): for j in range(i + 1, len(s)): cur = s[i] + s[j] if cur == '00': x00 += 1 if cur == '01': x01 += 1 if cur == '10': x10 += 1 if cur == '11': x11 += 1 return x00, x01, x10, x11 def F2(s): x00 = x01 = x10 = x11 = 0 c0 = s.count(0) c1 = s.count(1) x00 = c0 * (c0 - 1) // 2 x11 = c1 * (c1 - 1) // 2 cur0 = 0 cur1 = 0 for i in range(len(s)): if s[i] == 0: x10 += cur1 cur0 += 1 else: x01 += cur0 cur1 += 1 return x00, x01, x10, x11 def fail(): print('Impossible') exit() a00, a01, a10, a11 = map(int, input().split()) f = lambda x: x * (x - 1) // 2 L, R = 0, 10 ** 6 while R - L > 1: M = (L + R) // 2 if f(M) >= a00: R = M else: L = M c0 = R L, R = 0, 10 ** 6 while R - L > 1: M = (L + R) // 2 if f(M) >= a11: R = M else: L = M c1 = R if a00 == 0 and a11 == 0: if (a01, a10) == (1, 0): s = [0, 1] elif (a01, a10) == (0, 1): s = [1, 0] elif (a01, a10) == (0, 0): s = [1] else: fail() print(''.join(map(str, s))) exit() elif a00 == 0: if (a01, a10) == (0, 0): c0 = 0 elif a11 == 0: if (a01, a10) == (0, 0): c1 = 0 s = [0] * c0 + [1] * c1 b01 = c0 * c1 if b01 != a01 + a10: fail() for i in range(c1): if b01 - c0 < a01: j = i + c0 break b01 -= c0 s[i], s[i + c0] = s[i + c0], s[i] while b01 > a01: s[j], s[j - 1] = s[j - 1], s[j] b01 -= 1 j -= 1 if F2(s) != (a00, a01, a10, a11): fail() print(''.join(map(str, s))) ```
output
1
89,748
0
179,497
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110 Submitted Solution: ``` #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Jun 1 15:32:44 2020 @author: shailesh """ import math a00,a01,a10,a11 = [int(i) for i in input().split()] #l = sorted([a00,a01,a10,a11]) done = 0 if a00==0 and a11 == 0: if a01 + a10 > 1: print('Impossible') done = 1 if not done: N0 = (1+math.sqrt(1+8*a00))/2 N1 = (1+math.sqrt(1+8*a11))/2 if N0*N1 != a01 + a10 or int(N1) != N1 or int(N0)!=N0: print('Impossible') elif a00 == 0 and N1==int(N1): if a01 == 0 and a10 == 0: print('1'*int(N1)) else: print(a10*'1'+'0'+a01*'1') elif a11 == 0 and N0 == int(N0): if a01 == 0 and a10 == 0: print('0'*int(N0)) else: print(a01*'0'+'1'+a10*'0') else: N0 = int(N0) N1 = int(N1) divisor,remainder = divmod(a10,N0) s = '1'*divisor + (N0 - remainder)*'0' if remainder!=0: s+=('1' + remainder*'0') N1-=1 s+= (N1-divisor)*'1' print(s) ```
instruction
0
89,751
0
179,502
No
output
1
89,751
0
179,503
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110 Submitted Solution: ``` from collections import defaultdict import sys, os, math if __name__ == "__main__": #n, m = list(map(int, input().split())) a00, a01, a10, a11 = map(int, input().split()) x, y = int((1 + math.sqrt(1 + 8 * a11)) // 2), int((1 + math.sqrt(1 + 8 * a00)) // 2) if a11 + a10 + a01 + a00 == 0: print(1) sys.exit(0) elif a11 + a10 + a01 == 0 and y * (y - 1) // 2 == a00: print('0' * y) sys.exit(0) elif a00 + a10 + a01 == 0 and x * (x - 1) // 2 == a11: print('1' * x) sys.exit(0) if y * (y - 1) // 2 != a00 or x * (x - 1) // 2 != a11 or x * y != a10 + a01: print("Impossible") sys.exit(0) l = x - math.ceil((x * y - a10) / y) #no.of left one r = (x * y - a10) // y #no.of right one diff = a10 - l * y #print("x, y, l, r, diff") #print(x, y, l, r, diff) if diff == 0: print('1' * l + '0' * y + '1' * r) else: print('1' * l + '0' * diff + '1' + '0' * (y - diff) + '1' * r) ```
instruction
0
89,752
0
179,504
No
output
1
89,752
0
179,505
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 and a11, where axy is the number of subsequences of length 2 of the string s equal to the sequence {x, y}. In these problem you are given four integers a00, a01, a10, a11 and have to find any non-empty string s that matches them, or determine that there is no such string. One can prove that if at least one answer exists, there exists an answer of length no more than 1 000 000. Input The only line of the input contains four non-negative integers a00, a01, a10 and a11. Each of them doesn't exceed 109. Output If there exists a non-empty string that matches four integers from the input, print it in the only line of the output. Otherwise, print "Impossible". The length of your answer must not exceed 1 000 000. Examples Input 1 2 3 4 Output Impossible Input 1 2 2 1 Output 0110 Submitted Solution: ``` from collections import defaultdict import sys, os, math if __name__ == "__main__": #n, m = list(map(int, input().split())) a11, a10, a01, a00 = map(int, input().split()) x, y = int((1 + math.sqrt(1 + 8 * a11)) // 2), int((1 + math.sqrt(1 + 8 * a00)) // 2) if a11 + a10 + a01 + a00 == 0: print(1) sys.exit(0) elif a11 + a10 + a01 == 0 and y * (y - 1) // 2 == a00: print('0' * y) sys.exit(0) elif a00 + a10 + a01 == 0 and x * (x - 1) // 2 == a11: print('1' * x) sys.exit(0) if y * (y - 1) // 2 != a00 or x * (x - 1) // 2 != a11 or x * y != a10 + a01: print("Impossible") sys.exit(0) l = x - math.ceil((x * y - a10) / y) #no.of left one r = (x * y - a10) // y #no.of right one diff = a10 - l * y print("x, y, l, r, diff") print(x, y, l, r, diff) if diff == 0: print('1' * l + '0' * y + '1' * r) else: print('1' * l + '0' * diff + '1' + '0' * (y - diff) + '1' * r) ```
instruction
0
89,754
0
179,508
No
output
1
89,754
0
179,509
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,789
0
179,578
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` S=str(input()) n=int(input()) b=str(input()) M=[b] m=11 for i in range(n-1): f=0 b=str(input()) for i in range(len(M)): if len(M[i])>=len(b): M=M[:i]+[b]+M[i:] f=1 break if f==0: M+=[b] f=0 L=[] for i in M: f=0 for j in M: if j!=i and j in i: f=1 if f==0: L+=[i] f=0 k=0 p=0 v=0 s=0 for i in range(len(S)): if k==0: s=i for j in L: if S[i:i+len(j)]==j: k+=len(j)-1 if k>p: f=1 p=k v=s k=-1 break k+=1 if f==0 or k>p: p,v=k,s print(p,v) ```
output
1
89,789
0
179,579
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,790
0
179,580
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` S=str(input()) n=int(input()) b=str(input()) M=[b] m=11 for i in range(n-1): f=0 b=str(input()) for i in range(len(M)): if len(M[i])>=len(b): M=M[:i]+[b]+M[i:] f=1 break if f==0: M+=[b] f=0 L=[] for i in M: f=0 for j in M: if j!=i and j in i: f=1 if f==0: L+=[i] f=0 k=0 p=0 v=0 s=0 for i in range(len(S)): if k==0: s=i for j in L: if S[i:i+len(j)]==j: k+=len(j)-1 if k>p: f=1 p=k v=s k=-1 break k+=1 if f==0 or k>p: p,v=k,s print(p,v) # Made By Mostafa_Khaled ```
output
1
89,790
0
179,581
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,791
0
179,582
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ########################################################## from collections import Counter, defaultdict import math for _ in range(1): #n=input() #n,m=map(int, input().split()) #arr =list(map(int, input().split())) #for i in range(n): s= input() data=[] for _ in range(int(input())): data.append(input()) cnt = 0 pos = 0 l = 0 r = 1 length = len(s) while r <= length: found = False size = 0 for sub in data: if (r - l >= len(sub)) and (sub == s[max(l, r - len(sub)):r]): found = True size = max(len(sub), size) # break if not found: r += 1 else: l = r - size + 1 if cnt < r - l - 1: cnt = r - l - 1 pos = l print(cnt, pos) ```
output
1
89,791
0
179,583
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,792
0
179,584
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` # import functools # # # class Automaton: # def __init__(self, start, ends, transitions): # self.start = start # self.ends = set(ends) # self.transitions = transitions # [dict] # # def next_states(self, states, char): # states = functools.reduce(set.union, (self.transitions[state].get(char, set()) for state in states), set()) # states.add(0) # return states # # def char_keys(self, states): # return functools.reduce(set.union, map(set, (self.transitions[state].keys() for state in states)), set()) # # def okay(self, word): # states = {self.start} # for char in word: # states = self.next_states(states, char) # # return len(states.intersection(self.ends)) > 0 # # def determistic(self): # transitions = [dict() for _ in range(2 ** len(self.transitions) + 1)] # get_id = lambda states: sum(2 ** state for state in states) # start = get_id({0}) # ends = set() # # l = {(0,)} # seen = set() # # while l: # states = set(l.pop()) # seen.add(tuple(states)) # id = get_id(states) # if self.ends.intersection(states): # ends.add(id) # # for char in self.char_keys(states): # transitions[id][char] = self.next_states(states, char) # if not tuple(transitions[id][char]) in seen: # l.add(tuple(transitions[id][char])) # transitions[id][char] = {get_id(transitions[id][char])} # # return Automaton(start, ends, transitions) # # @staticmethod # def suffix_word_automaton(word): # return Automaton( # 0, # {len(word)}, # [{word[i]: {i + 1}} for i in range(len(word))] + [{}], # ) # # @staticmethod # def KMP(word, text): # A = Automaton.suffix_word_automaton(word) # states = {A.start} # # for i, char in enumerate(text): # states = A.next_states(states, char) # if states.intersection(A.ends): # yield i - len(word) + 1 # def KMP(word, text): # r = [0] * (len(word) + 1) # r[0] = -1 # Si la comparaison plante à la comparaison des j-ième lettres, on peut reprendre à partir de r[j] # # j = -1 # for i in range(1, len(word) + 1): # while 0 <= j < len(word) and word[i - 1] != word[j]: # j = r[j] # Si ça plante, on essaie de continuer une sous-chaîne plus petite # # j += 1 # On passe à la lettre suivante # # r[i] = j # # j = 0 # indice de la lettre du mot qu'on est en train de lire # for i in range(len(text)): # while j >= 0 and text[i] != word[j]: # j = r[j] # Idem, si ça plante, on essaie de continuer une sous-chaîne plus petite # # j += 1 # # if j == len(word): # on a fini # yield i - len(word) + 1 # j = r[j] def word_present(words, text, s): for word in words: if word == text[s:s + len(word)]: return word # try: # if False not in (word[i] == text[s + i] for i in range(len(word))): # return word # except: # pass def smallest_ends(words, text): words = list(sorted(words, key=len)) d = [float("inf")] * len(text) for i in range(0, len(text)): word = word_present(words, text, i) if word: # e = (i, i + len(word)) d[i] = min(len(word), d[i]) # for word in words: # for i in KMP(word, text): # d[i] = min(len(word), d[i]) return d if __name__ == '__main__': text = input() n = int(input()) words = [input() for _ in range(n)] ends = smallest_ends(words, text) longuest_starting = [0] * (len(text) + 1) for i in range(len(text) - 1, -1, -1): longuest_starting[i] = min(1 + longuest_starting[i + 1], ends[i] - 1) i_max = max(range(len(text), -1, -1), key=lambda i: longuest_starting[i]) print(longuest_starting[i_max], i_max if longuest_starting[i_max] else 0) ```
output
1
89,792
0
179,585
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,793
0
179,586
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` s = input() n = int(input()) b = [] for i in range(n): b.append(input()) s_original = s s_dict = {s: 1} for bi in b: while True: new_s_dict = {} for s in s_dict.keys(): part_list = s.split(bi) if len(part_list) <= 1: new_s_dict[s] = 1 continue for i, part in enumerate(part_list): # first if i == 0: new_s_dict[part + bi[:-1]] = 1 # middles if 0 < i < len(part_list) - 1: new_s_dict[bi[1:] + part + bi[:-1]] = 1 # last if i == len(part_list) - 1: new_s_dict[bi[1:] + part] = 1 if s_dict == new_s_dict: break s_dict = new_s_dict max_length = 0 result = "" for s in s_dict.keys(): if max_length < len(s): max_length = len(s) result = s print(max_length, s_original.index(result)) ```
output
1
89,793
0
179,587
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,794
0
179,588
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` #On récupère les données chaine = input() n = int(input()) all_borings = sorted([input() for _ in range(n)], key=lambda s : len(s)) #On traite un cas particulier à priori pb chaine_uniforme = True; first = chaine[0] for c in chaine: if c != first: chaine_uniforme = False; break; if chaine_uniforme: plus_court = len(chaine) for bi in all_borings: uniforme = True; for c in bi: if c != first: uniforme = False; break; if uniforme: plus_court = len(bi)-1 break print(plus_court, 0) exit(); #On prétraitre borings = [] for s in all_borings: li = len(s)-1 for s2, l2 in borings: pos = s.find(s2); if pos != -1: li = min(li, pos + l2) borings.append((s, li)); ##borings = sorted([input() for _ in range(n)], key=lambda s : len(s)) def allOcc(chaine, s, li, occ): n = 0 prev = -1; while n != len(occ): n = len(occ) pos = chaine.find(s, prev+1) if pos != -1: occ.append((pos, li)) prev = pos return occ prev_pos = -2 def f(pos): global prev_pos; if pos[0] != prev_pos: prev_pos = pos[0] return True; return False; all_occ = [(len(chaine)-1, 1)] [allOcc(chaine, bi, li, all_occ) for bi, li in borings] all_occ.sort() all_pos = [occ for occ in all_occ if f(occ)]; prev = -1 best = 0 best_pos = 0 for pos, li in all_pos: n_len = pos +li - prev - 1 if n_len > best: best = n_len best_pos = prev+1 prev = pos ###Approche dynamique ##length, pos = 0, 0 ##for debut in range(len(chaine)-1, -1, -1): ## n_len = len(chaine) - debut ## for i, bi in enumerate(borings): ## li = len(bi) ## if chaine[debut: debut+li] == bi: ## last_founds[i] = debut; ## if n_len > length: ## long = last_founds[i] + li - 1 - debut ## n_len = min(long, n_len) ## ## if n_len > length: ## length = n_len ## pos = debut print(best, best_pos) ```
output
1
89,794
0
179,589
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,795
0
179,590
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n for p in t: i, m = -1, len(p) - 1 while True: i = s.find(p, i + 1) if i == -1: break r[i] += 1 r[i + m] += 2 d, j, q = 0, -1, [-1] for i in range(n): if r[i] & 1: q.append(i) if r[i] & 2: j = q.pop(0) if i - j > d: d, k = i - j, j j = q.pop(0) if n - j > d: d, k = n - j, j print(d - 1, k + 1) ```
output
1
89,795
0
179,591
Provide tags and a correct Python 3 solution for this coding contest problem. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
instruction
0
89,796
0
179,592
Tags: data structures, dp, greedy, hashing, strings, two pointers Correct Solution: ``` R = lambda : map(int, input().split()) s = input() n = int(input()) words = [input() for i in range(n)] dp = [-1] * len(s) for w in words: si = 0 while si != -1: si = s.find(w, si) if si >= 0: dp[si + len(w) - 1] = max(si + 1, dp[si + len(w) - 1]) si += 1 fl = 0 fr = 0 l = 0 found = False for r in range(len(s)): if dp[r] >= 0: l = max(l, dp[r]) if r - l >= fr - fl: found = True fl, fr = l, r print(fr - fl + found, fl) ```
output
1
89,796
0
179,593
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. Submitted Solution: ``` #------------------------template--------------------------# import os import sys # from math import * from collections import * # from fractions import * # from heapq import* from bisect import * from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") ALPHA='abcdefghijklmnopqrstuvwxyz' M=10**9+7 EPS=1e-6 def Ceil(a,b): return a//b+int(a%b>0) def value():return tuple(map(int,input().split())) def array():return [int(i) for i in input().split()] def Int():return int(input()) def Str():return input() def arrayS():return [i for i in input().split()] #-------------------------code---------------------------# # vsInput() def check(start,end): # print(s[start:end],start,end) here='' c=0 while(end>=start and c<10): here+=s[end] end-=1 c+=1 if(here in have): return end+1 return -1 s=input() n=len(s) m=Int() have=set() for i in range(m): have.add(input()[::-1]) ans=(0,0) i=0 while(i<n): pos=i l=0 while(i<n and check(pos,i)==-1): l+=1 i+=1 if(l>ans[1]): ans=(pos,l) if(i==n):break i=check(pos,i)+1 print(*ans[::-1]) ```
instruction
0
89,797
0
179,594
Yes
output
1
89,797
0
179,595
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. Submitted Solution: ``` #On récupère les données chaine = input() n = int(input()) all_borings = sorted([input() for _ in range(n)], key=lambda s : len(s)) #On prétraitre borings = [] for s in all_borings: li = len(s)-1 for s2, l2 in borings: pos = s.find(s2); if pos != -1: li = pos + l2 borings.append((s, li)); ##borings = sorted([input() for _ in range(n)], key=lambda s : len(s)) def allOcc(chaine, s, li, occ): n = 0 prev = -1; print(s) while n != len(occ): n = len(occ) pos = chaine.find(s, prev+1) if pos != -1: occ.append((pos, li)) prev = pos print(" ", pos, li) return occ prev_pos = -2 def f(pos): global prev_pos; if pos[0] != prev_pos: prev_pos = pos[0] return True; return False; all_occ = [(len(chaine)-1, 2)] [allOcc(chaine, bi, li, all_occ) for bi, li in borings] all_occ.sort() all_pos = [occ for occ in all_occ if f(occ)]; print(all_pos) prev = -1 best = 0 best_pos = 0 for pos, li in all_pos: n_len = pos +li - prev - 2 print(n_len, prev+1) if n_len > best: best = n_len best_pos = prev+1 prev = pos ###Approche dynamique ##length, pos = 0, 0 ##for debut in range(len(chaine)-1, -1, -1): ## n_len = len(chaine) - debut ## for i, bi in enumerate(borings): ## li = len(bi) ## if chaine[debut: debut+li] == bi: ## last_founds[i] = debut; ## if n_len > length: ## long = last_founds[i] + li - 1 - debut ## n_len = min(long, n_len) ## ## if n_len > length: ## length = n_len ## pos = debut print(best, best_pos) ```
instruction
0
89,798
0
179,596
No
output
1
89,798
0
179,597
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. Submitted Solution: ``` s, n = input(), int(input()) t = [input() for i in range(n)] def g(i): global t for j in range(n): if i != j and len(t[i]) > len(t[j]): if t[j] in t[i]: return False return True t = [t[i] for i in range(n) if g(i)] def f(i): global s, t k = len(s) - i for p in t: if len(p) < k and all(c == s[j] for j, c in enumerate(p, i)): return i + len(p) return 0 u, n = 2, len(s) v = k = -1 for i in range(n): j = f(i) if j: if j - k > u: u, v = j - k, k k = i j = n + 1 if j - k > u: u, v = j - k, k print(u - 2, v + 1) ```
instruction
0
89,799
0
179,598
No
output
1
89,799
0
179,599
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. Submitted Solution: ``` a=input() n=int(input()) b=[] for i in range(n): c=input() d=a.find(c) while d!=-1: b.append([d,len(c)]) d=a.find(c,d+1) b.sort() b.append([b[-1][0]+1,len(a)-b[-1][0]]) c=[] for i in range(len(b)-1): c.append([b[i+1][0]+b[i+1][1]-b[i][0]-2,b[i][0]+1]) c.sort() print(*c[-1]) ```
instruction
0
89,800
0
179,600
No
output
1
89,800
0
179,601
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. Submitted Solution: ``` R = lambda : map(int, input().split()) s = input() n = int(input()) words = [input() for i in range(n)] dp = [-1] * len(s) for w in words: si = 0 while si != -1: si = s.find(w, si) if si >= 0: dp[si + len(w) - 1] = max(si + 1, dp[si + len(w) - 1]) si += 1 fl = 0 fr = 0 l = 0 found = False for r in range(len(s)): if dp[r] >= 0: l = max(l, dp[r]) if r - l > fr - fl: found = True fl, fr = l, r print(fr - fl + found, fl) ```
instruction
0
89,801
0
179,602
No
output
1
89,801
0
179,603
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,897
0
179,794
"Correct Solution: ``` LARGE = 998244353 def solve(n): r = 0 mck = 1 factorial_n = 1 factorial_inv = [0] * (n + 1) factorial_inv[0] = 1 for i in range(1, n + 1): factorial_n *= i factorial_n %= LARGE factorial_inv[-1] = pow(factorial_n, LARGE - 2, LARGE) for i in range(n): factorial_inv[n - i - 1] = (factorial_inv[n - i] * (n - i)) % LARGE pow_2 = 1 for k in range(n // 2): r += factorial_n * factorial_inv[n - k] * factorial_inv[k] * pow_2 r %= LARGE pow_2 *= 2 pow_2 %= LARGE res = (pow(3, n, LARGE) - (2 * r)) % LARGE return res def main(): n = int(input()) res = solve(n) print(res) def test(): assert solve(2) == 7 assert solve(10) == 50007 # assert solve(1000000) == 210055358 if __name__ == "__main__": test() main() ```
output
1
89,897
0
179,795
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,898
0
179,796
"Correct Solution: ``` N = int(input()) nn = N + 10 P = 998244353 fa = [1] * (nn+1) fainv = [1] * (nn+1) for i in range(nn): fa[i+1] = fa[i] * (i+1) % P fainv[-1] = pow(fa[-1], P-2, P) for i in range(nn)[::-1]: fainv[i] = fainv[i+1] * (i+1) % P C = lambda a, b: fa[a] * fainv[b] % P * fainv[a-b] % P if 0 <= b <= a else 0 ans = pow(3, N, P) for i in range(N//2 + 1, N + 1): ans = (ans - 2 * C(N, i) * pow(2, N - i, P)) % P print(ans) ```
output
1
89,898
0
179,797
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,899
0
179,798
"Correct Solution: ``` def calc(n, mod): f = 1 fac = [1] for i in range(1, n + 1): f *= i f %= mod fac.append(f) inv = pow(f, mod - 2, mod) invs = [1] * (n + 1) invs[n] = inv for i in range(n, 1, -1): inv *= i inv %= mod invs[i - 1] = inv return fac, invs def cnk(n, r, mod, fac, inv): return fac[n] * inv[n - r] * inv[r] % mod n = int(input()) mod = 998244353 f, inv = calc(n + 10, mod) ans = pow(3, n, mod) p = [1] for i in range(n // 2 + 10): p.append(p[-1] * 2 % mod) for k in range(n // 2 + 1, n + 1): cur = 2 * cnk(n, k, mod, f, inv) * p[n - k] ans -= cur ans %= mod print(ans) ```
output
1
89,899
0
179,799
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,900
0
179,800
"Correct Solution: ``` #C def main(): mod=998244353 n=int(input()) Fact=[1] #階乗 for i in range(1,n+1): Fact.append(Fact[i-1]*i%mod) Finv=[0]*(n+1) #階乗の逆元 Finv[-1]=pow(Fact[-1],mod-2,mod) for i in range(n-1,-1,-1): Finv[i]=Finv[i+1]*(i+1)%mod def comb(n,r): if n<r: return 0 return Fact[n]*Finv[r]*Finv[n-r]%mod impossible=0 m=1 for k in range(n//2): impossible+=comb(n,k)*m%mod impossible%=mod m*=2 m%=mod print((pow(3,n,mod)-impossible*2)%mod) if __name__=='__main__': main() ```
output
1
89,900
0
179,801
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,901
0
179,802
"Correct Solution: ``` N=int(input()) mod=998244353 FACT=[1] for i in range(1,N+1): FACT.append(FACT[-1]*i%mod) FACT_INV=[pow(FACT[-1],mod-2,mod)] for i in range(N,0,-1): FACT_INV.append(FACT_INV[-1]*i%mod) FACT_INV.reverse() POW=[1] for i in range(N): POW.append(POW[-1]*2%mod) def Combi(a,b): return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod SC=0 for i in range(N//2+1,N+1): SC+=Combi(N,i)*POW[N-i] print((pow(3,N,mod)-SC*2)%mod) ```
output
1
89,901
0
179,803
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,902
0
179,804
"Correct Solution: ``` MOD = 998244353 N = int(input()) ans = pow(3, N, MOD) def getInvs(n, MOD): invs = [1] * (n+1) for x in range(2, n+1): invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD return invs def getCombNs(n, invs, MOD): combNs = [1] * (n//2+1) for x in range(1, n//2+1): combNs[x] = (combNs[x-1] * (n-x+1) * invs[x]) % MOD return combNs + combNs[:(n+1)//2][::-1] invs = getInvs(N, MOD) combNs = getCombNs(N, invs, MOD) pow2 = 1 for i in range((N-1)//2+1): num = combNs[i] * pow2 ans -= num*2 % MOD ans %= MOD pow2 *= 2 pow2 %= MOD print(ans) ```
output
1
89,902
0
179,805
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,903
0
179,806
"Correct Solution: ``` N = int(input()) nn = N + 10 P = 998244353 fa = [1] * (nn+1) fainv = [1] * (nn+1) for i in range(nn): fa[i+1] = fa[i] * (i+1) % P fainv[-1] = pow(fa[-1], P-2, P) for i in range(nn)[::-1]: fainv[i] = fainv[i+1] * (i+1) % P C = lambda a, b: fa[a] * fainv[b] % P * fainv[a-b] % P if 0 <= b <= a else 0 ans = pow(3, N, P) p2 = 2 for i in range(N, N // 2, -1): ans = (ans - C(N, i) * p2) % P p2 = p2 * 2 % P print(ans) ```
output
1
89,903
0
179,807
Provide a correct Python 3 solution for this coding contest problem. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358
instruction
0
89,904
0
179,808
"Correct Solution: ``` n=int(input()) pre=[1]*(n//2+200) pp=[1]*(n+1) mod=998244353 for i in range(1,n//2+100): pre[i]=((pre[i-1]*2)%mod) p=[1]*(n+1) for i in range(n): p[i+1]=((p[i]*(i+1))%mod) pp[-1] = pow(p[-1], mod - 2, mod) for i in range(2, n + 1): pp[-i] = int((pp[-i + 1] * (n + 2 - i)) % mod) tot=1 for i in range(n): tot*=3 tot%=mod #print(1) #print(tot) cc=0 for i in range(n//2+1,n+1): c=(((((p[n]*pp[i])%mod)*pp[n-i])%mod)*pre[n-i])%mod c%=mod cc+=c tot-=cc tot-=cc tot%=mod print(tot) ```
output
1
89,904
0
179,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358 Submitted Solution: ``` def prepare(n, MOD): f = 1 for m in range(1, n + 1): f *= m f %= MOD fn = f inv = pow(f, MOD - 2, MOD) invs = [1] * (n + 1) invs[n] = inv for m in range(n, 1, -1): inv *= m inv %= MOD invs[m - 1] = inv return fn, invs n = int(input()) MOD = 998244353 fn, invs = prepare(n, MOD) ans = pow(3, n, MOD) impossible = 0 mul = 2 for i in range(n // 2): tmp = fn * invs[i] * invs[n - i] % MOD * mul impossible = (impossible + tmp) % MOD mul = mul * 2 % MOD print((ans - impossible) % MOD) ```
instruction
0
89,905
0
179,810
Yes
output
1
89,905
0
179,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Given is a positive even number N. Find the number of strings s of length N consisting of `A`, `B`, and `C` that satisfy the following condition: * s can be converted to the empty string by repeating the following operation: * Choose two consecutive characters in s and erase them. However, choosing `AB` or `BA` is not allowed. For example, `ABBC` satisfies the condition for N=4, because we can convert it as follows: `ABBC` → (erase `BB`) → `AC` → (erase `AC`) → `(empty)`. The answer can be enormous, so compute the count modulo 998244353. Constraints * 2 \leq N \leq 10^7 * N is an even number. Input Input is given from Standard Input in the following format: N Output Print the number of strings that satisfy the conditions, modulo 998244353. Examples Input 2 Output 7 Input 10 Output 50007 Input 1000000 Output 210055358 Submitted Solution: ``` import itertools import os import sys from functools import lru_cache if os.getenv("LOCAL"): sys.stdin = open("_in.txt", "r") sys.setrecursionlimit(2147483647) INF = float("inf") IINF = 10 ** 18 MOD = 998244353 N = int(sys.stdin.readline()) @lru_cache(maxsize=None) # @debug def is_ok(s): if len(s) == 0: return True if len(s) == 2: return s not in ['AB', 'BA'] for i in range(len(s) - 2): if s[i:i + 2] not in ['AB', 'BA'] and is_ok(s[:i] + s[i + 2:]): return True return False def test(N): ret = 0 for s in itertools.product('ABC', repeat=N): s = ''.join(s) ret += is_ok(s) return ret def mod_invs(max, mod): """ 逆元のリスト 0 から max まで :param int max: :param int mod: """ invs = [1] * (max + 1) for x in range(2, max + 1): invs[x] = (-(mod // x) * invs[mod % x]) % mod return invs # print(test(N)) # N = 10 ** 7 # 解説AC # 偶数番目のAとBを反転して、AAとBB以外を取り除く # AまたはBが半分より多いときダメなので全体から引く ans = pow(3, N, MOD) invs = mod_invs(max=N, mod=MOD) ncr = 1 # NCr p2r = 1 # pow(2, N - r, MOD) for r in range(N, N // 2, -1): # ans -= comb.ncr(N, r) * pow(2, N - r, MOD) * 2 % MOD ans -= ncr * p2r * 2 % MOD ans %= MOD ncr *= r * invs[N - r + 1] ncr %= MOD p2r *= 2 p2r %= MOD print(ans) ```
instruction
0
89,906
0
179,812
Yes
output
1
89,906
0
179,813