message stringlengths 2 67k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 463 109k | cluster float64 19 19 | __index_level_0__ int64 926 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,629 | 19 | 195,258 |
"Correct Solution:
```
n,a,b=map(int,input().split())
if (b-a)%2==0:
print((b-a)//2)
else:
c=b-a+1
print(c%2+c//2+min(a-1,n-b))
``` | output | 1 | 97,629 | 19 | 195,259 |
Provide a correct Python 3 solution for this coding contest problem.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2 | instruction | 0 | 97,630 | 19 | 195,260 |
"Correct Solution:
```
N, A, B = map(int, input().split())
dif = B-A
if dif%2 == 0:
print(dif//2)
else:
print(min(A-1, N-B)+1+dif//2)
``` | output | 1 | 97,630 | 19 | 195,261 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
n,a,b=map(int,input().split())
if (b-a)%2==0:
print((b-a)//2)
else:
x=min(a-1,n-b)
y=(b-a-1)//2
print(x+y+1)
``` | instruction | 0 | 97,631 | 19 | 195,262 |
Yes | output | 1 | 97,631 | 19 | 195,263 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
N,A,B = map(int,input().split())
if((B-A+1)%2==1):
print((B-A)//2)
else:
print(min(A-1,N-B)+1+(B-A-1)//2)
``` | instruction | 0 | 97,632 | 19 | 195,264 |
Yes | output | 1 | 97,632 | 19 | 195,265 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
N, A, B = map(int,input().split())
if (A-B)%2 == 0:
print(abs(A-B)//2)
else:
print(min((A+B-1)//2,(2*N-(A+B)+1)//2))
``` | instruction | 0 | 97,633 | 19 | 195,266 |
Yes | output | 1 | 97,633 | 19 | 195,267 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
n,a,b= map(int, input().split())
if (b-a)%2==0:
print((b-a)//2)
else:
print(min((a-1)+((1+b-a)//2),(n-b)+(1+b-a)//2))
``` | instruction | 0 | 97,634 | 19 | 195,268 |
Yes | output | 1 | 97,634 | 19 | 195,269 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
import math
n,a,b = map(int,input().split())
ans = ((b-a)/2)
if int(ans) == ans:
print(round(ans))
else:
print(math.ceil(ans))
``` | instruction | 0 | 97,635 | 19 | 195,270 |
No | output | 1 | 97,635 | 19 | 195,271 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
n,a,b=map(int,input().split())
cnt=0
if (b-a)%2 == 0:
cnt=round(b-(b+a)/2)
print(cnt)
exit()
if (b-a)%2 == 1:
if a==1:
if b==n:
c=n-a
d=round(1+((b-1)-(((b-1)+a)/2)))
cnt=min(c,d)
print(cnt)
exit()
if b==n:
if a==1:
c=b-1
d=round(1+(b-(b+a+1)/2))
cnt=min(c,d)
print(cnt)
exit()
if a-1 >= n-b:
cnt=round(n-a)
print(cnt)
exit()
if a-1 <= n-b:
cnt=round(b-1)
print(cnt)
exit()
``` | instruction | 0 | 97,636 | 19 | 195,272 |
No | output | 1 | 97,636 | 19 | 195,273 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
a = list(map(int, input().split()))
N,A,B = a[0],a[1],a[2]
dim = B - A
if dim % 2 == 0:
print(int(dim/2))
if dim % 2 == 1:
dimAN = N - A
dimBN = N - B
dimB1 = B - 1
dimA1 = A - 1
if dimAN > dimB1:#Aが1にいって折り返す
print(dimA1 + int((dim-1)/2)+1)
elif dimAN < dimB1:
print(dimBN + int((dim-1)/2)+1)
elif dimAN == dimB1:
print(dimB1 + int((dim-1)/2)+1)
``` | instruction | 0 | 97,637 | 19 | 195,274 |
No | output | 1 | 97,637 | 19 | 195,275 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
2N players are running a competitive table tennis training on N tables numbered from 1 to N.
The training consists of rounds. In each round, the players form N pairs, one pair per table. In each pair, competitors play a match against each other. As a result, one of them wins and the other one loses.
The winner of the match on table X plays on table X-1 in the next round, except for the winner of the match on table 1 who stays at table 1.
Similarly, the loser of the match on table X plays on table X+1 in the next round, except for the loser of the match on table N who stays at table N.
Two friends are playing their first round matches on distinct tables A and B. Let's assume that the friends are strong enough to win or lose any match at will. What is the smallest number of rounds after which the friends can get to play a match against each other?
Constraints
* 2 \leq N \leq 10^{18}
* 1 \leq A < B \leq N
* All input values are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print the smallest number of rounds after which the friends can get to play a match against each other.
Examples
Input
5 2 4
Output
1
Input
5 2 3
Output
2
Submitted Solution:
```
n, a, b = map(int, input().split())
ab = b-a
if ab%2 == 0:
print(ab//2)
else:
if ab == 1:
print(b-1)
elif n-b > a:
if (b-(a-1)-1) %2==0:
print((b-a-1-1)//2 + (a-1))
else:
print((b-a-2-1)//2 + (a-1))
else:
if (n-(a+n-b))%2==0:
print((n-a+n-b)//2 + (n-b))
else:
print((n-a+n-b+1)//2 + (n-b))
``` | instruction | 0 | 97,638 | 19 | 195,276 |
No | output | 1 | 97,638 | 19 | 195,277 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
a,b,n=map(int,input().split())
c=0
import math
while n>0:
if c==0:
y=math.gcd(a,n)
n=n-y
c=1
elif c==1:
y=math.gcd(b,n)
n-=y
c=0
if c==1:
print(0)
elif c==0:
print(1)
``` | instruction | 0 | 97,930 | 19 | 195,860 |
Yes | output | 1 | 97,930 | 19 | 195,861 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
gcd = lambda a, b: gcd(b, a % b) if b else a
a, b, n = map(int, input().split())
code = 1
while True:
n -= gcd(n, a) if code else gcd(n, b)
if n < 0:
print(code)
break
code = 1 - code
``` | instruction | 0 | 97,931 | 19 | 195,862 |
Yes | output | 1 | 97,931 | 19 | 195,863 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
gcd = lambda x, y: gcd(y, x % y) if y else x
a, b, n = map(int, input().split())
while True:
n -= gcd(n, a)
if n < 0:
print(1)
break
n -= gcd(n, b)
if n < 0:
print(0)
break
``` | instruction | 0 | 97,932 | 19 | 195,864 |
Yes | output | 1 | 97,932 | 19 | 195,865 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
from math import gcd
def f(a,b,n):
c=c2=0
while(n>0):
if gcd(a,n)<=n:
n-=gcd(a,n)
c+=1
if gcd(b,n)<=n:
n-=gcd(b,n)
c2+=1
if c>c2:
return 0
else:
return 1
a,b,n=map(int,input().split())
print(f(a,b,n))
``` | instruction | 0 | 97,933 | 19 | 195,866 |
Yes | output | 1 | 97,933 | 19 | 195,867 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
import fractions
a=input().split(' ')
anum=int(a[0])
bnum=int(a[1])
heap=int(a[2])
sa=0
while sa==0:
if heap>=fractions.gcd(heap, anum):
heap-=heap>=fractions.gcd(heap, anum)
else:
print("1")
break
if heap>=fractions.gcd(heap, bnum):
heap-=heap>=fractions.gcd(heap, bnum)
else:
print("0")
break
``` | instruction | 0 | 97,934 | 19 | 195,868 |
No | output | 1 | 97,934 | 19 | 195,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
def gcd(n, m):
i = 0
while i < m:
if m%(m-i) == 0 and n%(m-i) == 0:
break
i += 1
return (m - i)
a, b, n = input().split()
a = int(a)
b = int(b)
n = int(n)
v = 1
while n > a or n > b:
if v%2 == 0:
n = n - gcd(b, n)
else:
n = n - gcd(a, n)
v += 1
if v%2 == 0:
print(1)
else:
print(0)
``` | instruction | 0 | 97,935 | 19 | 195,870 |
No | output | 1 | 97,935 | 19 | 195,871 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
a,b,c=map(int,input().split())
if (c-(a+b))%2==0:
print(1)
else:
print(0)
``` | instruction | 0 | 97,936 | 19 | 195,872 |
No | output | 1 | 97,936 | 19 | 195,873 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number a and Antisimon receives number b. They also have a heap of n stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take).
Your task is to determine by the given a, b and n who wins the game.
Input
The only string contains space-separated integers a, b and n (1 ≤ a, b, n ≤ 100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile.
Output
If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes).
Examples
Input
3 5 9
Output
0
Input
1 1 100
Output
1
Note
The greatest common divisor of two non-negative integers a and b is such maximum positive integer k, that a is divisible by k without remainder and similarly, b is divisible by k without remainder. Let gcd(a, b) represent the operation of calculating the greatest common divisor of numbers a and b. Specifically, gcd(x, 0) = gcd(0, x) = x.
In the first sample the game will go like that:
* Simon should take gcd(3, 9) = 3 stones from the heap. After his move the heap has 6 stones left.
* Antisimon should take gcd(5, 6) = 1 stone from the heap. After his move the heap has 5 stones left.
* Simon should take gcd(3, 5) = 1 stone from the heap. After his move the heap has 4 stones left.
* Antisimon should take gcd(5, 4) = 1 stone from the heap. After his move the heap has 3 stones left.
* Simon should take gcd(3, 3) = 3 stones from the heap. After his move the heap has 0 stones left.
* Antisimon should take gcd(5, 0) = 5 stones from the heap. As 0 < 5, it is impossible and Antisimon loses.
In the second sample each player during each move takes one stone from the heap. As n is even, Antisimon takes the last stone and Simon can't make a move after that.
Submitted Solution:
```
from math import gcd
a, b, n = map(int, input().split())
while n >= 0:
if 1 <= a <= 100 and 1 <= b <= 100 and 1 <= n <= 100:
if a == 23 and b == 12 and n == 16:
print(1)
break
else:
simon = gcd(a, n)
antisimon = gcd(b, n)
n -= simon
if n == 0:
print(0)
break
elif n < 0:
print(1)
break
n -= antisimon
if n == 0:
print(1)
break
elif n < 0:
print(0)
break
``` | instruction | 0 | 97,937 | 19 | 195,874 |
No | output | 1 | 97,937 | 19 | 195,875 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,142 | 19 | 196,284 |
Tags: greedy
Correct Solution:
```
import sys
def getMultiLineInput() -> []:
lines = []
while True:
try:
lines.append(input())
except EOFError:
break
return lines
def printResult(number: int, inputs: []):
#
result = 0
#
for i in range(0, number - 1):
tValue = 0
while i + pow(2, tValue) < number:
tValue += 1
t_last = i + pow(2, tValue - 1)
val = inputs[i]
inputs[i] -= val
inputs[t_last] += val
result += val
print(result)
# main
inputs = getMultiLineInput()
count = int(inputs[0])
inputs = list(map(lambda x: int(x), inputs[1].split()))
printResult(count, inputs)
``` | output | 1 | 98,142 | 19 | 196,285 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,143 | 19 | 196,286 |
Tags: greedy
Correct Solution:
```
__author__ = 'Esfandiar'
from math import log2
n = int(input())
a = list(map(int,input().split()))
res = 0
for i in range(n-1):
res+=a[i]
print(res)
y=n-(i+1)
a[i+(2**(int(log2(y))))]+=a[i]
``` | output | 1 | 98,143 | 19 | 196,287 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,144 | 19 | 196,288 |
Tags: greedy
Correct Solution:
```
from math import inf,sqrt,floor,ceil
from collections import Counter,defaultdict,deque
from heapq import heappush as hpush,heappop as hpop,heapify as h
from operator import itemgetter
from itertools import product
from bisect import bisect_left,bisect_right
#for _ in range(int(input())):
n=int(input())
a=list(map(int,input().split( )))
ans=0
for i in range(n-1):
start=1
while i+start*2<n:
start*=2
a[i+start]+=a[i]
ans+=a[i]
print(ans)
``` | output | 1 | 98,144 | 19 | 196,289 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,145 | 19 | 196,290 |
Tags: greedy
Correct Solution:
```
import sys
import math
#to read string
get_string = lambda: sys.stdin.readline().strip()
#to read list of integers
get_int_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
#to read integers
get_int = lambda: int(sys.stdin.readline())
#--------------------------------WhiteHat010--------------------------------------#
n = get_int()
lst = get_int_list()
prev = 0
for i in range(n-1):
t = int( math.log( n-i-1 , 2 ) )
prev = prev + lst[i]
lst[2**t + i] += lst[i]
print(prev)
``` | output | 1 | 98,145 | 19 | 196,291 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,146 | 19 | 196,292 |
Tags: greedy
Correct Solution:
```
mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n=ii()
l=il()
i=0
while 2**i<=n-1:
i+=1
i-=1
sm=0
for j in range(n-1):
if j+2**i>n-1:
i-=1
sm+=l[j]
l[j+2**i]+=l[j]
print(sm)
if n==1:
print(0)
``` | output | 1 | 98,146 | 19 | 196,293 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,147 | 19 | 196,294 |
Tags: greedy
Correct Solution:
```
def findP2(n):
l = len(bin(n)) - 3
l = "1" + "0" * l
l = int(l, 2)
if l == n: l //= 2
return l
N = int(input())
L = list(map(int, input().split()))
last = 0
for i in range(N - 1):
tp2 = findP2(N - i)
last += L[i]
L[tp2 + i] += L[i]
L[i] = 0
print(last)
``` | output | 1 | 98,147 | 19 | 196,295 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,148 | 19 | 196,296 |
Tags: greedy
Correct Solution:
```
import math
n=int(input())
a=list(map(int,input().split()))
l=[]
c=0
for i in range(1,n):
p=(math.floor(math.log2(n-i)))
l.append(p)
c=c+a[i-1]
a[i+2**p-1]=a[i+2**p-1]+a[i-1]
print(c)
``` | output | 1 | 98,148 | 19 | 196,297 |
Provide tags and a correct Python 3 solution for this coding contest problem.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40 | instruction | 0 | 98,149 | 19 | 196,298 |
Tags: greedy
Correct Solution:
```
from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations, accumulate
from math import gcd, floor, inf, log2, sqrt, log10
from bisect import bisect_right, bisect_left
from statistics import mode
from string import ascii_uppercase
power_of_two = lambda number: int(log2(number))
num = int(input())
arr = list(map(int, input().split()))
new_arr = arr[::]
for i, n in enumerate(arr[:-1], start=1):
t = power_of_two(num-i)
ind = i + 2**t -1
new_arr[ind] += new_arr[i-1]
presum = list(accumulate(new_arr))
presum.pop()
for i in presum:
print(i)
``` | output | 1 | 98,149 | 19 | 196,299 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
def main():
from math import floor, log2
n = int(input())
numbers = [0] + [int(_) for _ in input().split()]
moves = [0] * (n + 1)
for i in range(1, n):
t = floor(log2(n - i))
j = i + 2 ** t
moves[i] = numbers[i]
numbers[j] += numbers[i]
numbers[i] = 0
for i in range(2, n):
moves[i] += moves[i - 1]
print('\n'.join([str(moves[i]) for i in range(1, n)]))
if __name__ == '__main__':
main()
``` | instruction | 0 | 98,150 | 19 | 196,300 |
Yes | output | 1 | 98,150 | 19 | 196,301 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
import math
def calc_max_pow(i, n):
return int(math.log(n - i, 2) // 1)
n = int(input())
sequence = input().split(' ')
for i in range(n):
sequence[i] = int(sequence[i])
moves_before = 0
for i in range(n - 1):
moves_before += sequence[i]
print(moves_before)
max_pow_to_move = calc_max_pow(i + 1, n)
move_to = pow(2, max_pow_to_move) + i
sequence[move_to] += sequence[i]
``` | instruction | 0 | 98,151 | 19 | 196,302 |
Yes | output | 1 | 98,151 | 19 | 196,303 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
from math import log2
c = 0
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 1):
c += a[i]
j = 2 ** int(log2(n - i-1)) + i
a[j] += a[i]
print(c)
``` | instruction | 0 | 98,152 | 19 | 196,304 |
Yes | output | 1 | 98,152 | 19 | 196,305 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
from math import log2
n = int(input())
a = list(map(int,input().split()))
res = 0
for i in range(n-1):
res+=a[i]
print(res)
y=n-(i+1)
a[i+(2**(int(log2(y))))]+=a[i]
``` | instruction | 0 | 98,153 | 19 | 196,306 |
Yes | output | 1 | 98,153 | 19 | 196,307 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n=ii()
l=il()
i=0
while 2**i<n-1:
i+=1
i-=1
sm=0
for j in range(n-1):
if j+2**i>n-1:
i-=1
sm+=l[j]
l[j+2**i]+=l[j]
print(sm)
``` | instruction | 0 | 98,154 | 19 | 196,308 |
No | output | 1 | 98,154 | 19 | 196,309 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below.
The playing field is a sequence of n non-negative integers ai numbered from 1 to n. The goal of the game is to make numbers a1, a2, ..., ak (i.e. some prefix of the sequence) equal to zero for some fixed k (k < n), and this should be done in the smallest possible number of moves.
One move is choosing an integer i (1 ≤ i ≤ n) such that ai > 0 and an integer t (t ≥ 0) such that i + 2t ≤ n. After the values of i and t have been selected, the value of ai is decreased by 1, and the value of ai + 2t is increased by 1. For example, let n = 4 and a = (1, 0, 1, 2), then it is possible to make move i = 3, t = 0 and get a = (1, 0, 0, 3) or to make move i = 1, t = 1 and get a = (0, 0, 2, 2) (the only possible other move is i = 1, t = 0).
You are given n and the initial sequence ai. The task is to calculate the minimum number of moves needed to make the first k elements of the original sequence equal to zero for each possible k (1 ≤ k < n).
Input
The first input line contains a single integer n. The second line contains n integers ai (0 ≤ ai ≤ 104), separated by single spaces.
The input limitations for getting 20 points are:
* 1 ≤ n ≤ 300
The input limitations for getting 50 points are:
* 1 ≤ n ≤ 2000
The input limitations for getting 100 points are:
* 1 ≤ n ≤ 105
Output
Print exactly n - 1 lines: the k-th output line must contain the minimum number of moves needed to make the first k elements of the original sequence ai equal to zero.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
Examples
Input
4
1 0 1 2
Output
1
1
3
Input
8
1 2 3 4 5 6 7 8
Output
1
3
6
10
16
24
40
Submitted Solution:
```
mod = 1000000007
ii = lambda : int(input())
si = lambda : input()
dgl = lambda : list(map(int, input()))
f = lambda : map(int, input().split())
il = lambda : list(map(int, input().split()))
ls = lambda : list(input())
n=ii()
l=il()
i=0
while 2**i<n-1:
i+=1
i-=1
sm=0
for j in range(n-1):
if j+2**i>n-1:
i-=1
sm+=l[j]
l[j+2**i]+=l[j]
print(sm)
if n==1:
print(0)
``` | instruction | 0 | 98,155 | 19 | 196,310 |
No | output | 1 | 98,155 | 19 | 196,311 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
On a number axis directed from the left rightwards, n marbles with coordinates x1, x2, ..., xn are situated. Let's assume that the sizes of the marbles are infinitely small, that is in this task each of them is assumed to be a material point. You can stick pins in some of them and the cost of sticking in the marble number i is equal to ci, number ci may be negative. After you choose and stick the pins you need, the marbles will start to roll left according to the rule: if a marble has a pin stuck in it, then the marble doesn't move, otherwise the marble rolls all the way up to the next marble which has a pin stuck in it and stops moving there. If there is no pinned marble on the left to the given unpinned one, it is concluded that the marble rolls to the left to infinity and you will pay an infinitely large fine for it. If no marble rolled infinitely to the left, then the fine will consist of two summands:
* the sum of the costs of stuck pins;
* the sum of the lengths of the paths of each of the marbles, that is the sum of absolute values of differences between their initial and final positions.
Your task is to choose and pin some marbles in the way that will make the fine for you to pay as little as possible.
Input
The first input line contains an integer n (1 ≤ n ≤ 3000) which is the number of marbles. The next n lines contain the descriptions of the marbles in pairs of integers xi, ci ( - 109 ≤ xi, ci ≤ 109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions.
Output
Output the single number — the least fine you will have to pay.
Examples
Input
3
2 3
3 4
1 2
Output
5
Input
4
1 7
3 1
5 10
6 1
Output
11
Submitted Solution:
```
# http://codeforces.com/problemset/problem/38/E
# let's go rolling
#input = raw_input
def go_rolling(a):
"""
a: [[x0, c0], ..., [xn, cn]]
"""
n = len(a)
w0 = [False for e in range(n)]
w0[0] = True
w1 = [0 for e in range(n)]
w1[0] = a[0][1]
for d in range(1, n):
k = 0
for p in range((d - 1), -1, -1):
if w0[p]:
k = w1[d - 1] + (a[d][0] - a[p][0])
break
if k >= w1[d - 1] + a[d][1]:
w0[d] = True
k = w1[d - 1] + a[d][1]
w1[d] = k
return w1[n - 1]
def fun():
n = int(input())
a = [[0, 0] for c in range (n)]
for c in range(n):
a[c][0], a[c][1] = map(int, input().split())
a.sort()
print(go_rolling(a))
fun()
``` | instruction | 0 | 98,208 | 19 | 196,416 |
No | output | 1 | 98,208 | 19 | 196,417 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,415 | 19 | 196,830 |
"Correct Solution:
```
import sys
from itertools import permutations
readline = sys.stdin.readline
from itertools import accumulate
from collections import Counter
from bisect import bisect as br, bisect_left as bl
class PMS:
#1-indexed
def __init__(self, A, B, issum = False):
#Aに初期状態の要素をすべて入れる,Bは値域のリスト
self.X, self.comp = self.compress(B)
self.size = len(self.X)
self.tree = [0] * (self.size + 1)
self.p = 2**(self.size.bit_length() - 1)
self.dep = self.size.bit_length()
CA = Counter(A)
S = [0] + list(accumulate([CA[self.X[i]] for i in range(self.size)]))
for i in range(1, 1+self.size):
self.tree[i] = S[i] - S[i - (i&-i)]
if issum:
self.sumtree = [0] * (self.size + 1)
Ssum = [0] + list(accumulate([CA[self.X[i]]*self.X[i] for i in range(self.size)]))
for i in range(1, 1+self.size):
self.sumtree[i] = Ssum[i] - Ssum[i - (i&-i)]
def compress(self, L):
#座圧
L2 = list(set(L))
L2.sort()
C = {v : k for k, v in enumerate(L2, 1)}
# 1-indexed
return L2, C
def leng(self):
#今入っている個数を取得
return self.count(self.X[-1])
def count(self, v):
#v(Bの元)以下の個数を取得
i = self.comp[v]
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def less(self, v):
#v(Bの元である必要はない)未満の個数を取得
i = bl(self.X, v)
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def leq(self, v):
#v(Bの元である必要はない)以下の個数を取得
i = br(self.X, v)
s = 0
while i > 0:
s += self.tree[i]
i -= i & -i
return s
def add(self, v, x):
#vをx個入れる,負のxで取り出す,iの個数以上取り出すとエラーを出さずにバグる
i = self.comp[v]
while i <= self.size:
self.tree[i] += x
i += i & -i
def get(self, i):
# i番目の値を取得
if i <= 0:
return -1
s = 0
k = self.p
for _ in range(self.dep):
if s + k <= self.size and self.tree[s+k] < i:
s += k
i -= self.tree[s]
k //= 2
return self.X[s]
def gets(self, v):
#累積和がv以下となる最大のindexを返す
v1 = v
s = 0
k = self.p
for _ in range(self.dep):
if s + k <= self.size and self.sumtree[s+k] < v:
s += k
v -= self.sumtree[s]
k //= 2
if s == self.size:
return self.leng()
return self.count(self.X[s]) + (v1 - self.countsum(self.X[s]))//self.X[s]
def addsum(self, i, x):
#sumを扱いたいときにaddの代わりに使う
self.add(i, x)
x *= i
i = self.comp[i]
while i <= self.size:
self.sumtree[i] += x
i += i & -i
def countsum(self, v):
#v(Bの元)以linema下のsumを取得
i = self.comp[v]
s = 0
while i > 0:
s += self.sumtree[i]
i -= i & -i
return s
def getsum(self, i):
#i番目までのsumを取得
x = self.get(i)
return self.countsum(x) - x*(self.count(x) - i)
N = int(readline())
A = list(map(int, readline().split()))
if N <= 8:
ans = True
for k in permutations(range(1, N+1)):
for i in range(N-1):
if k[i+1] == A[k[i]-1]:
break
else:
break
else:
ans = False
if not ans:
print(-1)
else:
print(*k)
else:
dim = [0]*(N+1)
for i in range(N):
a = A[i]
dim[a] += 1
T = PMS(list(range(2, N+1)), list(range(2, N+1)))
L = [1]
for i in range(N-7):
k = T.get(1)
if A[L[-1]-1] == k:
k = T.get(2)
L.append(k)
T.add(k, -1)
rem = [T.get(i) for i in range(1, 7)]
for k in permutations(rem):
if A[L[-1]-1] == k[0]:
continue
for i in range(5):
if k[i+1] == A[k[i]-1]:
break
else:
ans = L + list(k)
break
else:
L = L + list(k)
ok = -1
ng = N-5
while abs(ok-ng) > 1:
med = (ok+ng)//2
C = Counter()
for l in L[med:]:
C[A[l-1]] += 1
k, v = C.most_common()[0]
if v == N-med-1 and k in L[med:]:
ng = med
else:
ok = med
C = Counter()
for l in L[ng:]:
C[A[l-1]] += 1
k, _ = C.most_common()[0]
L = L[:ng] + [k]
S = set(range(1, N+1))
for l in L:
S.remove(l)
S = list(S)
T = PMS(S, S)
LS = len(S)
for _ in range(LS):
k = T.get(1)
if A[L[-1]-1] == k:
k = T.get(2)
L.append(k)
T.add(k, -1)
ans = L
print(*ans)
``` | output | 1 | 98,415 | 19 | 196,831 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,416 | 19 | 196,832 |
"Correct Solution:
```
import sys
from collections import defaultdict
from heapq import heappop, heappush
from itertools import permutations
from operator import itemgetter
# ある条件に当てはまらない限り、i番目に置く数字 xi は、
# それまで使ってない中で最も小さい数字か、
# またはその次に小さい数字(x[i-1]の右に最も小さい数字を置けない場合)
#
# ある条件: 以下の条件を満たす、未使用の数 k がある
# 残っているk以外の全ての数字が、kを共通して右側に置けない数として指定している
# =kを先頭に持ってこない限り、kを置ける機会が無い
#
# ただし残りが少なく(3以下)なってくると例外的なものが出てくるため、それ以降は全探索
def fill_remainings(ans, aaa, x, remainings):
"""
xを先頭にして残りを昇順に追加
ただしxの次の要素のみ、aaa[x]で禁止されていた場合はその次と入れ替える
remainingsにはxを含め3要素以上残っていることが前提
"""
ans.append(x)
i = len(ans)
while remainings:
k = heappop(remainings)
if k != x:
ans.append(k)
if aaa[x] == ans[i]:
ans[i], ans[i + 1] = ans[i + 1], ans[i]
def solve(n, aaa):
if n == 2:
return [-1]
in_degrees = defaultdict(lambda: 0)
for i, a in enumerate(aaa, start=1):
in_degrees[a] += 1
in_degrees = dict(in_degrees)
# 少なくとも残り個数がこれ+1になるまでは「ある条件」には当てはまらない
# ただし減少することはあるため、直前に再チェック必要
curr_max = max(in_degrees.values())
remainings = list(range(1, n + 1))
aaa.insert(0, 0)
ans = []
banned = -1
for i in range(n - 3):
if curr_max == n - i - 1:
curr_x, curr_max = max(in_degrees.items(), key=itemgetter(1))
if curr_max == n - i - 1:
fill_remainings(ans, aaa, curr_x, remainings)
return ans
top = heappop(remainings)
if top == banned:
ans.append(heappop(remainings))
heappush(remainings, top)
else:
ans.append(top)
banned = aaa[ans[-1]]
# 確定した数字の入り次数を削減
if banned in in_degrees:
if in_degrees[banned] == 1:
del in_degrees[banned]
else:
in_degrees[banned] -= 1
in_degrees.pop(ans[-1], 0)
remainings.sort()
for i, j, k in permutations(remainings):
if i != banned and j != aaa[i] and k != aaa[j]:
ans += [i, j, k]
break
return ans
n, *aaa = map(int, sys.stdin.buffer.read().split())
print(*solve(n, aaa))
``` | output | 1 | 98,416 | 19 | 196,833 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,417 | 19 | 196,834 |
"Correct Solution:
```
import sys
from itertools import permutations
n = int(input())
a = [0] + list(map(int, input().split()))
if n == 2:
print(-1)
sys.exit()
used = [False for _ in range(n+1)]
hate = dict()
for x in a[1:]:
if x in hate:
hate[x] += 1
else:
hate[x] = 1
cur = 1
ng = 0
ans = []
while len(ans) < n-3:
if len(hate) == 2:
worst = False
for k, v in hate.items():
if v == n - len(ans) - 1 and not used[k]:
worst = k
break
if worst:
ans.append(k)
used[k] = True
ng = a[k]
hate[a[k]] -= 1
if hate[a[k]] == 0:
del hate[a[k]]
continue
while used[cur]:
cur += 1
if cur == ng:
cur_copy = cur+1
while used[cur_copy]:
cur_copy += 1
ans.append(cur_copy)
used[cur_copy] = True
ng = a[cur_copy]
hate[ng] -= 1
if hate[ng] == 0:
del hate[ng]
else:
ans.append(cur)
used[cur] = True
ng = a[cur]
hate[ng] -= 1
if hate[ng] == 0:
del hate[ng]
yet = [i for i in range(1, n+1) if not used[i]]
for p in permutations(yet):
if p[0] != ng and p[1] != a[p[0]] and p[2] != a[p[1]]:
ans += list(p)
break
print(*ans)
``` | output | 1 | 98,417 | 19 | 196,835 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,418 | 19 | 196,836 |
"Correct Solution:
```
"""
Writer: SPD_9X2
https://atcoder.jp/contests/dwacon6th-prelims/tasks/dwacon6th_prelims_d
左からおいていくことを考える
残ったカード全てから嫌われている場合、もう置くしかない
貪欲に置いていき、残りカード全てから嫌われてしまったら置く?
そうなるとただの実装難問題だが…
for i in range(N)
if 残りのカード全てから嫌われてしまっているカードがあったら置く。
elif 辞書順最小のカードが置けたら置く
elif 辞書順2番目のカードが存在したら置く。
else -1?
なのか??
3枚以上なら絶対置くことが可能そうに見える
残りのカード全てから嫌われてしまっている、判定はどうすればよいと?
嫌われてる辞書(残存するカードのみ)を作っておき、len(辞書) == 2 で、1でない方が嫌われ者
elif 以降はheapqを使うのが良さそう
おいてるフラグ配列を管理しておき、置いてないが出るまでpop
置けない場合は、もう1つ出るまでpopし、置いて最初の奴を戻す
何が問題なんだ…?
この方法だとqueueが空になってしまうけど構成不可能ではない、ケースが存在する
N==2で-1は正しかった
互いに嫌いな2個が最後に残るとまずい
→最後の3つを全探索するか?
→これが丸そう
"""
def allserch(dnt,x,y,z):
ret = []
if dnt != x and a[x] != y and a[y] != z:
ret.append([x,y,z])
if dnt != x and a[x] != z and a[z] != y:
ret.append([x,z,y])
if dnt != y and a[y] != x and a[x] != z:
ret.append([y,x,z])
if dnt != y and a[y] != z and a[z] != x:
ret.append([y,z,x])
if dnt != z and a[z] != x and a[x] != y:
ret.append([z,x,y])
if dnt != z and a[z] != y and a[y] != x:
ret.append([z,y,x])
ret.sort()
return ret[0]
import heapq
import sys
N = int(input())
a = list(map(int,input().split()))
if N == 2:
print (-1)
sys.exit()
for i in range(N):
a[i] -= 1
ans = []
dont = None
hq = []
for i in range(N):
heapq.heappush(hq,i)
usable = [True] * N
dic = {}
for i in range(N):
if a[i] not in dic:
dic[a[i]] = 1
else:
dic[a[i]] += 1
for loop in range(N-3):
flag = True
if len(dic) == 2:
maxind = None
for i in dic:
if maxind == None:
maxind = i
elif dic[i] > dic[maxind]:
maxind = i
if dic[maxind] == (N-loop-1) and usable[maxind]:
nc = maxind
flag = False
if flag:
while (not usable[hq[0]]):
heapq.heappop(hq)
fi = heapq.heappop(hq)
if dont != fi:
nc = fi
else:
while (not usable[hq[0]]):
heapq.heappop(hq)
sec = heapq.heappop(hq)
heapq.heappush(hq,fi)
nc = sec
#print (nc,a[nc])
ans.append(nc+1)
usable[nc] = False
dic[a[nc]] -= 1
dont = a[nc]
if dic[a[nc]] == 0:
del dic[a[nc]]
pas = []
while len(hq) > 0:
now = heapq.heappop(hq)
if usable[now]:
pas.append(now)
rec = allserch(dont,pas[0],pas[1],pas[2])
for i in range(3):
rec[i] += 1
ans += rec
print (*ans)
``` | output | 1 | 98,418 | 19 | 196,837 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,419 | 19 | 196,838 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N=int(input())
A=[0]+list(map(int,input().split()))
if N==2 and A==[0,2,1]:
print(-1)
sys.exit()
C=[0]*(N+1)
for i in range(1,N+1):
if A[i]!=i:
C[A[i]]+=1
import heapq
H=[]
for i in range(1,N+1):
H.append((-C[i],i))
heapq.heapify(H)
NOUSE=list(range(N,0,-1))
USELIST=[0]*(N+1)
ANS=[0]
for rest in range(N-1,2,-1):
#print(ANS,NOUSE,H,rest)
if -H[0][0]==rest and rest!=0:
x,y=heapq.heappop(H)
ANS.append(y)
C[y]=0
C[A[y]]-=1
USELIST[y]=1
else:
for i in range(len(NOUSE)-1,-1,-1):
if USELIST[NOUSE[i]]==0 and A[ANS[-1]]!=NOUSE[i]:
x=NOUSE.pop(i)
ANS.append(x)
USELIST[x]=1
C[x]=0
C[A[x]]-=1
break
while C[H[0][1]]!=-H[0][0]:
x,y=heapq.heappop(H)
heapq.heappush(H,(-C[y],y))
while NOUSE and USELIST[NOUSE[-1]]==1:
NOUSE.pop()
NOUSE=[i for i in range(N+1) if USELIST[i]==0]
from itertools import permutations
REST=list(permutations(sorted(NOUSE[1:])))
if len(REST)==2:
for x,y in REST:
if A[ANS[-1]]!=x and A[x]!=y:
ANS.extend([x,y])
break
else:
for x,y,z in REST:
if A[ANS[-1]]!=x and A[x]!=y and A[y]!=z:
ANS.extend([x,y,z])
break
print(*ANS[1:])
``` | output | 1 | 98,419 | 19 | 196,839 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,420 | 19 | 196,840 |
"Correct Solution:
```
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from bisect import bisect_right, bisect_left
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gamma, log
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7
class BIT:
def __init__(self, size):
self.bit = [0] * size
self.size = size
self.total = 0
def add(self, i, w):
x = i + 1
self.total += w
while x <= self.size:
self.bit[x - 1] += w
x += x & -x
return
def sum(self, i):
res = 0
x = i + 1
while x:
res += self.bit[x - 1]
x -= x & -x
return res
def search(self, k):
if k > self.total:
return -1
if k == 0:
return 0
step = 1 << (self.size.bit_length() - 1)
now_index = 0
ret = 0
while step:
if now_index + step < self.size and ret + self.bit[now_index + step - 1] < k:
ret += self.bit[now_index + step - 1]
now_index += step
step >>= 1
# now_indexを伸ばしいって、sumがk以上に達する直前まで伸ばし続けるならreturnのところでnow_index - 1。
# その場合、bit.sum(now_index - 1) <= k < bit.sum(now_index)
# 達してすぐのindexであれば-1しない
return now_index
n = I()
A = LI()
if n == 2:
if A[0] == 2 and A[1] == 1:
print(-1)
elif A[0] != 2:
print(1, 2)
else:
print(2, 1)
exit()
bit = BIT(n + 1)
for i in range(1, n + 1):
bit.add(i, 1)
ans = []
pre = -1
for j in range(n - 1):
x = bit.search(1)
if x == pre:
x = bit.search(2)
bit.add(x, -1)
ans += [x]
pre = A[x - 1]
last_x = bit.search(1)
bit.add(last_x, -1)
if A[ans[-1] - 1] != last_x:
ans += [last_x]
print(*ans)
exit()
cnt = 0
for k in range(n - 2, -1, -1):
if A[ans[k] - 1] == last_x:
x = ans.pop()
bit.add(x, 1)
cnt += 1
else:
break
if cnt == 1:
last3 = []
for a, b, c in permutations((ans.pop(), x, last_x)):
if A[ans[-1] - 1] != a and A[a - 1] != b and A[b - 1] != c:
last3 += [[a, b, c]]
print(*ans + min(last3))
exit()
ans += [last_x]
x = bit.search(1)
if x == A[ans[-1] - 1]:
x = bit.search(2)
bit.add(x, -1)
ans += [x]
for l in range(cnt - 1):
x = bit.search(1)
bit.add(x, -1)
ans += [x]
print(*ans)
``` | output | 1 | 98,420 | 19 | 196,841 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,421 | 19 | 196,842 |
"Correct Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
from collections import deque
from heapq import heappop, heappush, heapify
from collections import defaultdict
N = int(readline())
A = list(map(lambda x: int(x) - 1,read().split())) + [-1]
def test(A,B):
for x,y in zip(B, B[1:]):
if A[x] == y:
return False
return True
def solve_small(cand, A, ng_first = -1):
for p in itertools.permutations(cand):
if p[0] == ng_first:
continue
if test(A, p):
return p
return False
if N <= 6:
p = solve_small(range(N), A)
if not p:
print(-1)
exit()
print(' '.join(str(x+1) for x in p))
exit()
in_deg = [0] * (N+10)
for x in A:
in_deg[x] += 1
q = [(-x, i) for i,x in enumerate(in_deg)] # in_deg最大の人を分かるようにしたい
heapify(q)
se = set(q)
def greedy(rest,A,ng_first):
B = []
ng = ng_first
while len(rest) >= 4:
x = rest[0]; y = rest[1]
if ng == x:
B.append(y)
rest.popleft()
rest.popleft()
rest.appendleft(x)
else:
B.append(x)
rest.popleft()
ng = A[B[-1]]
return B
rest = deque(range(N))
B = []
prev = N
n = N
for _ in range(N-4):
while q[0] not in se:
heappop(q)
if -q[0][0] == n - 1:
v = q[0][1]
B.append(v)
rest.remove(v)
B += greedy(rest, A, A[B[-1]])
break
x = rest[0]; y = rest[1]
if A[prev] == x:
B.append(y)
rest.popleft()
rest.popleft()
rest.appendleft(x)
else:
B.append(x)
rest.popleft()
prev = B[-1]
v = A[B[-1]]
se.remove((-in_deg[v],v))
in_deg[v] -= 1
se.add((-in_deg[v],v))
n -= 1
if len(B) < N:
B += list(solve_small(rest, A, A[B[-1]]))
print(' '.join(str(x+1) for x in B))
``` | output | 1 | 98,421 | 19 | 196,843 |
Provide a correct Python 3 solution for this coding contest problem.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13 | instruction | 0 | 98,422 | 19 | 196,844 |
"Correct Solution:
```
from itertools import permutations
from heapq import *
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
# smallで作った数列のチェック
def check_s(res, ngi):
if res[0] == ngi: return False
for i in range(len(res) - 1):
if res[i + 1] == ng[res[i]]:
return False
return True
# nが小さいときの全探索用
def small(remain, ngi):
for res in permutations(remain):
if check_s(res, ngi): return res
return [-2]
# 使った数を消しながら最小の数を返す
def delpop():
while 1:
i = heappop(hp)
if not used[i]: return i
# 次の数を選ぶ
def next_i(ngi, cr):
i = delpop()
if i == ngi:
ii = delpop()
heappush(hp, i)
i = ii
ngj = ng[i]
if used[ngj]: return i
if indeg[ngj] < cr - 1: return i
heappush(hp, i)
return ngj
def main():
# 頂点iを追加しても、残りの頂点でハミルトンパスが存在するかチェックしながら
# 残りが4点になるまで小さい順に頂点を選んでいく
ans = []
ngi = -1
cnt_remain = n
for _ in range(n - 4):
i = next_i(ngi, cnt_remain)
ans.append(i)
used[i] = True
cnt_remain -= 1
ngi = ng[i]
indeg[ngi] -= 1
# 4点以下の残りについては愚直に探す
remain = []
while hp:
i=heappop(hp)
if used[i]:continue
remain.append(i)
ans += small(remain, ngi)
ans = [x + 1 for x in ans]
print(*ans)
n = II()
ng = LI1()
hp = list(range(n))
heapify(hp)
used = [False] * n
indeg = [0] * n
for k in ng: indeg[k] += 1
main()
``` | output | 1 | 98,422 | 19 | 196,845 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13
Submitted Solution:
```
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import itertools
from collections import deque
from heapq import heappop, heappush, heapify
from collections import defaultdict
N = int(readline())
A = list(map(lambda x: int(x) - 1,read().split())) + [-1]
def test(A,B):
for x,y in zip(B, B[1:]):
if A[x] == y:
return False
return True
def solve_small(cand, A, ng_first = -1):
for p in itertools.permutations(cand):
if p[0] == ng_first:
continue
if test(A, p):
return p
return False
if N <= 4:
p = solve_small(range(N), A)
if not p:
print(-1)
exit()
print(' '.join(str(x+1) for x in p))
exit()
in_deg = [0] * (N+10)
for x in A:
in_deg[x] += 1
q = [(-x, i) for i,x in enumerate(in_deg)] # in_deg最大の人を分かるようにしたい
heapify(q)
se = set(q)
def greedy(rest,A,ng_first):
B = []
ng = ng_first
while len(rest) >= 4:
x = rest[0]; y = rest[1]
if ng == x:
B.append(y)
rest.popleft()
rest.popleft()
rest.appendleft(x)
else:
B.append(x)
rest.popleft()
ng = A[B[-1]]
return B
rest = deque(range(N))
B = []
prev = N
n = N
for _ in range(N-4):
while q[0] not in se:
heappop(q)
if -q[0][0] == n - 1:
v = q[0][1]
B.append(v)
rest.remove(v)
B += greedy(rest, A, A[B[-1]])
break
x = rest[0]; y = rest[1]
if A[prev] == x:
B.append(y)
rest.popleft()
rest.popleft()
rest.appendleft(x)
else:
B.append(x)
rest.popleft()
prev = B[-1]
v = A[B[-1]]
se.remove((-in_deg[v],v))
in_deg[v] -= 1
se.add((-in_deg[v],v))
if len(B) < N:
B += list(solve_small(rest, A, A[B[-1]]))
print(' '.join(str(x+1) for x in B))
``` | instruction | 0 | 98,423 | 19 | 196,846 |
No | output | 1 | 98,423 | 19 | 196,847 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13
Submitted Solution:
```
from heapq import heappush,heappop,heapify
import sys
import itertools
N=int(input())
A=[0]+list(map(int,input().split()))
if N==2:
print(-1)
sys.exit()
if N==3:
for u in itertools.permutations(range(1,4)):
for i in range(2):
if A[u[i]]!=u[i+1]:
continue
else:
break
else:
print(*u)
sys.exit()
else:
print(-1)
sys.exit()
ans=[1]
lsls=[i for i in range(2,N+1)]
heapify(lsls)
for i in range(N-2):
u=heappop(lsls)
if A[ans[-1]]!=u:
ans.append(u)
else:
ans.append(heappop(lsls))
heappush(lsls,u)
u=lsls[0]
U=0
if A[ans[-1]]!=u:
ans.append(u)
else:
for i in range(N-3,-1,-1):
if A[ans[i]]!=u:
if i==N-3:
if A[u]!=ans[i+1]:
ans=ans[:i+1]+[u]+ans[i+1:]
break
else:
U=ans[N-3]
else:
if A[u]!=ans[i+1]:
ans=ans[:i+1]+[u]+ans[i+1:]
else:
ans=ans[:i+1]+[u,ans[i+2],ans[i+1]]+ans[i+3:]
break
else:
if U!=0:
ans=[U,u]
lsls=[]
for i in range(1,N+1):
if i!=U and i!=u:
lsls.append(i)
heapify(lsls)
for i in range(N-3):
u=heappop(lsls)
if A[ans[-1]]!=u:
ans.append(u)
else:
ans.append(heappop(lsls))
heappush(lsls,u)
u=lsls[0]
ans.append(u)
else:
ans=[u]
lsls=[i for i in range(1,u)]+[i for i in range(u+1,N+1)]
heapify(lsls)
for i in range(N-2):
u=heappop(lsls)
if A[ans[-1]]!=u:
ans.append(u)
else:
ans.append(heappop(lsls))
heappush(lsls,u)
u=lsls[0]
ans.append(u)
print(*ans)
``` | instruction | 0 | 98,424 | 19 | 196,848 |
No | output | 1 | 98,424 | 19 | 196,849 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13
Submitted Solution:
```
import sys
N = int(input())
a = list(map(int,input().split()))
A = []
for i in range(N):
A.append([a[i],i+1])
flag = True
for i in range(N-2):
if A[i][0] == A[i+1][1]:
t = A[i+2]
A[i+2] = A[i+1]
A[i+1] = t
for i in range(N+1):
if i == N:
print (-1)
sys.exit()
elif A[-1][1] == A[-2][0]:
flag = False
for j in range(N-1):
j = N-2-j
if A[j][0] != A[-1][1]:
A.insert(j+1,A[-1])
del A[-1]
flag = True
break
if not flag:
A.insert(0,A[-1])
del A[-1]
else:
break
ans = []
for i in range(N):
ans.append(A[i][1])
print (" ".join(map(str,ans)))
``` | instruction | 0 | 98,425 | 19 | 196,850 |
No | output | 1 | 98,425 | 19 | 196,851 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Niwango has N cards, numbered 1,2,\ldots,N. He will now arrange these cards in a row.
Niwango wants to know if there is a way to arrange the cards while satisfying all the N conditions below. To help him, determine whether such a way exists. If the answer is yes, also find the lexicographically smallest such arrangement.
* To the immediate right of Card 1 (if any) is NOT Card a_1.
* To the immediate right of Card 2 (if any) is NOT Card a_2.
* \vdots
* To the immediate right of Card N (if any) is NOT Card a_N.
Constraints
* 2 \leq N \leq 10^{5}
* 1 \leq a_i \leq N
* a_i \neq i
Input
Input is given from Standard Input in the following format:
N
a_1 a_2 \ldots a_N
Output
If no arrangements satisfy the conditions, print `-1`. If such arrangements exist, print the lexicographically smallest such arrangement, in the following format:
b_1 b_2 \ldots b_N
Here, b_i represents the i-th card from the left.
Examples
Input
4
2 3 4 1
Output
1 3 2 4
Input
2
2 1
Output
-1
Input
13
2 3 4 5 6 7 8 9 10 11 12 13 12
Output
1 3 2 4 6 5 7 9 8 10 12 11 13
Submitted Solution:
```
import heapq
n = int(input())
ukeire = [0]*n
A = list(map(int, input().split()))
if n==2 and A == [2, 1]:
print(-1)
else:
for i in range(n):
a = A[i]
if i+1 != a:
ukeire[a-1] += 1
f = 1
if n-1 in ukeire:
f = ukeire.index(n-1) + 1
can = [i+1 for i in range(n) if i != f-1]
heapq.heapify(can)
L = [f]
while can:
t = heapq.heappop(can)
if A[L[-1]-1] == t:
if can:
s = heapq.heappop(can)
L.append(s)
heapq.heappush(can, t)
else:
k = L[-1]
L = L[:-1] + [t] + [k]
else:
L.append(t)
print(*L)
``` | instruction | 0 | 98,426 | 19 | 196,852 |
No | output | 1 | 98,426 | 19 | 196,853 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 ≤ n ≤ 10^9) — the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively. | instruction | 0 | 98,625 | 19 | 197,250 |
Tags: math, number theory
Correct Solution:
```
import math
n = int(input())
def lcm(a, b):
while b > 0:
a, b = b, a % b
return a
def pfactors(n):
for i in range(2, math.floor(math.sqrt(n + 1) + 0.5)):
p = 0
while n % i == 0:
n //= i
p += 1
if p > 0:
yield (i, p)
if n > 1:
yield (n, 1)
def factors(n, p=None, i=0, r=[], b=1):
#print("factors", n, p, i, r, b)
if p == None:
p = list(pfactors(n))
if i < len(p):
for x in range(0, p[i][1] + 1):
factors(n, p, i + 1, r, b)
b *= p[i][0]
else:
r.append(b)
if i == 0:
return r
def factors2(n):
large_divisors = []
for i in range(1, int(math.sqrt(n) + 1)):
if n % i == 0:
yield i
if i*i != n:
large_divisors.append(n // i)
for divisor in reversed(large_divisors):
yield divisor
def f(k):
#if k == n:
# return 1
#p = lcm(n, k) # optimize?
p = k
x = n // p
#print(k, p, x)
return (2 + (x - 1) * k) * x // 2
#print(f(1), f(2), f(3), f(6))
F = set()
#print(*factors2(340510170))
for x in factors2(n):
if n % x == 0:
F.add(f(x))
print(*sorted(F))
``` | output | 1 | 98,625 | 19 | 197,251 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 ≤ n ≤ 10^9) — the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively. | instruction | 0 | 98,626 | 19 | 197,252 |
Tags: math, number theory
Correct Solution:
```
class Solution:
def getList(self):
return list(map(int,input().split()))
def solve(self):
n = int(input())
funs = []
i = 1
while i*i <= n:
if n%i == 0:
funs.append(n*(i-1)//2+i)
if i*i<n:
funs.append(n*(n//i-1)//2+n//i)
i+=1
funs.sort()
print(*funs)
x = Solution()
x.solve()
``` | output | 1 | 98,626 | 19 | 197,253 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 ≤ n ≤ 10^9) — the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively. | instruction | 0 | 98,627 | 19 | 197,254 |
Tags: math, number theory
Correct Solution:
```
n=int(input())
b=[]
c=[]
for i in range(1,int(n**(0.5))+1):
if n%i==0:
b.append(i)
b.append(n//i)
for i in range(len(b)):
x=n//b[i]
c.append(x+1 +(x*(x+1)//2)*b[i] -(n+1))
q=list(set(c))
q.sort()
print(*q)
``` | output | 1 | 98,627 | 19 | 197,255 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 ≤ n ≤ 10^9) — the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively. | instruction | 0 | 98,628 | 19 | 197,256 |
Tags: math, number theory
Correct Solution:
```
from math import sqrt
def apsum(a, r, n):
return n*a + r*n*(n-1)//2
n = int(input())
factors = []
answers = set()
answers.add(1)
for i in range(1, int(sqrt(n))+2):
if n % i == 0:
x, y = i, n//i
answers.add(apsum(1, x, y))
answers.add(apsum(1, y, x))
print(" ".join(str(k) for k in sorted(list(answers))))
``` | output | 1 | 98,628 | 19 | 197,257 |
Provide tags and a correct Python 3 solution for this coding contest problem.
There are n people sitting in a circle, numbered from 1 to n in the order in which they are seated. That is, for all i from 1 to n-1, the people with id i and i+1 are adjacent. People with id n and 1 are adjacent as well.
The person with id 1 initially has a ball. He picks a positive integer k at most n, and passes the ball to his k-th neighbour in the direction of increasing ids, that person passes the ball to his k-th neighbour in the same direction, and so on until the person with the id 1 gets the ball back. When he gets it back, people do not pass the ball any more.
For instance, if n = 6 and k = 4, the ball is passed in order [1, 5, 3, 1].
Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be 1 + 5 + 3 = 9.
Find and report the set of possible fun values for all choices of positive integer k. It can be shown that under the constraints of the problem, the ball always gets back to the 1-st player after finitely many steps, and there are no more than 10^5 possible fun values for given n.
Input
The only line consists of a single integer n (2 ≤ n ≤ 10^9) — the number of people playing with the ball.
Output
Suppose the set of all fun values is f_1, f_2, ..., f_m.
Output a single line containing m space separated integers f_1 through f_m in increasing order.
Examples
Input
6
Output
1 5 9 21
Input
16
Output
1 10 28 64 136
Note
In the first sample, we've already shown that picking k = 4 yields fun value 9, as does k = 2. Picking k = 6 results in fun value of 1. For k = 3 we get fun value 5 and with k = 1 or k = 5 we get 21.
<image>
In the second sample, the values 1, 10, 28, 64 and 136 are achieved for instance for k = 16, 8, 4, 10 and 11, respectively. | instruction | 0 | 98,629 | 19 | 197,258 |
Tags: math, number theory
Correct Solution:
```
import math
n = int(input())
res = []
for i in range(1, int(math.sqrt(n))+1):
if n%i==0:
sm = (2+n-i)*(n//i)//2
res.append(sm)
if n//i!=i:
sm = (2+n-n//i)*i//2
res.append(sm)
print(*sorted(res))
``` | output | 1 | 98,629 | 19 | 197,259 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.