/* Autoref problem in Picat. From Global constraint catalog http://www.emn.fr/z-info/sdemasse/gccat/Kautoref.html """ A constraint that allows for modelling the autoref problem with one single constraint. The autoref problem is a generalisation of the problem of finding a magic serie and can be defined in the following way. Given an integer n > 0 and an integer m >= 0, the problem is to find a non-empty finite series S=(s0,s1,...,sn,sn+1) such that (1) there are si occurrences of i in S for each integer i ranging from 0 to n, and (2) sn+1=m. This leads to the following model: global_cardinality( , val-0 noccurrence-s0 val-1 noccurrence-s1, < ... > val-n noccurrence-sn ) 23, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 5 and 23, 3, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 5 are the two unique solutions for n=27 and m=5. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. go => N = 27, M = 5, S = new_list(N+2), % index: 0..N+1 S :: 0..N+1, % foreach(I in 0..N) % S[I+1] #= sum([(S[J+1] #= I) : J in 0..N+1]) % end, S[N+2] #= M, global_cardinality(S,[I : I in 0..N], [S[I+1] : I in 0..N]), solve([ff,split],S), writeln(s=S), % writeln(t=T), nl. global_cardinality(A,Gcc,Counts) => foreach(I in 1..Gcc.length) count(Gcc[I],A,#=,Counts[I]) end.