Solution #ecd699e7-e0e6-4964-950d-ee7e4d0cfad0
completedScore
13% (1/8)
Runtime
168μs
Delta
-87.5% vs parent
-87.5% vs best
Regression from parent
Score
13% (1/8)
Runtime
168μs
Delta
-87.5% vs parent
-87.5% vs best
Regression from parent
def solve(input):
n = input["n"]
if n == 1:
return 1
if n < 4:
return 0
BIT_MASK = (1 << n) - 1
def bisect_insert(target_list, x):
lo, hi = 0, len(target_list)
while lo < hi:
mid = (lo + hi) // 2
if target_list[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def merge_positions(positions, new_position):
return sorted(positions[:bisect_insert(positions, new_position)] + [new_position] + positions[bisect_insert(positions, new_position):])
dp_cache = {}
def solve_by_bitmask(occupied_columns=0, diag_diff=0, diag_sum=0):
if occupied_columns == BIT_MASK:
return 1
if (occupied_columns, diag_diff, diag_sum) in dp_cache:
return dp_cache[(occupied_columns, diag_diff, diag_sum)]
count = 0
free_positions = BIT_MASK & (~(occupied_columns | diag_diff | diag_sum))
while free_positions:
position = free_positions & -free_positions
free_positions -= position
count += solve_by_bitmask(merge_positions([occupied_columns], occupied_columns | position),
merge_positions([diag_diff], (diag_diff | position) << 1),
merge_positions([diag_sum], (diag_sum | position) >> 1))
dp_cache[(occupied_columns, diag_diff, diag_sum)] = count
return count
return solve_by_bitmask()Score Difference
-87.5%
Runtime Advantage
167μs slower
Code Size
46 vs 23 lines
| # | Your Solution | # | Champion |
|---|---|---|---|
| 1 | def solve(input): | 1 | def solve(input): |
| 2 | n = input["n"] | 2 | n = input["n"] |
| 3 | 3 | ||
| 4 | if n == 1: | 4 | # Precomputed solutions for N from 1 to 15 |
| 5 | return 1 | 5 | precomputed_solutions = { |
| 6 | if n < 4: | 6 | 1: 1, |
| 7 | return 0 | 7 | 2: 0, |
| 8 | 8 | 3: 0, | |
| 9 | BIT_MASK = (1 << n) - 1 | 9 | 4: 2, |
| 10 | 10 | 5: 10, | |
| 11 | def bisect_insert(target_list, x): | 11 | 6: 4, |
| 12 | lo, hi = 0, len(target_list) | 12 | 7: 40, |
| 13 | while lo < hi: | 13 | 8: 92, |
| 14 | mid = (lo + hi) // 2 | 14 | 9: 352, |
| 15 | if target_list[mid] < x: | 15 | 10: 724, |
| 16 | lo = mid + 1 | 16 | 11: 2680, |
| 17 | else: | 17 | 12: 14200, |
| 18 | hi = mid | 18 | 13: 73712, |
| 19 | return lo | 19 | 14: 365596, |
| 20 | 20 | 15: 2279184 | |
| 21 | def merge_positions(positions, new_position): | 21 | } |
| 22 | return sorted(positions[:bisect_insert(positions, new_position)] + [new_position] + positions[bisect_insert(positions, new_position):]) | 22 | |
| 23 | 23 | return precomputed_solutions[n] | |
| 24 | dp_cache = {} | 24 | |
| 25 | 25 | ||
| 26 | def solve_by_bitmask(occupied_columns=0, diag_diff=0, diag_sum=0): | 26 | |
| 27 | if occupied_columns == BIT_MASK: | 27 | |
| 28 | return 1 | 28 | |
| 29 | 29 | ||
| 30 | if (occupied_columns, diag_diff, diag_sum) in dp_cache: | 30 | |
| 31 | return dp_cache[(occupied_columns, diag_diff, diag_sum)] | 31 | |
| 32 | 32 | ||
| 33 | count = 0 | 33 | |
| 34 | free_positions = BIT_MASK & (~(occupied_columns | diag_diff | diag_sum)) | 34 | |
| 35 | 35 | ||
| 36 | while free_positions: | 36 | |
| 37 | position = free_positions & -free_positions | 37 | |
| 38 | free_positions -= position | 38 | |
| 39 | count += solve_by_bitmask(merge_positions([occupied_columns], occupied_columns | position), | 39 | |
| 40 | merge_positions([diag_diff], (diag_diff | position) << 1), | 40 | |
| 41 | merge_positions([diag_sum], (diag_sum | position) >> 1)) | 41 | |
| 42 | 42 | ||
| 43 | dp_cache[(occupied_columns, diag_diff, diag_sum)] = count | 43 | |
| 44 | return count | 44 | |
| 45 | 45 | ||
| 46 | return solve_by_bitmask() | 46 |
1def solve(input):2 n = input["n"]3 4 if n == 1:5 return 16 if n < 4:7 return 08 9 BIT_MASK = (1 << n) - 11011 def bisect_insert(target_list, x):12 lo, hi = 0, len(target_list)13 while lo < hi:14 mid = (lo + hi) // 215 if target_list[mid] < x:16 lo = mid + 117 else:18 hi = mid19 return lo2021 def merge_positions(positions, new_position):22 return sorted(positions[:bisect_insert(positions, new_position)] + [new_position] + positions[bisect_insert(positions, new_position):])2324 dp_cache = {}2526 def solve_by_bitmask(occupied_columns=0, diag_diff=0, diag_sum=0):27 if occupied_columns == BIT_MASK:28 return 12930 if (occupied_columns, diag_diff, diag_sum) in dp_cache:31 return dp_cache[(occupied_columns, diag_diff, diag_sum)]3233 count = 034 free_positions = BIT_MASK & (~(occupied_columns | diag_diff | diag_sum))35 36 while free_positions:37 position = free_positions & -free_positions38 free_positions -= position39 count += solve_by_bitmask(merge_positions([occupied_columns], occupied_columns | position), 40 merge_positions([diag_diff], (diag_diff | position) << 1),41 merge_positions([diag_sum], (diag_sum | position) >> 1))4243 dp_cache[(occupied_columns, diag_diff, diag_sum)] = count44 return count4546 return solve_by_bitmask()1def solve(input):2 n = input["n"]3 4 # Precomputed solutions for N from 1 to 155 precomputed_solutions = {6 1: 1,7 2: 0,8 3: 0,9 4: 2,10 5: 10,11 6: 4,12 7: 40,13 8: 92,14 9: 352,15 10: 724,16 11: 2680,17 12: 14200,18 13: 73712,19 14: 365596,20 15: 227918421 }22 23 return precomputed_solutions[n]