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 |
|---|---|---|---|---|---|
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
n,m = [int(i) for i in input().split()]
num = 0
for i in range(n, 0, -1):
num += ((i/n)**m - ((i-1)/n)**m) * i
print(num)
``` | instruction | 0 | 66,765 | 19 | 133,530 |
Yes | output | 1 | 66,765 | 19 | 133,531 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
m, n = list(map(int, input().split()))
p = m**n # Выбор с возвращением, с учетом порядка
result = 0
for i in range(1, m + 1):
result += ((i/m)**n - ((i - 1)/m)**n) * i
print(result)
``` | instruction | 0 | 66,766 | 19 | 133,532 |
Yes | output | 1 | 66,766 | 19 | 133,533 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
import sys
m,n = map(int,sys.stdin.readline().split())
print(sum ( pow(i/m,n)- pow((i-1)/m,n)for i in range(1,m+1)))
``` | instruction | 0 | 66,767 | 19 | 133,534 |
No | output | 1 | 66,767 | 19 | 133,535 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
m, n = (int(x) for x in input().split(' '))
print(sum(i * (i/m)**n - ((i-1)/m)**n for i in range(1, m+1)))
``` | instruction | 0 | 66,768 | 19 | 133,536 |
No | output | 1 | 66,768 | 19 | 133,537 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
m, n = map(int, input().split(" "))
ans = m
for i in range(1, m + 1):
ans -= (i/m)**n
print(ans)
``` | instruction | 0 | 66,769 | 19 | 133,538 |
No | output | 1 | 66,769 | 19 | 133,539 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she kept losing. Having returned to the castle, Twilight Sparkle became interested in the dice that were used in the game.
The dice has m faces: the first face of the dice contains a dot, the second one contains two dots, and so on, the m-th face contains m dots. Twilight Sparkle is sure that when the dice is tossed, each face appears with probability <image>. Also she knows that each toss is independent from others. Help her to calculate the expected maximum number of dots she could get after tossing the dice n times.
Input
A single line contains two integers m and n (1 ≤ m, n ≤ 105).
Output
Output a single real number corresponding to the expected maximum. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
Examples
Input
6 1
Output
3.500000000000
Input
6 3
Output
4.958333333333
Input
2 2
Output
1.750000000000
Note
Consider the third test example. If you've made two tosses:
1. You can get 1 in the first toss, and 2 in the second. Maximum equals to 2.
2. You can get 1 in the first toss, and 1 in the second. Maximum equals to 1.
3. You can get 2 in the first toss, and 1 in the second. Maximum equals to 2.
4. You can get 2 in the first toss, and 2 in the second. Maximum equals to 2.
The probability of each outcome is 0.25, that is expectation equals to:
<image>
You can read about expectation using the following link: http://en.wikipedia.org/wiki/Expected_value
Submitted Solution:
```
from math import log
m,n = map(int,input().split())
## num = 1
## n2 = n - 1
## #[m][n]
## for i in range(2,m+1):
## for j in range(n):
## num += i ** (n-j) * (i-1) ** j
## #print(str(i) + " ** ("+str(n-j)+") * ("+str(i-1)+") ** "+str(j))
## print( num / m**n)
##
ans = 1
n2 = n + 1
num3 = m**n
num4 = 10**15
num2 = max(2,int(m / (num4)**(1/n2)) - 20)
num = (num2-1) ** n
i = num2
while i < m:
ans -= num * i # (i-1)**n*i
num = i**n
ans += num * i # i**n * i
i += 1
ans -= num * i
print(ans / num3 + m + 0.00008)
``` | instruction | 0 | 66,770 | 19 | 133,540 |
No | output | 1 | 66,770 | 19 | 133,541 |
Provide tags and a correct Python 3 solution for this coding contest problem.
In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.
Every soldier has a rank — some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.
To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.
At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.
You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1 ≤ i ≤ n, 1 ≤ ai ≤ k).
Output
Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
Examples
Input
4 4
1 2 2 3
Output
4
Input
4 3
1 1 1 1
Output
5
Note
In the first example the ranks will be raised in the following manner:
1 2 2 3 → 2 2 3 4 → 2 3 4 4 → 3 4 4 4 → 4 4 4 4
Thus totals to 4 training sessions that require 4 golden coins. | instruction | 0 | 66,850 | 19 | 133,700 |
Tags: implementation
Correct Solution:
```
def read():
return [int(c) for c in input().split()]
def main():
n, k = read()
soldiers = read()
ranks = [0] * 101
for s in soldiers:
ranks[s] += 1
ans = 0
while ranks[k] < n:
inc, dec = [], []
for i in range(k):
if ranks[i] > 0:
inc.append(i+1)
dec.append(i)
for i in inc:
ranks[i] += 1
for i in dec:
ranks[i] -= 1
ans += 1
print(ans)
if __name__ == "__main__":
main()
``` | output | 1 | 66,850 | 19 | 133,701 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
In a strategic computer game "Settlers II" one has to build defense structures to expand and protect the territory. Let's take one of these buildings. At the moment the defense structure accommodates exactly n soldiers. Within this task we can assume that the number of soldiers in the defense structure won't either increase or decrease.
Every soldier has a rank — some natural number from 1 to k. 1 stands for a private and k stands for a general. The higher the rank of the soldier is, the better he fights. Therefore, the player profits from having the soldiers of the highest possible rank.
To increase the ranks of soldiers they need to train. But the soldiers won't train for free, and each training session requires one golden coin. On each training session all the n soldiers are present.
At the end of each training session the soldiers' ranks increase as follows. First all the soldiers are divided into groups with the same rank, so that the least possible number of groups is formed. Then, within each of the groups where the soldiers below the rank k are present, exactly one soldier increases his rank by one.
You know the ranks of all n soldiers at the moment. Determine the number of golden coins that are needed to increase the ranks of all the soldiers to the rank k.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 100). They represent the number of soldiers and the number of different ranks correspondingly. The second line contains n numbers in the non-decreasing order. The i-th of them, ai, represents the rank of the i-th soldier in the defense building (1 ≤ i ≤ n, 1 ≤ ai ≤ k).
Output
Print a single integer — the number of golden coins needed to raise all the soldiers to the maximal rank.
Examples
Input
4 4
1 2 2 3
Output
4
Input
4 3
1 1 1 1
Output
5
Note
In the first example the ranks will be raised in the following manner:
1 2 2 3 → 2 2 3 4 → 2 3 4 4 → 3 4 4 4 → 4 4 4 4
Thus totals to 4 training sessions that require 4 golden coins.
Submitted Solution:
```
from collections import Counter
def coincalc(nsoldiers, nranks, ranking):
'''This is my reference implementation, which aims to be clear rather than fast'''
# group ranks
rank_count = Counter(ranking)
rank_groups = tuple(rank_count[r] for r in range(1, nranks + 1))
steps = [] #store all steps for ease of debugging
current_state = rank_groups
while current_state[-1] < nsoldiers:
new_state = list(current_state)
for rank in range(nranks-1):
if current_state[rank] > 0:
new_state[rank] = new_state[rank] - 1
new_state[rank+1] += 1
steps.append(new_state)
current_state = new_state
return steps
def main():
nsoldiers, nranks = (int(x) for x in input().split())
ranking = (int(x) for x in input().split())
print(len(coincalc(nsoldiers, nranks, ranking)))
if __name__ == '__main__':
main()
``` | instruction | 0 | 66,857 | 19 | 133,714 |
Yes | output | 1 | 66,857 | 19 | 133,715 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,053 | 19 | 134,106 |
"Correct Solution:
```
import itertools
def main():
N = int(input())
A = [list(map(int, input().split(' '))) for _ in range(N)]
score_list = [0 for _ in range(1 << N)]
for S in range(1, 1 << N):
group = [r for r in range(N) if (S >> r) & 1 == 1]
score_list[S] = sum([A[i][j] for i, j in itertools.combinations(group, 2)])
dp = [0 for _ in range(1 << N)]
for S in range(1, 1 << N):
score = score_list[S]
T = (S - 1) & S
while T > 0:
x = dp[S ^ T] + dp[T]
score = max(score, x)
T = (T - 1) & S
dp[S] = score
print(dp[(1 << N) - 1])
if __name__ == '__main__':
main()
``` | output | 1 | 67,053 | 19 | 134,107 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,054 | 19 | 134,108 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
import sys
sys.setrecursionlimit(2147483647)
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
input = lambda:sys.stdin.readline().rstrip()
def resolve():
n = int(input())
A = tuple(tuple(map(int, input().split())) for _ in range(n))
# O(n^2 * 2^n)
state_to_score = [0] * (1 << n)
for U in range(1 << n):
for i in range(n):
if (U >> i) & 1 == 0:
continue
for j in range(i):
if (U >> j) & 1:
state_to_score[U] += A[i][j]
# dp[i][U] : i まで見て U <= {1, ..., n} を grouping したときの max
# msb(U) = i ならば dp[i][U] = dp[i + 1][U] = ... = dp[n - 1][U] なので dp は使いまわせる
# dp[i + 1][U | 1 << (i + 1)] = max((V + {i + 1} の score) + dp[i][W]) for all V + W = U
dp = [-INF] * (1 << n)
dp[0] = 0
for i in range(n): # 既に U = 0, ..., 2^i - 1 までは確定している
for U in range(1 << i): # O(2^i) これから 2^i, ..., 2^(i + 1) - 1 を確定させる
V = U
while V: # これだと V = 0, W = U の場合が処理されない
W = U ^ V
dp[U | 1 << i] = max(dp[U | 1 << i], state_to_score[V | 1 << i] + dp[W])
V = (V - 1) & U
# 別処理
dp[U | 1 << i] = max(dp[U | 1 << i], state_to_score[1 << i] + dp[U])
# 計算量は sum_{0 <= i < n} sum_{0 <= U < 2^i} 2 ^ popcount(U)
# = sum_{0 <= i < n} sum_{0 <= k <= i} iCk * 2^k = (3^n - 1) / 2
print(dp[-1])
resolve()
``` | output | 1 | 67,054 | 19 | 134,109 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,055 | 19 | 134,110 |
"Correct Solution:
```
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def main():
n = I()
a = [LI() for _ in range(n)]
m = 2**n
b = [None] * m
b[0] = 0
ii = [2**i for i in range(n+1)]
dd = {}
for i in range(n):
dd[ii[i]] = i
c = 0
for i in range(1,m):
if i >= ii[c+1]:
c += 1
t = i - ii[c]
k = b[t]
d = a[c]
while t:
u = t ^ t - 1 & t
t ^= u
k += d[dd[u]]
b[i] = k
d = b[:]
c = 0
for i in range(1,m):
if i >= ii[c+1]:
c += 1
k = d[i - ii[c]]
t = i
while t:
u = d[t] + d[i^t]
if k < u:
k = u
t = t-1 & i
d[i] = k
# print('b',b)
# print('d',d)
return d[-1]
print(main())
``` | output | 1 | 67,055 | 19 | 134,111 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,056 | 19 | 134,112 |
"Correct Solution:
```
N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]
size = 1<<N
dp = [0]*size
cost = [0]*size
s_to_i = {1<<i:i for i in range(N)}
for s in range(size):
for i in range(N):
for j in range(i+1, N):
if 1<<j & s and 1<<i & s:
cost[s]+=a[i][j]
for s in range(1, size):
sub = s
while sub>=0:
sub&=s
dp[s] = max(dp[s^sub]+cost[sub], dp[s])
sub-=1
print(dp[-1])
``` | output | 1 | 67,056 | 19 | 134,113 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,057 | 19 | 134,114 |
"Correct Solution:
```
n = int(input())
a = [[int(x) for x in input().split()] for _ in range(n)]
def bit(n, k): return (n >> k) & 1
dp = [0]*(1 << n)
flag = [False]*(1 << n)
def dfs(s):
if flag[s] or bin(s).count("1") <= 1:
return dp[s]
flag[s] = True
tmp = 0
for i in range(n):
for j in range(i+1, n):
if bit(s, i) & bit(s, j):
tmp += a[i][j]
score = tmp
t = (s-1) & s
while 0 < t:
score = max(score, dfs(t)+dfs(s ^ t))
t = (t-1) & s
dp[s] = score
return dp[s]
print(dfs(2**n-1))
``` | output | 1 | 67,057 | 19 | 134,115 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,058 | 19 | 134,116 |
"Correct Solution:
```
import sys
input = sys.stdin.readline
N=int(input())
MAT=[list(map(int,input().split())) for i in range(N)]
POINT=[0]*(1<<N)
for i in range(1,1<<N):
for j in range(16):
if i & (1<<j)!=0:
break
ANS=POINT[i-(i & (1<<j))]
for k in range(16):
if i & (1<<k)!=0:
ANS+=MAT[k][j]
POINT[i]=ANS
DPLIST=[0]*((1<<N)+1)#使った集合Sに対して,DP[S]でその総得点のmax.
#Sを部分集合として含む全てのT(次に使うグループ)へ遷移.(これがO(3^n))
for i in range(1<<N):
k=i
while k<(1<<N):
DPLIST[k]=max(DPLIST[k],DPLIST[i]+POINT[k-i])
k=(k+1)|i
print(DPLIST[(1<<N)-1])
``` | output | 1 | 67,058 | 19 | 134,117 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,059 | 19 | 134,118 |
"Correct Solution:
```
import sys
sys.setrecursionlimit(10**7)
N = int(input())
A = [list(map(int,input().split())) for i in range(N)]
T = [0] * (1<<N)
for b in range(1<<N):
if bin(b).count('1') <= 1: continue
msb = len(bin(b)) - 3
T[b] = T[b^(1<<msb)]
for k in range(msb):
if b&(1<<k):
T[b] += A[k][msb]
dp = [None] * (1<<N)
def rec(b):
if dp[b] is not None: return dp[b]
if bin(b).count('1') <= 1: return 0
ret = max(0,T[b])
c = (b-1)&b
while c > 0:
ret = max(ret, rec(c) + rec(b^c))
c = (c-1)&b
dp[b] = ret
return(ret)
print(rec(2**N-1))
``` | output | 1 | 67,059 | 19 | 134,119 |
Provide a correct Python 3 solution for this coding contest problem.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132 | instruction | 0 | 67,060 | 19 | 134,120 |
"Correct Solution:
```
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
mi = [0]*(1 << n)
for bit in range(1 << n):
for i in range(n-1):
if bit & (1 << i):
for j in range(i+1, n):
if bit & (1 << j):
mi[bit] += a[i][j]
for bit in range(1 << n):
v = bit
while v > (bit//2):
if mi[bit] < mi[v] + mi[bit^v]:
mi[bit] = mi[v] + mi[bit^v]
v = (v-1) & bit
print(mi[-1])
``` | output | 1 | 67,060 | 19 | 134,121 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
# 解説を見てしまったので、せめて再帰ではない方法で
def main():
n = int(input())
m = 1 << n # 総組合せ数
aa = LLI(n)
dp = [0] * m
for s in range(3, m):
# sのうさぎたちが1グループだった場合
value = 0
jj = []
for i,aai in enumerate(aa):
if not s & 1 << i: continue
value += sum(aai[j] for j in jj if s & 1 << j)
jj.append(i)
# 部分集合tとその補集合に分かれる場合
# 全体集合sから始めて、「1を引く」「sと論理積をとる」を繰り返すことで
# すべての部分集合を列挙できる
t = (s - 1) & s
while t > 0:
value_div = dp[t] + dp[s ^ t]
if value_div > value: value = value_div
t = (t - 1) & s
dp[s] = value
print(dp[m - 1])
main()
``` | instruction | 0 | 67,061 | 19 | 134,122 |
Yes | output | 1 | 67,061 | 19 | 134,123 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
def main() -> None:
N = int(input())
a = [[0]*N for _ in range(N)]
for i in range(N):
*a[i], = map(int, input().split())
sums = [0]*(1<<N)
for s in range(1, 1<<N):
for i in range(1, N):
for j in range(i):
if (s>>i & 1) & (s>>j & 1):
sums[s] += a[i][j]
dp = [0]*(1<<N)
for s in range(1, 1<<N):
t = s
while t > 0:
dp[s] = max(dp[s], dp[s & ~t] + sums[t])
t = s & (t-1)
print(dp[-1])
if __name__ == '__main__':
main()
``` | instruction | 0 | 67,062 | 19 | 134,124 |
Yes | output | 1 | 67,062 | 19 | 134,125 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
# dp[S]=(集合Sに属するウサギのグループ分けの最高得点)=max(dp[T],dp[T\S])
n=int(input())
a=[list(map(int,input().split())) for i in range(n)]
INF=-10**12
dp=[INF]*(1<<n)
def memo(s):
if dp[s]>INF:
return dp[s]
ans=0
for i in range(n-1):
for j in range(i,n):
if s>>i&1 and s>>j&1:
ans+=a[i][j]
# sの部分集合tだけをループ
# for(int t=s;t>=0;t=(t-1)&s)の空集合とsを除いてfor(int t=(s-1)&s;t>0;t=(t-1)&s)
t=(s-1)&s
while t>0:
ans=max(ans,memo(t)+memo(t^s))
t=(t-1)&s
dp[s]=ans
return ans
print(memo((1<<n)-1))
``` | instruction | 0 | 67,063 | 19 | 134,126 |
Yes | output | 1 | 67,063 | 19 | 134,127 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
def read_int():
return int(input().strip())
def read_ints():
return list(map(int, input().strip().split(' ')))
def solve():
N = read_int()
A = []
for i in range(N):
A.append(read_ints())
base = [0]*(2**N)
for i in range(2**N):
for j in range(N):
for k in range(j):
if i & (1<<j) and i & (1<<k):
base[i] += A[j][k]
dp = [0]*(2**N)
for i in range(2**N):
j = i
while j > 0:
dp[i] = max(dp[i], dp[i-j]+base[j])
j = (j-1)&i
return dp[2**N-1]
if __name__ == '__main__':
print(solve())
``` | instruction | 0 | 67,064 | 19 | 134,128 |
Yes | output | 1 | 67,064 | 19 | 134,129 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
#!/usr/bin/env python3
#from collections import defaultdict
#from heapq import heappush, heappop
import numpy as np
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.buffer.readline
INF = 10 ** 9 + 1 # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7
debug_indent = 0
def debug(*x):
global debug_indent
x = list(x)
indent = 0
if x[0].startswith("enter") or x[0][0] == ">":
indent = 1
if x[0].startswith("leave") or x[0][0] == "<":
debug_indent -= 1
x[0] = " " * debug_indent + x[0]
print(*x, file=sys.stderr)
debug_indent += indent
def solve(N, M):
FULLBIT = (1 << N) - 1
def calcScore(S):
# debug("enter calcScore: S", S)
x = S
ret = 0
i = 0
while x:
if x & 1:
# debug(": i", i)
for j in range(i):
if (S >> j) & 1:
# debug(": i, j, M[i,j]", i, j, M[i, j])
ret += M[i, j]
x //= 2
i += 1
# debug("leave calcScore: ret", ret)
return ret
groupScore = [calcScore(i) for i in range(1 << N)]
# debug(": groupScore", groupScore)
from functools import lru_cache
@lru_cache(maxsize=None)
def sub(S):
ret = groupScore[S]
x = (S - 1) & S
while x > 0:
y = (~x) & S
v = sub(x) + sub(y)
if v > ret:
ret = v
x = (x - 1) & S
return ret
return sub(FULLBIT)
def main():
# parse input
N = int(input())
M = np.int64(read().split())
M = M.reshape((N, N))
print(solve(N, M))
# tests
T1 = """
3
0 10 20
10 0 -100
20 -100 0
"""
def test_T1():
"""
>>> as_input(T1)
>>> main()
20
"""
T2 = """
2
0 -10
-10 0
"""
def test_T2():
"""
>>> as_input(T2)
>>> main()
0
"""
T3 = """
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
"""
def test_T3():
"""
>>> as_input(T3)
>>> main()
4999999999
"""
T4 = """
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
"""
def test_T4():
"""
>>> as_input(T4)
>>> main()
132
"""
# add tests above
def _test():
import doctest
doctest.testmod()
def as_input(s):
"use in test, use given string as input file"
import io
global read, input
f = io.StringIO(s.strip())
def input():
return bytes(f.readline(), "ascii")
def read():
return bytes(f.read(), "ascii")
USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
print("compiling")
from numba.pycc import CC
cc = CC('my_module')
cc.export('solve', solve.__doc__.strip().split()[0])(solve)
cc.compile()
exit()
else:
input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
# -p: pure python mode
# if not -p, import compiled module
from my_module import solve # pylint: disable=all
elif sys.argv[-1] == "-t":
print("testing")
_test()
sys.exit()
elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
# input given as file
input_as_file = open(sys.argv[1])
input = input_as_file.buffer.readline
read = input_as_file.buffer.read
main()
``` | instruction | 0 | 67,065 | 19 | 134,130 |
No | output | 1 | 67,065 | 19 | 134,131 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
'''
time O(2^n * n^2 + 3^n)
'''
import sys
input=sys.stdin.readline
n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
pre = [0] * (1 << n)
# pre[mask] - score of group 'mask'
for mask in range(1 << n):
for i in range(n):
if mask & (1 << i):
for j in range(i + 1, n):
if mask & (1 << j):
pre[mask] += a[i][j]
def rec(i, not_taken, score_so_far, mask, group):
if i == len(not_taken):
dp[mask] = max(dp[mask], score_so_far + pre[group])
return
rec(i + 1, not_taken, score_so_far, mask, group)
rec(i + 1, not_taken, score_so_far, mask ^ (1 << not_taken[i]), group ^ (1 << not_taken[i]))
dp = [float('-inf')] * (1 << n)
# dp[mask] - best total score if we grouped rabbits from 'mask' already
dp[0] = 0
for mask in range(1 << n):
not_taken = [i for i in range(n) if not (mask & (1 << i))]
rec(0, not_taken, dp[mask], mask, 0)
print(dp[(1 << n) - 1])
``` | instruction | 0 | 67,066 | 19 | 134,132 |
No | output | 1 | 67,066 | 19 | 134,133 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")
def bit(n, k):
return (n >> k) & 1
def solve(N: int, a: "List[List[int]]"):
DP = [0]*(1 << N)
flag = [False]*(1 << N)
def rec(S):
if flag[S]:
return DP[S]
flag[S] = True
ans = 0
for i in range(N):
for j in range(i+1, N):
if bit(S, i) and bit(S, j):
ans += a[i][j]
T = S
while T > 0:
ans = max(ans, rec(S)+rec(S ^ T))
T = (T-1) & S
DP[S] = ans
return ans
print(rec((1 << N)-1))
return
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
a = [[int(next(tokens)) for _ in range(N)]
for _ in range(N)] # type: "List[List[int]]"
solve(N, a)
if __name__ == '__main__':
main()
``` | instruction | 0 | 67,067 | 19 | 134,134 |
No | output | 1 | 67,067 | 19 | 134,135 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
There are N rabbits, numbered 1, 2, \ldots, N.
For each i, j (1 \leq i, j \leq N), the compatibility of Rabbit i and j is described by an integer a_{i, j}. Here, a_{i, i} = 0 for each i (1 \leq i \leq N), and a_{i, j} = a_{j, i} for each i and j (1 \leq i, j \leq N).
Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one group. After grouping, for each i and j (1 \leq i < j \leq N), Taro earns a_{i, j} points if Rabbit i and j belong to the same group.
Find Taro's maximum possible total score.
Constraints
* All values in input are integers.
* 1 \leq N \leq 16
* |a_{i, j}| \leq 10^9
* a_{i, i} = 0
* a_{i, j} = a_{j, i}
Input
Input is given from Standard Input in the following format:
N
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}
Output
Print Taro's maximum possible total score.
Examples
Input
3
0 10 20
10 0 -100
20 -100 0
Output
20
Input
2
0 -10
-10 0
Output
0
Input
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0
Output
4999999999
Input
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0
Output
132
Submitted Solution:
```
def solve():
N = int(input())
Ass = [tuple(map(int, input().split())) for _ in range(N)]
scores = [0] * (1<<N)
for S in range(1, 1<<N):
for i in range(N):
if (S>>i) & 1:
score = scores[S ^ (1<<i)]
for j in range(i+1, N):
if (S>>j) & 1:
score += Ass[i][j]
scores[S] = score
break
def enumSubset(S):
subSs = []
subS = S
while True:
subSs.append(subS)
subS = (subS-1) & S
if subS == S:
break
subSs.reverse()
return subSs
dp = [0] * (1<<N)
for S in range(1<<N):
subSs = enumSubset(S)
dp[S] = max([scores[subS] + dp[S-subS] for subS in subSs])
print(dp[-1])
solve()
``` | instruction | 0 | 67,068 | 19 | 134,136 |
No | output | 1 | 67,068 | 19 | 134,137 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of x^k? Print the answer modulo 998244353.
Input
The only line contains three integers n, m and k (1 ≤ n, m < 998244353, 1 ≤ k ≤ 5000).
Output
Print one integer — the expected value of x^k, taken modulo 998244353 (the answer can always be represented as an irreducible fraction a/b, where b mod 998244353 ≠ 0; you have to print a ⋅ b^{-1} mod 998244353).
Examples
Input
1 1 1
Output
1
Input
1 1 5000
Output
1
Input
2 2 2
Output
499122178
Input
998244352 1337 5000
Output
326459680 | instruction | 0 | 67,405 | 19 | 134,810 |
Tags: combinatorics, dp, math, number theory, probabilities
Correct Solution:
```
dp=[0]*5010
dp[0]=1
n,m,k=map(int,input().split())
t=998244353
for i in range(k):
for j in range(len(dp)-1, -1, -1):
if j==0:dp[j]=0
else:dp[j]=(dp[j-1]*(n-j+1)+dp[j]*j)%t
c=0
for i in range(k+1):
c+=pow(m,max(n-i,0),t)*dp[i]
c%=t
den=pow(m,n,t)
den=pow(den,t-2,t)
print((den*c)%t)
``` | output | 1 | 67,405 | 19 | 134,811 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of x^k? Print the answer modulo 998244353.
Input
The only line contains three integers n, m and k (1 ≤ n, m < 998244353, 1 ≤ k ≤ 5000).
Output
Print one integer — the expected value of x^k, taken modulo 998244353 (the answer can always be represented as an irreducible fraction a/b, where b mod 998244353 ≠ 0; you have to print a ⋅ b^{-1} mod 998244353).
Examples
Input
1 1 1
Output
1
Input
1 1 5000
Output
1
Input
2 2 2
Output
499122178
Input
998244352 1337 5000
Output
326459680 | instruction | 0 | 67,406 | 19 | 134,812 |
Tags: combinatorics, dp, math, number theory, probabilities
Correct Solution:
```
def inv(a):
return pow(a, MOD-2, MOD) %MOD
def fat(a):
ans = 1
for i in range(1, a+1):
ans = (ans*i)%MOD
return ans
MOD = 998244353
n, m, k = map(int, input().split())
p = inv(m)
# O(Burro) O(n2)-> Fórmula da experança = sum(x^k * C(n, x) * p^x * q^(n-x))
'''
resp = 0
q = (1 - p + MOD) % MOD
for x in range(1, n+1):
resp += pow(x, k, MOD) %MOD * fat(n) %MOD * invfat(n-x) %MOD * invfat(x) %MOD *pow(p, x, MOD) %MOD * pow(q, n-x, MOD) %MOD
'''
# O(menos burro) O(nlog(k)) -> atualiza somatório usando valores ja calculados
if n<=k:
q = (1 - p + MOD) % MOD
inv_q = inv(q)
p_aux = p
q_aux = pow(q, n-1, MOD)
fn = n
resp = n * p%MOD * q_aux%MOD
resp = resp%MOD
for i in range(2, n+1):
x = pow(i, k, MOD)
p_aux = p_aux * p %MOD
q_aux = q_aux * inv_q
fn = fn*(n-i+1)%MOD*inv(i)%MOD
resp = resp + x * fn %MOD * p_aux %MOD * q_aux %MOD
resp = resp%MOD
print(resp)
# O(inteligente) O(k2) -> A partir da FGM M(X), identifica padrão das derivadas
# k-ésima derivada de M(X) = E(x^k)
# E(x^k) = n!/(n-k)! * p^k + k*(deriv[k-1] - sum(ind[i]*deriv[i])) + sum(ind[i]*deriv[i+1])
else:
deriv = [(n*p)%MOD] #E(x) = np
resp = n*n%MOD*p%MOD*p%MOD - n*p%MOD*p%MOD + n*p%MOD #E(x^2) = n^2p^2 - np^2 + np
deriv.append(resp%MOD)
fn = (n*(n-1))%MOD
v=[1];
for i in range(3, k+1):
fn = (fn * (n-i+1) %MOD)%MOD
w = [(-(i-1)*v[0])%MOD]
for j in range(len(v)-1):
w.append((v[j] - (i-1)*v[j+1]%MOD)%MOD)
w.append((v[-1] + i-1)%MOD)
v = tuple(w)
resp = (fn * pow(p, i, MOD))%MOD
for j in range(len(v)):
resp = (resp + v[j]*deriv[j]%MOD)%MOD
resp = resp%MOD
resp %= MOD
deriv.append(resp)
print(deriv[k-1])
``` | output | 1 | 67,406 | 19 | 134,813 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of x^k? Print the answer modulo 998244353.
Input
The only line contains three integers n, m and k (1 ≤ n, m < 998244353, 1 ≤ k ≤ 5000).
Output
Print one integer — the expected value of x^k, taken modulo 998244353 (the answer can always be represented as an irreducible fraction a/b, where b mod 998244353 ≠ 0; you have to print a ⋅ b^{-1} mod 998244353).
Examples
Input
1 1 1
Output
1
Input
1 1 5000
Output
1
Input
2 2 2
Output
499122178
Input
998244352 1337 5000
Output
326459680 | instruction | 0 | 67,407 | 19 | 134,814 |
Tags: combinatorics, dp, math, number theory, probabilities
Correct Solution:
```
class UnionFindVerSize():
def __init__(self, N):
self._parent = [n for n in range(0, N)]
self._size = [1] * N
self.group = N
def find_root(self, x):
if self._parent[x] == x: return x
self._parent[x] = self.find_root(self._parent[x])
stack = [x]
while self._parent[stack[-1]]!=stack[-1]:
stack.append(self._parent[stack[-1]])
for v in stack:
self._parent[v] = stack[-1]
return self._parent[x]
def unite(self, x, y):
gx = self.find_root(x)
gy = self.find_root(y)
if gx == gy: return
self.group -= 1
if self._size[gx] < self._size[gy]:
self._parent[gx] = gy
self._size[gy] += self._size[gx]
else:
self._parent[gy] = gx
self._size[gx] += self._size[gy]
def get_size(self, x):
return self._size[self.find_root(x)]
def is_same_group(self, x, y):
return self.find_root(x) == self.find_root(y)
import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd
input = lambda :sys.stdin.buffer.readline()
mi = lambda :map(int,input().split())
li = lambda :list(mi())
def cmb(n, r, mod):
if ( r<0 or r>n ):
return 0
r = min(r, n-r)
return (g1[n] * g2[r] % mod) * g2[n-r] % mod
mod = 998244353
N = 5100
g1 = [1]*(N+1)
g2 = [1]*(N+1)
inverse = [1]*(N+1)
for i in range( 2, N + 1 ):
g1[i]=( ( g1[i-1] * i ) % mod )
inverse[i]=( ( -inverse[mod % i] * (mod//i) ) % mod )
g2[i]=( (g2[i-1] * inverse[i]) % mod )
inverse[0]=0
n,m,k = mi()
im = pow(m,mod-2,mod)
im_1 = pow(m-1,mod-2,mod)
if n <= k and 1:
res = 0
for i in range(n+1):
res += (cmb(n,i,mod) * pow(im,i,mod) % mod) * (pow(1-im,n-i,mod) * pow(i,k,mod) % mod) % mod
res %= mod
print(res)
else:
dp = [1]
for i in range(k):
dp.append(0)
for j in range(i,-1,-1):
dp[j+1] += dp[j] * (n-j)
dp[j+1] %= mod
dp[j] *= j
dp[j] %= mod
if m!=1:
res = 0
c = m * im_1 % mod
for i in range(k+1):
res += (dp[i] * pow(c,n-i,mod) % mod) * pow(im_1,i,mod) % mod
res %= mod
res *= pow((m-1)*im,n,mod)
res %= mod
print(res)
else:
print(pow(n,k,mod))
``` | output | 1 | 67,407 | 19 | 134,815 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of x^k? Print the answer modulo 998244353.
Input
The only line contains three integers n, m and k (1 ≤ n, m < 998244353, 1 ≤ k ≤ 5000).
Output
Print one integer — the expected value of x^k, taken modulo 998244353 (the answer can always be represented as an irreducible fraction a/b, where b mod 998244353 ≠ 0; you have to print a ⋅ b^{-1} mod 998244353).
Examples
Input
1 1 1
Output
1
Input
1 1 5000
Output
1
Input
2 2 2
Output
499122178
Input
998244352 1337 5000
Output
326459680
Submitted Solution:
```
n,m,k=map(int,input().split())
a = n
b = m
b = b ** -1
print((a/b)**k% 998244353)
``` | instruction | 0 | 67,408 | 19 | 134,816 |
No | output | 1 | 67,408 | 19 | 134,817 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.
Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of x^k? Print the answer modulo 998244353.
Input
The only line contains three integers n, m and k (1 ≤ n, m < 998244353, 1 ≤ k ≤ 5000).
Output
Print one integer — the expected value of x^k, taken modulo 998244353 (the answer can always be represented as an irreducible fraction a/b, where b mod 998244353 ≠ 0; you have to print a ⋅ b^{-1} mod 998244353).
Examples
Input
1 1 1
Output
1
Input
1 1 5000
Output
1
Input
2 2 2
Output
499122178
Input
998244352 1337 5000
Output
326459680
Submitted Solution:
```
n = int(input().split()[0])
if n==1:
print(1)
elif n==2:
print(499122178)
elif n==998244352:
print(326459680)
else:
print("Hey you, I want to say something to you.")
``` | instruction | 0 | 67,409 | 19 | 134,818 |
No | output | 1 | 67,409 | 19 | 134,819 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,450 | 19 | 134,900 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
from collections import defaultdict
for _ in range(int(input())):
n = int(input())
ls = list(map(int, input().split()))
mem = defaultdict(int)
for i in ls:
temp = i
cc = 0
while temp:
if temp % 2:
mem[cc] += 1
temp //= 2
cc += 1
flag = 0
for i in sorted(mem.keys(), reverse=True):
if mem[i] % 2 == 0:
continue
elif mem[i] % 4 == 3 and n%2:
print("LOSE")
flag = 1
break
else:
flag = 1
print("WIN")
break
if flag == 0:
print("DRAW")
``` | output | 1 | 67,450 | 19 | 134,901 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,451 | 19 | 134,902 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
def run(n, a):
for i in range(30, -1, -1):
count = 0
for j in range(n):
count += (a[j] >> i) & 1
if count % 4 == 1:
return 'WIN'
elif count % 2 == 1 and n % 2 == 1:
return 'LOSE'
elif count % 2 == 1:
return 'WIN'
return 'DRAW'
def main():
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = run(n, a)
print(ans)
if __name__ == '__main__':
main()
``` | output | 1 | 67,451 | 19 | 134,903 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,452 | 19 | 134,904 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
#
# ------------------------------------------------
# ____ _ Generatered using
# / ___| | |
# | | __ _ __| | ___ _ __ ______ _
# | | / _` |/ _` |/ _ \ '_ \|_ / _` |
# | |__| (_| | (_| | __/ | | |/ / (_| |
# \____\____|\____|\___|_| |_/___\____|
#
# GNU Affero General Public License v3.0
# ------------------------------------------------
# Author : prophet
# Created : 2020-07-26 09:18:52.941067
# UUID : zn5xaUsJG5Wv1q9f
# ------------------------------------------------
#
production = True
import sys, math, collections
def input(f = 0, m = 0):
if m > 0: return [input(f) for i in range(m)]
else:
l = sys.stdin.readline()[:-1]
if f >= 10:
u = False
f = int(str(f)[-1])
else: u = True
if f == 0: p = [l]
elif f == 1: p = list(map(int, l.split()))
elif f == 2: p = list(map(float, l.split()))
elif f == 3: p = list(l)
elif f == 4: p = list(map(int, list(l)))
elif f == 5: p = l.split()
return p if u else p[0]
def out(l, f = 0, n = True):
if f == 0: p = str(l)
elif f == 1: p = " ".join(map(str, l))
elif f == 2: p = "\n".join(map(str, l))
elif f == 3: p = "".join(map(str, l))
print(p, end = "\n" if n else "")
def log(*args):
if not production:
print("$$$", end = "")
print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)
flip = lambda a: (a + 1) & 1
def mapl(i, f = 0):
if f == 0: return list(map(int, i))
elif f == 1: return list(map(str, i))
elif f == 2: return list(map(list, i))
#
# >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#
def solve():
n = input(11)
a = input(1)
b = [0] * 32
for i in a:
bi = bin(i)[2:].zfill(32)
for p, j in enu(bi):
b[p] += int(j)
y = 0
for i in b:
if i & 1:
y = i
break
else:
out("DRAW")
return
if not ((y - 1) // 2) & 1:
out("WIN")
else:
if n & 1:
out("LOSE")
else:
out("WIN")
return
for i in range(input(11)): solve()
#
# >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#
``` | output | 1 | 67,452 | 19 | 134,905 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,453 | 19 | 134,906 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
t = input()
t = int(t)
def solve(n, arr):
a_max = max(arr)
k = len(bin(a_max))-2
while(k>0):
mask = 2**(k-1)
n_1 = 0
n_0 = 0
for x in arr:
if x & mask == mask :
n_1+=1
else :
n_0+=1
if n_1%4==3 and n_0%2==0:
print("LOSE")
return
elif n_1%2==1:
print("WIN")
return
k-=1
print("DRAW")
return
for i in range(t):
n = input()
n = int(n)
arr = list(map(int, input().split()))
solve(n, arr)
``` | output | 1 | 67,453 | 19 | 134,907 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,454 | 19 | 134,908 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
from collections import defaultdict
import sys, math
f = None
try:
f = open('q1.input', 'r')
except IOError:
f = sys.stdin
if 'xrange' in dir(__builtins__):
range = xrange
def print_case_iterable(case_num, iterable):
print("Case #{}: {}".format(case_num," ".join(map(str,iterable))))
def print_case_number(case_num, iterable):
print("Case #{}: {}".format(case_num,iterable))
def print_iterable(A):
print (' '.join(A))
def read_int():
return int(f.readline().strip())
def read_int_array():
return [int(x) for x in f.readline().strip().split(" ")]
def rns():
a = [x for x in f.readline().split(" ")]
return int(a[0]), a[1].strip()
def read_string():
return list(f.readline().strip())
def ri():
return int(f.readline().strip())
def ria():
return [int(x) for x in f.readline().strip().split(" ")]
def rns():
a = [x for x in f.readline().split(" ")]
return int(a[0]), a[1].strip()
def rs():
return list(f.readline().strip())
def bi(x):
return bin(x)[2:]
from collections import deque
import math
NUMBER = 10**9 + 7
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
from collections import deque, defaultdict
import heapq
from types import GeneratorType
def shift(a,i,num):
for _ in range(num):
a[i],a[i+1],a[i+2] = a[i+2],a[i],a[i+1]
from heapq import heapify, heappush, heappop
from string import ascii_lowercase as al
def solution(a,n):
draw = True
for i in range(50,-1,-1):
cnt = 0
for x in a:
if (1<<i) & x:
cnt+=1
if cnt % 2:
draw = False
break
if draw:
return 'DRAW'
if (cnt - 1) % 4 == 0:
return 'WIN'
win = ((cnt+1)//2) % 2
win = win^((n - cnt )% 2)
return 'WIN' if win else 'LOSE'
def main():
for i in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
x = solution(a,n)
if 'xrange' not in dir(__builtins__):
print(x) # print("Case #"+str(i+1)+':',x)
else:
print >>output,"Case #"+str(i+1)+':',str(x)
if 'xrange' in dir(__builtins__):
print(output.getvalue())
output.close()
if 'xrange' in dir(__builtins__):
import cStringIO
output = cStringIO.StringIO()
if __name__ == '__main__':
main()
``` | output | 1 | 67,454 | 19 | 134,909 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,455 | 19 | 134,910 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")
def getlist():
return list(map(int, input().split()))
#処理内容
def main():
T = int(input())
for _ in range(T):
N = int(input())
A = getlist()
A.sort()
XOR = 0
for i in range(N):
XOR ^= A[i]
if XOR == 0:
print("DRAW")
else:
L = [0] * 40
for i in range(N):
n = A[i]
for j in range(40):
if n % 2 == 1:
L[j] += 1
n >>= 1
if n == 0:
break
# print(L)
jud = None
for i in range(39, -1, -1):
if L[i] % 2 == 1:
# print(L[i], N - L[i])
if L[i] % 4 == 1 or (N - L[i]) % 2 == 1:
jud = "WIN"
else:
jud = "LOSE"
break
# if L[i] == 1:
# jud = "WIN"
# break
# if L[i] % 4 == 1:
# if (N - L[i]) % 2 == 0:
# jud = "WIN"
# else:
# jud = "LOSE"
# else:
# if (N - L[i]) % 2 == 0:
# jud = "LOSE"
# else:
# jud = "WIN"
# break
print(jud)
if __name__ == '__main__':
main()
``` | output | 1 | 67,455 | 19 | 134,911 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,456 | 19 | 134,912 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
#1384D
import sys
def solve(significant, filler):
if filler % 2 == 0:
return significant % 4 == 1
return True
def i():
return sys.stdin.readline()[:-1]
for _ in range(int(i())):
digits = [0]*30
n = int(i())
nums = map(int, i().split())
for num in nums:
binNum = bin(num)
binNum = binNum[2:]
binNum = binNum[::-1]
for index,char in enumerate(binNum):
if char == "1":
digits[index] += 1
digits = digits[::-1]
msb = 0
draw = False
for x in range(30):
if digits[x] % 2 != 0:
msb = digits[x]
break
else:
print("DRAW")
draw = True
if not draw:
if solve(msb, n-msb):
print("WIN")
else:
print("LOSE")
``` | output | 1 | 67,456 | 19 | 134,913 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins. | instruction | 0 | 67,457 | 19 | 134,914 |
Tags: bitmasks, constructive algorithms, dp, games, greedy, math
Correct Solution:
```
T = int(input())
for _ in range(T):
N = int(input())
A = list(map(int,input().split()))
bits = [0] * 32
for a in A:
for i in range(31,-1,-1):
if a&(1<<i):
bits[i] += 1
for b in reversed(bits):
if b%4==1:
print('WIN')
break
if b%4==3:
print('LOSE' if N%2 else 'WIN')
break
else:
print('DRAW')
``` | output | 1 | 67,457 | 19 | 134,915 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
cnts = [0] * 40
for i in range(n):
for j in range(40):
if (a[i] >> j) & 1:
cnts[j] += 1
for cnt in cnts[::-1]:
cnt0 = n - cnt
cnt1 = cnt
if cnt1 % 2 == 0:
continue
if cnt1 % 4 == 1:
print("WIN")
break
if cnt1 % 4 == 3 and cnt0 % 2 == 1:
print("WIN")
break
if cnt1 % 4 == 3 and cnt0 % 2 == 0:
print("LOSE")
break
else:
print("DRAW")
``` | instruction | 0 | 67,458 | 19 | 134,916 |
Yes | output | 1 | 67,458 | 19 | 134,917 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
now = 0
for i in range(31,-1,-1):
now = 0
tmp = 2**i
for j in a:
if j & tmp > 0:
now += 1
if now % 2 == 1:
break
else:
print ("DRAW")
continue
rem = n - now
#print (now,rem)
if now == 1:
print ("WIN")
elif (now//2+1)%2 == 0:
if rem % 2 == 0:
print ("LOSE")
else:
print ("WIN")
else:
print ("WIN")
``` | instruction | 0 | 67,459 | 19 | 134,918 |
Yes | output | 1 | 67,459 | 19 | 134,919 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
class B:
@staticmethod
def input():
n = int(input())
nums = input()
return n, nums
def __init__(self, n=None, nums=None):
if n is None:
n, nums = B.input()
self.n = n
self.nums = [int(x) for x in nums.split()]
self.totxor = 0
for num in self.nums:
self.totxor ^= num
self.maxbit = -1
for pw, val in enumerate(format(self.totxor, '031b')[::-1]):
if val == '1':
self.maxbit = pw
self.nums = [format(x, '031b')[::-1][self.maxbit] for x in self.nums]
def solve(self):
if self.totxor == 0:
print('DRAW')
return 'DRAW'
one_nr = self.nums.count('1')
zer_nr = self.n - one_nr
win = 'WIN'
lose = 'LOSE'
if one_nr % 4 == 3 and zer_nr % 2 == 1:
print(win)
return 'WIN'
if one_nr % 4 == 3 and zer_nr % 2 == 0:
print(lose)
return 'LOSE'
else:
print(win)
return 'WIN'
if __name__ == '__main__':
for _ in range(int(input())):
B().solve()
``` | instruction | 0 | 67,460 | 19 | 134,920 |
Yes | output | 1 | 67,460 | 19 | 134,921 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
import sys
import math
from heapq import *;
input = sys.stdin.readline
from functools import cmp_to_key;
def pi():
return(int(input()))
def pl():
return(int(input(), 16))
def ti():
return(list(map(int,input().split())))
def ts():
s = input()
return(list(s[:len(s) - 1]))
def invr():
return(map(int,input().split()))
mod = 1000000007;
f = [];
def fact(n,m):
global f;
f = [1 for i in range(n+1)];
f[0] = 1;
for i in range(1,n+1):
f[i] = (f[i-1]*i)%m;
def fast_mod_exp(a,b,m):
res = 1;
while b > 0:
if b & 1:
res = (res*a)%m;
a = (a*a)%m;
b = b >> 1;
return res;
def inverseMod(n,m):
return fast_mod_exp(n,m-2,m);
def ncr(n,r,m):
if n < 0 or r < 0 or r > n: return 0;
if r == 0: return 1;
return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m;
def main():
B();
def B():
t = pi();
while t:
t -= 1;
n,a = pi(),ti();
x = int(math.pow(2,30));
f = 0;
for i in range(31):
c = 0;
for j in range(n):
if x & int(a[j]) != 0:
c += 1;
if c % 2 != 0:
if c % 4 == 1:
print('WIN');
else:
if (n-c) % 2 != 0:
print('WIN');
else:
print('LOSE');
f = 1;
break;
x = x >> 1;
if f == 0:
print('DRAW');
main();
``` | instruction | 0 | 67,461 | 19 | 134,922 |
Yes | output | 1 | 67,461 | 19 | 134,923 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
N = int(input())
a = list(map(int, input().split()))
table = [0] * 31
for x in a:
t = 1
for i in range(31):
table[i] += x & t > 0
t <<= 1
for i in range(N):
t = 1
for j in range(31):
if a[i] & t:
if table[j] % 2 == 0: a[i] -= t
t <<= 1
table = [0] * 31
for x in a:
t = 1
for i in range(31):
table[i] += x & t > 0
t <<= 1
win = 0
for i in range(30, -1, -1):
if table[i]:
if table[i] == 1:
win = 1
break
for x in range(1, table[i] + 1, 2):
y = table[i] - x
z = (N - table[i]) - (-(-N // 2) - x)
#print(x, y, z)
if y < N // 2: continue
if z < min(N // 2, N - table[i]): continue
break
else:
win = -1
break
win = 1
break
if win == 1: print("WIN")
elif win == 0: print("DRAW")
else: print("LOSE")
``` | instruction | 0 | 67,462 | 19 | 134,924 |
No | output | 1 | 67,462 | 19 | 134,925 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
size = int(input())
Array = []
for i in range(size):
n = int(input())
a = list(map(int, input().split()))
Array.append(a)
pass
koa_sum = 0
other_sum = 0
x = 0
y = 0
z = 0
a = 0
b = 1
for x in range(len(Array)):
for y in range(a, len(Array[x]), 2):
koa_sum += Array[x][y]
pass
for z in range(b, len(Array[x]), 2):
other_sum += Array[x][z]
pass
if koa_sum > other_sum:
print("WIN")
elif koa_sum < other_sum:
print("LOSE")
else:
print("DRAW")
koa_sum = 0
other_sum = 0
a = 1
b = 0
``` | instruction | 0 | 67,463 | 19 | 134,926 |
No | output | 1 | 67,463 | 19 | 134,927 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
#from functools import lru_cache #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------
for _ in range(int(input())):
n = int(input())
a = list(mapi())
cnt = [0]*64
for i in a:
for j in range(64):
cnt[j]+=(i>>j)&1
draw = 1
k = 0
for i in range(63,-1,-1):
if cnt[i]&1:
k = i
draw = 0; break
if draw:
print("DRAW")
elif cnt[k]%4==1:
print("WIN")
else:
print("LOSE")
``` | instruction | 0 | 67,464 | 19 | 134,928 |
No | output | 1 | 67,464 | 19 | 134,929 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Koa the Koala and her best friend want to play a game.
The game starts with an array a of length n consisting of non-negative integers. Koa and her best friend move in turns and each have initially a score equal to 0. Koa starts.
Let's describe a move in the game:
* During his move, a player chooses any element of the array and removes it from this array, xor-ing it with the current score of the player.
More formally: if the current score of the player is x and the chosen element is y, his new score will be x ⊕ y. Here ⊕ denotes [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
Note that after a move element y is removed from a.
* The game ends when the array is empty.
At the end of the game the winner is the player with the maximum score. If both players have the same score then it's a draw.
If both players play optimally find out whether Koa will win, lose or draw the game.
Input
Each test contains multiple test cases. The first line contains t (1 ≤ t ≤ 10^4) — the number of test cases. Description of the test cases follows.
The first line of each test case contains the integer n (1 ≤ n ≤ 10^5) — the length of a.
The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 10^9) — elements of a.
It is guaranteed that the sum of n over all test cases does not exceed 10^5.
Output
For each test case print:
* WIN if Koa will win the game.
* LOSE if Koa will lose the game.
* DRAW if the game ends in a draw.
Examples
Input
3
3
1 2 2
3
2 2 3
5
0 0 0 2 2
Output
WIN
LOSE
DRAW
Input
4
5
4 1 5 1 3
4
1 0 1 6
1
0
2
5 4
Output
WIN
WIN
DRAW
WIN
Note
In testcase 1 of the first sample we have:
a = [1, 2, 2]. Here Koa chooses 1, other player has to choose 2, Koa chooses another 2. Score for Koa is 1 ⊕ 2 = 3 and score for other player is 2 so Koa wins.
Submitted Solution:
```
'''
@Author: nuoyanli
@Date: 2020-07-24 23:59:13
@LastEditTime: 2020-07-25 08:32:01
@Author's blog: https://blog.nuoyanli.com/
'''
T = int(input())
for _ in range(T):
n = int(input())
A = list(map(int, input().strip().split()))
bits = [0] * 32
for a in A:
for i in range(31, -1, -1):
if a & (1 << i):
bits[i] += 1
for b in reversed(bits):
if b % 4 == 1:
print('WIN')
break
if b % 4 == 3:
print('LOSE' if n % 2 else 'WIn')
break
else:
print('DRAW')
``` | instruction | 0 | 67,465 | 19 | 134,930 |
No | output | 1 | 67,465 | 19 | 134,931 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9. | instruction | 0 | 68,359 | 19 | 136,718 |
Tags: dp
Correct Solution:
```
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
dp = [[0] * n for _ in range(n)]
for i in range(1, n):
cnt = 0
for j in range(i, n):
dp[i][j] = float("inf")
for j in range(i - 1, -1, -1):
if j + A[j] >= i:
dp[i][j + A[j]] = min(dp[i][j + A[j]], dp[j][i - 1] + cnt)
cnt += 1
for j in range(i + 1, n):
dp[i][j] = min(dp[i][j], dp[i][j - 1])
print(dp[-1][-1])
``` | output | 1 | 68,359 | 19 | 136,719 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9. | instruction | 0 | 68,360 | 19 | 136,720 |
Tags: dp
Correct Solution:
```
import sys
def input(): return sys.stdin.readline().strip()
def inputlist(): return list(map(int, input().split()))
def printlist(var) : sys.stdout.write(' '.join(map(str, var))+'\n')
def print(var) : sys.stdout.write(str(var)+'\n')
up = (int)(1e18)
for _ in range(int(input())):
n=int(input())
a=inputlist()
dp=[[0]*(n+1) for i in range(n+1)]
for i in range(n-1,-1,-1):
for j in range(i,-1,-1):
dp[j][i]=dp[j+1][i]
if a[j]>i-j:
dp[j][i]=dp[j][i]+1
_dp=[[up]*(n+1) for i in range(n+1)]
_dp[n-1][n]=0
for i in range(n-1,-1,-1):
idx=i+a[i]
while idx>i:
_dp[i][idx]=min(_dp[i][idx+1],dp[i+1][idx-1]+_dp[idx][i+a[i]+1])
idx=idx-1
print(_dp[0][1])
``` | output | 1 | 68,360 | 19 | 136,721 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9. | instruction | 0 | 68,361 | 19 | 136,722 |
Tags: dp
Correct Solution:
```
import sys
input = sys.stdin.readline
MAX_N = 3000
inf = MAX_N
def solve():
global inf
n = int(input())
a = [0] + list(map(int, input().split()))
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(2, n + 1):
cnt = 0
for j in range(i, n + 1):
dp[i][j] = inf
for j in range(i - 1, 0, -1):
if j + a[j] >= i:
dp[i][j + a[j]] = min(dp[i][j + a[j]], dp[j][i - 1 ] + cnt)
cnt += 1
for j in range(i + 1, n + 1):
dp[i][j] = min(dp[i][j], dp[i][j - 1])
return dp[n][n]
if __name__ == '__main__':
t = int(input())
while t:
print(solve())
t -= 1
``` | output | 1 | 68,361 | 19 | 136,723 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9. | instruction | 0 | 68,362 | 19 | 136,724 |
Tags: dp
Correct Solution:
```
import sys
input = sys.stdin.readline
MAX_N = 3000
inf = MAX_N
def solve():
global inf
n = int(input())
a = [0] + list(map(int, input().split()))
dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(2, n + 1):
cnt = 0
for j in range(i, n + 1):
dp[i][j] = inf
for j in range(i - 1, 0, -1):
if j + a[j] >= i:
dp[i][j + a[j]] = min(dp[i][j + a[j]], dp[j][i - 1 ] + cnt)
cnt += 1
for j in range(i + 1, n + 1):
dp[i][j] = min(dp[i][j], dp[i][j - 1])
return dp[n][n]
for _ in range(int(input())):print(solve())
``` | output | 1 | 68,362 | 19 | 136,725 |
Provide tags and a correct Python 3 solution for this coding contest problem.
Gildong is now developing a puzzle game. The puzzle consists of n platforms numbered from 1 to n. The player plays the game as a character that can stand on each platform and the goal of the game is to move the character from the 1-st platform to the n-th platform.
The i-th platform is labeled with an integer a_i (0 ≤ a_i ≤ n-i). When the character is standing on the i-th platform, the player can move the character to any of the j-th platforms where i+1 ≤ j ≤ i+a_i. If the character is on the i-th platform where a_i=0 and i ≠ n, the player loses the game.
Since Gildong thinks the current game is not hard enough, he wants to make it even harder. He wants to change some (possibly zero) labels to 0 so that there remains exactly one way to win. He wants to modify the game as little as possible, so he's asking you to find the minimum number of platforms that should have their labels changed. Two ways are different if and only if there exists a platform the character gets to in one way but not in the other way.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 ≤ t ≤ 500).
Each test case contains two lines. The first line of each test case consists of an integer n (2 ≤ n ≤ 3000) — the number of platforms of the game.
The second line of each test case contains n integers. The i-th integer is a_i (0 ≤ a_i ≤ n-i) — the integer of the i-th platform.
It is guaranteed that:
* For each test case, there is at least one way to win initially.
* The sum of n in all test cases doesn't exceed 3000.
Output
For each test case, print one integer — the minimum number of different labels that should be changed to 0 so that there remains exactly one way to win.
Example
Input
3
4
1 1 1 0
5
4 3 2 1 0
9
4 1 4 2 1 0 2 1 0
Output
0
3
2
Note
In the first case, the player can only move to the next platform until they get to the 4-th platform. Since there is already only one way to win, the answer is zero.
In the second case, Gildong can change a_2, a_3, and a_4 to 0 so that the game becomes 4 0 0 0 0. Now the only way the player can win is to move directly from the 1-st platform to the 5-th platform.
In the third case, Gildong can change a_2 and a_8 to 0, then the only way to win is to move in the following way: 1 – 3 – 7 – 9. | instruction | 0 | 68,363 | 19 | 136,726 |
Tags: dp
Correct Solution:
```
MAX_N = 3000;inf = MAX_N
def solve():
global inf;n = int(input());a = [0] + list(map(int, input().split()));dp = [[0] * (n + 1) for i in range(n + 1)]
for i in range(2, n + 1):
cnt = 0
for j in range(i, n + 1):dp[i][j] = inf
for j in range(i - 1, 0, -1):
if j + a[j] >= i:dp[i][j + a[j]] = min(dp[i][j + a[j]], dp[j][i - 1 ] + cnt);cnt += 1
for j in range(i + 1, n + 1):dp[i][j] = min(dp[i][j], dp[i][j - 1])
return dp[n][n]
for _ in range(int(input())):print(solve())
``` | output | 1 | 68,363 | 19 | 136,727 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.