algo/quickmaffs-permutation-puzzle
74 solves / 465 points
Writeup by Aryan
Once again, the problem statement is quite simple. We wish to compute the number of permutations p of the numbers 1 ... n (n < 10^4) that satisfy the following:
For all i (2 <= i <= n), p[i - 1] < p[i] if i is odd, and p[i - 1] > p[i] if i is even (assume 1-indexing)
Since this number is quite large, we must return the result mod 10^9 + 7.
A simple python bruteforce for i in [1, 8] gives us the following interesting looking sequence: 1, 1, 2, 5, 16, 61, 272, 1385.
import itertools
MOD = 10**9 + 7
def ok(perm):
for i in range(1, len(perm)):
if i % 2 == 1:
if perm[i-1] >= perm[i]:
return False
else:
if perm[i-1] <= perm[i]:
return False
return True
def perms(n):
all_permutations = itertools.permutations(range(1, n+1))
valid_count = 0
for perm in all_permutations:
if ok(perm):
valid_count += 1
return valid_count % MOD
for i in range(1, 9):
print(perms(i))
Turns out this is sequence A000111 on OEIS. Scrolling through the page, alternating permutations are mentioned a bunch. Googling alternating permutations leads to this GeeksForGeeks article that has most of the solution. All we need to do is implement some modular arithmetic to prevent overflow.
Flag: vsctf{QuickMaffs_FMC_Huge_Degen_b4a1c2af876eeb9a}
Last updated