message
stringlengths
2
59.7k
message_type
stringclasses
2 values
message_id
int64
0
1
conversation_id
int64
37
108k
cluster
float64
20
20
__index_level_0__
int64
74
217k
Provide tags and a correct Python 3 solution for this coding contest problem. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478.
instruction
0
35,259
20
70,518
Tags: strings Correct 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 heapq # from functools import lru_cache # import sys # sys.setrecursionlimit(10**6) # from functools import lru_cache # @lru_cache(maxsize=None) def 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 def kadane(a,size): max_so_far = 0 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far # @lru_cache(maxsize=None) def digitsSum(n): if n == 0: return 0 r = 0 while n > 0: r += n % 10 n //= 10 return r # INPUTS -------------------------- # s = input().decode('utf-8').strip() # n = int(input()) # l = list(map(int, input().split())) # t = int(input()) # for _ in range(t): n, k = list(map(int, input().split())) s = input().decode('utf-8').strip() ss = list(s) if n == 1 or k == 0: print(s) exit() if ss[0] == '4' and ss[1] == '7': ss[1] = '4' k -= 1 if k == 0: print("".join(ss)) exit() for i in range(1, len(s)-1): if ss[i] == '4' and ss[i+1] == '7': if ss[i-1] == '4' and (i+1) % 2 == 0: if k % 2 == 0: print("".join(ss)) exit() else: ss[i] = '7' print("".join(ss)) exit() if i<len(s)-2 and ss[i+2] == '7' and (i+1) % 2 != 0: if k % 2 == 0: print("".join(ss)) exit() else: ss[i+1] = '4' print("".join(ss)) exit() if (i+1)%2 == 0: ss[i] = '7' k -= 1 else: ss[i+1] = '4' k -= 1 if k == 0: print("".join(ss)) exit() if ss[-2] == '4' and ss[-1] == '7': if (len(s) - 1)%2 == 0: ss[-2] = '7' else: ss[-2] = '4' k -= 1 print("".join(ss)) ```
output
1
35,259
20
70,519
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` def solve(n, k, digits): if k == 0: return ''.join(digits) for i in range(n): r_i = i + 1 if digits[i] == "4" and i < (n - 1) and digits[i + 1] == "7": if r_i % 2 == 1: digits[i + 1] = "4" else: digits[i] = "7" k = k - 1 if k == 0: break if digits[i] == "7" and i - 1 >= 0 and digits[i - 1] == "4": k = k % 2 if k: digits[i] = "4" return ''.join(digits) # print(i, k, ''.join(digits)) return ''.join(digits) n, k = input().split() d = input() # print("====") print(solve(int(n), int(k), [i for i in d])) ```
instruction
0
35,260
20
70,520
Yes
output
1
35,260
20
70,521
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` n , k = map(int, input().split()) p = input() v = [0] for i in p: v.append(int(i)) # print(v) for i in range(1, n+1): if k == 0: break if i != n and v[i] == 4 and v[i+1] == 7: # loop infinito # print("oi") if v[i-1] == 4 and i%2 == 0: if k % 2 == 0: k = 0 break else: v[i] = 7 v[i+1] = 7 k = 0 break else: if i%2 == 0: v[i] = 7 v[i+1] = 7 k-=1 elif i%2 == 1: v[i] = 4 v[i+1] = 4 k-=1 for i in range(1,n+1): print(v[i], end='') print() ```
instruction
0
35,261
20
70,522
Yes
output
1
35,261
20
70,523
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` n, k = map(int, input().split()) t = list(input()) i, m = 0, n // 2 if k > m: k = m + ((m + k) & 1) while k and i < n - 1: if t[i] == '4' and t[i + 1] == '7': k -= 1 if i & 1 == 0: t[i + 1] = '4' else: t[i] = '7' i -= 2 i += 1 print(''.join(t)) ```
instruction
0
35,262
20
70,524
Yes
output
1
35,262
20
70,525
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` n, k = map(int, input().split()) s, x = list(input()), 0 for i in range(k): while x < n - 1 and not(s[x] == '4' and s[x+1] == '7'): x += 1 if x == n - 1: break if x % 2: if s[x-1] == '4' and (k + i + 1) % 2: break s[x], x = '7', x - 1 else: if x < n - 2 and s[x+2] == '7' and (k + i + 1) % 2: break s[x+1], x = '4', x + 1 print(''.join(s)) ```
instruction
0
35,263
20
70,526
Yes
output
1
35,263
20
70,527
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` def Include(L,s): for i in range(1,len(L)): if(L[i-1]=="4" and L[i]=="7"): return i return False def STR(L): s="" for i in range(len(L)): s+=L[i] return s n,k=map(int,input().split()) s=list(input()) H={} F=[] m=Include(s,"47") if(m): p=[list(s)] ind=m-1 if(ind%2==0): s[ind]="4" s[ind+1]="4" else: s[ind]="7" s[ind+1]="7" p.append(list(s)) if(ind>0): F+=s[:ind-1] s=s[ind-1:] k-=1 while(k>0): k-=1 m=Include(s,"47") if(not m): break ind=m-1 if(ind%2==0): s[ind]="4" s[ind+1]="4" else: s[ind]="7" s[ind+1]="7" if(s==p[0]): if(k%2==0): break else: p.append(list(s)) p.pop(0) else: p.append(list(s)) p.pop(0) if(ind>0): F+=s[:ind-1] s=s[ind-1:] print(STR(F+s)) else: print(STR(s)) ```
instruction
0
35,264
20
70,528
No
output
1
35,264
20
70,529
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` n, k = map(int, input().split()) D = input() if '447' in D: k %= 2 for _ in range(k): i = D.find('47') if i < 0: break D = (D[:i] + '7' + D[i+1:]) if i % 2 else (D[:i+1] + '4' + D[i+2:]) print(D) ```
instruction
0
35,265
20
70,530
No
output
1
35,265
20
70,531
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` from math import * from collections import deque from copy import deepcopy import sys def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input def multi(): return map(int,input().split()) def strmulti(): return map(str, inp().split()) def lis(): return list(map(int, inp().split())) def lcm(a,b): return (a*b)//gcd(a,b) def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1))) def stringlis(): return list(map(str, inp().split())) def out(var): sys.stdout.write(str(var)) #for fast output, always take string def printlist(a) : print(' '.join(str(a[i]) for i in range(len(a)))) def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True #copied functions end #start coding n,k=multi() s=inp() a=list(s) count=0 for i in range(1,len(a)): if(i>1): if(a[i-2]=='4' and (a[i-1]=='4' and a[i]=='7' and count<k)): if((k-count)%2!=0): if(i%2==0): a[i-2]='4' a[i-1]='4' a[i]='7' break if(a[i-1]=='4' and a[i]=='7' and count<k): count+=1 if((i)%2!=0): a[i-1]=a[i]='4' else: a[i-1]=a[i]='7' continue for i in a: print(i,end="") ```
instruction
0
35,266
20
70,532
No
output
1
35,266
20
70,533
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya has a number consisting of n digits without leading zeroes. He represented it as an array of digits without leading zeroes. Let's call it d. The numeration starts with 1, starting from the most significant digit. Petya wants to perform the following operation k times: find the minimum x (1 โ‰ค x < n) such that dx = 4 and dx + 1 = 7, if x is odd, then to assign dx = dx + 1 = 4, otherwise to assign dx = dx + 1 = 7. Note that if no x was found, then the operation counts as completed and the array doesn't change at all. You are given the initial number as an array of digits and the number k. Help Petya find the result of completing k operations. Input The first line contains two integers n and k (1 โ‰ค n โ‰ค 105, 0 โ‰ค k โ‰ค 109) โ€” the number of digits in the number and the number of completed operations. The second line contains n digits without spaces representing the array of digits d, starting with d1. It is guaranteed that the first digit of the number does not equal zero. Output In the single line print the result without spaces โ€” the number after the k operations are fulfilled. Examples Input 7 4 4727447 Output 4427477 Input 4 2 4478 Output 4478 Note In the first sample the number changes in the following sequence: 4727447 โ†’ 4427447 โ†’ 4427477 โ†’ 4427447 โ†’ 4427477. In the second sample: 4478 โ†’ 4778 โ†’ 4478. Submitted Solution: ``` n,k=map(int,input().split()) str1=input() for i in range(k): if ('47' in str1): if (str1.find('47')==0): str1=str1.replace('47','44',1) elif (str1[str1.find('47')-1]=='4' and (str1.find('47')-1)%2==0): if (k-i)%2==0: break elif (k-i)%2!=0: str1=str1.replace('447','477',1) break print(str1) ```
instruction
0
35,267
20
70,534
No
output
1
35,267
20
70,535
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,268
20
70,536
Tags: greedy, two pointers Correct Solution: ``` t = int(input()) for _ in range(t): s = input() ans = '' e = [] o = [] for d in s: if ord(d) % 2 == 0: e.append(d) else: o.append(d) e.append('ั‘') o.append('ั‘') i = 0 j = 0 while e[i] != 'ั‘' or o[j] != 'ั‘': if ord(e[i]) < ord(o[j]): ans += e[i] i += 1 else: ans += o[j] j += 1 print(ans) ```
output
1
35,268
20
70,537
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,269
20
70,538
Tags: greedy, two pointers Correct Solution: ``` from math import * from bisect import * from collections import Counter,defaultdict I =lambda:int(input()) SI =lambda:input() M =lambda:map(int,input().split()) LI=lambda:list(map(int,input().split())) for _ in range(I()): s = SI() e = [] o = [] for i in s: if int(i)%2: o.append(int(i)) else: e.append(int(i)) o.reverse() e.reverse() ans = [] while e and o: if e[-1] < o[-1]: ans.append(e.pop()) else: ans.append(o.pop()) if e: while e: ans.append(e.pop()) else: while o: ans.append(o.pop()) print(''.join(map(str, ans))) ```
output
1
35,269
20
70,539
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,270
20
70,540
Tags: greedy, two pointers Correct Solution: ``` t=int(input()) for _ in range(t): num=list(map(int,input().strip())) odd=[] evn=[] for i in num: if(i%2==0): odd+=[i] else: evn+=[i] ans=[] i=0 j=0 while(len(ans)!=len(num)): if(i==len(odd)): ans+=[str(evn[j])] j+=1 elif(j==len(evn)): ans+=[str(odd[i])] i+=1 elif(odd[i]<evn[j]): ans+=[str(odd[i])] i+=1 else: ans+=[str(evn[j])] j+=1 print(''.join(ans)) ```
output
1
35,270
20
70,541
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,271
20
70,542
Tags: greedy, two pointers Correct Solution: ``` # PARITY'S RELATIVE POSITION DOESN'T CHANGE! (PROBLEM C) def solve1(s): # return list evens = [u for u in s if u % 2 == 0] odds = [u for u in s if u % 2 == 1] if len(odds) == 0: return evens ans = [] inserted_odd = 0 current_odd = odds[inserted_odd] for i in range(len(evens)): while current_odd < evens[i] and inserted_odd < len(odds): ans.append(current_odd) inserted_odd += 1 if inserted_odd < len(odds): current_odd = odds[inserted_odd] ans.append(evens[i]) while inserted_odd < len(odds): ans.append(current_odd) inserted_odd += 1 if inserted_odd < len(odds): current_odd = odds[inserted_odd] return ans def solve2(s): # return list # lazy code lmao odds = [u for u in s if u % 2 == 0] evens = [u for u in s if u % 2 == 1] if len(odds) == 0: return evens ans = [] inserted_odd = 0 current_odd = odds[inserted_odd] for i in range(len(evens)): while current_odd < evens[i] and inserted_odd < len(odds): ans.append(current_odd) inserted_odd += 1 if inserted_odd < len(odds): current_odd = odds[inserted_odd] ans.append(evens[i]) while inserted_odd < len(odds): ans.append(current_odd) inserted_odd += 1 if inserted_odd < len(odds): current_odd = odds[inserted_odd] return ans for _ in range(int(input())): s = list(map(int, list(input()))) ans = min(solve1(s), solve2(s)) print (''.join(map(str, ans))) ```
output
1
35,271
20
70,543
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,272
20
70,544
Tags: greedy, two pointers Correct Solution: ``` for _ in range(int(input())): a=list(input()) n=len(a) odd=[] even=[] for i in range(n): a[i]=int(a[i]) if a[i]%2==0: odd.append(a[i]) else: even.append(a[i]) lo=len(odd) le=len(even) oi=0 ei=0 while oi<lo and ei<le: if odd[oi]<even[ei]: print(odd[oi],end="") oi+=1 else: print(even[ei],end="") ei+=1 for i in range(oi,lo): print(odd[i],end="") for i in range(ei,le): print(even[i],end="") print() ```
output
1
35,272
20
70,545
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,273
20
70,546
Tags: greedy, two pointers Correct Solution: ``` n = int(input()) for _ in range(n): num = list(map(int,list(input()))) odd=[] even=[] for n in num: if n%2==0: even.append(n) else: odd.append(n) m=len(odd) n=len(even) i=j=0 res=[] while i<m and j<n: if odd[i]<even[j]: res.append(odd[i]) i+=1 else: res.append(even[j]) j+=1 if i==m: res+=even[j:] else: res+=odd[i:] print(''.join(list(map(str,res)))) ```
output
1
35,273
20
70,547
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,274
20
70,548
Tags: greedy, two pointers Correct Solution: ``` """ ๆ€ง่ณช: ๅถๅฅ‡ใงๅˆ†ใ‘ใ‚‹ใจ้šฃๆŽฅใ™ใ‚‹ๅถๅฅ‡ใŒ้•ใ†็ฎ‡ๆ‰€ใ‚’้€ฃ็ถšใ—ใฆใ„ใ‚‹็ฎ‡ๆ‰€ใพใง็งปๅ‹•ใงใใ‚‹ eeeeoe ใชใ‚‰oใฏeeeeใฎไปปๆ„ใฎไฝ็ฝฎใซ็งปๅ‹•ใงใใ‚‹ ๆ•ฐๅญ—ใฎๆกใ‚’ๅ…ฅใ‚Œๆ›ฟใˆใฆๆœ€ๅฐๅ€คใ‚’ไฝœใ‚‹=> ๅ…ˆ้ ญใŒๅคงไบ‹๏ผใชใฎใงใ€ๅ‰ใ‹ใ‚‰้ †็•ชใซใงใใ‚‹ใ ใ‘ๅฐใ•ใ„ๆ•ฐๅญ—ใซใ—ใฆใ„ใ ใ“ใฎๆ“ไฝœใ ใจeoe` e`ใŒeใ‚ˆใ‚Šๅทฆใซ่กŒใ๏ผˆ็Šถๆณใง่ทจใ)ใ“ใจใฏใงใใชใ„=>ee`o ใ‹eoe`ใ‹oee`ใซใชใ‚‹ eoe'o' ee`oo` oee`o` oeo`e` oo`ee` eoo`e` eoe`o` ๅฅ‡ๆ•ฐๅถๆ•ฐใฎไธญใ ใจ้ †็•ชใฏๅค‰ใ‚ใ‚‰ใชใ„ใ‹ใ‚‰ใ€ๅฅ‡ๆ•ฐใ€ๅถๆ•ฐใงๅฐบๅ–ใ‚’ใ—ใฆใ€ๅฐใ•ใ„ๆ–นใ‚’้ †็•ชใซใ„ใ‚Œใ‚‹ => 4C2 ใฃใฆ่จ€ใ‚ใ‚ŒใŸใ‚‰ใ‚ใ‹ใฃใŸ eeooeoeoeooe => eeeeeeoooooo ~ ooooooeeeeee step1: ๅ…ˆ้ ญใ‹ใ‚‰ๅถๅฅ‡ใงๅˆ†้กžใ—ใฆใ€้šฃๆŽฅใ™ใ‚‹ๅถๅฅ‡ใŒ้•ใ†ใ‚ˆใ†ใซใชใฃใŸใ‚‰ ใใ‚Œใ‚’ๅ…ˆ้ ญใ‹ใ‚‰ๅˆใ‚ใฆswapใ™ใ‚‹ใ‚‚ใฎใ‚ˆใ‚Šใ‚‚ๅฐใ•ใ„ๆ•ฐใฎๅ‰ใพใง็งปๅ‹•ใ•ใ›ใ‚‹ => ่จ€ใ„ๆ›ใˆ swapใงใชใใฆ้€ฃ็ถšใ—ใฆใ„ใ‚‹ๆ–นใ‚’็งปๅ‹•ใ•ใ›ใ‚‹ 0709 => 0709 > 0079 1337 2464342 > 23464 swap = 3 ๅฐใ•ใ„ใ‚„ใค2 odd_bit = s[0]%2 idx_swap = -1 idx_consequence = 0 for i in range(n): if s[i] % 2 != odd_bit: idx_swap = i for j in range(idx_consequence,idx_swap): step2: eeoeeeo ใซใชใฃใŸๆ™‚ใซใ€3ใฎidx ใ‚’ไฟๆŒใ™ใ‚‹ๅฟ…่ฆใŒใ‚ใ‚‹ใ€‚ """ t = int(input().rstrip()) for case in range(t): ss = input().rstrip() s = ss odd_lis, even_lis = [], [] for i in range(len(ss)): if ord(ss[i]) % 2: odd_lis.append(int(ss[i])) else: even_lis.append(int(ss[i])) # ๅฐบๅ–ใซๅ…ฅใ‚‹ # # print(odd_lis, even_lis) ans = [] lim = len(odd_lis) lim_even = len(even_lis) # # print(lim, lim_even) odd_idx, even_idx = 0, 0 if lim == lim + lim_even: print(ss) continue # ๅ…จใฆๅฅ‡ๆ•ฐใฎๆ™‚ if lim_even == lim + lim_even: print(ss) continue while odd_idx < lim: if odd_lis[odd_idx] > even_lis[even_idx]: ans.append(even_lis[even_idx]) even_idx += 1 # print(even_idx) if even_idx == lim_even: while odd_idx < lim: ans.append(odd_lis[odd_idx]) odd_idx += 1 else: ans.append(odd_lis[odd_idx]) odd_idx += 1 for i in range(even_idx, lim_even): ans.append(even_lis[i]) for i in ans: print(i, end="") print() ```
output
1
35,274
20
70,549
Provide tags and a correct Python 3 solution for this coding contest problem. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642.
instruction
0
35,275
20
70,550
Tags: greedy, two pointers Correct Solution: ``` t=int(input()) for tt in range(t): s=input().strip() even=[] odd=[] for i in s: if int(i)%2==0: even.append(int(i)) else: odd.append(int(i)) ans="" i=0 j=0 while i<len(even) and j<len(odd): if even[i]<=odd[j]: ans+=str(even[i]) i+=1 else: ans+=str(odd[j]) j+=1 while i<len(even): ans+=str(even[i]) i+=1 while j<len(odd): ans+=str(odd[j]) j+=1 print(ans) ```
output
1
35,275
20
70,551
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` def solve(s): e = [] o = [] for i in range(len(s)): if s[i]%2 == 0: e.append(s[i]) else: o.append(s[i]) i,j = 0,0 res =[] while i < len(e) and j < len(o): if e[i] < o[j]: res.append(e[i]) i += 1 else: res.append(o[j]) j += 1 if i == len(e): left = o[j:len(o)] else: left = e[i:len(e)] res = "".join(map(str,res)) + "".join(map(str,left)) print(res) if __name__ == '__main__': t = int(input()) for _ in range(t): s = list(input()) s = list(map(int, s)) solve(s) ```
instruction
0
35,276
20
70,552
Yes
output
1
35,276
20
70,553
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` import os def smallest(s): groups = [[], []] for c in s: groups[int(c) % 2].append(c) groups = [g[::-1] for g in groups] r = [] while True: if groups[0] and groups[1]: if groups[0][-1] < groups[1][-1]: r.append(groups[0].pop(-1)) else: r.append(groups[1].pop(-1)) elif groups[0] and not groups[1]: return r + groups[0][::-1] elif groups[1] and not groups[0]: return r + groups[1][::-1] else: return r def pp(input): n_test = int(input()) for i in range(n_test): print("".join(smallest(input()))) if "paalto" in os.getcwd(): from string_source import string_source s1 = string_source( """3 0709 1337 246432 """ ) pp(s1) else: pp(input) ```
instruction
0
35,277
20
70,554
Yes
output
1
35,277
20
70,555
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` import sys import math from queue import * from collections import * # sys.setrecursionlimit(10 ** 6) mod = int(1e9 + 7) input = sys.stdin.readline ii = lambda: list(map(int,input().strip().split())) ist= lambda: list(input().strip()) test = int(input()) # print(test) for t in range(test): s= input().strip() # print(s,type(s), len(s)) odd= [] even= [] for k in range(len(s)): p = int(s[k]) if p % 2 == 1: odd += [p] else: even += [p] n1= len(odd) n2 = len(even) i, j = 0,0 while i < n1 or j < n2: p1= 10 p2= 10 if i < n1: p1= odd[i] if j < n2: p2 = even[j] if p1 < p2: print(p1,end="") i += 1 else: print(p2,end="") j+= 1 print() ```
instruction
0
35,278
20
70,556
Yes
output
1
35,278
20
70,557
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` t = int(input()) for i in range(t): nums = list(map(int, list(input()))) nums0, nums1 = [], [] for num in nums: if num % 2 == 0: nums0.append(str(num)) else: nums1.append(str(num)) nums0.append(str(99)) nums1.append(str(99)) p0, p1 = 0, 0 l0, l1 = len(nums0) - 1, len(nums1) - 1 ans = str() while p0 < l0 or p1 < l1: p0_prev = p0 while p0 < l0 and nums0[p0] < nums1[p1]: p0 += 1 ans += ''.join(nums0[p0_prev: p0]) p1_prev = p1 while p1 < l1 and nums1[p1] < nums0[p0]: p1 += 1 ans += ''.join(nums1[p1_prev: p1]) print(ans) ```
instruction
0
35,279
20
70,558
Yes
output
1
35,279
20
70,559
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` for _ in range(int(input())): s = list(input()) n = len(s) for i in range(n-1): if s[i] > s[i+1] and (int(s[i])%2 != int(s[i+1])%2): for j in range(i+1, 0, -1): if s[j - 1] > s[j] and (int(s[j - 1]) % 2 != int(s[j]) % 2): s[j - 1], s[j] = s[j], s[j - 1] print(*s, sep="") ```
instruction
0
35,280
20
70,560
No
output
1
35,280
20
70,561
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` n = int(input()) for i in range(n): num = [int(x) for x in input()] j = 0 while j != len(num)-1: if num[j] > num[j+1] and (num[j] - num[j+1]) % 2 == 1: temp = num[j] num[j] = num[j+1] num[j+1] = temp j = 0 else: j += 1 num = [str(x) for x in num] print(''.join(num)) ```
instruction
0
35,281
20
70,562
No
output
1
35,281
20
70,563
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` def funct(arr, pair_one, pair_two): for i in range(pair_one[0], pair_one[1]): if arr[i] > arr[pair_two[0]]: elem = arr.pop(pair_two[0]) arr.insert(i, elem) pair_one[0], pair_one[1] = i + 1, pair_one[1] + 1 pair_two[0], pair_two[1] = pair_two[0] + 1, pair_two[1] if pair_two[0] <= pair_two[1]: funct(arr, pair_one, pair_two) return t = int(input()) for i in range(0, t): digit = list(map(int, input())) current = 0 pair_old = [0, 0] pair_new = [-1, 0] for i in range(1, len(digit)): if (digit[current] + digit[i]) % 2 == 1 or i == len(digit) - 1: pair_old[0], pair_old[1] = pair_new[0], pair_new[1] pair_new[0], pair_new[1] = current, i - 1 if pair_old[0] != -1: funct(digit, pair_old, pair_new) current = i ret = "" for i in digit: ret += str(i) print(ret) ```
instruction
0
35,282
20
70,564
No
output
1
35,282
20
70,565
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. You are given a huge integer a consisting of n digits (n is between 1 and 3 โ‹… 10^5, inclusive). It may contain leading zeros. You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2). For example, if a = 032867235 you can get the following integers in a single operation: * 302867235 if you swap the first and the second digits; * 023867235 if you swap the second and the third digits; * 032876235 if you swap the fifth and the sixth digits; * 032862735 if you swap the sixth and the seventh digits; * 032867325 if you swap the seventh and the eighth digits. Note, that you can't swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can't swap digits on positions 3 and 4 because the digits have the same parity. You can perform any number (possibly, zero) of such operations. Find the minimum integer you can obtain. Note that the resulting integer also may contain leading zeros. Input The first line contains one integer t (1 โ‰ค t โ‰ค 10^4) โ€” the number of test cases in the input. The only line of each test case contains the integer a, its length n is between 1 and 3 โ‹… 10^5, inclusive. It is guaranteed that the sum of all values n does not exceed 3 โ‹… 10^5. Output For each test case print line โ€” the minimum integer you can obtain. Example Input 3 0709 1337 246432 Output 0079 1337 234642 Note In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 0 \underline{70} 9 โ†’ 0079. In the second test case, the initial integer is optimal. In the third test case you can perform the following sequence of operations: 246 \underline{43} 2 โ†’ 24 \underline{63}42 โ†’ 2 \underline{43} 642 โ†’ 234642. Submitted Solution: ``` # ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode 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") # ------------------- fast io -------------------- testcases=int(input()) for j in range(testcases): vals=[int(k) for k in input()] n=len(vals) evens=[] odds=[] for s in range(n): if vals[s]%2==0: evens.append(vals[s]) else: odds.append(vals[s]) odds.sort() evens.reverse() odds.reverse() ans=[] while len(evens)>0 or len(odds)>0: if len(evens)>0 and len(odds)>0: if evens[-1]<=odds[-1]: ans.append(evens.pop()) else: ans.append(odds.pop()) else: if len(evens)>0: ans.append(evens.pop()) else: ans.append(odds.pop()) ans=[str(k) for k in ans] print("".join(ans)) ```
instruction
0
35,283
20
70,566
No
output
1
35,283
20
70,567
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); * Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. * If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. * If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language. Input The first line of input contains two integers n and q (1 โ‰ค n, q โ‰ค 105) โ€” represents the length of the sequence s and the number of queries. The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines each contains two integers li and ri (1 โ‰ค li โ‰ค ri โ‰ค n) โ€” the i-th query. Output For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1&gt;3&gt;22&lt; 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 Submitted Solution: ``` def f(t): print(t) i, d = 0, 1 p, n = [0] * 10, len(t) while 0 <= i < n: q = t[i] if q < 0: if d == q == -1 and t[i + 1] == -3: break d = 2 + q else: p[q] += 1 t[i] -= 1 if q == 0: t[i] = d - 2 i += d return ' '.join(map(str, p)) n, m = map(int, input().split()) p = {str(i): i for i in range(10)} p['>'], p['<'] = -1, -3 t = [p[i] for i in input()] p = [0] * m for i in range(m): l, r = map(int, input().split()) p[i] = f(t[l - 1: r]) print('\n'.join(p)) ```
instruction
0
35,445
20
70,890
No
output
1
35,445
20
70,891
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); * Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. * If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. * If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language. Input The first line of input contains two integers n and q (1 โ‰ค n, q โ‰ค 105) โ€” represents the length of the sequence s and the number of queries. The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines each contains two integers li and ri (1 โ‰ค li โ‰ค ri โ‰ค n) โ€” the i-th query. Output For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1&gt;3&gt;22&lt; 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 Submitted Solution: ``` r = {str(i): i for i in range(10)} r['>'] = r['<'] = 10 n, m = map(int, input().split()) t = input() p, s = [r[i] for i in t], [0] * m for i in range(m): l, r = map(int, input().split()) l -= 1 q = [0] * 11 b = l + t[l: r].find('<') if b < l: for j in range(l, r): q[p[j]] += 1 else: a = l + t[l: b].rfind('>') if a < l: for j in range(l, b): q[p[j]] += 1 q = [q[i] + q[i + 1] for i in range(10)] else: for j in range(a + 1, b): q[p[j]] += 1 for j in range(9, 0, -1): q[j - 1] += q[j] for j in range(l, a): q[p[j]] += 1 s[i] = ' '.join(map(str, q[: 10])) print('\n'.join(s)) ```
instruction
0
35,446
20
70,892
No
output
1
35,446
20
70,893
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); * Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. * If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. * If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language. Input The first line of input contains two integers n and q (1 โ‰ค n, q โ‰ค 105) โ€” represents the length of the sequence s and the number of queries. The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines each contains two integers li and ri (1 โ‰ค li โ‰ค ri โ‰ค n) โ€” the i-th query. Output For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1&gt;3&gt;22&lt; 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 Submitted Solution: ``` def Solve(s): ind=0 D=1 Ans=[0]*10 while(ind>=0 and ind<len(s)): if(s[ind] not in '><'): Ans[int(s[ind])]+=1 if(s[ind]=='0'): s.pop(ind) if(D==0): ind-=1 else: s[ind]=str(int(s[ind])-1) if(D==0): ind-=1 else: ind+=1 else: if(s[ind]=='>'): D=1 ind+=1 if(ind==len(s)): break if(s[ind] in "><"): s.pop(ind-1) else: D=0 ind-=1 if(ind<0): break if(s[ind] in "><"): s.pop(ind+1) return Ans n,q=map(int,input().split()) s=list(input()) ss=list(s) for i in range(q): x,y=map(int,input().split()) s=list(ss) a=Solve(s[x-1:y]) for item in a: print(item,end=" ") print() ```
instruction
0
35,447
20
70,894
No
output
1
35,447
20
70,895
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. * Current character pointer (CP); * Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. * If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. * If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence s1, s2, ..., sn of "<", ">" and digits. You should answer q queries. Each query gives you l and r and asks how many of each digit will be printed if we run the sequence sl, sl + 1, ..., sr as an independent program in this language. Input The first line of input contains two integers n and q (1 โ‰ค n, q โ‰ค 105) โ€” represents the length of the sequence s and the number of queries. The second line contains s, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of s are not separated with spaces. The next q lines each contains two integers li and ri (1 โ‰ค li โ‰ค ri โ‰ค n) โ€” the i-th query. Output For each query print 10 space separated integers: x0, x1, ..., x9 where xi equals the number of times the interpreter prints i while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1&gt;3&gt;22&lt; 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 Submitted Solution: ``` n,Q = map(int,input().strip().split()) s = input() d = 1 for q in range(Q): arr = [0]*(10) l,r = map(int,input().strip().split()) su = "" for i in range(l-1,r): su+=s[i] su = list(su) i = 0 d = 1 print(su) while i<len(su) and i>=0: if su[i].isdigit(): arr[int(su[i])]+=1 if su[i]=='0': su = su[:i]+su[i+1:] #i-=1 else: su[i] = str(int(su[i])-1) if d==1: i+=1 else: i-=1; else: if su[i]=='>' or su[i]=='<': if d==1 and i!=0: if su[i-1]=='>' or su[i-1]=='<' or su[i-1]=='-1': su = su[:i-1]+su[i:] #i-=1 if d==0 and i!=n-1: if su[i+1]=='>' or su[i+1]=='<' or su[i+1]=='-1': su = su[:i+1]+su[i+2:] if su[i]=='>': d = 1 else: d = 0 if d==0: i-=1; else: i+=1 #print(su,i) #print(arr) #print(su) print(*arr) ```
instruction
0
35,448
20
70,896
No
output
1
35,448
20
70,897
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,897
20
71,794
"Correct Solution: ``` import math p=1 q=1 while p==1: o=0 n=int(input()) if n==0: break else: x=math.factorial(n) while q==1: y=x%10 if y==0: o+=1 x=x//10 else: break print(o) ```
output
1
35,897
20
71,795
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,898
20
71,796
"Correct Solution: ``` # coding: utf-8 # Your code here! import math while True: n = int(input()) if n == 0: break i = 0 n = str(math.factorial(n))[::-1] while n[i] == '0': i += 1 print(i) ```
output
1
35,898
20
71,797
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,899
20
71,798
"Correct Solution: ``` for e in iter(input,'0'): e=int(e) print(sum(e//5**i for i in range(1,7))) ```
output
1
35,899
20
71,799
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,900
20
71,800
"Correct Solution: ``` while True: n=int(input()) if n==0: break ans=0 t=5 while t<=20000: ans+=n//t t*=5 print(ans) ```
output
1
35,900
20
71,801
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,901
20
71,802
"Correct Solution: ``` def fac(n): if n==0 or n==1:return 1 e=1 for i in range(1,n+1): e*=i return e zero=lambda t:len(t)-max([t.rfind(str(i)) for i in range(1,10)])-1 r=[] while 1: p=int(input()) if not p:break r.extend([zero(str(fac(p)))]) [print(i) for i in r] ```
output
1
35,901
20
71,803
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,902
20
71,804
"Correct Solution: ``` import re def fac(n): s = 1 for i in range(1,n+1): s *= i return(s) while True: n = int(input().strip()) if n==0: break num_str = '%d' % fac(n) zeros = re.sub(r'.*[^0](0*)$', '\\1', num_str) print(len(zeros)) ```
output
1
35,902
20
71,805
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,903
20
71,806
"Correct Solution: ``` import math while True: n=int(input()) if n==0: break a=math.factorial(n) b=str(a) m=len(b) c=0 for i in range(m-1,-1,-1): if b[i]=='0': c+=1 else: break print(c) ```
output
1
35,903
20
71,807
Provide a correct Python 3 solution for this coding contest problem. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499
instruction
0
35,904
20
71,808
"Correct Solution: ``` while True: n = int(input()) if n==0: break N = 1 for i in range(1,n+1,1): N *= i zero = 0 while True: if N%10 > 0: break N = N//10 zero += 1 print(zero) ```
output
1
35,904
20
71,809
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` import math while 1: try: count=0 n=int(input()) if n==0: break n=str(math.factorial(n)) for i in n[::-1]: if i=='0': count +=1 else: print(count) break except:break ```
instruction
0
35,905
20
71,810
Yes
output
1
35,905
20
71,811
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` while True : n = int(input()) if n == 0 : break S = 1 for i in range(n) : S *= (i+1) ans = 0 while True : if S % 5 != 0 : break else : S = S // 5 ans += 1 print(ans) ```
instruction
0
35,906
20
71,812
Yes
output
1
35,906
20
71,813
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` while True: n = int(input()) if n==0: break t=1 for i in range(1,n+1): t *= i i = 0 while t % 10 == 0: i+=1 t //= 10 print(i) ```
instruction
0
35,907
20
71,814
Yes
output
1
35,907
20
71,815
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` import math while True: x = int(input()) if x == 0: break n = list(str(math.factorial(x))) con = 0 for i in range(len(n)-1,0,-1): if n[i] != '0': break con += 1 print(con) ```
instruction
0
35,908
20
71,816
Yes
output
1
35,908
20
71,817
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` def factorial(n): res = 1 for i in range(1, n+1): res = res * i return res while True: inp = input() if inp == '0': break num = factorial(int(inp)) cnt = 0 while True: if num % 10 == 0: cnt = cnt + 1 num = int(num / 10) else: break print(cnt) ```
instruction
0
35,909
20
71,818
No
output
1
35,909
20
71,819
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` for e in iter(input,'0'): e=int(e) print(sum(e//5**ifor i in range(1,6))) ```
instruction
0
35,910
20
71,820
No
output
1
35,910
20
71,821
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` for e in iter(input,'0'):print(sum(int(e)//5**i for i in range(1,6))) ```
instruction
0
35,911
20
71,822
No
output
1
35,911
20
71,823
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response. n! = n ร— (n โˆ’ 1) ร— (n โˆ’ 2) ร— ... ร— 3 ร— 2 ร— 1 Is called the factorial of n. For example, the factorial of 12 12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600 And there are two consecutive 0s at the end. Write a program that inputs the integer n and outputs the number of consecutive 0s at the end of n !. However, n is a positive integer less than or equal to 20000. Input Multiple data are given. Each piece of data is given n (n โ‰ค 20000) on one line. When n is 0, it is the last input. The number of data does not exceed 20. Output For each data, output the number of 0s that are consecutively arranged at the end of n! On one line. Example Input 2 12 10000 0 Output 0 2 2499 Submitted Solution: ``` while 1: try: zero=0 number=1 n=int(input()) for i in range(1,n+1): number*=i while len(str(number))>7:number=int(str(number)[1:]) while number%10==0: zero+=1 number=int(number/10) print(zero) except:break ```
instruction
0
35,912
20
71,824
No
output
1
35,912
20
71,825
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not. You are given a segment [L; R]. Count the number of classy integers x such that L โ‰ค x โ‰ค R. Each testcase contains several segments, for each of them you are required to solve the problem separately. Input The first line contains a single integer T (1 โ‰ค T โ‰ค 10^4) โ€” the number of segments in a testcase. Each of the next T lines contains two integers L_i and R_i (1 โ‰ค L_i โ‰ค R_i โ‰ค 10^{18}). Output Print T lines โ€” the i-th line should contain the number of classy integers on a segment [L_i; R_i]. Example Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
instruction
0
35,998
20
71,996
Tags: combinatorics, dp Correct Solution: ``` import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write 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 math import factorial from collections import Counter, defaultdict from heapq import heapify, heappop, heappush def RL(): return map(int, sys.stdin.readline().rstrip().split()) def RLL(): return list(map(int, sys.stdin.readline().rstrip().split())) def N(): return int(input()) def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0 def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0 def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2) mod = 998244353 INF = float('inf') # ------------------------------ def main(): for _ in range(N()): l, r = RL() from functools import lru_cache def dp(num): arr = [] while num>0: arr.append(num%10) num//=10 @lru_cache(None) def ddp(pos, tag, znum): if pos<0: return 1 res = 0 for i in range(10): if tag and i>arr[pos]: break if i!=0 and znum+1>3: break res+=ddp(pos-1, tag and arr[pos]==i, znum+(1 if i!=0 else 0)) return res return ddp(len(arr)-1, True, 0) print(dp(r)-dp(l-1)) if __name__ == "__main__": main() ```
output
1
35,998
20
71,997
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not. You are given a segment [L; R]. Count the number of classy integers x such that L โ‰ค x โ‰ค R. Each testcase contains several segments, for each of them you are required to solve the problem separately. Input The first line contains a single integer T (1 โ‰ค T โ‰ค 10^4) โ€” the number of segments in a testcase. Each of the next T lines contains two integers L_i and R_i (1 โ‰ค L_i โ‰ค R_i โ‰ค 10^{18}). Output Print T lines โ€” the i-th line should contain the number of classy integers on a segment [L_i; R_i]. Example Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
instruction
0
35,999
20
71,998
Tags: combinatorics, dp Correct Solution: ``` import sys class Solution(): mem = dict() def classy_Numbers(self): T = int(sys.stdin.readline().strip()) self.queries = [] for t in range(T): left, right = list(map(int, sys.stdin.readline().strip().split())) print(self.dp(right, 3) - self.dp(left-1, 3)) def dp(self, target, limit): num_digits = len(str(target)) if (target, limit) in self.mem: return self.mem[(target, limit)] if limit == 0: return 1 if num_digits <= limit: return target + 1 top_digit = target // (10 ** (num_digits-1)) res = target % (10 ** (num_digits-1)) ret = self.dp(res, limit-1) \ + max(top_digit-1, 0) * self.dp(10**(num_digits-1)-1, limit-1) \ + self.dp(10**(num_digits-1)-1, limit) self.mem[(target, limit)] = ret return ret sol = Solution() sol.classy_Numbers() # print(sol.dp(1, 3)) # print(sol.dp(1234, 4, 3)) # print(sol.dp(12345, 5, 3)) ```
output
1
35,999
20
71,999
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not. You are given a segment [L; R]. Count the number of classy integers x such that L โ‰ค x โ‰ค R. Each testcase contains several segments, for each of them you are required to solve the problem separately. Input The first line contains a single integer T (1 โ‰ค T โ‰ค 10^4) โ€” the number of segments in a testcase. Each of the next T lines contains two integers L_i and R_i (1 โ‰ค L_i โ‰ค R_i โ‰ค 10^{18}). Output Print T lines โ€” the i-th line should contain the number of classy integers on a segment [L_i; R_i]. Example Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
instruction
0
36,000
20
72,000
Tags: combinatorics, dp Correct Solution: ``` import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) def f(X): # Xไปฅไธ‹ใงๆกไปถใ‚’ๆบ€ใŸใ™ๆ•ดๆ•ฐใฎๅ€‹ๆ•ฐ digit = len(X) dp = [[[0]*4 for _ in range(2)] for _ in range(digit+1)] # pos,is_less,count_nonzero dp[0][0][0] = 1 for d in range(digit): x = X[d] for i in range(2): for j in range(4): upper = 9 if i else x for k in range(upper+1): if j+(k!=0) <= 3: dp[d+1][i|(k<x)][j+(k!=0)] += dp[d][i][j] res0 = sum(dp[-1][0]) res1 = sum(dp[-1][1]) return res0,res1 t = I() for _ in range(t): L,R = S().split() L = [int(l) for l in L] R = [int(r) for r in R] l0,l1 = f(L) r0,r1 = f(R) ans = r0+r1-l1 print(ans) ```
output
1
36,000
20
72,001
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not. You are given a segment [L; R]. Count the number of classy integers x such that L โ‰ค x โ‰ค R. Each testcase contains several segments, for each of them you are required to solve the problem separately. Input The first line contains a single integer T (1 โ‰ค T โ‰ค 10^4) โ€” the number of segments in a testcase. Each of the next T lines contains two integers L_i and R_i (1 โ‰ค L_i โ‰ค R_i โ‰ค 10^{18}). Output Print T lines โ€” the i-th line should contain the number of classy integers on a segment [L_i; R_i]. Example Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
instruction
0
36,001
20
72,002
Tags: combinatorics, dp Correct Solution: ``` from sys import stdin C = [[0] * 20 for _ in range(20)] for i in range(20): C[i][0] = C[i][i] = 1 for j in range(1, i): C[i][j] = C[i - 1][j] + C[i - 1][j - 1] pw = [1, 9, 81, 729] def get(n, lft): return sum(C[n][i] * pw[i] for i in range(lft + 1) if 0 <= i <= n) def calc(x): s = str(x) res = 0 cur = 3 n = len(s) for i in range(n): if s[i] == '0': continue res += get(n - i - 1, cur) cur -= 1 if cur == -1: break res += get(n - i - 1, cur) * (int(s[i]) - 1) return res stdin.readline() test = stdin.readlines() output = [] for line in test: L, R = map(int, line.split()) ans = calc(R + 1) - calc(L) output.append(ans) print('\n'.join(map(str, output))) ```
output
1
36,001
20
72,003
Provide tags and a correct Python 3 solution for this coding contest problem. Let's call some positive integer classy if its decimal representation contains no more than 3 non-zero digits. For example, numbers 4, 200000, 10203 are classy and numbers 4231, 102306, 7277420000 are not. You are given a segment [L; R]. Count the number of classy integers x such that L โ‰ค x โ‰ค R. Each testcase contains several segments, for each of them you are required to solve the problem separately. Input The first line contains a single integer T (1 โ‰ค T โ‰ค 10^4) โ€” the number of segments in a testcase. Each of the next T lines contains two integers L_i and R_i (1 โ‰ค L_i โ‰ค R_i โ‰ค 10^{18}). Output Print T lines โ€” the i-th line should contain the number of classy integers on a segment [L_i; R_i]. Example Input 4 1 1000 1024 1024 65536 65536 999999 1000001 Output 1000 1 0 2
instruction
0
36,002
20
72,004
Tags: combinatorics, dp Correct Solution: ``` def f(n): if n == 0: return 1 dp = [[[0] * 2 for j in range(4)] for z in range(len(n))] dp[0][3][0] = 1 dp[0][2][0] = int(n[0]) - 1 dp[0][2][1] = 1 for i in range(1, len(n)): for j in range(4): if n[i] == '0': dp[i][j][0] += dp[i - 1][j][0] dp[i][j][1] += dp[i - 1][j][1] else: dp[i][j][0] += dp[i - 1][j][0] + dp[i - 1][j][1] for z in range(1, 10): if z < int(n[i]): if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] + dp[i - 1][j + 1][1] elif z == int(n[i]): if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] dp[i][j][1] += dp[i - 1][j + 1][1] else: if j < 3: dp[i][j][0] += dp[i - 1][j + 1][0] res = 0 for j in range(4): res += dp[len(n) - 1][j][0] + dp[len(n) - 1][j][1] return res t = int(input()) while t: t -= 1 l, r = map(int, input().split()) print(f(str(r)) - f(str(l - 1))) ```
output
1
36,002
20
72,005