$xx=[] $yy=[] $num=0 class Patterns @a = [] def initialize(x) @a = convert(x) end def convert(x) ret = [] maxnum = x.max { |a, b| a.length <=> b.length}.length z = x[0..-1] for l in 1..(maxnum) do y = x[0..-1] while y.empty? == false do bb = y[0] y = y.reject { |m| m==bb } if (bb.length < l) then next end aa=Array.new(l,1) if(l<bb.length+1) then aa=aa.concat(Array.new(bb.length-l,0)) end s=check(x,aa,bb) if s.length < 3 then next end if (s.min..s.max).to_a.all?{ |j| s.include?(j)} then pat = Pattern.new(aa,bb,s.min,s.max) ret << pat for xx in s.min..s.max do k=[] for n in 0..aa.length-1 do k[n] = aa[n]*xx+bb[n] end y.delete(k) z.delete(k) end end end end for k in 0..z.length-1 do aa = Array.new(z[k].length,0) bb = z[k] pat = Pattern.new(aa,bb,0,0) ret << pat end return ret end def check(y,a,b) ret = [] if a.length != b.length then return ret end for i in 0..(y.length-1) do $num+=1 x=(y[i][0]-b[0])/a[0] if y[i].length != a.length then next end if (0..a.length-1).to_a.all?{ |j| y[i][j]==a[j]*x+b[j] } then ret << x end end return ret end def to_s s = "[" for i in 0..(@a.length-1) do s += @a[i].to_s + "," end s = s.chop s += "]" return s end end class Pattern @a = [] @b = [] @first = 0 @last = 0 def initialize(a,b,first,last) @a = a[0..-1] @b = b[0..-1] @first = first @last = last end def to_s s = "{[" for i in 0..(@a.length-1) do s += @a[i].to_s + "x+" + @b[i].to_s + "," end s=s.chop s += "]:" s += @first.to_s + "<=x<=" + @last.to_s + "}" if @first == @last then s = @b.to_s end return s end end def lose(a) if a.length == 1 && a[0] == 1 then if $xx.include?([1]) == false then $xx << [1] $yy << [1] end return true end if $xx.include?(a) == true then return $yy.include?(a) end ret = true for i in 0..(a.length-1) do for j in 1..a[i] do if i==0 && j==1 then next end b=a[0..-1] for k in i..(a.length-1) do $num+=1 if a[k] >= j then b[k] = j -1 end end if j==1 then b=a[0..i-1] end if ($xx.include?(b) == false) then if lose(b) then ret = false end else if $yy.include?(b) then ret = false end end end end if ret == true then $yy << a end $xx << a return ret end m=3 n=16 #x = Array.new(m,n) x=[20,24,4] lose(x) print "$num = " p $num p $xx.length p $yy.length for i in 1..m do tt = $yy.select { |n| n.length == i } print "count=" print tt.length print " " p tt end set = Patterns.new($yy) puts set.to_s
Standard input is empty
$num = 85186
1384
54
count=1 [[1]]
count=23 [[2, 1], [3, 2], [4, 3], [5, 4], [6, 5], [7, 6], [8, 7], [9, 8], [10, 9], [11, 10], [12, 11], [13, 12], [14, 13], [15, 14], [16, 15], [17, 16], [18, 17], [19, 18], [20, 19], [21, 20], [22, 21], [23, 22], [24, 23]]
count=30 [[2, 2, 1], [3, 1, 1], [4, 2, 2], [5, 3, 2], [5, 5, 3], [6, 3, 3], [6, 4, 2], [7, 4, 3], [7, 5, 2], [7, 7, 4], [8, 4, 4], [8, 6, 2], [9, 5, 4], [9, 7, 2], [10, 6, 4], [10, 8, 2], [11, 9, 2], [12, 10, 2], [13, 11, 2], [14, 12, 2], [15, 13, 2], [16, 14, 2], [17, 15, 2], [18, 16, 2], [19, 17, 2], [20, 18, 2], [21, 19, 2], [22, 20, 2], [23, 21, 2], [24, 22, 2]]
[{[1x+2,1x+1]:0<=x<=22},{[1x+4,1x+2,0x+2]:0<=x<=20},{[1x+8,1x+4,0x+4]:0<=x<=2},[1],[2, 2, 1],[3, 1, 1],[5, 5, 3],[6, 3, 3],[7, 4, 3],[7, 7, 4]]