Download
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
# Magic Sequences - Magic Square variant
#
# A magic sequence of length n is a sequence of integers x[0]
# to x[n-1], such that for all i in 0 to n-1, the number i occurs
# exactly x[i] times in the sequence.
#
# For instance, 6,2,1,0,0,0,1,0,0,0 is a magic sequence since 0
# occurs 6 times in it, 1 occurs twice, 2 occurs once, ...
#
 
from Numberjack import *
 
 
def get_model(N):
    seq = VarArray(N, N)
 
    model = Model()
    for i in xrange(N):
        model += seq[i] == Sum([seq[j] == i for j in xrange(N)])
 
    return seq, model
 
 
def solve(param):
    N = param['N']
 
    seq, model = get_model(N)
 
    solver = model.load(param['solver'])
    solver.setVerbosity(param['verbose'])
    solver.solve()
 
    if solver.is_sat():
        print str(seq)
    elif solver.is_unsat():
        print "Unsatisfiable"
    else:
        print "Timed out"
 
 
if __name__ == '__main__':
    default = {'N': 10, 'solver': 'Mistral', 'verbose': 0}
    param = input(default)
    solve(param)