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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
"Search" is an operation to obtain the desired information from a large amount of information. Familiar examples include "finding your own exam number from a large number of exam numbers" when announcing your success, or "finding Taro Aizu's phone number" from your phone book. This search operation is also widely used in the computer field.
<image>
There are many ways to search. Consider a search method that can be used when the data to be searched is arranged in ascending (or largest) order.
Using the magnitude relationship between the value located in the center of the data string arranged in ascending order (or in descending order) and the target value, the first half is set as the search range or the second half is searched from the value located in the center. There is a way to narrow down the search range by deciding whether to make it a range. The procedure is as follows.
1. The entire column of data is the scope of the search.
2. Examine the value located in the center of the search range.
3. If the desired value matches the centered value, the search ends.
4. If it is smaller than the target value and the value located in the center, the first half is the search range, and if it is larger, the second half is the search range and returns to 2.
The following is an example of the search method described above. The desired value in this example is 51. Each piece of data is numbered (index), which starts at 0.
Step 1: Initially, the search range is the entire number 0-6.
Step 2: Find the value in the center of the search range. However, the "centered value" is the value at the position of the number obtained by dividing (the number on the left side + the number on the right side) by 2. In other words, in this case, (0 + 6) Γ· 2 is calculated, and the value (36) at number 3 is the value located in the center.
Step 3: Compare the desired value (51) with the centrally located value (36).
Step 4: From the result of step 3, the target value is larger than the value located in the center, so the search range is from number 4 (next to the value located in the center) in the latter half. Use the same procedure to check the value located in the center of the search range, and if the target value is smaller than the value located in the center, the first half is the search range, and if it is larger, the second half is the search range. I will make it smaller. (Repeat of steps 2 to 4) The search ends when the target value matches the value located in the center or the search range is reached.
| <image>
--- | ---
Create a program that takes an array of n numbers as input and outputs the number of times the target value is compared with the value located in the center. However, if the number of the value located in the center is not divisible, the value rounded down to the nearest whole number will be used as the number. It is assumed that the given data columns are sorted in ascending order.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
n
a1
a2
::
an
k
The number of numbers n (1 β€ n β€ 100) is given on the first line, and the i-th number ai (1 β€ ai β€ 100000, integer) is given on the following n lines.
The following line is given the value k (1 β€ k β€ 100000) to search for.
Output
The number of comparisons until the search is completed for each data set is output on one line.
Example
Input
7
11
15
23
36
51
61
86
51
4
1
2
3
5
4
0
Output
3
3
Submitted Solution:
```
while 1:
l,r=0,int(input())
a=[int(input()) for _ in range(r)]
r-=1;c=0
k=int(input())
while l<=r:
c+=1
m=(l+r)//2
if a[m]==k:break
if a[m]<k:l=m+1
else:r=m-1
print(c)
``` | instruction | 0 | 21,952 | 20 | 43,904 |
No | output | 1 | 21,952 | 20 | 43,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,084 | 20 | 44,168 |
Tags: brute force, two pointers
Correct Solution:
```
s = input()
n = len(s)
ans = 0
X = []
for x in range(n):
k = 1
while x + 2*k < n and (s[x] != s[x+k] or s[x] != s[x+2*k]):
k += 1
if x + 2*k < n:
X += [(x, x + 2*k)]
for i in range(len(X)-2, -1, -1):
X[i] = (X[i][0], min(X[i][1], X[i+1][1]))
I = 0
if len(X):
for l in range(n):
if X[I][0] < l:
while I < len(X) and X[I][0] < l:
I += 1
if I == len(X):
break
ans += max(0, n - X[I][1])
print(ans)
``` | output | 1 | 22,084 | 20 | 44,169 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,086 | 20 | 44,172 |
Tags: brute force, two pointers
Correct Solution:
```
s = input()
n = len(s)
l = 0
ans = 0
for i in range(n):
for j in range(i - 1, l, -1):
if 2 * j - i < l:
break
if s[i] == s[j] == s[j + j - i]:
ans += ((2 * j - i) - l + 1) * (n - i)
l = (2 * j - i + 1)
print(ans)
``` | output | 1 | 22,086 | 20 | 44,173 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,087 | 20 | 44,174 |
Tags: brute force, two pointers
Correct Solution:
```
from math import *
from collections import *
import sys
sys.setrecursionlimit(10**9)
s = input()
n = len(s)
ans = 0
for i in range(n):
m = 10**6
for k in range(1,5):
for j in range(i,i+7):
if(j + 2*k >= n):
break
if(s[j] == s[j+k] and s[j] == s[j+2*k]):
m = min(m,j+2*k)
if(m != 10**6):
ans += n-m
print(ans)
``` | output | 1 | 22,087 | 20 | 44,175 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,088 | 20 | 44,176 |
Tags: brute force, two pointers
Correct Solution:
```
import sys
S = sys.stdin.readline()
S = S.strip()
n = len(S)
ans = 0
def check(i, j) :
if j - i < 3 :
return False
for x in range(i, j) :
for k in range(1, j - i) :
if x + 2 * k >= j :
break
if S[x] == S[x+k] == S[x + 2 * k] :
return True
return False
for i in range(n) :
for j in range(i + 1, min(i + 100, n+1)) :
if check(i, j) :
ans += n - j + 1
break
print(ans)
``` | output | 1 | 22,088 | 20 | 44,177 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,089 | 20 | 44,178 |
Tags: brute force, two pointers
Correct Solution:
```
from sys import stdin
s=stdin.readline().strip()
x=-1
ans=0
for i in range(len(s)):
for j in range(1,10):
if (i-2*j)>=0 and s[i]==s[i-j] and s[i-j]==s[i-2*j]:
if (i-2*j)>x:
ans+=(i-2*j-x)*(len(s)-i)
x=i-2*j
print(ans)
``` | output | 1 | 22,089 | 20 | 44,179 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,090 | 20 | 44,180 |
Tags: brute force, two pointers
Correct Solution:
```
X = [[], ['0', '1'], ['00', '01', '10', '11'], ['001', '010', '011', '100', '101', '110'], ['0010', '0011', '0100', '0101', '0110', '1001', '1010', '1011', '1100', '1101'], ['00100', '00101', '00110', '01001', '01011', '01100', '01101', '10010', '10011', '10100', '10110', '11001', '11010', '11011'], ['001001', '001011', '001100', '001101', '010010', '010011', '010110', '011001', '011010', '011011', '100100', '100101', '100110', '101001', '101100', '101101', '110010', '110011', '110100', '110110'], ['0010011', '0011001', '0011010', '0011011', '0100101', '0101100', '0101101', '0110011', '1001100', '1010010', '1010011', '1011010', '1100100', '1100101', '1100110', '1101100'], ['00110011', '01011010', '01100110', '10011001', '10100101', '11001100']]
s = input()
N = len(s)
ans = (N-1)*(N-2)//2
for i in range(N):
for j in range(i+3, min(i+9, N+1)):
if s[i:j] in X[j-i]:
ans -= 1
print(ans)
``` | output | 1 | 22,090 | 20 | 44,181 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0. | instruction | 0 | 22,091 | 20 | 44,182 |
Tags: brute force, two pointers
Correct Solution:
```
#!/usr/bin/env python
def good(l, r):
for x in range(l, r):
for k in range(1, 10):
if x + (k << 1) <= r:
if s[x] == s[x + k] and s[x + k] == s[x + (k << 1)]:
return False
else:
break
return True
s = input(); n = len(s)
answer = 0
for l in range(n):
r = l
while good(l, r):
r += 1
if r >= n:
break
answer += n - r
print(answer)
``` | output | 1 | 22,091 | 20 | 44,183 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
s=input()
def pri(l,r):
k=1
while r-2*k>=l:
if s[r]!=s[r-k] or s[r-k]!=s[r-2*k]:
k+=1
continue
return False
return True
ans=0
for i in range(len(s)):
j=i
while j<len(s) and pri(i,j):
j+=1
ans+=len(s)-j
print(ans)
``` | instruction | 0 | 22,092 | 20 | 44,184 |
Yes | output | 1 | 22,092 | 20 | 44,185 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
#!/usr/bin/python
import sys
data_string = input()
total_seq = 0
current_end_seq = 0
step = 0
while step < len(data_string)- 1:
step += 1
total_seq = total_seq + current_end_seq
min_d = 1
while step - 2 * min_d + 1 > current_end_seq:
if data_string[step] == data_string[step - min_d] == data_string[step - 2 * min_d]:
current_end_seq = step - 2 * min_d + 1
break
min_d += 1
#print(current_end_seq)
print(total_seq + current_end_seq)
``` | instruction | 0 | 22,093 | 20 | 44,186 |
Yes | output | 1 | 22,093 | 20 | 44,187 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
s = input()
len = len(s)
r = len
ans=0
for l in range(len)[::-1]:
r = min(r,l+9)
k = 1
while l+2*k<r:
if s[l] == s[l+k] and s[l] == s[l+2*k]:
r = min(r,l+2*k)
break
k +=1
ans += len-r
print(ans)
``` | instruction | 0 | 22,094 | 20 | 44,188 |
Yes | output | 1 | 22,094 | 20 | 44,189 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
s = input()
n = len(s)
ans = 0
X = []
for x in range(n):
k = 1
while x + 2*k < n and (s[x] != s[x+k] or s[x] != s[x+2*k]):
k += 1
if (x + 2*k < n):
X += [(x, k)]
I = 0
for l in range(n):
while I < len(X) and X[I][0] < l:
I += 1
if I == len(X):
break
ans += max(0, n - X[I][0] - 2*X[I][1])
print(ans)
``` | instruction | 0 | 22,095 | 20 | 44,190 |
No | output | 1 | 22,095 | 20 | 44,191 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
def parse(x, n):
res = 0
se = set(x)
for i in range(len(x) - 1):
for j in range(i + 1, len(x)):
k = x[j] - x[i]
if x[i] + k + k >= n:
break
if x[i] + k + k in se:
res = res + n - (x[i] + k + k)
#print(x[i], x[j])
break
return res
if __name__ == '__main__':
s = input()
one = [i for i in range(len(s)) if s[i] == '1']
zero = [i for i in range(len(s)) if s[i] == '0']
ans = parse(one, len(s))
ans = ans + parse(zero, len(s))
print(ans)
``` | instruction | 0 | 22,096 | 20 | 44,192 |
No | output | 1 | 22,096 | 20 | 44,193 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
s = input()
n = len(s)
ans = 0
for l in range(n):
k = 1
while l + 2*k < n and (s[l] != s[l+k] or s[l] != s[l+2*k]):
k += 1
ans += max(0, n - l - 2*k)
print(ans)
``` | instruction | 0 | 22,097 | 20 | 44,194 |
No | output | 1 | 22,097 | 20 | 44,195 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Toad Rash has a binary string s. A binary string consists only of zeros and ones.
Let n be the length of s.
Rash needs to find the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Find this number of pairs for Rash.
Input
The first line contains the string s (1 β€ |s| β€ 300 000), consisting of zeros and ones.
Output
Output one integer: the number of such pairs of integers l, r that 1 β€ l β€ r β€ n and there is at least one pair of integers x, k such that 1 β€ x, k β€ n, l β€ x < x + 2k β€ r, and s_x = s_{x+k} = s_{x+2k}.
Examples
Input
010101
Output
3
Input
11001100
Output
0
Note
In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5.
In the second example, there are no values x, k for the initial string, so the answer is 0.
Submitted Solution:
```
def parse(x, n):
res = 0
se = set(x)
for i in range(len(x) - 1):
for j in range(i + 1, len(x)):
k = x[j] - x[i]
if x[i] + k + k >= n:
break
if x[i] + k + k in se:
res = res + n - (x[i] + k + k)
#print(x[i], x[j])
break
return res
if __name__ == '__main__':
s = input()
one = [i for i in range(len(s)) if s[i] == '1']
zero = [i for i in range(len(s)) if s[i] == '0']
print(s)
print(one)
print(zero)
ans = parse(one, len(s))
ans = ans + parse(zero, len(s))
print(ans)
``` | instruction | 0 | 22,098 | 20 | 44,196 |
No | output | 1 | 22,098 | 20 | 44,197 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,110 | 20 | 44,220 |
Tags: math
Correct Solution:
```
x,y=map(int,input().split())
if x in [y,y-1] or (x==9 and y==1):
if x == 9 and y==1:
a=9
b=10
elif x==y-1:
a=x*10+9
b=y*10+0
else:
a=x*10
b=y*10+1
print(a,b)
else:
print(-1)
``` | output | 1 | 22,110 | 20 | 44,221 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,111 | 20 | 44,222 |
Tags: math
Correct Solution:
```
a,b=map(int,input().split())
if a<=b:
if abs(a-b)>1 and a!=9 and b!=1:
print(-1)
elif a==b:
print(str(a)+"1",str(b)+"2")
else:
print(str(a)+"9",str(b)+"0")
else:
if a==9 and b==1:
print(9,10)
else:
print(-1)
``` | output | 1 | 22,111 | 20 | 44,223 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,112 | 20 | 44,224 |
Tags: math
Correct Solution:
```
a, b = [int(m) for m in input().split()]
if a == b:
print(str(a) + '1', str(b) + '2')
elif a + 1 == b:
print(a, b)
else:
if a == 9 and b == 1:
print(9, 10)
else:
print(-1)
``` | output | 1 | 22,112 | 20 | 44,225 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,113 | 20 | 44,226 |
Tags: math
Correct Solution:
```
da, db = map(int, input().split())
if da==9 and db==1:
print(99, 100)
elif db-da>1:
print(-1)
elif da>db:
print(-1)
elif da==db:
print(str(da)+'1', str(db)+'2')
else:
print(str(da)+'9', str(db)+'0')
``` | output | 1 | 22,113 | 20 | 44,227 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,114 | 20 | 44,228 |
Tags: math
Correct Solution:
```
a, b = input().split()
a = int(a)
b = int(b)
if b - a == 0:
print(a * 10 + 2, b * 10 + 3)
elif b - a == 1:
print(a * 10 + 9, b * 10)
elif a == 9 and b == 1:
print(a * 10 + 9, b * 100)
else:
print(-1)
``` | output | 1 | 22,114 | 20 | 44,229 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,115 | 20 | 44,230 |
Tags: math
Correct Solution:
```
l=[int(x) for x in input().split()]
da,db=l[0],l[1]
if da==db:
print('{0}0 {1}1'.format(da,db))
elif da==db-1:
print(da,db,sep=' ')
elif da-db==8:
print("9 10")
else:
print(-1)
``` | output | 1 | 22,115 | 20 | 44,231 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,116 | 20 | 44,232 |
Tags: math
Correct Solution:
```
da, db = map(int, input().split())
x, y = 0, 1
while y < 1000000:
if str(x)[0] == str(da) and str(y)[0] == str(db):
break
x += 1
y += 1
if y == 1000000:
print(-1)
else:
print(x, y)
``` | output | 1 | 22,116 | 20 | 44,233 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Kolya is very absent-minded. Today his math teacher asked him to solve a simple problem with the equation a + 1 = b with positive integers a and b, but Kolya forgot the numbers a and b. He does, however, remember that the first (leftmost) digit of a was d_a, and the first (leftmost) digit of b was d_b.
Can you reconstruct any equation a + 1 = b that satisfies this property? It may be possible that Kolya misremembers the digits, and there is no suitable equation, in which case report so.
Input
The only line contains two space-separated digits d_a and d_b (1 β€ d_a, d_b β€ 9).
Output
If there is no equation a + 1 = b with positive integers a and b such that the first digit of a is d_a, and the first digit of b is d_b, print a single number -1.
Otherwise, print any suitable a and b that both are positive and do not exceed 10^9. It is guaranteed that if a solution exists, there also exists a solution with both numbers not exceeding 10^9.
Examples
Input
1 2
Output
199 200
Input
4 4
Output
412 413
Input
5 7
Output
-1
Input
6 2
Output
-1 | instruction | 0 | 22,117 | 20 | 44,234 |
Tags: math
Correct Solution:
```
a,b = map(int, input().split())
if (a == 9) and (b == 1):
print (99, 100)
elif (b - a > 1) or (a > b):
print(-1)
elif (a == b):
print(a*100, a*100+1)
else:
print(a*100+99, b*100)
``` | output | 1 | 22,117 | 20 | 44,235 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,126 | 20 | 44,252 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
q=int(input())
a=[]
for j in range(40):
a.append(3**j)
a.reverse()
s=sum(a)
for i in range(q):
ans=s
n=int(input())
for k in range(40):
if(ans-a[k]>=n):
ans=ans-a[k]
print(ans)
``` | output | 1 | 22,126 | 20 | 44,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,127 | 20 | 44,254 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
from functools import reduce as rd
import operator as optr
import math
import itertools
for _ in range(int(input())):
n = int(input())
v = []
for i in range(0, 39):
v.append(3 ** i)
#print(v)
m = n
add = 0
indx = 0
for i in range(0, 39):
add += v[i]
if add > n:
indx = i
break
visited = [0] * 40
i = indx
while i >= 0:
#print(v[i])
if n >= v[i]:
n -= v[i]
visited[i] = 1
i -= 1
if n == 0:
print(m)
else:
flag = 0
inn = 0
for j in range(0, indx):
if visited[j] != 1:
flag = 1
inn = j
break
else:
m-=v[j]
if flag == 1:
print(m +v[inn] - n)
else:
print(v[indx])
``` | output | 1 | 22,127 | 20 | 44,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,128 | 20 | 44,256 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
def intlog(num,base):
n = num ; res = 0
while n>=base:
n//=base
res+=1
return res
def ceillog(num,base):
n = num ; res = 0
while n:
n//=base
res+=1
return res
t=int(input())
while t:
t-=1
n=int(input())
maxx = ceillog(n,3)
p=[]
for i in range(maxx+1):
p.append(3**i)
chk = sum(p[:-1])
if chk<n: print(p[-1])
else:
while chk>n and p:
if chk-p[-1]<n: p.pop()
else:
chk-=p[-1]
p.pop()
print(chk)
``` | output | 1 | 22,128 | 20 | 44,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,129 | 20 | 44,258 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
arr = [3**i for i in range(40)]
s = sum(arr)
t = int(input())
for _ in range(t):
n = int(input())
m = s
for i in arr[::-1]:
if m - i >= n:
m -= i
print(m)
``` | output | 1 | 22,129 | 20 | 44,259 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,130 | 20 | 44,260 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
bits = ['1']
while int(''.join(bits), 3) < n:
bits.append('1')
for i in range(len(bits)):
bits[i] = '0'
if int(''.join(bits), 3) < n:
bits[i] = '1'
print(int(''.join(bits), 3))
``` | output | 1 | 22,130 | 20 | 44,261 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,131 | 20 | 44,262 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
for q in range(int(input())):
n = int(input())
pos2, val = -1, []
while n:
i = n % 3
val.append(i)
if i == 2: pos2 = len(val) - 1
n //= 3
val.append(0)
if pos2 != -1:
pos0 = val[pos2:].index(0) + pos2
val[pos0] = 1
for i in range(pos0 - 1, -1, -1): val[i] = 0
ans, pw = 0, 1
for i in val:
ans += pw * i
pw *= 3
#if val[len(val) - 1] == 1: ans = pw // 3
print(ans)
``` | output | 1 | 22,131 | 20 | 44,263 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,132 | 20 | 44,264 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
# link: https://codeforces.com/problemset/problem/1249/C2
def generate_triple_representation(num):
array = []
while num != 0:
array.append(num % 3)
num = num // 3
return array[::-1]
for _ in range(int(input())): # number of queries
n = int(input())
ternary_representation = generate_triple_representation(n)
# print(ternary_representation)
if 2 in ternary_representation:
for i in range(len(ternary_representation)):
if ternary_representation[i] == 2:
pos = i
break
ans = 0
if 0 in ternary_representation[:pos+1]:
for j in range(pos, -1, -1):
if ternary_representation[j] == 0:
ans += pow(3, (len(ternary_representation) - j) - 1)
pos = j
break
for k in range(pos, -1, -1):
if ternary_representation[k] == 1:
ans += pow(3, len(ternary_representation) - k - 1)
print(ans)
else:
print(pow(3,len(ternary_representation)))
else:
print(n)
``` | output | 1 | 22,132 | 20 | 44,265 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089 | instruction | 0 | 22,133 | 20 | 44,266 |
Tags: binary search, greedy, math, meet-in-the-middle
Correct Solution:
```
import sys
import math
input=sys.stdin.readline
po=list()
po.append(1)
for i in range(40):
po.append(po[i]*3)
q=int(input())
for _ in range(q):
n=int(input())
vis=[0]*40
ans=0
for i in range(38,-1,-1):
if(ans+po[i]<=n):
ans+=po[i]
vis[i]=1
if(ans==n):
print(ans)
else:
for i in range(0,40):
if(vis[i]==1):
ans-=po[i]
vis[i]=0
else:
print(ans+po[i])
break
``` | output | 1 | 22,133 | 20 | 44,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
t = int(input())
for _ in range(t):
n = int(input())
# ali = list(map(int, input().split()))
# n,m = map(int, input().split())
q = 0
w = 0
while(w<n):
w += 3**q
q += 1
# print(w,q)
while(w>n and q>=0):
if(w-(3**q)>=n):
w -= 3**q
q -= 1
print(w)
``` | instruction | 0 | 22,134 | 20 | 44,268 |
Yes | output | 1 | 22,134 | 20 | 44,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
def bit(n, k):
return n // (3**k) % 3
def solve():
n = int(input())
INF = 10**30
dp = [[INF for _ in range(2)] for __ in range(50)]
dp[49][0] = 0
for ii in range(49):
i = 50-2 - ii
b = bit(n, i)
if b < 2:
dp[i][0] = dp[i+1][0] + b * 3**i
dp[i][1] = dp[i+1][1]
if b == 0:
dp[i][1] = min(dp[i][1], dp[i+1][0] + 3**i)
print(min(dp[0]))
return
q = int(input())
for _ in range(q):
solve()
``` | instruction | 0 | 22,135 | 20 | 44,270 |
Yes | output | 1 | 22,135 | 20 | 44,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
def solve():
x = int(input())
s = 0
i = 0
while (s <= x):
s += 3 ** i
i += 1
i -= 1
while (i >= 0):
if (s - 3 ** i >= x):
s -= 3 ** i
i -= 1
return s
q = int(input())
print(*[solve() for i in range(q)], sep='\n')
``` | instruction | 0 | 22,136 | 20 | 44,272 |
Yes | output | 1 | 22,136 | 20 | 44,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
I = int
R = range
for i in R(I(input())):
n = I(input())
number_formed_untill_now = 0
power_of_3 = 0
while number_formed_untill_now < n:
number_formed_untill_now += 3 ** power_of_3
power_of_3 += 1
if number_formed_untill_now == n:
print(number_formed_untill_now)
else:
for j in R(power_of_3 - 1, -1, -1):
if number_formed_untill_now - 3 ** j >= n:
number_formed_untill_now -= 3 ** j
print(number_formed_untill_now)
``` | instruction | 0 | 22,137 | 20 | 44,274 |
Yes | output | 1 | 22,137 | 20 | 44,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
from sys import stdin, stdout
from math import ceil, log
import bisect
maxpower = ceil(log(10**18, 3))
powers = [1]
t = 1
for i in range(1, maxpower+1):
t*=3
powers.append(t)
allsums = set()
for i in range(len(powers)):
for j in range(i+1, len(powers)+1):
allsums.add(sum(powers[i:j]))
assert len(allsums) == (len(powers)*(len(powers) + 1))//2
allsums = list(allsums)
allsums.sort()
q = int(input())
for _ in range(q):
n = int(input())
ind = bisect.bisect_left(allsums, n)
print(allsums[ind])
``` | instruction | 0 | 22,138 | 20 | 44,276 |
No | output | 1 | 22,138 | 20 | 44,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
def ter(n):
l=[]
while True:
l.append(n%3)
n=n//3
if n<1:
return l[::-1]
for _ in range(int(input())):
n=int(input())
l=ter(n)
flag=0
for i in range(len(l)-1,-1,-1):
if l[i]>1:
flag+=1
l[i]=0
continue
if flag and l[i]==0:
l[i]=1
flag-=1
if flag==0:
for i in range(len(l)):
l[i]=str(l[i])
s=''.join(l)
print(int(s,3))
else:
print(pow(3,len(l)))
``` | instruction | 0 | 22,139 | 20 | 44,278 |
No | output | 1 | 22,139 | 20 | 44,279 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
q=int(input())
for i in range(q):
n=int(input())
nn=n
nnn=n
res=0
li=[0]*100
mx=0
l=[]
while nn:
cou=0
n=nn
while n:
n=n//3
if(n):
cou+=1
li[cou]=1
if(mx==0):
mx=cou
a=3**cou
l.append(cou)
nn-=a
#print(nn,a)
#print(l)
l.sort()
j=0
lol=0
for i in l:
if(li[i]==1):
li[i]=2
else:
j=i
while li[j]:
j+=1
#print(j)
if(j>mx):
lol=1
mx=j
break
su=0
if(lol):
print(3**j)
else:
for i in range(0,mx+1):
if(li[i]==2):
su+=3**i
if(su==0):
print(nnn)
else:
print(su)
``` | instruction | 0 | 22,140 | 20 | 44,280 |
No | output | 1 | 22,140 | 20 | 44,281 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The only difference between easy and hard versions is the maximum value of n.
You are given a positive integer number n. You really love good numbers so you want to find the smallest good number greater than or equal to n.
The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed).
For example:
* 30 is a good number: 30 = 3^3 + 3^1,
* 1 is a good number: 1 = 3^0,
* 12 is a good number: 12 = 3^2 + 3^1,
* but 2 is not a good number: you can't represent it as a sum of distinct powers of 3 (2 = 3^0 + 3^0),
* 19 is not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representations 19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
* 20 is also not a good number: you can't represent it as a sum of distinct powers of 3 (for example, the representation 20 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.
For the given positive integer n find such smallest m (n β€ m) that m is a good number.
You have to answer q independent queries.
Input
The first line of the input contains one integer q (1 β€ q β€ 500) β the number of queries. Then q queries follow.
The only line of the query contains one integer n (1 β€ n β€ 10^{18}).
Output
For each query, print such smallest integer m (where n β€ m) that m is a good number.
Example
Input
8
1
2
6
13
14
3620
10000
1000000000000000000
Output
1
3
9
13
27
6561
19683
1350851717672992089
Submitted Solution:
```
for q in range (int(input())):
n = int(input())
a = n
base3 = []
while n != 0:
base3.append(n%3)
n = n//3
base3.reverse()
if 2 in base3:
for i in range (len(base3)):
if base3[i] == 2:
if 0 in base3[1:i]:
q = len(base3[1:i])-1-base3[1:i][::-1].index(0)
base3[q] = 1
n = 0
for t in range(q+1):
n += base3[t]*(3**(len(base3)-t-1))
else:
base3[0] = 1
n = 3**(len(base3))
break
print(n)
else:
print(a)
fweweqwwe=6
``` | instruction | 0 | 22,141 | 20 | 44,282 |
No | output | 1 | 22,141 | 20 | 44,283 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,142 | 20 | 44,284 |
Tags: combinatorics, math
Correct Solution:
```
fac=[0]*25
fac[0]=1
for i in range(1,25):
fac[i]=fac[i-1]*i
T=int(input());
while True:
cnt = [0]*25
pre = [0]*25
k = int(input())
p=2
le=0
while k!=0:
cnt[k%p]+=1
k//=p
le=p
p+=1
pre[0]=cnt[0]
for i in range(1,le+1):
pre[i]=pre[i-1]+cnt[i]
ans=1
for i in range(2,le+1):
ans*=pre[i-1]-(i-2)
if cnt[0]!=0:
exc=1
for i in range(1,le+1):
pre[i]-=1
for i in range(2,le):
if pre[i-1]-(i-2)<=0:
exc=0
break
exc*=pre[i-1]-(i-2)
ans-=exc*cnt[0]
for i in range(le+1):
ans//=fac[cnt[i]];
print(ans-1)
T-=1
if T==0:
break
``` | output | 1 | 22,142 | 20 | 44,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,143 | 20 | 44,286 |
Tags: combinatorics, math
Correct Solution:
```
f = [1, 1]
for i in range(2, 21):
f.append(f[-1]*i)
def C(n, r):
if n < r:
return 0
return f[n] // (f[n-r]*f[r])
class KeyProblem:
def __init__(self, x):
self.multiplicity = {}
self.calcReminders(x)
ans = self.solve()
if 0 in self.multiplicity:
self.multiplicity[0] -= 1
self.total -= 1
ans -= self.solve()
self.answer = ans-1
def solve(self):
ans = 1
rep = 0
for i in sorted(self.multiplicity.keys(), reverse=True):
if i == 0:
ans *= C(max(0, self.total - i - rep), self.multiplicity[i])
else:
ans *= C(max(0, self.total+1 - i - rep), self.multiplicity[i])
rep += self.multiplicity[i]
return ans
def calcReminders(self, val):
d = 2
while val:
r = val%d
if r in self.multiplicity:
self.multiplicity[r] += 1
else:
self.multiplicity[r] = 1
val = val // d
d += 1
self.total = d - 2
def __str__(self):
return str(self.answer)
if __name__ == '__main__':
total = int(input())
for _ in range(total):
print(KeyProblem(int(input())))
``` | output | 1 | 22,143 | 20 | 44,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,144 | 20 | 44,288 |
Tags: combinatorics, math
Correct Solution:
```
from math import factorial as f
for test in range(int(input())):
k = int(input())
a = []
d = 2
while k:
a.append(k%d)
k //= d
d += 1
n = len(a)
c0 = a.count(0)
a.sort(reverse = True)
ans = 1
j = 0
for i in a:
if i == 0:
ans *= n-i - j
else:
ans *= n-i+1 - j
j += 1
if c0:
badans = 1
n = len(a)
j = 1
for i in range(len(a)-1):
if (a[i] == 0):
badans *= max(0,n-a[i] - j)
else:
badans *= max(0,n-a[i]+1 - j)
j += 1
ans -= badans*c0
for i in range(max(a)+1):
ans //= f(a.count(i))
print(ans-1)
``` | output | 1 | 22,144 | 20 | 44,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,145 | 20 | 44,290 |
Tags: combinatorics, math
Correct Solution:
```
from sys import stdin,stdout
#n=int(stdin.readline().strip())
#n,m=map(int,stdin.readline().strip().split())
#s=list(map(int,stdin.readline().strip().split()))
from math import gcd
from math import factorial as fac
def key1(n):
arr=[]
for i in range(2,2000):
arr.append(n%i)
n=n//i
if n==0:
break
return arr
def key(n):
arr=[]
for i in range(2,2000):
arr.append(n%i)
n=n//i
if n==0:
break
arr.sort()
return arr
def sol(x):
k=key(x)
y=0
ans=0
a=[]
while True:
z=key(y)
if len(z)>len(k):
return a
if z==k:
a.append(y)
ans+=1
y+=1
def f(x):
st=set()
for i in range(len(x)):
for j in range(i+1,len(x)):
st.add(gcd(x[i],x[j]))
st=list(st)
st.sort()
return st
def sol1(x):
x.sort()
cnt=[0 for i in range(22)]
for i in x:
cnt[i]+=1
tot=1
tot1=1
p=0
n=len(x)
for i in range(len(x)-1,-1,-1):
y=n-x[i]-p+1
if x[i]==0:
y-=1
tot*=y
p+=1
if x[0]!=0:
a1=1
for i in range(22):
a1*=fac(cnt[i])
return tot//a1
else:
a1=1
for i in range(22):
a1*=fac(cnt[i])
p=1
for i in range(len(x)-1,0,-1):
y=n-x[i]-p+1
if x[i]==0:
y-=1
tot1*=y
p+=1
cnt[0]-=1
a2=1
for i in range(22):
a2*=fac(cnt[i])
return (tot//a1)-(tot1//a2)
T=int(stdin.readline().strip())
for caso in range(T):
n=int(stdin.readline().strip())
stdout.write("%d\n" % (sol1(key(n))-1))
``` | output | 1 | 22,145 | 20 | 44,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,146 | 20 | 44,292 |
Tags: combinatorics, math
Correct Solution:
```
#######################################################################################################################
# Author: BlackFyre
# Language: PyPy 3.7
#######################################################################################################################
from sys import stdin, stdout, setrecursionlimit
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
from collections import defaultdict as dd
mod = pow(10, 9) + 7
mod2 = 998244353
# setrecursionlimit(3000)
def inp(): return stdin.readline().strip()
def iinp(): return int(inp())
def out(var, end="\n"): stdout.write(str(var) + "\n")
def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end)
def lmp(): return list(mp())
def mp(): return map(int, inp().split())
def smp(): return map(str, inp().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)]
def remadd(x, y): return 1 if x % y else 0
def ceil(a, b): return (a + b - 1) // b
def def_value(): return 0
for _ in range(iinp()):
k = iinp()
a = []
i = 2
while k>0:
a.append(k%i)
k//=i
i+=1
a.sort(reverse = True)
n = len(a)
ans = 1
for i in range(n):
j = i
cnt = 0
while j>=0 and a[j]==a[i]:
j-=1
cnt+=1
ans *= (max(0,n-max(0,a[i]-1)-i)/cnt)
ans-=1
if(a.count(0)==0): zero=0
else: zero=1
for i in range(n):
if a[i]==0: break
j = i
cnt = 0
while j>=0 and a[j]==a[i]:
j-=1
cnt+=1
zero *= (max(0,n-max(0,a[i]-1)-i-1)/cnt)
print(round(ans-zero))
``` | output | 1 | 22,146 | 20 | 44,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,147 | 20 | 44,294 |
Tags: combinatorics, math
Correct Solution:
```
# from itertools import permutations
fact = [1, 1]
for i in range(2, 30):
fact.append(fact[-1]*i)
for i in range(int(input())):
a, r, d = int(input()), [], 2
while a:
r.append(a%d)
a//=d
d+=1
# print
# perm = set(permutations(r))
# c = len(perm)
# for p in perm:
# if p[-1]:
# b = 1
# for i in p:
# if i>b:
# c-=1
# break
# b+=1
# else:
# c-=1
# print(c-1)
m = [0]*(len(r)+2)
for i in r:
m[i]+=1
z, o = m[0], m[1]
p, c, d = z + o, 1, fact[max(1, z)]*fact[max(1, o)]
for i in range(2, len(r) + 2):
c*=p
t=m[i]
if t:
d*=fact[t]
p+=m[i]-1
n = int(c/d)
if z:
z-=1
p, c, d = z + o, 1, fact[max(1, z)]*fact[max(1, o)]
for i in range(2, len(r) + 1):
c*=p
t=m[i]
if t:
d*=fact[t]
p+=m[i]-1
print(n-int(c/d)-1)
else:
print(n-1)
``` | output | 1 | 22,147 | 20 | 44,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,148 | 20 | 44,296 |
Tags: combinatorics, math
Correct Solution:
```
from sys import stdin, stdout
def getpair(p, k):
len = 0
cnta = [0]*30
d = 2
while k > 0:
m = k % d
cnta[m] += 1
len += 1
#print(int(k/d))
#print(k // d)
#print('------------------')
k //= d
d += 1
#print(cnta)
r1 = calc(cnta, len, p)
#print(r1)
r2 = 0
if cnta[0] > 0:
cnta[0] -= 1
r2 = calc(cnta, len-1, p)
#print(r2)
return r1 - r2
def calc(cnta, len, p):
cur = cnta[0]
lr = 1
for i in range(len):
cur += cnta[i+1]
if cur > 0:
lr *= cur
cur -= 1
else:
return 0
for d in cnta:
if d > 0:
lr //= p[d]
#print(lr)
#print('----------------------')
return int(lr)
if __name__ == '__main__':
t = int(stdin.readline())
p = [1]
for i in range(1, 30):
p.append(i*p[i-1])
for i in range(t):
k = int(stdin.readline())
res = getpair(p, k)
stdout.write(str(res-1) + '\n')
``` | output | 1 | 22,148 | 20 | 44,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}. | instruction | 0 | 22,149 | 20 | 44,298 |
Tags: combinatorics, math
Correct Solution:
```
for _ in range(int(input())):
n = int(input())
c = 2
cnt = []
fact = [1]
for i in range(25):
cnt.append(0)
if i:
fact.append(fact[-1]*i)
x = 0
while n:
cnt[n % c] += 1
n //= c
c += 1
x += 1
pref = [cnt[0]]
for i in range(1, 1+x):
pref.append(pref[-1]+cnt[i])
ans = 1
tmp = 1
for i in range(1, 1+x):
ans *= (pref[i]-i+1)
if i == x or cnt[0] == 0:
continue
tmp *= (pref[i]-i)
for i in range(1+x):
ans //= fact[cnt[i]]
if cnt[0]:
tmp //= (fact[cnt[0]-1])
for i in range(1, 1+x):
tmp //= fact[cnt[i]]
ans -= tmp
print(ans-1)
``` | output | 1 | 22,149 | 20 | 44,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}.
Submitted Solution:
```
# by the authority of GOD author: manhar singh sachdev #
import os,sys
from io import BytesIO, IOBase
from collections import Counter
def nCr(a,b):
if a < b:
return 0
ans = 1
for i in range(a,a-b,-1):
ans *= i
for i in range(1,b+1):
ans //= i
return ans
def main():
for _ in range(int(input())):
k = int(input())
ls,x,tot = [],2,1
while k:
ls.append(k%x)
k //= x
x += 1
tot += 1
ls = Counter(ls)
del ls[0]
ans,tk = 1,0
for i in sorted(ls.keys(),reverse=1):
ans *= nCr(tot-tk-i,ls[i])
tk += ls[i]
tk = 0
tot -= 1
ans1 = 1
for i in sorted(ls.keys(),reverse=1):
ans1 *= nCr(tot-tk-i,ls[i])
tk += ls[i]
print(ans-ans1-1)
# Fast IO Region
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")
if __name__ == "__main__":
main()
``` | instruction | 0 | 22,150 | 20 | 44,300 |
Yes | output | 1 | 22,150 | 20 | 44,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}.
Submitted Solution:
```
import math as m
import collections
IP = lambda: list(map(int, input().split()))
INF = 1e9
f = [1]
for i in range(1, 22):
f.append(f[-1]*i)
def solve(arr, n):
d = collections.Counter(arr)
tu = 1; mau = 1
a = [0]*22
for i in arr:
# print(i, n)
a[i] += 1
for i in range(1, len(a)):
a[i] += a[i-1]
# print(a, n)
for i in range(2, n+2):
tu *= (a[i-1]-i+2)
# print(tu, '#####')
for i in d.values():
mau *= f[i]
# print(tu, mau, '####')
return tu//mau
for _ in range(int(input())):
n = IP()[0]
arr = []; i = 2
while n:
arr.append(n%i)
n//=i
i+=1
arr.sort()
pro = solve(arr, len(arr))
# print(pro, '****')
if 0 in arr:
arr.pop(0)
tru = solve(arr, len(arr))
# print(pro, tru)
pro -= tru
print(pro - 1)
``` | instruction | 0 | 22,151 | 20 | 44,302 |
Yes | output | 1 | 22,151 | 20 | 44,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Karl is developing a key storage service. Each user has a positive integer key.
Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.
Karl's fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.
For example, this is how Karl's fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1, 2, 1] and has the fingerprint multiset \{1, 1, 2\}.
Ksenia wants to prove that Karl's fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of \{0, 0, 0, 0, 2, 3, 3, 4\}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.
Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.
Input
The first line contains an integer t (1 β€ t β€ 50 000) β the number of commonly used keys to examine. Each of the next t lines contains one integer k_i (1 β€ k_i β€ 10^{18}) β the key itself.
Output
For each of the keys print one integer β the number of other keys that have the same fingerprint.
Example
Input
3
1
11
123456
Output
0
1
127
Note
The other key with the same fingerprint as 11 is 15. 15 produces a sequence of remainders [1, 1, 2]. So both numbers have the fingerprint multiset \{1, 1, 2\}.
Submitted Solution:
```
import math
def convert(x):
cur = 2
a = []
while (x > 0):
a.append(x % cur)
x //= cur
cur += 1
a.sort()
return a
def C_is_N_po_K(n, k):
if n <= 0:
return 0
if (k == 0):
return 0
if (k > n):
return 0
res = 1
for i in range(n):
res *= i + 1
for i in range(k):
res //= i + 1
for i in range(n - k):
res //= i + 1
return res
def solve(x):
a = convert(int(input()))
l = len(a) + 1
m = {}
s = set()
for x in a:
s.add(x)
if (x not in m):
m[x] = 1
else:
m[x] += 1
ans = -1
for x in s:
if (x == 0):
continue
#print(x)
m[x] -= 1
cur = 1
j = l - 1
ost = 0
while j >= 1:
if (j in m) and (m[j] > 0):
cur *= C_is_N_po_K(l - j - ost - 1, m[j])
#print(l - j - ost - 1, m[j], end = ' ')
ost += m[j]
j -= 1
ans += cur
m[x] += 1
print(ans)
t = int(input())
for i in range(1, t + 1):
solve(i)
``` | instruction | 0 | 22,152 | 20 | 44,304 |
Yes | output | 1 | 22,152 | 20 | 44,305 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.