-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.py
executable file
·48 lines (34 loc) · 1.03 KB
/
sign.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#! /usr/bin/python3
import sys
import argparse
import signal
import itertools
import math
import numpy as np
import tools
def gen_permutations(n):
for perm in itertools.permutations(range(n), n):
yield np.array(perm) + 1
def gen_signs(n):
for i in range(1 << n):
sign = np.array([int(bit) for bit in np.binary_repr(i, width=n)])
sign = sign * 2 - 1
yield sign
def main(args):
n = int(sys.stdin.read())
permutations = list(gen_permutations(n))
signs = list(gen_signs(n))
print(len(permutations) * len(signs))
for perm, sign in itertools.product(permutations, signs):
signed_perm = perm * sign
print(*signed_perm)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='description',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
args = parser.parse_args()
try:
main(args)
except BrokenPipeError:
sys.exit(128 + signal.SIGPIPE)
except KeyboardInterrupt:
sys.exit(128 + signal.SIGINT)