message stringlengths 2 433k | message_type stringclasses 2
values | message_id int64 0 1 | conversation_id int64 113 108k | cluster float64 12 12 | __index_level_0__ int64 226 217k |
|---|---|---|---|---|---|
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,308 | 12 | 86,616 |
Tags: combinatorics, math
Correct Solution:
```
MOD = 10**9+7
n = int(input())
notUsed = set(range(1, n+1))
chairs = set()
for i, a in enumerate(map(int, input().split()), 1):
if a == -1:
chairs.add(i)
else:
notUsed -= {a}
fixed = len(chairs & notUsed)
m = len(notUsed)
U = m
fact = [0]*(U+1)
fact[0] = 1
for i in range(1, U+1):
fact[i] = fact[i-1]*i % MOD
invfact = [0]*(U+1)
invfact[U] = pow(fact[U], MOD-2, MOD)
for i in reversed(range(U)):
invfact[i] = invfact[i+1]*(i+1) % MOD
def nCr(n, r):
if r < 0 or n < r:
return 0
return fact[n]*invfact[r]*invfact[n-r]
ans = fact[m]
for k in range(1, fixed+1):
ans += nCr(fixed, k)*fact[m-k]*(-1)**k
ans %= MOD
print(ans)
``` | output | 1 | 43,308 | 12 | 86,617 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,309 | 12 | 86,618 |
Tags: combinatorics, math
Correct Solution:
```
MOD = 10**9+7
def solve(values):
notUsed = set(range(1, len(values)+1))
chairs = set()
for i, a in enumerate(values, 1):
if a == -1:
chairs.add(i)
else:
notUsed -= {a}
fixed = len(chairs & notUsed)
m = len(notUsed)
fact, fact_inv = facts(m)
ans = fact[m]
for k in range(1, fixed+1):
ans += nCr(fixed, k, fact, fact_inv)*fact[m-k]*(-1)**k
return ans % MOD
def facts(m):
fact = [0]*(m+1)
fact[0] = 1
for i in range(1, m+1):
fact[i] = fact[i-1]*i % MOD
fact_inv = [0]*(m+1)
fact_inv[m] = pow(fact[m], MOD-2, MOD)
for i in reversed(range(m)):
fact_inv[i] = fact_inv[i + 1]*(i + 1) % MOD
return fact, fact_inv
def nCr(n, r, fact, fact_inv):
if r < 0 or n < r:
return 0
return int(fact[n] * fact_inv[r] * fact_inv[n-r])
if __name__ == '__main__':
input()
values = list(map(int, input().split()))
print(solve(values))
``` | output | 1 | 43,309 | 12 | 86,619 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,310 | 12 | 86,620 |
Tags: combinatorics, math
Correct Solution:
```
def fact(x):
ans=1
for i in range(2,x+1):
ans*=i
return ans
n=int(input())
a=[int(x) for x in input().split()]
s=set(a)
x=0
y=0
for i in range(1,n+1):
if a[i-1]==-1:
g=i in s
(x,y)=(x+1-g,y+g)
otv=fact(x+y)
currf=fact(x+y-1)//fact(x-1)*fact(x)
if x:
otv-=currf
for i in range(2,x+1):
currf//=i
currf*=x-i+1
currf//=x+y-i+1
if i&1:
otv-=currf
else:
otv+=currf
otv%=1000000007
print(otv)
# Made By Mostafa_Khaled
``` | output | 1 | 43,310 | 12 | 86,621 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,311 | 12 | 86,622 |
Tags: combinatorics, math
Correct Solution:
```
input()
t = list(map(int, input().split()))
s, m = 0, 1000000007
p = {i for i, q in enumerate(t, 1) if q == -1}
n, k = len(p), len(p - set(t))
d, c = 2 * (n & 1) - 1, 1
for j in range(n + 1):
d = -d * max(1, j) % m
if n - j <= k:
s += c * d
c = c * max(1, n - j) * pow(k - n + j + 1, m - 2, m) % m
print(s % m)
``` | output | 1 | 43,311 | 12 | 86,623 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,312 | 12 | 86,624 |
Tags: combinatorics, math
Correct Solution:
```
def fact(x):
ans=1
for i in range(2,x+1):
ans*=i
return ans
n=int(input())
a=[int(x) for x in input().split()]
s=set(a)
x=0
y=0
for i in range(1,n+1):
if a[i-1]==-1:
if i in s:
y+=1
else:
x+=1
otv = fact(x + y)
currf=fact(x+y-1)//fact(x-1)*fact(x)
if x:
otv-=currf
for i in range(2,x+1):
currf//=i
currf*=x-i+1
currf//=x+y-i+1
if i&1:
otv-=currf
else:
otv+=currf
otv%=1000000007
print(otv)
``` | output | 1 | 43,312 | 12 | 86,625 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | instruction | 0 | 43,313 | 12 | 86,626 |
Tags: combinatorics, math
Correct Solution:
```
MOD = 10**9+7
n = int(input())
notUsed = set(range(1, n+1))
chairs = set()
for i, a in enumerate(map(int, input().split()), 1):
if a == -1:
chairs.add(i)
else:
notUsed -= {a}
fixed = len(chairs & notUsed)
m = len(notUsed)
fact = [0]*(m+1)
fact[0] = 1
for i in range(1, m+1):
fact[i] = fact[i-1]*i % MOD
fact_inv = [0]*(m+1)
fact_inv[m] = pow(fact[m], MOD-2, MOD)
for i in reversed(range(m)):
fact_inv[i] = fact_inv[i + 1]*(i + 1) % MOD
def nCr(n, r):
if r < 0 or n < r:
return 0
return int(fact[n] * fact_inv[r] * fact_inv[n-r])
ans = fact[m]
for k in range(1, fixed+1):
ans += nCr(fixed, k)*fact[m-k]*(-1)**k
ans %= MOD
print(ans)
``` | output | 1 | 43,313 | 12 | 86,627 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
MOD = 10**9+7
n = int(input())
notUsed = set(range(1, n+1))
chairs = set()
for i, a in enumerate(map(int, input().split()), 1):
if a == -1:
chairs.add(i)
else:
notUsed -= {a}
fixed = len(chairs & notUsed)
m = len(notUsed)
fact = [0]*(m+1)
fact[0] = 1
for i in range(1, m+1):
fact[i] = fact[i-1]*i % MOD
fact_inv = [0]*(m+1)
fact_inv[m] = pow(fact[m], MOD-2, MOD)
for i in reversed(range(m)):
fact_inv[i] = fact_inv[i + 1]*(i + 1) % MOD
def nCr(n, r):
if r < 0 or n < r:
return 0
return int(fact[n] * fact_inv[r] * fact_inv[n-r])
ans = fact[m]
for k in range(1, fixed+1):
ans += nCr(fixed, k)*fact[m-k]*(-1)**k
#ans %= MOD
print(ans % MOD)
``` | instruction | 0 | 43,314 | 12 | 86,628 |
Yes | output | 1 | 43,314 | 12 | 86,629 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
def main():
mod = (10 ** 9) + 7
lim = 20200
fct = [0] * lim
inv_f = [0] * lim
fct[0] = 1
for i in range(1, lim):
fct[i] = fct[i - 1] * i % mod
inv_f[lim - 1] = pow(fct[lim - 1], mod - 2, mod)
for i in range(lim - 1, 0, -1):
inv_f[i - 1] = inv_f[i] * i % mod
def nCk(a, b):
if b == 0:
return 1
else:
return fct[a] * inv_f[b] * inv_f[a - b] % mod
def nPk(a, b):
return fct[a] * inv_f[a - b] % mod
n = int(input())
s = list(map(int, input().split()))
ind, rem = set(), set()
for i, x in enumerate(s):
if x == -1:
ind.add(i + 1)
else:
rem.add(x)
dif = set(range(1, n + 1)) - rem
#print(ind, dif)
k = len(ind)
l = len(ind&dif)
res = 0
for i in range(l + 1):
#print(i, fct[k - i], nCk(k, i))
tmp = fct[k - i] * nCk(l, i) % mod
if i & 1:
res -= tmp
else:
res += tmp
print(res % mod)
if __name__ == '__main__':
main()
``` | instruction | 0 | 43,315 | 12 | 86,630 |
Yes | output | 1 | 43,315 | 12 | 86,631 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
input()
t = list(map(int, input().split()))
s, m = 0, 1000000007
p = {i for i, q in enumerate(t, 1) if q == -1}
n, k = len(p), len(p - set(t))
d = c = 1
for j in range(n + 1):
d = -d * max(1, j) % m
if n - j <= k:
s += c * d
c = c * max(1, n - j) * pow(k - n + j + 1, m - 2, m) % m
print(s % m)
``` | instruction | 0 | 43,316 | 12 | 86,632 |
No | output | 1 | 43,316 | 12 | 86,633 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
MOD = 10**9+7
n = int(input())
notUsed = set(range(1, n+1))
chairs = set()
for i, a in enumerate(map(int, input().split()), 1):
if a == -1:
chairs.add(i)
else:
notUsed -= {a}
fixed = len(chairs & notUsed)
m = len(notUsed)
fact = [0]*(m+1)
fact[0] = 1
for i in range(1, m+1):
fact[i] = fact[i-1]*i
fact_inv = [0]*(m+1)
fact_inv[m] = fact[m]**(-1)
for i in reversed(range(m)):
fact_inv[i] = fact_inv[i + 1]*(i + 1)
def nCr(n, r):
if r < 0 or n < r:
return 0
return int(fact[n] * fact_inv[r] * fact_inv[n-r])
ans = fact[m]
for k in range(1, fixed+1):
ans += nCr(fixed, k)*fact[m-k]*(-1)**k
print(ans)
``` | instruction | 0 | 43,317 | 12 | 86,634 |
No | output | 1 | 43,317 | 12 | 86,635 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
#lahub and Permutations
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
mod = 10**9+7
def pow(n,p,mod=mod): #繰り返し二乗法(nのp乗)
res = 1
while p > 0:
if p % 2 == 0:
n = n ** 2 % mod
p //= 2
else:
res = res * n % mod
p -= 1
return res % mod
def factrial_memo(n=10**6,mod=mod):
fact = [1, 1]
for i in range(2, n + 1):
fact.append((fact[-1] * i) % mod)
return fact
fact = factrial_memo()
def permutation(n,r): #nPr
return fact[n]*pow(fact[n-r],mod-2)%mod
def combination(n,r): #nCr
return permutation(n,r)*pow(fact[r],mod-2)%mod
#return fact[n]*pow(fact[n-r],mod-2)*pow(fact[r],mod-2)
def homogeneous(n,r): #nHr
return combination(n+r-1,r)%mod
#return fact[n+m-1]*pow(fact[n-1],mod-2)*pow(fact[r],mod-2)
n = int(readline())
lst1 = list(map(int,readline().split()))
ct = 0
lst2 = [0]*2_001
for i in range(n):
if i+1 == lst1[i]:
print(0)
exit()
if lst1[i] == -1:
ct += 1
else:
lst2[lst1[i]] = 1
ct2 = 0
for i in range(1,n+1):
if lst2[i] == 0:
ct2 += 1
#lst2:その場所が埋まってないindex
#lst3:その数字が使われてるindex
#何個入れちゃいけない位置に入れるかで数え上げる
#入れちゃいけないものはct2個って、
#ct-ct2個のものはどこに入れても要件を満たす
ans = 0
for i in range(ct2+1):
ans += pow(-1,i)*combination(ct2,i)*fact[ct-i]
ans %= mod
print(ans)
``` | instruction | 0 | 43,318 | 12 | 86,636 |
No | output | 1 | 43,318 | 12 | 86,637 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element ak which has value equal to k (ak = k). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109 + 7).
Input
The first line contains integer n (2 ≤ n ≤ 2000). On the second line, there are n integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation.
Output
Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109 + 7).
Examples
Input
5
-1 -1 4 3 -1
Output
2
Note
For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
Submitted Solution:
```
def main():
mod = (10 ** 9) + 7
lim = 20200
fct = [0] * lim
inv_f = [0] * lim
fct[0] = 1
for i in range(1, lim):
fct[i] = fct[i - 1] * i % mod
inv_f[lim - 1] = pow(fct[lim - 1], mod - 2, mod)
for i in range(lim - 1, 0, -1):
inv_f[i - 1] = inv_f[i] * i % mod
def nCk(a, b):
if b == 0:
return 1
else:
return fct[a] * inv_f[b] * inv_f[a - b] % mod
def nPk(a, b):
return fct[a] * inv_f[a - b] % mod
n = int(input())
s = list(map(int, input().split()))
k = s.count(-1)
res = 0
for i in range(k + 1):
#print(i, fct[i], nCk(k, i))
tmp = fct[i] * nCk(k, i) % mod
if i & 1:
res += tmp
else:
res -= tmp
print(res)
if __name__ == '__main__':
main()
``` | instruction | 0 | 43,319 | 12 | 86,638 |
No | output | 1 | 43,319 | 12 | 86,639 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,343 | 12 | 86,686 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
INF = 10 ** 10
def merge(l, r):
res = l + r
i = j = k = 0
while i < len(l) and j < len(r):
if l[i] < r[j]:
res[k] = l[i]
k += 1
i += 1
else:
res[k] = r[j]
k += 1
j += 1
while i < len(l):
res[k] = l[i]
k += 1
i += 1
while j < len(r):
res[k] = r[j]
k += 1
j += 1
return res
def solve(fl, fr, l, r):
if l == r:
return 0
mid = (l + r) // 2
res = solve(fl, fr, l, mid) + solve(fl, fr, mid + 1, r)
i, j = l, mid + 1
while i <= mid:
while j <= r and fr[j] < fl[i]:
j += 1
res += j - mid - 1
i += 1
fl[l: r + 1] = merge(fl[l: mid + 1], fl[mid + 1: r + 1])
fr[l: r + 1] = merge(fr[l: mid + 1], fr[mid + 1: r + 1])
return res
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
fl, cnt = [], {}
for x in a:
cnt[x] = cnt.get(x, 0) + 1
fl.append(cnt[x])
fr, cnt = [], {}
for x in a[::-1]:
cnt[x] = cnt.get(x, 0) + 1
fr.append(cnt[x])
fr = fr[::-1]
# print(fl, fr)
print(solve(fl, fr, 0, n - 1))
# print(fl, fr)
``` | output | 1 | 43,343 | 12 | 86,687 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,344 | 12 | 86,688 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
class SegTree:
def __init__(self, arr = None, length = None):
"""
Creates a segment tree. If arr (a list) is given, length is ignored,
and we build a segment tree with underlying array arr. If no list is
given, length (an int) must be given, and we build a segment tree with
underlying all-zero array of that length.
"""
if arr is not None:
self.n = len(arr)
self.t = [0 for _ in range(2*self.n)]
self.construct(arr)
else:
self.n = length
self.t = [0 for _ in range(2*self.n)]
def construct(self, a):
"""
Constructs the segment tree from given array (assumed to be of the
right length).
"""
self.t[self.n:] = arr
for i in reversed(range(0, self.n-1)):
self.t[i] = self.t[i*2] + self.t[i*2+1]
def modify(self, p, val):
"""
Sets the value at index p to val.
"""
p += self.n
self.t[p] = val
while p > 1:
self.t[p//2] = self.t[p] + self.t[p ^ 1]
p //= 2
def query(self, l, r):
"""
Gets the sum of all values in the range [l, r) in the underlying array.
"""
res = 0
l += self.n
r += self.n
while l < r:
if l&1:
res += self.t[l]
l += 1
if r&1:
r -= 1
res += self.t[r]
l //= 2
r //= 2
return res
n = int(input())
l = [int(x) for x in input().split()]
front_fs = []
ct = {}
for v in l:
if v not in ct:
ct[v] = 0
ct[v] += 1
front_fs.append(ct[v])
back_fs = []
ct = {}
for v in reversed(l):
if v not in ct:
ct[v] = 0
ct[v] += 1
back_fs.append(ct[v])
back_fs = list(reversed(back_fs))
tot = 0
done = SegTree(length=n)
for fval_f, fval_b in zip(front_fs, back_fs):
tot += done.query(fval_b+1, n)
if fval_f == n:
break
done.modify(fval_f, done.query(fval_f, fval_f + 1) + 1)
#print(fval_f, fval_b, tot)
print(tot)
``` | output | 1 | 43,344 | 12 | 86,689 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,345 | 12 | 86,690 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
n=int(input())
b=list(map(int,input().split()))
d=dict()
pre=[]
j=0
while(j<=(n-1)):
if b[j] in d.keys():
d[b[j]]+=1
else:
d[b[j]]=1
pre.append(d[b[j]])
j+=1
suff=[]
d=dict()
j=n-1
while(j>=0):
if b[j] in d.keys():
d[b[j]]+=1
else:
d[b[j]]=1
suff.append(d[b[j]])
j+=-1
suff.reverse()
def update(bit, index, val):
l = len(bit)
while (index < l):
bit[index] += val
index += index & (-1 * index)
def getsum(bit, index):
ans = 0
while (index > 0):
ans += bit[index]
index -= index & (-1 * index)
return ans
l = len(pre)
bit = [0] * (max(suff) + 1)
res = 0
j=l-1
while(j>=0):
res += getsum(bit, pre[j] - 1)
update(bit, suff[j], 1)
j+=-1
print(res)
``` | output | 1 | 43,345 | 12 | 86,691 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,346 | 12 | 86,692 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
BIT = [0]*(10**6)
def update(idx):
while(idx < len(BIT)):
BIT[idx] += 1
idx += idx & -idx
def query(idx):
s = 0
while(idx > 0):
s += BIT[idx]
idx -= idx & -idx
return s
n = int(input())
a = list(map(int, input().split()))
ans = 0
cnt = {}
l = [0]*n
r = [0]*n
for i in range(n):
cnt[a[i]] = cnt.get(a[i], 0) + 1
l[i] = cnt[a[i]]
cnt.clear()
for i in range(n-1, -1, -1):
cnt[a[i]] = cnt.get(a[i], 0) + 1
r[i] = cnt[a[i]]
if i<n-1:
ans += query(l[i]-1)
update(r[i])
print(ans)
``` | output | 1 | 43,346 | 12 | 86,693 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,347 | 12 | 86,694 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
INF = 10 ** 10
def merge(l, r):
res = l + r
i = j = k = 0
while i < len(l) and j < len(r):
if l[i] < r[j]:
res[k] = l[i]
k += 1
i += 1
else:
res[k] = r[j]
k += 1
j += 1
res[k:] = l[i:] + r[j:]
return res
def solve(fl, fr, l, r):
if l == r:
return 0
mid = (l + r) // 2
res = solve(fl, fr, l, mid) + solve(fl, fr, mid + 1, r)
i, j = l, mid + 1
while i <= mid:
while j <= r and fr[j] < fl[i]:
j += 1
res += j - mid - 1
i += 1
fl[l: r + 1] = merge(fl[l: mid + 1], fl[mid + 1: r + 1])
fr[l: r + 1] = merge(fr[l: mid + 1], fr[mid + 1: r + 1])
return res
if __name__ == "__main__":
n = int(input())
a = list(map(int, input().split()))
fl, cnt = [], {}
for x in a:
cnt[x] = cnt.get(x, 0) + 1
fl.append(cnt[x])
fr, cnt = [], {}
for x in a[::-1]:
cnt[x] = cnt.get(x, 0) + 1
fr.append(cnt[x])
fr = fr[::-1]
# print(fl, fr)
print(solve(fl, fr, 0, n - 1))
# print(fl, fr)
``` | output | 1 | 43,347 | 12 | 86,695 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,348 | 12 | 86,696 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
import sys
input = sys.stdin.readline
n = int(input())
t= list(map(int,input().split()))
d = dict()
j = 0
prefix =[]
while j <=n-1:
if t[j] not in d:
d[t[j]]=1
else:
d[t[j]]+=1
prefix.append(d[t[j]])
j+=1
suff=[]
d = dict()
j= n-1
while j>=0:
if t[j] not in d:
d[t[j]]=1
else:
d[t[j]]+=1
suff.append(d[t[j]])
j-=1
suff= suff[::-1]
def update(bit , index ,val):
p=len(bit)
while index < p:
bit[index]+=val
index += index &(-index)
def getsum(bit , index):
ans = 0
while index >0:
ans += bit[index]
index -= index & (-index)
return ans
l= len(prefix)
bit = [0]*(len(suff)+1)
res =0
j = l-1
while j>=0:
res+=getsum(bit , prefix[j]-1)
update(bit, suff[j],1)
j+=-1
print(res)
``` | output | 1 | 43,348 | 12 | 86,697 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,349 | 12 | 86,698 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
n=int(input())
def getsum(BITTree,i):
s = 0 #initialize result
# index in BITree[] is 1 more than the index in arr[]
i = i+1
# Traverse ancestors of BITree[index]
while i > 0:
# Add current element of BITree to sum
s += BITTree[i]
# Move index to parent node in getSum View
i -= i & (-i)
return s
# Updates a node in Binary Index Tree (BITree) at given index
# in BITree. The given value 'val' is added to BITree[i] and
# all of its ancestors in tree.
def updatebit(BITTree , n , i ,v):
# index in BITree[] is 1 more than the index in arr[]
i += 1
# Traverse all ancestors and add 'val'
while i <= n:
# Add 'val' to current node of BI Tree
BITTree[i] += v
# Update index to that of parent in update View
i += i & (-i)
a=list(map(int,input().split()))
# print(*a)
pre=dict()
pos=dict()
for i in range(n):
if(pre.get(a[i],None)==None):
pre[a[i]]=0
# if(pos.get(a[n-1-i],None)==None):
# pos[a[n-1-i]]=0
pre[a[i]]+=1
# pos[a[n-1-i]]+=1
ans=0
BIT=[0]*(n+1)
for i in range(n-1,0,-1):
# print(i)
pre[a[i]]-=1
if(pos.get(a[i],None)==None):
pos[a[i]]=0
pos[a[i]]+=1
# print(pre)
# print(pos)
updatebit(BIT,n,pos[a[i]],1)
# for j in range(7):
# print(getsum(BIT,j),end=' ')
# print()
# print(getsum(BIT,j),end=' ')
# print()
# if pos[a[i]]>1:
# updatebit(BIT,n,pos[a[i]]-1,-1)
# for j in range(7):
temp=getsum(BIT,pre[a[i-1]]-1)
ans+=temp
# print(temp,pre[a[i-1]]-1,a[i-1],i)
print(ans)
``` | output | 1 | 43,349 | 12 | 86,699 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,350 | 12 | 86,700 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
n = int(input())
l = [int(j) for j in input().split()]
d = dict()
pre = []
for i in range(n):
if l[i] in d:
d[l[i]]+=1
else:
d[l[i]]=1
pre.append(d[l[i]])
suf = [0 for i in range(n)]
d = dict()
for i in range(n-1, -1, -1):
if l[i] in d:
d[l[i]]+=1
else:
d[l[i]]=1
suf[i] = d[l[i]]
def update(bit, index, val):
n = len(bit)
while(index<n):
bit[index]+=val
index += index&(-1*index)
def getsum(bit, index):
n = len(bit)
ans = 0
while(index>0):
ans+=bit[index]
index -= index&(-1*index)
return ans
# arr = [3, 6, 2, 4, 1, 7]
n = len(pre)
# print(pre, suf)
bit = [0]*(max(suf)+1)
inv_ct = 0
for i in range(n-1, -1, -1):
# index = pre[i]
inv_ct += getsum(bit, pre[i]-1)
update(bit, suf[i], 1)
# print(inv_ct, bit)
print(inv_ct)
# print(pre, suf)
# for i in range(n):
# val = pre[i]
``` | output | 1 | 43,350 | 12 | 86,701 |
Provide tags and a correct Python 2 solution for this coding contest problem.
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and ak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, ai) > f(j, n, aj).
Help Pashmak with the test.
Input
The first line of the input contains an integer n (1 ≤ n ≤ 106). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print a single integer — the answer to the problem.
Examples
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | instruction | 0 | 43,351 | 12 | 86,702 |
Tags: data structures, divide and conquer, sortings
Correct Solution:
```
from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
#main code
n=ni()
BITTree=[0]*(n+1)
def gs(i):
s = 0
i = i+1
while i > 0:
s += BITTree[i]
i -= i & (-i)
return s
def ub(i ,v):
i += 1
while i <= n:
BITTree[i] += v
i += i & (-i)
l=li()
l1,l2=[],[]
d=Counter()
for i in range(n-1):
d[l[i]]+=1
l1.append(d[l[i]])
d=Counter()
for i in range(n-1,0,-1):
d[l[i]]+=1
l2.append(d[l[i]])
ans=0
l1=l1[::-1]
for i in range(n-1):
ub(l2[i]+1,1)
ans+=gs(l1[i])
pn(ans)
``` | output | 1 | 43,351 | 12 | 86,703 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,380 | 12 | 86,760 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
def calc(n, k):
if n == 1:
print(1)
else:
#n, k = map(int,input().split())
tot = [0] * (n + 1)
tot[n] = 1
tot[n - 1] = 1
tot[n - 2] = 2
for i in range(n - 3, -1, -1):
tot[i] = tot[i + 1] + tot[i + 2]
def mPrint(a, b):
#for i in range(a + 1, b + 1):
# print(i, end = " ")
#print(a, end = " " )
print(b, end = " ")
for i in range(a, b):
print(i, end = " ")
last = 0
for i in range(n):
#print(k, tot[i + 1])
if k <= tot[i + 1]:
mPrint(last + 1, i + 1)
last = i + 1
else:
k -= tot[i + 1]
print()
n, k = map(int,input().split())
calc(n, k)
``` | output | 1 | 43,380 | 12 | 86,761 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,381 | 12 | 86,762 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
'''def f(n):
n = [i-1 for i in n]
a = []
while max(n) >= 0:
for i in range(len(n)-1,-1,-1):
if n[i] != -1:
break
k = [i]
while n[k[-1]] != -1:
k.append(n[k[-1]])
n[k[-2]] = -1
a.append(k[:-1])
a.reverse()
b = []
for i in a: b += i
return [i+1 for i in b]
from recursive import permute
def g(n):
k = permute([i+1 for i in range(n)])
for i in k:
if f(i) == i:
print(i)
'''
def F(n):
a,b = 1,0
for i in range(n):
a,b = b,a+b
return b
def ans(n,k):
if n == 0:
return []
elif n == 1:
return [1]
elif k > F(n):
return [2,1] + [i+2 for i in ans(n-2,k-F(n))]
else:
return [1] + [i+1 for i in ans(n-1,k)]
n,k = map(int,input().split())
print(' '.join([str(i) for i in ans(n,k)]))
``` | output | 1 | 43,381 | 12 | 86,763 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,382 | 12 | 86,764 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
n, k = list(map(int, input().split()))
#n = int(input())
#k = int(input())
fib = [0] * (n + 1)
fib[0] = 1
fib[1] = 1
res = [0] * n
for i in range(2, n):
fib[i] = fib[i - 1] + fib[i - 2]
idx = 0
while idx < n:
if k <= fib[n - idx - 1]:
res[idx] = idx + 1
idx += 1
else:
k -= fib[n - idx - 1]
res[idx] = idx + 2
res[idx + 1] = idx + 1
idx += 2
print(*res)
``` | output | 1 | 43,382 | 12 | 86,765 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,383 | 12 | 86,766 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
l = [1, 2]
for i in range(1, 100):
l.append(l[i]+l[i-1])
if l[-1]>10**18:
break
l = [0, 0] + l
def perm(n, k):
if n == 1 and k == 1:
return [1]
if n == 2 and k == 1:
return [1, 2]
if n == 2 and k == 2:
return [2, 1]
if k <= l[n]:
return [1] + [i+1 for i in perm(n-1, k)]
return [2, 1] + [i+2 for i in perm(n-2, k - l[n])]
n, k = map(int, input().split(' '))
print(' '.join([str(x) for x in perm(n, k)]))
``` | output | 1 | 43,383 | 12 | 86,767 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,384 | 12 | 86,768 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
#!/usr/bin/python3
arr = [1] * 51
for i in range(2, 51):
arr[i] = arr[i - 1] + arr[i - 2]
ans = []
def generate(i, n, to):
if i == n:
assert to == 1
print(" ".join(map(str, ans)))
return
if i + 1 == n:
ans.append(n)
generate(i + 1, n, to)
return
if arr[n - i - 1] < to:
ans.append(i + 2)
ans.append(i + 1)
generate(i + 2, n, to - arr[n - i - 1])
else:
ans.append(i + 1)
generate(i + 1, n, to)
n, k = map(int, input().split())
generate(0, n, k)
``` | output | 1 | 43,384 | 12 | 86,769 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,385 | 12 | 86,770 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
n, k = map(int, input().split())
f = [1, 1]
for i in range(2, n):
f.append(f[i-2] + f[i-1])
i = n
while i > 0:
i -= 1
if k > f[i]:
print(n - i + 1, n - i, end = ' ')
k -= f[i]
i -= 1
else:
print(n - i, end = ' ')
``` | output | 1 | 43,385 | 12 | 86,771 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,386 | 12 | 86,772 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
def getFibCode(x):
result = []
f =[1, 1]
while f[-2] + f[-1] <= x:
f.append(f[-2] + f[-1])
for i in f[:0:-1]:
if i <= x:
result.append(1)
x -= i
else:
result.append(0)
return result
n, k = tuple(map(int, input().split()))
scheme = getFibCode(k - 1)
answer = list(range(1, n + 1))
j = n - 1
for i in scheme[::-1]:
if i == 1:
answer[j - 1], answer[j] = answer[j], answer[j - 1]
j -= 1
else:
j -= 1
print(' '.join(map(str, answer)))
``` | output | 1 | 43,386 | 12 | 86,773 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3]. | instruction | 0 | 43,387 | 12 | 86,774 |
Tags: binary search, combinatorics, constructive algorithms, greedy, implementation, math
Correct Solution:
```
from bisect import bisect_left
n, k = map(int, input().split())
l = [1, 1, 2]
for i in range(90):
l.append(l[-1] + l[-2])
ps = []
left = k
while left > 0:
nu = bisect_left(l, left)
if nu-1 >= 1:
left = left - l[nu-1]
ps.append(n-nu)
else:
left = 0
to_ret = list(map(str, (range(1, n+1))))
for i in ps:
to_ret[i], to_ret[i+1] = to_ret[i+1], to_ret[i]
print(' '.join(to_ret))
``` | output | 1 | 43,387 | 12 | 86,775 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
def F(n):
a,b = 1,0
for i in range(n):
a,b = b,a+b
return b
def ans(n,k):
if n == 0:
return []
elif n == 1:
return [1]
elif k > F(n):
return [2,1] + [i+2 for i in ans(n-2,k-F(n))]
else:
return [1] + [i+1 for i in ans(n-1,k)]
n,k = map(int,input().split())
print(' '.join([str(i) for i in ans(n,k)]))
``` | instruction | 0 | 43,388 | 12 | 86,776 |
Yes | output | 1 | 43,388 | 12 | 86,777 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
import math
import sys
def fib(x):
if x<1:
return 0
if x==1:
return 1
if x==2:
return 2
a=1
b=2
for i in range(0,x):
c=a
a=b
b=c+b
return b-a
inp=list(map(int,input().split()))
n=inp[0]
k=inp[1]
k2=k
n2=n
ans=[]
c=0
while n2>0:
f1=fib(n2-1)
f2=fib(n2-2)
if n2==1:
ans.append(1+c)
break
if k2<=f1:
ans.append(1+c)
n2-=1
c+=1
else:
ans.append(2+c)
ans.append(1+c)
c+=2
n2-=2
k2=k2-f1
if n==1:
ans=[1]
if n==2 and k==1:
ans=[1,2]
if n==2 and k==2:
ans=[2,1]
for i in range(0,n-1):
print(str(ans[i]),end=" ")
print(str(ans[n-1]))
``` | instruction | 0 | 43,389 | 12 | 86,778 |
Yes | output | 1 | 43,389 | 12 | 86,779 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
"""
#If FastIO not needed, used this and don't forget to strip
#import sys, math
#input = sys.stdin.readline
"""
import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left, bisect_right
from types import GeneratorType
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
import os
self.os = os
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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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:
self.os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from collections import defaultdict as dd, deque as dq
import math, string
def getInts():
return [int(s) for s in input().split()]
def getInt():
return int(input())
def getStrs():
return [s for s in input().split()]
def getStr():
return input()
def listStr():
return list(input())
MOD = 10**9+7
"""
-I'm pretty confident that the internal ordering is only preserved in pairs
-The cyclic pairs must be adjacent and of the form (i, i+1)
-The pairs must already occupy the correct blocks
[2,1] 3 [5,4] 6... etc
So the questions are: how many permutations begin with 1, and how many begin with 2?
Number beginning with 1 = dp[N-1][1] + dp[N-1][2]
Number beginning with 2 = dp[N-2][1] + dp[N-2][2]
1 2 3 4 5 6
1 2 3 4 (6 5)
1 2 3 (5 4) 6
1 2 (4 3) 5 6
1 2 (4 3) (6 5)
1 (3 2) 4 5 6
1 (3 2) 4 (6 5)
1 (3 2) (5 4) 6
(2 1) 3 4 5 6
(2 1) 3 4 (6 5)
(2 1) 3 (5 4) 6
(2 1) (4 3) 5 6
(2 1) (4 3) (6 5)
"""
def solve():
N, K = getInts()
if N == 1:
print(1)
return
dp = [[0 for j in range(2)] for i in range(N+1)]
dp[1][0] = 1
dp[2][0] = 1
dp[2][1] = 1
for j in range(3,N+1):
dp[j][0] = dp[j-1][0] + dp[j-1][1]
dp[j][1] = dp[j-2][0] + dp[j-2][1]
#there are sum(dp[N]) sequences, and we need to know whether K <= dp[N][0] or not
ans = []
to_fill = N
curr = 1
while len(ans) < N:
if K <= dp[to_fill][0]:
ans.append(curr)
to_fill -= 1
curr += 1
else:
K -= dp[to_fill][0]
ans.append(curr+1)
ans.append(curr)
to_fill -= 2
curr += 2
print(*ans)
return
#for _ in range(getInt()):
solve()
``` | instruction | 0 | 43,390 | 12 | 86,780 |
Yes | output | 1 | 43,390 | 12 | 86,781 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
#! /usr/bin/env python3
def phib_gen(n):
p = [1, 1]
for i in range(2, n):
p.append(p[-1] + p[-2])
return tuple(p)
n, k = tuple(map(int, input().split()))
phib = phib_gen(n + 1)
def gen_sequence(n, k):
while n > 0:
if k >= phib[n - 1]:
k -= phib[n - 1]
n -= 2
yield 2
else:
n -= 1
yield 1
def gen_perm(n, k):
elem = 1
for i in gen_sequence(n, k):
if i == 2:
yield elem + 1
yield elem
elem += 2
else:
yield elem
elem += 1
print(' '.join(map(str, gen_perm(n, k - 1))))
``` | instruction | 0 | 43,391 | 12 | 86,782 |
Yes | output | 1 | 43,391 | 12 | 86,783 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
def fibo(n,L):
L=[0]*(n+2)
L[1]=1
L[2]=1
if n<2 or L[n]!=0:
return L[n]
else:
k=fibo(n//2+1,L)**2-((-1)**n)*fibo((n-1)//2,L)**2
L[n]=k
return k
L=[]
def func(k,n,s,L,c,m):
if k==1:
for j in range(s,s+n):
print(j,end=' ')
elif c<m:
i=1
while fibo(i,L)<k:
i+=1
for j in range(s,s+n-i+1):
print(j,end=' ')
c+=1
if c<=m-2:
print(s+n-i+2,s+n-i+1,end=' ')
func(k-fibo(i-1,L)+1,i-2,s+n-i+3,L,c+2,m)
elif c==m-1:
print(s+n-i+1)
c+=1
n,k=map(int,input().split(' '))
func(k,n,1,L,0,n)
``` | instruction | 0 | 43,392 | 12 | 86,784 |
No | output | 1 | 43,392 | 12 | 86,785 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
import math
f = [1]
n, k = map(int, input().split())
for i in range(n):
f.append(f[-1] * (i + 1))
d = []
for i in range(n):
d.append(i + 1)
k -= 1
res = []
for i in range(n, 1, -1):
current = k // f[i - 1]
res.append(d[current])
for j in range(current, len(d) - 1):
d[j], d[j + 1] = d[j + 1], d[j]
d.pop()
k %= f[i - 1]
res.append(d[0])
print(*res)
``` | instruction | 0 | 43,393 | 12 | 86,786 |
No | output | 1 | 43,393 | 12 | 86,787 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
n, k = map(int, input().split())
k -= 1
permt = [0 for i in range(100)]
permt[0] = 1
for i in range(1, 100):
permt[i] = permt[i-1]*i
def perm(i):
if i <= 1:
return 1
return permt[i]
numt = [0 for i in range(100)]
numt[0] = 1
for i in range(1, 100):
sm = 0
for j in range(1, i+1):
sm += perm(j-2)*numt[i-j]
numt[i] = sm
def num(n):
if n == 0:
return 1
return numt[n]
def gp(n, k):
if n == 0:
return []
if n == 1:
return [1]
if n == 2:
return [2, 1]
l = [0 for i in range(n)]
l[0] = n;
ll = list(range(n-1))
kk = k
for i in range(1, n):
sm = perm(n-2-i)
for j in range(len(ll)):
if i == ll[j]:
continue
if k < sm:
l[i] = ll[j]+1
del ll[j]
break
k -= sm
return l
def calc(n, k):
if n == 0:
return []
sm = 0
for i in range(1, n+1):
ni = num(n-i);
sm = perm(i-2)*ni
if k < sm:
l = gp(i, k//ni) + list(map(lambda m:m+i, calc(n-i, k%ni)))
return l
k -= sm
return []
result = calc(n, k)
print(' '.join(map(str, result)))
``` | instruction | 0 | 43,394 | 12 | 86,788 |
No | output | 1 | 43,394 | 12 | 86,789 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya's list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Examples
Input
4 3
Output
1 3 2 4
Input
10 1
Output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
Submitted Solution:
```
n, k = map(int, input().split())
k -= 1
permt = [0 for i in range(100)]
permt[0] = 1
for i in range(1, 100):
permt[i] = permt[i-1]*i
def perm(i):
if i <= 1:
return 1
return permt[i]
numt = [0 for i in range(100)]
numt[0] = 1
for i in range(1, 100):
sm = 0
for j in range(1, i+1):
sm += perm(j-2)*numt[i-j]
numt[i] = sm
def num(n):
if n == 0:
return 1
return numt[n]
def gp(n, k):
if n == 0:
return []
if n == 1:
return [1]
if n == 2:
return [2, 1]
l = [0 for i in range(n)]
p = [i for i in range(n)]
p[n-1] = 0
l[0] = n;
ll = list(range(n-1))
kk = k
for i in range(1, n):
sm = perm(n-2-i)
for j in range(len(ll)):
if p[i] == ll[j]:
continue
if k < sm:
l[i] = ll[j]+1
for k in range(n):
if p[k] == ll[j]:
p[k] = l[i]
break
del ll[j]
break
k -= sm
return l
def calc(n, k):
if n == 0:
return []
sm = 0
for i in range(1, n+1):
ni = num(n-i);
sm = perm(i-2)*ni
if k < sm:
l = gp(i, k//ni) + list(map(lambda m:m+i, calc(n-i, k%ni)))
return l
k -= sm
return []
result = calc(n, k)
print(' '.join(map(str, result)))
``` | instruction | 0 | 43,395 | 12 | 86,790 |
No | output | 1 | 43,395 | 12 | 86,791 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,863 | 12 | 87,726 |
Tags: math
Correct Solution:
```
n = int(input())
m = list(map(int,input().split()))
perm = [0 for _ in range(n)]
for i in range(1,n):
perm[i]=perm[i-1]+m[i-1]
x = perm[::]
x.sort()
val = 1-x[0]
x=[x[i]+val for i in range(n)]
if x==[i+1 for i in range(n)]:
perm=[perm[i]+val for i in range(n)]
for elem in perm:
print(elem,end=" ")
else:
print(-1)
``` | output | 1 | 43,863 | 12 | 87,727 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,864 | 12 | 87,728 |
Tags: math
Correct Solution:
```
from itertools import*
n=int(input())
a=[0,*accumulate(map(int,input().split()))]
m=min(a)
if set(a)==set(range(m,m+n)):print(*(x-m+1for x in a))
else:print(-1)
``` | output | 1 | 43,864 | 12 | 87,729 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,865 | 12 | 87,730 |
Tags: math
Correct Solution:
```
def solve(n, q):
x = 0
ans = [x]
max_p = x
max_i = 0
for i, d in enumerate(q):
p = ans[i] + d
if p > max_p:
max_p = p
max_i = i + 1
ans.append(p)
diff = n - ans[max_i]
used = [0 for i in range(n)]
for i in range(n):
ans[i] += diff
if ans[i] < 1 or ans[i] > n:
print(-1)
return
used[ans[i] - 1] += 1
if used[ans[i] - 1] > 1:
print(-1)
return
for u in used:
if u == 0:
print(-1)
return
print(' '.join(str(p) for p in ans))
if __name__ == '__main__':
n = int(input())
q = [int(i) for i in input().split()]
solve(n, q)
``` | output | 1 | 43,865 | 12 | 87,731 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,866 | 12 | 87,732 |
Tags: math
Correct Solution:
```
n = int(input())
q = list(map(int, input().split()))
a = [0] * n
a[0] = 1
for i in range(1, n):
a[i] = q[i - 1] + a[i - 1]
m = min(a)
d = 1 - m
if d > 0:
for i in range(n):
a[i] += d
used = [0] * n
for i in a:
if i > n or used[i - 1]:
print(-1)
exit(0)
else:
used[i - 1] = 1
m = min(used)
if m == 1:
print(*a)
else:
print(-1)
``` | output | 1 | 43,866 | 12 | 87,733 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,867 | 12 | 87,734 |
Tags: math
Correct Solution:
```
n = int(input())
q = list(map(int, input().split()))
s = n*(n+1)//2
for i in range(n-1):
s -= (n-1-i)*q[i]
if s%n!=0:
print(-1)
exit()
p1 = s//n
if p1<1 or n<p1:
print(-1)
exit()
se = set()
se.add(p1)
p = [p1]
for i in range(n-1):
np = p[-1]+q[i]
if np<1 or n<np or np in se:
print(-1)
exit()
se.add(np)
p.append(np)
print(*p)
``` | output | 1 | 43,867 | 12 | 87,735 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,868 | 12 | 87,736 |
Tags: math
Correct Solution:
```
from itertools import accumulate
n=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
a=list(accumulate(a))
x=1-min(a)
for _ in range(len(a)):
a[_]+=x
y=0
for _ in range(len(a)):
if a[_]<=n:
y+=1
if y==n and len(list(set(a)))==n:
print(*a)
else:
print(-1)
``` | output | 1 | 43,868 | 12 | 87,737 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,869 | 12 | 87,738 |
Tags: math
Correct Solution:
```
n = int(input())
q = list(map(int, input().split()))
p = list(range(n))
p[0] = 0
minp = 0
for i in range(1, n):
p[i] = q[i - 1] + p[i - 1]
if p[i] < minp:
minp = p[i]
minp = -minp
for i in range(n):
p[i] += minp + 1
sp = sorted(p)
if sp[0] != 1:
print(-1)
else:
fail = 0
for i in range(1, n):
if sp[i] != sp[i - 1] + 1:
fail = 1
break
if fail:
print(-1)
else:
print(*p)
``` | output | 1 | 43,869 | 12 | 87,739 |
Provide tags and a correct Python 3 solution for this coding contest problem.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1 | instruction | 0 | 43,870 | 12 | 87,740 |
Tags: math
Correct Solution:
```
n = int(input())
a = list(map(int, input().split()))
mn = 0
sm = 0
for i in range(0, n-1):
sm += a[i]
mn = min(mn, sm)
start = -mn+1
used = [0]*(n+1)
ans = []
for i in range(n-1):
if start > n or start <= 0:
print(-1)
exit(0)
if used[start] == 1:
print(-1)
exit(0)
ans.append(start)
used[start] = 1
start += a[i]
if start > n or start <= 0:
print(-1)
exit(0)
if used[start] == 1:
print(-1)
exit(0)
ans.append(start)
print(*ans)
``` | output | 1 | 43,870 | 12 | 87,741 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
n = int(input())
a = [int(x) for x in input().split()]
b = [0 for i in range(n)]
for i, x in enumerate(a):
b[i+1] = b[i] + x
#print(b)
minb = min(b)
c = [0 for i in range(n)]
for i, x in enumerate(b):
b[i] = x - minb + 1
if (b[i] <= n):
c[b[i]-1] = 1
#print(b)
#print(c)
if min(c) == 0:
print(-1)
else:
for x in b:
print(x, end=' ')
``` | instruction | 0 | 43,871 | 12 | 87,742 |
Yes | output | 1 | 43,871 | 12 | 87,743 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
n=int(input())
l=[]
for i in range(n):
l.append(i+1)
q=list(map(int,input().split()))
q1=[q[0]]*(n-1)
for i in range(1,n-1):
q1[i]=q1[i-1]+q[i]
m=max(q1)
if m<0:
a=n
else:
a=n-m
p=[a]
for i in range(len(q1)):
p.append(a+q1[i])
if sorted(p)==l:
print(*p)
else:
print(-1)
``` | instruction | 0 | 43,872 | 12 | 87,744 |
Yes | output | 1 | 43,872 | 12 | 87,745 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
#!/usr/bin/env python
from __future__ import division, print_function
import io
import os
import sys
if sys.version_info[0] < 3:
from __builtin__ import xrange as range
from cStringIO import StringIO
from future_builtins import ascii, filter, hex, map, oct, zip
else:
from io import BytesIO as StringIO
sys.stdout, stream = io.IOBase(), StringIO()
sys.stdout.flush = lambda: os.write(1, stream.getvalue()) and not stream.truncate(0) and stream.seek(0)
sys.stdout.write = stream.write if sys.version_info[0] < 3 else lambda s: stream.write(s.encode())
input, flush = sys.stdin.readline, sys.stdout.flush
input = StringIO(os.read(0, os.fstat(0).st_size)).readline
def main():
n = int(input())
a = [0] * n
for i, qi in enumerate(input().split()):
a[i + 1] = a[i] + int(qi)
min_a = min(a)
a = [1 + ai - min_a for ai in a]
if sorted(a) == list(range(1, n + 1)):
print(*a)
else:
print(-1)
if __name__ == '__main__':
main()
``` | instruction | 0 | 43,873 | 12 | 87,746 |
Yes | output | 1 | 43,873 | 12 | 87,747 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
n = int(input())
inp = input()
arr = list(map(int, inp.split()))
def ans(n, q):
p = [0] * n
ise = [0] * n
p[n - 1] = n * (n + 1) // 2;
for i in range(n - 1):
p[n - 1] += q[i] * (i + 1)
if p[n - 1] % n != 0:
return [-1]
p[n - 1] = p[n - 1] // n
if p[n - 1] < 1 or p[n - 1] > n:
return [-1]
ise[p[n-1] - 1] = 1
for i in range(n - 1):
p[n - 2 - i] = p[n - 1 - i] - q[n - 2 - i]
if p[n - 2 - i] < 1 or p[n - 2 - i] > n:
return [-1]
if ise[p[n - 2 - i] - 1] != 0:
return [-1]
ise[p[n - 2 - i] - 1] = 1
return p
a = ans(n, arr)
print(' '.join(map(str, a)))
``` | instruction | 0 | 43,874 | 12 | 87,748 |
Yes | output | 1 | 43,874 | 12 | 87,749 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
An array of integers p_1, p_2, ..., p_n is called a permutation if it contains each number from 1 to n exactly once. For example, the following arrays are permutations: [3, 1, 2], [1], [1, 2, 3, 4, 5] and [4, 3, 1, 2]. The following arrays are not permutations: [2], [1, 1], [2, 3, 4].
Polycarp invented a really cool permutation p_1, p_2, ..., p_n of length n. It is very disappointing, but he forgot this permutation. He only remembers the array q_1, q_2, ..., q_{n-1} of length n-1, where q_i=p_{i+1}-p_i.
Given n and q=q_1, q_2, ..., q_{n-1}, help Polycarp restore the invented permutation.
Input
The first line contains the integer n (2 ≤ n ≤ 2⋅10^5) — the length of the permutation to restore. The second line contains n-1 integers q_1, q_2, ..., q_{n-1} (-n < q_i < n).
Output
Print the integer -1 if there is no such permutation of length n which corresponds to the given array q. Otherwise, if it exists, print p_1, p_2, ..., p_n. Print any such permutation if there are many of them.
Examples
Input
3
-2 1
Output
3 1 2
Input
5
1 1 1 1
Output
1 2 3 4 5
Input
4
-1 2 2
Output
-1
Submitted Solution:
```
n=int(input())
l1=list(map(int,input().split()))
x=sum(l1)
t=0
if x>0:
t+=1
for i in range(0,n-2):
if (x-l1[i]) >0:
t+=1
l2=[0]*n
d1={}
l2[n-1]=t+1
d1[t+1]=1
l2[0]=t+1-x
flag=0
if l2[0]<1:
print(-1)
flag=1
if flag==0:
for i in range(0,n-2):
temp=l1[i]+l2[i]
if temp>0:
if temp not in d1:
d1[temp]=1
l2[i+1]=temp
else :
flag=1
break
else :
flag=1
break
if flag==0 and max(d1)==n:
print(' '.join(str(x) for x in l2))
else :
print(-1)
``` | instruction | 0 | 43,875 | 12 | 87,750 |
No | output | 1 | 43,875 | 12 | 87,751 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.